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>
93 #include <netinet/ip_options.h>
95 #include <netinet/pim.h>
96 #include <netinet/pim_var.h>
98 #include <netinet/udp.h>
99 #include <machine/in_cksum.h>
102 * Control debugging code for rsvp and multicast routing code.
103 * Can only set them with the debugger.
105 static u_int rsvpdebug; /* non-zero enables debugging */
107 static u_int mrtdebug; /* any set of the flags below */
108 #define DEBUG_MFC 0x02
109 #define DEBUG_FORWARD 0x04
110 #define DEBUG_EXPIRE 0x08
111 #define DEBUG_XMIT 0x10
112 #define DEBUG_PIM 0x20
114 #define VIFI_INVALID ((vifi_t) -1)
116 #define M_HASCL(m) ((m)->m_flags & M_EXT)
118 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
121 * Locking. We use two locks: one for the virtual interface table and
122 * one for the forwarding table. These locks may be nested in which case
123 * the VIF lock must always be taken first. Note that each lock is used
124 * to cover not only the specific data structure but also related data
125 * structures. It may be better to add more fine-grained locking later;
126 * it's not clear how performance-critical this code is.
129 static struct mrtstat mrtstat;
130 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
132 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
134 static struct mfc *mfctable[MFCTBLSIZ];
135 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
136 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
137 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
139 static struct mtx mfc_mtx;
140 #define MFC_LOCK() mtx_lock(&mfc_mtx)
141 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
142 #define MFC_LOCK_ASSERT() do { \
143 mtx_assert(&mfc_mtx, MA_OWNED); \
144 NET_ASSERT_GIANT(); \
146 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
147 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
149 static struct vif viftable[MAXVIFS];
150 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
151 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
152 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
154 static struct mtx vif_mtx;
155 #define VIF_LOCK() mtx_lock(&vif_mtx)
156 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
157 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
158 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
159 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
161 static u_char nexpire[MFCTBLSIZ];
163 static struct callout expire_upcalls_ch;
165 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
166 #define UPCALL_EXPIRE 6 /* number of timeouts */
169 * Define the token bucket filter structures
170 * tbftable -> each vif has one of these for storing info
173 static struct tbf tbftable[MAXVIFS];
174 #define TBF_REPROCESS (hz / 100) /* 100x / second */
177 * 'Interfaces' associated with decapsulator (so we can tell
178 * packets that went through it from ones that get reflected
179 * by a broken gateway). These interfaces are never linked into
180 * the system ifnet list & no routes point to them. I.e., packets
181 * can't be sent this way. They only exist as a placeholder for
182 * multicast source verification.
184 static struct ifnet multicast_decap_if[MAXVIFS];
187 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
189 /* prototype IP hdr for encapsulated packets */
190 static struct ip multicast_encap_iphdr = {
191 #if BYTE_ORDER == LITTLE_ENDIAN
192 sizeof(struct ip) >> 2, IPVERSION,
194 IPVERSION, sizeof(struct ip) >> 2,
197 sizeof(struct ip), /* total length */
200 ENCAP_TTL, ENCAP_PROTO,
205 * Bandwidth meter variables and constants
207 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
209 * Pending timeouts are stored in a hash table, the key being the
210 * expiration time. Periodically, the entries are analysed and processed.
212 #define BW_METER_BUCKETS 1024
213 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
214 static struct callout bw_meter_ch;
215 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
218 * Pending upcalls are stored in a vector which is flushed when
219 * full, or periodically
221 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
222 static u_int bw_upcalls_n; /* # of pending upcalls */
223 static struct callout bw_upcalls_ch;
224 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
227 static struct pimstat pimstat;
228 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
230 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
233 * Note: the PIM Register encapsulation adds the following in front of a
236 * struct pim_encap_hdr {
238 * struct pim_encap_pimhdr pim;
243 struct pim_encap_pimhdr {
248 static struct ip pim_encap_iphdr = {
249 #if BYTE_ORDER == LITTLE_ENDIAN
250 sizeof(struct ip) >> 2,
254 sizeof(struct ip) >> 2,
257 sizeof(struct ip), /* total length */
265 static struct pim_encap_pimhdr pim_encap_pimhdr = {
267 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
274 static struct ifnet multicast_register_if;
275 static vifi_t reg_vif_num = VIFI_INVALID;
281 static vifi_t numvifs;
282 static const struct encaptab *encap_cookie;
285 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
286 * given a datagram's src ip address.
288 static u_long last_encap_src;
289 static struct vif *last_encap_vif;
292 * Callout for queue processing.
294 static struct callout tbf_reprocess_ch;
296 static u_long X_ip_mcast_src(int vifi);
297 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
298 struct mbuf *m, struct ip_moptions *imo);
299 static int X_ip_mrouter_done(void);
300 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
301 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
302 static int X_legal_vif_num(int vif);
303 static int X_mrt_ioctl(int cmd, caddr_t data);
305 static int get_sg_cnt(struct sioc_sg_req *);
306 static int get_vif_cnt(struct sioc_vif_req *);
307 static int ip_mrouter_init(struct socket *, int);
308 static int add_vif(struct vifctl *);
309 static int del_vif(vifi_t);
310 static int add_mfc(struct mfcctl2 *);
311 static int del_mfc(struct mfcctl2 *);
312 static int set_api_config(uint32_t *); /* chose API capabilities */
313 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
314 static int set_assert(int);
315 static void expire_upcalls(void *);
316 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
317 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
318 static void encap_send(struct ip *, struct vif *, struct mbuf *);
319 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
320 static void tbf_queue(struct vif *, struct mbuf *);
321 static void tbf_process_q(struct vif *);
322 static void tbf_reprocess_q(void *);
323 static int tbf_dq_sel(struct vif *, struct ip *);
324 static void tbf_send_packet(struct vif *, struct mbuf *);
325 static void tbf_update_tokens(struct vif *);
326 static int priority(struct vif *, struct ip *);
329 * Bandwidth monitoring
331 static void free_bw_list(struct bw_meter *list);
332 static int add_bw_upcall(struct bw_upcall *);
333 static int del_bw_upcall(struct bw_upcall *);
334 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
335 struct timeval *nowp);
336 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
337 static void bw_upcalls_send(void);
338 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
339 static void unschedule_bw_meter(struct bw_meter *x);
340 static void bw_meter_process(void);
341 static void expire_bw_upcalls_send(void *);
342 static void expire_bw_meter_process(void *);
345 static int pim_register_send(struct ip *, struct vif *,
346 struct mbuf *, struct mfc *);
347 static int pim_register_send_rp(struct ip *, struct vif *,
348 struct mbuf *, struct mfc *);
349 static int pim_register_send_upcall(struct ip *, struct vif *,
350 struct mbuf *, struct mfc *);
351 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
355 * whether or not special PIM assert processing is enabled.
357 static int pim_assert;
359 * Rate limit for assert notification messages, in usec
361 #define ASSERT_MSG_TIME 3000000
364 * Kernel multicast routing API capabilities and setup.
365 * If more API capabilities are added to the kernel, they should be
366 * recorded in `mrt_api_support'.
368 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
369 MRT_MFC_FLAGS_BORDER_VIF |
372 static uint32_t mrt_api_config = 0;
375 * Hash function for a source, group entry
377 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
378 ((g) >> 20) ^ ((g) >> 10) ^ (g))
381 * Find a route for a given origin IP address and Multicast group address
382 * Type of service parameter to be added in the future!!!
383 * Statistics are updated by the caller if needed
384 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
387 mfc_find(in_addr_t o, in_addr_t g)
393 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
394 if ((rt->mfc_origin.s_addr == o) &&
395 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
401 * Macros to compute elapsed time efficiently
402 * Borrowed from Van Jacobson's scheduling code
404 #define TV_DELTA(a, b, delta) { \
406 delta = (a).tv_usec - (b).tv_usec; \
407 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
416 delta += (1000000 * xxs); \
421 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
422 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
425 * Handle MRT setsockopt commands to modify the multicast routing tables.
428 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
434 struct bw_upcall bw_upcall;
437 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
441 switch (sopt->sopt_name) {
443 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
446 error = ip_mrouter_init(so, optval);
450 error = ip_mrouter_done();
454 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
457 error = add_vif(&vifc);
461 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
464 error = del_vif(vifi);
470 * select data size depending on API version.
472 if (sopt->sopt_name == MRT_ADD_MFC &&
473 mrt_api_config & MRT_API_FLAGS_ALL) {
474 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
475 sizeof(struct mfcctl2));
477 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
478 sizeof(struct mfcctl));
479 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
480 sizeof(mfc) - sizeof(struct mfcctl));
484 if (sopt->sopt_name == MRT_ADD_MFC)
485 error = add_mfc(&mfc);
487 error = del_mfc(&mfc);
491 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
498 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
500 error = set_api_config(&i);
502 error = sooptcopyout(sopt, &i, sizeof i);
505 case MRT_ADD_BW_UPCALL:
506 case MRT_DEL_BW_UPCALL:
507 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
511 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
512 error = add_bw_upcall(&bw_upcall);
514 error = del_bw_upcall(&bw_upcall);
525 * Handle MRT getsockopt commands
528 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
531 static int version = 0x0305; /* !!! why is this here? XXX */
533 switch (sopt->sopt_name) {
535 error = sooptcopyout(sopt, &version, sizeof version);
539 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
542 case MRT_API_SUPPORT:
543 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
547 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
558 * Handle ioctl commands to obtain information from the cache
561 X_mrt_ioctl(int cmd, caddr_t data)
566 * Currently the only function calling this ioctl routine is rtioctl().
567 * Typically, only root can create the raw socket in order to execute
568 * this ioctl method, however the request might be coming from a prison
570 error = suser(curthread);
574 case (SIOCGETVIFCNT):
575 error = get_vif_cnt((struct sioc_vif_req *)data);
579 error = get_sg_cnt((struct sioc_sg_req *)data);
590 * returns the packet, byte, rpf-failure count for the source group provided
593 get_sg_cnt(struct sioc_sg_req *req)
598 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
601 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
602 return EADDRNOTAVAIL;
604 req->pktcnt = rt->mfc_pkt_cnt;
605 req->bytecnt = rt->mfc_byte_cnt;
606 req->wrong_if = rt->mfc_wrong_if;
612 * returns the input and output packet and byte counts on the vif provided
615 get_vif_cnt(struct sioc_vif_req *req)
617 vifi_t vifi = req->vifi;
620 if (vifi >= numvifs) {
625 req->icount = viftable[vifi].v_pkt_in;
626 req->ocount = viftable[vifi].v_pkt_out;
627 req->ibytes = viftable[vifi].v_bytes_in;
628 req->obytes = viftable[vifi].v_bytes_out;
635 ip_mrouter_reset(void)
637 bzero((caddr_t)mfctable, sizeof(mfctable));
638 bzero((caddr_t)nexpire, sizeof(nexpire));
643 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
646 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
647 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
648 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
650 callout_init(&tbf_reprocess_ch, NET_CALLOUT_MPSAFE);
653 static struct mtx mrouter_mtx; /* used to synch init/done work */
656 * Enable multicast routing
659 ip_mrouter_init(struct socket *so, int version)
662 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
663 so->so_type, so->so_proto->pr_protocol);
665 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
671 mtx_lock(&mrouter_mtx);
673 if (ip_mrouter != NULL) {
674 mtx_unlock(&mrouter_mtx);
678 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
680 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
681 expire_bw_upcalls_send, NULL);
682 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
686 mtx_unlock(&mrouter_mtx);
689 log(LOG_DEBUG, "ip_mrouter_init\n");
695 * Disable multicast routing
698 X_ip_mrouter_done(void)
707 mtx_lock(&mrouter_mtx);
709 if (ip_mrouter == NULL) {
710 mtx_unlock(&mrouter_mtx);
715 * Detach/disable hooks to the reset of the system.
722 const struct encaptab *c = encap_cookie;
728 callout_stop(&tbf_reprocess_ch);
732 * For each phyint in use, disable promiscuous reception of all IP
735 for (vifi = 0; vifi < numvifs; vifi++) {
736 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
737 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
738 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
740 so->sin_len = sizeof(struct sockaddr_in);
741 so->sin_family = AF_INET;
742 so->sin_addr.s_addr = INADDR_ANY;
743 ifp = viftable[vifi].v_ifp;
747 bzero((caddr_t)tbftable, sizeof(tbftable));
748 bzero((caddr_t)viftable, sizeof(viftable));
754 * Free all multicast forwarding cache entries.
756 callout_stop(&expire_upcalls_ch);
757 callout_stop(&bw_upcalls_ch);
758 callout_stop(&bw_meter_ch);
761 for (i = 0; i < MFCTBLSIZ; i++) {
762 for (rt = mfctable[i]; rt != NULL; ) {
763 struct mfc *nr = rt->mfc_next;
765 for (rte = rt->mfc_stall; rte != NULL; ) {
766 struct rtdetq *n = rte->next;
769 free(rte, M_MRTABLE);
772 free_bw_list(rt->mfc_bw_meter);
777 bzero((caddr_t)mfctable, sizeof(mfctable));
778 bzero((caddr_t)nexpire, sizeof(nexpire));
780 bzero(bw_meter_timers, sizeof(bw_meter_timers));
784 * Reset de-encapsulation cache
786 last_encap_src = INADDR_ANY;
787 last_encap_vif = NULL;
789 reg_vif_num = VIFI_INVALID;
792 mtx_unlock(&mrouter_mtx);
795 log(LOG_DEBUG, "ip_mrouter_done\n");
801 * Set PIM assert processing global
806 if ((i != 1) && (i != 0))
815 * Configure API capabilities
818 set_api_config(uint32_t *apival)
823 * We can set the API capabilities only if it is the first operation
824 * after MRT_INIT. I.e.:
825 * - there are no vifs installed
826 * - pim_assert is not enabled
827 * - the MFC table is empty
837 for (i = 0; i < MFCTBLSIZ; i++) {
838 if (mfctable[i] != NULL) {
844 mrt_api_config = *apival & mrt_api_support;
845 *apival = mrt_api_config;
851 * Decide if a packet is from a tunnelled peer.
852 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
855 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
857 struct ip *ip = mtod(m, struct ip *);
858 int hlen = ip->ip_hl << 2;
861 * don't claim the packet if it's not to a multicast destination or if
862 * we don't have an encapsulating tunnel with the source.
863 * Note: This code assumes that the remote site IP address
864 * uniquely identifies the tunnel (i.e., that this site has
865 * at most one tunnel with the remote site).
867 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
869 if (ip->ip_src.s_addr != last_encap_src) {
870 struct vif *vifp = viftable;
871 struct vif *vife = vifp + numvifs;
873 last_encap_src = ip->ip_src.s_addr;
874 last_encap_vif = NULL;
875 for ( ; vifp < vife; ++vifp)
876 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
877 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
878 last_encap_vif = vifp;
882 if (last_encap_vif == NULL) {
883 last_encap_src = INADDR_ANY;
890 * De-encapsulate a packet and feed it back through ip input (this
891 * routine is called whenever IP gets a packet that mroute_encap_func()
895 mroute_encap_input(struct mbuf *m, int off)
897 struct ip *ip = mtod(m, struct ip *);
898 int hlen = ip->ip_hl << 2;
900 if (hlen > sizeof(struct ip))
901 ip_stripoptions(m, (struct mbuf *) 0);
902 m->m_data += sizeof(struct ip);
903 m->m_len -= sizeof(struct ip);
904 m->m_pkthdr.len -= sizeof(struct ip);
906 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
908 netisr_queue(NETISR_IP, m); /* mbuf is free'd on failure. */
910 * normally we would need a "schednetisr(NETISR_IP)"
911 * here but we were called by ip_input and it is going
912 * to loop back & try to dequeue the packet we just
913 * queued as soon as we return so we avoid the
914 * unnecessary software interrrupt.
917 * This no longer holds - we may have direct-dispatched the packet,
918 * or there may be a queue processing limit.
922 extern struct domain inetdomain;
923 static struct protosw mroute_encap_protosw =
926 .pr_domain = &inetdomain,
927 .pr_protocol = IPPROTO_IPV4,
928 .pr_flags = PR_ATOMIC|PR_ADDR,
929 .pr_input = mroute_encap_input,
930 .pr_ctloutput = rip_ctloutput,
931 .pr_usrreqs = &rip_usrreqs
935 * Add a vif to the vif table
938 add_vif(struct vifctl *vifcp)
940 struct vif *vifp = viftable + vifcp->vifc_vifi;
941 struct sockaddr_in sin = {sizeof sin, AF_INET};
945 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
948 if (vifcp->vifc_vifi >= MAXVIFS) {
952 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
956 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
958 return EADDRNOTAVAIL;
961 /* Find the interface with an address in AF_INET family */
963 if (vifcp->vifc_flags & VIFF_REGISTER) {
965 * XXX: Because VIFF_REGISTER does not really need a valid
966 * local interface (e.g. it could be 127.0.0.2), we don't
973 sin.sin_addr = vifcp->vifc_lcl_addr;
974 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
977 return EADDRNOTAVAIL;
982 if (vifcp->vifc_flags & VIFF_TUNNEL) {
983 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
985 * An encapsulating tunnel is wanted. Tell
986 * mroute_encap_input() to start paying attention
987 * to encapsulated packets.
989 if (encap_cookie == NULL) {
992 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
994 (struct protosw *)&mroute_encap_protosw, NULL);
996 if (encap_cookie == NULL) {
997 printf("ip_mroute: unable to attach encap\n");
999 return EIO; /* XXX */
1001 for (i = 0; i < MAXVIFS; ++i) {
1002 if_initname(&multicast_decap_if[i], "mdecap", i);
1006 * Set interface to fake encapsulator interface
1008 ifp = &multicast_decap_if[vifcp->vifc_vifi];
1010 * Prepare cached route entry
1012 bzero(&vifp->v_route, sizeof(vifp->v_route));
1014 log(LOG_ERR, "source routed tunnels not supported\n");
1019 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
1020 ifp = &multicast_register_if;
1022 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
1023 (void *)&multicast_register_if);
1024 if (reg_vif_num == VIFI_INVALID) {
1025 if_initname(&multicast_register_if, "register_vif", 0);
1026 multicast_register_if.if_flags = IFF_LOOPBACK;
1027 bzero(&vifp->v_route, sizeof(vifp->v_route));
1028 reg_vif_num = vifcp->vifc_vifi;
1031 } else { /* Make sure the interface supports multicast */
1032 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1037 /* Enable promiscuous reception of all IP multicasts from the if */
1038 error = if_allmulti(ifp, 1);
1045 /* define parameters for the tbf structure */
1046 vifp->v_tbf = v_tbf;
1047 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
1048 vifp->v_tbf->tbf_n_tok = 0;
1049 vifp->v_tbf->tbf_q_len = 0;
1050 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
1051 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
1053 vifp->v_flags = vifcp->vifc_flags;
1054 vifp->v_threshold = vifcp->vifc_threshold;
1055 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1056 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1058 /* scaling up here allows division by 1024 in critical code */
1059 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
1060 vifp->v_rsvp_on = 0;
1061 vifp->v_rsvpd = NULL;
1062 /* initialize per vif pkt counters */
1064 vifp->v_pkt_out = 0;
1065 vifp->v_bytes_in = 0;
1066 vifp->v_bytes_out = 0;
1068 /* Adjust numvifs up if the vifi is higher than numvifs */
1069 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
1074 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
1076 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
1077 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1078 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
1079 vifcp->vifc_threshold,
1080 vifcp->vifc_rate_limit);
1086 * Delete a vif from the vif table
1089 del_vif(vifi_t vifi)
1095 if (vifi >= numvifs) {
1099 vifp = &viftable[vifi];
1100 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1102 return EADDRNOTAVAIL;
1105 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1106 if_allmulti(vifp->v_ifp, 0);
1108 if (vifp == last_encap_vif) {
1109 last_encap_vif = NULL;
1110 last_encap_src = INADDR_ANY;
1114 * Free packets queued at the interface
1116 while (vifp->v_tbf->tbf_q) {
1117 struct mbuf *m = vifp->v_tbf->tbf_q;
1119 vifp->v_tbf->tbf_q = m->m_act;
1124 if (vifp->v_flags & VIFF_REGISTER)
1125 reg_vif_num = VIFI_INVALID;
1128 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
1129 bzero((caddr_t)vifp, sizeof (*vifp));
1132 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1134 /* Adjust numvifs down */
1135 for (vifi = numvifs; vifi > 0; vifi--)
1136 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1146 * update an mfc entry without resetting counters and S,G addresses.
1149 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1153 rt->mfc_parent = mfccp->mfcc_parent;
1154 for (i = 0; i < numvifs; i++) {
1155 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1156 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1159 /* set the RP address */
1160 if (mrt_api_config & MRT_MFC_RP)
1161 rt->mfc_rp = mfccp->mfcc_rp;
1163 rt->mfc_rp.s_addr = INADDR_ANY;
1167 * fully initialize an mfc entry from the parameter.
1170 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1172 rt->mfc_origin = mfccp->mfcc_origin;
1173 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1175 update_mfc_params(rt, mfccp);
1177 /* initialize pkt counters per src-grp */
1178 rt->mfc_pkt_cnt = 0;
1179 rt->mfc_byte_cnt = 0;
1180 rt->mfc_wrong_if = 0;
1181 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1189 add_mfc(struct mfcctl2 *mfccp)
1199 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1201 /* If an entry already exists, just update the fields */
1203 if (mrtdebug & DEBUG_MFC)
1204 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1205 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1206 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1207 mfccp->mfcc_parent);
1209 update_mfc_params(rt, mfccp);
1216 * Find the entry for which the upcall was made and update
1218 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1219 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1221 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1222 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1223 (rt->mfc_stall != NULL)) {
1226 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1227 "multiple kernel entries",
1228 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1229 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1230 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1232 if (mrtdebug & DEBUG_MFC)
1233 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1234 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1235 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1236 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1238 init_mfc_params(rt, mfccp);
1240 rt->mfc_expire = 0; /* Don't clean this guy up */
1243 /* free packets Qed at the end of this entry */
1244 for (rte = rt->mfc_stall; rte != NULL; ) {
1245 struct rtdetq *n = rte->next;
1247 ip_mdq(rte->m, rte->ifp, rt, -1);
1249 free(rte, M_MRTABLE);
1252 rt->mfc_stall = NULL;
1257 * It is possible that an entry is being inserted without an upcall
1260 if (mrtdebug & DEBUG_MFC)
1261 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1262 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1263 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1264 mfccp->mfcc_parent);
1266 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1267 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1268 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1269 init_mfc_params(rt, mfccp);
1276 if (rt == NULL) { /* no upcall, so make a new entry */
1277 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1284 init_mfc_params(rt, mfccp);
1286 rt->mfc_stall = NULL;
1288 rt->mfc_bw_meter = NULL;
1289 /* insert new entry at head of hash chain */
1290 rt->mfc_next = mfctable[hash];
1291 mfctable[hash] = rt;
1300 * Delete an mfc entry
1303 del_mfc(struct mfcctl2 *mfccp)
1305 struct in_addr origin;
1306 struct in_addr mcastgrp;
1310 struct bw_meter *list;
1312 origin = mfccp->mfcc_origin;
1313 mcastgrp = mfccp->mfcc_mcastgrp;
1315 if (mrtdebug & DEBUG_MFC)
1316 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1317 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1321 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1322 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1323 if (origin.s_addr == rt->mfc_origin.s_addr &&
1324 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1325 rt->mfc_stall == NULL)
1329 return EADDRNOTAVAIL;
1332 *nptr = rt->mfc_next;
1335 * free the bw_meter entries
1337 list = rt->mfc_bw_meter;
1338 rt->mfc_bw_meter = NULL;
1340 free(rt, M_MRTABLE);
1350 * Send a message to mrouted on the multicast routing socket
1353 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1356 SOCKBUF_LOCK(&s->so_rcv);
1357 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1359 sorwakeup_locked(s);
1362 SOCKBUF_UNLOCK(&s->so_rcv);
1369 * IP multicast forwarding function. This function assumes that the packet
1370 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1371 * pointed to by "ifp", and the packet is to be relayed to other networks
1372 * that have members of the packet's destination IP multicast group.
1374 * The packet is returned unscathed to the caller, unless it is
1375 * erroneous, in which case a non-zero return value tells the caller to
1379 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1382 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1383 struct ip_moptions *imo)
1389 if (mrtdebug & DEBUG_FORWARD)
1390 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1391 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1394 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1395 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1397 * Packet arrived via a physical interface or
1398 * an encapsulated tunnel or a register_vif.
1402 * Packet arrived through a source-route tunnel.
1403 * Source-route tunnels are no longer supported.
1405 static int last_log;
1406 if (last_log != time_uptime) {
1407 last_log = time_uptime;
1409 "ip_mforward: received source-routed packet from %lx\n",
1410 (u_long)ntohl(ip->ip_src.s_addr));
1417 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1418 if (ip->ip_ttl < 255)
1419 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1420 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1421 struct vif *vifp = viftable + vifi;
1423 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1424 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1426 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1427 vifp->v_ifp->if_xname);
1429 error = ip_mdq(m, ifp, NULL, vifi);
1434 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1435 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1436 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1438 printf("In fact, no options were specified at all\n");
1442 * Don't forward a packet with time-to-live of zero or one,
1443 * or a packet destined to a local-only group.
1445 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1452 * Determine forwarding vifs from the forwarding cache table
1454 ++mrtstat.mrts_mfc_lookups;
1455 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1457 /* Entry exists, so forward if necessary */
1459 error = ip_mdq(m, ifp, rt, -1);
1465 * If we don't have a route for packet's origin,
1466 * Make a copy of the packet & send message to routing daemon
1472 int hlen = ip->ip_hl << 2;
1474 ++mrtstat.mrts_mfc_misses;
1476 mrtstat.mrts_no_route++;
1477 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1478 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1479 (u_long)ntohl(ip->ip_src.s_addr),
1480 (u_long)ntohl(ip->ip_dst.s_addr));
1483 * Allocate mbufs early so that we don't do extra work if we are
1484 * just going to fail anyway. Make sure to pullup the header so
1485 * that other people can't step on it.
1487 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1493 mb0 = m_copypacket(m, M_DONTWAIT);
1494 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1495 mb0 = m_pullup(mb0, hlen);
1497 free(rte, M_MRTABLE);
1503 /* is there an upcall waiting for this flow ? */
1504 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1505 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1506 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1507 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1508 (rt->mfc_stall != NULL))
1515 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1519 * Locate the vifi for the incoming interface for this packet.
1520 * If none found, drop packet.
1522 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1524 if (vifi >= numvifs) /* vif not found, drop packet */
1527 /* no upcall, so make a new entry */
1528 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1531 /* Make a copy of the header to send to the user level process */
1532 mm = m_copy(mb0, 0, hlen);
1537 * Send message to routing daemon to install
1538 * a route into the kernel table
1541 im = mtod(mm, struct igmpmsg *);
1542 im->im_msgtype = IGMPMSG_NOCACHE;
1546 mrtstat.mrts_upcalls++;
1548 k_igmpsrc.sin_addr = ip->ip_src;
1549 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1550 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1551 ++mrtstat.mrts_upq_sockfull;
1553 free(rt, M_MRTABLE);
1555 free(rte, M_MRTABLE);
1562 /* insert new entry at head of hash chain */
1563 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1564 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1565 rt->mfc_expire = UPCALL_EXPIRE;
1567 for (i = 0; i < numvifs; i++) {
1568 rt->mfc_ttls[i] = 0;
1569 rt->mfc_flags[i] = 0;
1571 rt->mfc_parent = -1;
1573 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1575 rt->mfc_bw_meter = NULL;
1577 /* link into table */
1578 rt->mfc_next = mfctable[hash];
1579 mfctable[hash] = rt;
1580 rt->mfc_stall = rte;
1583 /* determine if q has overflowed */
1588 * XXX ouch! we need to append to the list, but we
1589 * only have a pointer to the front, so we have to
1590 * scan the entire list every time.
1592 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1595 if (npkts > MAX_UPQ) {
1596 mrtstat.mrts_upq_ovflw++;
1598 free(rte, M_MRTABLE);
1605 /* Add this entry to the end of the queue */
1621 * Clean up the cache entry if upcall is not serviced
1624 expire_upcalls(void *unused)
1627 struct mfc *mfc, **nptr;
1631 for (i = 0; i < MFCTBLSIZ; i++) {
1632 if (nexpire[i] == 0)
1634 nptr = &mfctable[i];
1635 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1637 * Skip real cache entries
1638 * Make sure it wasn't marked to not expire (shouldn't happen)
1641 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1642 --mfc->mfc_expire == 0) {
1643 if (mrtdebug & DEBUG_EXPIRE)
1644 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1645 (u_long)ntohl(mfc->mfc_origin.s_addr),
1646 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1648 * drop all the packets
1649 * free the mbuf with the pkt, if, timing info
1651 for (rte = mfc->mfc_stall; rte; ) {
1652 struct rtdetq *n = rte->next;
1655 free(rte, M_MRTABLE);
1658 ++mrtstat.mrts_cache_cleanups;
1662 * free the bw_meter entries
1664 while (mfc->mfc_bw_meter != NULL) {
1665 struct bw_meter *x = mfc->mfc_bw_meter;
1667 mfc->mfc_bw_meter = x->bm_mfc_next;
1671 *nptr = mfc->mfc_next;
1672 free(mfc, M_MRTABLE);
1674 nptr = &mfc->mfc_next;
1680 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1684 * Packet forwarding routine once entry in the cache is made
1687 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1689 struct ip *ip = mtod(m, struct ip *);
1691 int plen = ip->ip_len;
1695 * Macro to send packet on vif. Since RSVP packets don't get counted on
1696 * input, they shouldn't get counted on output, so statistics keeping is
1699 #define MC_SEND(ip,vifp,m) { \
1700 if ((vifp)->v_flags & VIFF_TUNNEL) \
1701 encap_send((ip), (vifp), (m)); \
1703 phyint_send((ip), (vifp), (m)); \
1707 * If xmt_vif is not -1, send on only the requested vif.
1709 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1711 if (xmt_vif < numvifs) {
1713 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1714 pim_register_send(ip, viftable + xmt_vif, m, rt);
1717 MC_SEND(ip, viftable + xmt_vif, m);
1722 * Don't forward if it didn't arrive from the parent vif for its origin.
1724 vifi = rt->mfc_parent;
1725 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1726 /* came in the wrong interface */
1727 if (mrtdebug & DEBUG_FORWARD)
1728 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1729 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1730 ++mrtstat.mrts_wrong_if;
1733 * If we are doing PIM assert processing, send a message
1734 * to the routing daemon.
1736 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1737 * can complete the SPT switch, regardless of the type
1738 * of the iif (broadcast media, GRE tunnel, etc).
1740 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1745 if (ifp == &multicast_register_if)
1746 pimstat.pims_rcv_registers_wrongiif++;
1749 /* Get vifi for the incoming packet */
1750 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1752 if (vifi >= numvifs)
1753 return 0; /* The iif is not found: ignore the packet. */
1755 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1756 return 0; /* WRONGVIF disabled: ignore the packet */
1760 TV_DELTA(now, rt->mfc_last_assert, delta);
1762 if (delta > ASSERT_MSG_TIME) {
1763 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1765 int hlen = ip->ip_hl << 2;
1766 struct mbuf *mm = m_copy(m, 0, hlen);
1768 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1769 mm = m_pullup(mm, hlen);
1773 rt->mfc_last_assert = now;
1775 im = mtod(mm, struct igmpmsg *);
1776 im->im_msgtype = IGMPMSG_WRONGVIF;
1780 mrtstat.mrts_upcalls++;
1782 k_igmpsrc.sin_addr = im->im_src;
1783 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1785 "ip_mforward: ip_mrouter socket queue full\n");
1786 ++mrtstat.mrts_upq_sockfull;
1794 /* If I sourced this packet, it counts as output, else it was input. */
1795 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1796 viftable[vifi].v_pkt_out++;
1797 viftable[vifi].v_bytes_out += plen;
1799 viftable[vifi].v_pkt_in++;
1800 viftable[vifi].v_bytes_in += plen;
1803 rt->mfc_byte_cnt += plen;
1806 * For each vif, decide if a copy of the packet should be forwarded.
1808 * - the ttl exceeds the vif's threshold
1809 * - there are group members downstream on interface
1811 for (vifi = 0; vifi < numvifs; vifi++)
1812 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1813 viftable[vifi].v_pkt_out++;
1814 viftable[vifi].v_bytes_out += plen;
1816 if (viftable[vifi].v_flags & VIFF_REGISTER)
1817 pim_register_send(ip, viftable + vifi, m, rt);
1820 MC_SEND(ip, viftable+vifi, m);
1824 * Perform upcall-related bw measuring.
1826 if (rt->mfc_bw_meter != NULL) {
1832 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1833 bw_meter_receive_packet(x, plen, &now);
1840 * check if a vif number is legal/ok. This is used by ip_output.
1843 X_legal_vif_num(int vif)
1845 /* XXX unlocked, matter? */
1846 return (vif >= 0 && vif < numvifs);
1850 * Return the local address used by this vif
1853 X_ip_mcast_src(int vifi)
1855 /* XXX unlocked, matter? */
1856 if (vifi >= 0 && vifi < numvifs)
1857 return viftable[vifi].v_lcl_addr.s_addr;
1863 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1865 struct mbuf *mb_copy;
1866 int hlen = ip->ip_hl << 2;
1871 * Make a new reference to the packet; make sure that
1872 * the IP header is actually copied, not just referenced,
1873 * so that ip_output() only scribbles on the copy.
1875 mb_copy = m_copypacket(m, M_DONTWAIT);
1876 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1877 mb_copy = m_pullup(mb_copy, hlen);
1878 if (mb_copy == NULL)
1881 if (vifp->v_rate_limit == 0)
1882 tbf_send_packet(vifp, mb_copy);
1884 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1888 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1890 struct mbuf *mb_copy;
1892 int i, len = ip->ip_len;
1896 /* Take care of delayed checksums */
1897 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1898 in_delayed_cksum(m);
1899 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1903 * copy the old packet & pullup its IP header into the
1904 * new mbuf so we can modify it. Try to fill the new
1905 * mbuf since if we don't the ethernet driver will.
1907 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1908 if (mb_copy == NULL)
1911 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1913 mb_copy->m_data += max_linkhdr;
1914 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1916 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1920 i = MHLEN - M_LEADINGSPACE(mb_copy);
1923 mb_copy = m_pullup(mb_copy, i);
1924 if (mb_copy == NULL)
1926 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1929 * fill in the encapsulating IP header.
1931 ip_copy = mtod(mb_copy, struct ip *);
1932 *ip_copy = multicast_encap_iphdr;
1933 ip_copy->ip_id = ip_newid();
1934 ip_copy->ip_len += len;
1935 ip_copy->ip_src = vifp->v_lcl_addr;
1936 ip_copy->ip_dst = vifp->v_rmt_addr;
1939 * turn the encapsulated IP header back into a valid one.
1941 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1943 ip->ip_len = htons(ip->ip_len);
1944 ip->ip_off = htons(ip->ip_off);
1946 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1947 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1948 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1950 if (vifp->v_rate_limit == 0)
1951 tbf_send_packet(vifp, mb_copy);
1953 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1957 * Token bucket filter module
1961 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
1963 struct tbf *t = vifp->v_tbf;
1967 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
1968 mrtstat.mrts_pkt2large++;
1973 tbf_update_tokens(vifp);
1975 if (t->tbf_q_len == 0) { /* queue empty... */
1976 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
1977 t->tbf_n_tok -= p_len;
1978 tbf_send_packet(vifp, m);
1979 } else { /* no, queue packet and try later */
1981 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS,
1982 tbf_reprocess_q, vifp);
1984 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1985 /* finite queue length, so queue pkts and process queue */
1987 tbf_process_q(vifp);
1989 /* queue full, try to dq and queue and process */
1990 if (!tbf_dq_sel(vifp, ip)) {
1991 mrtstat.mrts_q_overflow++;
1995 tbf_process_q(vifp);
2001 * adds a packet to the queue at the interface
2004 tbf_queue(struct vif *vifp, struct mbuf *m)
2006 struct tbf *t = vifp->v_tbf;
2010 if (t->tbf_t == NULL) /* Queue was empty */
2012 else /* Insert at tail */
2013 t->tbf_t->m_act = m;
2015 t->tbf_t = m; /* Set new tail pointer */
2018 /* Make sure we didn't get fed a bogus mbuf */
2020 panic("tbf_queue: m_act");
2028 * processes the queue at the interface
2031 tbf_process_q(struct vif *vifp)
2033 struct tbf *t = vifp->v_tbf;
2037 /* loop through the queue at the interface and send as many packets
2040 while (t->tbf_q_len > 0) {
2041 struct mbuf *m = t->tbf_q;
2042 int len = mtod(m, struct ip *)->ip_len;
2044 /* determine if the packet can be sent */
2045 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
2047 /* ok, reduce no of tokens, dequeue and send the packet. */
2048 t->tbf_n_tok -= len;
2050 t->tbf_q = m->m_act;
2051 if (--t->tbf_q_len == 0)
2055 tbf_send_packet(vifp, m);
2060 tbf_reprocess_q(void *xvifp)
2062 struct vif *vifp = xvifp;
2064 if (ip_mrouter == NULL)
2067 tbf_update_tokens(vifp);
2068 tbf_process_q(vifp);
2069 if (vifp->v_tbf->tbf_q_len)
2070 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS, tbf_reprocess_q, vifp);
2074 /* function that will selectively discard a member of the queue
2075 * based on the precedence value and the priority
2078 tbf_dq_sel(struct vif *vifp, struct ip *ip)
2081 struct mbuf *m, *last;
2083 struct tbf *t = vifp->v_tbf;
2087 p = priority(vifp, ip);
2091 while ((m = *np) != NULL) {
2092 if (p > priority(vifp, mtod(m, struct ip *))) {
2094 /* If we're removing the last packet, fix the tail pointer */
2098 /* It's impossible for the queue to be empty, but check anyways. */
2099 if (--t->tbf_q_len == 0)
2101 mrtstat.mrts_drop_sel++;
2111 tbf_send_packet(struct vif *vifp, struct mbuf *m)
2115 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
2116 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
2118 struct ip_moptions imo;
2120 static struct route ro; /* XXX check this */
2122 imo.imo_multicast_ifp = vifp->v_ifp;
2123 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2124 imo.imo_multicast_loop = 1;
2125 imo.imo_multicast_vif = -1;
2128 * Re-entrancy should not be a problem here, because
2129 * the packets that we send out and are looped back at us
2130 * should get rejected because they appear to come from
2131 * the loopback interface, thus preventing looping.
2133 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
2135 if (mrtdebug & DEBUG_XMIT)
2136 log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
2137 vifp - viftable, error);
2141 /* determine the current time and then
2142 * the elapsed time (between the last time and time now)
2143 * in milliseconds & update the no. of tokens in the bucket
2146 tbf_update_tokens(struct vif *vifp)
2150 struct tbf *t = vifp->v_tbf;
2156 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2159 * This formula is actually
2160 * "time in seconds" * "bytes/second".
2162 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2164 * The (1000/1024) was introduced in add_vif to optimize
2165 * this divide into a shift.
2167 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2168 t->tbf_last_pkt_t = tp;
2170 if (t->tbf_n_tok > MAX_BKT_SIZE)
2171 t->tbf_n_tok = MAX_BKT_SIZE;
2175 priority(struct vif *vifp, struct ip *ip)
2177 int prio = 50; /* the lowest priority -- default case */
2179 /* temporary hack; may add general packet classifier some day */
2182 * The UDP port space is divided up into four priority ranges:
2183 * [0, 16384) : unclassified - lowest priority
2184 * [16384, 32768) : audio - highest priority
2185 * [32768, 49152) : whiteboard - medium priority
2186 * [49152, 65536) : video - low priority
2188 * Everything else gets lowest priority.
2190 if (ip->ip_p == IPPROTO_UDP) {
2191 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2192 switch (ntohs(udp->uh_dport) & 0xc000) {
2208 * End of token bucket filter modifications
2212 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2216 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2219 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2225 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2227 return EADDRNOTAVAIL;
2230 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2231 /* Check if socket is available. */
2232 if (viftable[vifi].v_rsvpd != NULL) {
2237 viftable[vifi].v_rsvpd = so;
2238 /* This may seem silly, but we need to be sure we don't over-increment
2239 * the RSVP counter, in case something slips up.
2241 if (!viftable[vifi].v_rsvp_on) {
2242 viftable[vifi].v_rsvp_on = 1;
2245 } else { /* must be VIF_OFF */
2247 * XXX as an additional consistency check, one could make sure
2248 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2249 * first parameter is pretty useless.
2251 viftable[vifi].v_rsvpd = NULL;
2253 * This may seem silly, but we need to be sure we don't over-decrement
2254 * the RSVP counter, in case something slips up.
2256 if (viftable[vifi].v_rsvp_on) {
2257 viftable[vifi].v_rsvp_on = 0;
2266 X_ip_rsvp_force_done(struct socket *so)
2270 /* Don't bother if it is not the right type of socket. */
2271 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2276 /* The socket may be attached to more than one vif...this
2277 * is perfectly legal.
2279 for (vifi = 0; vifi < numvifs; vifi++) {
2280 if (viftable[vifi].v_rsvpd == so) {
2281 viftable[vifi].v_rsvpd = NULL;
2282 /* This may seem silly, but we need to be sure we don't
2283 * over-decrement the RSVP counter, in case something slips up.
2285 if (viftable[vifi].v_rsvp_on) {
2286 viftable[vifi].v_rsvp_on = 0;
2296 X_rsvp_input(struct mbuf *m, int off)
2299 struct ip *ip = mtod(m, struct ip *);
2300 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2304 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2306 /* Can still get packets with rsvp_on = 0 if there is a local member
2307 * of the group to which the RSVP packet is addressed. But in this
2308 * case we want to throw the packet away.
2316 printf("rsvp_input: check vifs\n");
2322 ifp = m->m_pkthdr.rcvif;
2325 /* Find which vif the packet arrived on. */
2326 for (vifi = 0; vifi < numvifs; vifi++)
2327 if (viftable[vifi].v_ifp == ifp)
2330 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2332 * Drop the lock here to avoid holding it across rip_input.
2333 * This could make rsvpdebug printfs wrong. If you care,
2334 * record the state of stuff before dropping the lock.
2338 * If the old-style non-vif-associated socket is set,
2339 * then use it. Otherwise, drop packet since there
2340 * is no specific socket for this vif.
2342 if (ip_rsvpd != NULL) {
2344 printf("rsvp_input: Sending packet up old-style socket\n");
2345 rip_input(m, off); /* xxx */
2347 if (rsvpdebug && vifi == numvifs)
2348 printf("rsvp_input: Can't find vif for packet.\n");
2349 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2350 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2355 rsvp_src.sin_addr = ip->ip_src;
2358 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2359 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2361 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2363 printf("rsvp_input: Failed to append to socket\n");
2366 printf("rsvp_input: send packet up\n");
2372 * Code for bandwidth monitors
2376 * Define common interface for timeval-related methods
2378 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2379 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2380 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2383 compute_bw_meter_flags(struct bw_upcall *req)
2387 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2388 flags |= BW_METER_UNIT_PACKETS;
2389 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2390 flags |= BW_METER_UNIT_BYTES;
2391 if (req->bu_flags & BW_UPCALL_GEQ)
2392 flags |= BW_METER_GEQ;
2393 if (req->bu_flags & BW_UPCALL_LEQ)
2394 flags |= BW_METER_LEQ;
2400 * Add a bw_meter entry
2403 add_bw_upcall(struct bw_upcall *req)
2406 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2407 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2412 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2415 /* Test if the flags are valid */
2416 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2418 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2420 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2421 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2424 /* Test if the threshold time interval is valid */
2425 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2428 flags = compute_bw_meter_flags(req);
2431 * Find if we have already same bw_meter entry
2434 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2437 return EADDRNOTAVAIL;
2439 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2440 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2441 &req->bu_threshold.b_time, ==)) &&
2442 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2443 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2444 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2446 return 0; /* XXX Already installed */
2450 /* Allocate the new bw_meter entry */
2451 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2457 /* Set the new bw_meter entry */
2458 x->bm_threshold.b_time = req->bu_threshold.b_time;
2460 x->bm_start_time = now;
2461 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2462 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2463 x->bm_measured.b_packets = 0;
2464 x->bm_measured.b_bytes = 0;
2465 x->bm_flags = flags;
2466 x->bm_time_next = NULL;
2467 x->bm_time_hash = BW_METER_BUCKETS;
2469 /* Add the new bw_meter entry to the front of entries for this MFC */
2471 x->bm_mfc_next = mfc->mfc_bw_meter;
2472 mfc->mfc_bw_meter = x;
2473 schedule_bw_meter(x, &now);
2480 free_bw_list(struct bw_meter *list)
2482 while (list != NULL) {
2483 struct bw_meter *x = list;
2485 list = list->bm_mfc_next;
2486 unschedule_bw_meter(x);
2492 * Delete one or multiple bw_meter entries
2495 del_bw_upcall(struct bw_upcall *req)
2500 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2504 /* Find the corresponding MFC entry */
2505 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2508 return EADDRNOTAVAIL;
2509 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2511 * Delete all bw_meter entries for this mfc
2513 struct bw_meter *list;
2515 list = mfc->mfc_bw_meter;
2516 mfc->mfc_bw_meter = NULL;
2520 } else { /* Delete a single bw_meter entry */
2521 struct bw_meter *prev;
2524 flags = compute_bw_meter_flags(req);
2526 /* Find the bw_meter entry to delete */
2527 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2528 prev = x, x = x->bm_mfc_next) {
2529 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2530 &req->bu_threshold.b_time, ==)) &&
2531 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2532 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2533 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2536 if (x != NULL) { /* Delete entry from the list for this MFC */
2538 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2540 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2542 unschedule_bw_meter(x);
2544 /* Free the bw_meter entry */
2556 * Perform bandwidth measurement processing that may result in an upcall
2559 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2561 struct timeval delta;
2566 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2568 if (x->bm_flags & BW_METER_GEQ) {
2570 * Processing for ">=" type of bw_meter entry
2572 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2573 /* Reset the bw_meter entry */
2574 x->bm_start_time = *nowp;
2575 x->bm_measured.b_packets = 0;
2576 x->bm_measured.b_bytes = 0;
2577 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2580 /* Record that a packet is received */
2581 x->bm_measured.b_packets++;
2582 x->bm_measured.b_bytes += plen;
2585 * Test if we should deliver an upcall
2587 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2588 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2589 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2590 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2591 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2592 /* Prepare an upcall for delivery */
2593 bw_meter_prepare_upcall(x, nowp);
2594 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2597 } else if (x->bm_flags & BW_METER_LEQ) {
2599 * Processing for "<=" type of bw_meter entry
2601 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2603 * We are behind time with the multicast forwarding table
2604 * scanning for "<=" type of bw_meter entries, so test now
2605 * if we should deliver an upcall.
2607 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2608 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2609 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2610 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2611 /* Prepare an upcall for delivery */
2612 bw_meter_prepare_upcall(x, nowp);
2614 /* Reschedule the bw_meter entry */
2615 unschedule_bw_meter(x);
2616 schedule_bw_meter(x, nowp);
2619 /* Record that a packet is received */
2620 x->bm_measured.b_packets++;
2621 x->bm_measured.b_bytes += plen;
2624 * Test if we should restart the measuring interval
2626 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2627 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2628 (x->bm_flags & BW_METER_UNIT_BYTES &&
2629 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2630 /* Don't restart the measuring interval */
2632 /* Do restart the measuring interval */
2634 * XXX: note that we don't unschedule and schedule, because this
2635 * might be too much overhead per packet. Instead, when we process
2636 * all entries for a given timer hash bin, we check whether it is
2637 * really a timeout. If not, we reschedule at that time.
2639 x->bm_start_time = *nowp;
2640 x->bm_measured.b_packets = 0;
2641 x->bm_measured.b_bytes = 0;
2642 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2648 * Prepare a bandwidth-related upcall
2651 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2653 struct timeval delta;
2654 struct bw_upcall *u;
2659 * Compute the measured time interval
2662 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2665 * If there are too many pending upcalls, deliver them now
2667 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2671 * Set the bw_upcall entry
2673 u = &bw_upcalls[bw_upcalls_n++];
2674 u->bu_src = x->bm_mfc->mfc_origin;
2675 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2676 u->bu_threshold.b_time = x->bm_threshold.b_time;
2677 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2678 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2679 u->bu_measured.b_time = delta;
2680 u->bu_measured.b_packets = x->bm_measured.b_packets;
2681 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2683 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2684 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2685 if (x->bm_flags & BW_METER_UNIT_BYTES)
2686 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2687 if (x->bm_flags & BW_METER_GEQ)
2688 u->bu_flags |= BW_UPCALL_GEQ;
2689 if (x->bm_flags & BW_METER_LEQ)
2690 u->bu_flags |= BW_UPCALL_LEQ;
2694 * Send the pending bandwidth-related upcalls
2697 bw_upcalls_send(void)
2700 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2701 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2702 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2704 IGMPMSG_BW_UPCALL,/* im_msgtype */
2709 { 0 } }; /* im_dst */
2713 if (bw_upcalls_n == 0)
2714 return; /* No pending upcalls */
2719 * Allocate a new mbuf, initialize it with the header and
2720 * the payload for the pending calls.
2722 MGETHDR(m, M_DONTWAIT, MT_DATA);
2724 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2728 m->m_len = m->m_pkthdr.len = 0;
2729 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2730 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2734 * XXX do we need to set the address in k_igmpsrc ?
2736 mrtstat.mrts_upcalls++;
2737 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2738 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2739 ++mrtstat.mrts_upq_sockfull;
2744 * Compute the timeout hash value for the bw_meter entries
2746 #define BW_METER_TIMEHASH(bw_meter, hash) \
2748 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2750 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2751 (hash) = next_timeval.tv_sec; \
2752 if (next_timeval.tv_usec) \
2753 (hash)++; /* XXX: make sure we don't timeout early */ \
2754 (hash) %= BW_METER_BUCKETS; \
2758 * Schedule a timer to process periodically bw_meter entry of type "<="
2759 * by linking the entry in the proper hash bucket.
2762 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2768 if (!(x->bm_flags & BW_METER_LEQ))
2769 return; /* XXX: we schedule timers only for "<=" entries */
2772 * Reset the bw_meter entry
2774 x->bm_start_time = *nowp;
2775 x->bm_measured.b_packets = 0;
2776 x->bm_measured.b_bytes = 0;
2777 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2780 * Compute the timeout hash value and insert the entry
2782 BW_METER_TIMEHASH(x, time_hash);
2783 x->bm_time_next = bw_meter_timers[time_hash];
2784 bw_meter_timers[time_hash] = x;
2785 x->bm_time_hash = time_hash;
2789 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2790 * by removing the entry from the proper hash bucket.
2793 unschedule_bw_meter(struct bw_meter *x)
2796 struct bw_meter *prev, *tmp;
2800 if (!(x->bm_flags & BW_METER_LEQ))
2801 return; /* XXX: we schedule timers only for "<=" entries */
2804 * Compute the timeout hash value and delete the entry
2806 time_hash = x->bm_time_hash;
2807 if (time_hash >= BW_METER_BUCKETS)
2808 return; /* Entry was not scheduled */
2810 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2811 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2816 panic("unschedule_bw_meter: bw_meter entry not found");
2819 prev->bm_time_next = x->bm_time_next;
2821 bw_meter_timers[time_hash] = x->bm_time_next;
2823 x->bm_time_next = NULL;
2824 x->bm_time_hash = BW_METER_BUCKETS;
2829 * Process all "<=" type of bw_meter that should be processed now,
2830 * and for each entry prepare an upcall if necessary. Each processed
2831 * entry is rescheduled again for the (periodic) processing.
2833 * This is run periodically (once per second normally). On each round,
2834 * all the potentially matching entries are in the hash slot that we are
2840 static uint32_t last_tv_sec; /* last time we processed this */
2844 struct timeval now, process_endtime;
2847 if (last_tv_sec == now.tv_sec)
2848 return; /* nothing to do */
2850 loops = now.tv_sec - last_tv_sec;
2851 last_tv_sec = now.tv_sec;
2852 if (loops > BW_METER_BUCKETS)
2853 loops = BW_METER_BUCKETS;
2857 * Process all bins of bw_meter entries from the one after the last
2858 * processed to the current one. On entry, i points to the last bucket
2859 * visited, so we need to increment i at the beginning of the loop.
2861 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2862 struct bw_meter *x, *tmp_list;
2864 if (++i >= BW_METER_BUCKETS)
2867 /* Disconnect the list of bw_meter entries from the bin */
2868 tmp_list = bw_meter_timers[i];
2869 bw_meter_timers[i] = NULL;
2871 /* Process the list of bw_meter entries */
2872 while (tmp_list != NULL) {
2874 tmp_list = tmp_list->bm_time_next;
2876 /* Test if the time interval is over */
2877 process_endtime = x->bm_start_time;
2878 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2879 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2880 /* Not yet: reschedule, but don't reset */
2883 BW_METER_TIMEHASH(x, time_hash);
2884 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2886 * XXX: somehow the bin processing is a bit ahead of time.
2887 * Put the entry in the next bin.
2889 if (++time_hash >= BW_METER_BUCKETS)
2892 x->bm_time_next = bw_meter_timers[time_hash];
2893 bw_meter_timers[time_hash] = x;
2894 x->bm_time_hash = time_hash;
2900 * Test if we should deliver an upcall
2902 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2903 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2904 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2905 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2906 /* Prepare an upcall for delivery */
2907 bw_meter_prepare_upcall(x, &now);
2911 * Reschedule for next processing
2913 schedule_bw_meter(x, &now);
2917 /* Send all upcalls that are pending delivery */
2924 * A periodic function for sending all upcalls that are pending delivery
2927 expire_bw_upcalls_send(void *unused)
2933 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2934 expire_bw_upcalls_send, NULL);
2938 * A periodic function for periodic scanning of the multicast forwarding
2939 * table for processing all "<=" bw_meter entries.
2942 expire_bw_meter_process(void *unused)
2944 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2947 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2951 * End of bandwidth monitoring code
2956 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2960 pim_register_send(struct ip *ip, struct vif *vifp,
2961 struct mbuf *m, struct mfc *rt)
2963 struct mbuf *mb_copy, *mm;
2965 if (mrtdebug & DEBUG_PIM)
2966 log(LOG_DEBUG, "pim_register_send: ");
2968 mb_copy = pim_register_prepare(ip, m);
2969 if (mb_copy == NULL)
2973 * Send all the fragments. Note that the mbuf for each fragment
2974 * is freed by the sending machinery.
2976 for (mm = mb_copy; mm; mm = mb_copy) {
2977 mb_copy = mm->m_nextpkt;
2979 mm = m_pullup(mm, sizeof(struct ip));
2981 ip = mtod(mm, struct ip *);
2982 if ((mrt_api_config & MRT_MFC_RP) &&
2983 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2984 pim_register_send_rp(ip, vifp, mm, rt);
2986 pim_register_send_upcall(ip, vifp, mm, rt);
2995 * Return a copy of the data packet that is ready for PIM Register
2997 * XXX: Note that in the returned copy the IP header is a valid one.
2999 static struct mbuf *
3000 pim_register_prepare(struct ip *ip, struct mbuf *m)
3002 struct mbuf *mb_copy = NULL;
3005 /* Take care of delayed checksums */
3006 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3007 in_delayed_cksum(m);
3008 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3012 * Copy the old packet & pullup its IP header into the
3013 * new mbuf so we can modify it.
3015 mb_copy = m_copypacket(m, M_DONTWAIT);
3016 if (mb_copy == NULL)
3018 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3019 if (mb_copy == NULL)
3022 /* take care of the TTL */
3023 ip = mtod(mb_copy, struct ip *);
3026 /* Compute the MTU after the PIM Register encapsulation */
3027 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3029 if (ip->ip_len <= mtu) {
3030 /* Turn the IP header into a valid one */
3031 ip->ip_len = htons(ip->ip_len);
3032 ip->ip_off = htons(ip->ip_off);
3034 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3036 /* Fragment the packet */
3037 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
3046 * Send an upcall with the data packet to the user-level process.
3049 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3050 struct mbuf *mb_copy, struct mfc *rt)
3052 struct mbuf *mb_first;
3053 int len = ntohs(ip->ip_len);
3055 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3060 * Add a new mbuf with an upcall header
3062 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
3063 if (mb_first == NULL) {
3067 mb_first->m_data += max_linkhdr;
3068 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3069 mb_first->m_len = sizeof(struct igmpmsg);
3070 mb_first->m_next = mb_copy;
3072 /* Send message to routing daemon */
3073 im = mtod(mb_first, struct igmpmsg *);
3074 im->im_msgtype = IGMPMSG_WHOLEPKT;
3076 im->im_vif = vifp - viftable;
3077 im->im_src = ip->ip_src;
3078 im->im_dst = ip->ip_dst;
3080 k_igmpsrc.sin_addr = ip->ip_src;
3082 mrtstat.mrts_upcalls++;
3084 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3085 if (mrtdebug & DEBUG_PIM)
3087 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3088 ++mrtstat.mrts_upq_sockfull;
3092 /* Keep statistics */
3093 pimstat.pims_snd_registers_msgs++;
3094 pimstat.pims_snd_registers_bytes += len;
3100 * Encapsulate the data packet in PIM Register message and send it to the RP.
3103 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3104 struct mbuf *mb_copy, struct mfc *rt)
3106 struct mbuf *mb_first;
3107 struct ip *ip_outer;
3108 struct pim_encap_pimhdr *pimhdr;
3109 int len = ntohs(ip->ip_len);
3110 vifi_t vifi = rt->mfc_parent;
3114 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
3116 return EADDRNOTAVAIL; /* The iif vif is invalid */
3120 * Add a new mbuf with the encapsulating header
3122 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
3123 if (mb_first == NULL) {
3127 mb_first->m_data += max_linkhdr;
3128 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3129 mb_first->m_next = mb_copy;
3131 mb_first->m_pkthdr.len = len + mb_first->m_len;
3134 * Fill in the encapsulating IP and PIM header
3136 ip_outer = mtod(mb_first, struct ip *);
3137 *ip_outer = pim_encap_iphdr;
3138 ip_outer->ip_id = ip_newid();
3139 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3140 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3141 ip_outer->ip_dst = rt->mfc_rp;
3143 * Copy the inner header TOS to the outer header, and take care of the
3146 ip_outer->ip_tos = ip->ip_tos;
3147 if (ntohs(ip->ip_off) & IP_DF)
3148 ip_outer->ip_off |= IP_DF;
3149 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3150 + sizeof(pim_encap_iphdr));
3151 *pimhdr = pim_encap_pimhdr;
3152 /* If the iif crosses a border, set the Border-bit */
3153 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3154 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3156 mb_first->m_data += sizeof(pim_encap_iphdr);
3157 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3158 mb_first->m_data -= sizeof(pim_encap_iphdr);
3160 if (vifp->v_rate_limit == 0)
3161 tbf_send_packet(vifp, mb_first);
3163 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3165 /* Keep statistics */
3166 pimstat.pims_snd_registers_msgs++;
3167 pimstat.pims_snd_registers_bytes += len;
3173 * PIM-SMv2 and PIM-DM messages processing.
3174 * Receives and verifies the PIM control messages, and passes them
3175 * up to the listening socket, using rip_input().
3176 * The only message with special processing is the PIM_REGISTER message
3177 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3178 * is passed to if_simloop().
3181 pim_input(struct mbuf *m, int off)
3183 struct ip *ip = mtod(m, struct ip *);
3186 int datalen = ip->ip_len;
3190 /* Keep statistics */
3191 pimstat.pims_rcv_total_msgs++;
3192 pimstat.pims_rcv_total_bytes += datalen;
3197 if (datalen < PIM_MINLEN) {
3198 pimstat.pims_rcv_tooshort++;
3199 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3200 datalen, (u_long)ip->ip_src.s_addr);
3206 * If the packet is at least as big as a REGISTER, go agead
3207 * and grab the PIM REGISTER header size, to avoid another
3208 * possible m_pullup() later.
3210 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3211 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3213 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3215 * Get the IP and PIM headers in contiguous memory, and
3216 * possibly the PIM REGISTER header.
3218 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3219 (m = m_pullup(m, minlen)) == 0) {
3220 log(LOG_ERR, "pim_input: m_pullup failure\n");
3223 /* m_pullup() may have given us a new mbuf so reset ip. */
3224 ip = mtod(m, struct ip *);
3225 ip_tos = ip->ip_tos;
3227 /* adjust mbuf to point to the PIM header */
3228 m->m_data += iphlen;
3230 pim = mtod(m, struct pim *);
3233 * Validate checksum. If PIM REGISTER, exclude the data packet.
3235 * XXX: some older PIMv2 implementations don't make this distinction,
3236 * so for compatibility reason perform the checksum over part of the
3237 * message, and if error, then over the whole message.
3239 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3240 /* do nothing, checksum okay */
3241 } else if (in_cksum(m, datalen)) {
3242 pimstat.pims_rcv_badsum++;
3243 if (mrtdebug & DEBUG_PIM)
3244 log(LOG_DEBUG, "pim_input: invalid checksum");
3249 /* PIM version check */
3250 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3251 pimstat.pims_rcv_badversion++;
3252 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3253 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3258 /* restore mbuf back to the outer IP */
3259 m->m_data -= iphlen;
3262 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3264 * Since this is a REGISTER, we'll make a copy of the register
3265 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3268 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3270 struct ip *encap_ip;
3275 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3277 if (mrtdebug & DEBUG_PIM)
3279 "pim_input: register vif not set: %d\n", reg_vif_num);
3283 /* XXX need refcnt? */
3284 vifp = viftable[reg_vif_num].v_ifp;
3290 if (datalen < PIM_REG_MINLEN) {
3291 pimstat.pims_rcv_tooshort++;
3292 pimstat.pims_rcv_badregisters++;
3294 "pim_input: register packet size too small %d from %lx\n",
3295 datalen, (u_long)ip->ip_src.s_addr);
3300 reghdr = (u_int32_t *)(pim + 1);
3301 encap_ip = (struct ip *)(reghdr + 1);
3303 if (mrtdebug & DEBUG_PIM) {
3305 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3306 (u_long)ntohl(encap_ip->ip_src.s_addr),
3307 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3308 ntohs(encap_ip->ip_len));
3311 /* verify the version number of the inner packet */
3312 if (encap_ip->ip_v != IPVERSION) {
3313 pimstat.pims_rcv_badregisters++;
3314 if (mrtdebug & DEBUG_PIM) {
3315 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3316 "of the inner packet\n", encap_ip->ip_v);
3322 /* verify the inner packet is destined to a mcast group */
3323 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3324 pimstat.pims_rcv_badregisters++;
3325 if (mrtdebug & DEBUG_PIM)
3327 "pim_input: inner packet of register is not "
3329 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3334 /* If a NULL_REGISTER, pass it to the daemon */
3335 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3336 goto pim_input_to_daemon;
3339 * Copy the TOS from the outer IP header to the inner IP header.
3341 if (encap_ip->ip_tos != ip_tos) {
3342 /* Outer TOS -> inner TOS */
3343 encap_ip->ip_tos = ip_tos;
3344 /* Recompute the inner header checksum. Sigh... */
3346 /* adjust mbuf to point to the inner IP header */
3347 m->m_data += (iphlen + PIM_MINLEN);
3348 m->m_len -= (iphlen + PIM_MINLEN);
3350 encap_ip->ip_sum = 0;
3351 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3353 /* restore mbuf to point back to the outer IP header */
3354 m->m_data -= (iphlen + PIM_MINLEN);
3355 m->m_len += (iphlen + PIM_MINLEN);
3359 * Decapsulate the inner IP packet and loopback to forward it
3360 * as a normal multicast packet. Also, make a copy of the
3361 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3362 * to pass to the daemon later, so it can take the appropriate
3363 * actions (e.g., send back PIM_REGISTER_STOP).
3364 * XXX: here m->m_data points to the outer IP header.
3366 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3369 "pim_input: pim register: could not copy register head\n");
3374 /* Keep statistics */
3375 /* XXX: registers_bytes include only the encap. mcast pkt */
3376 pimstat.pims_rcv_registers_msgs++;
3377 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3380 * forward the inner ip packet; point m_data at the inner ip.
3382 m_adj(m, iphlen + PIM_MINLEN);
3384 if (mrtdebug & DEBUG_PIM) {
3386 "pim_input: forwarding decapsulated register: "
3387 "src %lx, dst %lx, vif %d\n",
3388 (u_long)ntohl(encap_ip->ip_src.s_addr),
3389 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3392 /* NB: vifp was collected above; can it change on us? */
3393 if_simloop(vifp, m, dst.sin_family, 0);
3395 /* prepare the register head to send to the mrouting daemon */
3399 pim_input_to_daemon:
3401 * Pass the PIM message up to the daemon; if it is a Register message,
3402 * pass the 'head' only up to the daemon. This includes the
3403 * outer IP header, PIM header, PIM-Register header and the
3405 * XXX: the outer IP header pkt size of a Register is not adjust to
3406 * reflect the fact that the inner multicast data is truncated.
3408 rip_input(m, iphlen);
3415 ip_mroute_modevent(module_t mod, int type, void *unused)
3419 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3423 ip_mcast_src = X_ip_mcast_src;
3424 ip_mforward = X_ip_mforward;
3425 ip_mrouter_done = X_ip_mrouter_done;
3426 ip_mrouter_get = X_ip_mrouter_get;
3427 ip_mrouter_set = X_ip_mrouter_set;
3428 ip_rsvp_force_done = X_ip_rsvp_force_done;
3429 ip_rsvp_vif = X_ip_rsvp_vif;
3430 legal_vif_num = X_legal_vif_num;
3431 mrt_ioctl = X_mrt_ioctl;
3432 rsvp_input_p = X_rsvp_input;
3437 * Typically module unload happens after the user-level
3438 * process has shutdown the kernel services (the check
3439 * below insures someone can't just yank the module out
3440 * from under a running process). But if the module is
3441 * just loaded and then unloaded w/o starting up a user
3442 * process we still need to cleanup.
3447 X_ip_mrouter_done();
3448 ip_mcast_src = NULL;
3450 ip_mrouter_done = NULL;
3451 ip_mrouter_get = NULL;
3452 ip_mrouter_set = NULL;
3453 ip_rsvp_force_done = NULL;
3455 legal_vif_num = NULL;
3457 rsvp_input_p = NULL;
3460 mtx_destroy(&mrouter_mtx);
3468 static moduledata_t ip_mroutemod = {
3473 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);