2 * Copyright (c) 1989 Stephen Deering
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
37 * IP multicast forwarding procedures
39 * Written by David Waitzman, BBN Labs, August 1988.
40 * Modified by Steve Deering, Stanford, February 1989.
41 * Modified by Mark J. Steiglitz, Stanford, May, 1991
42 * Modified by Van Jacobson, LBL, January 1993
43 * Modified by Ajit Thyagarajan, PARC, August 1993
44 * Modified by Bill Fenner, PARC, April 1995
45 * Modified by Ahmed Helmy, SGI, June 1996
46 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
47 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
48 * Modified by Hitoshi Asaeda, WIDE, August 2000
49 * Modified by Pavlin Radoslavov, ICSI, October 2002
51 * MROUTING Revision: 3.5
52 * and PIM-SMv2 and PIM-DM support, advanced API support,
53 * bandwidth metering and signaling
59 #include "opt_mrouting.h"
63 #include <sys/param.h>
64 #include <sys/kernel.h>
66 #include <sys/malloc.h>
68 #include <sys/module.h>
70 #include <sys/protosw.h>
71 #include <sys/signalvar.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 #include <sys/syslog.h>
78 #include <sys/systm.h>
81 #include <net/netisr.h>
82 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/igmp.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_encap.h>
89 #include <netinet/ip_mroute.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/ip_options.h>
92 #include <netinet/pim.h>
93 #include <netinet/pim_var.h>
94 #include <netinet/udp.h>
95 #include <machine/in_cksum.h>
97 #include <security/mac/mac_framework.h>
100 * Control debugging code for rsvp and multicast routing code.
101 * Can only set them with the debugger.
103 static u_int rsvpdebug; /* non-zero enables debugging */
105 static u_int mrtdebug; /* any set of the flags below */
106 #define DEBUG_MFC 0x02
107 #define DEBUG_FORWARD 0x04
108 #define DEBUG_EXPIRE 0x08
109 #define DEBUG_XMIT 0x10
110 #define DEBUG_PIM 0x20
112 #define VIFI_INVALID ((vifi_t) -1)
114 #define M_HASCL(m) ((m)->m_flags & M_EXT)
116 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
119 * Locking. We use two locks: one for the virtual interface table and
120 * one for the forwarding table. These locks may be nested in which case
121 * the VIF lock must always be taken first. Note that each lock is used
122 * to cover not only the specific data structure but also related data
123 * structures. It may be better to add more fine-grained locking later;
124 * it's not clear how performance-critical this code is.
126 * XXX: This module could particularly benefit from being cleaned
127 * up to use the <sys/queue.h> macros.
131 static struct mrtstat mrtstat;
132 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
134 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
136 static struct mfc *mfctable[MFCTBLSIZ];
137 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
138 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
139 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
141 static struct mtx mfc_mtx;
142 #define MFC_LOCK() mtx_lock(&mfc_mtx)
143 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
144 #define MFC_LOCK_ASSERT() do { \
145 mtx_assert(&mfc_mtx, MA_OWNED); \
146 NET_ASSERT_GIANT(); \
148 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
149 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
151 static struct vif viftable[MAXVIFS];
152 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
153 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
154 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
156 static struct mtx vif_mtx;
157 #define VIF_LOCK() mtx_lock(&vif_mtx)
158 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
159 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
160 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
161 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
163 static u_char nexpire[MFCTBLSIZ];
165 static eventhandler_tag if_detach_event_tag = NULL;
167 static struct callout expire_upcalls_ch;
169 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
170 #define UPCALL_EXPIRE 6 /* number of timeouts */
175 * Bandwidth meter variables and constants
177 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
179 * Pending timeouts are stored in a hash table, the key being the
180 * expiration time. Periodically, the entries are analysed and processed.
182 #define BW_METER_BUCKETS 1024
183 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
184 static struct callout bw_meter_ch;
185 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
188 * Pending upcalls are stored in a vector which is flushed when
189 * full, or periodically
191 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
192 static u_int bw_upcalls_n; /* # of pending upcalls */
193 static struct callout bw_upcalls_ch;
194 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
196 static struct pimstat pimstat;
198 SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
199 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
201 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
203 extern struct domain inetdomain;
204 struct protosw in_pim_protosw = {
206 .pr_domain = &inetdomain,
207 .pr_protocol = IPPROTO_PIM,
208 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
209 .pr_input = pim_input,
210 .pr_output = (pr_output_t*)rip_output,
211 .pr_ctloutput = rip_ctloutput,
212 .pr_usrreqs = &rip_usrreqs
214 static const struct encaptab *pim_encap_cookie;
215 static int pim_encapcheck(const struct mbuf *, int, int, void *);
218 * Note: the PIM Register encapsulation adds the following in front of a
221 * struct pim_encap_hdr {
223 * struct pim_encap_pimhdr pim;
228 struct pim_encap_pimhdr {
233 static struct ip pim_encap_iphdr = {
234 #if BYTE_ORDER == LITTLE_ENDIAN
235 sizeof(struct ip) >> 2,
239 sizeof(struct ip) >> 2,
242 sizeof(struct ip), /* total length */
250 static struct pim_encap_pimhdr pim_encap_pimhdr = {
252 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
259 static struct ifnet multicast_register_if;
260 static vifi_t reg_vif_num = VIFI_INVALID;
265 static vifi_t numvifs;
267 static u_long X_ip_mcast_src(int vifi);
268 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
269 struct mbuf *m, struct ip_moptions *imo);
270 static int X_ip_mrouter_done(void);
271 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
272 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
273 static int X_legal_vif_num(int vif);
274 static int X_mrt_ioctl(int cmd, caddr_t data);
276 static int get_sg_cnt(struct sioc_sg_req *);
277 static int get_vif_cnt(struct sioc_vif_req *);
278 static void if_detached_event(void *arg __unused, struct ifnet *);
279 static int ip_mrouter_init(struct socket *, int);
280 static int add_vif(struct vifctl *);
281 static int del_vif_locked(vifi_t);
282 static int del_vif(vifi_t);
283 static int add_mfc(struct mfcctl2 *);
284 static int del_mfc(struct mfcctl2 *);
285 static int set_api_config(uint32_t *); /* chose API capabilities */
286 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
287 static int set_assert(int);
288 static void expire_upcalls(void *);
289 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
290 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
291 static void send_packet(struct vif *, struct mbuf *);
294 * Bandwidth monitoring
296 static void free_bw_list(struct bw_meter *list);
297 static int add_bw_upcall(struct bw_upcall *);
298 static int del_bw_upcall(struct bw_upcall *);
299 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
300 struct timeval *nowp);
301 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
302 static void bw_upcalls_send(void);
303 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
304 static void unschedule_bw_meter(struct bw_meter *x);
305 static void bw_meter_process(void);
306 static void expire_bw_upcalls_send(void *);
307 static void expire_bw_meter_process(void *);
309 static int pim_register_send(struct ip *, struct vif *,
310 struct mbuf *, struct mfc *);
311 static int pim_register_send_rp(struct ip *, struct vif *,
312 struct mbuf *, struct mfc *);
313 static int pim_register_send_upcall(struct ip *, struct vif *,
314 struct mbuf *, struct mfc *);
315 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
318 * whether or not special PIM assert processing is enabled.
320 static int pim_assert;
322 * Rate limit for assert notification messages, in usec
324 #define ASSERT_MSG_TIME 3000000
327 * Kernel multicast routing API capabilities and setup.
328 * If more API capabilities are added to the kernel, they should be
329 * recorded in `mrt_api_support'.
331 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
332 MRT_MFC_FLAGS_BORDER_VIF |
335 static uint32_t mrt_api_config = 0;
338 * Hash function for a source, group entry
340 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
341 ((g) >> 20) ^ ((g) >> 10) ^ (g))
344 * Find a route for a given origin IP address and Multicast group address
345 * Type of service parameter to be added in the future!!!
346 * Statistics are updated by the caller if needed
347 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
350 mfc_find(in_addr_t o, in_addr_t g)
356 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
357 if ((rt->mfc_origin.s_addr == o) &&
358 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
364 * Macros to compute elapsed time efficiently
365 * Borrowed from Van Jacobson's scheduling code
367 #define TV_DELTA(a, b, delta) { \
369 delta = (a).tv_usec - (b).tv_usec; \
370 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
379 delta += (1000000 * xxs); \
384 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
385 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
388 * Handle MRT setsockopt commands to modify the multicast routing tables.
391 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
397 struct bw_upcall bw_upcall;
400 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
404 switch (sopt->sopt_name) {
406 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
409 error = ip_mrouter_init(so, optval);
413 error = ip_mrouter_done();
417 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
420 error = add_vif(&vifc);
424 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
427 error = del_vif(vifi);
433 * select data size depending on API version.
435 if (sopt->sopt_name == MRT_ADD_MFC &&
436 mrt_api_config & MRT_API_FLAGS_ALL) {
437 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
438 sizeof(struct mfcctl2));
440 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
441 sizeof(struct mfcctl));
442 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
443 sizeof(mfc) - sizeof(struct mfcctl));
447 if (sopt->sopt_name == MRT_ADD_MFC)
448 error = add_mfc(&mfc);
450 error = del_mfc(&mfc);
454 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
461 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
463 error = set_api_config(&i);
465 error = sooptcopyout(sopt, &i, sizeof i);
468 case MRT_ADD_BW_UPCALL:
469 case MRT_DEL_BW_UPCALL:
470 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
474 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
475 error = add_bw_upcall(&bw_upcall);
477 error = del_bw_upcall(&bw_upcall);
488 * Handle MRT getsockopt commands
491 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
494 static int version = 0x0305; /* !!! why is this here? XXX */
496 switch (sopt->sopt_name) {
498 error = sooptcopyout(sopt, &version, sizeof version);
502 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
505 case MRT_API_SUPPORT:
506 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
510 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
521 * Handle ioctl commands to obtain information from the cache
524 X_mrt_ioctl(int cmd, caddr_t data)
529 * Currently the only function calling this ioctl routine is rtioctl().
530 * Typically, only root can create the raw socket in order to execute
531 * this ioctl method, however the request might be coming from a prison
533 error = priv_check(curthread, PRIV_NETINET_MROUTE);
537 case (SIOCGETVIFCNT):
538 error = get_vif_cnt((struct sioc_vif_req *)data);
542 error = get_sg_cnt((struct sioc_sg_req *)data);
553 * returns the packet, byte, rpf-failure count for the source group provided
556 get_sg_cnt(struct sioc_sg_req *req)
561 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
564 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
565 return EADDRNOTAVAIL;
567 req->pktcnt = rt->mfc_pkt_cnt;
568 req->bytecnt = rt->mfc_byte_cnt;
569 req->wrong_if = rt->mfc_wrong_if;
575 * returns the input and output packet and byte counts on the vif provided
578 get_vif_cnt(struct sioc_vif_req *req)
580 vifi_t vifi = req->vifi;
583 if (vifi >= numvifs) {
588 req->icount = viftable[vifi].v_pkt_in;
589 req->ocount = viftable[vifi].v_pkt_out;
590 req->ibytes = viftable[vifi].v_bytes_in;
591 req->obytes = viftable[vifi].v_bytes_out;
598 ip_mrouter_reset(void)
600 bzero((caddr_t)mfctable, sizeof(mfctable));
601 bzero((caddr_t)nexpire, sizeof(nexpire));
606 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
609 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
610 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
611 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
614 static struct mtx mrouter_mtx;
617 if_detached_event(void *arg __unused, struct ifnet *ifp)
623 struct mfc **ppmfc; /* Pointer to previous node's next-pointer */
627 mtx_lock(&mrouter_mtx);
628 if (ip_mrouter == NULL) {
629 mtx_unlock(&mrouter_mtx);
633 * Tear down multicast forwarder state associated with this ifnet.
634 * 1. Walk the vif list, matching vifs against this ifnet.
635 * 2. Walk the multicast forwarding cache (mfc) looking for
636 * inner matches with this vif's index.
637 * 3. Free any pending mbufs for this mfc.
638 * 4. Free the associated mfc entry and state associated with this vif.
639 * Be very careful about unlinking from a singly-linked list whose
640 * "head node" is a pointer in a simple array.
641 * 5. Free vif state. This should disable ALLMULTI on the interface.
645 for (vifi = 0; vifi < numvifs; vifi++) {
646 if (viftable[vifi].v_ifp != ifp)
648 for (i = 0; i < MFCTBLSIZ; i++) {
649 ppmfc = &mfctable[i];
650 for (mfc = mfctable[i]; mfc != NULL; ) {
651 nmfc = mfc->mfc_next;
652 if (mfc->mfc_parent == vifi) {
653 for (pq = mfc->mfc_stall; pq != NULL; ) {
659 free_bw_list(mfc->mfc_bw_meter);
660 free(mfc, M_MRTABLE);
663 ppmfc = &mfc->mfc_next;
668 del_vif_locked(vifi);
673 mtx_unlock(&mrouter_mtx);
677 * Enable multicast routing
680 ip_mrouter_init(struct socket *so, int version)
683 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
684 so->so_type, so->so_proto->pr_protocol);
686 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
692 mtx_lock(&mrouter_mtx);
694 if (ip_mrouter != NULL) {
695 mtx_unlock(&mrouter_mtx);
699 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
700 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
701 if (if_detach_event_tag == NULL)
704 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
706 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
707 expire_bw_upcalls_send, NULL);
708 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
712 mtx_unlock(&mrouter_mtx);
715 log(LOG_DEBUG, "ip_mrouter_init\n");
721 * Disable multicast routing
724 X_ip_mrouter_done(void)
733 mtx_lock(&mrouter_mtx);
735 if (ip_mrouter == NULL) {
736 mtx_unlock(&mrouter_mtx);
741 * Detach/disable hooks to the reset of the system.
748 * For each phyint in use, disable promiscuous reception of all IP
751 for (vifi = 0; vifi < numvifs; vifi++) {
752 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
753 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
754 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
756 so->sin_len = sizeof(struct sockaddr_in);
757 so->sin_family = AF_INET;
758 so->sin_addr.s_addr = INADDR_ANY;
759 ifp = viftable[vifi].v_ifp;
763 bzero((caddr_t)viftable, sizeof(viftable));
767 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
770 * Free all multicast forwarding cache entries.
772 callout_stop(&expire_upcalls_ch);
773 callout_stop(&bw_upcalls_ch);
774 callout_stop(&bw_meter_ch);
777 for (i = 0; i < MFCTBLSIZ; i++) {
778 for (rt = mfctable[i]; rt != NULL; ) {
779 struct mfc *nr = rt->mfc_next;
781 for (rte = rt->mfc_stall; rte != NULL; ) {
782 struct rtdetq *n = rte->next;
785 free(rte, M_MRTABLE);
788 free_bw_list(rt->mfc_bw_meter);
793 bzero((caddr_t)mfctable, sizeof(mfctable));
794 bzero((caddr_t)nexpire, sizeof(nexpire));
796 bzero(bw_meter_timers, sizeof(bw_meter_timers));
799 reg_vif_num = VIFI_INVALID;
801 mtx_unlock(&mrouter_mtx);
804 log(LOG_DEBUG, "ip_mrouter_done\n");
810 * Set PIM assert processing global
815 if ((i != 1) && (i != 0))
824 * Configure API capabilities
827 set_api_config(uint32_t *apival)
832 * We can set the API capabilities only if it is the first operation
833 * after MRT_INIT. I.e.:
834 * - there are no vifs installed
835 * - pim_assert is not enabled
836 * - the MFC table is empty
846 for (i = 0; i < MFCTBLSIZ; i++) {
847 if (mfctable[i] != NULL) {
853 mrt_api_config = *apival & mrt_api_support;
854 *apival = mrt_api_config;
860 * Add a vif to the vif table
863 add_vif(struct vifctl *vifcp)
865 struct vif *vifp = viftable + vifcp->vifc_vifi;
866 struct sockaddr_in sin = {sizeof sin, AF_INET};
872 if (vifcp->vifc_vifi >= MAXVIFS) {
876 /* rate limiting is no longer supported by this code */
877 if (vifcp->vifc_rate_limit != 0) {
878 log(LOG_ERR, "rate limiting is no longer supported\n");
882 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
886 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
888 return EADDRNOTAVAIL;
891 /* Find the interface with an address in AF_INET family */
892 if (vifcp->vifc_flags & VIFF_REGISTER) {
894 * XXX: Because VIFF_REGISTER does not really need a valid
895 * local interface (e.g. it could be 127.0.0.2), we don't
900 sin.sin_addr = vifcp->vifc_lcl_addr;
901 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
904 return EADDRNOTAVAIL;
909 if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
910 log(LOG_ERR, "tunnels are no longer supported\n");
913 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
914 ifp = &multicast_register_if;
916 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
917 (void *)&multicast_register_if);
918 if (reg_vif_num == VIFI_INVALID) {
919 if_initname(&multicast_register_if, "register_vif", 0);
920 multicast_register_if.if_flags = IFF_LOOPBACK;
921 reg_vif_num = vifcp->vifc_vifi;
923 } else { /* Make sure the interface supports multicast */
924 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
929 /* Enable promiscuous reception of all IP multicasts from the if */
930 error = if_allmulti(ifp, 1);
937 vifp->v_flags = vifcp->vifc_flags;
938 vifp->v_threshold = vifcp->vifc_threshold;
939 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
940 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
943 vifp->v_rsvpd = NULL;
944 /* initialize per vif pkt counters */
947 vifp->v_bytes_in = 0;
948 vifp->v_bytes_out = 0;
949 bzero(&vifp->v_route, sizeof(vifp->v_route));
951 /* Adjust numvifs up if the vifi is higher than numvifs */
952 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
957 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x\n",
959 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
960 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
961 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
962 vifcp->vifc_threshold);
968 * Delete a vif from the vif table
971 del_vif_locked(vifi_t vifi)
977 if (vifi >= numvifs) {
980 vifp = &viftable[vifi];
981 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
982 return EADDRNOTAVAIL;
985 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
986 if_allmulti(vifp->v_ifp, 0);
988 if (vifp->v_flags & VIFF_REGISTER)
989 reg_vif_num = VIFI_INVALID;
991 bzero((caddr_t)vifp, sizeof (*vifp));
994 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
996 /* Adjust numvifs down */
997 for (vifi = numvifs; vifi > 0; vifi--)
998 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1006 del_vif(vifi_t vifi)
1011 cc = del_vif_locked(vifi);
1018 * update an mfc entry without resetting counters and S,G addresses.
1021 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1025 rt->mfc_parent = mfccp->mfcc_parent;
1026 for (i = 0; i < numvifs; i++) {
1027 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1028 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1031 /* set the RP address */
1032 if (mrt_api_config & MRT_MFC_RP)
1033 rt->mfc_rp = mfccp->mfcc_rp;
1035 rt->mfc_rp.s_addr = INADDR_ANY;
1039 * fully initialize an mfc entry from the parameter.
1042 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1044 rt->mfc_origin = mfccp->mfcc_origin;
1045 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1047 update_mfc_params(rt, mfccp);
1049 /* initialize pkt counters per src-grp */
1050 rt->mfc_pkt_cnt = 0;
1051 rt->mfc_byte_cnt = 0;
1052 rt->mfc_wrong_if = 0;
1053 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1061 add_mfc(struct mfcctl2 *mfccp)
1071 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1073 /* If an entry already exists, just update the fields */
1075 if (mrtdebug & DEBUG_MFC)
1076 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1077 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1078 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1079 mfccp->mfcc_parent);
1081 update_mfc_params(rt, mfccp);
1088 * Find the entry for which the upcall was made and update
1090 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1091 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1093 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1094 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1095 (rt->mfc_stall != NULL)) {
1098 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1099 "multiple kernel entries",
1100 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1101 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1102 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1104 if (mrtdebug & DEBUG_MFC)
1105 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1106 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1107 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1108 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1110 init_mfc_params(rt, mfccp);
1112 rt->mfc_expire = 0; /* Don't clean this guy up */
1115 /* free packets Qed at the end of this entry */
1116 for (rte = rt->mfc_stall; rte != NULL; ) {
1117 struct rtdetq *n = rte->next;
1119 ip_mdq(rte->m, rte->ifp, rt, -1);
1121 free(rte, M_MRTABLE);
1124 rt->mfc_stall = NULL;
1129 * It is possible that an entry is being inserted without an upcall
1132 if (mrtdebug & DEBUG_MFC)
1133 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1134 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1135 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1136 mfccp->mfcc_parent);
1138 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1139 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1140 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1141 init_mfc_params(rt, mfccp);
1148 if (rt == NULL) { /* no upcall, so make a new entry */
1149 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1156 init_mfc_params(rt, mfccp);
1158 rt->mfc_stall = NULL;
1160 rt->mfc_bw_meter = NULL;
1161 /* insert new entry at head of hash chain */
1162 rt->mfc_next = mfctable[hash];
1163 mfctable[hash] = rt;
1172 * Delete an mfc entry
1175 del_mfc(struct mfcctl2 *mfccp)
1177 struct in_addr origin;
1178 struct in_addr mcastgrp;
1182 struct bw_meter *list;
1184 origin = mfccp->mfcc_origin;
1185 mcastgrp = mfccp->mfcc_mcastgrp;
1187 if (mrtdebug & DEBUG_MFC)
1188 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1189 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1193 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1194 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1195 if (origin.s_addr == rt->mfc_origin.s_addr &&
1196 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1197 rt->mfc_stall == NULL)
1201 return EADDRNOTAVAIL;
1204 *nptr = rt->mfc_next;
1207 * free the bw_meter entries
1209 list = rt->mfc_bw_meter;
1210 rt->mfc_bw_meter = NULL;
1212 free(rt, M_MRTABLE);
1222 * Send a message to the routing daemon on the multicast routing socket
1225 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1228 SOCKBUF_LOCK(&s->so_rcv);
1229 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1231 sorwakeup_locked(s);
1234 SOCKBUF_UNLOCK(&s->so_rcv);
1241 * IP multicast forwarding function. This function assumes that the packet
1242 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1243 * pointed to by "ifp", and the packet is to be relayed to other networks
1244 * that have members of the packet's destination IP multicast group.
1246 * The packet is returned unscathed to the caller, unless it is
1247 * erroneous, in which case a non-zero return value tells the caller to
1251 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1254 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1255 struct ip_moptions *imo)
1261 if (mrtdebug & DEBUG_FORWARD)
1262 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1263 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1266 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1267 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1269 * Packet arrived via a physical interface or
1270 * an encapsulated tunnel or a register_vif.
1274 * Packet arrived through a source-route tunnel.
1275 * Source-route tunnels are no longer supported.
1277 static int last_log;
1278 if (last_log != time_uptime) {
1279 last_log = time_uptime;
1281 "ip_mforward: received source-routed packet from %lx\n",
1282 (u_long)ntohl(ip->ip_src.s_addr));
1289 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1290 if (ip->ip_ttl < 255)
1291 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1292 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1293 struct vif *vifp = viftable + vifi;
1295 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1296 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1298 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1299 vifp->v_ifp->if_xname);
1301 error = ip_mdq(m, ifp, NULL, vifi);
1306 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1307 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1308 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1310 printf("In fact, no options were specified at all\n");
1314 * Don't forward a packet with time-to-live of zero or one,
1315 * or a packet destined to a local-only group.
1317 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1324 * Determine forwarding vifs from the forwarding cache table
1326 ++mrtstat.mrts_mfc_lookups;
1327 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1329 /* Entry exists, so forward if necessary */
1331 error = ip_mdq(m, ifp, rt, -1);
1337 * If we don't have a route for packet's origin,
1338 * Make a copy of the packet & send message to routing daemon
1344 int hlen = ip->ip_hl << 2;
1346 ++mrtstat.mrts_mfc_misses;
1348 mrtstat.mrts_no_route++;
1349 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1350 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1351 (u_long)ntohl(ip->ip_src.s_addr),
1352 (u_long)ntohl(ip->ip_dst.s_addr));
1355 * Allocate mbufs early so that we don't do extra work if we are
1356 * just going to fail anyway. Make sure to pullup the header so
1357 * that other people can't step on it.
1359 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1365 mb0 = m_copypacket(m, M_DONTWAIT);
1366 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1367 mb0 = m_pullup(mb0, hlen);
1369 free(rte, M_MRTABLE);
1375 /* is there an upcall waiting for this flow ? */
1376 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1377 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1378 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1379 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1380 (rt->mfc_stall != NULL))
1387 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1391 * Locate the vifi for the incoming interface for this packet.
1392 * If none found, drop packet.
1394 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1396 if (vifi >= numvifs) /* vif not found, drop packet */
1399 /* no upcall, so make a new entry */
1400 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1403 /* Make a copy of the header to send to the user level process */
1404 mm = m_copy(mb0, 0, hlen);
1409 * Send message to routing daemon to install
1410 * a route into the kernel table
1413 im = mtod(mm, struct igmpmsg *);
1414 im->im_msgtype = IGMPMSG_NOCACHE;
1418 mrtstat.mrts_upcalls++;
1420 k_igmpsrc.sin_addr = ip->ip_src;
1421 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1422 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1423 ++mrtstat.mrts_upq_sockfull;
1425 free(rt, M_MRTABLE);
1427 free(rte, M_MRTABLE);
1434 /* insert new entry at head of hash chain */
1435 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1436 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1437 rt->mfc_expire = UPCALL_EXPIRE;
1439 for (i = 0; i < numvifs; i++) {
1440 rt->mfc_ttls[i] = 0;
1441 rt->mfc_flags[i] = 0;
1443 rt->mfc_parent = -1;
1445 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1447 rt->mfc_bw_meter = NULL;
1449 /* link into table */
1450 rt->mfc_next = mfctable[hash];
1451 mfctable[hash] = rt;
1452 rt->mfc_stall = rte;
1455 /* determine if q has overflowed */
1460 * XXX ouch! we need to append to the list, but we
1461 * only have a pointer to the front, so we have to
1462 * scan the entire list every time.
1464 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1467 if (npkts > MAX_UPQ) {
1468 mrtstat.mrts_upq_ovflw++;
1470 free(rte, M_MRTABLE);
1477 /* Add this entry to the end of the queue */
1493 * Clean up the cache entry if upcall is not serviced
1496 expire_upcalls(void *unused)
1499 struct mfc *mfc, **nptr;
1503 for (i = 0; i < MFCTBLSIZ; i++) {
1504 if (nexpire[i] == 0)
1506 nptr = &mfctable[i];
1507 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1509 * Skip real cache entries
1510 * Make sure it wasn't marked to not expire (shouldn't happen)
1513 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1514 --mfc->mfc_expire == 0) {
1515 if (mrtdebug & DEBUG_EXPIRE)
1516 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1517 (u_long)ntohl(mfc->mfc_origin.s_addr),
1518 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1520 * drop all the packets
1521 * free the mbuf with the pkt, if, timing info
1523 for (rte = mfc->mfc_stall; rte; ) {
1524 struct rtdetq *n = rte->next;
1527 free(rte, M_MRTABLE);
1530 ++mrtstat.mrts_cache_cleanups;
1534 * free the bw_meter entries
1536 while (mfc->mfc_bw_meter != NULL) {
1537 struct bw_meter *x = mfc->mfc_bw_meter;
1539 mfc->mfc_bw_meter = x->bm_mfc_next;
1543 *nptr = mfc->mfc_next;
1544 free(mfc, M_MRTABLE);
1546 nptr = &mfc->mfc_next;
1552 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1556 * Packet forwarding routine once entry in the cache is made
1559 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1561 struct ip *ip = mtod(m, struct ip *);
1563 int plen = ip->ip_len;
1568 * If xmt_vif is not -1, send on only the requested vif.
1570 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1572 if (xmt_vif < numvifs) {
1573 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1574 pim_register_send(ip, viftable + xmt_vif, m, rt);
1576 phyint_send(ip, viftable + xmt_vif, m);
1581 * Don't forward if it didn't arrive from the parent vif for its origin.
1583 vifi = rt->mfc_parent;
1584 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1585 /* came in the wrong interface */
1586 if (mrtdebug & DEBUG_FORWARD)
1587 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1588 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1589 ++mrtstat.mrts_wrong_if;
1592 * If we are doing PIM assert processing, send a message
1593 * to the routing daemon.
1595 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1596 * can complete the SPT switch, regardless of the type
1597 * of the iif (broadcast media, GRE tunnel, etc).
1599 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1603 if (ifp == &multicast_register_if)
1604 pimstat.pims_rcv_registers_wrongiif++;
1606 /* Get vifi for the incoming packet */
1607 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1609 if (vifi >= 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 */
1617 TV_DELTA(now, rt->mfc_last_assert, delta);
1619 if (delta > ASSERT_MSG_TIME) {
1620 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1622 int hlen = ip->ip_hl << 2;
1623 struct mbuf *mm = m_copy(m, 0, hlen);
1625 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1626 mm = m_pullup(mm, hlen);
1630 rt->mfc_last_assert = now;
1632 im = mtod(mm, struct igmpmsg *);
1633 im->im_msgtype = IGMPMSG_WRONGVIF;
1637 mrtstat.mrts_upcalls++;
1639 k_igmpsrc.sin_addr = im->im_src;
1640 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1642 "ip_mforward: ip_mrouter socket queue full\n");
1643 ++mrtstat.mrts_upq_sockfull;
1651 /* If I sourced this packet, it counts as output, else it was input. */
1652 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1653 viftable[vifi].v_pkt_out++;
1654 viftable[vifi].v_bytes_out += plen;
1656 viftable[vifi].v_pkt_in++;
1657 viftable[vifi].v_bytes_in += plen;
1660 rt->mfc_byte_cnt += plen;
1663 * For each vif, decide if a copy of the packet should be forwarded.
1665 * - the ttl exceeds the vif's threshold
1666 * - there are group members downstream on interface
1668 for (vifi = 0; vifi < numvifs; vifi++)
1669 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1670 viftable[vifi].v_pkt_out++;
1671 viftable[vifi].v_bytes_out += plen;
1672 if (viftable[vifi].v_flags & VIFF_REGISTER)
1673 pim_register_send(ip, viftable + vifi, m, rt);
1675 phyint_send(ip, viftable + vifi, m);
1679 * Perform upcall-related bw measuring.
1681 if (rt->mfc_bw_meter != NULL) {
1687 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1688 bw_meter_receive_packet(x, plen, &now);
1695 * check if a vif number is legal/ok. This is used by ip_output.
1698 X_legal_vif_num(int vif)
1700 /* XXX unlocked, matter? */
1701 return (vif >= 0 && vif < numvifs);
1705 * Return the local address used by this vif
1708 X_ip_mcast_src(int vifi)
1710 /* XXX unlocked, matter? */
1711 if (vifi >= 0 && vifi < numvifs)
1712 return viftable[vifi].v_lcl_addr.s_addr;
1718 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1720 struct mbuf *mb_copy;
1721 int hlen = ip->ip_hl << 2;
1726 * Make a new reference to the packet; make sure that
1727 * the IP header is actually copied, not just referenced,
1728 * so that ip_output() only scribbles on the copy.
1730 mb_copy = m_copypacket(m, M_DONTWAIT);
1731 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1732 mb_copy = m_pullup(mb_copy, hlen);
1733 if (mb_copy == NULL)
1736 send_packet(vifp, mb_copy);
1740 send_packet(struct vif *vifp, struct mbuf *m)
1742 struct ip_moptions imo;
1743 struct in_multi *imm[2];
1748 imo.imo_multicast_ifp = vifp->v_ifp;
1749 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1750 imo.imo_multicast_loop = 1;
1751 imo.imo_multicast_vif = -1;
1752 imo.imo_num_memberships = 0;
1753 imo.imo_max_memberships = 2;
1754 imo.imo_membership = &imm[0];
1757 * Re-entrancy should not be a problem here, because
1758 * the packets that we send out and are looped back at us
1759 * should get rejected because they appear to come from
1760 * the loopback interface, thus preventing looping.
1762 error = ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, &imo, NULL);
1763 if (mrtdebug & DEBUG_XMIT) {
1764 log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
1765 vifp - viftable, error);
1770 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
1774 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1777 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
1783 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
1785 return EADDRNOTAVAIL;
1788 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
1789 /* Check if socket is available. */
1790 if (viftable[vifi].v_rsvpd != NULL) {
1795 viftable[vifi].v_rsvpd = so;
1796 /* This may seem silly, but we need to be sure we don't over-increment
1797 * the RSVP counter, in case something slips up.
1799 if (!viftable[vifi].v_rsvp_on) {
1800 viftable[vifi].v_rsvp_on = 1;
1803 } else { /* must be VIF_OFF */
1805 * XXX as an additional consistency check, one could make sure
1806 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
1807 * first parameter is pretty useless.
1809 viftable[vifi].v_rsvpd = NULL;
1811 * This may seem silly, but we need to be sure we don't over-decrement
1812 * the RSVP counter, in case something slips up.
1814 if (viftable[vifi].v_rsvp_on) {
1815 viftable[vifi].v_rsvp_on = 0;
1824 X_ip_rsvp_force_done(struct socket *so)
1828 /* Don't bother if it is not the right type of socket. */
1829 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1834 /* The socket may be attached to more than one vif...this
1835 * is perfectly legal.
1837 for (vifi = 0; vifi < numvifs; vifi++) {
1838 if (viftable[vifi].v_rsvpd == so) {
1839 viftable[vifi].v_rsvpd = NULL;
1840 /* This may seem silly, but we need to be sure we don't
1841 * over-decrement the RSVP counter, in case something slips up.
1843 if (viftable[vifi].v_rsvp_on) {
1844 viftable[vifi].v_rsvp_on = 0;
1854 X_rsvp_input(struct mbuf *m, int off)
1857 struct ip *ip = mtod(m, struct ip *);
1858 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
1862 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
1864 /* Can still get packets with rsvp_on = 0 if there is a local member
1865 * of the group to which the RSVP packet is addressed. But in this
1866 * case we want to throw the packet away.
1874 printf("rsvp_input: check vifs\n");
1880 ifp = m->m_pkthdr.rcvif;
1883 /* Find which vif the packet arrived on. */
1884 for (vifi = 0; vifi < numvifs; vifi++)
1885 if (viftable[vifi].v_ifp == ifp)
1888 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
1890 * Drop the lock here to avoid holding it across rip_input.
1891 * This could make rsvpdebug printfs wrong. If you care,
1892 * record the state of stuff before dropping the lock.
1896 * If the old-style non-vif-associated socket is set,
1897 * then use it. Otherwise, drop packet since there
1898 * is no specific socket for this vif.
1900 if (ip_rsvpd != NULL) {
1902 printf("rsvp_input: Sending packet up old-style socket\n");
1903 rip_input(m, off); /* xxx */
1905 if (rsvpdebug && vifi == numvifs)
1906 printf("rsvp_input: Can't find vif for packet.\n");
1907 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
1908 printf("rsvp_input: No socket defined for vif %d\n",vifi);
1913 rsvp_src.sin_addr = ip->ip_src;
1916 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
1917 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
1919 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
1921 printf("rsvp_input: Failed to append to socket\n");
1924 printf("rsvp_input: send packet up\n");
1930 * Code for bandwidth monitors
1934 * Define common interface for timeval-related methods
1936 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1937 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1938 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1941 compute_bw_meter_flags(struct bw_upcall *req)
1945 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1946 flags |= BW_METER_UNIT_PACKETS;
1947 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1948 flags |= BW_METER_UNIT_BYTES;
1949 if (req->bu_flags & BW_UPCALL_GEQ)
1950 flags |= BW_METER_GEQ;
1951 if (req->bu_flags & BW_UPCALL_LEQ)
1952 flags |= BW_METER_LEQ;
1958 * Add a bw_meter entry
1961 add_bw_upcall(struct bw_upcall *req)
1964 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
1965 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
1970 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
1973 /* Test if the flags are valid */
1974 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
1976 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
1978 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
1979 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
1982 /* Test if the threshold time interval is valid */
1983 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
1986 flags = compute_bw_meter_flags(req);
1989 * Find if we have already same bw_meter entry
1992 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
1995 return EADDRNOTAVAIL;
1997 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
1998 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
1999 &req->bu_threshold.b_time, ==)) &&
2000 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2001 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2002 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2004 return 0; /* XXX Already installed */
2008 /* Allocate the new bw_meter entry */
2009 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2015 /* Set the new bw_meter entry */
2016 x->bm_threshold.b_time = req->bu_threshold.b_time;
2018 x->bm_start_time = now;
2019 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2020 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2021 x->bm_measured.b_packets = 0;
2022 x->bm_measured.b_bytes = 0;
2023 x->bm_flags = flags;
2024 x->bm_time_next = NULL;
2025 x->bm_time_hash = BW_METER_BUCKETS;
2027 /* Add the new bw_meter entry to the front of entries for this MFC */
2029 x->bm_mfc_next = mfc->mfc_bw_meter;
2030 mfc->mfc_bw_meter = x;
2031 schedule_bw_meter(x, &now);
2038 free_bw_list(struct bw_meter *list)
2040 while (list != NULL) {
2041 struct bw_meter *x = list;
2043 list = list->bm_mfc_next;
2044 unschedule_bw_meter(x);
2050 * Delete one or multiple bw_meter entries
2053 del_bw_upcall(struct bw_upcall *req)
2058 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2062 /* Find the corresponding MFC entry */
2063 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2066 return EADDRNOTAVAIL;
2067 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2069 * Delete all bw_meter entries for this mfc
2071 struct bw_meter *list;
2073 list = mfc->mfc_bw_meter;
2074 mfc->mfc_bw_meter = NULL;
2078 } else { /* Delete a single bw_meter entry */
2079 struct bw_meter *prev;
2082 flags = compute_bw_meter_flags(req);
2084 /* Find the bw_meter entry to delete */
2085 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2086 prev = x, x = x->bm_mfc_next) {
2087 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2088 &req->bu_threshold.b_time, ==)) &&
2089 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2090 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2091 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2094 if (x != NULL) { /* Delete entry from the list for this MFC */
2096 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2098 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2100 unschedule_bw_meter(x);
2102 /* Free the bw_meter entry */
2114 * Perform bandwidth measurement processing that may result in an upcall
2117 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2119 struct timeval delta;
2124 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2126 if (x->bm_flags & BW_METER_GEQ) {
2128 * Processing for ">=" type of bw_meter entry
2130 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2131 /* Reset the bw_meter entry */
2132 x->bm_start_time = *nowp;
2133 x->bm_measured.b_packets = 0;
2134 x->bm_measured.b_bytes = 0;
2135 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2138 /* Record that a packet is received */
2139 x->bm_measured.b_packets++;
2140 x->bm_measured.b_bytes += plen;
2143 * Test if we should deliver an upcall
2145 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2146 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2147 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2148 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2149 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2150 /* Prepare an upcall for delivery */
2151 bw_meter_prepare_upcall(x, nowp);
2152 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2155 } else if (x->bm_flags & BW_METER_LEQ) {
2157 * Processing for "<=" type of bw_meter entry
2159 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2161 * We are behind time with the multicast forwarding table
2162 * scanning for "<=" type of bw_meter entries, so test now
2163 * if we should deliver an upcall.
2165 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2166 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2167 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2168 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2169 /* Prepare an upcall for delivery */
2170 bw_meter_prepare_upcall(x, nowp);
2172 /* Reschedule the bw_meter entry */
2173 unschedule_bw_meter(x);
2174 schedule_bw_meter(x, nowp);
2177 /* Record that a packet is received */
2178 x->bm_measured.b_packets++;
2179 x->bm_measured.b_bytes += plen;
2182 * Test if we should restart the measuring interval
2184 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2185 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2186 (x->bm_flags & BW_METER_UNIT_BYTES &&
2187 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2188 /* Don't restart the measuring interval */
2190 /* Do restart the measuring interval */
2192 * XXX: note that we don't unschedule and schedule, because this
2193 * might be too much overhead per packet. Instead, when we process
2194 * all entries for a given timer hash bin, we check whether it is
2195 * really a timeout. If not, we reschedule at that time.
2197 x->bm_start_time = *nowp;
2198 x->bm_measured.b_packets = 0;
2199 x->bm_measured.b_bytes = 0;
2200 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2206 * Prepare a bandwidth-related upcall
2209 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2211 struct timeval delta;
2212 struct bw_upcall *u;
2217 * Compute the measured time interval
2220 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2223 * If there are too many pending upcalls, deliver them now
2225 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2229 * Set the bw_upcall entry
2231 u = &bw_upcalls[bw_upcalls_n++];
2232 u->bu_src = x->bm_mfc->mfc_origin;
2233 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2234 u->bu_threshold.b_time = x->bm_threshold.b_time;
2235 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2236 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2237 u->bu_measured.b_time = delta;
2238 u->bu_measured.b_packets = x->bm_measured.b_packets;
2239 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2241 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2242 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2243 if (x->bm_flags & BW_METER_UNIT_BYTES)
2244 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2245 if (x->bm_flags & BW_METER_GEQ)
2246 u->bu_flags |= BW_UPCALL_GEQ;
2247 if (x->bm_flags & BW_METER_LEQ)
2248 u->bu_flags |= BW_UPCALL_LEQ;
2252 * Send the pending bandwidth-related upcalls
2255 bw_upcalls_send(void)
2258 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2259 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2260 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2262 IGMPMSG_BW_UPCALL,/* im_msgtype */
2267 { 0 } }; /* im_dst */
2271 if (bw_upcalls_n == 0)
2272 return; /* No pending upcalls */
2277 * Allocate a new mbuf, initialize it with the header and
2278 * the payload for the pending calls.
2280 MGETHDR(m, M_DONTWAIT, MT_DATA);
2282 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2286 m->m_len = m->m_pkthdr.len = 0;
2287 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2288 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2292 * XXX do we need to set the address in k_igmpsrc ?
2294 mrtstat.mrts_upcalls++;
2295 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2296 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2297 ++mrtstat.mrts_upq_sockfull;
2302 * Compute the timeout hash value for the bw_meter entries
2304 #define BW_METER_TIMEHASH(bw_meter, hash) \
2306 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2308 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2309 (hash) = next_timeval.tv_sec; \
2310 if (next_timeval.tv_usec) \
2311 (hash)++; /* XXX: make sure we don't timeout early */ \
2312 (hash) %= BW_METER_BUCKETS; \
2316 * Schedule a timer to process periodically bw_meter entry of type "<="
2317 * by linking the entry in the proper hash bucket.
2320 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2326 if (!(x->bm_flags & BW_METER_LEQ))
2327 return; /* XXX: we schedule timers only for "<=" entries */
2330 * Reset the bw_meter entry
2332 x->bm_start_time = *nowp;
2333 x->bm_measured.b_packets = 0;
2334 x->bm_measured.b_bytes = 0;
2335 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2338 * Compute the timeout hash value and insert the entry
2340 BW_METER_TIMEHASH(x, time_hash);
2341 x->bm_time_next = bw_meter_timers[time_hash];
2342 bw_meter_timers[time_hash] = x;
2343 x->bm_time_hash = time_hash;
2347 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2348 * by removing the entry from the proper hash bucket.
2351 unschedule_bw_meter(struct bw_meter *x)
2354 struct bw_meter *prev, *tmp;
2358 if (!(x->bm_flags & BW_METER_LEQ))
2359 return; /* XXX: we schedule timers only for "<=" entries */
2362 * Compute the timeout hash value and delete the entry
2364 time_hash = x->bm_time_hash;
2365 if (time_hash >= BW_METER_BUCKETS)
2366 return; /* Entry was not scheduled */
2368 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2369 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2374 panic("unschedule_bw_meter: bw_meter entry not found");
2377 prev->bm_time_next = x->bm_time_next;
2379 bw_meter_timers[time_hash] = x->bm_time_next;
2381 x->bm_time_next = NULL;
2382 x->bm_time_hash = BW_METER_BUCKETS;
2387 * Process all "<=" type of bw_meter that should be processed now,
2388 * and for each entry prepare an upcall if necessary. Each processed
2389 * entry is rescheduled again for the (periodic) processing.
2391 * This is run periodically (once per second normally). On each round,
2392 * all the potentially matching entries are in the hash slot that we are
2398 static uint32_t last_tv_sec; /* last time we processed this */
2402 struct timeval now, process_endtime;
2405 if (last_tv_sec == now.tv_sec)
2406 return; /* nothing to do */
2408 loops = now.tv_sec - last_tv_sec;
2409 last_tv_sec = now.tv_sec;
2410 if (loops > BW_METER_BUCKETS)
2411 loops = BW_METER_BUCKETS;
2415 * Process all bins of bw_meter entries from the one after the last
2416 * processed to the current one. On entry, i points to the last bucket
2417 * visited, so we need to increment i at the beginning of the loop.
2419 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2420 struct bw_meter *x, *tmp_list;
2422 if (++i >= BW_METER_BUCKETS)
2425 /* Disconnect the list of bw_meter entries from the bin */
2426 tmp_list = bw_meter_timers[i];
2427 bw_meter_timers[i] = NULL;
2429 /* Process the list of bw_meter entries */
2430 while (tmp_list != NULL) {
2432 tmp_list = tmp_list->bm_time_next;
2434 /* Test if the time interval is over */
2435 process_endtime = x->bm_start_time;
2436 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2437 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2438 /* Not yet: reschedule, but don't reset */
2441 BW_METER_TIMEHASH(x, time_hash);
2442 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2444 * XXX: somehow the bin processing is a bit ahead of time.
2445 * Put the entry in the next bin.
2447 if (++time_hash >= BW_METER_BUCKETS)
2450 x->bm_time_next = bw_meter_timers[time_hash];
2451 bw_meter_timers[time_hash] = x;
2452 x->bm_time_hash = time_hash;
2458 * Test if we should deliver an upcall
2460 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2461 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2462 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2463 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2464 /* Prepare an upcall for delivery */
2465 bw_meter_prepare_upcall(x, &now);
2469 * Reschedule for next processing
2471 schedule_bw_meter(x, &now);
2475 /* Send all upcalls that are pending delivery */
2482 * A periodic function for sending all upcalls that are pending delivery
2485 expire_bw_upcalls_send(void *unused)
2491 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2492 expire_bw_upcalls_send, NULL);
2496 * A periodic function for periodic scanning of the multicast forwarding
2497 * table for processing all "<=" bw_meter entries.
2500 expire_bw_meter_process(void *unused)
2502 if (mrt_api_config & MRT_MFC_BW_UPCALL)
2505 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2509 * End of bandwidth monitoring code
2513 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2517 pim_register_send(struct ip *ip, struct vif *vifp,
2518 struct mbuf *m, struct mfc *rt)
2520 struct mbuf *mb_copy, *mm;
2522 if (mrtdebug & DEBUG_PIM)
2523 log(LOG_DEBUG, "pim_register_send: ");
2525 mb_copy = pim_register_prepare(ip, m);
2526 if (mb_copy == NULL)
2530 * Send all the fragments. Note that the mbuf for each fragment
2531 * is freed by the sending machinery.
2533 for (mm = mb_copy; mm; mm = mb_copy) {
2534 mb_copy = mm->m_nextpkt;
2536 mm = m_pullup(mm, sizeof(struct ip));
2538 ip = mtod(mm, struct ip *);
2539 if ((mrt_api_config & MRT_MFC_RP) &&
2540 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2541 pim_register_send_rp(ip, vifp, mm, rt);
2543 pim_register_send_upcall(ip, vifp, mm, rt);
2552 * Return a copy of the data packet that is ready for PIM Register
2554 * XXX: Note that in the returned copy the IP header is a valid one.
2556 static struct mbuf *
2557 pim_register_prepare(struct ip *ip, struct mbuf *m)
2559 struct mbuf *mb_copy = NULL;
2562 /* Take care of delayed checksums */
2563 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2564 in_delayed_cksum(m);
2565 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2569 * Copy the old packet & pullup its IP header into the
2570 * new mbuf so we can modify it.
2572 mb_copy = m_copypacket(m, M_DONTWAIT);
2573 if (mb_copy == NULL)
2575 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2576 if (mb_copy == NULL)
2579 /* take care of the TTL */
2580 ip = mtod(mb_copy, struct ip *);
2583 /* Compute the MTU after the PIM Register encapsulation */
2584 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2586 if (ip->ip_len <= mtu) {
2587 /* Turn the IP header into a valid one */
2588 ip->ip_len = htons(ip->ip_len);
2589 ip->ip_off = htons(ip->ip_off);
2591 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2593 /* Fragment the packet */
2594 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2603 * Send an upcall with the data packet to the user-level process.
2606 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2607 struct mbuf *mb_copy, struct mfc *rt)
2609 struct mbuf *mb_first;
2610 int len = ntohs(ip->ip_len);
2612 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2617 * Add a new mbuf with an upcall header
2619 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2620 if (mb_first == NULL) {
2624 mb_first->m_data += max_linkhdr;
2625 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2626 mb_first->m_len = sizeof(struct igmpmsg);
2627 mb_first->m_next = mb_copy;
2629 /* Send message to routing daemon */
2630 im = mtod(mb_first, struct igmpmsg *);
2631 im->im_msgtype = IGMPMSG_WHOLEPKT;
2633 im->im_vif = vifp - viftable;
2634 im->im_src = ip->ip_src;
2635 im->im_dst = ip->ip_dst;
2637 k_igmpsrc.sin_addr = ip->ip_src;
2639 mrtstat.mrts_upcalls++;
2641 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2642 if (mrtdebug & DEBUG_PIM)
2644 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2645 ++mrtstat.mrts_upq_sockfull;
2649 /* Keep statistics */
2650 pimstat.pims_snd_registers_msgs++;
2651 pimstat.pims_snd_registers_bytes += len;
2657 * Encapsulate the data packet in PIM Register message and send it to the RP.
2660 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2661 struct mbuf *mb_copy, struct mfc *rt)
2663 struct mbuf *mb_first;
2664 struct ip *ip_outer;
2665 struct pim_encap_pimhdr *pimhdr;
2666 int len = ntohs(ip->ip_len);
2667 vifi_t vifi = rt->mfc_parent;
2671 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2673 return EADDRNOTAVAIL; /* The iif vif is invalid */
2677 * Add a new mbuf with the encapsulating header
2679 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2680 if (mb_first == NULL) {
2684 mb_first->m_data += max_linkhdr;
2685 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2686 mb_first->m_next = mb_copy;
2688 mb_first->m_pkthdr.len = len + mb_first->m_len;
2691 * Fill in the encapsulating IP and PIM header
2693 ip_outer = mtod(mb_first, struct ip *);
2694 *ip_outer = pim_encap_iphdr;
2695 ip_outer->ip_id = ip_newid();
2696 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2697 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2698 ip_outer->ip_dst = rt->mfc_rp;
2700 * Copy the inner header TOS to the outer header, and take care of the
2703 ip_outer->ip_tos = ip->ip_tos;
2704 if (ntohs(ip->ip_off) & IP_DF)
2705 ip_outer->ip_off |= IP_DF;
2706 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2707 + sizeof(pim_encap_iphdr));
2708 *pimhdr = pim_encap_pimhdr;
2709 /* If the iif crosses a border, set the Border-bit */
2710 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2711 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2713 mb_first->m_data += sizeof(pim_encap_iphdr);
2714 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2715 mb_first->m_data -= sizeof(pim_encap_iphdr);
2717 send_packet(vifp, mb_first);
2719 /* Keep statistics */
2720 pimstat.pims_snd_registers_msgs++;
2721 pimstat.pims_snd_registers_bytes += len;
2727 * pim_encapcheck() is called by the encap4_input() path at runtime to
2728 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2732 pim_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
2736 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2738 if (proto != IPPROTO_PIM)
2739 return 0; /* not for us; reject the datagram. */
2741 return 64; /* claim the datagram. */
2745 * PIM-SMv2 and PIM-DM messages processing.
2746 * Receives and verifies the PIM control messages, and passes them
2747 * up to the listening socket, using rip_input().
2748 * The only message with special processing is the PIM_REGISTER message
2749 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2750 * is passed to if_simloop().
2753 pim_input(struct mbuf *m, int off)
2755 struct ip *ip = mtod(m, struct ip *);
2758 int datalen = ip->ip_len;
2762 /* Keep statistics */
2763 pimstat.pims_rcv_total_msgs++;
2764 pimstat.pims_rcv_total_bytes += datalen;
2769 if (datalen < PIM_MINLEN) {
2770 pimstat.pims_rcv_tooshort++;
2771 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
2772 datalen, (u_long)ip->ip_src.s_addr);
2778 * If the packet is at least as big as a REGISTER, go agead
2779 * and grab the PIM REGISTER header size, to avoid another
2780 * possible m_pullup() later.
2782 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
2783 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2785 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2787 * Get the IP and PIM headers in contiguous memory, and
2788 * possibly the PIM REGISTER header.
2790 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
2791 (m = m_pullup(m, minlen)) == 0) {
2792 log(LOG_ERR, "pim_input: m_pullup failure\n");
2795 /* m_pullup() may have given us a new mbuf so reset ip. */
2796 ip = mtod(m, struct ip *);
2797 ip_tos = ip->ip_tos;
2799 /* adjust mbuf to point to the PIM header */
2800 m->m_data += iphlen;
2802 pim = mtod(m, struct pim *);
2805 * Validate checksum. If PIM REGISTER, exclude the data packet.
2807 * XXX: some older PIMv2 implementations don't make this distinction,
2808 * so for compatibility reason perform the checksum over part of the
2809 * message, and if error, then over the whole message.
2811 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2812 /* do nothing, checksum okay */
2813 } else if (in_cksum(m, datalen)) {
2814 pimstat.pims_rcv_badsum++;
2815 if (mrtdebug & DEBUG_PIM)
2816 log(LOG_DEBUG, "pim_input: invalid checksum");
2821 /* PIM version check */
2822 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2823 pimstat.pims_rcv_badversion++;
2824 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
2825 PIM_VT_V(pim->pim_vt), PIM_VERSION);
2830 /* restore mbuf back to the outer IP */
2831 m->m_data -= iphlen;
2834 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2836 * Since this is a REGISTER, we'll make a copy of the register
2837 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2840 struct sockaddr_in dst = { sizeof(dst), AF_INET };
2842 struct ip *encap_ip;
2847 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
2849 if (mrtdebug & DEBUG_PIM)
2851 "pim_input: register vif not set: %d\n", reg_vif_num);
2855 /* XXX need refcnt? */
2856 vifp = viftable[reg_vif_num].v_ifp;
2862 if (datalen < PIM_REG_MINLEN) {
2863 pimstat.pims_rcv_tooshort++;
2864 pimstat.pims_rcv_badregisters++;
2866 "pim_input: register packet size too small %d from %lx\n",
2867 datalen, (u_long)ip->ip_src.s_addr);
2872 reghdr = (u_int32_t *)(pim + 1);
2873 encap_ip = (struct ip *)(reghdr + 1);
2875 if (mrtdebug & DEBUG_PIM) {
2877 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
2878 (u_long)ntohl(encap_ip->ip_src.s_addr),
2879 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2880 ntohs(encap_ip->ip_len));
2883 /* verify the version number of the inner packet */
2884 if (encap_ip->ip_v != IPVERSION) {
2885 pimstat.pims_rcv_badregisters++;
2886 if (mrtdebug & DEBUG_PIM) {
2887 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
2888 "of the inner packet\n", encap_ip->ip_v);
2894 /* verify the inner packet is destined to a mcast group */
2895 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2896 pimstat.pims_rcv_badregisters++;
2897 if (mrtdebug & DEBUG_PIM)
2899 "pim_input: inner packet of register is not "
2901 (u_long)ntohl(encap_ip->ip_dst.s_addr));
2906 /* If a NULL_REGISTER, pass it to the daemon */
2907 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2908 goto pim_input_to_daemon;
2911 * Copy the TOS from the outer IP header to the inner IP header.
2913 if (encap_ip->ip_tos != ip_tos) {
2914 /* Outer TOS -> inner TOS */
2915 encap_ip->ip_tos = ip_tos;
2916 /* Recompute the inner header checksum. Sigh... */
2918 /* adjust mbuf to point to the inner IP header */
2919 m->m_data += (iphlen + PIM_MINLEN);
2920 m->m_len -= (iphlen + PIM_MINLEN);
2922 encap_ip->ip_sum = 0;
2923 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2925 /* restore mbuf to point back to the outer IP header */
2926 m->m_data -= (iphlen + PIM_MINLEN);
2927 m->m_len += (iphlen + PIM_MINLEN);
2931 * Decapsulate the inner IP packet and loopback to forward it
2932 * as a normal multicast packet. Also, make a copy of the
2933 * outer_iphdr + pimhdr + reghdr + encap_iphdr
2934 * to pass to the daemon later, so it can take the appropriate
2935 * actions (e.g., send back PIM_REGISTER_STOP).
2936 * XXX: here m->m_data points to the outer IP header.
2938 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
2941 "pim_input: pim register: could not copy register head\n");
2946 /* Keep statistics */
2947 /* XXX: registers_bytes include only the encap. mcast pkt */
2948 pimstat.pims_rcv_registers_msgs++;
2949 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
2952 * forward the inner ip packet; point m_data at the inner ip.
2954 m_adj(m, iphlen + PIM_MINLEN);
2956 if (mrtdebug & DEBUG_PIM) {
2958 "pim_input: forwarding decapsulated register: "
2959 "src %lx, dst %lx, vif %d\n",
2960 (u_long)ntohl(encap_ip->ip_src.s_addr),
2961 (u_long)ntohl(encap_ip->ip_dst.s_addr),
2964 /* NB: vifp was collected above; can it change on us? */
2965 if_simloop(vifp, m, dst.sin_family, 0);
2967 /* prepare the register head to send to the mrouting daemon */
2971 pim_input_to_daemon:
2973 * Pass the PIM message up to the daemon; if it is a Register message,
2974 * pass the 'head' only up to the daemon. This includes the
2975 * outer IP header, PIM header, PIM-Register header and the
2977 * XXX: the outer IP header pkt size of a Register is not adjust to
2978 * reflect the fact that the inner multicast data is truncated.
2980 rip_input(m, iphlen);
2986 ip_mroute_modevent(module_t mod, int type, void *unused)
2990 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
2994 pim_encap_cookie = encap_attach_func(AF_INET, IPPROTO_PIM,
2995 pim_encapcheck, &in_pim_protosw, NULL);
2996 if (pim_encap_cookie == NULL) {
2997 printf("ip_mroute: unable to attach pim encap\n");
3000 mtx_destroy(&mrouter_mtx);
3003 ip_mcast_src = X_ip_mcast_src;
3004 ip_mforward = X_ip_mforward;
3005 ip_mrouter_done = X_ip_mrouter_done;
3006 ip_mrouter_get = X_ip_mrouter_get;
3007 ip_mrouter_set = X_ip_mrouter_set;
3008 ip_rsvp_force_done = X_ip_rsvp_force_done;
3009 ip_rsvp_vif = X_ip_rsvp_vif;
3010 legal_vif_num = X_legal_vif_num;
3011 mrt_ioctl = X_mrt_ioctl;
3012 rsvp_input_p = X_rsvp_input;
3017 * Typically module unload happens after the user-level
3018 * process has shutdown the kernel services (the check
3019 * below insures someone can't just yank the module out
3020 * from under a running process). But if the module is
3021 * just loaded and then unloaded w/o starting up a user
3022 * process we still need to cleanup.
3027 if (pim_encap_cookie) {
3028 encap_detach(pim_encap_cookie);
3029 pim_encap_cookie = NULL;
3032 X_ip_mrouter_done();
3033 ip_mcast_src = NULL;
3035 ip_mrouter_done = NULL;
3036 ip_mrouter_get = NULL;
3037 ip_mrouter_set = NULL;
3038 ip_rsvp_force_done = NULL;
3040 legal_vif_num = NULL;
3042 rsvp_input_p = NULL;
3045 mtx_destroy(&mrouter_mtx);
3053 static moduledata_t ip_mroutemod = {
3058 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);