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_inet6.h"
61 #include "opt_mrouting.h"
65 #include <sys/param.h>
66 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #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>
94 #include <netinet/pim.h>
95 #include <netinet/pim_var.h>
96 #include <netinet/udp.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/ip6_var.h>
103 #include <machine/in_cksum.h>
105 #include <security/mac/mac_framework.h>
108 * Control debugging code for rsvp and multicast routing code.
109 * Can only set them with the debugger.
111 static u_int rsvpdebug; /* non-zero enables debugging */
113 static u_int mrtdebug; /* any set of the flags below */
114 #define DEBUG_MFC 0x02
115 #define DEBUG_FORWARD 0x04
116 #define DEBUG_EXPIRE 0x08
117 #define DEBUG_XMIT 0x10
118 #define DEBUG_PIM 0x20
120 #define VIFI_INVALID ((vifi_t) -1)
122 #define M_HASCL(m) ((m)->m_flags & M_EXT)
124 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
127 * Locking. We use two locks: one for the virtual interface table and
128 * one for the forwarding table. These locks may be nested in which case
129 * the VIF lock must always be taken first. Note that each lock is used
130 * to cover not only the specific data structure but also related data
131 * structures. It may be better to add more fine-grained locking later;
132 * it's not clear how performance-critical this code is.
134 * XXX: This module could particularly benefit from being cleaned
135 * up to use the <sys/queue.h> macros.
139 static struct mrtstat mrtstat;
140 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
142 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
144 static struct mfc *mfctable[MFCTBLSIZ];
145 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
146 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
147 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
149 static struct mtx mrouter_mtx;
150 #define MROUTER_LOCK() mtx_lock(&mrouter_mtx)
151 #define MROUTER_UNLOCK() mtx_unlock(&mrouter_mtx)
152 #define MROUTER_LOCK_ASSERT() mtx_assert(&mrouter_mtx, MA_OWNED)
153 #define MROUTER_LOCK_INIT() \
154 mtx_init(&mrouter_mtx, "IPv4 multicast forwarding", NULL, MTX_DEF)
155 #define MROUTER_LOCK_DESTROY() mtx_destroy(&mrouter_mtx)
157 static struct mtx mfc_mtx;
158 #define MFC_LOCK() mtx_lock(&mfc_mtx)
159 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
160 #define MFC_LOCK_ASSERT() mtx_assert(&mfc_mtx, MA_OWNED)
161 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
162 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
164 static struct vif viftable[MAXVIFS];
165 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
166 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
167 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
169 static struct mtx vif_mtx;
170 #define VIF_LOCK() mtx_lock(&vif_mtx)
171 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
172 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
173 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
174 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
176 static u_char nexpire[MFCTBLSIZ];
178 static eventhandler_tag if_detach_event_tag = NULL;
180 static struct callout expire_upcalls_ch;
182 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
183 #define UPCALL_EXPIRE 6 /* number of timeouts */
188 * Bandwidth meter variables and constants
190 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
192 * Pending timeouts are stored in a hash table, the key being the
193 * expiration time. Periodically, the entries are analysed and processed.
195 #define BW_METER_BUCKETS 1024
196 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
197 static struct callout bw_meter_ch;
198 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
201 * Pending upcalls are stored in a vector which is flushed when
202 * full, or periodically
204 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
205 static u_int bw_upcalls_n; /* # of pending upcalls */
206 static struct callout bw_upcalls_ch;
207 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
209 static struct pimstat pimstat;
211 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
212 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
214 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
216 static u_long pim_squelch_wholepkt = 0;
217 SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RW,
218 &pim_squelch_wholepkt, 0,
219 "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
221 extern struct domain inetdomain;
222 struct protosw in_pim_protosw = {
224 .pr_domain = &inetdomain,
225 .pr_protocol = IPPROTO_PIM,
226 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
227 .pr_input = pim_input,
228 .pr_output = (pr_output_t*)rip_output,
229 .pr_ctloutput = rip_ctloutput,
230 .pr_usrreqs = &rip_usrreqs
232 static const struct encaptab *pim_encap_cookie;
235 /* ip6_mroute.c glue */
236 extern struct in6_protosw in6_pim_protosw;
237 static const struct encaptab *pim6_encap_cookie;
239 extern int X_ip6_mrouter_set(struct socket *, struct sockopt *);
240 extern int X_ip6_mrouter_get(struct socket *, struct sockopt *);
241 extern int X_ip6_mrouter_done(void);
242 extern int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
243 extern int X_mrt6_ioctl(int, caddr_t);
246 static int pim_encapcheck(const struct mbuf *, int, int, void *);
249 * Note: the PIM Register encapsulation adds the following in front of a
252 * struct pim_encap_hdr {
254 * struct pim_encap_pimhdr pim;
259 struct pim_encap_pimhdr {
264 static struct ip pim_encap_iphdr = {
265 #if BYTE_ORDER == LITTLE_ENDIAN
266 sizeof(struct ip) >> 2,
270 sizeof(struct ip) >> 2,
273 sizeof(struct ip), /* total length */
281 static struct pim_encap_pimhdr pim_encap_pimhdr = {
283 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
290 static struct ifnet multicast_register_if;
291 static vifi_t reg_vif_num = VIFI_INVALID;
296 static vifi_t numvifs;
298 static u_long X_ip_mcast_src(int vifi);
299 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
300 struct mbuf *m, struct ip_moptions *imo);
301 static int X_ip_mrouter_done(void);
302 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
303 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
304 static int X_legal_vif_num(int vif);
305 static int X_mrt_ioctl(int cmd, caddr_t data);
307 static int get_sg_cnt(struct sioc_sg_req *);
308 static int get_vif_cnt(struct sioc_vif_req *);
309 static void if_detached_event(void *arg __unused, struct ifnet *);
310 static int ip_mrouter_init(struct socket *, int);
311 static int add_vif(struct vifctl *);
312 static int del_vif_locked(vifi_t);
313 static int del_vif(vifi_t);
314 static int add_mfc(struct mfcctl2 *);
315 static int del_mfc(struct mfcctl2 *);
316 static int set_api_config(uint32_t *); /* chose API capabilities */
317 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
318 static int set_assert(int);
319 static void expire_upcalls(void *);
320 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
321 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
322 static void send_packet(struct vif *, struct mbuf *);
325 * Bandwidth monitoring
327 static void free_bw_list(struct bw_meter *list);
328 static int add_bw_upcall(struct bw_upcall *);
329 static int del_bw_upcall(struct bw_upcall *);
330 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
331 struct timeval *nowp);
332 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
333 static void bw_upcalls_send(void);
334 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
335 static void unschedule_bw_meter(struct bw_meter *x);
336 static void bw_meter_process(void);
337 static void expire_bw_upcalls_send(void *);
338 static void expire_bw_meter_process(void *);
340 static int pim_register_send(struct ip *, struct vif *,
341 struct mbuf *, struct mfc *);
342 static int pim_register_send_rp(struct ip *, struct vif *,
343 struct mbuf *, struct mfc *);
344 static int pim_register_send_upcall(struct ip *, struct vif *,
345 struct mbuf *, struct mfc *);
346 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
349 * whether or not special PIM assert processing is enabled.
351 static int pim_assert;
353 * Rate limit for assert notification messages, in usec
355 #define ASSERT_MSG_TIME 3000000
358 * Kernel multicast routing API capabilities and setup.
359 * If more API capabilities are added to the kernel, they should be
360 * recorded in `mrt_api_support'.
362 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
363 MRT_MFC_FLAGS_BORDER_VIF |
366 static uint32_t mrt_api_config = 0;
369 * Hash function for a source, group entry
371 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
372 ((g) >> 20) ^ ((g) >> 10) ^ (g))
375 * Find a route for a given origin IP address and Multicast group address
376 * Statistics are updated by the caller if needed
377 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
380 mfc_find(in_addr_t o, in_addr_t g)
386 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
387 if ((rt->mfc_origin.s_addr == o) &&
388 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
394 * Macros to compute elapsed time efficiently
395 * Borrowed from Van Jacobson's scheduling code
397 #define TV_DELTA(a, b, delta) { \
399 delta = (a).tv_usec - (b).tv_usec; \
400 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
409 delta += (1000000 * xxs); \
414 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
415 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
418 * Handle MRT setsockopt commands to modify the multicast routing tables.
421 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
427 struct bw_upcall bw_upcall;
430 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
434 switch (sopt->sopt_name) {
436 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
439 error = ip_mrouter_init(so, optval);
443 error = ip_mrouter_done();
447 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
450 error = add_vif(&vifc);
454 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
457 error = del_vif(vifi);
463 * select data size depending on API version.
465 if (sopt->sopt_name == MRT_ADD_MFC &&
466 mrt_api_config & MRT_API_FLAGS_ALL) {
467 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
468 sizeof(struct mfcctl2));
470 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
471 sizeof(struct mfcctl));
472 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
473 sizeof(mfc) - sizeof(struct mfcctl));
477 if (sopt->sopt_name == MRT_ADD_MFC)
478 error = add_mfc(&mfc);
480 error = del_mfc(&mfc);
484 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
491 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
493 error = set_api_config(&i);
495 error = sooptcopyout(sopt, &i, sizeof i);
498 case MRT_ADD_BW_UPCALL:
499 case MRT_DEL_BW_UPCALL:
500 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
504 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
505 error = add_bw_upcall(&bw_upcall);
507 error = del_bw_upcall(&bw_upcall);
518 * Handle MRT getsockopt commands
521 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
524 static int version = 0x0305; /* !!! why is this here? XXX */
526 switch (sopt->sopt_name) {
528 error = sooptcopyout(sopt, &version, sizeof version);
532 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
535 case MRT_API_SUPPORT:
536 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
540 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
551 * Handle ioctl commands to obtain information from the cache
554 X_mrt_ioctl(int cmd, caddr_t data)
559 * Currently the only function calling this ioctl routine is rtioctl().
560 * Typically, only root can create the raw socket in order to execute
561 * this ioctl method, however the request might be coming from a prison
563 error = priv_check(curthread, PRIV_NETINET_MROUTE);
567 case (SIOCGETVIFCNT):
568 error = get_vif_cnt((struct sioc_vif_req *)data);
572 error = get_sg_cnt((struct sioc_sg_req *)data);
583 * returns the packet, byte, rpf-failure count for the source group provided
586 get_sg_cnt(struct sioc_sg_req *req)
591 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
594 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
595 return EADDRNOTAVAIL;
597 req->pktcnt = rt->mfc_pkt_cnt;
598 req->bytecnt = rt->mfc_byte_cnt;
599 req->wrong_if = rt->mfc_wrong_if;
605 * returns the input and output packet and byte counts on the vif provided
608 get_vif_cnt(struct sioc_vif_req *req)
610 vifi_t vifi = req->vifi;
613 if (vifi >= numvifs) {
618 req->icount = viftable[vifi].v_pkt_in;
619 req->ocount = viftable[vifi].v_pkt_out;
620 req->ibytes = viftable[vifi].v_bytes_in;
621 req->obytes = viftable[vifi].v_bytes_out;
628 ip_mrouter_reset(void)
630 bzero((caddr_t)mfctable, sizeof(mfctable));
631 bzero((caddr_t)nexpire, sizeof(nexpire));
636 callout_init(&expire_upcalls_ch, CALLOUT_MPSAFE);
639 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
640 callout_init(&bw_upcalls_ch, CALLOUT_MPSAFE);
641 callout_init(&bw_meter_ch, CALLOUT_MPSAFE);
645 if_detached_event(void *arg __unused, struct ifnet *ifp)
651 struct mfc **ppmfc; /* Pointer to previous node's next-pointer */
656 if (ip_mrouter == NULL) {
661 * Tear down multicast forwarder state associated with this ifnet.
662 * 1. Walk the vif list, matching vifs against this ifnet.
663 * 2. Walk the multicast forwarding cache (mfc) looking for
664 * inner matches with this vif's index.
665 * 3. Free any pending mbufs for this mfc.
666 * 4. Free the associated mfc entry and state associated with this vif.
667 * Be very careful about unlinking from a singly-linked list whose
668 * "head node" is a pointer in a simple array.
669 * 5. Free vif state. This should disable ALLMULTI on the interface.
673 for (vifi = 0; vifi < numvifs; vifi++) {
674 if (viftable[vifi].v_ifp != ifp)
676 for (i = 0; i < MFCTBLSIZ; i++) {
677 ppmfc = &mfctable[i];
678 for (mfc = mfctable[i]; mfc != NULL; ) {
679 nmfc = mfc->mfc_next;
680 if (mfc->mfc_parent == vifi) {
681 for (pq = mfc->mfc_stall; pq != NULL; ) {
687 free_bw_list(mfc->mfc_bw_meter);
688 free(mfc, M_MRTABLE);
691 ppmfc = &mfc->mfc_next;
696 del_vif_locked(vifi);
705 * Enable multicast routing
708 ip_mrouter_init(struct socket *so, int version)
711 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
712 so->so_type, so->so_proto->pr_protocol);
714 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
722 if (ip_mrouter != NULL) {
727 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
728 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
729 if (if_detach_event_tag == NULL) {
734 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
736 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
737 expire_bw_upcalls_send, NULL);
738 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
745 log(LOG_DEBUG, "ip_mrouter_init\n");
751 * Disable multicast routing
754 X_ip_mrouter_done(void)
765 if (ip_mrouter == NULL) {
771 * Detach/disable hooks to the reset of the system.
778 * For each phyint in use, disable promiscuous reception of all IP
781 for (vifi = 0; vifi < numvifs; vifi++) {
782 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
783 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
784 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
786 so->sin_len = sizeof(struct sockaddr_in);
787 so->sin_family = AF_INET;
788 so->sin_addr.s_addr = INADDR_ANY;
789 ifp = viftable[vifi].v_ifp;
793 bzero((caddr_t)viftable, sizeof(viftable));
797 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
800 * Free all multicast forwarding cache entries.
802 callout_stop(&expire_upcalls_ch);
803 callout_stop(&bw_upcalls_ch);
804 callout_stop(&bw_meter_ch);
807 for (i = 0; i < MFCTBLSIZ; i++) {
808 for (rt = mfctable[i]; rt != NULL; ) {
809 struct mfc *nr = rt->mfc_next;
811 for (rte = rt->mfc_stall; rte != NULL; ) {
812 struct rtdetq *n = rte->next;
815 free(rte, M_MRTABLE);
818 free_bw_list(rt->mfc_bw_meter);
823 bzero((caddr_t)mfctable, sizeof(mfctable));
824 bzero((caddr_t)nexpire, sizeof(nexpire));
826 bzero(bw_meter_timers, sizeof(bw_meter_timers));
829 reg_vif_num = VIFI_INVALID;
834 log(LOG_DEBUG, "ip_mrouter_done\n");
840 * Set PIM assert processing global
845 if ((i != 1) && (i != 0))
854 * Configure API capabilities
857 set_api_config(uint32_t *apival)
862 * We can set the API capabilities only if it is the first operation
863 * after MRT_INIT. I.e.:
864 * - there are no vifs installed
865 * - pim_assert is not enabled
866 * - the MFC table is empty
876 for (i = 0; i < MFCTBLSIZ; i++) {
877 if (mfctable[i] != NULL) {
883 mrt_api_config = *apival & mrt_api_support;
884 *apival = mrt_api_config;
890 * Add a vif to the vif table
893 add_vif(struct vifctl *vifcp)
895 struct vif *vifp = viftable + vifcp->vifc_vifi;
896 struct sockaddr_in sin = {sizeof sin, AF_INET};
902 if (vifcp->vifc_vifi >= MAXVIFS) {
906 /* rate limiting is no longer supported by this code */
907 if (vifcp->vifc_rate_limit != 0) {
908 log(LOG_ERR, "rate limiting is no longer supported\n");
912 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
916 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
918 return EADDRNOTAVAIL;
921 /* Find the interface with an address in AF_INET family */
922 if (vifcp->vifc_flags & VIFF_REGISTER) {
924 * XXX: Because VIFF_REGISTER does not really need a valid
925 * local interface (e.g. it could be 127.0.0.2), we don't
930 sin.sin_addr = vifcp->vifc_lcl_addr;
931 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
934 return EADDRNOTAVAIL;
939 if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
940 log(LOG_ERR, "tunnels are no longer supported\n");
943 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
944 ifp = &multicast_register_if;
946 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
947 (void *)&multicast_register_if);
948 if (reg_vif_num == VIFI_INVALID) {
949 if_initname(&multicast_register_if, "register_vif", 0);
950 multicast_register_if.if_flags = IFF_LOOPBACK;
951 reg_vif_num = vifcp->vifc_vifi;
953 } else { /* Make sure the interface supports multicast */
954 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
959 /* Enable promiscuous reception of all IP multicasts from the if */
960 error = if_allmulti(ifp, 1);
967 vifp->v_flags = vifcp->vifc_flags;
968 vifp->v_threshold = vifcp->vifc_threshold;
969 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
970 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
973 vifp->v_rsvpd = NULL;
974 /* initialize per vif pkt counters */
977 vifp->v_bytes_in = 0;
978 vifp->v_bytes_out = 0;
979 bzero(&vifp->v_route, sizeof(vifp->v_route));
981 /* Adjust numvifs up if the vifi is higher than numvifs */
982 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
987 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x\n",
989 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
990 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
991 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
992 vifcp->vifc_threshold);
998 * Delete a vif from the vif table
1001 del_vif_locked(vifi_t vifi)
1007 if (vifi >= numvifs) {
1010 vifp = &viftable[vifi];
1011 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1012 return EADDRNOTAVAIL;
1015 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1016 if_allmulti(vifp->v_ifp, 0);
1018 if (vifp->v_flags & VIFF_REGISTER)
1019 reg_vif_num = VIFI_INVALID;
1021 bzero((caddr_t)vifp, sizeof (*vifp));
1024 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1026 /* Adjust numvifs down */
1027 for (vifi = numvifs; vifi > 0; vifi--)
1028 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1036 del_vif(vifi_t vifi)
1041 cc = del_vif_locked(vifi);
1048 * update an mfc entry without resetting counters and S,G addresses.
1051 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1055 rt->mfc_parent = mfccp->mfcc_parent;
1056 for (i = 0; i < numvifs; i++) {
1057 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1058 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1061 /* set the RP address */
1062 if (mrt_api_config & MRT_MFC_RP)
1063 rt->mfc_rp = mfccp->mfcc_rp;
1065 rt->mfc_rp.s_addr = INADDR_ANY;
1069 * fully initialize an mfc entry from the parameter.
1072 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1074 rt->mfc_origin = mfccp->mfcc_origin;
1075 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1077 update_mfc_params(rt, mfccp);
1079 /* initialize pkt counters per src-grp */
1080 rt->mfc_pkt_cnt = 0;
1081 rt->mfc_byte_cnt = 0;
1082 rt->mfc_wrong_if = 0;
1083 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1091 add_mfc(struct mfcctl2 *mfccp)
1101 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1103 /* If an entry already exists, just update the fields */
1105 if (mrtdebug & DEBUG_MFC)
1106 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1107 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1108 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1109 mfccp->mfcc_parent);
1111 update_mfc_params(rt, mfccp);
1118 * Find the entry for which the upcall was made and update
1120 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1121 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1123 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1124 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1125 (rt->mfc_stall != NULL)) {
1128 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1129 "multiple kernel entries",
1130 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1131 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1132 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1134 if (mrtdebug & DEBUG_MFC)
1135 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1136 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1137 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1138 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1140 init_mfc_params(rt, mfccp);
1142 rt->mfc_expire = 0; /* Don't clean this guy up */
1145 /* free packets Qed at the end of this entry */
1146 for (rte = rt->mfc_stall; rte != NULL; ) {
1147 struct rtdetq *n = rte->next;
1149 ip_mdq(rte->m, rte->ifp, rt, -1);
1151 free(rte, M_MRTABLE);
1154 rt->mfc_stall = NULL;
1159 * It is possible that an entry is being inserted without an upcall
1162 if (mrtdebug & DEBUG_MFC)
1163 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1164 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1165 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1166 mfccp->mfcc_parent);
1168 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1169 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1170 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1171 init_mfc_params(rt, mfccp);
1178 if (rt == NULL) { /* no upcall, so make a new entry */
1179 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1186 init_mfc_params(rt, mfccp);
1188 rt->mfc_stall = NULL;
1190 rt->mfc_bw_meter = NULL;
1191 /* insert new entry at head of hash chain */
1192 rt->mfc_next = mfctable[hash];
1193 mfctable[hash] = rt;
1202 * Delete an mfc entry
1205 del_mfc(struct mfcctl2 *mfccp)
1207 struct in_addr origin;
1208 struct in_addr mcastgrp;
1212 struct bw_meter *list;
1214 origin = mfccp->mfcc_origin;
1215 mcastgrp = mfccp->mfcc_mcastgrp;
1217 if (mrtdebug & DEBUG_MFC)
1218 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1219 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1223 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1224 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1225 if (origin.s_addr == rt->mfc_origin.s_addr &&
1226 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1227 rt->mfc_stall == NULL)
1231 return EADDRNOTAVAIL;
1234 *nptr = rt->mfc_next;
1237 * free the bw_meter entries
1239 list = rt->mfc_bw_meter;
1240 rt->mfc_bw_meter = NULL;
1242 free(rt, M_MRTABLE);
1252 * Send a message to the routing daemon on the multicast routing socket
1255 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1258 SOCKBUF_LOCK(&s->so_rcv);
1259 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1261 sorwakeup_locked(s);
1264 SOCKBUF_UNLOCK(&s->so_rcv);
1271 * IP multicast forwarding function. This function assumes that the packet
1272 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1273 * pointed to by "ifp", and the packet is to be relayed to other networks
1274 * that have members of the packet's destination IP multicast group.
1276 * The packet is returned unscathed to the caller, unless it is
1277 * erroneous, in which case a non-zero return value tells the caller to
1281 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1284 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1285 struct ip_moptions *imo)
1291 if (mrtdebug & DEBUG_FORWARD)
1292 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1293 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1296 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1297 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1299 * Packet arrived via a physical interface or
1300 * an encapsulated tunnel or a register_vif.
1304 * Packet arrived through a source-route tunnel.
1305 * Source-route tunnels are no longer supported.
1307 static int last_log;
1308 if (last_log != time_uptime) {
1309 last_log = time_uptime;
1311 "ip_mforward: received source-routed packet from %lx\n",
1312 (u_long)ntohl(ip->ip_src.s_addr));
1319 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1320 if (ip->ip_ttl < MAXTTL)
1321 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1322 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1323 struct vif *vifp = viftable + vifi;
1325 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1326 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1328 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1329 vifp->v_ifp->if_xname);
1331 error = ip_mdq(m, ifp, NULL, vifi);
1336 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1337 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1338 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1340 printf("In fact, no options were specified at all\n");
1344 * Don't forward a packet with time-to-live of zero or one,
1345 * or a packet destined to a local-only group.
1347 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr))) {
1354 * Determine forwarding vifs from the forwarding cache table
1356 ++mrtstat.mrts_mfc_lookups;
1357 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1359 /* Entry exists, so forward if necessary */
1361 error = ip_mdq(m, ifp, rt, -1);
1367 * If we don't have a route for packet's origin,
1368 * Make a copy of the packet & send message to routing daemon
1374 int hlen = ip->ip_hl << 2;
1376 ++mrtstat.mrts_mfc_misses;
1378 mrtstat.mrts_no_route++;
1379 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1380 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1381 (u_long)ntohl(ip->ip_src.s_addr),
1382 (u_long)ntohl(ip->ip_dst.s_addr));
1385 * Allocate mbufs early so that we don't do extra work if we are
1386 * just going to fail anyway. Make sure to pullup the header so
1387 * that other people can't step on it.
1389 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1395 mb0 = m_copypacket(m, M_DONTWAIT);
1396 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1397 mb0 = m_pullup(mb0, hlen);
1399 free(rte, M_MRTABLE);
1405 /* is there an upcall waiting for this flow ? */
1406 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1407 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1408 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1409 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1410 (rt->mfc_stall != NULL))
1417 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1421 * Locate the vifi for the incoming interface for this packet.
1422 * If none found, drop packet.
1424 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1426 if (vifi >= numvifs) /* vif not found, drop packet */
1429 /* no upcall, so make a new entry */
1430 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1433 /* Make a copy of the header to send to the user level process */
1434 mm = m_copy(mb0, 0, hlen);
1439 * Send message to routing daemon to install
1440 * a route into the kernel table
1443 im = mtod(mm, struct igmpmsg *);
1444 im->im_msgtype = IGMPMSG_NOCACHE;
1448 mrtstat.mrts_upcalls++;
1450 k_igmpsrc.sin_addr = ip->ip_src;
1451 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1452 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1453 ++mrtstat.mrts_upq_sockfull;
1455 free(rt, M_MRTABLE);
1457 free(rte, M_MRTABLE);
1464 /* insert new entry at head of hash chain */
1465 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1466 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1467 rt->mfc_expire = UPCALL_EXPIRE;
1469 for (i = 0; i < numvifs; i++) {
1470 rt->mfc_ttls[i] = 0;
1471 rt->mfc_flags[i] = 0;
1473 rt->mfc_parent = -1;
1475 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1477 rt->mfc_bw_meter = NULL;
1479 /* link into table */
1480 rt->mfc_next = mfctable[hash];
1481 mfctable[hash] = rt;
1482 rt->mfc_stall = rte;
1485 /* determine if q has overflowed */
1490 * XXX ouch! we need to append to the list, but we
1491 * only have a pointer to the front, so we have to
1492 * scan the entire list every time.
1494 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1497 if (npkts > MAX_UPQ) {
1498 mrtstat.mrts_upq_ovflw++;
1500 free(rte, M_MRTABLE);
1507 /* Add this entry to the end of the queue */
1523 * Clean up the cache entry if upcall is not serviced
1526 expire_upcalls(void *unused)
1529 struct mfc *mfc, **nptr;
1533 for (i = 0; i < MFCTBLSIZ; i++) {
1534 if (nexpire[i] == 0)
1536 nptr = &mfctable[i];
1537 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1539 * Skip real cache entries
1540 * Make sure it wasn't marked to not expire (shouldn't happen)
1543 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1544 --mfc->mfc_expire == 0) {
1545 if (mrtdebug & DEBUG_EXPIRE)
1546 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1547 (u_long)ntohl(mfc->mfc_origin.s_addr),
1548 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1550 * drop all the packets
1551 * free the mbuf with the pkt, if, timing info
1553 for (rte = mfc->mfc_stall; rte; ) {
1554 struct rtdetq *n = rte->next;
1557 free(rte, M_MRTABLE);
1560 ++mrtstat.mrts_cache_cleanups;
1564 * free the bw_meter entries
1566 while (mfc->mfc_bw_meter != NULL) {
1567 struct bw_meter *x = mfc->mfc_bw_meter;
1569 mfc->mfc_bw_meter = x->bm_mfc_next;
1573 *nptr = mfc->mfc_next;
1574 free(mfc, M_MRTABLE);
1576 nptr = &mfc->mfc_next;
1582 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1586 * Packet forwarding routine once entry in the cache is made
1589 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1591 struct ip *ip = mtod(m, struct ip *);
1593 int plen = ip->ip_len;
1598 * If xmt_vif is not -1, send on only the requested vif.
1600 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1602 if (xmt_vif < numvifs) {
1603 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1604 pim_register_send(ip, viftable + xmt_vif, m, rt);
1606 phyint_send(ip, viftable + xmt_vif, m);
1611 * Don't forward if it didn't arrive from the parent vif for its origin.
1613 vifi = rt->mfc_parent;
1614 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1615 /* came in the wrong interface */
1616 if (mrtdebug & DEBUG_FORWARD)
1617 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1618 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1619 ++mrtstat.mrts_wrong_if;
1622 * If we are doing PIM assert processing, send a message
1623 * to the routing daemon.
1625 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1626 * can complete the SPT switch, regardless of the type
1627 * of the iif (broadcast media, GRE tunnel, etc).
1629 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1633 if (ifp == &multicast_register_if)
1634 pimstat.pims_rcv_registers_wrongiif++;
1636 /* Get vifi for the incoming packet */
1637 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1639 if (vifi >= numvifs)
1640 return 0; /* The iif is not found: ignore the packet. */
1642 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1643 return 0; /* WRONGVIF disabled: ignore the packet */
1647 TV_DELTA(now, rt->mfc_last_assert, delta);
1649 if (delta > ASSERT_MSG_TIME) {
1650 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1652 int hlen = ip->ip_hl << 2;
1653 struct mbuf *mm = m_copy(m, 0, hlen);
1655 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1656 mm = m_pullup(mm, hlen);
1660 rt->mfc_last_assert = now;
1662 im = mtod(mm, struct igmpmsg *);
1663 im->im_msgtype = IGMPMSG_WRONGVIF;
1667 mrtstat.mrts_upcalls++;
1669 k_igmpsrc.sin_addr = im->im_src;
1670 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1672 "ip_mforward: ip_mrouter socket queue full\n");
1673 ++mrtstat.mrts_upq_sockfull;
1681 /* If I sourced this packet, it counts as output, else it was input. */
1682 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1683 viftable[vifi].v_pkt_out++;
1684 viftable[vifi].v_bytes_out += plen;
1686 viftable[vifi].v_pkt_in++;
1687 viftable[vifi].v_bytes_in += plen;
1690 rt->mfc_byte_cnt += plen;
1693 * For each vif, decide if a copy of the packet should be forwarded.
1695 * - the ttl exceeds the vif's threshold
1696 * - there are group members downstream on interface
1698 for (vifi = 0; vifi < numvifs; vifi++)
1699 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1700 viftable[vifi].v_pkt_out++;
1701 viftable[vifi].v_bytes_out += plen;
1702 if (viftable[vifi].v_flags & VIFF_REGISTER)
1703 pim_register_send(ip, viftable + vifi, m, rt);
1705 phyint_send(ip, viftable + vifi, m);
1709 * Perform upcall-related bw measuring.
1711 if (rt->mfc_bw_meter != NULL) {
1717 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1718 bw_meter_receive_packet(x, plen, &now);
1725 * check if a vif number is legal/ok. This is used by ip_output.
1728 X_legal_vif_num(int vif)
1730 /* XXX unlocked, matter? */
1731 return (vif >= 0 && vif < numvifs);
1735 * Return the local address used by this vif
1738 X_ip_mcast_src(int vifi)
1740 /* XXX unlocked, matter? */
1741 if (vifi >= 0 && vifi < numvifs)
1742 return viftable[vifi].v_lcl_addr.s_addr;
1748 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1750 struct mbuf *mb_copy;
1751 int hlen = ip->ip_hl << 2;
1756 * Make a new reference to the packet; make sure that
1757 * the IP header is actually copied, not just referenced,
1758 * so that ip_output() only scribbles on the copy.
1760 mb_copy = m_copypacket(m, M_DONTWAIT);
1761 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1762 mb_copy = m_pullup(mb_copy, hlen);
1763 if (mb_copy == NULL)
1766 send_packet(vifp, mb_copy);
1770 send_packet(struct vif *vifp, struct mbuf *m)
1772 struct ip_moptions imo;
1773 struct in_multi *imm[2];
1778 imo.imo_multicast_ifp = vifp->v_ifp;
1779 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1780 imo.imo_multicast_loop = 1;
1781 imo.imo_multicast_vif = -1;
1782 imo.imo_num_memberships = 0;
1783 imo.imo_max_memberships = 2;
1784 imo.imo_membership = &imm[0];
1787 * Re-entrancy should not be a problem here, because
1788 * the packets that we send out and are looped back at us
1789 * should get rejected because they appear to come from
1790 * the loopback interface, thus preventing looping.
1792 error = ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, &imo, NULL);
1793 if (mrtdebug & DEBUG_XMIT) {
1794 log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
1795 vifp - viftable, error);
1800 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
1804 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1807 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
1813 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
1815 return EADDRNOTAVAIL;
1818 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
1819 /* Check if socket is available. */
1820 if (viftable[vifi].v_rsvpd != NULL) {
1825 viftable[vifi].v_rsvpd = so;
1826 /* This may seem silly, but we need to be sure we don't over-increment
1827 * the RSVP counter, in case something slips up.
1829 if (!viftable[vifi].v_rsvp_on) {
1830 viftable[vifi].v_rsvp_on = 1;
1833 } else { /* must be VIF_OFF */
1835 * XXX as an additional consistency check, one could make sure
1836 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
1837 * first parameter is pretty useless.
1839 viftable[vifi].v_rsvpd = NULL;
1841 * This may seem silly, but we need to be sure we don't over-decrement
1842 * the RSVP counter, in case something slips up.
1844 if (viftable[vifi].v_rsvp_on) {
1845 viftable[vifi].v_rsvp_on = 0;
1854 X_ip_rsvp_force_done(struct socket *so)
1858 /* Don't bother if it is not the right type of socket. */
1859 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1864 /* The socket may be attached to more than one vif...this
1865 * is perfectly legal.
1867 for (vifi = 0; vifi < numvifs; vifi++) {
1868 if (viftable[vifi].v_rsvpd == so) {
1869 viftable[vifi].v_rsvpd = NULL;
1870 /* This may seem silly, but we need to be sure we don't
1871 * over-decrement the RSVP counter, in case something slips up.
1873 if (viftable[vifi].v_rsvp_on) {
1874 viftable[vifi].v_rsvp_on = 0;
1884 X_rsvp_input(struct mbuf *m, int off)
1887 struct ip *ip = mtod(m, struct ip *);
1888 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
1892 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
1894 /* Can still get packets with rsvp_on = 0 if there is a local member
1895 * of the group to which the RSVP packet is addressed. But in this
1896 * case we want to throw the packet away.
1904 printf("rsvp_input: check vifs\n");
1910 ifp = m->m_pkthdr.rcvif;
1913 /* Find which vif the packet arrived on. */
1914 for (vifi = 0; vifi < numvifs; vifi++)
1915 if (viftable[vifi].v_ifp == ifp)
1918 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
1920 * Drop the lock here to avoid holding it across rip_input.
1921 * This could make rsvpdebug printfs wrong. If you care,
1922 * record the state of stuff before dropping the lock.
1926 * If the old-style non-vif-associated socket is set,
1927 * then use it. Otherwise, drop packet since there
1928 * is no specific socket for this vif.
1930 if (ip_rsvpd != NULL) {
1932 printf("rsvp_input: Sending packet up old-style socket\n");
1933 rip_input(m, off); /* xxx */
1935 if (rsvpdebug && vifi == numvifs)
1936 printf("rsvp_input: Can't find vif for packet.\n");
1937 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
1938 printf("rsvp_input: No socket defined for vif %d\n",vifi);
1943 rsvp_src.sin_addr = ip->ip_src;
1946 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
1947 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
1949 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
1951 printf("rsvp_input: Failed to append to socket\n");
1954 printf("rsvp_input: send packet up\n");
1960 * Code for bandwidth monitors
1964 * Define common interface for timeval-related methods
1966 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1967 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1968 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1971 compute_bw_meter_flags(struct bw_upcall *req)
1975 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1976 flags |= BW_METER_UNIT_PACKETS;
1977 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1978 flags |= BW_METER_UNIT_BYTES;
1979 if (req->bu_flags & BW_UPCALL_GEQ)
1980 flags |= BW_METER_GEQ;
1981 if (req->bu_flags & BW_UPCALL_LEQ)
1982 flags |= BW_METER_LEQ;
1988 * Add a bw_meter entry
1991 add_bw_upcall(struct bw_upcall *req)
1994 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
1995 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2000 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2003 /* Test if the flags are valid */
2004 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2006 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2008 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2009 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2012 /* Test if the threshold time interval is valid */
2013 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2016 flags = compute_bw_meter_flags(req);
2019 * Find if we have already same bw_meter entry
2022 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2025 return EADDRNOTAVAIL;
2027 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2028 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2029 &req->bu_threshold.b_time, ==)) &&
2030 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2031 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2032 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2034 return 0; /* XXX Already installed */
2038 /* Allocate the new bw_meter entry */
2039 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2045 /* Set the new bw_meter entry */
2046 x->bm_threshold.b_time = req->bu_threshold.b_time;
2048 x->bm_start_time = now;
2049 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2050 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2051 x->bm_measured.b_packets = 0;
2052 x->bm_measured.b_bytes = 0;
2053 x->bm_flags = flags;
2054 x->bm_time_next = NULL;
2055 x->bm_time_hash = BW_METER_BUCKETS;
2057 /* Add the new bw_meter entry to the front of entries for this MFC */
2059 x->bm_mfc_next = mfc->mfc_bw_meter;
2060 mfc->mfc_bw_meter = x;
2061 schedule_bw_meter(x, &now);
2068 free_bw_list(struct bw_meter *list)
2070 while (list != NULL) {
2071 struct bw_meter *x = list;
2073 list = list->bm_mfc_next;
2074 unschedule_bw_meter(x);
2080 * Delete one or multiple bw_meter entries
2083 del_bw_upcall(struct bw_upcall *req)
2088 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2092 /* Find the corresponding MFC entry */
2093 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2096 return EADDRNOTAVAIL;
2097 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2099 * Delete all bw_meter entries for this mfc
2101 struct bw_meter *list;
2103 list = mfc->mfc_bw_meter;
2104 mfc->mfc_bw_meter = NULL;
2108 } else { /* Delete a single bw_meter entry */
2109 struct bw_meter *prev;
2112 flags = compute_bw_meter_flags(req);
2114 /* Find the bw_meter entry to delete */
2115 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2116 prev = x, x = x->bm_mfc_next) {
2117 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2118 &req->bu_threshold.b_time, ==)) &&
2119 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2120 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2121 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2124 if (x != NULL) { /* Delete entry from the list for this MFC */
2126 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2128 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2130 unschedule_bw_meter(x);
2132 /* Free the bw_meter entry */
2144 * Perform bandwidth measurement processing that may result in an upcall
2147 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2149 struct timeval delta;
2154 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2156 if (x->bm_flags & BW_METER_GEQ) {
2158 * Processing for ">=" type of bw_meter entry
2160 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2161 /* Reset the bw_meter entry */
2162 x->bm_start_time = *nowp;
2163 x->bm_measured.b_packets = 0;
2164 x->bm_measured.b_bytes = 0;
2165 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2168 /* Record that a packet is received */
2169 x->bm_measured.b_packets++;
2170 x->bm_measured.b_bytes += plen;
2173 * Test if we should deliver an upcall
2175 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2176 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2177 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2178 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2179 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2180 /* Prepare an upcall for delivery */
2181 bw_meter_prepare_upcall(x, nowp);
2182 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2185 } else if (x->bm_flags & BW_METER_LEQ) {
2187 * Processing for "<=" type of bw_meter entry
2189 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2191 * We are behind time with the multicast forwarding table
2192 * scanning for "<=" type of bw_meter entries, so test now
2193 * if we should deliver an upcall.
2195 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2196 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2197 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2198 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2199 /* Prepare an upcall for delivery */
2200 bw_meter_prepare_upcall(x, nowp);
2202 /* Reschedule the bw_meter entry */
2203 unschedule_bw_meter(x);
2204 schedule_bw_meter(x, nowp);
2207 /* Record that a packet is received */
2208 x->bm_measured.b_packets++;
2209 x->bm_measured.b_bytes += plen;
2212 * Test if we should restart the measuring interval
2214 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2215 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2216 (x->bm_flags & BW_METER_UNIT_BYTES &&
2217 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2218 /* Don't restart the measuring interval */
2220 /* Do restart the measuring interval */
2222 * XXX: note that we don't unschedule and schedule, because this
2223 * might be too much overhead per packet. Instead, when we process
2224 * all entries for a given timer hash bin, we check whether it is
2225 * really a timeout. If not, we reschedule at that time.
2227 x->bm_start_time = *nowp;
2228 x->bm_measured.b_packets = 0;
2229 x->bm_measured.b_bytes = 0;
2230 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2236 * Prepare a bandwidth-related upcall
2239 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2241 struct timeval delta;
2242 struct bw_upcall *u;
2247 * Compute the measured time interval
2250 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2253 * If there are too many pending upcalls, deliver them now
2255 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2259 * Set the bw_upcall entry
2261 u = &bw_upcalls[bw_upcalls_n++];
2262 u->bu_src = x->bm_mfc->mfc_origin;
2263 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2264 u->bu_threshold.b_time = x->bm_threshold.b_time;
2265 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2266 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2267 u->bu_measured.b_time = delta;
2268 u->bu_measured.b_packets = x->bm_measured.b_packets;
2269 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2271 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2272 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2273 if (x->bm_flags & BW_METER_UNIT_BYTES)
2274 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2275 if (x->bm_flags & BW_METER_GEQ)
2276 u->bu_flags |= BW_UPCALL_GEQ;
2277 if (x->bm_flags & BW_METER_LEQ)
2278 u->bu_flags |= BW_UPCALL_LEQ;
2282 * Send the pending bandwidth-related upcalls
2285 bw_upcalls_send(void)
2288 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2289 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2290 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2292 IGMPMSG_BW_UPCALL,/* im_msgtype */
2297 { 0 } }; /* im_dst */
2301 if (bw_upcalls_n == 0)
2302 return; /* No pending upcalls */
2307 * Allocate a new mbuf, initialize it with the header and
2308 * the payload for the pending calls.
2310 MGETHDR(m, M_DONTWAIT, MT_DATA);
2312 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2316 m->m_len = m->m_pkthdr.len = 0;
2317 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2318 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2322 * XXX do we need to set the address in k_igmpsrc ?
2324 mrtstat.mrts_upcalls++;
2325 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2326 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2327 ++mrtstat.mrts_upq_sockfull;
2332 * Compute the timeout hash value for the bw_meter entries
2334 #define BW_METER_TIMEHASH(bw_meter, hash) \
2336 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2338 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2339 (hash) = next_timeval.tv_sec; \
2340 if (next_timeval.tv_usec) \
2341 (hash)++; /* XXX: make sure we don't timeout early */ \
2342 (hash) %= BW_METER_BUCKETS; \
2346 * Schedule a timer to process periodically bw_meter entry of type "<="
2347 * by linking the entry in the proper hash bucket.
2350 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2356 if (!(x->bm_flags & BW_METER_LEQ))
2357 return; /* XXX: we schedule timers only for "<=" entries */
2360 * Reset the bw_meter entry
2362 x->bm_start_time = *nowp;
2363 x->bm_measured.b_packets = 0;
2364 x->bm_measured.b_bytes = 0;
2365 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2368 * Compute the timeout hash value and insert the entry
2370 BW_METER_TIMEHASH(x, time_hash);
2371 x->bm_time_next = bw_meter_timers[time_hash];
2372 bw_meter_timers[time_hash] = x;
2373 x->bm_time_hash = time_hash;
2377 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2378 * by removing the entry from the proper hash bucket.
2381 unschedule_bw_meter(struct bw_meter *x)
2384 struct bw_meter *prev, *tmp;
2388 if (!(x->bm_flags & BW_METER_LEQ))
2389 return; /* XXX: we schedule timers only for "<=" entries */
2392 * Compute the timeout hash value and delete the entry
2394 time_hash = x->bm_time_hash;
2395 if (time_hash >= BW_METER_BUCKETS)
2396 return; /* Entry was not scheduled */
2398 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2399 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2404 panic("unschedule_bw_meter: bw_meter entry not found");
2407 prev->bm_time_next = x->bm_time_next;
2409 bw_meter_timers[time_hash] = x->bm_time_next;
2411 x->bm_time_next = NULL;
2412 x->bm_time_hash = BW_METER_BUCKETS;
2417 * Process all "<=" type of bw_meter that should be processed now,
2418 * and for each entry prepare an upcall if necessary. Each processed
2419 * entry is rescheduled again for the (periodic) processing.
2421 * This is run periodically (once per second normally). On each round,
2422 * all the potentially matching entries are in the hash slot that we are
2428 static uint32_t last_tv_sec; /* last time we processed this */
2432 struct timeval now, process_endtime;
2435 if (last_tv_sec == now.tv_sec)
2436 return; /* nothing to do */
2438 loops = now.tv_sec - last_tv_sec;
2439 last_tv_sec = now.tv_sec;
2440 if (loops > BW_METER_BUCKETS)
2441 loops = BW_METER_BUCKETS;
2445 * Process all bins of bw_meter entries from the one after the last
2446 * processed to the current one. On entry, i points to the last bucket
2447 * visited, so we need to increment i at the beginning of the loop.
2449 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2450 struct bw_meter *x, *tmp_list;
2452 if (++i >= BW_METER_BUCKETS)
2455 /* Disconnect the list of bw_meter entries from the bin */
2456 tmp_list = bw_meter_timers[i];
2457 bw_meter_timers[i] = NULL;
2459 /* Process the list of bw_meter entries */
2460 while (tmp_list != NULL) {
2462 tmp_list = tmp_list->bm_time_next;
2464 /* Test if the time interval is over */
2465 process_endtime = x->bm_start_time;
2466 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2467 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2468 /* Not yet: reschedule, but don't reset */
2471 BW_METER_TIMEHASH(x, time_hash);
2472 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2474 * XXX: somehow the bin processing is a bit ahead of time.
2475 * Put the entry in the next bin.
2477 if (++time_hash >= BW_METER_BUCKETS)
2480 x->bm_time_next = bw_meter_timers[time_hash];
2481 bw_meter_timers[time_hash] = x;
2482 x->bm_time_hash = time_hash;
2488 * Test if we should deliver an upcall
2490 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2491 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2492 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2493 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2494 /* Prepare an upcall for delivery */
2495 bw_meter_prepare_upcall(x, &now);
2499 * Reschedule for next processing
2501 schedule_bw_meter(x, &now);
2505 /* Send all upcalls that are pending delivery */
2512 * A periodic function for sending all upcalls that are pending delivery
2515 expire_bw_upcalls_send(void *unused)
2521 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2522 expire_bw_upcalls_send, NULL);
2526 * A periodic function for periodic scanning of the multicast forwarding
2527 * table for processing all "<=" bw_meter entries.
2530 expire_bw_meter_process(void *unused)
2532 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2535 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2539 * End of bandwidth monitoring code
2543 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2547 pim_register_send(struct ip *ip, struct vif *vifp, struct mbuf *m,
2550 struct mbuf *mb_copy, *mm;
2552 if (mrtdebug & DEBUG_PIM)
2553 log(LOG_DEBUG, "pim_register_send: ");
2556 * Do not send IGMP_WHOLEPKT notifications to userland, if the
2557 * rendezvous point was unspecified, and we were told not to.
2559 if (pim_squelch_wholepkt != 0 && (mrt_api_config & MRT_MFC_RP) &&
2560 (rt->mfc_rp.s_addr == INADDR_ANY))
2563 mb_copy = pim_register_prepare(ip, m);
2564 if (mb_copy == NULL)
2568 * Send all the fragments. Note that the mbuf for each fragment
2569 * is freed by the sending machinery.
2571 for (mm = mb_copy; mm; mm = mb_copy) {
2572 mb_copy = mm->m_nextpkt;
2574 mm = m_pullup(mm, sizeof(struct ip));
2576 ip = mtod(mm, struct ip *);
2577 if ((mrt_api_config & MRT_MFC_RP) &&
2578 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2579 pim_register_send_rp(ip, vifp, mm, rt);
2581 pim_register_send_upcall(ip, vifp, mm, rt);
2590 * Return a copy of the data packet that is ready for PIM Register
2592 * XXX: Note that in the returned copy the IP header is a valid one.
2594 static struct mbuf *
2595 pim_register_prepare(struct ip *ip, struct mbuf *m)
2597 struct mbuf *mb_copy = NULL;
2600 /* Take care of delayed checksums */
2601 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2602 in_delayed_cksum(m);
2603 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2607 * Copy the old packet & pullup its IP header into the
2608 * new mbuf so we can modify it.
2610 mb_copy = m_copypacket(m, M_DONTWAIT);
2611 if (mb_copy == NULL)
2613 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2614 if (mb_copy == NULL)
2617 /* take care of the TTL */
2618 ip = mtod(mb_copy, struct ip *);
2621 /* Compute the MTU after the PIM Register encapsulation */
2622 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2624 if (ip->ip_len <= mtu) {
2625 /* Turn the IP header into a valid one */
2626 ip->ip_len = htons(ip->ip_len);
2627 ip->ip_off = htons(ip->ip_off);
2629 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2631 /* Fragment the packet */
2632 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2641 * Send an upcall with the data packet to the user-level process.
2644 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2645 struct mbuf *mb_copy, struct mfc *rt)
2647 struct mbuf *mb_first;
2648 int len = ntohs(ip->ip_len);
2650 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2655 * Add a new mbuf with an upcall header
2657 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2658 if (mb_first == NULL) {
2662 mb_first->m_data += max_linkhdr;
2663 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2664 mb_first->m_len = sizeof(struct igmpmsg);
2665 mb_first->m_next = mb_copy;
2667 /* Send message to routing daemon */
2668 im = mtod(mb_first, struct igmpmsg *);
2669 im->im_msgtype = IGMPMSG_WHOLEPKT;
2671 im->im_vif = vifp - viftable;
2672 im->im_src = ip->ip_src;
2673 im->im_dst = ip->ip_dst;
2675 k_igmpsrc.sin_addr = ip->ip_src;
2677 mrtstat.mrts_upcalls++;
2679 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2680 if (mrtdebug & DEBUG_PIM)
2682 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2683 ++mrtstat.mrts_upq_sockfull;
2687 /* Keep statistics */
2688 pimstat.pims_snd_registers_msgs++;
2689 pimstat.pims_snd_registers_bytes += len;
2695 * Encapsulate the data packet in PIM Register message and send it to the RP.
2698 pim_register_send_rp(struct ip *ip, struct vif *vifp, struct mbuf *mb_copy,
2701 struct mbuf *mb_first;
2702 struct ip *ip_outer;
2703 struct pim_encap_pimhdr *pimhdr;
2704 int len = ntohs(ip->ip_len);
2705 vifi_t vifi = rt->mfc_parent;
2709 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2711 return EADDRNOTAVAIL; /* The iif vif is invalid */
2715 * Add a new mbuf with the encapsulating header
2717 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2718 if (mb_first == NULL) {
2722 mb_first->m_data += max_linkhdr;
2723 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2724 mb_first->m_next = mb_copy;
2726 mb_first->m_pkthdr.len = len + mb_first->m_len;
2729 * Fill in the encapsulating IP and PIM header
2731 ip_outer = mtod(mb_first, struct ip *);
2732 *ip_outer = pim_encap_iphdr;
2733 ip_outer->ip_id = ip_newid();
2734 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2735 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2736 ip_outer->ip_dst = rt->mfc_rp;
2738 * Copy the inner header TOS to the outer header, and take care of the
2741 ip_outer->ip_tos = ip->ip_tos;
2742 if (ntohs(ip->ip_off) & IP_DF)
2743 ip_outer->ip_off |= IP_DF;
2744 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2745 + sizeof(pim_encap_iphdr));
2746 *pimhdr = pim_encap_pimhdr;
2747 /* If the iif crosses a border, set the Border-bit */
2748 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2749 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2751 mb_first->m_data += sizeof(pim_encap_iphdr);
2752 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2753 mb_first->m_data -= sizeof(pim_encap_iphdr);
2755 send_packet(vifp, mb_first);
2757 /* Keep statistics */
2758 pimstat.pims_snd_registers_msgs++;
2759 pimstat.pims_snd_registers_bytes += len;
2765 * pim_encapcheck() is called by the encap[46]_input() path at runtime to
2766 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2770 pim_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
2774 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2776 if (proto != IPPROTO_PIM)
2777 return 0; /* not for us; reject the datagram. */
2779 return 64; /* claim the datagram. */
2783 * PIM-SMv2 and PIM-DM messages processing.
2784 * Receives and verifies the PIM control messages, and passes them
2785 * up to the listening socket, using rip_input().
2786 * The only message with special processing is the PIM_REGISTER message
2787 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2788 * is passed to if_simloop().
2791 pim_input(struct mbuf *m, int off)
2793 struct ip *ip = mtod(m, struct ip *);
2796 int datalen = ip->ip_len;
2800 /* Keep statistics */
2801 pimstat.pims_rcv_total_msgs++;
2802 pimstat.pims_rcv_total_bytes += datalen;
2807 if (datalen < PIM_MINLEN) {
2808 pimstat.pims_rcv_tooshort++;
2809 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
2810 datalen, (u_long)ip->ip_src.s_addr);
2816 * If the packet is at least as big as a REGISTER, go agead
2817 * and grab the PIM REGISTER header size, to avoid another
2818 * possible m_pullup() later.
2820 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
2821 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2823 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2825 * Get the IP and PIM headers in contiguous memory, and
2826 * possibly the PIM REGISTER header.
2828 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
2829 (m = m_pullup(m, minlen)) == 0) {
2830 log(LOG_ERR, "pim_input: m_pullup failure\n");
2833 /* m_pullup() may have given us a new mbuf so reset ip. */
2834 ip = mtod(m, struct ip *);
2835 ip_tos = ip->ip_tos;
2837 /* adjust mbuf to point to the PIM header */
2838 m->m_data += iphlen;
2840 pim = mtod(m, struct pim *);
2843 * Validate checksum. If PIM REGISTER, exclude the data packet.
2845 * XXX: some older PIMv2 implementations don't make this distinction,
2846 * so for compatibility reason perform the checksum over part of the
2847 * message, and if error, then over the whole message.
2849 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2850 /* do nothing, checksum okay */
2851 } else if (in_cksum(m, datalen)) {
2852 pimstat.pims_rcv_badsum++;
2853 if (mrtdebug & DEBUG_PIM)
2854 log(LOG_DEBUG, "pim_input: invalid checksum");
2859 /* PIM version check */
2860 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2861 pimstat.pims_rcv_badversion++;
2862 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
2863 PIM_VT_V(pim->pim_vt), PIM_VERSION);
2868 /* restore mbuf back to the outer IP */
2869 m->m_data -= iphlen;
2872 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2874 * Since this is a REGISTER, we'll make a copy of the register
2875 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2878 struct sockaddr_in dst = { sizeof(dst), AF_INET };
2880 struct ip *encap_ip;
2885 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
2887 if (mrtdebug & DEBUG_PIM)
2889 "pim_input: register vif not set: %d\n", reg_vif_num);
2893 /* XXX need refcnt? */
2894 vifp = viftable[reg_vif_num].v_ifp;
2900 if (datalen < PIM_REG_MINLEN) {
2901 pimstat.pims_rcv_tooshort++;
2902 pimstat.pims_rcv_badregisters++;
2904 "pim_input: register packet size too small %d from %lx\n",
2905 datalen, (u_long)ip->ip_src.s_addr);
2910 reghdr = (u_int32_t *)(pim + 1);
2911 encap_ip = (struct ip *)(reghdr + 1);
2913 if (mrtdebug & DEBUG_PIM) {
2915 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
2916 (u_long)ntohl(encap_ip->ip_src.s_addr),
2917 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2918 ntohs(encap_ip->ip_len));
2921 /* verify the version number of the inner packet */
2922 if (encap_ip->ip_v != IPVERSION) {
2923 pimstat.pims_rcv_badregisters++;
2924 if (mrtdebug & DEBUG_PIM) {
2925 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
2926 "of the inner packet\n", encap_ip->ip_v);
2932 /* verify the inner packet is destined to a mcast group */
2933 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2934 pimstat.pims_rcv_badregisters++;
2935 if (mrtdebug & DEBUG_PIM)
2937 "pim_input: inner packet of register is not "
2939 (u_long)ntohl(encap_ip->ip_dst.s_addr));
2944 /* If a NULL_REGISTER, pass it to the daemon */
2945 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2946 goto pim_input_to_daemon;
2949 * Copy the TOS from the outer IP header to the inner IP header.
2951 if (encap_ip->ip_tos != ip_tos) {
2952 /* Outer TOS -> inner TOS */
2953 encap_ip->ip_tos = ip_tos;
2954 /* Recompute the inner header checksum. Sigh... */
2956 /* adjust mbuf to point to the inner IP header */
2957 m->m_data += (iphlen + PIM_MINLEN);
2958 m->m_len -= (iphlen + PIM_MINLEN);
2960 encap_ip->ip_sum = 0;
2961 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2963 /* restore mbuf to point back to the outer IP header */
2964 m->m_data -= (iphlen + PIM_MINLEN);
2965 m->m_len += (iphlen + PIM_MINLEN);
2969 * Decapsulate the inner IP packet and loopback to forward it
2970 * as a normal multicast packet. Also, make a copy of the
2971 * outer_iphdr + pimhdr + reghdr + encap_iphdr
2972 * to pass to the daemon later, so it can take the appropriate
2973 * actions (e.g., send back PIM_REGISTER_STOP).
2974 * XXX: here m->m_data points to the outer IP header.
2976 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
2979 "pim_input: pim register: could not copy register head\n");
2984 /* Keep statistics */
2985 /* XXX: registers_bytes include only the encap. mcast pkt */
2986 pimstat.pims_rcv_registers_msgs++;
2987 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
2990 * forward the inner ip packet; point m_data at the inner ip.
2992 m_adj(m, iphlen + PIM_MINLEN);
2994 if (mrtdebug & DEBUG_PIM) {
2996 "pim_input: forwarding decapsulated register: "
2997 "src %lx, dst %lx, vif %d\n",
2998 (u_long)ntohl(encap_ip->ip_src.s_addr),
2999 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3002 /* NB: vifp was collected above; can it change on us? */
3003 if_simloop(vifp, m, dst.sin_family, 0);
3005 /* prepare the register head to send to the mrouting daemon */
3009 pim_input_to_daemon:
3011 * Pass the PIM message up to the daemon; if it is a Register message,
3012 * pass the 'head' only up to the daemon. This includes the
3013 * outer IP header, PIM header, PIM-Register header and the
3015 * XXX: the outer IP header pkt size of a Register is not adjust to
3016 * reflect the fact that the inner multicast data is truncated.
3018 rip_input(m, iphlen);
3024 * XXX: This is common code for dealing with initialization for both
3025 * the IPv4 and IPv6 multicast forwarding paths. It could do with cleanup.
3028 ip_mroute_modevent(module_t mod, int type, void *unused)
3032 MROUTER_LOCK_INIT();
3036 TUNABLE_ULONG_FETCH("net.inet.pim.squelch_wholepkt",
3037 &pim_squelch_wholepkt);
3039 pim_encap_cookie = encap_attach_func(AF_INET, IPPROTO_PIM,
3040 pim_encapcheck, &in_pim_protosw, NULL);
3041 if (pim_encap_cookie == NULL) {
3042 printf("ip_mroute: unable to attach pim encap\n");
3045 MROUTER_LOCK_DESTROY();
3050 pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
3051 pim_encapcheck, (struct protosw *)&in6_pim_protosw, NULL);
3052 if (pim6_encap_cookie == NULL) {
3053 printf("ip_mroute: unable to attach pim6 encap\n");
3054 if (pim_encap_cookie) {
3055 encap_detach(pim_encap_cookie);
3056 pim_encap_cookie = NULL;
3060 MROUTER_LOCK_DESTROY();
3065 ip_mcast_src = X_ip_mcast_src;
3066 ip_mforward = X_ip_mforward;
3067 ip_mrouter_done = X_ip_mrouter_done;
3068 ip_mrouter_get = X_ip_mrouter_get;
3069 ip_mrouter_set = X_ip_mrouter_set;
3072 ip6_mforward = X_ip6_mforward;
3073 ip6_mrouter_done = X_ip6_mrouter_done;
3074 ip6_mrouter_get = X_ip6_mrouter_get;
3075 ip6_mrouter_set = X_ip6_mrouter_set;
3076 mrt6_ioctl = X_mrt6_ioctl;
3079 ip_rsvp_force_done = X_ip_rsvp_force_done;
3080 ip_rsvp_vif = X_ip_rsvp_vif;
3082 legal_vif_num = X_legal_vif_num;
3083 mrt_ioctl = X_mrt_ioctl;
3084 rsvp_input_p = X_rsvp_input;
3089 * Typically module unload happens after the user-level
3090 * process has shutdown the kernel services (the check
3091 * below insures someone can't just yank the module out
3092 * from under a running process). But if the module is
3093 * just loaded and then unloaded w/o starting up a user
3094 * process we still need to cleanup.
3104 if (pim6_encap_cookie) {
3105 encap_detach(pim6_encap_cookie);
3106 pim6_encap_cookie = NULL;
3108 X_ip6_mrouter_done();
3109 ip6_mforward = NULL;
3110 ip6_mrouter_done = NULL;
3111 ip6_mrouter_get = NULL;
3112 ip6_mrouter_set = NULL;
3116 if (pim_encap_cookie) {
3117 encap_detach(pim_encap_cookie);
3118 pim_encap_cookie = NULL;
3120 X_ip_mrouter_done();
3121 ip_mcast_src = NULL;
3123 ip_mrouter_done = NULL;
3124 ip_mrouter_get = NULL;
3125 ip_mrouter_set = NULL;
3127 ip_rsvp_force_done = NULL;
3130 legal_vif_num = NULL;
3132 rsvp_input_p = NULL;
3136 MROUTER_LOCK_DESTROY();
3145 static moduledata_t ip_mroutemod = {
3150 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);