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() do { \
153 mtx_assert(&mrouter_mtx, MA_OWNED); \
154 NET_ASSERT_GIANT(); \
156 #define MROUTER_LOCK_INIT() \
157 mtx_init(&mrouter_mtx, "IPv4 multicast forwarding", NULL, MTX_DEF)
158 #define MROUTER_LOCK_DESTROY() mtx_destroy(&mrouter_mtx)
160 static struct mtx mfc_mtx;
161 #define MFC_LOCK() mtx_lock(&mfc_mtx)
162 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
163 #define MFC_LOCK_ASSERT() do { \
164 mtx_assert(&mfc_mtx, MA_OWNED); \
165 NET_ASSERT_GIANT(); \
167 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
168 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
170 static struct vif viftable[MAXVIFS];
171 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
172 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
173 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
175 static struct mtx vif_mtx;
176 #define VIF_LOCK() mtx_lock(&vif_mtx)
177 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
178 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
179 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
180 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
182 static u_char nexpire[MFCTBLSIZ];
184 static eventhandler_tag if_detach_event_tag = NULL;
186 static struct callout expire_upcalls_ch;
188 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
189 #define UPCALL_EXPIRE 6 /* number of timeouts */
194 * Bandwidth meter variables and constants
196 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
198 * Pending timeouts are stored in a hash table, the key being the
199 * expiration time. Periodically, the entries are analysed and processed.
201 #define BW_METER_BUCKETS 1024
202 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
203 static struct callout bw_meter_ch;
204 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
207 * Pending upcalls are stored in a vector which is flushed when
208 * full, or periodically
210 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
211 static u_int bw_upcalls_n; /* # of pending upcalls */
212 static struct callout bw_upcalls_ch;
213 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
215 static struct pimstat pimstat;
217 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
218 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
220 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
222 static u_long pim_squelch_wholepkt = 0;
223 SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RW,
224 &pim_squelch_wholepkt, 0,
225 "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
227 extern struct domain inetdomain;
228 struct protosw in_pim_protosw = {
230 .pr_domain = &inetdomain,
231 .pr_protocol = IPPROTO_PIM,
232 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
233 .pr_input = pim_input,
234 .pr_output = (pr_output_t*)rip_output,
235 .pr_ctloutput = rip_ctloutput,
236 .pr_usrreqs = &rip_usrreqs
238 static const struct encaptab *pim_encap_cookie;
241 /* ip6_mroute.c glue */
242 extern struct in6_protosw in6_pim_protosw;
243 static const struct encaptab *pim6_encap_cookie;
245 extern int X_ip6_mrouter_set(struct socket *, struct sockopt *);
246 extern int X_ip6_mrouter_get(struct socket *, struct sockopt *);
247 extern int X_ip6_mrouter_done(void);
248 extern int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
249 extern int X_mrt6_ioctl(int, caddr_t);
252 static int pim_encapcheck(const struct mbuf *, int, int, void *);
255 * Note: the PIM Register encapsulation adds the following in front of a
258 * struct pim_encap_hdr {
260 * struct pim_encap_pimhdr pim;
265 struct pim_encap_pimhdr {
270 static struct ip pim_encap_iphdr = {
271 #if BYTE_ORDER == LITTLE_ENDIAN
272 sizeof(struct ip) >> 2,
276 sizeof(struct ip) >> 2,
279 sizeof(struct ip), /* total length */
287 static struct pim_encap_pimhdr pim_encap_pimhdr = {
289 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
296 static struct ifnet multicast_register_if;
297 static vifi_t reg_vif_num = VIFI_INVALID;
302 static vifi_t numvifs;
304 static u_long X_ip_mcast_src(int vifi);
305 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
306 struct mbuf *m, struct ip_moptions *imo);
307 static int X_ip_mrouter_done(void);
308 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
309 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
310 static int X_legal_vif_num(int vif);
311 static int X_mrt_ioctl(int cmd, caddr_t data);
313 static int get_sg_cnt(struct sioc_sg_req *);
314 static int get_vif_cnt(struct sioc_vif_req *);
315 static void if_detached_event(void *arg __unused, struct ifnet *);
316 static int ip_mrouter_init(struct socket *, int);
317 static int add_vif(struct vifctl *);
318 static int del_vif_locked(vifi_t);
319 static int del_vif(vifi_t);
320 static int add_mfc(struct mfcctl2 *);
321 static int del_mfc(struct mfcctl2 *);
322 static int set_api_config(uint32_t *); /* chose API capabilities */
323 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
324 static int set_assert(int);
325 static void expire_upcalls(void *);
326 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
327 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
328 static void send_packet(struct vif *, struct mbuf *);
331 * Bandwidth monitoring
333 static void free_bw_list(struct bw_meter *list);
334 static int add_bw_upcall(struct bw_upcall *);
335 static int del_bw_upcall(struct bw_upcall *);
336 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
337 struct timeval *nowp);
338 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
339 static void bw_upcalls_send(void);
340 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
341 static void unschedule_bw_meter(struct bw_meter *x);
342 static void bw_meter_process(void);
343 static void expire_bw_upcalls_send(void *);
344 static void expire_bw_meter_process(void *);
346 static int pim_register_send(struct ip *, struct vif *,
347 struct mbuf *, struct mfc *);
348 static int pim_register_send_rp(struct ip *, struct vif *,
349 struct mbuf *, struct mfc *);
350 static int pim_register_send_upcall(struct ip *, struct vif *,
351 struct mbuf *, struct mfc *);
352 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 * Statistics are updated by the caller if needed
383 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
386 mfc_find(in_addr_t o, in_addr_t g)
392 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
393 if ((rt->mfc_origin.s_addr == o) &&
394 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
400 * Macros to compute elapsed time efficiently
401 * Borrowed from Van Jacobson's scheduling code
403 #define TV_DELTA(a, b, delta) { \
405 delta = (a).tv_usec - (b).tv_usec; \
406 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
415 delta += (1000000 * xxs); \
420 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
421 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
424 * Handle MRT setsockopt commands to modify the multicast routing tables.
427 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
433 struct bw_upcall bw_upcall;
436 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
440 switch (sopt->sopt_name) {
442 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
445 error = ip_mrouter_init(so, optval);
449 error = ip_mrouter_done();
453 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
456 error = add_vif(&vifc);
460 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
463 error = del_vif(vifi);
469 * select data size depending on API version.
471 if (sopt->sopt_name == MRT_ADD_MFC &&
472 mrt_api_config & MRT_API_FLAGS_ALL) {
473 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
474 sizeof(struct mfcctl2));
476 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
477 sizeof(struct mfcctl));
478 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
479 sizeof(mfc) - sizeof(struct mfcctl));
483 if (sopt->sopt_name == MRT_ADD_MFC)
484 error = add_mfc(&mfc);
486 error = del_mfc(&mfc);
490 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
497 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
499 error = set_api_config(&i);
501 error = sooptcopyout(sopt, &i, sizeof i);
504 case MRT_ADD_BW_UPCALL:
505 case MRT_DEL_BW_UPCALL:
506 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
510 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
511 error = add_bw_upcall(&bw_upcall);
513 error = del_bw_upcall(&bw_upcall);
524 * Handle MRT getsockopt commands
527 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
530 static int version = 0x0305; /* !!! why is this here? XXX */
532 switch (sopt->sopt_name) {
534 error = sooptcopyout(sopt, &version, sizeof version);
538 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
541 case MRT_API_SUPPORT:
542 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
546 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
557 * Handle ioctl commands to obtain information from the cache
560 X_mrt_ioctl(int cmd, caddr_t data)
565 * Currently the only function calling this ioctl routine is rtioctl().
566 * Typically, only root can create the raw socket in order to execute
567 * this ioctl method, however the request might be coming from a prison
569 error = priv_check(curthread, PRIV_NETINET_MROUTE);
573 case (SIOCGETVIFCNT):
574 error = get_vif_cnt((struct sioc_vif_req *)data);
578 error = get_sg_cnt((struct sioc_sg_req *)data);
589 * returns the packet, byte, rpf-failure count for the source group provided
592 get_sg_cnt(struct sioc_sg_req *req)
597 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
600 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
601 return EADDRNOTAVAIL;
603 req->pktcnt = rt->mfc_pkt_cnt;
604 req->bytecnt = rt->mfc_byte_cnt;
605 req->wrong_if = rt->mfc_wrong_if;
611 * returns the input and output packet and byte counts on the vif provided
614 get_vif_cnt(struct sioc_vif_req *req)
616 vifi_t vifi = req->vifi;
619 if (vifi >= numvifs) {
624 req->icount = viftable[vifi].v_pkt_in;
625 req->ocount = viftable[vifi].v_pkt_out;
626 req->ibytes = viftable[vifi].v_bytes_in;
627 req->obytes = viftable[vifi].v_bytes_out;
634 ip_mrouter_reset(void)
636 bzero((caddr_t)mfctable, sizeof(mfctable));
637 bzero((caddr_t)nexpire, sizeof(nexpire));
642 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
645 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
646 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
647 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
651 if_detached_event(void *arg __unused, struct ifnet *ifp)
657 struct mfc **ppmfc; /* Pointer to previous node's next-pointer */
662 if (ip_mrouter == NULL) {
667 * Tear down multicast forwarder state associated with this ifnet.
668 * 1. Walk the vif list, matching vifs against this ifnet.
669 * 2. Walk the multicast forwarding cache (mfc) looking for
670 * inner matches with this vif's index.
671 * 3. Free any pending mbufs for this mfc.
672 * 4. Free the associated mfc entry and state associated with this vif.
673 * Be very careful about unlinking from a singly-linked list whose
674 * "head node" is a pointer in a simple array.
675 * 5. Free vif state. This should disable ALLMULTI on the interface.
679 for (vifi = 0; vifi < numvifs; vifi++) {
680 if (viftable[vifi].v_ifp != ifp)
682 for (i = 0; i < MFCTBLSIZ; i++) {
683 ppmfc = &mfctable[i];
684 for (mfc = mfctable[i]; mfc != NULL; ) {
685 nmfc = mfc->mfc_next;
686 if (mfc->mfc_parent == vifi) {
687 for (pq = mfc->mfc_stall; pq != NULL; ) {
693 free_bw_list(mfc->mfc_bw_meter);
694 free(mfc, M_MRTABLE);
697 ppmfc = &mfc->mfc_next;
702 del_vif_locked(vifi);
711 * Enable multicast routing
714 ip_mrouter_init(struct socket *so, int version)
717 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
718 so->so_type, so->so_proto->pr_protocol);
720 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
728 if (ip_mrouter != NULL) {
733 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
734 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
735 if (if_detach_event_tag == NULL) {
740 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
742 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
743 expire_bw_upcalls_send, NULL);
744 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
751 log(LOG_DEBUG, "ip_mrouter_init\n");
757 * Disable multicast routing
760 X_ip_mrouter_done(void)
771 if (ip_mrouter == NULL) {
777 * Detach/disable hooks to the reset of the system.
784 * For each phyint in use, disable promiscuous reception of all IP
787 for (vifi = 0; vifi < numvifs; vifi++) {
788 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
789 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
790 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
792 so->sin_len = sizeof(struct sockaddr_in);
793 so->sin_family = AF_INET;
794 so->sin_addr.s_addr = INADDR_ANY;
795 ifp = viftable[vifi].v_ifp;
799 bzero((caddr_t)viftable, sizeof(viftable));
803 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
806 * Free all multicast forwarding cache entries.
808 callout_stop(&expire_upcalls_ch);
809 callout_stop(&bw_upcalls_ch);
810 callout_stop(&bw_meter_ch);
813 for (i = 0; i < MFCTBLSIZ; i++) {
814 for (rt = mfctable[i]; rt != NULL; ) {
815 struct mfc *nr = rt->mfc_next;
817 for (rte = rt->mfc_stall; rte != NULL; ) {
818 struct rtdetq *n = rte->next;
821 free(rte, M_MRTABLE);
824 free_bw_list(rt->mfc_bw_meter);
829 bzero((caddr_t)mfctable, sizeof(mfctable));
830 bzero((caddr_t)nexpire, sizeof(nexpire));
832 bzero(bw_meter_timers, sizeof(bw_meter_timers));
835 reg_vif_num = VIFI_INVALID;
840 log(LOG_DEBUG, "ip_mrouter_done\n");
846 * Set PIM assert processing global
851 if ((i != 1) && (i != 0))
860 * Configure API capabilities
863 set_api_config(uint32_t *apival)
868 * We can set the API capabilities only if it is the first operation
869 * after MRT_INIT. I.e.:
870 * - there are no vifs installed
871 * - pim_assert is not enabled
872 * - the MFC table is empty
882 for (i = 0; i < MFCTBLSIZ; i++) {
883 if (mfctable[i] != NULL) {
889 mrt_api_config = *apival & mrt_api_support;
890 *apival = mrt_api_config;
896 * Add a vif to the vif table
899 add_vif(struct vifctl *vifcp)
901 struct vif *vifp = viftable + vifcp->vifc_vifi;
902 struct sockaddr_in sin = {sizeof sin, AF_INET};
908 if (vifcp->vifc_vifi >= MAXVIFS) {
912 /* rate limiting is no longer supported by this code */
913 if (vifcp->vifc_rate_limit != 0) {
914 log(LOG_ERR, "rate limiting is no longer supported\n");
918 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
922 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
924 return EADDRNOTAVAIL;
927 /* Find the interface with an address in AF_INET family */
928 if (vifcp->vifc_flags & VIFF_REGISTER) {
930 * XXX: Because VIFF_REGISTER does not really need a valid
931 * local interface (e.g. it could be 127.0.0.2), we don't
936 sin.sin_addr = vifcp->vifc_lcl_addr;
937 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
940 return EADDRNOTAVAIL;
945 if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
946 log(LOG_ERR, "tunnels are no longer supported\n");
949 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
950 ifp = &multicast_register_if;
952 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
953 (void *)&multicast_register_if);
954 if (reg_vif_num == VIFI_INVALID) {
955 if_initname(&multicast_register_if, "register_vif", 0);
956 multicast_register_if.if_flags = IFF_LOOPBACK;
957 reg_vif_num = vifcp->vifc_vifi;
959 } else { /* Make sure the interface supports multicast */
960 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
965 /* Enable promiscuous reception of all IP multicasts from the if */
966 error = if_allmulti(ifp, 1);
973 vifp->v_flags = vifcp->vifc_flags;
974 vifp->v_threshold = vifcp->vifc_threshold;
975 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
976 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
979 vifp->v_rsvpd = NULL;
980 /* initialize per vif pkt counters */
983 vifp->v_bytes_in = 0;
984 vifp->v_bytes_out = 0;
985 bzero(&vifp->v_route, sizeof(vifp->v_route));
987 /* Adjust numvifs up if the vifi is higher than numvifs */
988 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
993 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x\n",
995 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
996 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
997 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
998 vifcp->vifc_threshold);
1004 * Delete a vif from the vif table
1007 del_vif_locked(vifi_t vifi)
1013 if (vifi >= numvifs) {
1016 vifp = &viftable[vifi];
1017 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1018 return EADDRNOTAVAIL;
1021 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1022 if_allmulti(vifp->v_ifp, 0);
1024 if (vifp->v_flags & VIFF_REGISTER)
1025 reg_vif_num = VIFI_INVALID;
1027 bzero((caddr_t)vifp, sizeof (*vifp));
1030 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1032 /* Adjust numvifs down */
1033 for (vifi = numvifs; vifi > 0; vifi--)
1034 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1042 del_vif(vifi_t vifi)
1047 cc = del_vif_locked(vifi);
1054 * update an mfc entry without resetting counters and S,G addresses.
1057 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1061 rt->mfc_parent = mfccp->mfcc_parent;
1062 for (i = 0; i < numvifs; i++) {
1063 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1064 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1067 /* set the RP address */
1068 if (mrt_api_config & MRT_MFC_RP)
1069 rt->mfc_rp = mfccp->mfcc_rp;
1071 rt->mfc_rp.s_addr = INADDR_ANY;
1075 * fully initialize an mfc entry from the parameter.
1078 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1080 rt->mfc_origin = mfccp->mfcc_origin;
1081 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1083 update_mfc_params(rt, mfccp);
1085 /* initialize pkt counters per src-grp */
1086 rt->mfc_pkt_cnt = 0;
1087 rt->mfc_byte_cnt = 0;
1088 rt->mfc_wrong_if = 0;
1089 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1097 add_mfc(struct mfcctl2 *mfccp)
1107 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1109 /* If an entry already exists, just update the fields */
1111 if (mrtdebug & DEBUG_MFC)
1112 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1113 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1114 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1115 mfccp->mfcc_parent);
1117 update_mfc_params(rt, mfccp);
1124 * Find the entry for which the upcall was made and update
1126 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1127 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1129 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1130 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1131 (rt->mfc_stall != NULL)) {
1134 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1135 "multiple kernel entries",
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 if (mrtdebug & DEBUG_MFC)
1141 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1142 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1143 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1144 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1146 init_mfc_params(rt, mfccp);
1148 rt->mfc_expire = 0; /* Don't clean this guy up */
1151 /* free packets Qed at the end of this entry */
1152 for (rte = rt->mfc_stall; rte != NULL; ) {
1153 struct rtdetq *n = rte->next;
1155 ip_mdq(rte->m, rte->ifp, rt, -1);
1157 free(rte, M_MRTABLE);
1160 rt->mfc_stall = NULL;
1165 * It is possible that an entry is being inserted without an upcall
1168 if (mrtdebug & DEBUG_MFC)
1169 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1170 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1171 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1172 mfccp->mfcc_parent);
1174 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1175 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1176 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1177 init_mfc_params(rt, mfccp);
1184 if (rt == NULL) { /* no upcall, so make a new entry */
1185 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1192 init_mfc_params(rt, mfccp);
1194 rt->mfc_stall = NULL;
1196 rt->mfc_bw_meter = NULL;
1197 /* insert new entry at head of hash chain */
1198 rt->mfc_next = mfctable[hash];
1199 mfctable[hash] = rt;
1208 * Delete an mfc entry
1211 del_mfc(struct mfcctl2 *mfccp)
1213 struct in_addr origin;
1214 struct in_addr mcastgrp;
1218 struct bw_meter *list;
1220 origin = mfccp->mfcc_origin;
1221 mcastgrp = mfccp->mfcc_mcastgrp;
1223 if (mrtdebug & DEBUG_MFC)
1224 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1225 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1229 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1230 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1231 if (origin.s_addr == rt->mfc_origin.s_addr &&
1232 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1233 rt->mfc_stall == NULL)
1237 return EADDRNOTAVAIL;
1240 *nptr = rt->mfc_next;
1243 * free the bw_meter entries
1245 list = rt->mfc_bw_meter;
1246 rt->mfc_bw_meter = NULL;
1248 free(rt, M_MRTABLE);
1258 * Send a message to the routing daemon on the multicast routing socket
1261 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1264 SOCKBUF_LOCK(&s->so_rcv);
1265 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1267 sorwakeup_locked(s);
1270 SOCKBUF_UNLOCK(&s->so_rcv);
1277 * IP multicast forwarding function. This function assumes that the packet
1278 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1279 * pointed to by "ifp", and the packet is to be relayed to other networks
1280 * that have members of the packet's destination IP multicast group.
1282 * The packet is returned unscathed to the caller, unless it is
1283 * erroneous, in which case a non-zero return value tells the caller to
1287 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1290 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1291 struct ip_moptions *imo)
1297 if (mrtdebug & DEBUG_FORWARD)
1298 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1299 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1302 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1303 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1305 * Packet arrived via a physical interface or
1306 * an encapsulated tunnel or a register_vif.
1310 * Packet arrived through a source-route tunnel.
1311 * Source-route tunnels are no longer supported.
1313 static int last_log;
1314 if (last_log != time_uptime) {
1315 last_log = time_uptime;
1317 "ip_mforward: received source-routed packet from %lx\n",
1318 (u_long)ntohl(ip->ip_src.s_addr));
1325 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1326 if (ip->ip_ttl < MAXTTL)
1327 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1328 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1329 struct vif *vifp = viftable + vifi;
1331 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1332 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1334 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1335 vifp->v_ifp->if_xname);
1337 error = ip_mdq(m, ifp, NULL, vifi);
1342 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1343 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1344 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1346 printf("In fact, no options were specified at all\n");
1350 * Don't forward a packet with time-to-live of zero or one,
1351 * or a packet destined to a local-only group.
1353 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr))) {
1360 * Determine forwarding vifs from the forwarding cache table
1362 ++mrtstat.mrts_mfc_lookups;
1363 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1365 /* Entry exists, so forward if necessary */
1367 error = ip_mdq(m, ifp, rt, -1);
1373 * If we don't have a route for packet's origin,
1374 * Make a copy of the packet & send message to routing daemon
1380 int hlen = ip->ip_hl << 2;
1382 ++mrtstat.mrts_mfc_misses;
1384 mrtstat.mrts_no_route++;
1385 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1386 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1387 (u_long)ntohl(ip->ip_src.s_addr),
1388 (u_long)ntohl(ip->ip_dst.s_addr));
1391 * Allocate mbufs early so that we don't do extra work if we are
1392 * just going to fail anyway. Make sure to pullup the header so
1393 * that other people can't step on it.
1395 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1401 mb0 = m_copypacket(m, M_DONTWAIT);
1402 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1403 mb0 = m_pullup(mb0, hlen);
1405 free(rte, M_MRTABLE);
1411 /* is there an upcall waiting for this flow ? */
1412 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1413 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1414 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1415 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1416 (rt->mfc_stall != NULL))
1423 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1427 * Locate the vifi for the incoming interface for this packet.
1428 * If none found, drop packet.
1430 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1432 if (vifi >= numvifs) /* vif not found, drop packet */
1435 /* no upcall, so make a new entry */
1436 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1439 /* Make a copy of the header to send to the user level process */
1440 mm = m_copy(mb0, 0, hlen);
1445 * Send message to routing daemon to install
1446 * a route into the kernel table
1449 im = mtod(mm, struct igmpmsg *);
1450 im->im_msgtype = IGMPMSG_NOCACHE;
1454 mrtstat.mrts_upcalls++;
1456 k_igmpsrc.sin_addr = ip->ip_src;
1457 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1458 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1459 ++mrtstat.mrts_upq_sockfull;
1461 free(rt, M_MRTABLE);
1463 free(rte, M_MRTABLE);
1470 /* insert new entry at head of hash chain */
1471 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1472 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1473 rt->mfc_expire = UPCALL_EXPIRE;
1475 for (i = 0; i < numvifs; i++) {
1476 rt->mfc_ttls[i] = 0;
1477 rt->mfc_flags[i] = 0;
1479 rt->mfc_parent = -1;
1481 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1483 rt->mfc_bw_meter = NULL;
1485 /* link into table */
1486 rt->mfc_next = mfctable[hash];
1487 mfctable[hash] = rt;
1488 rt->mfc_stall = rte;
1491 /* determine if q has overflowed */
1496 * XXX ouch! we need to append to the list, but we
1497 * only have a pointer to the front, so we have to
1498 * scan the entire list every time.
1500 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1503 if (npkts > MAX_UPQ) {
1504 mrtstat.mrts_upq_ovflw++;
1506 free(rte, M_MRTABLE);
1513 /* Add this entry to the end of the queue */
1529 * Clean up the cache entry if upcall is not serviced
1532 expire_upcalls(void *unused)
1535 struct mfc *mfc, **nptr;
1539 for (i = 0; i < MFCTBLSIZ; i++) {
1540 if (nexpire[i] == 0)
1542 nptr = &mfctable[i];
1543 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1545 * Skip real cache entries
1546 * Make sure it wasn't marked to not expire (shouldn't happen)
1549 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1550 --mfc->mfc_expire == 0) {
1551 if (mrtdebug & DEBUG_EXPIRE)
1552 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1553 (u_long)ntohl(mfc->mfc_origin.s_addr),
1554 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1556 * drop all the packets
1557 * free the mbuf with the pkt, if, timing info
1559 for (rte = mfc->mfc_stall; rte; ) {
1560 struct rtdetq *n = rte->next;
1563 free(rte, M_MRTABLE);
1566 ++mrtstat.mrts_cache_cleanups;
1570 * free the bw_meter entries
1572 while (mfc->mfc_bw_meter != NULL) {
1573 struct bw_meter *x = mfc->mfc_bw_meter;
1575 mfc->mfc_bw_meter = x->bm_mfc_next;
1579 *nptr = mfc->mfc_next;
1580 free(mfc, M_MRTABLE);
1582 nptr = &mfc->mfc_next;
1588 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1592 * Packet forwarding routine once entry in the cache is made
1595 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1597 struct ip *ip = mtod(m, struct ip *);
1599 int plen = ip->ip_len;
1604 * If xmt_vif is not -1, send on only the requested vif.
1606 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1608 if (xmt_vif < numvifs) {
1609 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1610 pim_register_send(ip, viftable + xmt_vif, m, rt);
1612 phyint_send(ip, viftable + xmt_vif, m);
1617 * Don't forward if it didn't arrive from the parent vif for its origin.
1619 vifi = rt->mfc_parent;
1620 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1621 /* came in the wrong interface */
1622 if (mrtdebug & DEBUG_FORWARD)
1623 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1624 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1625 ++mrtstat.mrts_wrong_if;
1628 * If we are doing PIM assert processing, send a message
1629 * to the routing daemon.
1631 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1632 * can complete the SPT switch, regardless of the type
1633 * of the iif (broadcast media, GRE tunnel, etc).
1635 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1639 if (ifp == &multicast_register_if)
1640 pimstat.pims_rcv_registers_wrongiif++;
1642 /* Get vifi for the incoming packet */
1643 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1645 if (vifi >= numvifs)
1646 return 0; /* The iif is not found: ignore the packet. */
1648 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1649 return 0; /* WRONGVIF disabled: ignore the packet */
1653 TV_DELTA(now, rt->mfc_last_assert, delta);
1655 if (delta > ASSERT_MSG_TIME) {
1656 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1658 int hlen = ip->ip_hl << 2;
1659 struct mbuf *mm = m_copy(m, 0, hlen);
1661 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1662 mm = m_pullup(mm, hlen);
1666 rt->mfc_last_assert = now;
1668 im = mtod(mm, struct igmpmsg *);
1669 im->im_msgtype = IGMPMSG_WRONGVIF;
1673 mrtstat.mrts_upcalls++;
1675 k_igmpsrc.sin_addr = im->im_src;
1676 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1678 "ip_mforward: ip_mrouter socket queue full\n");
1679 ++mrtstat.mrts_upq_sockfull;
1687 /* If I sourced this packet, it counts as output, else it was input. */
1688 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1689 viftable[vifi].v_pkt_out++;
1690 viftable[vifi].v_bytes_out += plen;
1692 viftable[vifi].v_pkt_in++;
1693 viftable[vifi].v_bytes_in += plen;
1696 rt->mfc_byte_cnt += plen;
1699 * For each vif, decide if a copy of the packet should be forwarded.
1701 * - the ttl exceeds the vif's threshold
1702 * - there are group members downstream on interface
1704 for (vifi = 0; vifi < numvifs; vifi++)
1705 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1706 viftable[vifi].v_pkt_out++;
1707 viftable[vifi].v_bytes_out += plen;
1708 if (viftable[vifi].v_flags & VIFF_REGISTER)
1709 pim_register_send(ip, viftable + vifi, m, rt);
1711 phyint_send(ip, viftable + vifi, m);
1715 * Perform upcall-related bw measuring.
1717 if (rt->mfc_bw_meter != NULL) {
1723 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1724 bw_meter_receive_packet(x, plen, &now);
1731 * check if a vif number is legal/ok. This is used by ip_output.
1734 X_legal_vif_num(int vif)
1736 /* XXX unlocked, matter? */
1737 return (vif >= 0 && vif < numvifs);
1741 * Return the local address used by this vif
1744 X_ip_mcast_src(int vifi)
1746 /* XXX unlocked, matter? */
1747 if (vifi >= 0 && vifi < numvifs)
1748 return viftable[vifi].v_lcl_addr.s_addr;
1754 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1756 struct mbuf *mb_copy;
1757 int hlen = ip->ip_hl << 2;
1762 * Make a new reference to the packet; make sure that
1763 * the IP header is actually copied, not just referenced,
1764 * so that ip_output() only scribbles on the copy.
1766 mb_copy = m_copypacket(m, M_DONTWAIT);
1767 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1768 mb_copy = m_pullup(mb_copy, hlen);
1769 if (mb_copy == NULL)
1772 send_packet(vifp, mb_copy);
1776 send_packet(struct vif *vifp, struct mbuf *m)
1778 struct ip_moptions imo;
1779 struct in_multi *imm[2];
1784 imo.imo_multicast_ifp = vifp->v_ifp;
1785 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1786 imo.imo_multicast_loop = 1;
1787 imo.imo_multicast_vif = -1;
1788 imo.imo_num_memberships = 0;
1789 imo.imo_max_memberships = 2;
1790 imo.imo_membership = &imm[0];
1793 * Re-entrancy should not be a problem here, because
1794 * the packets that we send out and are looped back at us
1795 * should get rejected because they appear to come from
1796 * the loopback interface, thus preventing looping.
1798 error = ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, &imo, NULL);
1799 if (mrtdebug & DEBUG_XMIT) {
1800 log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
1801 vifp - viftable, error);
1806 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
1810 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1813 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
1819 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
1821 return EADDRNOTAVAIL;
1824 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
1825 /* Check if socket is available. */
1826 if (viftable[vifi].v_rsvpd != NULL) {
1831 viftable[vifi].v_rsvpd = so;
1832 /* This may seem silly, but we need to be sure we don't over-increment
1833 * the RSVP counter, in case something slips up.
1835 if (!viftable[vifi].v_rsvp_on) {
1836 viftable[vifi].v_rsvp_on = 1;
1839 } else { /* must be VIF_OFF */
1841 * XXX as an additional consistency check, one could make sure
1842 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
1843 * first parameter is pretty useless.
1845 viftable[vifi].v_rsvpd = NULL;
1847 * This may seem silly, but we need to be sure we don't over-decrement
1848 * the RSVP counter, in case something slips up.
1850 if (viftable[vifi].v_rsvp_on) {
1851 viftable[vifi].v_rsvp_on = 0;
1860 X_ip_rsvp_force_done(struct socket *so)
1864 /* Don't bother if it is not the right type of socket. */
1865 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1870 /* The socket may be attached to more than one vif...this
1871 * is perfectly legal.
1873 for (vifi = 0; vifi < numvifs; vifi++) {
1874 if (viftable[vifi].v_rsvpd == so) {
1875 viftable[vifi].v_rsvpd = NULL;
1876 /* This may seem silly, but we need to be sure we don't
1877 * over-decrement the RSVP counter, in case something slips up.
1879 if (viftable[vifi].v_rsvp_on) {
1880 viftable[vifi].v_rsvp_on = 0;
1890 X_rsvp_input(struct mbuf *m, int off)
1893 struct ip *ip = mtod(m, struct ip *);
1894 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
1898 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
1900 /* Can still get packets with rsvp_on = 0 if there is a local member
1901 * of the group to which the RSVP packet is addressed. But in this
1902 * case we want to throw the packet away.
1910 printf("rsvp_input: check vifs\n");
1916 ifp = m->m_pkthdr.rcvif;
1919 /* Find which vif the packet arrived on. */
1920 for (vifi = 0; vifi < numvifs; vifi++)
1921 if (viftable[vifi].v_ifp == ifp)
1924 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
1926 * Drop the lock here to avoid holding it across rip_input.
1927 * This could make rsvpdebug printfs wrong. If you care,
1928 * record the state of stuff before dropping the lock.
1932 * If the old-style non-vif-associated socket is set,
1933 * then use it. Otherwise, drop packet since there
1934 * is no specific socket for this vif.
1936 if (ip_rsvpd != NULL) {
1938 printf("rsvp_input: Sending packet up old-style socket\n");
1939 rip_input(m, off); /* xxx */
1941 if (rsvpdebug && vifi == numvifs)
1942 printf("rsvp_input: Can't find vif for packet.\n");
1943 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
1944 printf("rsvp_input: No socket defined for vif %d\n",vifi);
1949 rsvp_src.sin_addr = ip->ip_src;
1952 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
1953 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
1955 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
1957 printf("rsvp_input: Failed to append to socket\n");
1960 printf("rsvp_input: send packet up\n");
1966 * Code for bandwidth monitors
1970 * Define common interface for timeval-related methods
1972 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1973 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1974 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1977 compute_bw_meter_flags(struct bw_upcall *req)
1981 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1982 flags |= BW_METER_UNIT_PACKETS;
1983 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1984 flags |= BW_METER_UNIT_BYTES;
1985 if (req->bu_flags & BW_UPCALL_GEQ)
1986 flags |= BW_METER_GEQ;
1987 if (req->bu_flags & BW_UPCALL_LEQ)
1988 flags |= BW_METER_LEQ;
1994 * Add a bw_meter entry
1997 add_bw_upcall(struct bw_upcall *req)
2000 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2001 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2006 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2009 /* Test if the flags are valid */
2010 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2012 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2014 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2015 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2018 /* Test if the threshold time interval is valid */
2019 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2022 flags = compute_bw_meter_flags(req);
2025 * Find if we have already same bw_meter entry
2028 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2031 return EADDRNOTAVAIL;
2033 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2034 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2035 &req->bu_threshold.b_time, ==)) &&
2036 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2037 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2038 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2040 return 0; /* XXX Already installed */
2044 /* Allocate the new bw_meter entry */
2045 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2051 /* Set the new bw_meter entry */
2052 x->bm_threshold.b_time = req->bu_threshold.b_time;
2054 x->bm_start_time = now;
2055 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2056 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2057 x->bm_measured.b_packets = 0;
2058 x->bm_measured.b_bytes = 0;
2059 x->bm_flags = flags;
2060 x->bm_time_next = NULL;
2061 x->bm_time_hash = BW_METER_BUCKETS;
2063 /* Add the new bw_meter entry to the front of entries for this MFC */
2065 x->bm_mfc_next = mfc->mfc_bw_meter;
2066 mfc->mfc_bw_meter = x;
2067 schedule_bw_meter(x, &now);
2074 free_bw_list(struct bw_meter *list)
2076 while (list != NULL) {
2077 struct bw_meter *x = list;
2079 list = list->bm_mfc_next;
2080 unschedule_bw_meter(x);
2086 * Delete one or multiple bw_meter entries
2089 del_bw_upcall(struct bw_upcall *req)
2094 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2098 /* Find the corresponding MFC entry */
2099 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2102 return EADDRNOTAVAIL;
2103 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2105 * Delete all bw_meter entries for this mfc
2107 struct bw_meter *list;
2109 list = mfc->mfc_bw_meter;
2110 mfc->mfc_bw_meter = NULL;
2114 } else { /* Delete a single bw_meter entry */
2115 struct bw_meter *prev;
2118 flags = compute_bw_meter_flags(req);
2120 /* Find the bw_meter entry to delete */
2121 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2122 prev = x, x = x->bm_mfc_next) {
2123 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2124 &req->bu_threshold.b_time, ==)) &&
2125 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2126 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2127 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2130 if (x != NULL) { /* Delete entry from the list for this MFC */
2132 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2134 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2136 unschedule_bw_meter(x);
2138 /* Free the bw_meter entry */
2150 * Perform bandwidth measurement processing that may result in an upcall
2153 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2155 struct timeval delta;
2160 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2162 if (x->bm_flags & BW_METER_GEQ) {
2164 * Processing for ">=" type of bw_meter entry
2166 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2167 /* Reset the bw_meter entry */
2168 x->bm_start_time = *nowp;
2169 x->bm_measured.b_packets = 0;
2170 x->bm_measured.b_bytes = 0;
2171 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2174 /* Record that a packet is received */
2175 x->bm_measured.b_packets++;
2176 x->bm_measured.b_bytes += plen;
2179 * Test if we should deliver an upcall
2181 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2182 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2183 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2184 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2185 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2186 /* Prepare an upcall for delivery */
2187 bw_meter_prepare_upcall(x, nowp);
2188 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2191 } else if (x->bm_flags & BW_METER_LEQ) {
2193 * Processing for "<=" type of bw_meter entry
2195 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2197 * We are behind time with the multicast forwarding table
2198 * scanning for "<=" type of bw_meter entries, so test now
2199 * if we should deliver an upcall.
2201 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2202 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2203 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2204 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2205 /* Prepare an upcall for delivery */
2206 bw_meter_prepare_upcall(x, nowp);
2208 /* Reschedule the bw_meter entry */
2209 unschedule_bw_meter(x);
2210 schedule_bw_meter(x, nowp);
2213 /* Record that a packet is received */
2214 x->bm_measured.b_packets++;
2215 x->bm_measured.b_bytes += plen;
2218 * Test if we should restart the measuring interval
2220 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2221 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2222 (x->bm_flags & BW_METER_UNIT_BYTES &&
2223 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2224 /* Don't restart the measuring interval */
2226 /* Do restart the measuring interval */
2228 * XXX: note that we don't unschedule and schedule, because this
2229 * might be too much overhead per packet. Instead, when we process
2230 * all entries for a given timer hash bin, we check whether it is
2231 * really a timeout. If not, we reschedule at that time.
2233 x->bm_start_time = *nowp;
2234 x->bm_measured.b_packets = 0;
2235 x->bm_measured.b_bytes = 0;
2236 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2242 * Prepare a bandwidth-related upcall
2245 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2247 struct timeval delta;
2248 struct bw_upcall *u;
2253 * Compute the measured time interval
2256 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2259 * If there are too many pending upcalls, deliver them now
2261 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2265 * Set the bw_upcall entry
2267 u = &bw_upcalls[bw_upcalls_n++];
2268 u->bu_src = x->bm_mfc->mfc_origin;
2269 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2270 u->bu_threshold.b_time = x->bm_threshold.b_time;
2271 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2272 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2273 u->bu_measured.b_time = delta;
2274 u->bu_measured.b_packets = x->bm_measured.b_packets;
2275 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2277 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2278 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2279 if (x->bm_flags & BW_METER_UNIT_BYTES)
2280 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2281 if (x->bm_flags & BW_METER_GEQ)
2282 u->bu_flags |= BW_UPCALL_GEQ;
2283 if (x->bm_flags & BW_METER_LEQ)
2284 u->bu_flags |= BW_UPCALL_LEQ;
2288 * Send the pending bandwidth-related upcalls
2291 bw_upcalls_send(void)
2294 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2295 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2296 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2298 IGMPMSG_BW_UPCALL,/* im_msgtype */
2303 { 0 } }; /* im_dst */
2307 if (bw_upcalls_n == 0)
2308 return; /* No pending upcalls */
2313 * Allocate a new mbuf, initialize it with the header and
2314 * the payload for the pending calls.
2316 MGETHDR(m, M_DONTWAIT, MT_DATA);
2318 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2322 m->m_len = m->m_pkthdr.len = 0;
2323 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2324 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2328 * XXX do we need to set the address in k_igmpsrc ?
2330 mrtstat.mrts_upcalls++;
2331 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2332 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2333 ++mrtstat.mrts_upq_sockfull;
2338 * Compute the timeout hash value for the bw_meter entries
2340 #define BW_METER_TIMEHASH(bw_meter, hash) \
2342 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2344 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2345 (hash) = next_timeval.tv_sec; \
2346 if (next_timeval.tv_usec) \
2347 (hash)++; /* XXX: make sure we don't timeout early */ \
2348 (hash) %= BW_METER_BUCKETS; \
2352 * Schedule a timer to process periodically bw_meter entry of type "<="
2353 * by linking the entry in the proper hash bucket.
2356 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2362 if (!(x->bm_flags & BW_METER_LEQ))
2363 return; /* XXX: we schedule timers only for "<=" entries */
2366 * Reset the bw_meter entry
2368 x->bm_start_time = *nowp;
2369 x->bm_measured.b_packets = 0;
2370 x->bm_measured.b_bytes = 0;
2371 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2374 * Compute the timeout hash value and insert the entry
2376 BW_METER_TIMEHASH(x, time_hash);
2377 x->bm_time_next = bw_meter_timers[time_hash];
2378 bw_meter_timers[time_hash] = x;
2379 x->bm_time_hash = time_hash;
2383 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2384 * by removing the entry from the proper hash bucket.
2387 unschedule_bw_meter(struct bw_meter *x)
2390 struct bw_meter *prev, *tmp;
2394 if (!(x->bm_flags & BW_METER_LEQ))
2395 return; /* XXX: we schedule timers only for "<=" entries */
2398 * Compute the timeout hash value and delete the entry
2400 time_hash = x->bm_time_hash;
2401 if (time_hash >= BW_METER_BUCKETS)
2402 return; /* Entry was not scheduled */
2404 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2405 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2410 panic("unschedule_bw_meter: bw_meter entry not found");
2413 prev->bm_time_next = x->bm_time_next;
2415 bw_meter_timers[time_hash] = x->bm_time_next;
2417 x->bm_time_next = NULL;
2418 x->bm_time_hash = BW_METER_BUCKETS;
2423 * Process all "<=" type of bw_meter that should be processed now,
2424 * and for each entry prepare an upcall if necessary. Each processed
2425 * entry is rescheduled again for the (periodic) processing.
2427 * This is run periodically (once per second normally). On each round,
2428 * all the potentially matching entries are in the hash slot that we are
2434 static uint32_t last_tv_sec; /* last time we processed this */
2438 struct timeval now, process_endtime;
2441 if (last_tv_sec == now.tv_sec)
2442 return; /* nothing to do */
2444 loops = now.tv_sec - last_tv_sec;
2445 last_tv_sec = now.tv_sec;
2446 if (loops > BW_METER_BUCKETS)
2447 loops = BW_METER_BUCKETS;
2451 * Process all bins of bw_meter entries from the one after the last
2452 * processed to the current one. On entry, i points to the last bucket
2453 * visited, so we need to increment i at the beginning of the loop.
2455 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2456 struct bw_meter *x, *tmp_list;
2458 if (++i >= BW_METER_BUCKETS)
2461 /* Disconnect the list of bw_meter entries from the bin */
2462 tmp_list = bw_meter_timers[i];
2463 bw_meter_timers[i] = NULL;
2465 /* Process the list of bw_meter entries */
2466 while (tmp_list != NULL) {
2468 tmp_list = tmp_list->bm_time_next;
2470 /* Test if the time interval is over */
2471 process_endtime = x->bm_start_time;
2472 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2473 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2474 /* Not yet: reschedule, but don't reset */
2477 BW_METER_TIMEHASH(x, time_hash);
2478 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2480 * XXX: somehow the bin processing is a bit ahead of time.
2481 * Put the entry in the next bin.
2483 if (++time_hash >= BW_METER_BUCKETS)
2486 x->bm_time_next = bw_meter_timers[time_hash];
2487 bw_meter_timers[time_hash] = x;
2488 x->bm_time_hash = time_hash;
2494 * Test if we should deliver an upcall
2496 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2497 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2498 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2499 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2500 /* Prepare an upcall for delivery */
2501 bw_meter_prepare_upcall(x, &now);
2505 * Reschedule for next processing
2507 schedule_bw_meter(x, &now);
2511 /* Send all upcalls that are pending delivery */
2518 * A periodic function for sending all upcalls that are pending delivery
2521 expire_bw_upcalls_send(void *unused)
2527 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2528 expire_bw_upcalls_send, NULL);
2532 * A periodic function for periodic scanning of the multicast forwarding
2533 * table for processing all "<=" bw_meter entries.
2536 expire_bw_meter_process(void *unused)
2538 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2541 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2545 * End of bandwidth monitoring code
2549 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2553 pim_register_send(struct ip *ip, struct vif *vifp,
2554 struct mbuf *m, struct mfc *rt)
2556 struct mbuf *mb_copy, *mm;
2558 if (mrtdebug & DEBUG_PIM)
2559 log(LOG_DEBUG, "pim_register_send: ");
2562 * Do not send IGMP_WHOLEPKT notifications to userland, if the
2563 * rendezvous point was unspecified, and we were told not to.
2565 if (pim_squelch_wholepkt != 0 && (mrt_api_config & MRT_MFC_RP) &&
2566 (rt->mfc_rp.s_addr == INADDR_ANY))
2569 mb_copy = pim_register_prepare(ip, m);
2570 if (mb_copy == NULL)
2574 * Send all the fragments. Note that the mbuf for each fragment
2575 * is freed by the sending machinery.
2577 for (mm = mb_copy; mm; mm = mb_copy) {
2578 mb_copy = mm->m_nextpkt;
2580 mm = m_pullup(mm, sizeof(struct ip));
2582 ip = mtod(mm, struct ip *);
2583 if ((mrt_api_config & MRT_MFC_RP) &&
2584 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2585 pim_register_send_rp(ip, vifp, mm, rt);
2587 pim_register_send_upcall(ip, vifp, mm, rt);
2596 * Return a copy of the data packet that is ready for PIM Register
2598 * XXX: Note that in the returned copy the IP header is a valid one.
2600 static struct mbuf *
2601 pim_register_prepare(struct ip *ip, struct mbuf *m)
2603 struct mbuf *mb_copy = NULL;
2606 /* Take care of delayed checksums */
2607 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2608 in_delayed_cksum(m);
2609 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2613 * Copy the old packet & pullup its IP header into the
2614 * new mbuf so we can modify it.
2616 mb_copy = m_copypacket(m, M_DONTWAIT);
2617 if (mb_copy == NULL)
2619 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2620 if (mb_copy == NULL)
2623 /* take care of the TTL */
2624 ip = mtod(mb_copy, struct ip *);
2627 /* Compute the MTU after the PIM Register encapsulation */
2628 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2630 if (ip->ip_len <= mtu) {
2631 /* Turn the IP header into a valid one */
2632 ip->ip_len = htons(ip->ip_len);
2633 ip->ip_off = htons(ip->ip_off);
2635 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2637 /* Fragment the packet */
2638 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2647 * Send an upcall with the data packet to the user-level process.
2650 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2651 struct mbuf *mb_copy, struct mfc *rt)
2653 struct mbuf *mb_first;
2654 int len = ntohs(ip->ip_len);
2656 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2661 * Add a new mbuf with an upcall header
2663 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2664 if (mb_first == NULL) {
2668 mb_first->m_data += max_linkhdr;
2669 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2670 mb_first->m_len = sizeof(struct igmpmsg);
2671 mb_first->m_next = mb_copy;
2673 /* Send message to routing daemon */
2674 im = mtod(mb_first, struct igmpmsg *);
2675 im->im_msgtype = IGMPMSG_WHOLEPKT;
2677 im->im_vif = vifp - viftable;
2678 im->im_src = ip->ip_src;
2679 im->im_dst = ip->ip_dst;
2681 k_igmpsrc.sin_addr = ip->ip_src;
2683 mrtstat.mrts_upcalls++;
2685 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2686 if (mrtdebug & DEBUG_PIM)
2688 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2689 ++mrtstat.mrts_upq_sockfull;
2693 /* Keep statistics */
2694 pimstat.pims_snd_registers_msgs++;
2695 pimstat.pims_snd_registers_bytes += len;
2701 * Encapsulate the data packet in PIM Register message and send it to the RP.
2704 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2705 struct mbuf *mb_copy, struct mfc *rt)
2707 struct mbuf *mb_first;
2708 struct ip *ip_outer;
2709 struct pim_encap_pimhdr *pimhdr;
2710 int len = ntohs(ip->ip_len);
2711 vifi_t vifi = rt->mfc_parent;
2715 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2717 return EADDRNOTAVAIL; /* The iif vif is invalid */
2721 * Add a new mbuf with the encapsulating header
2723 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2724 if (mb_first == NULL) {
2728 mb_first->m_data += max_linkhdr;
2729 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2730 mb_first->m_next = mb_copy;
2732 mb_first->m_pkthdr.len = len + mb_first->m_len;
2735 * Fill in the encapsulating IP and PIM header
2737 ip_outer = mtod(mb_first, struct ip *);
2738 *ip_outer = pim_encap_iphdr;
2739 ip_outer->ip_id = ip_newid();
2740 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2741 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2742 ip_outer->ip_dst = rt->mfc_rp;
2744 * Copy the inner header TOS to the outer header, and take care of the
2747 ip_outer->ip_tos = ip->ip_tos;
2748 if (ntohs(ip->ip_off) & IP_DF)
2749 ip_outer->ip_off |= IP_DF;
2750 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2751 + sizeof(pim_encap_iphdr));
2752 *pimhdr = pim_encap_pimhdr;
2753 /* If the iif crosses a border, set the Border-bit */
2754 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2755 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2757 mb_first->m_data += sizeof(pim_encap_iphdr);
2758 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2759 mb_first->m_data -= sizeof(pim_encap_iphdr);
2761 send_packet(vifp, mb_first);
2763 /* Keep statistics */
2764 pimstat.pims_snd_registers_msgs++;
2765 pimstat.pims_snd_registers_bytes += len;
2771 * pim_encapcheck() is called by the encap[46]_input() path at runtime to
2772 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2776 pim_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
2780 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2782 if (proto != IPPROTO_PIM)
2783 return 0; /* not for us; reject the datagram. */
2785 return 64; /* claim the datagram. */
2789 * PIM-SMv2 and PIM-DM messages processing.
2790 * Receives and verifies the PIM control messages, and passes them
2791 * up to the listening socket, using rip_input().
2792 * The only message with special processing is the PIM_REGISTER message
2793 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2794 * is passed to if_simloop().
2797 pim_input(struct mbuf *m, int off)
2799 struct ip *ip = mtod(m, struct ip *);
2802 int datalen = ip->ip_len;
2806 /* Keep statistics */
2807 pimstat.pims_rcv_total_msgs++;
2808 pimstat.pims_rcv_total_bytes += datalen;
2813 if (datalen < PIM_MINLEN) {
2814 pimstat.pims_rcv_tooshort++;
2815 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
2816 datalen, (u_long)ip->ip_src.s_addr);
2822 * If the packet is at least as big as a REGISTER, go agead
2823 * and grab the PIM REGISTER header size, to avoid another
2824 * possible m_pullup() later.
2826 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
2827 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2829 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2831 * Get the IP and PIM headers in contiguous memory, and
2832 * possibly the PIM REGISTER header.
2834 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
2835 (m = m_pullup(m, minlen)) == 0) {
2836 log(LOG_ERR, "pim_input: m_pullup failure\n");
2839 /* m_pullup() may have given us a new mbuf so reset ip. */
2840 ip = mtod(m, struct ip *);
2841 ip_tos = ip->ip_tos;
2843 /* adjust mbuf to point to the PIM header */
2844 m->m_data += iphlen;
2846 pim = mtod(m, struct pim *);
2849 * Validate checksum. If PIM REGISTER, exclude the data packet.
2851 * XXX: some older PIMv2 implementations don't make this distinction,
2852 * so for compatibility reason perform the checksum over part of the
2853 * message, and if error, then over the whole message.
2855 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2856 /* do nothing, checksum okay */
2857 } else if (in_cksum(m, datalen)) {
2858 pimstat.pims_rcv_badsum++;
2859 if (mrtdebug & DEBUG_PIM)
2860 log(LOG_DEBUG, "pim_input: invalid checksum");
2865 /* PIM version check */
2866 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2867 pimstat.pims_rcv_badversion++;
2868 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
2869 PIM_VT_V(pim->pim_vt), PIM_VERSION);
2874 /* restore mbuf back to the outer IP */
2875 m->m_data -= iphlen;
2878 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2880 * Since this is a REGISTER, we'll make a copy of the register
2881 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2884 struct sockaddr_in dst = { sizeof(dst), AF_INET };
2886 struct ip *encap_ip;
2891 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
2893 if (mrtdebug & DEBUG_PIM)
2895 "pim_input: register vif not set: %d\n", reg_vif_num);
2899 /* XXX need refcnt? */
2900 vifp = viftable[reg_vif_num].v_ifp;
2906 if (datalen < PIM_REG_MINLEN) {
2907 pimstat.pims_rcv_tooshort++;
2908 pimstat.pims_rcv_badregisters++;
2910 "pim_input: register packet size too small %d from %lx\n",
2911 datalen, (u_long)ip->ip_src.s_addr);
2916 reghdr = (u_int32_t *)(pim + 1);
2917 encap_ip = (struct ip *)(reghdr + 1);
2919 if (mrtdebug & DEBUG_PIM) {
2921 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
2922 (u_long)ntohl(encap_ip->ip_src.s_addr),
2923 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2924 ntohs(encap_ip->ip_len));
2927 /* verify the version number of the inner packet */
2928 if (encap_ip->ip_v != IPVERSION) {
2929 pimstat.pims_rcv_badregisters++;
2930 if (mrtdebug & DEBUG_PIM) {
2931 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
2932 "of the inner packet\n", encap_ip->ip_v);
2938 /* verify the inner packet is destined to a mcast group */
2939 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2940 pimstat.pims_rcv_badregisters++;
2941 if (mrtdebug & DEBUG_PIM)
2943 "pim_input: inner packet of register is not "
2945 (u_long)ntohl(encap_ip->ip_dst.s_addr));
2950 /* If a NULL_REGISTER, pass it to the daemon */
2951 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2952 goto pim_input_to_daemon;
2955 * Copy the TOS from the outer IP header to the inner IP header.
2957 if (encap_ip->ip_tos != ip_tos) {
2958 /* Outer TOS -> inner TOS */
2959 encap_ip->ip_tos = ip_tos;
2960 /* Recompute the inner header checksum. Sigh... */
2962 /* adjust mbuf to point to the inner IP header */
2963 m->m_data += (iphlen + PIM_MINLEN);
2964 m->m_len -= (iphlen + PIM_MINLEN);
2966 encap_ip->ip_sum = 0;
2967 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2969 /* restore mbuf to point back to the outer IP header */
2970 m->m_data -= (iphlen + PIM_MINLEN);
2971 m->m_len += (iphlen + PIM_MINLEN);
2975 * Decapsulate the inner IP packet and loopback to forward it
2976 * as a normal multicast packet. Also, make a copy of the
2977 * outer_iphdr + pimhdr + reghdr + encap_iphdr
2978 * to pass to the daemon later, so it can take the appropriate
2979 * actions (e.g., send back PIM_REGISTER_STOP).
2980 * XXX: here m->m_data points to the outer IP header.
2982 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
2985 "pim_input: pim register: could not copy register head\n");
2990 /* Keep statistics */
2991 /* XXX: registers_bytes include only the encap. mcast pkt */
2992 pimstat.pims_rcv_registers_msgs++;
2993 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
2996 * forward the inner ip packet; point m_data at the inner ip.
2998 m_adj(m, iphlen + PIM_MINLEN);
3000 if (mrtdebug & DEBUG_PIM) {
3002 "pim_input: forwarding decapsulated register: "
3003 "src %lx, dst %lx, vif %d\n",
3004 (u_long)ntohl(encap_ip->ip_src.s_addr),
3005 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3008 /* NB: vifp was collected above; can it change on us? */
3009 if_simloop(vifp, m, dst.sin_family, 0);
3011 /* prepare the register head to send to the mrouting daemon */
3015 pim_input_to_daemon:
3017 * Pass the PIM message up to the daemon; if it is a Register message,
3018 * pass the 'head' only up to the daemon. This includes the
3019 * outer IP header, PIM header, PIM-Register header and the
3021 * XXX: the outer IP header pkt size of a Register is not adjust to
3022 * reflect the fact that the inner multicast data is truncated.
3024 rip_input(m, iphlen);
3030 * XXX: This is common code for dealing with initialization for both
3031 * the IPv4 and IPv6 multicast forwarding paths. It could do with cleanup.
3034 ip_mroute_modevent(module_t mod, int type, void *unused)
3038 MROUTER_LOCK_INIT();
3042 TUNABLE_ULONG_FETCH("net.inet.pim.squelch_wholepkt",
3043 &pim_squelch_wholepkt);
3045 pim_encap_cookie = encap_attach_func(AF_INET, IPPROTO_PIM,
3046 pim_encapcheck, &in_pim_protosw, NULL);
3047 if (pim_encap_cookie == NULL) {
3048 printf("ip_mroute: unable to attach pim encap\n");
3051 MROUTER_LOCK_DESTROY();
3056 pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
3057 pim_encapcheck, (struct protosw *)&in6_pim_protosw, NULL);
3058 if (pim6_encap_cookie == NULL) {
3059 printf("ip_mroute: unable to attach pim6 encap\n");
3060 if (pim_encap_cookie) {
3061 encap_detach(pim_encap_cookie);
3062 pim_encap_cookie = NULL;
3066 MROUTER_LOCK_DESTROY();
3071 ip_mcast_src = X_ip_mcast_src;
3072 ip_mforward = X_ip_mforward;
3073 ip_mrouter_done = X_ip_mrouter_done;
3074 ip_mrouter_get = X_ip_mrouter_get;
3075 ip_mrouter_set = X_ip_mrouter_set;
3078 ip6_mforward = X_ip6_mforward;
3079 ip6_mrouter_done = X_ip6_mrouter_done;
3080 ip6_mrouter_get = X_ip6_mrouter_get;
3081 ip6_mrouter_set = X_ip6_mrouter_set;
3082 mrt6_ioctl = X_mrt6_ioctl;
3085 ip_rsvp_force_done = X_ip_rsvp_force_done;
3086 ip_rsvp_vif = X_ip_rsvp_vif;
3088 legal_vif_num = X_legal_vif_num;
3089 mrt_ioctl = X_mrt_ioctl;
3090 rsvp_input_p = X_rsvp_input;
3095 * Typically module unload happens after the user-level
3096 * process has shutdown the kernel services (the check
3097 * below insures someone can't just yank the module out
3098 * from under a running process). But if the module is
3099 * just loaded and then unloaded w/o starting up a user
3100 * process we still need to cleanup.
3110 if (pim6_encap_cookie) {
3111 encap_detach(pim6_encap_cookie);
3112 pim6_encap_cookie = NULL;
3114 X_ip6_mrouter_done();
3115 ip6_mforward = NULL;
3116 ip6_mrouter_done = NULL;
3117 ip6_mrouter_get = NULL;
3118 ip6_mrouter_set = NULL;
3122 if (pim_encap_cookie) {
3123 encap_detach(pim_encap_cookie);
3124 pim_encap_cookie = NULL;
3126 X_ip_mrouter_done();
3127 ip_mcast_src = NULL;
3129 ip_mrouter_done = NULL;
3130 ip_mrouter_get = NULL;
3131 ip_mrouter_set = NULL;
3133 ip_rsvp_force_done = NULL;
3136 legal_vif_num = NULL;
3138 rsvp_input_p = NULL;
3142 MROUTER_LOCK_DESTROY();
3151 static moduledata_t ip_mroutemod = {
3156 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);