mtw(4) remove misplaced DEBUG_FLAGS
[freebsd/src.git] / sys / netinet / ip_mroute.c
blob501b3e0d9a43b841e1d3c83358045c1f9f9c5909
1 /*-
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
13 * are met:
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
33 * SUCH DAMAGE.
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
50 * Modified by Wojciech Macek, Semihalf, May 2021
52 * MROUTING Revision: 3.5
53 * and PIM-SMv2 and PIM-DM support, advanced API support,
54 * bandwidth metering and signaling
58 * TODO: Prefix functions with ipmf_.
59 * TODO: Maintain a refcount on if_allmulti() in ifnet or in the protocol
60 * domain attachment (if_afdata) so we can track consumers of that service.
61 * TODO: Deprecate routing socket path for SIOCGETSGCNT and SIOCGETVIFCNT,
62 * move it to socket options.
63 * TODO: Cleanup LSRR removal further.
64 * TODO: Push RSVP stubs into raw_ip.c.
65 * TODO: Use bitstring.h for vif set.
66 * TODO: Fix mrt6_ioctl dangling ref when dynamically loaded.
67 * TODO: Sync ip6_mroute.c with this file.
70 #include <sys/cdefs.h>
71 #include "opt_inet.h"
72 #include "opt_mrouting.h"
74 #define _PIM_VT 1
76 #include <sys/types.h>
77 #include <sys/param.h>
78 #include <sys/kernel.h>
79 #include <sys/stddef.h>
80 #include <sys/condvar.h>
81 #include <sys/eventhandler.h>
82 #include <sys/lock.h>
83 #include <sys/kthread.h>
84 #include <sys/ktr.h>
85 #include <sys/malloc.h>
86 #include <sys/mbuf.h>
87 #include <sys/module.h>
88 #include <sys/priv.h>
89 #include <sys/protosw.h>
90 #include <sys/signalvar.h>
91 #include <sys/socket.h>
92 #include <sys/socketvar.h>
93 #include <sys/sockio.h>
94 #include <sys/sx.h>
95 #include <sys/sysctl.h>
96 #include <sys/syslog.h>
97 #include <sys/systm.h>
98 #include <sys/taskqueue.h>
99 #include <sys/time.h>
100 #include <sys/counter.h>
101 #include <machine/atomic.h>
103 #include <net/if.h>
104 #include <net/if_var.h>
105 #include <net/if_private.h>
106 #include <net/if_types.h>
107 #include <net/netisr.h>
108 #include <net/route.h>
109 #include <net/vnet.h>
111 #include <netinet/in.h>
112 #include <netinet/igmp.h>
113 #include <netinet/in_systm.h>
114 #include <netinet/in_var.h>
115 #include <netinet/ip.h>
116 #include <netinet/ip_encap.h>
117 #include <netinet/ip_mroute.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/ip_options.h>
120 #include <netinet/pim.h>
121 #include <netinet/pim_var.h>
122 #include <netinet/udp.h>
124 #include <machine/in_cksum.h>
126 #ifndef KTR_IPMF
127 #define KTR_IPMF KTR_INET
128 #endif
130 #define VIFI_INVALID ((vifi_t) -1)
132 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast forwarding cache");
135 * Locking. We use two locks: one for the virtual interface table and
136 * one for the forwarding table. These locks may be nested in which case
137 * the VIF lock must always be taken first. Note that each lock is used
138 * to cover not only the specific data structure but also related data
139 * structures.
142 static struct rwlock mrouter_lock;
143 #define MRW_RLOCK() rw_rlock(&mrouter_lock)
144 #define MRW_WLOCK() rw_wlock(&mrouter_lock)
145 #define MRW_RUNLOCK() rw_runlock(&mrouter_lock)
146 #define MRW_WUNLOCK() rw_wunlock(&mrouter_lock)
147 #define MRW_UNLOCK() rw_unlock(&mrouter_lock)
148 #define MRW_LOCK_ASSERT() rw_assert(&mrouter_lock, RA_LOCKED)
149 #define MRW_WLOCK_ASSERT() rw_assert(&mrouter_lock, RA_WLOCKED)
150 #define MRW_LOCK_TRY_UPGRADE() rw_try_upgrade(&mrouter_lock)
151 #define MRW_WOWNED() rw_wowned(&mrouter_lock)
152 #define MRW_LOCK_INIT() \
153 rw_init(&mrouter_lock, "IPv4 multicast forwarding")
154 #define MRW_LOCK_DESTROY() rw_destroy(&mrouter_lock)
156 static int ip_mrouter_cnt; /* # of vnets with active mrouters */
157 static int ip_mrouter_unloading; /* Allow no more V_ip_mrouter sockets */
159 VNET_PCPUSTAT_DEFINE_STATIC(struct mrtstat, mrtstat);
160 VNET_PCPUSTAT_SYSINIT(mrtstat);
161 VNET_PCPUSTAT_SYSUNINIT(mrtstat);
162 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, OID_AUTO, mrtstat, struct mrtstat,
163 mrtstat, "IPv4 Multicast Forwarding Statistics (struct mrtstat, "
164 "netinet/ip_mroute.h)");
166 VNET_DEFINE_STATIC(u_long, mfchash);
167 #define V_mfchash VNET(mfchash)
168 #define MFCHASH(a, g) \
169 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
170 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & V_mfchash)
171 #define MFCHASHSIZE 256
173 static u_long mfchashsize = MFCHASHSIZE; /* Hash size */
174 SYSCTL_ULONG(_net_inet_ip, OID_AUTO, mfchashsize, CTLFLAG_RDTUN,
175 &mfchashsize, 0, "IPv4 Multicast Forwarding Table hash size");
176 VNET_DEFINE_STATIC(u_char *, nexpire); /* 0..mfchashsize-1 */
177 #define V_nexpire VNET(nexpire)
178 VNET_DEFINE_STATIC(LIST_HEAD(mfchashhdr, mfc)*, mfchashtbl);
179 #define V_mfchashtbl VNET(mfchashtbl)
180 VNET_DEFINE_STATIC(struct taskqueue *, task_queue);
181 #define V_task_queue VNET(task_queue)
182 VNET_DEFINE_STATIC(struct task, task);
183 #define V_task VNET(task)
185 VNET_DEFINE_STATIC(vifi_t, numvifs);
186 #define V_numvifs VNET(numvifs)
187 VNET_DEFINE_STATIC(struct vif *, viftable);
188 #define V_viftable VNET(viftable)
190 static eventhandler_tag if_detach_event_tag = NULL;
192 VNET_DEFINE_STATIC(struct callout, expire_upcalls_ch);
193 #define V_expire_upcalls_ch VNET(expire_upcalls_ch)
195 VNET_DEFINE_STATIC(struct mtx, buf_ring_mtx);
196 #define V_buf_ring_mtx VNET(buf_ring_mtx)
198 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
199 #define UPCALL_EXPIRE 6 /* number of timeouts */
202 * Bandwidth meter variables and constants
204 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
207 * Pending upcalls are stored in a ring which is flushed when
208 * full, or periodically
210 VNET_DEFINE_STATIC(struct callout, bw_upcalls_ch);
211 #define V_bw_upcalls_ch VNET(bw_upcalls_ch)
212 VNET_DEFINE_STATIC(struct buf_ring *, bw_upcalls_ring);
213 #define V_bw_upcalls_ring VNET(bw_upcalls_ring)
214 VNET_DEFINE_STATIC(struct mtx, bw_upcalls_ring_mtx);
215 #define V_bw_upcalls_ring_mtx VNET(bw_upcalls_ring_mtx)
217 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
219 VNET_PCPUSTAT_DEFINE_STATIC(struct pimstat, pimstat);
220 VNET_PCPUSTAT_SYSINIT(pimstat);
221 VNET_PCPUSTAT_SYSUNINIT(pimstat);
223 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
224 "PIM");
225 SYSCTL_VNET_PCPUSTAT(_net_inet_pim, PIMCTL_STATS, stats, struct pimstat,
226 pimstat, "PIM Statistics (struct pimstat, netinet/pim_var.h)");
228 static u_long pim_squelch_wholepkt = 0;
229 SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RWTUN,
230 &pim_squelch_wholepkt, 0,
231 "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
233 static const struct encaptab *pim_encap_cookie;
234 static int pim_encapcheck(const struct mbuf *, int, int, void *);
235 static int pim_input(struct mbuf *, int, int, void *);
237 extern int in_mcast_loop;
239 static const struct encap_config ipv4_encap_cfg = {
240 .proto = IPPROTO_PIM,
241 .min_length = sizeof(struct ip) + PIM_MINLEN,
242 .exact_match = 8,
243 .check = pim_encapcheck,
244 .input = pim_input
248 * Note: the PIM Register encapsulation adds the following in front of a
249 * data packet:
251 * struct pim_encap_hdr {
252 * struct ip ip;
253 * struct pim_encap_pimhdr pim;
258 struct pim_encap_pimhdr {
259 struct pim pim;
260 uint32_t flags;
262 #define PIM_ENCAP_TTL 64
264 static struct ip pim_encap_iphdr = {
265 #if BYTE_ORDER == LITTLE_ENDIAN
266 sizeof(struct ip) >> 2,
267 IPVERSION,
268 #else
269 IPVERSION,
270 sizeof(struct ip) >> 2,
271 #endif
272 0, /* tos */
273 sizeof(struct ip), /* total length */
274 0, /* id */
275 0, /* frag offset */
276 PIM_ENCAP_TTL,
277 IPPROTO_PIM,
278 0, /* checksum */
281 static struct pim_encap_pimhdr pim_encap_pimhdr = {
283 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
284 0, /* reserved */
285 0, /* checksum */
287 0 /* flags */
290 VNET_DEFINE_STATIC(vifi_t, reg_vif_num) = VIFI_INVALID;
291 #define V_reg_vif_num VNET(reg_vif_num)
292 VNET_DEFINE_STATIC(struct ifnet *, multicast_register_if);
293 #define V_multicast_register_if VNET(multicast_register_if)
296 * Private variables.
299 static u_long X_ip_mcast_src(int);
300 static int X_ip_mforward(struct ip *, struct ifnet *, struct mbuf *,
301 struct ip_moptions *);
302 static int X_ip_mrouter_done(void);
303 static int X_ip_mrouter_get(struct socket *, struct sockopt *);
304 static int X_ip_mrouter_set(struct socket *, struct sockopt *);
305 static int X_legal_vif_num(int);
306 static int X_mrt_ioctl(u_long, caddr_t, int);
308 static int add_bw_upcall(struct bw_upcall *);
309 static int add_mfc(struct mfcctl2 *);
310 static int add_vif(struct vifctl *);
311 static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *);
312 static void bw_meter_geq_receive_packet(struct bw_meter *, int,
313 struct timeval *);
314 static void bw_upcalls_send(void);
315 static int del_bw_upcall(struct bw_upcall *);
316 static int del_mfc(struct mfcctl2 *);
317 static int del_vif(vifi_t);
318 static int del_vif_locked(vifi_t, struct ifnet **, struct ifnet **);
319 static void expire_bw_upcalls_send(void *);
320 static void expire_mfc(struct mfc *);
321 static void expire_upcalls(void *);
322 static void free_bw_list(struct bw_meter *);
323 static int get_sg_cnt(struct sioc_sg_req *);
324 static int get_vif_cnt(struct sioc_vif_req *);
325 static void if_detached_event(void *, struct ifnet *);
326 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
327 static int ip_mrouter_init(struct socket *, int);
328 static __inline struct mfc *
329 mfc_find(struct in_addr *, struct in_addr *);
330 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
331 static struct mbuf *
332 pim_register_prepare(struct ip *, struct mbuf *);
333 static int pim_register_send(struct ip *, struct vif *,
334 struct mbuf *, struct mfc *);
335 static int pim_register_send_rp(struct ip *, struct vif *,
336 struct mbuf *, struct mfc *);
337 static int pim_register_send_upcall(struct ip *, struct vif *,
338 struct mbuf *, struct mfc *);
339 static void send_packet(struct vif *, struct mbuf *);
340 static int set_api_config(uint32_t *);
341 static int set_assert(int);
342 static int socket_send(struct socket *, struct mbuf *,
343 struct sockaddr_in *);
346 * Kernel multicast forwarding API capabilities and setup.
347 * If more API capabilities are added to the kernel, they should be
348 * recorded in `mrt_api_support'.
350 #define MRT_API_VERSION 0x0305
352 static const int mrt_api_version = MRT_API_VERSION;
353 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
354 MRT_MFC_FLAGS_BORDER_VIF |
355 MRT_MFC_RP |
356 MRT_MFC_BW_UPCALL);
357 VNET_DEFINE_STATIC(uint32_t, mrt_api_config);
358 #define V_mrt_api_config VNET(mrt_api_config)
359 VNET_DEFINE_STATIC(int, pim_assert_enabled);
360 #define V_pim_assert_enabled VNET(pim_assert_enabled)
361 static struct timeval pim_assert_interval = { 3, 0 }; /* Rate limit */
364 * Find a route for a given origin IP address and multicast group address.
365 * Statistics must be updated by the caller.
367 static __inline struct mfc *
368 mfc_find(struct in_addr *o, struct in_addr *g)
370 struct mfc *rt;
373 * Might be called both RLOCK and WLOCK.
374 * Check if any, it's caller responsibility
375 * to choose correct option.
377 MRW_LOCK_ASSERT();
379 LIST_FOREACH(rt, &V_mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
380 if (in_hosteq(rt->mfc_origin, *o) &&
381 in_hosteq(rt->mfc_mcastgrp, *g) &&
382 buf_ring_empty(rt->mfc_stall_ring))
383 break;
386 return (rt);
389 static __inline struct mfc *
390 mfc_alloc(void)
392 struct mfc *rt;
393 rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT | M_ZERO);
394 if (rt == NULL)
395 return rt;
397 rt->mfc_stall_ring = buf_ring_alloc(MAX_UPQ, M_MRTABLE,
398 M_NOWAIT, &V_buf_ring_mtx);
399 if (rt->mfc_stall_ring == NULL) {
400 free(rt, M_MRTABLE);
401 return NULL;
404 return rt;
408 * Handle MRT setsockopt commands to modify the multicast forwarding tables.
410 static int
411 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
413 int error, optval;
414 vifi_t vifi;
415 struct vifctl vifc;
416 struct mfcctl2 mfc;
417 struct bw_upcall bw_upcall;
418 uint32_t i;
420 if (so != V_ip_mrouter && sopt->sopt_name != MRT_INIT)
421 return EPERM;
423 error = 0;
424 switch (sopt->sopt_name) {
425 case MRT_INIT:
426 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
427 if (error)
428 break;
429 error = ip_mrouter_init(so, optval);
430 break;
431 case MRT_DONE:
432 error = ip_mrouter_done();
433 break;
434 case MRT_ADD_VIF:
435 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
436 if (error)
437 break;
438 error = add_vif(&vifc);
439 break;
440 case MRT_DEL_VIF:
441 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
442 if (error)
443 break;
444 error = del_vif(vifi);
445 break;
446 case MRT_ADD_MFC:
447 case MRT_DEL_MFC:
449 * select data size depending on API version.
451 if (sopt->sopt_name == MRT_ADD_MFC &&
452 V_mrt_api_config & MRT_API_FLAGS_ALL) {
453 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
454 sizeof(struct mfcctl2));
455 } else {
456 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
457 sizeof(struct mfcctl));
458 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
459 sizeof(mfc) - sizeof(struct mfcctl));
461 if (error)
462 break;
463 if (sopt->sopt_name == MRT_ADD_MFC)
464 error = add_mfc(&mfc);
465 else
466 error = del_mfc(&mfc);
467 break;
469 case MRT_ASSERT:
470 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
471 if (error)
472 break;
473 set_assert(optval);
474 break;
476 case MRT_API_CONFIG:
477 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
478 if (!error)
479 error = set_api_config(&i);
480 if (!error)
481 error = sooptcopyout(sopt, &i, sizeof i);
482 break;
484 case MRT_ADD_BW_UPCALL:
485 case MRT_DEL_BW_UPCALL:
486 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
487 sizeof bw_upcall);
488 if (error)
489 break;
490 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
491 error = add_bw_upcall(&bw_upcall);
492 else
493 error = del_bw_upcall(&bw_upcall);
494 break;
496 default:
497 error = EOPNOTSUPP;
498 break;
500 return error;
504 * Handle MRT getsockopt commands
506 static int
507 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
509 int error;
511 switch (sopt->sopt_name) {
512 case MRT_VERSION:
513 error = sooptcopyout(sopt, &mrt_api_version,
514 sizeof mrt_api_version);
515 break;
516 case MRT_ASSERT:
517 error = sooptcopyout(sopt, &V_pim_assert_enabled,
518 sizeof V_pim_assert_enabled);
519 break;
520 case MRT_API_SUPPORT:
521 error = sooptcopyout(sopt, &mrt_api_support,
522 sizeof mrt_api_support);
523 break;
524 case MRT_API_CONFIG:
525 error = sooptcopyout(sopt, &V_mrt_api_config,
526 sizeof V_mrt_api_config);
527 break;
528 default:
529 error = EOPNOTSUPP;
530 break;
532 return error;
536 * Handle ioctl commands to obtain information from the cache
538 static int
539 X_mrt_ioctl(u_long cmd, caddr_t data, int fibnum __unused)
541 int error;
544 * Currently the only function calling this ioctl routine is rtioctl_fib().
545 * Typically, only root can create the raw socket in order to execute
546 * this ioctl method, however the request might be coming from a prison
548 error = priv_check(curthread, PRIV_NETINET_MROUTE);
549 if (error)
550 return (error);
551 switch (cmd) {
552 case (SIOCGETVIFCNT):
553 error = get_vif_cnt((struct sioc_vif_req *)data);
554 break;
556 case (SIOCGETSGCNT):
557 error = get_sg_cnt((struct sioc_sg_req *)data);
558 break;
560 default:
561 error = EINVAL;
562 break;
564 return error;
568 * returns the packet, byte, rpf-failure count for the source group provided
570 static int
571 get_sg_cnt(struct sioc_sg_req *req)
573 struct mfc *rt;
575 MRW_RLOCK();
576 rt = mfc_find(&req->src, &req->grp);
577 if (rt == NULL) {
578 MRW_RUNLOCK();
579 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
580 return EADDRNOTAVAIL;
582 req->pktcnt = rt->mfc_pkt_cnt;
583 req->bytecnt = rt->mfc_byte_cnt;
584 req->wrong_if = rt->mfc_wrong_if;
585 MRW_RUNLOCK();
586 return 0;
590 * returns the input and output packet and byte counts on the vif provided
592 static int
593 get_vif_cnt(struct sioc_vif_req *req)
595 vifi_t vifi = req->vifi;
597 MRW_RLOCK();
598 if (vifi >= V_numvifs) {
599 MRW_RUNLOCK();
600 return EINVAL;
603 mtx_lock_spin(&V_viftable[vifi].v_spin);
604 req->icount = V_viftable[vifi].v_pkt_in;
605 req->ocount = V_viftable[vifi].v_pkt_out;
606 req->ibytes = V_viftable[vifi].v_bytes_in;
607 req->obytes = V_viftable[vifi].v_bytes_out;
608 mtx_unlock_spin(&V_viftable[vifi].v_spin);
609 MRW_RUNLOCK();
611 return 0;
614 static void
615 if_detached_event(void *arg __unused, struct ifnet *ifp)
617 vifi_t vifi;
618 u_long i, vifi_cnt = 0;
619 struct ifnet *free_ptr, *multi_leave;
621 MRW_WLOCK();
623 if (V_ip_mrouter == NULL) {
624 MRW_WUNLOCK();
625 return;
629 * Tear down multicast forwarder state associated with this ifnet.
630 * 1. Walk the vif list, matching vifs against this ifnet.
631 * 2. Walk the multicast forwarding cache (mfc) looking for
632 * inner matches with this vif's index.
633 * 3. Expire any matching multicast forwarding cache entries.
634 * 4. Free vif state. This should disable ALLMULTI on the interface.
636 restart:
637 for (vifi = 0; vifi < V_numvifs; vifi++) {
638 if (V_viftable[vifi].v_ifp != ifp)
639 continue;
640 for (i = 0; i < mfchashsize; i++) {
641 struct mfc *rt, *nrt;
643 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
644 if (rt->mfc_parent == vifi) {
645 expire_mfc(rt);
649 del_vif_locked(vifi, &multi_leave, &free_ptr);
650 if (free_ptr != NULL)
651 vifi_cnt++;
652 if (multi_leave) {
653 MRW_WUNLOCK();
654 if_allmulti(multi_leave, 0);
655 MRW_WLOCK();
656 goto restart;
660 MRW_WUNLOCK();
663 * Free IFP. We don't have to use free_ptr here as it is the same
664 * that ifp. Perform free as many times as required in case
665 * refcount is greater than 1.
667 for (i = 0; i < vifi_cnt; i++)
668 if_free(ifp);
671 static void
672 ip_mrouter_upcall_thread(void *arg, int pending __unused)
674 CURVNET_SET((struct vnet *) arg);
676 MRW_WLOCK();
677 bw_upcalls_send();
678 MRW_WUNLOCK();
680 CURVNET_RESTORE();
684 * Enable multicast forwarding.
686 static int
687 ip_mrouter_init(struct socket *so, int version)
690 CTR2(KTR_IPMF, "%s: so %p", __func__, so);
692 if (version != 1)
693 return ENOPROTOOPT;
695 MRW_WLOCK();
697 if (ip_mrouter_unloading) {
698 MRW_WUNLOCK();
699 return ENOPROTOOPT;
702 if (V_ip_mrouter != NULL) {
703 MRW_WUNLOCK();
704 return EADDRINUSE;
707 V_mfchashtbl = hashinit_flags(mfchashsize, M_MRTABLE, &V_mfchash,
708 HASH_NOWAIT);
709 if (V_mfchashtbl == NULL) {
710 MRW_WUNLOCK();
711 return (ENOMEM);
714 /* Create upcall ring */
715 mtx_init(&V_bw_upcalls_ring_mtx, "mroute upcall buf_ring mtx", NULL, MTX_DEF);
716 V_bw_upcalls_ring = buf_ring_alloc(BW_UPCALLS_MAX, M_MRTABLE,
717 M_NOWAIT, &V_bw_upcalls_ring_mtx);
718 if (!V_bw_upcalls_ring) {
719 MRW_WUNLOCK();
720 return (ENOMEM);
723 TASK_INIT(&V_task, 0, ip_mrouter_upcall_thread, curvnet);
724 taskqueue_cancel(V_task_queue, &V_task, NULL);
725 taskqueue_unblock(V_task_queue);
727 callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
728 curvnet);
729 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
730 curvnet);
732 V_ip_mrouter = so;
733 atomic_add_int(&ip_mrouter_cnt, 1);
735 /* This is a mutex required by buf_ring init, but not used internally */
736 mtx_init(&V_buf_ring_mtx, "mroute buf_ring mtx", NULL, MTX_DEF);
738 MRW_WUNLOCK();
740 CTR1(KTR_IPMF, "%s: done", __func__);
742 return 0;
746 * Disable multicast forwarding.
748 static int
749 X_ip_mrouter_done(void)
751 struct ifnet **ifps;
752 int nifp;
753 u_long i;
754 vifi_t vifi;
755 struct bw_upcall *bu;
757 if (V_ip_mrouter == NULL)
758 return (EINVAL);
761 * Detach/disable hooks to the reset of the system.
763 V_ip_mrouter = NULL;
764 atomic_subtract_int(&ip_mrouter_cnt, 1);
765 V_mrt_api_config = 0;
768 * Wait for all epoch sections to complete to ensure
769 * V_ip_mrouter = NULL is visible to others.
771 NET_EPOCH_WAIT();
773 /* Stop and drain task queue */
774 taskqueue_block(V_task_queue);
775 while (taskqueue_cancel(V_task_queue, &V_task, NULL)) {
776 taskqueue_drain(V_task_queue, &V_task);
779 ifps = malloc(MAXVIFS * sizeof(*ifps), M_TEMP, M_WAITOK);
781 MRW_WLOCK();
782 taskqueue_cancel(V_task_queue, &V_task, NULL);
784 /* Destroy upcall ring */
785 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
786 free(bu, M_MRTABLE);
788 buf_ring_free(V_bw_upcalls_ring, M_MRTABLE);
789 mtx_destroy(&V_bw_upcalls_ring_mtx);
792 * For each phyint in use, prepare to disable promiscuous reception
793 * of all IP multicasts. Defer the actual call until the lock is released;
794 * just record the list of interfaces while locked. Some interfaces use
795 * sx locks in their ioctl routines, which is not allowed while holding
796 * a non-sleepable lock.
798 KASSERT(V_numvifs <= MAXVIFS, ("More vifs than possible"));
799 for (vifi = 0, nifp = 0; vifi < V_numvifs; vifi++) {
800 if (!in_nullhost(V_viftable[vifi].v_lcl_addr) &&
801 !(V_viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
802 ifps[nifp++] = V_viftable[vifi].v_ifp;
805 bzero((caddr_t)V_viftable, sizeof(*V_viftable) * MAXVIFS);
806 V_numvifs = 0;
807 V_pim_assert_enabled = 0;
809 callout_stop(&V_expire_upcalls_ch);
810 callout_stop(&V_bw_upcalls_ch);
813 * Free all multicast forwarding cache entries.
814 * Do not use hashdestroy(), as we must perform other cleanup.
816 for (i = 0; i < mfchashsize; i++) {
817 struct mfc *rt, *nrt;
819 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
820 expire_mfc(rt);
823 free(V_mfchashtbl, M_MRTABLE);
824 V_mfchashtbl = NULL;
826 bzero(V_nexpire, sizeof(V_nexpire[0]) * mfchashsize);
828 V_reg_vif_num = VIFI_INVALID;
830 mtx_destroy(&V_buf_ring_mtx);
832 MRW_WUNLOCK();
835 * Now drop our claim on promiscuous multicast on the interfaces recorded
836 * above. This is safe to do now because ALLMULTI is reference counted.
838 for (vifi = 0; vifi < nifp; vifi++)
839 if_allmulti(ifps[vifi], 0);
840 free(ifps, M_TEMP);
842 CTR1(KTR_IPMF, "%s: done", __func__);
844 return 0;
848 * Set PIM assert processing global
850 static int
851 set_assert(int i)
853 if ((i != 1) && (i != 0))
854 return EINVAL;
856 V_pim_assert_enabled = i;
858 return 0;
862 * Configure API capabilities
865 set_api_config(uint32_t *apival)
867 u_long i;
870 * We can set the API capabilities only if it is the first operation
871 * after MRT_INIT. I.e.:
872 * - there are no vifs installed
873 * - pim_assert is not enabled
874 * - the MFC table is empty
876 if (V_numvifs > 0) {
877 *apival = 0;
878 return EPERM;
880 if (V_pim_assert_enabled) {
881 *apival = 0;
882 return EPERM;
885 MRW_RLOCK();
887 for (i = 0; i < mfchashsize; i++) {
888 if (LIST_FIRST(&V_mfchashtbl[i]) != NULL) {
889 MRW_RUNLOCK();
890 *apival = 0;
891 return EPERM;
895 MRW_RUNLOCK();
897 V_mrt_api_config = *apival & mrt_api_support;
898 *apival = V_mrt_api_config;
900 return 0;
904 * Add a vif to the vif table
906 static int
907 add_vif(struct vifctl *vifcp)
909 struct vif *vifp = V_viftable + vifcp->vifc_vifi;
910 struct sockaddr_in sin = {sizeof sin, AF_INET};
911 struct ifaddr *ifa;
912 struct ifnet *ifp;
913 int error;
915 if (vifcp->vifc_vifi >= MAXVIFS)
916 return EINVAL;
917 /* rate limiting is no longer supported by this code */
918 if (vifcp->vifc_rate_limit != 0) {
919 log(LOG_ERR, "rate limiting is no longer supported\n");
920 return EINVAL;
923 if (in_nullhost(vifcp->vifc_lcl_addr))
924 return EADDRNOTAVAIL;
926 /* Find the interface with an address in AF_INET family */
927 if (vifcp->vifc_flags & VIFF_REGISTER) {
929 * XXX: Because VIFF_REGISTER does not really need a valid
930 * local interface (e.g. it could be 127.0.0.2), we don't
931 * check its address.
933 ifp = NULL;
934 } else {
935 struct epoch_tracker et;
937 sin.sin_addr = vifcp->vifc_lcl_addr;
938 NET_EPOCH_ENTER(et);
939 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
940 if (ifa == NULL) {
941 NET_EPOCH_EXIT(et);
942 return EADDRNOTAVAIL;
944 ifp = ifa->ifa_ifp;
945 /* XXX FIXME we need to take a ref on ifp and cleanup properly! */
946 NET_EPOCH_EXIT(et);
949 if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
950 CTR1(KTR_IPMF, "%s: tunnels are no longer supported", __func__);
951 return EOPNOTSUPP;
952 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
953 ifp = V_multicast_register_if = if_alloc(IFT_LOOP);
954 CTR2(KTR_IPMF, "%s: add register vif for ifp %p", __func__, ifp);
955 if (V_reg_vif_num == VIFI_INVALID) {
956 if_initname(V_multicast_register_if, "register_vif", 0);
957 V_reg_vif_num = vifcp->vifc_vifi;
959 } else { /* Make sure the interface supports multicast */
960 if ((ifp->if_flags & IFF_MULTICAST) == 0)
961 return EOPNOTSUPP;
963 /* Enable promiscuous reception of all IP multicasts from the if */
964 error = if_allmulti(ifp, 1);
965 if (error)
966 return error;
969 MRW_WLOCK();
971 if (!in_nullhost(vifp->v_lcl_addr)) {
972 if (ifp)
973 V_multicast_register_if = NULL;
974 MRW_WUNLOCK();
975 if (ifp)
976 if_free(ifp);
977 return EADDRINUSE;
980 vifp->v_flags = vifcp->vifc_flags;
981 vifp->v_threshold = vifcp->vifc_threshold;
982 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
983 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
984 vifp->v_ifp = ifp;
985 /* initialize per vif pkt counters */
986 vifp->v_pkt_in = 0;
987 vifp->v_pkt_out = 0;
988 vifp->v_bytes_in = 0;
989 vifp->v_bytes_out = 0;
990 sprintf(vifp->v_spin_name, "BM[%d] spin", vifcp->vifc_vifi);
991 mtx_init(&vifp->v_spin, vifp->v_spin_name, NULL, MTX_SPIN);
993 /* Adjust numvifs up if the vifi is higher than numvifs */
994 if (V_numvifs <= vifcp->vifc_vifi)
995 V_numvifs = vifcp->vifc_vifi + 1;
997 MRW_WUNLOCK();
999 CTR4(KTR_IPMF, "%s: add vif %d laddr 0x%08x thresh %x", __func__,
1000 (int)vifcp->vifc_vifi, ntohl(vifcp->vifc_lcl_addr.s_addr),
1001 (int)vifcp->vifc_threshold);
1003 return 0;
1007 * Delete a vif from the vif table
1009 static int
1010 del_vif_locked(vifi_t vifi, struct ifnet **ifp_multi_leave, struct ifnet **ifp_free)
1012 struct vif *vifp;
1014 *ifp_free = NULL;
1015 *ifp_multi_leave = NULL;
1017 MRW_WLOCK_ASSERT();
1019 if (vifi >= V_numvifs) {
1020 return EINVAL;
1022 vifp = &V_viftable[vifi];
1023 if (in_nullhost(vifp->v_lcl_addr)) {
1024 return EADDRNOTAVAIL;
1027 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1028 *ifp_multi_leave = vifp->v_ifp;
1030 if (vifp->v_flags & VIFF_REGISTER) {
1031 V_reg_vif_num = VIFI_INVALID;
1032 if (vifp->v_ifp) {
1033 if (vifp->v_ifp == V_multicast_register_if)
1034 V_multicast_register_if = NULL;
1035 *ifp_free = vifp->v_ifp;
1039 mtx_destroy(&vifp->v_spin);
1041 bzero((caddr_t)vifp, sizeof (*vifp));
1043 CTR2(KTR_IPMF, "%s: delete vif %d", __func__, (int)vifi);
1045 /* Adjust numvifs down */
1046 for (vifi = V_numvifs; vifi > 0; vifi--)
1047 if (!in_nullhost(V_viftable[vifi-1].v_lcl_addr))
1048 break;
1049 V_numvifs = vifi;
1051 return 0;
1054 static int
1055 del_vif(vifi_t vifi)
1057 int cc;
1058 struct ifnet *free_ptr, *multi_leave;
1060 MRW_WLOCK();
1061 cc = del_vif_locked(vifi, &multi_leave, &free_ptr);
1062 MRW_WUNLOCK();
1064 if (multi_leave)
1065 if_allmulti(multi_leave, 0);
1066 if (free_ptr) {
1067 if_free(free_ptr);
1070 return cc;
1074 * update an mfc entry without resetting counters and S,G addresses.
1076 static void
1077 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1079 int i;
1081 rt->mfc_parent = mfccp->mfcc_parent;
1082 for (i = 0; i < V_numvifs; i++) {
1083 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1084 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & V_mrt_api_config &
1085 MRT_MFC_FLAGS_ALL;
1087 /* set the RP address */
1088 if (V_mrt_api_config & MRT_MFC_RP)
1089 rt->mfc_rp = mfccp->mfcc_rp;
1090 else
1091 rt->mfc_rp.s_addr = INADDR_ANY;
1095 * fully initialize an mfc entry from the parameter.
1097 static void
1098 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1100 rt->mfc_origin = mfccp->mfcc_origin;
1101 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1103 update_mfc_params(rt, mfccp);
1105 /* initialize pkt counters per src-grp */
1106 rt->mfc_pkt_cnt = 0;
1107 rt->mfc_byte_cnt = 0;
1108 rt->mfc_wrong_if = 0;
1109 timevalclear(&rt->mfc_last_assert);
1112 static void
1113 expire_mfc(struct mfc *rt)
1115 struct rtdetq *rte;
1117 MRW_WLOCK_ASSERT();
1119 free_bw_list(rt->mfc_bw_meter_leq);
1120 free_bw_list(rt->mfc_bw_meter_geq);
1122 while (!buf_ring_empty(rt->mfc_stall_ring)) {
1123 rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
1124 if (rte) {
1125 m_freem(rte->m);
1126 free(rte, M_MRTABLE);
1129 buf_ring_free(rt->mfc_stall_ring, M_MRTABLE);
1131 LIST_REMOVE(rt, mfc_hash);
1132 free(rt, M_MRTABLE);
1136 * Add an mfc entry
1138 static int
1139 add_mfc(struct mfcctl2 *mfccp)
1141 struct mfc *rt;
1142 struct rtdetq *rte;
1143 u_long hash = 0;
1144 u_short nstl;
1145 struct epoch_tracker et;
1147 MRW_WLOCK();
1148 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1150 /* If an entry already exists, just update the fields */
1151 if (rt) {
1152 CTR4(KTR_IPMF, "%s: update mfc orig 0x%08x group %lx parent %x",
1153 __func__, ntohl(mfccp->mfcc_origin.s_addr),
1154 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1155 mfccp->mfcc_parent);
1156 update_mfc_params(rt, mfccp);
1157 MRW_WUNLOCK();
1158 return (0);
1162 * Find the entry for which the upcall was made and update
1164 nstl = 0;
1165 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
1166 NET_EPOCH_ENTER(et);
1167 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
1168 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1169 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
1170 !buf_ring_empty(rt->mfc_stall_ring)) {
1171 CTR5(KTR_IPMF,
1172 "%s: add mfc orig 0x%08x group %lx parent %x qh %p",
1173 __func__, ntohl(mfccp->mfcc_origin.s_addr),
1174 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1175 mfccp->mfcc_parent,
1176 rt->mfc_stall_ring);
1177 if (nstl++)
1178 CTR1(KTR_IPMF, "%s: multiple matches", __func__);
1180 init_mfc_params(rt, mfccp);
1181 rt->mfc_expire = 0; /* Don't clean this guy up */
1182 V_nexpire[hash]--;
1184 /* Free queued packets, but attempt to forward them first. */
1185 while (!buf_ring_empty(rt->mfc_stall_ring)) {
1186 rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
1187 if (rte->ifp != NULL)
1188 ip_mdq(rte->m, rte->ifp, rt, -1);
1189 m_freem(rte->m);
1190 free(rte, M_MRTABLE);
1194 NET_EPOCH_EXIT(et);
1197 * It is possible that an entry is being inserted without an upcall
1199 if (nstl == 0) {
1200 CTR1(KTR_IPMF, "%s: adding mfc w/o upcall", __func__);
1201 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
1202 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1203 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
1204 init_mfc_params(rt, mfccp);
1205 if (rt->mfc_expire)
1206 V_nexpire[hash]--;
1207 rt->mfc_expire = 0;
1208 break; /* XXX */
1212 if (rt == NULL) { /* no upcall, so make a new entry */
1213 rt = mfc_alloc();
1214 if (rt == NULL) {
1215 MRW_WUNLOCK();
1216 return (ENOBUFS);
1219 init_mfc_params(rt, mfccp);
1221 rt->mfc_expire = 0;
1222 rt->mfc_bw_meter_leq = NULL;
1223 rt->mfc_bw_meter_geq = NULL;
1225 /* insert new entry at head of hash chain */
1226 LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
1230 MRW_WUNLOCK();
1232 return (0);
1236 * Delete an mfc entry
1238 static int
1239 del_mfc(struct mfcctl2 *mfccp)
1241 struct in_addr origin;
1242 struct in_addr mcastgrp;
1243 struct mfc *rt;
1245 origin = mfccp->mfcc_origin;
1246 mcastgrp = mfccp->mfcc_mcastgrp;
1248 CTR3(KTR_IPMF, "%s: delete mfc orig 0x%08x group %lx", __func__,
1249 ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1251 MRW_WLOCK();
1253 LIST_FOREACH(rt, &V_mfchashtbl[MFCHASH(origin, mcastgrp)], mfc_hash) {
1254 if (in_hosteq(rt->mfc_origin, origin) &&
1255 in_hosteq(rt->mfc_mcastgrp, mcastgrp))
1256 break;
1258 if (rt == NULL) {
1259 MRW_WUNLOCK();
1260 return EADDRNOTAVAIL;
1263 expire_mfc(rt);
1265 MRW_WUNLOCK();
1267 return (0);
1271 * Send a message to the routing daemon on the multicast routing socket.
1273 static int
1274 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1276 if (s) {
1277 SOCKBUF_LOCK(&s->so_rcv);
1278 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1279 NULL) != 0) {
1280 sorwakeup_locked(s);
1281 return 0;
1283 soroverflow_locked(s);
1285 m_freem(mm);
1286 return -1;
1290 * IP multicast forwarding function. This function assumes that the packet
1291 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1292 * pointed to by "ifp", and the packet is to be relayed to other networks
1293 * that have members of the packet's destination IP multicast group.
1295 * The packet is returned unscathed to the caller, unless it is
1296 * erroneous, in which case a non-zero return value tells the caller to
1297 * discard it.
1300 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1302 static int
1303 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1304 struct ip_moptions *imo)
1306 struct mfc *rt;
1307 int error;
1308 vifi_t vifi;
1309 struct mbuf *mb0;
1310 struct rtdetq *rte;
1311 u_long hash;
1312 int hlen;
1314 M_ASSERTMAPPED(m);
1316 CTR3(KTR_IPMF, "ip_mforward: delete mfc orig 0x%08x group %lx ifp %p",
1317 ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr), ifp);
1319 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1320 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
1322 * Packet arrived via a physical interface or
1323 * an encapsulated tunnel or a register_vif.
1325 } else {
1327 * Packet arrived through a source-route tunnel.
1328 * Source-route tunnels are no longer supported.
1330 return (1);
1334 * BEGIN: MCAST ROUTING HOT PATH
1336 MRW_RLOCK();
1337 if (imo && ((vifi = imo->imo_multicast_vif) < V_numvifs)) {
1338 if (ip->ip_ttl < MAXTTL)
1339 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1340 error = ip_mdq(m, ifp, NULL, vifi);
1341 MRW_RUNLOCK();
1342 return error;
1346 * Don't forward a packet with time-to-live of zero or one,
1347 * or a packet destined to a local-only group.
1349 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr))) {
1350 MRW_RUNLOCK();
1351 return 0;
1354 mfc_find_retry:
1356 * Determine forwarding vifs from the forwarding cache table
1358 MRTSTAT_INC(mrts_mfc_lookups);
1359 rt = mfc_find(&ip->ip_src, &ip->ip_dst);
1361 /* Entry exists, so forward if necessary */
1362 if (rt != NULL) {
1363 error = ip_mdq(m, ifp, rt, -1);
1364 /* Generic unlock here as we might release R or W lock */
1365 MRW_UNLOCK();
1366 return error;
1370 * END: MCAST ROUTING HOT PATH
1373 /* Further processing must be done with WLOCK taken */
1374 if ((MRW_WOWNED() == 0) && (MRW_LOCK_TRY_UPGRADE() == 0)) {
1375 MRW_RUNLOCK();
1376 MRW_WLOCK();
1377 goto mfc_find_retry;
1381 * If we don't have a route for packet's origin,
1382 * Make a copy of the packet & send message to routing daemon
1384 hlen = ip->ip_hl << 2;
1386 MRTSTAT_INC(mrts_mfc_misses);
1387 MRTSTAT_INC(mrts_no_route);
1388 CTR2(KTR_IPMF, "ip_mforward: no mfc for (0x%08x,%lx)",
1389 ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr));
1392 * Allocate mbufs early so that we don't do extra work if we are
1393 * just going to fail anyway. Make sure to pullup the header so
1394 * that other people can't step on it.
1396 rte = malloc((sizeof *rte), M_MRTABLE, M_NOWAIT|M_ZERO);
1397 if (rte == NULL) {
1398 MRW_WUNLOCK();
1399 return ENOBUFS;
1402 mb0 = m_copypacket(m, M_NOWAIT);
1403 if (mb0 && (!M_WRITABLE(mb0) || mb0->m_len < hlen))
1404 mb0 = m_pullup(mb0, hlen);
1405 if (mb0 == NULL) {
1406 free(rte, M_MRTABLE);
1407 MRW_WUNLOCK();
1408 return ENOBUFS;
1411 /* is there an upcall waiting for this flow ? */
1412 hash = MFCHASH(ip->ip_src, ip->ip_dst);
1413 LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash)
1415 if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
1416 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
1417 !buf_ring_empty(rt->mfc_stall_ring))
1418 break;
1421 if (rt == NULL) {
1422 int i;
1423 struct igmpmsg *im;
1424 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1425 struct mbuf *mm;
1428 * Locate the vifi for the incoming interface for this packet.
1429 * If none found, drop packet.
1431 for (vifi = 0; vifi < V_numvifs &&
1432 V_viftable[vifi].v_ifp != ifp; vifi++)
1434 if (vifi >= V_numvifs) /* vif not found, drop packet */
1435 goto non_fatal;
1437 /* no upcall, so make a new entry */
1438 rt = mfc_alloc();
1439 if (rt == NULL)
1440 goto fail;
1442 /* Make a copy of the header to send to the user level process */
1443 mm = m_copym(mb0, 0, hlen, M_NOWAIT);
1444 if (mm == NULL)
1445 goto fail1;
1448 * Send message to routing daemon to install
1449 * a route into the kernel table
1452 im = mtod(mm, struct igmpmsg*);
1453 im->im_msgtype = IGMPMSG_NOCACHE;
1454 im->im_mbz = 0;
1455 im->im_vif = vifi;
1457 MRTSTAT_INC(mrts_upcalls);
1459 k_igmpsrc.sin_addr = ip->ip_src;
1460 if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
1461 CTR0(KTR_IPMF, "ip_mforward: socket queue full");
1462 MRTSTAT_INC(mrts_upq_sockfull);
1463 fail1: free(rt, M_MRTABLE);
1464 fail: free(rte, M_MRTABLE);
1465 m_freem(mb0);
1466 MRW_WUNLOCK();
1467 return ENOBUFS;
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;
1474 V_nexpire[hash]++;
1475 for (i = 0; i < V_numvifs; i++) {
1476 rt->mfc_ttls[i] = 0;
1477 rt->mfc_flags[i] = 0;
1479 rt->mfc_parent = -1;
1481 /* clear the RP address */
1482 rt->mfc_rp.s_addr = INADDR_ANY;
1483 rt->mfc_bw_meter_leq = NULL;
1484 rt->mfc_bw_meter_geq = NULL;
1486 /* initialize pkt counters per src-grp */
1487 rt->mfc_pkt_cnt = 0;
1488 rt->mfc_byte_cnt = 0;
1489 rt->mfc_wrong_if = 0;
1490 timevalclear(&rt->mfc_last_assert);
1492 buf_ring_enqueue(rt->mfc_stall_ring, rte);
1494 /* Add RT to hashtable as it didn't exist before */
1495 LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
1496 } else {
1497 /* determine if queue has overflowed */
1498 if (buf_ring_full(rt->mfc_stall_ring)) {
1499 MRTSTAT_INC(mrts_upq_ovflw);
1500 non_fatal: free(rte, M_MRTABLE);
1501 m_freem(mb0);
1502 MRW_WUNLOCK();
1503 return (0);
1506 buf_ring_enqueue(rt->mfc_stall_ring, rte);
1509 rte->m = mb0;
1510 rte->ifp = ifp;
1512 MRW_WUNLOCK();
1514 return 0;
1518 * Clean up the cache entry if upcall is not serviced
1520 static void
1521 expire_upcalls(void *arg)
1523 u_long i;
1525 CURVNET_SET((struct vnet *) arg);
1527 /*This callout is always run with MRW_WLOCK taken. */
1529 for (i = 0; i < mfchashsize; i++) {
1530 struct mfc *rt, *nrt;
1532 if (V_nexpire[i] == 0)
1533 continue;
1535 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
1536 if (buf_ring_empty(rt->mfc_stall_ring))
1537 continue;
1539 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
1540 continue;
1542 MRTSTAT_INC(mrts_cache_cleanups);
1543 CTR3(KTR_IPMF, "%s: expire (%lx, %lx)", __func__,
1544 (u_long)ntohl(rt->mfc_origin.s_addr),
1545 (u_long)ntohl(rt->mfc_mcastgrp.s_addr));
1547 expire_mfc(rt);
1551 callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
1552 curvnet);
1554 CURVNET_RESTORE();
1558 * Packet forwarding routine once entry in the cache is made
1560 static int
1561 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1563 struct ip *ip = mtod(m, struct ip *);
1564 vifi_t vifi;
1565 int plen = ntohs(ip->ip_len);
1567 M_ASSERTMAPPED(m);
1568 MRW_LOCK_ASSERT();
1569 NET_EPOCH_ASSERT();
1572 * If xmt_vif is not -1, send on only the requested vif.
1574 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1576 if (xmt_vif < V_numvifs) {
1577 if (V_viftable[xmt_vif].v_flags & VIFF_REGISTER)
1578 pim_register_send(ip, V_viftable + xmt_vif, m, rt);
1579 else
1580 phyint_send(ip, V_viftable + xmt_vif, m);
1581 return 1;
1585 * Don't forward if it didn't arrive from the parent vif for its origin.
1587 vifi = rt->mfc_parent;
1588 if ((vifi >= V_numvifs) || (V_viftable[vifi].v_ifp != ifp)) {
1589 CTR4(KTR_IPMF, "%s: rx on wrong ifp %p (vifi %d, v_ifp %p)",
1590 __func__, ifp, (int)vifi, V_viftable[vifi].v_ifp);
1591 MRTSTAT_INC(mrts_wrong_if);
1592 ++rt->mfc_wrong_if;
1594 * If we are doing PIM assert processing, send a message
1595 * to the routing daemon.
1597 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1598 * can complete the SPT switch, regardless of the type
1599 * of the iif (broadcast media, GRE tunnel, etc).
1601 if (V_pim_assert_enabled && (vifi < V_numvifs) &&
1602 V_viftable[vifi].v_ifp) {
1603 if (ifp == V_multicast_register_if)
1604 PIMSTAT_INC(pims_rcv_registers_wrongiif);
1606 /* Get vifi for the incoming packet */
1607 for (vifi = 0; vifi < V_numvifs && V_viftable[vifi].v_ifp != ifp; vifi++)
1609 if (vifi >= V_numvifs)
1610 return 0; /* The iif is not found: ignore the packet. */
1612 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1613 return 0; /* WRONGVIF disabled: ignore the packet */
1615 if (ratecheck(&rt->mfc_last_assert, &pim_assert_interval)) {
1616 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1617 struct igmpmsg *im;
1618 int hlen = ip->ip_hl << 2;
1619 struct mbuf *mm = m_copym(m, 0, hlen, M_NOWAIT);
1621 if (mm && (!M_WRITABLE(mm) || mm->m_len < hlen))
1622 mm = m_pullup(mm, hlen);
1623 if (mm == NULL)
1624 return ENOBUFS;
1626 im = mtod(mm, struct igmpmsg *);
1627 im->im_msgtype = IGMPMSG_WRONGVIF;
1628 im->im_mbz = 0;
1629 im->im_vif = vifi;
1631 MRTSTAT_INC(mrts_upcalls);
1633 k_igmpsrc.sin_addr = im->im_src;
1634 if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
1635 CTR1(KTR_IPMF, "%s: socket queue full", __func__);
1636 MRTSTAT_INC(mrts_upq_sockfull);
1637 return ENOBUFS;
1641 return 0;
1644 /* If I sourced this packet, it counts as output, else it was input. */
1645 mtx_lock_spin(&V_viftable[vifi].v_spin);
1646 if (in_hosteq(ip->ip_src, V_viftable[vifi].v_lcl_addr)) {
1647 V_viftable[vifi].v_pkt_out++;
1648 V_viftable[vifi].v_bytes_out += plen;
1649 } else {
1650 V_viftable[vifi].v_pkt_in++;
1651 V_viftable[vifi].v_bytes_in += plen;
1653 mtx_unlock_spin(&V_viftable[vifi].v_spin);
1655 rt->mfc_pkt_cnt++;
1656 rt->mfc_byte_cnt += plen;
1659 * For each vif, decide if a copy of the packet should be forwarded.
1660 * Forward if:
1661 * - the ttl exceeds the vif's threshold
1662 * - there are group members downstream on interface
1664 for (vifi = 0; vifi < V_numvifs; vifi++)
1665 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1666 V_viftable[vifi].v_pkt_out++;
1667 V_viftable[vifi].v_bytes_out += plen;
1668 if (V_viftable[vifi].v_flags & VIFF_REGISTER)
1669 pim_register_send(ip, V_viftable + vifi, m, rt);
1670 else
1671 phyint_send(ip, V_viftable + vifi, m);
1675 * Perform upcall-related bw measuring.
1677 if ((rt->mfc_bw_meter_geq != NULL) || (rt->mfc_bw_meter_leq != NULL)) {
1678 struct bw_meter *x;
1679 struct timeval now;
1681 microtime(&now);
1682 /* Process meters for Greater-or-EQual case */
1683 for (x = rt->mfc_bw_meter_geq; x != NULL; x = x->bm_mfc_next)
1684 bw_meter_geq_receive_packet(x, plen, &now);
1686 /* Process meters for Lower-or-EQual case */
1687 for (x = rt->mfc_bw_meter_leq; x != NULL; x = x->bm_mfc_next) {
1689 * Record that a packet is received.
1690 * Spin lock has to be taken as callout context
1691 * (expire_bw_meter_leq) might modify these fields
1692 * as well
1694 mtx_lock_spin(&x->bm_spin);
1695 x->bm_measured.b_packets++;
1696 x->bm_measured.b_bytes += plen;
1697 mtx_unlock_spin(&x->bm_spin);
1701 return 0;
1705 * Check if a vif number is legal/ok. This is used by in_mcast.c.
1707 static int
1708 X_legal_vif_num(int vif)
1710 int ret;
1712 ret = 0;
1713 if (vif < 0)
1714 return (ret);
1716 MRW_RLOCK();
1717 if (vif < V_numvifs)
1718 ret = 1;
1719 MRW_RUNLOCK();
1721 return (ret);
1725 * Return the local address used by this vif
1727 static u_long
1728 X_ip_mcast_src(int vifi)
1730 in_addr_t addr;
1732 addr = INADDR_ANY;
1733 if (vifi < 0)
1734 return (addr);
1736 MRW_RLOCK();
1737 if (vifi < V_numvifs)
1738 addr = V_viftable[vifi].v_lcl_addr.s_addr;
1739 MRW_RUNLOCK();
1741 return (addr);
1744 static void
1745 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1747 struct mbuf *mb_copy;
1748 int hlen = ip->ip_hl << 2;
1750 MRW_LOCK_ASSERT();
1751 M_ASSERTMAPPED(m);
1754 * Make a new reference to the packet; make sure that
1755 * the IP header is actually copied, not just referenced,
1756 * so that ip_output() only scribbles on the copy.
1758 mb_copy = m_copypacket(m, M_NOWAIT);
1759 if (mb_copy && (!M_WRITABLE(mb_copy) || mb_copy->m_len < hlen))
1760 mb_copy = m_pullup(mb_copy, hlen);
1761 if (mb_copy == NULL)
1762 return;
1764 send_packet(vifp, mb_copy);
1767 static void
1768 send_packet(struct vif *vifp, struct mbuf *m)
1770 struct ip_moptions imo;
1771 int error __unused;
1773 MRW_LOCK_ASSERT();
1774 NET_EPOCH_ASSERT();
1776 imo.imo_multicast_ifp = vifp->v_ifp;
1777 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1778 imo.imo_multicast_loop = !!in_mcast_loop;
1779 imo.imo_multicast_vif = -1;
1780 STAILQ_INIT(&imo.imo_head);
1783 * Re-entrancy should not be a problem here, because
1784 * the packets that we send out and are looped back at us
1785 * should get rejected because they appear to come from
1786 * the loopback interface, thus preventing looping.
1788 error = ip_output(m, NULL, NULL, IP_FORWARDING, &imo, NULL);
1789 CTR3(KTR_IPMF, "%s: vif %td err %d", __func__,
1790 (ptrdiff_t)(vifp - V_viftable), error);
1794 * Stubs for old RSVP socket shim implementation.
1797 static int
1798 X_ip_rsvp_vif(struct socket *so __unused, struct sockopt *sopt __unused)
1801 return (EOPNOTSUPP);
1804 static void
1805 X_ip_rsvp_force_done(struct socket *so __unused)
1810 static int
1811 X_rsvp_input(struct mbuf **mp, int *offp, int proto)
1813 struct mbuf *m;
1815 m = *mp;
1816 *mp = NULL;
1817 if (!V_rsvp_on)
1818 m_freem(m);
1819 return (IPPROTO_DONE);
1823 * Code for bandwidth monitors
1827 * Define common interface for timeval-related methods
1829 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1830 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1831 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1833 static uint32_t
1834 compute_bw_meter_flags(struct bw_upcall *req)
1836 uint32_t flags = 0;
1838 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1839 flags |= BW_METER_UNIT_PACKETS;
1840 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1841 flags |= BW_METER_UNIT_BYTES;
1842 if (req->bu_flags & BW_UPCALL_GEQ)
1843 flags |= BW_METER_GEQ;
1844 if (req->bu_flags & BW_UPCALL_LEQ)
1845 flags |= BW_METER_LEQ;
1847 return flags;
1850 static void
1851 expire_bw_meter_leq(void *arg)
1853 struct bw_meter *x = arg;
1854 struct timeval now;
1856 * INFO:
1857 * callout is always executed with MRW_WLOCK taken
1860 CURVNET_SET((struct vnet *)x->arg);
1862 microtime(&now);
1865 * Test if we should deliver an upcall
1867 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
1868 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
1869 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
1870 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
1871 /* Prepare an upcall for delivery */
1872 bw_meter_prepare_upcall(x, &now);
1875 /* Send all upcalls that are pending delivery */
1876 taskqueue_enqueue(V_task_queue, &V_task);
1878 /* Reset counters */
1879 x->bm_start_time = now;
1880 /* Spin lock has to be taken as ip_forward context
1881 * might modify these fields as well
1883 mtx_lock_spin(&x->bm_spin);
1884 x->bm_measured.b_bytes = 0;
1885 x->bm_measured.b_packets = 0;
1886 mtx_unlock_spin(&x->bm_spin);
1888 callout_schedule(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time));
1890 CURVNET_RESTORE();
1894 * Add a bw_meter entry
1896 static int
1897 add_bw_upcall(struct bw_upcall *req)
1899 struct mfc *mfc;
1900 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
1901 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
1902 struct timeval now;
1903 struct bw_meter *x, **bwm_ptr;
1904 uint32_t flags;
1906 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
1907 return EOPNOTSUPP;
1909 /* Test if the flags are valid */
1910 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
1911 return EINVAL;
1912 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
1913 return EINVAL;
1914 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
1915 return EINVAL;
1917 /* Test if the threshold time interval is valid */
1918 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
1919 return EINVAL;
1921 flags = compute_bw_meter_flags(req);
1924 * Find if we have already same bw_meter entry
1926 MRW_WLOCK();
1927 mfc = mfc_find(&req->bu_src, &req->bu_dst);
1928 if (mfc == NULL) {
1929 MRW_WUNLOCK();
1930 return EADDRNOTAVAIL;
1933 /* Choose an appropriate bw_meter list */
1934 if (req->bu_flags & BW_UPCALL_GEQ)
1935 bwm_ptr = &mfc->mfc_bw_meter_geq;
1936 else
1937 bwm_ptr = &mfc->mfc_bw_meter_leq;
1939 for (x = *bwm_ptr; x != NULL; x = x->bm_mfc_next) {
1940 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
1941 &req->bu_threshold.b_time, ==))
1942 && (x->bm_threshold.b_packets
1943 == req->bu_threshold.b_packets)
1944 && (x->bm_threshold.b_bytes
1945 == req->bu_threshold.b_bytes)
1946 && (x->bm_flags & BW_METER_USER_FLAGS)
1947 == flags) {
1948 MRW_WUNLOCK();
1949 return 0; /* XXX Already installed */
1953 /* Allocate the new bw_meter entry */
1954 x = malloc(sizeof(*x), M_BWMETER, M_ZERO | M_NOWAIT);
1955 if (x == NULL) {
1956 MRW_WUNLOCK();
1957 return ENOBUFS;
1960 /* Set the new bw_meter entry */
1961 x->bm_threshold.b_time = req->bu_threshold.b_time;
1962 microtime(&now);
1963 x->bm_start_time = now;
1964 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
1965 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
1966 x->bm_measured.b_packets = 0;
1967 x->bm_measured.b_bytes = 0;
1968 x->bm_flags = flags;
1969 x->bm_time_next = NULL;
1970 x->bm_mfc = mfc;
1971 x->arg = curvnet;
1972 sprintf(x->bm_spin_name, "BM spin %p", x);
1973 mtx_init(&x->bm_spin, x->bm_spin_name, NULL, MTX_SPIN);
1975 /* For LEQ case create periodic callout */
1976 if (req->bu_flags & BW_UPCALL_LEQ) {
1977 callout_init_rw(&x->bm_meter_callout, &mrouter_lock, CALLOUT_SHAREDLOCK);
1978 callout_reset(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time),
1979 expire_bw_meter_leq, x);
1982 /* Add the new bw_meter entry to the front of entries for this MFC */
1983 x->bm_mfc_next = *bwm_ptr;
1984 *bwm_ptr = x;
1986 MRW_WUNLOCK();
1988 return 0;
1991 static void
1992 free_bw_list(struct bw_meter *list)
1994 while (list != NULL) {
1995 struct bw_meter *x = list;
1997 /* MRW_WLOCK must be held here */
1998 if (x->bm_flags & BW_METER_LEQ) {
1999 callout_drain(&x->bm_meter_callout);
2000 mtx_destroy(&x->bm_spin);
2003 list = list->bm_mfc_next;
2004 free(x, M_BWMETER);
2009 * Delete one or multiple bw_meter entries
2011 static int
2012 del_bw_upcall(struct bw_upcall *req)
2014 struct mfc *mfc;
2015 struct bw_meter *x, **bwm_ptr;
2017 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
2018 return EOPNOTSUPP;
2020 MRW_WLOCK();
2022 /* Find the corresponding MFC entry */
2023 mfc = mfc_find(&req->bu_src, &req->bu_dst);
2024 if (mfc == NULL) {
2025 MRW_WUNLOCK();
2026 return EADDRNOTAVAIL;
2027 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2029 * Delete all bw_meter entries for this mfc
2031 struct bw_meter *list;
2033 /* Free LEQ list */
2034 list = mfc->mfc_bw_meter_leq;
2035 mfc->mfc_bw_meter_leq = NULL;
2036 free_bw_list(list);
2038 /* Free GEQ list */
2039 list = mfc->mfc_bw_meter_geq;
2040 mfc->mfc_bw_meter_geq = NULL;
2041 free_bw_list(list);
2042 MRW_WUNLOCK();
2043 return 0;
2044 } else { /* Delete a single bw_meter entry */
2045 struct bw_meter *prev;
2046 uint32_t flags = 0;
2048 flags = compute_bw_meter_flags(req);
2050 /* Choose an appropriate bw_meter list */
2051 if (req->bu_flags & BW_UPCALL_GEQ)
2052 bwm_ptr = &mfc->mfc_bw_meter_geq;
2053 else
2054 bwm_ptr = &mfc->mfc_bw_meter_leq;
2056 /* Find the bw_meter entry to delete */
2057 for (prev = NULL, x = *bwm_ptr; x != NULL;
2058 prev = x, x = x->bm_mfc_next) {
2059 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, &req->bu_threshold.b_time, ==)) &&
2060 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2061 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2062 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2063 break;
2065 if (x != NULL) { /* Delete entry from the list for this MFC */
2066 if (prev != NULL)
2067 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2068 else
2069 *bwm_ptr = x->bm_mfc_next;/* new head of list */
2071 if (req->bu_flags & BW_UPCALL_LEQ)
2072 callout_stop(&x->bm_meter_callout);
2074 MRW_WUNLOCK();
2075 /* Free the bw_meter entry */
2076 free(x, M_BWMETER);
2077 return 0;
2078 } else {
2079 MRW_WUNLOCK();
2080 return EINVAL;
2083 __assert_unreachable();
2087 * Perform bandwidth measurement processing that may result in an upcall
2089 static void
2090 bw_meter_geq_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2092 struct timeval delta;
2094 MRW_LOCK_ASSERT();
2096 delta = *nowp;
2097 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2100 * Processing for ">=" type of bw_meter entry.
2101 * bm_spin does not have to be hold here as in GEQ
2102 * case this is the only context accessing bm_measured.
2104 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2105 /* Reset the bw_meter entry */
2106 x->bm_start_time = *nowp;
2107 x->bm_measured.b_packets = 0;
2108 x->bm_measured.b_bytes = 0;
2109 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2112 /* Record that a packet is received */
2113 x->bm_measured.b_packets++;
2114 x->bm_measured.b_bytes += plen;
2117 * Test if we should deliver an upcall
2119 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2120 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2121 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2122 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2123 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2124 /* Prepare an upcall for delivery */
2125 bw_meter_prepare_upcall(x, nowp);
2126 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2132 * Prepare a bandwidth-related upcall
2134 static void
2135 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2137 struct timeval delta;
2138 struct bw_upcall *u;
2140 MRW_LOCK_ASSERT();
2143 * Compute the measured time interval
2145 delta = *nowp;
2146 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2149 * Set the bw_upcall entry
2151 u = malloc(sizeof(struct bw_upcall), M_MRTABLE, M_NOWAIT | M_ZERO);
2152 if (!u) {
2153 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot allocate entry\n");
2154 return;
2156 u->bu_src = x->bm_mfc->mfc_origin;
2157 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2158 u->bu_threshold.b_time = x->bm_threshold.b_time;
2159 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2160 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2161 u->bu_measured.b_time = delta;
2162 u->bu_measured.b_packets = x->bm_measured.b_packets;
2163 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2164 u->bu_flags = 0;
2165 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2166 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2167 if (x->bm_flags & BW_METER_UNIT_BYTES)
2168 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2169 if (x->bm_flags & BW_METER_GEQ)
2170 u->bu_flags |= BW_UPCALL_GEQ;
2171 if (x->bm_flags & BW_METER_LEQ)
2172 u->bu_flags |= BW_UPCALL_LEQ;
2174 if (buf_ring_enqueue(V_bw_upcalls_ring, u))
2175 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot enqueue upcall\n");
2176 if (buf_ring_count(V_bw_upcalls_ring) > (BW_UPCALLS_MAX / 2)) {
2177 taskqueue_enqueue(V_task_queue, &V_task);
2181 * Send the pending bandwidth-related upcalls
2183 static void
2184 bw_upcalls_send(void)
2186 struct mbuf *m;
2187 int len = 0;
2188 struct bw_upcall *bu;
2189 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2190 static struct igmpmsg igmpmsg = {
2191 0, /* unused1 */
2192 0, /* unused2 */
2193 IGMPMSG_BW_UPCALL,/* im_msgtype */
2194 0, /* im_mbz */
2195 0, /* im_vif */
2196 0, /* unused3 */
2197 { 0 }, /* im_src */
2198 { 0 } /* im_dst */
2201 MRW_LOCK_ASSERT();
2203 if (buf_ring_empty(V_bw_upcalls_ring))
2204 return;
2207 * Allocate a new mbuf, initialize it with the header and
2208 * the payload for the pending calls.
2210 m = m_gethdr(M_NOWAIT, MT_DATA);
2211 if (m == NULL) {
2212 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2213 return;
2216 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2217 len += sizeof(struct igmpmsg);
2218 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
2219 m_copyback(m, len, sizeof(struct bw_upcall), (caddr_t)bu);
2220 len += sizeof(struct bw_upcall);
2221 free(bu, M_MRTABLE);
2225 * Send the upcalls
2226 * XXX do we need to set the address in k_igmpsrc ?
2228 MRTSTAT_INC(mrts_upcalls);
2229 if (socket_send(V_ip_mrouter, m, &k_igmpsrc) < 0) {
2230 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2231 MRTSTAT_INC(mrts_upq_sockfull);
2236 * A periodic function for sending all upcalls that are pending delivery
2238 static void
2239 expire_bw_upcalls_send(void *arg)
2241 CURVNET_SET((struct vnet *) arg);
2243 /* This callout is run with MRW_RLOCK taken */
2245 bw_upcalls_send();
2247 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
2248 curvnet);
2249 CURVNET_RESTORE();
2253 * End of bandwidth monitoring code
2257 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2260 static int
2261 pim_register_send(struct ip *ip, struct vif *vifp, struct mbuf *m,
2262 struct mfc *rt)
2264 struct mbuf *mb_copy, *mm;
2267 * Do not send IGMP_WHOLEPKT notifications to userland, if the
2268 * rendezvous point was unspecified, and we were told not to.
2270 if (pim_squelch_wholepkt != 0 && (V_mrt_api_config & MRT_MFC_RP) &&
2271 in_nullhost(rt->mfc_rp))
2272 return 0;
2274 mb_copy = pim_register_prepare(ip, m);
2275 if (mb_copy == NULL)
2276 return ENOBUFS;
2279 * Send all the fragments. Note that the mbuf for each fragment
2280 * is freed by the sending machinery.
2282 for (mm = mb_copy; mm; mm = mb_copy) {
2283 mb_copy = mm->m_nextpkt;
2284 mm->m_nextpkt = 0;
2285 mm = m_pullup(mm, sizeof(struct ip));
2286 if (mm != NULL) {
2287 ip = mtod(mm, struct ip *);
2288 if ((V_mrt_api_config & MRT_MFC_RP) && !in_nullhost(rt->mfc_rp)) {
2289 pim_register_send_rp(ip, vifp, mm, rt);
2290 } else {
2291 pim_register_send_upcall(ip, vifp, mm, rt);
2296 return 0;
2300 * Return a copy of the data packet that is ready for PIM Register
2301 * encapsulation.
2302 * XXX: Note that in the returned copy the IP header is a valid one.
2304 static struct mbuf *
2305 pim_register_prepare(struct ip *ip, struct mbuf *m)
2307 struct mbuf *mb_copy = NULL;
2308 int mtu;
2310 /* Take care of delayed checksums */
2311 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2312 in_delayed_cksum(m);
2313 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2317 * Copy the old packet & pullup its IP header into the
2318 * new mbuf so we can modify it.
2320 mb_copy = m_copypacket(m, M_NOWAIT);
2321 if (mb_copy == NULL)
2322 return NULL;
2323 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2324 if (mb_copy == NULL)
2325 return NULL;
2327 /* take care of the TTL */
2328 ip = mtod(mb_copy, struct ip *);
2329 --ip->ip_ttl;
2331 /* Compute the MTU after the PIM Register encapsulation */
2332 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2334 if (ntohs(ip->ip_len) <= mtu) {
2335 /* Turn the IP header into a valid one */
2336 ip->ip_sum = 0;
2337 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2338 } else {
2339 /* Fragment the packet */
2340 mb_copy->m_pkthdr.csum_flags |= CSUM_IP;
2341 if (ip_fragment(ip, &mb_copy, mtu, 0) != 0) {
2342 m_freem(mb_copy);
2343 return NULL;
2346 return mb_copy;
2350 * Send an upcall with the data packet to the user-level process.
2352 static int
2353 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2354 struct mbuf *mb_copy, struct mfc *rt)
2356 struct mbuf *mb_first;
2357 int len = ntohs(ip->ip_len);
2358 struct igmpmsg *im;
2359 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2361 MRW_LOCK_ASSERT();
2364 * Add a new mbuf with an upcall header
2366 mb_first = m_gethdr(M_NOWAIT, MT_DATA);
2367 if (mb_first == NULL) {
2368 m_freem(mb_copy);
2369 return ENOBUFS;
2371 mb_first->m_data += max_linkhdr;
2372 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2373 mb_first->m_len = sizeof(struct igmpmsg);
2374 mb_first->m_next = mb_copy;
2376 /* Send message to routing daemon */
2377 im = mtod(mb_first, struct igmpmsg *);
2378 im->im_msgtype = IGMPMSG_WHOLEPKT;
2379 im->im_mbz = 0;
2380 im->im_vif = vifp - V_viftable;
2381 im->im_src = ip->ip_src;
2382 im->im_dst = ip->ip_dst;
2384 k_igmpsrc.sin_addr = ip->ip_src;
2386 MRTSTAT_INC(mrts_upcalls);
2388 if (socket_send(V_ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2389 CTR1(KTR_IPMF, "%s: socket queue full", __func__);
2390 MRTSTAT_INC(mrts_upq_sockfull);
2391 return ENOBUFS;
2394 /* Keep statistics */
2395 PIMSTAT_INC(pims_snd_registers_msgs);
2396 PIMSTAT_ADD(pims_snd_registers_bytes, len);
2398 return 0;
2402 * Encapsulate the data packet in PIM Register message and send it to the RP.
2404 static int
2405 pim_register_send_rp(struct ip *ip, struct vif *vifp, struct mbuf *mb_copy,
2406 struct mfc *rt)
2408 struct mbuf *mb_first;
2409 struct ip *ip_outer;
2410 struct pim_encap_pimhdr *pimhdr;
2411 int len = ntohs(ip->ip_len);
2412 vifi_t vifi = rt->mfc_parent;
2414 MRW_LOCK_ASSERT();
2416 if ((vifi >= V_numvifs) || in_nullhost(V_viftable[vifi].v_lcl_addr)) {
2417 m_freem(mb_copy);
2418 return EADDRNOTAVAIL; /* The iif vif is invalid */
2422 * Add a new mbuf with the encapsulating header
2424 mb_first = m_gethdr(M_NOWAIT, MT_DATA);
2425 if (mb_first == NULL) {
2426 m_freem(mb_copy);
2427 return ENOBUFS;
2429 mb_first->m_data += max_linkhdr;
2430 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2431 mb_first->m_next = mb_copy;
2433 mb_first->m_pkthdr.len = len + mb_first->m_len;
2436 * Fill in the encapsulating IP and PIM header
2438 ip_outer = mtod(mb_first, struct ip *);
2439 *ip_outer = pim_encap_iphdr;
2440 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
2441 sizeof(pim_encap_pimhdr));
2442 ip_outer->ip_src = V_viftable[vifi].v_lcl_addr;
2443 ip_outer->ip_dst = rt->mfc_rp;
2445 * Copy the inner header TOS to the outer header, and take care of the
2446 * IP_DF bit.
2448 ip_outer->ip_tos = ip->ip_tos;
2449 if (ip->ip_off & htons(IP_DF))
2450 ip_outer->ip_off |= htons(IP_DF);
2451 ip_fillid(ip_outer);
2452 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2453 + sizeof(pim_encap_iphdr));
2454 *pimhdr = pim_encap_pimhdr;
2455 /* If the iif crosses a border, set the Border-bit */
2456 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & V_mrt_api_config)
2457 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2459 mb_first->m_data += sizeof(pim_encap_iphdr);
2460 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2461 mb_first->m_data -= sizeof(pim_encap_iphdr);
2463 send_packet(vifp, mb_first);
2465 /* Keep statistics */
2466 PIMSTAT_INC(pims_snd_registers_msgs);
2467 PIMSTAT_ADD(pims_snd_registers_bytes, len);
2469 return 0;
2473 * pim_encapcheck() is called by the encap4_input() path at runtime to
2474 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2475 * into the kernel.
2477 static int
2478 pim_encapcheck(const struct mbuf *m __unused, int off __unused,
2479 int proto __unused, void *arg __unused)
2482 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2483 return (8); /* claim the datagram. */
2487 * PIM-SMv2 and PIM-DM messages processing.
2488 * Receives and verifies the PIM control messages, and passes them
2489 * up to the listening socket, using rip_input().
2490 * The only message with special processing is the PIM_REGISTER message
2491 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2492 * is passed to if_simloop().
2494 static int
2495 pim_input(struct mbuf *m, int off, int proto, void *arg __unused)
2497 struct ip *ip = mtod(m, struct ip *);
2498 struct pim *pim;
2499 int iphlen = off;
2500 int minlen;
2501 int datalen = ntohs(ip->ip_len) - iphlen;
2502 int ip_tos;
2504 /* Keep statistics */
2505 PIMSTAT_INC(pims_rcv_total_msgs);
2506 PIMSTAT_ADD(pims_rcv_total_bytes, datalen);
2509 * Validate lengths
2511 if (datalen < PIM_MINLEN) {
2512 PIMSTAT_INC(pims_rcv_tooshort);
2513 CTR3(KTR_IPMF, "%s: short packet (%d) from 0x%08x",
2514 __func__, datalen, ntohl(ip->ip_src.s_addr));
2515 m_freem(m);
2516 return (IPPROTO_DONE);
2520 * If the packet is at least as big as a REGISTER, go agead
2521 * and grab the PIM REGISTER header size, to avoid another
2522 * possible m_pullup() later.
2524 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
2525 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2527 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2529 * Get the IP and PIM headers in contiguous memory, and
2530 * possibly the PIM REGISTER header.
2532 if (m->m_len < minlen && (m = m_pullup(m, minlen)) == NULL) {
2533 CTR1(KTR_IPMF, "%s: m_pullup() failed", __func__);
2534 return (IPPROTO_DONE);
2537 /* m_pullup() may have given us a new mbuf so reset ip. */
2538 ip = mtod(m, struct ip *);
2539 ip_tos = ip->ip_tos;
2541 /* adjust mbuf to point to the PIM header */
2542 m->m_data += iphlen;
2543 m->m_len -= iphlen;
2544 pim = mtod(m, struct pim *);
2547 * Validate checksum. If PIM REGISTER, exclude the data packet.
2549 * XXX: some older PIMv2 implementations don't make this distinction,
2550 * so for compatibility reason perform the checksum over part of the
2551 * message, and if error, then over the whole message.
2553 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2554 /* do nothing, checksum okay */
2555 } else if (in_cksum(m, datalen)) {
2556 PIMSTAT_INC(pims_rcv_badsum);
2557 CTR1(KTR_IPMF, "%s: invalid checksum", __func__);
2558 m_freem(m);
2559 return (IPPROTO_DONE);
2562 /* PIM version check */
2563 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2564 PIMSTAT_INC(pims_rcv_badversion);
2565 CTR3(KTR_IPMF, "%s: bad version %d expect %d", __func__,
2566 (int)PIM_VT_V(pim->pim_vt), PIM_VERSION);
2567 m_freem(m);
2568 return (IPPROTO_DONE);
2571 /* restore mbuf back to the outer IP */
2572 m->m_data -= iphlen;
2573 m->m_len += iphlen;
2575 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2577 * Since this is a REGISTER, we'll make a copy of the register
2578 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2579 * routing daemon.
2581 struct sockaddr_in dst = { sizeof(dst), AF_INET };
2582 struct mbuf *mcp;
2583 struct ip *encap_ip;
2584 u_int32_t *reghdr;
2585 struct ifnet *vifp;
2587 MRW_RLOCK();
2588 if ((V_reg_vif_num >= V_numvifs) || (V_reg_vif_num == VIFI_INVALID)) {
2589 MRW_RUNLOCK();
2590 CTR2(KTR_IPMF, "%s: register vif not set: %d", __func__,
2591 (int)V_reg_vif_num);
2592 m_freem(m);
2593 return (IPPROTO_DONE);
2595 /* XXX need refcnt? */
2596 vifp = V_viftable[V_reg_vif_num].v_ifp;
2597 MRW_RUNLOCK();
2600 * Validate length
2602 if (datalen < PIM_REG_MINLEN) {
2603 PIMSTAT_INC(pims_rcv_tooshort);
2604 PIMSTAT_INC(pims_rcv_badregisters);
2605 CTR1(KTR_IPMF, "%s: register packet size too small", __func__);
2606 m_freem(m);
2607 return (IPPROTO_DONE);
2610 reghdr = (u_int32_t *)(pim + 1);
2611 encap_ip = (struct ip *)(reghdr + 1);
2613 CTR3(KTR_IPMF, "%s: register: encap ip src 0x%08x len %d",
2614 __func__, ntohl(encap_ip->ip_src.s_addr),
2615 ntohs(encap_ip->ip_len));
2617 /* verify the version number of the inner packet */
2618 if (encap_ip->ip_v != IPVERSION) {
2619 PIMSTAT_INC(pims_rcv_badregisters);
2620 CTR1(KTR_IPMF, "%s: bad encap ip version", __func__);
2621 m_freem(m);
2622 return (IPPROTO_DONE);
2625 /* verify the inner packet is destined to a mcast group */
2626 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2627 PIMSTAT_INC(pims_rcv_badregisters);
2628 CTR2(KTR_IPMF, "%s: bad encap ip dest 0x%08x", __func__,
2629 ntohl(encap_ip->ip_dst.s_addr));
2630 m_freem(m);
2631 return (IPPROTO_DONE);
2634 /* If a NULL_REGISTER, pass it to the daemon */
2635 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2636 goto pim_input_to_daemon;
2639 * Copy the TOS from the outer IP header to the inner IP header.
2641 if (encap_ip->ip_tos != ip_tos) {
2642 /* Outer TOS -> inner TOS */
2643 encap_ip->ip_tos = ip_tos;
2644 /* Recompute the inner header checksum. Sigh... */
2646 /* adjust mbuf to point to the inner IP header */
2647 m->m_data += (iphlen + PIM_MINLEN);
2648 m->m_len -= (iphlen + PIM_MINLEN);
2650 encap_ip->ip_sum = 0;
2651 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2653 /* restore mbuf to point back to the outer IP header */
2654 m->m_data -= (iphlen + PIM_MINLEN);
2655 m->m_len += (iphlen + PIM_MINLEN);
2659 * Decapsulate the inner IP packet and loopback to forward it
2660 * as a normal multicast packet. Also, make a copy of the
2661 * outer_iphdr + pimhdr + reghdr + encap_iphdr
2662 * to pass to the daemon later, so it can take the appropriate
2663 * actions (e.g., send back PIM_REGISTER_STOP).
2664 * XXX: here m->m_data points to the outer IP header.
2666 mcp = m_copym(m, 0, iphlen + PIM_REG_MINLEN, M_NOWAIT);
2667 if (mcp == NULL) {
2668 CTR1(KTR_IPMF, "%s: m_copym() failed", __func__);
2669 m_freem(m);
2670 return (IPPROTO_DONE);
2673 /* Keep statistics */
2674 /* XXX: registers_bytes include only the encap. mcast pkt */
2675 PIMSTAT_INC(pims_rcv_registers_msgs);
2676 PIMSTAT_ADD(pims_rcv_registers_bytes, ntohs(encap_ip->ip_len));
2679 * forward the inner ip packet; point m_data at the inner ip.
2681 m_adj(m, iphlen + PIM_MINLEN);
2683 CTR4(KTR_IPMF,
2684 "%s: forward decap'd REGISTER: src %lx dst %lx vif %d",
2685 __func__,
2686 (u_long)ntohl(encap_ip->ip_src.s_addr),
2687 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2688 (int)V_reg_vif_num);
2690 /* NB: vifp was collected above; can it change on us? */
2691 if_simloop(vifp, m, dst.sin_family, 0);
2693 /* prepare the register head to send to the mrouting daemon */
2694 m = mcp;
2697 pim_input_to_daemon:
2699 * Pass the PIM message up to the daemon; if it is a Register message,
2700 * pass the 'head' only up to the daemon. This includes the
2701 * outer IP header, PIM header, PIM-Register header and the
2702 * inner IP header.
2703 * XXX: the outer IP header pkt size of a Register is not adjust to
2704 * reflect the fact that the inner multicast data is truncated.
2706 return (rip_input(&m, &off, proto));
2709 static int
2710 sysctl_mfctable(SYSCTL_HANDLER_ARGS)
2712 struct mfc *rt;
2713 int error, i;
2715 if (req->newptr)
2716 return (EPERM);
2717 if (V_mfchashtbl == NULL) /* XXX unlocked */
2718 return (0);
2719 error = sysctl_wire_old_buffer(req, 0);
2720 if (error)
2721 return (error);
2723 MRW_RLOCK();
2724 for (i = 0; i < mfchashsize; i++) {
2725 LIST_FOREACH(rt, &V_mfchashtbl[i], mfc_hash) {
2726 error = SYSCTL_OUT(req, rt, sizeof(struct mfc));
2727 if (error)
2728 goto out_locked;
2731 out_locked:
2732 MRW_RUNLOCK();
2733 return (error);
2736 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mfctable,
2737 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mfctable,
2738 "IPv4 Multicast Forwarding Table "
2739 "(struct *mfc[mfchashsize], netinet/ip_mroute.h)");
2741 static int
2742 sysctl_viflist(SYSCTL_HANDLER_ARGS)
2744 int error, i;
2746 if (req->newptr)
2747 return (EPERM);
2748 if (V_viftable == NULL) /* XXX unlocked */
2749 return (0);
2750 error = sysctl_wire_old_buffer(req, MROUTE_VIF_SYSCTL_LEN * MAXVIFS);
2751 if (error)
2752 return (error);
2754 MRW_RLOCK();
2755 /* Copy out user-visible portion of vif entry. */
2756 for (i = 0; i < MAXVIFS; i++) {
2757 error = SYSCTL_OUT(req, &V_viftable[i], MROUTE_VIF_SYSCTL_LEN);
2758 if (error)
2759 break;
2761 MRW_RUNLOCK();
2762 return (error);
2765 SYSCTL_PROC(_net_inet_ip, OID_AUTO, viftable,
2766 CTLTYPE_OPAQUE | CTLFLAG_VNET | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2767 sysctl_viflist, "S,vif[MAXVIFS]",
2768 "IPv4 Multicast Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
2770 static void
2771 vnet_mroute_init(const void *unused __unused)
2774 V_nexpire = malloc(mfchashsize, M_MRTABLE, M_WAITOK|M_ZERO);
2776 V_viftable = mallocarray(MAXVIFS, sizeof(*V_viftable),
2777 M_MRTABLE, M_WAITOK|M_ZERO);
2779 callout_init_rw(&V_expire_upcalls_ch, &mrouter_lock, 0);
2780 callout_init_rw(&V_bw_upcalls_ch, &mrouter_lock, 0);
2782 /* Prepare taskqueue */
2783 V_task_queue = taskqueue_create_fast("ip_mroute_tskq", M_NOWAIT,
2784 taskqueue_thread_enqueue, &V_task_queue);
2785 taskqueue_start_threads(&V_task_queue, 1, PI_NET, "ip_mroute_tskq task");
2788 VNET_SYSINIT(vnet_mroute_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mroute_init,
2789 NULL);
2791 static void
2792 vnet_mroute_uninit(const void *unused __unused)
2795 /* Taskqueue should be cancelled and drained before freeing */
2796 taskqueue_free(V_task_queue);
2798 free(V_viftable, M_MRTABLE);
2799 free(V_nexpire, M_MRTABLE);
2800 V_nexpire = NULL;
2803 VNET_SYSUNINIT(vnet_mroute_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE,
2804 vnet_mroute_uninit, NULL);
2806 static int
2807 ip_mroute_modevent(module_t mod, int type, void *unused)
2810 switch (type) {
2811 case MOD_LOAD:
2812 MRW_LOCK_INIT();
2814 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2815 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
2816 if (if_detach_event_tag == NULL) {
2817 printf("ip_mroute: unable to register "
2818 "ifnet_departure_event handler\n");
2819 MRW_LOCK_DESTROY();
2820 return (EINVAL);
2823 if (!powerof2(mfchashsize)) {
2824 printf("WARNING: %s not a power of 2; using default\n",
2825 "net.inet.ip.mfchashsize");
2826 mfchashsize = MFCHASHSIZE;
2829 pim_encap_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
2831 ip_mcast_src = X_ip_mcast_src;
2832 ip_mforward = X_ip_mforward;
2833 ip_mrouter_done = X_ip_mrouter_done;
2834 ip_mrouter_get = X_ip_mrouter_get;
2835 ip_mrouter_set = X_ip_mrouter_set;
2837 ip_rsvp_force_done = X_ip_rsvp_force_done;
2838 ip_rsvp_vif = X_ip_rsvp_vif;
2840 legal_vif_num = X_legal_vif_num;
2841 mrt_ioctl = X_mrt_ioctl;
2842 rsvp_input_p = X_rsvp_input;
2843 break;
2845 case MOD_UNLOAD:
2847 * Typically module unload happens after the user-level
2848 * process has shutdown the kernel services (the check
2849 * below insures someone can't just yank the module out
2850 * from under a running process). But if the module is
2851 * just loaded and then unloaded w/o starting up a user
2852 * process we still need to cleanup.
2854 MRW_WLOCK();
2855 if (ip_mrouter_cnt != 0) {
2856 MRW_WUNLOCK();
2857 return (EINVAL);
2859 ip_mrouter_unloading = 1;
2860 MRW_WUNLOCK();
2862 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2864 if (pim_encap_cookie) {
2865 ip_encap_detach(pim_encap_cookie);
2866 pim_encap_cookie = NULL;
2869 ip_mcast_src = NULL;
2870 ip_mforward = NULL;
2871 ip_mrouter_done = NULL;
2872 ip_mrouter_get = NULL;
2873 ip_mrouter_set = NULL;
2875 ip_rsvp_force_done = NULL;
2876 ip_rsvp_vif = NULL;
2878 legal_vif_num = NULL;
2879 mrt_ioctl = NULL;
2880 rsvp_input_p = NULL;
2882 MRW_LOCK_DESTROY();
2883 break;
2885 default:
2886 return EOPNOTSUPP;
2888 return 0;
2891 static moduledata_t ip_mroutemod = {
2892 "ip_mroute",
2893 ip_mroute_modevent,
2897 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);