2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989 Stephen Deering
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Stephen Deering of Stanford University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
39 * IP multicast forwarding procedures
41 * Written by David Waitzman, BBN Labs, August 1988.
42 * Modified by Steve Deering, Stanford, February 1989.
43 * Modified by Mark J. Steiglitz, Stanford, May, 1991
44 * Modified by Van Jacobson, LBL, January 1993
45 * Modified by Ajit Thyagarajan, PARC, August 1993
46 * Modified by Bill Fenner, PARC, April 1995
47 * Modified by Ahmed Helmy, SGI, June 1996
48 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
49 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
50 * Modified by Hitoshi Asaeda, WIDE, August 2000
51 * Modified by Pavlin Radoslavov, ICSI, October 2002
52 * Modified by Wojciech Macek, Semihalf, May 2021
54 * MROUTING Revision: 3.5
55 * and PIM-SMv2 and PIM-DM support, advanced API support,
56 * bandwidth metering and signaling
60 * TODO: Prefix functions with ipmf_.
61 * TODO: Maintain a refcount on if_allmulti() in ifnet or in the protocol
62 * domain attachment (if_afdata) so we can track consumers of that service.
63 * TODO: Deprecate routing socket path for SIOCGETSGCNT and SIOCGETVIFCNT,
64 * move it to socket options.
65 * TODO: Cleanup LSRR removal further.
66 * TODO: Push RSVP stubs into raw_ip.c.
67 * TODO: Use bitstring.h for vif set.
68 * TODO: Fix mrt6_ioctl dangling ref when dynamically loaded.
69 * TODO: Sync ip6_mroute.c with this file.
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
76 #include "opt_mrouting.h"
80 #include <sys/types.h>
81 #include <sys/param.h>
82 #include <sys/kernel.h>
83 #include <sys/stddef.h>
84 #include <sys/condvar.h>
85 #include <sys/eventhandler.h>
87 #include <sys/kthread.h>
89 #include <sys/malloc.h>
91 #include <sys/module.h>
93 #include <sys/protosw.h>
94 #include <sys/signalvar.h>
95 #include <sys/socket.h>
96 #include <sys/socketvar.h>
97 #include <sys/sockio.h>
99 #include <sys/sysctl.h>
100 #include <sys/syslog.h>
101 #include <sys/systm.h>
102 #include <sys/taskqueue.h>
103 #include <sys/time.h>
104 #include <sys/counter.h>
105 #include <machine/atomic.h>
108 #include <net/if_var.h>
109 #include <net/if_types.h>
110 #include <net/netisr.h>
111 #include <net/route.h>
112 #include <net/vnet.h>
114 #include <netinet/in.h>
115 #include <netinet/igmp.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_encap.h>
120 #include <netinet/ip_mroute.h>
121 #include <netinet/ip_var.h>
122 #include <netinet/ip_options.h>
123 #include <netinet/pim.h>
124 #include <netinet/pim_var.h>
125 #include <netinet/udp.h>
127 #include <machine/in_cksum.h>
130 #define KTR_IPMF KTR_INET
133 #define VIFI_INVALID ((vifi_t) -1)
135 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast forwarding cache");
138 * Locking. We use two locks: one for the virtual interface table and
139 * one for the forwarding table. These locks may be nested in which case
140 * the VIF lock must always be taken first. Note that each lock is used
141 * to cover not only the specific data structure but also related data
145 static struct rwlock mrouter_mtx;
146 #define MRW_RLOCK() rw_rlock(&mrouter_mtx)
147 #define MRW_WLOCK() rw_wlock(&mrouter_mtx)
148 #define MRW_RUNLOCK() rw_runlock(&mrouter_mtx)
149 #define MRW_WUNLOCK() rw_wunlock(&mrouter_mtx)
150 #define MRW_UNLOCK() rw_unlock(&mrouter_mtx)
151 #define MRW_LOCK_ASSERT() rw_assert(&mrouter_mtx, RA_LOCKED)
152 #define MRW_WLOCK_ASSERT() rw_assert(&mrouter_mtx, RA_WLOCKED)
153 #define MRW_LOCK_TRY_UPGRADE() rw_try_upgrade(&mrouter_mtx)
154 #define MRW_WOWNED() rw_wowned(&mrouter_mtx)
155 #define MRW_LOCK_INIT() \
156 rw_init(&mrouter_mtx, "IPv4 multicast forwarding")
157 #define MRW_LOCK_DESTROY() rw_destroy(&mrouter_mtx)
159 static int ip_mrouter_cnt; /* # of vnets with active mrouters */
160 static int ip_mrouter_unloading; /* Allow no more V_ip_mrouter sockets */
162 VNET_PCPUSTAT_DEFINE_STATIC(struct mrtstat, mrtstat);
163 VNET_PCPUSTAT_SYSINIT(mrtstat);
164 VNET_PCPUSTAT_SYSUNINIT(mrtstat);
165 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, OID_AUTO, mrtstat, struct mrtstat,
166 mrtstat, "IPv4 Multicast Forwarding Statistics (struct mrtstat, "
167 "netinet/ip_mroute.h)");
169 VNET_DEFINE_STATIC(u_long, mfchash);
170 #define V_mfchash VNET(mfchash)
171 #define MFCHASH(a, g) \
172 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
173 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & V_mfchash)
174 #define MFCHASHSIZE 256
176 static u_long mfchashsize; /* Hash size */
177 VNET_DEFINE_STATIC(u_char *, nexpire); /* 0..mfchashsize-1 */
178 #define V_nexpire VNET(nexpire)
179 VNET_DEFINE_STATIC(LIST_HEAD(mfchashhdr, mfc)*, mfchashtbl);
180 #define V_mfchashtbl VNET(mfchashtbl)
181 VNET_DEFINE_STATIC(struct taskqueue *, task_queue);
182 #define V_task_queue VNET(task_queue)
183 VNET_DEFINE_STATIC(struct task, task);
184 #define V_task VNET(task)
186 VNET_DEFINE_STATIC(vifi_t, numvifs);
187 #define V_numvifs VNET(numvifs)
188 VNET_DEFINE_STATIC(struct vif *, viftable);
189 #define V_viftable VNET(viftable)
191 static eventhandler_tag if_detach_event_tag = NULL;
193 VNET_DEFINE_STATIC(struct callout, expire_upcalls_ch);
194 #define V_expire_upcalls_ch VNET(expire_upcalls_ch)
196 VNET_DEFINE_STATIC(struct mtx, buf_ring_mtx);
197 #define V_buf_ring_mtx VNET(buf_ring_mtx)
199 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
200 #define UPCALL_EXPIRE 6 /* number of timeouts */
203 * Bandwidth meter variables and constants
205 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
208 * Pending upcalls are stored in a ring which is flushed when
209 * full, or periodically
211 VNET_DEFINE_STATIC(struct callout, bw_upcalls_ch);
212 #define V_bw_upcalls_ch VNET(bw_upcalls_ch)
213 VNET_DEFINE_STATIC(struct buf_ring *, bw_upcalls_ring);
214 #define V_bw_upcalls_ring VNET(bw_upcalls_ring)
215 VNET_DEFINE_STATIC(struct mtx, bw_upcalls_ring_mtx);
216 #define V_bw_upcalls_ring_mtx VNET(bw_upcalls_ring_mtx)
218 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
220 VNET_PCPUSTAT_DEFINE_STATIC(struct pimstat, pimstat);
221 VNET_PCPUSTAT_SYSINIT(pimstat);
222 VNET_PCPUSTAT_SYSUNINIT(pimstat);
224 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
226 SYSCTL_VNET_PCPUSTAT(_net_inet_pim, PIMCTL_STATS, stats, struct pimstat,
227 pimstat, "PIM Statistics (struct pimstat, netinet/pim_var.h)");
229 static u_long pim_squelch_wholepkt = 0;
230 SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RW,
231 &pim_squelch_wholepkt, 0,
232 "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
234 static const struct encaptab *pim_encap_cookie;
235 static int pim_encapcheck(const struct mbuf *, int, int, void *);
236 static int pim_input(struct mbuf *, int, int, void *);
238 extern int in_mcast_loop;
240 static const struct encap_config ipv4_encap_cfg = {
241 .proto = IPPROTO_PIM,
242 .min_length = sizeof(struct ip) + PIM_MINLEN,
244 .check = pim_encapcheck,
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 {
263 #define PIM_ENCAP_TTL 64
265 static struct ip pim_encap_iphdr = {
266 #if BYTE_ORDER == LITTLE_ENDIAN
267 sizeof(struct ip) >> 2,
271 sizeof(struct ip) >> 2,
274 sizeof(struct ip), /* total length */
282 static struct pim_encap_pimhdr pim_encap_pimhdr = {
284 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
291 VNET_DEFINE_STATIC(vifi_t, reg_vif_num) = VIFI_INVALID;
292 #define V_reg_vif_num VNET(reg_vif_num)
293 VNET_DEFINE_STATIC(struct ifnet *, multicast_register_if);
294 #define V_multicast_register_if VNET(multicast_register_if)
300 static u_long X_ip_mcast_src(int);
301 static int X_ip_mforward(struct ip *, struct ifnet *, struct mbuf *,
302 struct ip_moptions *);
303 static int X_ip_mrouter_done(void *);
304 static int X_ip_mrouter_get(struct socket *, struct sockopt *);
305 static int X_ip_mrouter_set(struct socket *, struct sockopt *);
306 static int X_legal_vif_num(int);
307 static int X_mrt_ioctl(u_long, caddr_t, int);
309 static int add_bw_upcall(struct bw_upcall *);
310 static int add_mfc(struct mfcctl2 *);
311 static int add_vif(struct vifctl *);
312 static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *);
313 static void bw_meter_geq_receive_packet(struct bw_meter *, int,
315 static void bw_upcalls_send(void);
316 static int del_bw_upcall(struct bw_upcall *);
317 static int del_mfc(struct mfcctl2 *);
318 static int del_vif(vifi_t);
319 static int del_vif_locked(vifi_t);
320 static void expire_bw_upcalls_send(void *);
321 static void expire_mfc(struct mfc *);
322 static void expire_upcalls(void *);
323 static void free_bw_list(struct bw_meter *);
324 static int get_sg_cnt(struct sioc_sg_req *);
325 static int get_vif_cnt(struct sioc_vif_req *);
326 static void if_detached_event(void *, struct ifnet *);
327 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
328 static int ip_mrouter_init(struct socket *, int);
329 static __inline struct mfc *
330 mfc_find(struct in_addr *, struct in_addr *);
331 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
333 pim_register_prepare(struct ip *, struct mbuf *);
334 static int pim_register_send(struct ip *, struct vif *,
335 struct mbuf *, struct mfc *);
336 static int pim_register_send_rp(struct ip *, struct vif *,
337 struct mbuf *, struct mfc *);
338 static int pim_register_send_upcall(struct ip *, struct vif *,
339 struct mbuf *, struct mfc *);
340 static void send_packet(struct vif *, struct mbuf *);
341 static int set_api_config(uint32_t *);
342 static int set_assert(int);
343 static int socket_send(struct socket *, struct mbuf *,
344 struct sockaddr_in *);
347 * Kernel multicast forwarding API capabilities and setup.
348 * If more API capabilities are added to the kernel, they should be
349 * recorded in `mrt_api_support'.
351 #define MRT_API_VERSION 0x0305
353 static const int mrt_api_version = MRT_API_VERSION;
354 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
355 MRT_MFC_FLAGS_BORDER_VIF |
358 VNET_DEFINE_STATIC(uint32_t, mrt_api_config);
359 #define V_mrt_api_config VNET(mrt_api_config)
360 VNET_DEFINE_STATIC(int, pim_assert_enabled);
361 #define V_pim_assert_enabled VNET(pim_assert_enabled)
362 static struct timeval pim_assert_interval = { 3, 0 }; /* Rate limit */
365 * Find a route for a given origin IP address and multicast group address.
366 * Statistics must be updated by the caller.
368 static __inline struct mfc *
369 mfc_find(struct in_addr *o, struct in_addr *g)
374 * Might be called both RLOCK and WLOCK.
375 * Check if any, it's caller responsibility
376 * to choose correct option.
380 LIST_FOREACH(rt, &V_mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
381 if (in_hosteq(rt->mfc_origin, *o) &&
382 in_hosteq(rt->mfc_mcastgrp, *g) &&
383 buf_ring_empty(rt->mfc_stall_ring))
390 static __inline struct mfc *
394 rt = (struct mfc*) malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT | M_ZERO);
398 rt->mfc_stall_ring = buf_ring_alloc(MAX_UPQ, M_MRTABLE,
399 M_NOWAIT, &V_buf_ring_mtx);
400 if (rt->mfc_stall_ring == NULL) {
409 * Handle MRT setsockopt commands to modify the multicast forwarding tables.
412 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
418 struct bw_upcall bw_upcall;
421 if (so != V_ip_mrouter && sopt->sopt_name != MRT_INIT)
425 switch (sopt->sopt_name) {
427 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
430 error = ip_mrouter_init(so, optval);
434 error = ip_mrouter_done(NULL);
438 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
441 error = add_vif(&vifc);
445 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
448 error = del_vif(vifi);
454 * select data size depending on API version.
456 if (sopt->sopt_name == MRT_ADD_MFC &&
457 V_mrt_api_config & MRT_API_FLAGS_ALL) {
458 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
459 sizeof(struct mfcctl2));
461 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
462 sizeof(struct mfcctl));
463 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
464 sizeof(mfc) - sizeof(struct mfcctl));
468 if (sopt->sopt_name == MRT_ADD_MFC)
469 error = add_mfc(&mfc);
471 error = del_mfc(&mfc);
475 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
482 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
484 error = set_api_config(&i);
486 error = sooptcopyout(sopt, &i, sizeof i);
489 case MRT_ADD_BW_UPCALL:
490 case MRT_DEL_BW_UPCALL:
491 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
495 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
496 error = add_bw_upcall(&bw_upcall);
498 error = del_bw_upcall(&bw_upcall);
509 * Handle MRT getsockopt commands
512 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
516 switch (sopt->sopt_name) {
518 error = sooptcopyout(sopt, &mrt_api_version, sizeof mrt_api_version);
522 error = sooptcopyout(sopt, &V_pim_assert_enabled,
523 sizeof V_pim_assert_enabled);
526 case MRT_API_SUPPORT:
527 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
531 error = sooptcopyout(sopt, &V_mrt_api_config, sizeof V_mrt_api_config);
542 * Handle ioctl commands to obtain information from the cache
545 X_mrt_ioctl(u_long cmd, caddr_t data, int fibnum __unused)
550 * Currently the only function calling this ioctl routine is rtioctl_fib().
551 * Typically, only root can create the raw socket in order to execute
552 * this ioctl method, however the request might be coming from a prison
554 error = priv_check(curthread, PRIV_NETINET_MROUTE);
558 case (SIOCGETVIFCNT):
559 error = get_vif_cnt((struct sioc_vif_req *)data);
563 error = get_sg_cnt((struct sioc_sg_req *)data);
574 * returns the packet, byte, rpf-failure count for the source group provided
577 get_sg_cnt(struct sioc_sg_req *req)
582 rt = mfc_find(&req->src, &req->grp);
585 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
586 return EADDRNOTAVAIL;
588 req->pktcnt = rt->mfc_pkt_cnt;
589 req->bytecnt = rt->mfc_byte_cnt;
590 req->wrong_if = rt->mfc_wrong_if;
596 * returns the input and output packet and byte counts on the vif provided
599 get_vif_cnt(struct sioc_vif_req *req)
601 vifi_t vifi = req->vifi;
604 if (vifi >= V_numvifs) {
609 mtx_lock_spin(&V_viftable[vifi].v_spin);
610 req->icount = V_viftable[vifi].v_pkt_in;
611 req->ocount = V_viftable[vifi].v_pkt_out;
612 req->ibytes = V_viftable[vifi].v_bytes_in;
613 req->obytes = V_viftable[vifi].v_bytes_out;
614 mtx_unlock_spin(&V_viftable[vifi].v_spin);
621 if_detached_event(void *arg __unused, struct ifnet *ifp)
628 if (V_ip_mrouter == NULL) {
634 * Tear down multicast forwarder state associated with this ifnet.
635 * 1. Walk the vif list, matching vifs against this ifnet.
636 * 2. Walk the multicast forwarding cache (mfc) looking for
637 * inner matches with this vif's index.
638 * 3. Expire any matching multicast forwarding cache entries.
639 * 4. Free vif state. This should disable ALLMULTI on the interface.
641 for (vifi = 0; vifi < V_numvifs; vifi++) {
642 if (V_viftable[vifi].v_ifp != ifp)
644 for (i = 0; i < mfchashsize; i++) {
645 struct mfc *rt, *nrt;
647 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
648 if (rt->mfc_parent == vifi) {
653 del_vif_locked(vifi);
660 ip_mrouter_upcall_thread(void *arg, int pending __unused)
662 CURVNET_SET((struct vnet *) arg);
672 * Enable multicast forwarding.
675 ip_mrouter_init(struct socket *so, int version)
678 CTR3(KTR_IPMF, "%s: so_type %d, pr_protocol %d", __func__,
679 so->so_type, so->so_proto->pr_protocol);
681 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
689 if (ip_mrouter_unloading) {
694 if (V_ip_mrouter != NULL) {
699 V_mfchashtbl = hashinit_flags(mfchashsize, M_MRTABLE, &V_mfchash,
702 /* Create upcall ring */
703 mtx_init(&V_bw_upcalls_ring_mtx, "mroute upcall buf_ring mtx", NULL, MTX_DEF);
704 V_bw_upcalls_ring = buf_ring_alloc(BW_UPCALLS_MAX, M_MRTABLE,
705 M_NOWAIT, &V_bw_upcalls_ring_mtx);
706 if (!V_bw_upcalls_ring) {
711 TASK_INIT(&V_task, 0, ip_mrouter_upcall_thread, curvnet);
712 taskqueue_cancel(V_task_queue, &V_task, NULL);
713 taskqueue_unblock(V_task_queue);
715 callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
717 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
721 atomic_add_int(&ip_mrouter_cnt, 1);
723 /* This is a mutex required by buf_ring init, but not used internally */
724 mtx_init(&V_buf_ring_mtx, "mroute buf_ring mtx", NULL, MTX_DEF);
728 CTR1(KTR_IPMF, "%s: done", __func__);
734 * Disable multicast forwarding.
737 X_ip_mrouter_done(void *locked)
742 struct bw_upcall *bu;
744 if (V_ip_mrouter == NULL)
748 * Detach/disable hooks to the reset of the system.
751 atomic_subtract_int(&ip_mrouter_cnt, 1);
752 V_mrt_api_config = 0;
755 struct epoch_tracker *mrouter_et = locked;
756 MROUTER_RUNLOCK_PARAM(mrouter_et);
761 /* Stop and drain task queue */
762 taskqueue_block(V_task_queue);
763 while (taskqueue_cancel(V_task_queue, &V_task, NULL)) {
764 taskqueue_drain(V_task_queue, &V_task);
768 taskqueue_cancel(V_task_queue, &V_task, NULL);
770 /* Destroy upcall ring */
771 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
774 buf_ring_free(V_bw_upcalls_ring, M_MRTABLE);
775 mtx_destroy(&V_bw_upcalls_ring_mtx);
778 * For each phyint in use, disable promiscuous reception of all IP
781 for (vifi = 0; vifi < V_numvifs; vifi++) {
782 if (!in_nullhost(V_viftable[vifi].v_lcl_addr) &&
783 !(V_viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
784 ifp = V_viftable[vifi].v_ifp;
788 bzero((caddr_t)V_viftable, sizeof(*V_viftable) * MAXVIFS);
790 V_pim_assert_enabled = 0;
792 callout_stop(&V_expire_upcalls_ch);
793 callout_stop(&V_bw_upcalls_ch);
796 * Free all multicast forwarding cache entries.
797 * Do not use hashdestroy(), as we must perform other cleanup.
799 for (i = 0; i < mfchashsize; i++) {
800 struct mfc *rt, *nrt;
802 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
806 free(V_mfchashtbl, M_MRTABLE);
809 bzero(V_nexpire, sizeof(V_nexpire[0]) * mfchashsize);
811 V_reg_vif_num = VIFI_INVALID;
813 mtx_destroy(&V_buf_ring_mtx);
817 CTR1(KTR_IPMF, "%s: done", __func__);
823 * Set PIM assert processing global
828 if ((i != 1) && (i != 0))
831 V_pim_assert_enabled = i;
837 * Configure API capabilities
840 set_api_config(uint32_t *apival)
845 * We can set the API capabilities only if it is the first operation
846 * after MRT_INIT. I.e.:
847 * - there are no vifs installed
848 * - pim_assert is not enabled
849 * - the MFC table is empty
855 if (V_pim_assert_enabled) {
862 for (i = 0; i < mfchashsize; i++) {
863 if (LIST_FIRST(&V_mfchashtbl[i]) != NULL) {
872 V_mrt_api_config = *apival & mrt_api_support;
873 *apival = V_mrt_api_config;
879 * Add a vif to the vif table
882 add_vif(struct vifctl *vifcp)
884 struct vif *vifp = V_viftable + vifcp->vifc_vifi;
885 struct sockaddr_in sin = {sizeof sin, AF_INET};
891 if (vifcp->vifc_vifi >= MAXVIFS)
893 /* rate limiting is no longer supported by this code */
894 if (vifcp->vifc_rate_limit != 0) {
895 log(LOG_ERR, "rate limiting is no longer supported\n");
899 if (in_nullhost(vifcp->vifc_lcl_addr))
900 return EADDRNOTAVAIL;
902 /* Find the interface with an address in AF_INET family */
903 if (vifcp->vifc_flags & VIFF_REGISTER) {
905 * XXX: Because VIFF_REGISTER does not really need a valid
906 * local interface (e.g. it could be 127.0.0.2), we don't
911 struct epoch_tracker et;
913 sin.sin_addr = vifcp->vifc_lcl_addr;
915 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
918 return EADDRNOTAVAIL;
921 /* XXX FIXME we need to take a ref on ifp and cleanup properly! */
925 if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
926 CTR1(KTR_IPMF, "%s: tunnels are no longer supported", __func__);
928 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
929 ifp = V_multicast_register_if = if_alloc(IFT_LOOP);
930 CTR2(KTR_IPMF, "%s: add register vif for ifp %p", __func__, ifp);
931 if (V_reg_vif_num == VIFI_INVALID) {
932 if_initname(V_multicast_register_if, "register_vif", 0);
933 V_reg_vif_num = vifcp->vifc_vifi;
935 } else { /* Make sure the interface supports multicast */
936 if ((ifp->if_flags & IFF_MULTICAST) == 0)
939 /* Enable promiscuous reception of all IP multicasts from the if */
940 error = if_allmulti(ifp, 1);
947 if (!in_nullhost(vifp->v_lcl_addr)) {
949 V_multicast_register_if = NULL;
956 vifp->v_flags = vifcp->vifc_flags;
957 vifp->v_threshold = vifcp->vifc_threshold;
958 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
959 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
961 /* initialize per vif pkt counters */
964 vifp->v_bytes_in = 0;
965 vifp->v_bytes_out = 0;
966 sprintf(vifp->v_spin_name, "BM[%d] spin", vifcp->vifc_vifi);
967 mtx_init(&vifp->v_spin, vifp->v_spin_name, NULL, MTX_SPIN);
969 /* Adjust numvifs up if the vifi is higher than numvifs */
970 if (V_numvifs <= vifcp->vifc_vifi)
971 V_numvifs = vifcp->vifc_vifi + 1;
975 CTR4(KTR_IPMF, "%s: add vif %d laddr 0x%08x thresh %x", __func__,
976 (int)vifcp->vifc_vifi, ntohl(vifcp->vifc_lcl_addr.s_addr),
977 (int)vifcp->vifc_threshold);
983 * Delete a vif from the vif table
986 del_vif_locked(vifi_t vifi)
992 if (vifi >= V_numvifs) {
995 vifp = &V_viftable[vifi];
996 if (in_nullhost(vifp->v_lcl_addr)) {
997 return EADDRNOTAVAIL;
1000 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1001 if_allmulti(vifp->v_ifp, 0);
1003 if (vifp->v_flags & VIFF_REGISTER) {
1004 V_reg_vif_num = VIFI_INVALID;
1006 if (vifp->v_ifp == V_multicast_register_if)
1007 V_multicast_register_if = NULL;
1008 if_free(vifp->v_ifp);
1012 mtx_destroy(&vifp->v_spin);
1014 bzero((caddr_t)vifp, sizeof (*vifp));
1016 CTR2(KTR_IPMF, "%s: delete vif %d", __func__, (int)vifi);
1018 /* Adjust numvifs down */
1019 for (vifi = V_numvifs; vifi > 0; vifi--)
1020 if (!in_nullhost(V_viftable[vifi-1].v_lcl_addr))
1028 del_vif(vifi_t vifi)
1033 cc = del_vif_locked(vifi);
1040 * update an mfc entry without resetting counters and S,G addresses.
1043 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1047 rt->mfc_parent = mfccp->mfcc_parent;
1048 for (i = 0; i < V_numvifs; i++) {
1049 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1050 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & V_mrt_api_config &
1053 /* set the RP address */
1054 if (V_mrt_api_config & MRT_MFC_RP)
1055 rt->mfc_rp = mfccp->mfcc_rp;
1057 rt->mfc_rp.s_addr = INADDR_ANY;
1061 * fully initialize an mfc entry from the parameter.
1064 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1066 rt->mfc_origin = mfccp->mfcc_origin;
1067 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1069 update_mfc_params(rt, mfccp);
1071 /* initialize pkt counters per src-grp */
1072 rt->mfc_pkt_cnt = 0;
1073 rt->mfc_byte_cnt = 0;
1074 rt->mfc_wrong_if = 0;
1075 timevalclear(&rt->mfc_last_assert);
1079 expire_mfc(struct mfc *rt)
1085 free_bw_list(rt->mfc_bw_meter_leq);
1086 free_bw_list(rt->mfc_bw_meter_geq);
1088 while (!buf_ring_empty(rt->mfc_stall_ring)) {
1089 rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
1092 free(rte, M_MRTABLE);
1095 buf_ring_free(rt->mfc_stall_ring, M_MRTABLE);
1097 LIST_REMOVE(rt, mfc_hash);
1098 free(rt, M_MRTABLE);
1105 add_mfc(struct mfcctl2 *mfccp)
1113 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1115 /* If an entry already exists, just update the fields */
1117 CTR4(KTR_IPMF, "%s: update mfc orig 0x%08x group %lx parent %x",
1118 __func__, ntohl(mfccp->mfcc_origin.s_addr),
1119 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1120 mfccp->mfcc_parent);
1121 update_mfc_params(rt, mfccp);
1127 * Find the entry for which the upcall was made and update
1130 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
1131 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
1132 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1133 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
1134 !buf_ring_empty(rt->mfc_stall_ring)) {
1136 "%s: add mfc orig 0x%08x group %lx parent %x qh %p",
1137 __func__, ntohl(mfccp->mfcc_origin.s_addr),
1138 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1140 rt->mfc_stall_ring);
1142 CTR1(KTR_IPMF, "%s: multiple matches", __func__);
1144 init_mfc_params(rt, mfccp);
1145 rt->mfc_expire = 0; /* Don't clean this guy up */
1148 /* Free queued packets, but attempt to forward them first. */
1149 while (!buf_ring_empty(rt->mfc_stall_ring)) {
1150 rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
1151 if (rte->ifp != NULL)
1152 ip_mdq(rte->m, rte->ifp, rt, -1);
1154 free(rte, M_MRTABLE);
1160 * It is possible that an entry is being inserted without an upcall
1163 CTR1(KTR_IPMF, "%s: adding mfc w/o upcall", __func__);
1164 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
1165 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1166 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
1167 init_mfc_params(rt, mfccp);
1175 if (rt == NULL) { /* no upcall, so make a new entry */
1182 init_mfc_params(rt, mfccp);
1185 rt->mfc_bw_meter_leq = NULL;
1186 rt->mfc_bw_meter_geq = NULL;
1188 /* insert new entry at head of hash chain */
1189 LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
1199 * Delete an mfc entry
1202 del_mfc(struct mfcctl2 *mfccp)
1204 struct in_addr origin;
1205 struct in_addr mcastgrp;
1208 origin = mfccp->mfcc_origin;
1209 mcastgrp = mfccp->mfcc_mcastgrp;
1211 CTR3(KTR_IPMF, "%s: delete mfc orig 0x%08x group %lx", __func__,
1212 ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1216 rt = mfc_find(&origin, &mcastgrp);
1219 return EADDRNOTAVAIL;
1223 * free the bw_meter entries
1225 free_bw_list(rt->mfc_bw_meter_leq);
1226 rt->mfc_bw_meter_leq = NULL;
1227 free_bw_list(rt->mfc_bw_meter_geq);
1228 rt->mfc_bw_meter_geq = NULL;
1230 LIST_REMOVE(rt, mfc_hash);
1231 free(rt, M_MRTABLE);
1239 * Send a message to the routing daemon on the multicast routing socket.
1242 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1245 SOCKBUF_LOCK(&s->so_rcv);
1246 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1248 sorwakeup_locked(s);
1251 soroverflow_locked(s);
1258 * IP multicast forwarding function. This function assumes that the packet
1259 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1260 * pointed to by "ifp", and the packet is to be relayed to other networks
1261 * that have members of the packet's destination IP multicast group.
1263 * The packet is returned unscathed to the caller, unless it is
1264 * erroneous, in which case a non-zero return value tells the caller to
1268 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1271 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1272 struct ip_moptions *imo)
1282 CTR3(KTR_IPMF, "ip_mforward: delete mfc orig 0x%08x group %lx ifp %p",
1283 ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr), ifp);
1285 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1286 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
1288 * Packet arrived via a physical interface or
1289 * an encapsulated tunnel or a register_vif.
1293 * Packet arrived through a source-route tunnel.
1294 * Source-route tunnels are no longer supported.
1300 * BEGIN: MCAST ROUTING HOT PATH
1303 if (imo && ((vifi = imo->imo_multicast_vif) < V_numvifs)) {
1304 if (ip->ip_ttl < MAXTTL)
1305 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1306 error = ip_mdq(m, ifp, NULL, vifi);
1312 * Don't forward a packet with time-to-live of zero or one,
1313 * or a packet destined to a local-only group.
1315 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr))) {
1322 * Determine forwarding vifs from the forwarding cache table
1324 MRTSTAT_INC(mrts_mfc_lookups);
1325 rt = mfc_find(&ip->ip_src, &ip->ip_dst);
1327 /* Entry exists, so forward if necessary */
1329 error = ip_mdq(m, ifp, rt, -1);
1330 /* Generic unlock here as we might release R or W lock */
1336 * END: MCAST ROUTING HOT PATH
1339 /* Further processing must be done with WLOCK taken */
1340 if ((MRW_WOWNED() == 0) && (MRW_LOCK_TRY_UPGRADE() == 0)) {
1343 goto mfc_find_retry;
1347 * If we don't have a route for packet's origin,
1348 * Make a copy of the packet & send message to routing daemon
1350 hlen = ip->ip_hl << 2;
1352 MRTSTAT_INC(mrts_mfc_misses);
1353 MRTSTAT_INC(mrts_no_route);
1354 CTR2(KTR_IPMF, "ip_mforward: no mfc for (0x%08x,%lx)",
1355 ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr));
1358 * Allocate mbufs early so that we don't do extra work if we are
1359 * just going to fail anyway. Make sure to pullup the header so
1360 * that other people can't step on it.
1362 rte = (struct rtdetq*) malloc((sizeof *rte), M_MRTABLE,
1369 mb0 = m_copypacket(m, M_NOWAIT);
1370 if (mb0 && (!M_WRITABLE(mb0) || mb0->m_len < hlen))
1371 mb0 = m_pullup(mb0, hlen);
1373 free(rte, M_MRTABLE);
1378 /* is there an upcall waiting for this flow ? */
1379 hash = MFCHASH(ip->ip_src, ip->ip_dst);
1380 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash)
1382 if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
1383 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
1384 !buf_ring_empty(rt->mfc_stall_ring))
1391 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1395 * Locate the vifi for the incoming interface for this packet.
1396 * If none found, drop packet.
1398 for (vifi = 0; vifi < V_numvifs &&
1399 V_viftable[vifi].v_ifp != ifp; vifi++)
1401 if (vifi >= V_numvifs) /* vif not found, drop packet */
1404 /* no upcall, so make a new entry */
1409 /* Make a copy of the header to send to the user level process */
1410 mm = m_copym(mb0, 0, hlen, M_NOWAIT);
1415 * Send message to routing daemon to install
1416 * a route into the kernel table
1419 im = mtod(mm, struct igmpmsg*);
1420 im->im_msgtype = IGMPMSG_NOCACHE;
1424 MRTSTAT_INC(mrts_upcalls);
1426 k_igmpsrc.sin_addr = ip->ip_src;
1427 if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
1428 CTR0(KTR_IPMF, "ip_mforward: socket queue full");
1429 MRTSTAT_INC(mrts_upq_sockfull);
1430 fail1: free(rt, M_MRTABLE);
1431 fail: free(rte, M_MRTABLE);
1437 /* insert new entry at head of hash chain */
1438 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1439 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1440 rt->mfc_expire = UPCALL_EXPIRE;
1442 for (i = 0; i < V_numvifs; i++) {
1443 rt->mfc_ttls[i] = 0;
1444 rt->mfc_flags[i] = 0;
1446 rt->mfc_parent = -1;
1448 /* clear the RP address */
1449 rt->mfc_rp.s_addr = INADDR_ANY;
1450 rt->mfc_bw_meter_leq = NULL;
1451 rt->mfc_bw_meter_geq = NULL;
1453 /* initialize pkt counters per src-grp */
1454 rt->mfc_pkt_cnt = 0;
1455 rt->mfc_byte_cnt = 0;
1456 rt->mfc_wrong_if = 0;
1457 timevalclear(&rt->mfc_last_assert);
1459 buf_ring_enqueue(rt->mfc_stall_ring, rte);
1461 /* Add RT to hashtable as it didn't exist before */
1462 LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
1464 /* determine if queue has overflowed */
1465 if (buf_ring_full(rt->mfc_stall_ring)) {
1466 MRTSTAT_INC(mrts_upq_ovflw);
1467 non_fatal: free(rte, M_MRTABLE);
1473 buf_ring_enqueue(rt->mfc_stall_ring, rte);
1485 * Clean up the cache entry if upcall is not serviced
1488 expire_upcalls(void *arg)
1492 CURVNET_SET((struct vnet *) arg);
1494 /*This callout is always run with MRW_WLOCK taken. */
1496 for (i = 0; i < mfchashsize; i++) {
1497 struct mfc *rt, *nrt;
1499 if (V_nexpire[i] == 0)
1502 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
1503 if (buf_ring_empty(rt->mfc_stall_ring))
1506 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
1509 MRTSTAT_INC(mrts_cache_cleanups);
1510 CTR3(KTR_IPMF, "%s: expire (%lx, %lx)", __func__,
1511 (u_long)ntohl(rt->mfc_origin.s_addr),
1512 (u_long)ntohl(rt->mfc_mcastgrp.s_addr));
1518 callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
1525 * Packet forwarding routine once entry in the cache is made
1528 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1530 struct ip *ip = mtod(m, struct ip *);
1532 int plen = ntohs(ip->ip_len);
1537 * If xmt_vif is not -1, send on only the requested vif.
1539 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1541 if (xmt_vif < V_numvifs) {
1542 if (V_viftable[xmt_vif].v_flags & VIFF_REGISTER)
1543 pim_register_send(ip, V_viftable + xmt_vif, m, rt);
1545 phyint_send(ip, V_viftable + xmt_vif, m);
1550 * Don't forward if it didn't arrive from the parent vif for its origin.
1552 vifi = rt->mfc_parent;
1553 if ((vifi >= V_numvifs) || (V_viftable[vifi].v_ifp != ifp)) {
1554 CTR4(KTR_IPMF, "%s: rx on wrong ifp %p (vifi %d, v_ifp %p)",
1555 __func__, ifp, (int)vifi, V_viftable[vifi].v_ifp);
1556 MRTSTAT_INC(mrts_wrong_if);
1559 * If we are doing PIM assert processing, send a message
1560 * to the routing daemon.
1562 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1563 * can complete the SPT switch, regardless of the type
1564 * of the iif (broadcast media, GRE tunnel, etc).
1566 if (V_pim_assert_enabled && (vifi < V_numvifs) &&
1567 V_viftable[vifi].v_ifp) {
1568 if (ifp == V_multicast_register_if)
1569 PIMSTAT_INC(pims_rcv_registers_wrongiif);
1571 /* Get vifi for the incoming packet */
1572 for (vifi = 0; vifi < V_numvifs && V_viftable[vifi].v_ifp != ifp;
1575 if (vifi >= V_numvifs)
1576 return 0; /* The iif is not found: ignore the packet. */
1578 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1579 return 0; /* WRONGVIF disabled: ignore the packet */
1581 if (ratecheck(&rt->mfc_last_assert, &pim_assert_interval)) {
1582 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1584 int hlen = ip->ip_hl << 2;
1585 struct mbuf *mm = m_copym(m, 0, hlen, M_NOWAIT);
1587 if (mm && (!M_WRITABLE(mm) || mm->m_len < hlen))
1588 mm = m_pullup(mm, hlen);
1592 im = mtod(mm, struct igmpmsg *);
1593 im->im_msgtype = IGMPMSG_WRONGVIF;
1597 MRTSTAT_INC(mrts_upcalls);
1599 k_igmpsrc.sin_addr = im->im_src;
1600 if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
1601 CTR1(KTR_IPMF, "%s: socket queue full", __func__);
1602 MRTSTAT_INC(mrts_upq_sockfull);
1610 /* If I sourced this packet, it counts as output, else it was input. */
1611 mtx_lock_spin(&V_viftable[vifi].v_spin);
1612 if (in_hosteq(ip->ip_src, V_viftable[vifi].v_lcl_addr)) {
1613 V_viftable[vifi].v_pkt_out++;
1614 V_viftable[vifi].v_bytes_out += plen;
1616 V_viftable[vifi].v_pkt_in++;
1617 V_viftable[vifi].v_bytes_in += plen;
1619 mtx_unlock_spin(&V_viftable[vifi].v_spin);
1622 rt->mfc_byte_cnt += plen;
1625 * For each vif, decide if a copy of the packet should be forwarded.
1627 * - the ttl exceeds the vif's threshold
1628 * - there are group members downstream on interface
1630 for (vifi = 0; vifi < V_numvifs; vifi++)
1631 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1632 V_viftable[vifi].v_pkt_out++;
1633 V_viftable[vifi].v_bytes_out += plen;
1634 if (V_viftable[vifi].v_flags & VIFF_REGISTER)
1635 pim_register_send(ip, V_viftable + vifi, m, rt);
1637 phyint_send(ip, V_viftable + vifi, m);
1641 * Perform upcall-related bw measuring.
1643 if ((rt->mfc_bw_meter_geq != NULL) || (rt->mfc_bw_meter_leq != NULL)) {
1648 /* Process meters for Greater-or-EQual case */
1649 for (x = rt->mfc_bw_meter_geq; x != NULL; x = x->bm_mfc_next)
1650 bw_meter_geq_receive_packet(x, plen, &now);
1652 /* Process meters for Lower-or-EQual case */
1653 for (x = rt->mfc_bw_meter_leq; x != NULL; x = x->bm_mfc_next) {
1655 * Record that a packet is received.
1656 * Spin lock has to be taken as callout context
1657 * (expire_bw_meter_leq) might modify these fields
1660 mtx_lock_spin(&x->bm_spin);
1661 x->bm_measured.b_packets++;
1662 x->bm_measured.b_bytes += plen;
1663 mtx_unlock_spin(&x->bm_spin);
1671 * Check if a vif number is legal/ok. This is used by in_mcast.c.
1674 X_legal_vif_num(int vif)
1683 if (vif < V_numvifs)
1691 * Return the local address used by this vif
1694 X_ip_mcast_src(int vifi)
1703 if (vifi < V_numvifs)
1704 addr = V_viftable[vifi].v_lcl_addr.s_addr;
1711 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1713 struct mbuf *mb_copy;
1714 int hlen = ip->ip_hl << 2;
1719 * Make a new reference to the packet; make sure that
1720 * the IP header is actually copied, not just referenced,
1721 * so that ip_output() only scribbles on the copy.
1723 mb_copy = m_copypacket(m, M_NOWAIT);
1724 if (mb_copy && (!M_WRITABLE(mb_copy) || mb_copy->m_len < hlen))
1725 mb_copy = m_pullup(mb_copy, hlen);
1726 if (mb_copy == NULL)
1729 send_packet(vifp, mb_copy);
1733 send_packet(struct vif *vifp, struct mbuf *m)
1735 struct ip_moptions imo;
1740 imo.imo_multicast_ifp = vifp->v_ifp;
1741 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1742 imo.imo_multicast_loop = !!in_mcast_loop;
1743 imo.imo_multicast_vif = -1;
1744 STAILQ_INIT(&imo.imo_head);
1747 * Re-entrancy should not be a problem here, because
1748 * the packets that we send out and are looped back at us
1749 * should get rejected because they appear to come from
1750 * the loopback interface, thus preventing looping.
1752 error = ip_output(m, NULL, NULL, IP_FORWARDING, &imo, NULL);
1753 CTR3(KTR_IPMF, "%s: vif %td err %d", __func__,
1754 (ptrdiff_t)(vifp - V_viftable), error);
1758 * Stubs for old RSVP socket shim implementation.
1762 X_ip_rsvp_vif(struct socket *so __unused, struct sockopt *sopt __unused)
1765 return (EOPNOTSUPP);
1769 X_ip_rsvp_force_done(struct socket *so __unused)
1775 X_rsvp_input(struct mbuf **mp, int *offp, int proto)
1783 return (IPPROTO_DONE);
1787 * Code for bandwidth monitors
1791 * Define common interface for timeval-related methods
1793 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1794 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1795 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1798 compute_bw_meter_flags(struct bw_upcall *req)
1802 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1803 flags |= BW_METER_UNIT_PACKETS;
1804 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1805 flags |= BW_METER_UNIT_BYTES;
1806 if (req->bu_flags & BW_UPCALL_GEQ)
1807 flags |= BW_METER_GEQ;
1808 if (req->bu_flags & BW_UPCALL_LEQ)
1809 flags |= BW_METER_LEQ;
1815 expire_bw_meter_leq(void *arg)
1817 struct bw_meter *x = arg;
1821 * callout is always executed with MRW_WLOCK taken
1824 CURVNET_SET((struct vnet *)x->arg);
1829 * Test if we should deliver an upcall
1831 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
1832 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
1833 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
1834 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
1835 /* Prepare an upcall for delivery */
1836 bw_meter_prepare_upcall(x, &now);
1839 /* Send all upcalls that are pending delivery */
1840 taskqueue_enqueue(V_task_queue, &V_task);
1842 /* Reset counters */
1843 x->bm_start_time = now;
1844 /* Spin lock has to be taken as ip_forward context
1845 * might modify these fields as well
1847 mtx_lock_spin(&x->bm_spin);
1848 x->bm_measured.b_bytes = 0;
1849 x->bm_measured.b_packets = 0;
1850 mtx_unlock_spin(&x->bm_spin);
1852 callout_schedule(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time));
1858 * Add a bw_meter entry
1861 add_bw_upcall(struct bw_upcall *req)
1864 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
1865 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
1867 struct bw_meter *x, **bwm_ptr;
1870 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
1873 /* Test if the flags are valid */
1874 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
1876 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
1878 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
1879 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
1882 /* Test if the threshold time interval is valid */
1883 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
1886 flags = compute_bw_meter_flags(req);
1889 * Find if we have already same bw_meter entry
1892 mfc = mfc_find(&req->bu_src, &req->bu_dst);
1895 return EADDRNOTAVAIL;
1898 /* Choose an appropriate bw_meter list */
1899 if (req->bu_flags & BW_UPCALL_GEQ)
1900 bwm_ptr = &mfc->mfc_bw_meter_geq;
1902 bwm_ptr = &mfc->mfc_bw_meter_leq;
1904 for (x = *bwm_ptr; x != NULL; x = x->bm_mfc_next) {
1905 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
1906 &req->bu_threshold.b_time, ==))
1907 && (x->bm_threshold.b_packets
1908 == req->bu_threshold.b_packets)
1909 && (x->bm_threshold.b_bytes
1910 == req->bu_threshold.b_bytes)
1911 && (x->bm_flags & BW_METER_USER_FLAGS)
1914 return 0; /* XXX Already installed */
1918 /* Allocate the new bw_meter entry */
1919 x = (struct bw_meter*) malloc(sizeof(*x), M_BWMETER,
1926 /* Set the new bw_meter entry */
1927 x->bm_threshold.b_time = req->bu_threshold.b_time;
1929 x->bm_start_time = now;
1930 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
1931 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
1932 x->bm_measured.b_packets = 0;
1933 x->bm_measured.b_bytes = 0;
1934 x->bm_flags = flags;
1935 x->bm_time_next = NULL;
1938 sprintf(x->bm_spin_name, "BM spin %p", x);
1939 mtx_init(&x->bm_spin, x->bm_spin_name, NULL, MTX_SPIN);
1941 /* For LEQ case create periodic callout */
1942 if (req->bu_flags & BW_UPCALL_LEQ) {
1943 callout_init_rw(&x->bm_meter_callout, &mrouter_mtx, CALLOUT_SHAREDLOCK);
1944 callout_reset(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time),
1945 expire_bw_meter_leq, x);
1948 /* Add the new bw_meter entry to the front of entries for this MFC */
1949 x->bm_mfc_next = *bwm_ptr;
1958 free_bw_list(struct bw_meter *list)
1960 while (list != NULL) {
1961 struct bw_meter *x = list;
1963 /* MRW_WLOCK must be held here */
1964 if (x->bm_flags & BW_METER_LEQ) {
1965 callout_drain(&x->bm_meter_callout);
1966 mtx_destroy(&x->bm_spin);
1969 list = list->bm_mfc_next;
1975 * Delete one or multiple bw_meter entries
1978 del_bw_upcall(struct bw_upcall *req)
1981 struct bw_meter *x, **bwm_ptr;
1983 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
1988 /* Find the corresponding MFC entry */
1989 mfc = mfc_find(&req->bu_src, &req->bu_dst);
1992 return EADDRNOTAVAIL;
1993 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
1995 * Delete all bw_meter entries for this mfc
1997 struct bw_meter *list;
2000 list = mfc->mfc_bw_meter_leq;
2001 mfc->mfc_bw_meter_leq = NULL;
2005 list = mfc->mfc_bw_meter_geq;
2006 mfc->mfc_bw_meter_geq = NULL;
2010 } else { /* Delete a single bw_meter entry */
2011 struct bw_meter *prev;
2014 flags = compute_bw_meter_flags(req);
2016 /* Choose an appropriate bw_meter list */
2017 if (req->bu_flags & BW_UPCALL_GEQ)
2018 bwm_ptr = &mfc->mfc_bw_meter_geq;
2020 bwm_ptr = &mfc->mfc_bw_meter_leq;
2022 /* Find the bw_meter entry to delete */
2023 for (prev = NULL, x = *bwm_ptr; x != NULL;
2024 prev = x, x = x->bm_mfc_next) {
2025 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2026 &req->bu_threshold.b_time, ==)) &&
2027 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2028 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2029 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2032 if (x != NULL) { /* Delete entry from the list for this MFC */
2034 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2036 *bwm_ptr = x->bm_mfc_next;/* new head of list */
2038 if (req->bu_flags & BW_UPCALL_LEQ)
2039 callout_stop(&x->bm_meter_callout);
2042 /* Free the bw_meter entry */
2054 * Perform bandwidth measurement processing that may result in an upcall
2057 bw_meter_geq_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2059 struct timeval delta;
2064 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2067 * Processing for ">=" type of bw_meter entry.
2068 * bm_spin does not have to be hold here as in GEQ
2069 * case this is the only context accessing bm_measured.
2071 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2072 /* Reset the bw_meter entry */
2073 x->bm_start_time = *nowp;
2074 x->bm_measured.b_packets = 0;
2075 x->bm_measured.b_bytes = 0;
2076 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2079 /* Record that a packet is received */
2080 x->bm_measured.b_packets++;
2081 x->bm_measured.b_bytes += plen;
2084 * Test if we should deliver an upcall
2086 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2087 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2088 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2089 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2090 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2091 /* Prepare an upcall for delivery */
2092 bw_meter_prepare_upcall(x, nowp);
2093 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2099 * Prepare a bandwidth-related upcall
2102 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2104 struct timeval delta;
2105 struct bw_upcall *u;
2110 * Compute the measured time interval
2113 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2116 * Set the bw_upcall entry
2118 u = malloc(sizeof(struct bw_upcall), M_MRTABLE, M_NOWAIT | M_ZERO);
2120 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot allocate entry\n");
2123 u->bu_src = x->bm_mfc->mfc_origin;
2124 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2125 u->bu_threshold.b_time = x->bm_threshold.b_time;
2126 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2127 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2128 u->bu_measured.b_time = delta;
2129 u->bu_measured.b_packets = x->bm_measured.b_packets;
2130 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2132 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2133 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2134 if (x->bm_flags & BW_METER_UNIT_BYTES)
2135 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2136 if (x->bm_flags & BW_METER_GEQ)
2137 u->bu_flags |= BW_UPCALL_GEQ;
2138 if (x->bm_flags & BW_METER_LEQ)
2139 u->bu_flags |= BW_UPCALL_LEQ;
2141 if (buf_ring_enqueue(V_bw_upcalls_ring, u))
2142 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot enqueue upcall\n");
2143 if (buf_ring_count(V_bw_upcalls_ring) > (BW_UPCALLS_MAX / 2)) {
2144 taskqueue_enqueue(V_task_queue, &V_task);
2148 * Send the pending bandwidth-related upcalls
2151 bw_upcalls_send(void)
2155 struct bw_upcall *bu;
2156 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2157 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2159 IGMPMSG_BW_UPCALL,/* im_msgtype */
2164 { 0 } }; /* im_dst */
2168 if (buf_ring_empty(V_bw_upcalls_ring))
2172 * Allocate a new mbuf, initialize it with the header and
2173 * the payload for the pending calls.
2175 m = m_gethdr(M_NOWAIT, MT_DATA);
2177 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2181 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2182 len += sizeof(struct igmpmsg);
2183 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
2184 m_copyback(m, len, sizeof(struct bw_upcall), (caddr_t)bu);
2185 len += sizeof(struct bw_upcall);
2186 free(bu, M_MRTABLE);
2191 * XXX do we need to set the address in k_igmpsrc ?
2193 MRTSTAT_INC(mrts_upcalls);
2194 if (socket_send(V_ip_mrouter, m, &k_igmpsrc) < 0) {
2195 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2196 MRTSTAT_INC(mrts_upq_sockfull);
2201 * A periodic function for sending all upcalls that are pending delivery
2204 expire_bw_upcalls_send(void *arg)
2206 CURVNET_SET((struct vnet *) arg);
2208 /* This callout is run with MRW_RLOCK taken */
2212 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
2218 * End of bandwidth monitoring code
2222 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2226 pim_register_send(struct ip *ip, struct vif *vifp, struct mbuf *m,
2229 struct mbuf *mb_copy, *mm;
2232 * Do not send IGMP_WHOLEPKT notifications to userland, if the
2233 * rendezvous point was unspecified, and we were told not to.
2235 if (pim_squelch_wholepkt != 0 && (V_mrt_api_config & MRT_MFC_RP) &&
2236 in_nullhost(rt->mfc_rp))
2239 mb_copy = pim_register_prepare(ip, m);
2240 if (mb_copy == NULL)
2244 * Send all the fragments. Note that the mbuf for each fragment
2245 * is freed by the sending machinery.
2247 for (mm = mb_copy; mm; mm = mb_copy) {
2248 mb_copy = mm->m_nextpkt;
2250 mm = m_pullup(mm, sizeof(struct ip));
2252 ip = mtod(mm, struct ip *);
2253 if ((V_mrt_api_config & MRT_MFC_RP) && !in_nullhost(rt->mfc_rp)) {
2254 pim_register_send_rp(ip, vifp, mm, rt);
2256 pim_register_send_upcall(ip, vifp, mm, rt);
2265 * Return a copy of the data packet that is ready for PIM Register
2267 * XXX: Note that in the returned copy the IP header is a valid one.
2269 static struct mbuf *
2270 pim_register_prepare(struct ip *ip, struct mbuf *m)
2272 struct mbuf *mb_copy = NULL;
2275 /* Take care of delayed checksums */
2276 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2277 in_delayed_cksum(m);
2278 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2282 * Copy the old packet & pullup its IP header into the
2283 * new mbuf so we can modify it.
2285 mb_copy = m_copypacket(m, M_NOWAIT);
2286 if (mb_copy == NULL)
2288 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2289 if (mb_copy == NULL)
2292 /* take care of the TTL */
2293 ip = mtod(mb_copy, struct ip *);
2296 /* Compute the MTU after the PIM Register encapsulation */
2297 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2299 if (ntohs(ip->ip_len) <= mtu) {
2300 /* Turn the IP header into a valid one */
2302 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2304 /* Fragment the packet */
2305 mb_copy->m_pkthdr.csum_flags |= CSUM_IP;
2306 if (ip_fragment(ip, &mb_copy, mtu, 0) != 0) {
2315 * Send an upcall with the data packet to the user-level process.
2318 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2319 struct mbuf *mb_copy, struct mfc *rt)
2321 struct mbuf *mb_first;
2322 int len = ntohs(ip->ip_len);
2324 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2329 * Add a new mbuf with an upcall header
2331 mb_first = m_gethdr(M_NOWAIT, MT_DATA);
2332 if (mb_first == NULL) {
2336 mb_first->m_data += max_linkhdr;
2337 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2338 mb_first->m_len = sizeof(struct igmpmsg);
2339 mb_first->m_next = mb_copy;
2341 /* Send message to routing daemon */
2342 im = mtod(mb_first, struct igmpmsg *);
2343 im->im_msgtype = IGMPMSG_WHOLEPKT;
2345 im->im_vif = vifp - V_viftable;
2346 im->im_src = ip->ip_src;
2347 im->im_dst = ip->ip_dst;
2349 k_igmpsrc.sin_addr = ip->ip_src;
2351 MRTSTAT_INC(mrts_upcalls);
2353 if (socket_send(V_ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2354 CTR1(KTR_IPMF, "%s: socket queue full", __func__);
2355 MRTSTAT_INC(mrts_upq_sockfull);
2359 /* Keep statistics */
2360 PIMSTAT_INC(pims_snd_registers_msgs);
2361 PIMSTAT_ADD(pims_snd_registers_bytes, len);
2367 * Encapsulate the data packet in PIM Register message and send it to the RP.
2370 pim_register_send_rp(struct ip *ip, struct vif *vifp, struct mbuf *mb_copy,
2373 struct mbuf *mb_first;
2374 struct ip *ip_outer;
2375 struct pim_encap_pimhdr *pimhdr;
2376 int len = ntohs(ip->ip_len);
2377 vifi_t vifi = rt->mfc_parent;
2381 if ((vifi >= V_numvifs) || in_nullhost(V_viftable[vifi].v_lcl_addr)) {
2383 return EADDRNOTAVAIL; /* The iif vif is invalid */
2387 * Add a new mbuf with the encapsulating header
2389 mb_first = m_gethdr(M_NOWAIT, MT_DATA);
2390 if (mb_first == NULL) {
2394 mb_first->m_data += max_linkhdr;
2395 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2396 mb_first->m_next = mb_copy;
2398 mb_first->m_pkthdr.len = len + mb_first->m_len;
2401 * Fill in the encapsulating IP and PIM header
2403 ip_outer = mtod(mb_first, struct ip *);
2404 *ip_outer = pim_encap_iphdr;
2405 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
2406 sizeof(pim_encap_pimhdr));
2407 ip_outer->ip_src = V_viftable[vifi].v_lcl_addr;
2408 ip_outer->ip_dst = rt->mfc_rp;
2410 * Copy the inner header TOS to the outer header, and take care of the
2413 ip_outer->ip_tos = ip->ip_tos;
2414 if (ip->ip_off & htons(IP_DF))
2415 ip_outer->ip_off |= htons(IP_DF);
2416 ip_fillid(ip_outer);
2417 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2418 + sizeof(pim_encap_iphdr));
2419 *pimhdr = pim_encap_pimhdr;
2420 /* If the iif crosses a border, set the Border-bit */
2421 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & V_mrt_api_config)
2422 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2424 mb_first->m_data += sizeof(pim_encap_iphdr);
2425 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2426 mb_first->m_data -= sizeof(pim_encap_iphdr);
2428 send_packet(vifp, mb_first);
2430 /* Keep statistics */
2431 PIMSTAT_INC(pims_snd_registers_msgs);
2432 PIMSTAT_ADD(pims_snd_registers_bytes, len);
2438 * pim_encapcheck() is called by the encap4_input() path at runtime to
2439 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2443 pim_encapcheck(const struct mbuf *m __unused, int off __unused,
2444 int proto __unused, void *arg __unused)
2447 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2448 return (8); /* claim the datagram. */
2452 * PIM-SMv2 and PIM-DM messages processing.
2453 * Receives and verifies the PIM control messages, and passes them
2454 * up to the listening socket, using rip_input().
2455 * The only message with special processing is the PIM_REGISTER message
2456 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2457 * is passed to if_simloop().
2460 pim_input(struct mbuf *m, int off, int proto, void *arg __unused)
2462 struct ip *ip = mtod(m, struct ip *);
2466 int datalen = ntohs(ip->ip_len) - iphlen;
2469 /* Keep statistics */
2470 PIMSTAT_INC(pims_rcv_total_msgs);
2471 PIMSTAT_ADD(pims_rcv_total_bytes, datalen);
2476 if (datalen < PIM_MINLEN) {
2477 PIMSTAT_INC(pims_rcv_tooshort);
2478 CTR3(KTR_IPMF, "%s: short packet (%d) from 0x%08x",
2479 __func__, datalen, ntohl(ip->ip_src.s_addr));
2481 return (IPPROTO_DONE);
2485 * If the packet is at least as big as a REGISTER, go agead
2486 * and grab the PIM REGISTER header size, to avoid another
2487 * possible m_pullup() later.
2489 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
2490 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2492 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2494 * Get the IP and PIM headers in contiguous memory, and
2495 * possibly the PIM REGISTER header.
2497 if (m->m_len < minlen && (m = m_pullup(m, minlen)) == NULL) {
2498 CTR1(KTR_IPMF, "%s: m_pullup() failed", __func__);
2499 return (IPPROTO_DONE);
2502 /* m_pullup() may have given us a new mbuf so reset ip. */
2503 ip = mtod(m, struct ip *);
2504 ip_tos = ip->ip_tos;
2506 /* adjust mbuf to point to the PIM header */
2507 m->m_data += iphlen;
2509 pim = mtod(m, struct pim *);
2512 * Validate checksum. If PIM REGISTER, exclude the data packet.
2514 * XXX: some older PIMv2 implementations don't make this distinction,
2515 * so for compatibility reason perform the checksum over part of the
2516 * message, and if error, then over the whole message.
2518 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2519 /* do nothing, checksum okay */
2520 } else if (in_cksum(m, datalen)) {
2521 PIMSTAT_INC(pims_rcv_badsum);
2522 CTR1(KTR_IPMF, "%s: invalid checksum", __func__);
2524 return (IPPROTO_DONE);
2527 /* PIM version check */
2528 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2529 PIMSTAT_INC(pims_rcv_badversion);
2530 CTR3(KTR_IPMF, "%s: bad version %d expect %d", __func__,
2531 (int)PIM_VT_V(pim->pim_vt), PIM_VERSION);
2533 return (IPPROTO_DONE);
2536 /* restore mbuf back to the outer IP */
2537 m->m_data -= iphlen;
2540 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2542 * Since this is a REGISTER, we'll make a copy of the register
2543 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2546 struct sockaddr_in dst = { sizeof(dst), AF_INET };
2548 struct ip *encap_ip;
2553 if ((V_reg_vif_num >= V_numvifs) || (V_reg_vif_num == VIFI_INVALID)) {
2555 CTR2(KTR_IPMF, "%s: register vif not set: %d", __func__,
2556 (int)V_reg_vif_num);
2558 return (IPPROTO_DONE);
2560 /* XXX need refcnt? */
2561 vifp = V_viftable[V_reg_vif_num].v_ifp;
2567 if (datalen < PIM_REG_MINLEN) {
2568 PIMSTAT_INC(pims_rcv_tooshort);
2569 PIMSTAT_INC(pims_rcv_badregisters);
2570 CTR1(KTR_IPMF, "%s: register packet size too small", __func__);
2572 return (IPPROTO_DONE);
2575 reghdr = (u_int32_t *)(pim + 1);
2576 encap_ip = (struct ip *)(reghdr + 1);
2578 CTR3(KTR_IPMF, "%s: register: encap ip src 0x%08x len %d",
2579 __func__, ntohl(encap_ip->ip_src.s_addr),
2580 ntohs(encap_ip->ip_len));
2582 /* verify the version number of the inner packet */
2583 if (encap_ip->ip_v != IPVERSION) {
2584 PIMSTAT_INC(pims_rcv_badregisters);
2585 CTR1(KTR_IPMF, "%s: bad encap ip version", __func__);
2587 return (IPPROTO_DONE);
2590 /* verify the inner packet is destined to a mcast group */
2591 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2592 PIMSTAT_INC(pims_rcv_badregisters);
2593 CTR2(KTR_IPMF, "%s: bad encap ip dest 0x%08x", __func__,
2594 ntohl(encap_ip->ip_dst.s_addr));
2596 return (IPPROTO_DONE);
2599 /* If a NULL_REGISTER, pass it to the daemon */
2600 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2601 goto pim_input_to_daemon;
2604 * Copy the TOS from the outer IP header to the inner IP header.
2606 if (encap_ip->ip_tos != ip_tos) {
2607 /* Outer TOS -> inner TOS */
2608 encap_ip->ip_tos = ip_tos;
2609 /* Recompute the inner header checksum. Sigh... */
2611 /* adjust mbuf to point to the inner IP header */
2612 m->m_data += (iphlen + PIM_MINLEN);
2613 m->m_len -= (iphlen + PIM_MINLEN);
2615 encap_ip->ip_sum = 0;
2616 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2618 /* restore mbuf to point back to the outer IP header */
2619 m->m_data -= (iphlen + PIM_MINLEN);
2620 m->m_len += (iphlen + PIM_MINLEN);
2624 * Decapsulate the inner IP packet and loopback to forward it
2625 * as a normal multicast packet. Also, make a copy of the
2626 * outer_iphdr + pimhdr + reghdr + encap_iphdr
2627 * to pass to the daemon later, so it can take the appropriate
2628 * actions (e.g., send back PIM_REGISTER_STOP).
2629 * XXX: here m->m_data points to the outer IP header.
2631 mcp = m_copym(m, 0, iphlen + PIM_REG_MINLEN, M_NOWAIT);
2633 CTR1(KTR_IPMF, "%s: m_copym() failed", __func__);
2635 return (IPPROTO_DONE);
2638 /* Keep statistics */
2639 /* XXX: registers_bytes include only the encap. mcast pkt */
2640 PIMSTAT_INC(pims_rcv_registers_msgs);
2641 PIMSTAT_ADD(pims_rcv_registers_bytes, ntohs(encap_ip->ip_len));
2644 * forward the inner ip packet; point m_data at the inner ip.
2646 m_adj(m, iphlen + PIM_MINLEN);
2649 "%s: forward decap'd REGISTER: src %lx dst %lx vif %d",
2651 (u_long)ntohl(encap_ip->ip_src.s_addr),
2652 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2653 (int)V_reg_vif_num);
2655 /* NB: vifp was collected above; can it change on us? */
2656 if_simloop(vifp, m, dst.sin_family, 0);
2658 /* prepare the register head to send to the mrouting daemon */
2662 pim_input_to_daemon:
2664 * Pass the PIM message up to the daemon; if it is a Register message,
2665 * pass the 'head' only up to the daemon. This includes the
2666 * outer IP header, PIM header, PIM-Register header and the
2668 * XXX: the outer IP header pkt size of a Register is not adjust to
2669 * reflect the fact that the inner multicast data is truncated.
2671 return (rip_input(&m, &off, proto));
2675 sysctl_mfctable(SYSCTL_HANDLER_ARGS)
2682 if (V_mfchashtbl == NULL) /* XXX unlocked */
2684 error = sysctl_wire_old_buffer(req, 0);
2689 for (i = 0; i < mfchashsize; i++) {
2690 LIST_FOREACH(rt, &V_mfchashtbl[i], mfc_hash) {
2691 error = SYSCTL_OUT(req, rt, sizeof(struct mfc));
2701 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mfctable,
2702 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mfctable,
2703 "IPv4 Multicast Forwarding Table "
2704 "(struct *mfc[mfchashsize], netinet/ip_mroute.h)");
2707 sysctl_viflist(SYSCTL_HANDLER_ARGS)
2713 if (V_viftable == NULL) /* XXX unlocked */
2715 error = sysctl_wire_old_buffer(req, sizeof(*V_viftable) * MAXVIFS);
2720 error = SYSCTL_OUT(req, V_viftable, sizeof(*V_viftable) * MAXVIFS);
2725 SYSCTL_PROC(_net_inet_ip, OID_AUTO, viftable,
2726 CTLTYPE_OPAQUE | CTLFLAG_VNET | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2727 sysctl_viflist, "S,vif[MAXVIFS]",
2728 "IPv4 Multicast Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
2731 vnet_mroute_init(const void *unused __unused)
2734 V_nexpire = malloc(mfchashsize, M_MRTABLE, M_WAITOK|M_ZERO);
2736 V_viftable = mallocarray(MAXVIFS, sizeof(*V_viftable),
2737 M_MRTABLE, M_WAITOK|M_ZERO);
2739 callout_init_rw(&V_expire_upcalls_ch, &mrouter_mtx, 0);
2740 callout_init_rw(&V_bw_upcalls_ch, &mrouter_mtx, 0);
2742 /* Prepare taskqueue */
2743 V_task_queue = taskqueue_create_fast("ip_mroute_tskq", M_NOWAIT,
2744 taskqueue_thread_enqueue, &V_task_queue);
2745 taskqueue_start_threads(&V_task_queue, 1, PI_NET, "ip_mroute_tskq task");
2748 VNET_SYSINIT(vnet_mroute_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mroute_init,
2752 vnet_mroute_uninit(const void *unused __unused)
2755 /* Taskqueue should be cancelled and drained before freeing */
2756 taskqueue_free(V_task_queue);
2758 free(V_viftable, M_MRTABLE);
2759 free(V_nexpire, M_MRTABLE);
2763 VNET_SYSUNINIT(vnet_mroute_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE,
2764 vnet_mroute_uninit, NULL);
2767 ip_mroute_modevent(module_t mod, int type, void *unused)
2774 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2775 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
2776 if (if_detach_event_tag == NULL) {
2777 printf("ip_mroute: unable to register "
2778 "ifnet_departure_event handler\n");
2783 mfchashsize = MFCHASHSIZE;
2784 if (TUNABLE_ULONG_FETCH("net.inet.ip.mfchashsize", &mfchashsize) &&
2785 !powerof2(mfchashsize)) {
2786 printf("WARNING: %s not a power of 2; using default\n",
2787 "net.inet.ip.mfchashsize");
2788 mfchashsize = MFCHASHSIZE;
2791 pim_squelch_wholepkt = 0;
2792 TUNABLE_ULONG_FETCH("net.inet.pim.squelch_wholepkt",
2793 &pim_squelch_wholepkt);
2795 pim_encap_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
2796 if (pim_encap_cookie == NULL) {
2797 printf("ip_mroute: unable to attach pim encap\n");
2802 ip_mcast_src = X_ip_mcast_src;
2803 ip_mforward = X_ip_mforward;
2804 ip_mrouter_done = X_ip_mrouter_done;
2805 ip_mrouter_get = X_ip_mrouter_get;
2806 ip_mrouter_set = X_ip_mrouter_set;
2808 ip_rsvp_force_done = X_ip_rsvp_force_done;
2809 ip_rsvp_vif = X_ip_rsvp_vif;
2811 legal_vif_num = X_legal_vif_num;
2812 mrt_ioctl = X_mrt_ioctl;
2813 rsvp_input_p = X_rsvp_input;
2818 * Typically module unload happens after the user-level
2819 * process has shutdown the kernel services (the check
2820 * below insures someone can't just yank the module out
2821 * from under a running process). But if the module is
2822 * just loaded and then unloaded w/o starting up a user
2823 * process we still need to cleanup.
2826 if (ip_mrouter_cnt != 0) {
2830 ip_mrouter_unloading = 1;
2833 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2835 if (pim_encap_cookie) {
2836 ip_encap_detach(pim_encap_cookie);
2837 pim_encap_cookie = NULL;
2840 ip_mcast_src = NULL;
2842 ip_mrouter_done = NULL;
2843 ip_mrouter_get = NULL;
2844 ip_mrouter_set = NULL;
2846 ip_rsvp_force_done = NULL;
2849 legal_vif_num = NULL;
2851 rsvp_input_p = NULL;
2862 static moduledata_t ip_mroutemod = {
2868 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);