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"
65 #include <sys/param.h>
66 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #include <sys/module.h>
72 #include <sys/protosw.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
78 #include <sys/sysctl.h>
79 #include <sys/syslog.h>
80 #include <sys/systm.h>
83 #include <net/netisr.h>
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/igmp.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_encap.h>
91 #include <netinet/ip_mroute.h>
92 #include <netinet/ip_var.h>
93 #include <netinet/ip_options.h>
95 #include <netinet/pim.h>
96 #include <netinet/pim_var.h>
98 #include <netinet/udp.h>
99 #include <machine/in_cksum.h>
101 #include <security/mac/mac_framework.h>
104 * Control debugging code for rsvp and multicast routing code.
105 * Can only set them with the debugger.
107 static u_int rsvpdebug; /* non-zero enables debugging */
109 static u_int mrtdebug; /* any set of the flags below */
110 #define DEBUG_MFC 0x02
111 #define DEBUG_FORWARD 0x04
112 #define DEBUG_EXPIRE 0x08
113 #define DEBUG_XMIT 0x10
114 #define DEBUG_PIM 0x20
116 #define VIFI_INVALID ((vifi_t) -1)
118 #define M_HASCL(m) ((m)->m_flags & M_EXT)
120 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
123 * Locking. We use two locks: one for the virtual interface table and
124 * one for the forwarding table. These locks may be nested in which case
125 * the VIF lock must always be taken first. Note that each lock is used
126 * to cover not only the specific data structure but also related data
127 * structures. It may be better to add more fine-grained locking later;
128 * it's not clear how performance-critical this code is.
130 * XXX: This module could particularly benefit from being cleaned
131 * up to use the <sys/queue.h> macros.
135 static struct mrtstat mrtstat;
136 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
138 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
140 static struct mfc *mfctable[MFCTBLSIZ];
141 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
142 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
143 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
145 static struct mtx mfc_mtx;
146 #define MFC_LOCK() mtx_lock(&mfc_mtx)
147 #define MFC_UNLOCK() mtx_unlock(&mfc_mtx)
148 #define MFC_LOCK_ASSERT() do { \
149 mtx_assert(&mfc_mtx, MA_OWNED); \
150 NET_ASSERT_GIANT(); \
152 #define MFC_LOCK_INIT() mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
153 #define MFC_LOCK_DESTROY() mtx_destroy(&mfc_mtx)
155 static struct vif viftable[MAXVIFS];
156 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
157 &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
158 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
160 static struct mtx vif_mtx;
161 #define VIF_LOCK() mtx_lock(&vif_mtx)
162 #define VIF_UNLOCK() mtx_unlock(&vif_mtx)
163 #define VIF_LOCK_ASSERT() mtx_assert(&vif_mtx, MA_OWNED)
164 #define VIF_LOCK_INIT() mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
165 #define VIF_LOCK_DESTROY() mtx_destroy(&vif_mtx)
167 static u_char nexpire[MFCTBLSIZ];
169 static eventhandler_tag if_detach_event_tag = NULL;
171 static struct callout expire_upcalls_ch;
173 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
174 #define UPCALL_EXPIRE 6 /* number of timeouts */
177 * Define the token bucket filter structures
178 * tbftable -> each vif has one of these for storing info
181 static struct tbf tbftable[MAXVIFS];
182 #define TBF_REPROCESS (hz / 100) /* 100x / second */
185 * 'Interfaces' associated with decapsulator (so we can tell
186 * packets that went through it from ones that get reflected
187 * by a broken gateway). These interfaces are never linked into
188 * the system ifnet list & no routes point to them. I.e., packets
189 * can't be sent this way. They only exist as a placeholder for
190 * multicast source verification.
192 static struct ifnet multicast_decap_if[MAXVIFS];
195 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
197 /* prototype IP hdr for encapsulated packets */
198 static struct ip multicast_encap_iphdr = {
199 #if BYTE_ORDER == LITTLE_ENDIAN
200 sizeof(struct ip) >> 2, IPVERSION,
202 IPVERSION, sizeof(struct ip) >> 2,
205 sizeof(struct ip), /* total length */
208 ENCAP_TTL, ENCAP_PROTO,
213 * Bandwidth meter variables and constants
215 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
217 * Pending timeouts are stored in a hash table, the key being the
218 * expiration time. Periodically, the entries are analysed and processed.
220 #define BW_METER_BUCKETS 1024
221 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
222 static struct callout bw_meter_ch;
223 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
226 * Pending upcalls are stored in a vector which is flushed when
227 * full, or periodically
229 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
230 static u_int bw_upcalls_n; /* # of pending upcalls */
231 static struct callout bw_upcalls_ch;
232 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
235 static struct pimstat pimstat;
236 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
238 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
241 * Note: the PIM Register encapsulation adds the following in front of a
244 * struct pim_encap_hdr {
246 * struct pim_encap_pimhdr pim;
251 struct pim_encap_pimhdr {
256 static struct ip pim_encap_iphdr = {
257 #if BYTE_ORDER == LITTLE_ENDIAN
258 sizeof(struct ip) >> 2,
262 sizeof(struct ip) >> 2,
265 sizeof(struct ip), /* total length */
273 static struct pim_encap_pimhdr pim_encap_pimhdr = {
275 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
282 static struct ifnet multicast_register_if;
283 static vifi_t reg_vif_num = VIFI_INVALID;
289 static vifi_t numvifs;
290 static const struct encaptab *encap_cookie;
293 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
294 * given a datagram's src ip address.
296 static u_long last_encap_src;
297 static struct vif *last_encap_vif;
300 * Callout for queue processing.
302 static struct callout tbf_reprocess_ch;
304 static u_long X_ip_mcast_src(int vifi);
305 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp,
306 struct mbuf *m, struct ip_moptions *imo);
307 static int X_ip_mrouter_done(void);
308 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
309 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
310 static int X_legal_vif_num(int vif);
311 static int X_mrt_ioctl(int cmd, caddr_t data);
313 static int get_sg_cnt(struct sioc_sg_req *);
314 static int get_vif_cnt(struct sioc_vif_req *);
315 static void if_detached_event(void *arg __unused, struct ifnet *);
316 static int ip_mrouter_init(struct socket *, int);
317 static int add_vif(struct vifctl *);
318 static int del_vif_locked(vifi_t);
319 static int del_vif(vifi_t);
320 static int add_mfc(struct mfcctl2 *);
321 static int del_mfc(struct mfcctl2 *);
322 static int set_api_config(uint32_t *); /* chose API capabilities */
323 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
324 static int set_assert(int);
325 static void expire_upcalls(void *);
326 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
327 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
328 static void encap_send(struct ip *, struct vif *, struct mbuf *);
329 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
330 static void tbf_queue(struct vif *, struct mbuf *);
331 static void tbf_process_q(struct vif *);
332 static void tbf_reprocess_q(void *);
333 static int tbf_dq_sel(struct vif *, struct ip *);
334 static void tbf_send_packet(struct vif *, struct mbuf *);
335 static void tbf_update_tokens(struct vif *);
336 static int priority(struct vif *, struct ip *);
339 * Bandwidth monitoring
341 static void free_bw_list(struct bw_meter *list);
342 static int add_bw_upcall(struct bw_upcall *);
343 static int del_bw_upcall(struct bw_upcall *);
344 static void bw_meter_receive_packet(struct bw_meter *x, int plen,
345 struct timeval *nowp);
346 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
347 static void bw_upcalls_send(void);
348 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
349 static void unschedule_bw_meter(struct bw_meter *x);
350 static void bw_meter_process(void);
351 static void expire_bw_upcalls_send(void *);
352 static void expire_bw_meter_process(void *);
355 static int pim_register_send(struct ip *, struct vif *,
356 struct mbuf *, struct mfc *);
357 static int pim_register_send_rp(struct ip *, struct vif *,
358 struct mbuf *, struct mfc *);
359 static int pim_register_send_upcall(struct ip *, struct vif *,
360 struct mbuf *, struct mfc *);
361 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
365 * whether or not special PIM assert processing is enabled.
367 static int pim_assert;
369 * Rate limit for assert notification messages, in usec
371 #define ASSERT_MSG_TIME 3000000
374 * Kernel multicast routing API capabilities and setup.
375 * If more API capabilities are added to the kernel, they should be
376 * recorded in `mrt_api_support'.
378 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
379 MRT_MFC_FLAGS_BORDER_VIF |
382 static uint32_t mrt_api_config = 0;
385 * Hash function for a source, group entry
387 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
388 ((g) >> 20) ^ ((g) >> 10) ^ (g))
391 * Find a route for a given origin IP address and Multicast group address
392 * Type of service parameter to be added in the future!!!
393 * Statistics are updated by the caller if needed
394 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
397 mfc_find(in_addr_t o, in_addr_t g)
403 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
404 if ((rt->mfc_origin.s_addr == o) &&
405 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
411 * Macros to compute elapsed time efficiently
412 * Borrowed from Van Jacobson's scheduling code
414 #define TV_DELTA(a, b, delta) { \
416 delta = (a).tv_usec - (b).tv_usec; \
417 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
426 delta += (1000000 * xxs); \
431 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
432 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
435 * Handle MRT setsockopt commands to modify the multicast routing tables.
438 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
444 struct bw_upcall bw_upcall;
447 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
451 switch (sopt->sopt_name) {
453 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
456 error = ip_mrouter_init(so, optval);
460 error = ip_mrouter_done();
464 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
467 error = add_vif(&vifc);
471 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
474 error = del_vif(vifi);
480 * select data size depending on API version.
482 if (sopt->sopt_name == MRT_ADD_MFC &&
483 mrt_api_config & MRT_API_FLAGS_ALL) {
484 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
485 sizeof(struct mfcctl2));
487 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
488 sizeof(struct mfcctl));
489 bzero((caddr_t)&mfc + sizeof(struct mfcctl),
490 sizeof(mfc) - sizeof(struct mfcctl));
494 if (sopt->sopt_name == MRT_ADD_MFC)
495 error = add_mfc(&mfc);
497 error = del_mfc(&mfc);
501 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
508 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
510 error = set_api_config(&i);
512 error = sooptcopyout(sopt, &i, sizeof i);
515 case MRT_ADD_BW_UPCALL:
516 case MRT_DEL_BW_UPCALL:
517 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
521 if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
522 error = add_bw_upcall(&bw_upcall);
524 error = del_bw_upcall(&bw_upcall);
535 * Handle MRT getsockopt commands
538 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
541 static int version = 0x0305; /* !!! why is this here? XXX */
543 switch (sopt->sopt_name) {
545 error = sooptcopyout(sopt, &version, sizeof version);
549 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
552 case MRT_API_SUPPORT:
553 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
557 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
568 * Handle ioctl commands to obtain information from the cache
571 X_mrt_ioctl(int cmd, caddr_t data)
576 * Currently the only function calling this ioctl routine is rtioctl().
577 * Typically, only root can create the raw socket in order to execute
578 * this ioctl method, however the request might be coming from a prison
580 error = priv_check(curthread, PRIV_NETINET_MROUTE);
584 case (SIOCGETVIFCNT):
585 error = get_vif_cnt((struct sioc_vif_req *)data);
589 error = get_sg_cnt((struct sioc_sg_req *)data);
600 * returns the packet, byte, rpf-failure count for the source group provided
603 get_sg_cnt(struct sioc_sg_req *req)
608 rt = mfc_find(req->src.s_addr, req->grp.s_addr);
611 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
612 return EADDRNOTAVAIL;
614 req->pktcnt = rt->mfc_pkt_cnt;
615 req->bytecnt = rt->mfc_byte_cnt;
616 req->wrong_if = rt->mfc_wrong_if;
622 * returns the input and output packet and byte counts on the vif provided
625 get_vif_cnt(struct sioc_vif_req *req)
627 vifi_t vifi = req->vifi;
630 if (vifi >= numvifs) {
635 req->icount = viftable[vifi].v_pkt_in;
636 req->ocount = viftable[vifi].v_pkt_out;
637 req->ibytes = viftable[vifi].v_bytes_in;
638 req->obytes = viftable[vifi].v_bytes_out;
645 ip_mrouter_reset(void)
647 bzero((caddr_t)mfctable, sizeof(mfctable));
648 bzero((caddr_t)nexpire, sizeof(nexpire));
653 callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
656 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
657 callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
658 callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
660 callout_init(&tbf_reprocess_ch, NET_CALLOUT_MPSAFE);
663 static struct mtx mrouter_mtx; /* used to synch init/done work */
666 if_detached_event(void *arg __unused, struct ifnet *ifp)
672 struct mfc **ppmfc; /* Pointer to previous node's next-pointer */
676 mtx_lock(&mrouter_mtx);
677 if (ip_mrouter == NULL) {
678 mtx_unlock(&mrouter_mtx);
682 * Tear down multicast forwarder state associated with this ifnet.
683 * 1. Walk the vif list, matching vifs against this ifnet.
684 * 2. Walk the multicast forwarding cache (mfc) looking for
685 * inner matches with this vif's index.
686 * 3. Free any pending mbufs for this mfc.
687 * 4. Free the associated mfc entry and state associated with this vif.
688 * Be very careful about unlinking from a singly-linked list whose
689 * "head node" is a pointer in a simple array.
690 * 5. Free vif state. This should disable ALLMULTI on the interface.
694 for (vifi = 0; vifi < numvifs; vifi++) {
695 if (viftable[vifi].v_ifp != ifp)
697 for (i = 0; i < MFCTBLSIZ; i++) {
698 ppmfc = &mfctable[i];
699 for (mfc = mfctable[i]; mfc != NULL; ) {
700 nmfc = mfc->mfc_next;
701 if (mfc->mfc_parent == vifi) {
702 for (pq = mfc->mfc_stall; pq != NULL; ) {
708 free_bw_list(mfc->mfc_bw_meter);
709 free(mfc, M_MRTABLE);
712 ppmfc = &mfc->mfc_next;
717 del_vif_locked(vifi);
722 mtx_unlock(&mrouter_mtx);
726 * Enable multicast routing
729 ip_mrouter_init(struct socket *so, int version)
732 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
733 so->so_type, so->so_proto->pr_protocol);
735 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
741 mtx_lock(&mrouter_mtx);
743 if (ip_mrouter != NULL) {
744 mtx_unlock(&mrouter_mtx);
748 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
749 if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
750 if (if_detach_event_tag == NULL)
753 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
755 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
756 expire_bw_upcalls_send, NULL);
757 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
761 mtx_unlock(&mrouter_mtx);
764 log(LOG_DEBUG, "ip_mrouter_init\n");
770 * Disable multicast routing
773 X_ip_mrouter_done(void)
782 mtx_lock(&mrouter_mtx);
784 if (ip_mrouter == NULL) {
785 mtx_unlock(&mrouter_mtx);
790 * Detach/disable hooks to the reset of the system.
797 const struct encaptab *c = encap_cookie;
803 callout_stop(&tbf_reprocess_ch);
807 * For each phyint in use, disable promiscuous reception of all IP
810 for (vifi = 0; vifi < numvifs; vifi++) {
811 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
812 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
813 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
815 so->sin_len = sizeof(struct sockaddr_in);
816 so->sin_family = AF_INET;
817 so->sin_addr.s_addr = INADDR_ANY;
818 ifp = viftable[vifi].v_ifp;
822 bzero((caddr_t)tbftable, sizeof(tbftable));
823 bzero((caddr_t)viftable, sizeof(viftable));
827 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
830 * Free all multicast forwarding cache entries.
832 callout_stop(&expire_upcalls_ch);
833 callout_stop(&bw_upcalls_ch);
834 callout_stop(&bw_meter_ch);
837 for (i = 0; i < MFCTBLSIZ; i++) {
838 for (rt = mfctable[i]; rt != NULL; ) {
839 struct mfc *nr = rt->mfc_next;
841 for (rte = rt->mfc_stall; rte != NULL; ) {
842 struct rtdetq *n = rte->next;
845 free(rte, M_MRTABLE);
848 free_bw_list(rt->mfc_bw_meter);
853 bzero((caddr_t)mfctable, sizeof(mfctable));
854 bzero((caddr_t)nexpire, sizeof(nexpire));
856 bzero(bw_meter_timers, sizeof(bw_meter_timers));
860 * Reset de-encapsulation cache
862 last_encap_src = INADDR_ANY;
863 last_encap_vif = NULL;
865 reg_vif_num = VIFI_INVALID;
868 mtx_unlock(&mrouter_mtx);
871 log(LOG_DEBUG, "ip_mrouter_done\n");
877 * Set PIM assert processing global
882 if ((i != 1) && (i != 0))
891 * Configure API capabilities
894 set_api_config(uint32_t *apival)
899 * We can set the API capabilities only if it is the first operation
900 * after MRT_INIT. I.e.:
901 * - there are no vifs installed
902 * - pim_assert is not enabled
903 * - the MFC table is empty
913 for (i = 0; i < MFCTBLSIZ; i++) {
914 if (mfctable[i] != NULL) {
920 mrt_api_config = *apival & mrt_api_support;
921 *apival = mrt_api_config;
927 * Decide if a packet is from a tunnelled peer.
928 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
931 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
933 struct ip *ip = mtod(m, struct ip *);
934 int hlen = ip->ip_hl << 2;
937 * don't claim the packet if it's not to a multicast destination or if
938 * we don't have an encapsulating tunnel with the source.
939 * Note: This code assumes that the remote site IP address
940 * uniquely identifies the tunnel (i.e., that this site has
941 * at most one tunnel with the remote site).
943 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
945 if (ip->ip_src.s_addr != last_encap_src) {
946 struct vif *vifp = viftable;
947 struct vif *vife = vifp + numvifs;
949 last_encap_src = ip->ip_src.s_addr;
950 last_encap_vif = NULL;
951 for ( ; vifp < vife; ++vifp)
952 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
953 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
954 last_encap_vif = vifp;
958 if (last_encap_vif == NULL) {
959 last_encap_src = INADDR_ANY;
966 * De-encapsulate a packet and feed it back through ip input (this
967 * routine is called whenever IP gets a packet that mroute_encap_func()
971 mroute_encap_input(struct mbuf *m, int off)
973 struct ip *ip = mtod(m, struct ip *);
974 int hlen = ip->ip_hl << 2;
976 if (hlen > sizeof(struct ip))
977 ip_stripoptions(m, (struct mbuf *) 0);
978 m->m_data += sizeof(struct ip);
979 m->m_len -= sizeof(struct ip);
980 m->m_pkthdr.len -= sizeof(struct ip);
982 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
984 netisr_queue(NETISR_IP, m); /* mbuf is free'd on failure. */
986 * normally we would need a "schednetisr(NETISR_IP)"
987 * here but we were called by ip_input and it is going
988 * to loop back & try to dequeue the packet we just
989 * queued as soon as we return so we avoid the
990 * unnecessary software interrrupt.
993 * This no longer holds - we may have direct-dispatched the packet,
994 * or there may be a queue processing limit.
998 extern struct domain inetdomain;
999 static struct protosw mroute_encap_protosw =
1001 .pr_type = SOCK_RAW,
1002 .pr_domain = &inetdomain,
1003 .pr_protocol = IPPROTO_IPV4,
1004 .pr_flags = PR_ATOMIC|PR_ADDR,
1005 .pr_input = mroute_encap_input,
1006 .pr_ctloutput = rip_ctloutput,
1007 .pr_usrreqs = &rip_usrreqs
1011 * Add a vif to the vif table
1014 add_vif(struct vifctl *vifcp)
1016 struct vif *vifp = viftable + vifcp->vifc_vifi;
1017 struct sockaddr_in sin = {sizeof sin, AF_INET};
1021 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
1024 if (vifcp->vifc_vifi >= MAXVIFS) {
1028 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
1032 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
1034 return EADDRNOTAVAIL;
1037 /* Find the interface with an address in AF_INET family */
1039 if (vifcp->vifc_flags & VIFF_REGISTER) {
1041 * XXX: Because VIFF_REGISTER does not really need a valid
1042 * local interface (e.g. it could be 127.0.0.2), we don't
1043 * check its address.
1049 sin.sin_addr = vifcp->vifc_lcl_addr;
1050 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
1053 return EADDRNOTAVAIL;
1058 if (vifcp->vifc_flags & VIFF_TUNNEL) {
1059 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
1061 * An encapsulating tunnel is wanted. Tell
1062 * mroute_encap_input() to start paying attention
1063 * to encapsulated packets.
1065 if (encap_cookie == NULL) {
1068 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
1070 (struct protosw *)&mroute_encap_protosw, NULL);
1072 if (encap_cookie == NULL) {
1073 printf("ip_mroute: unable to attach encap\n");
1075 return EIO; /* XXX */
1077 for (i = 0; i < MAXVIFS; ++i) {
1078 if_initname(&multicast_decap_if[i], "mdecap", i);
1082 * Set interface to fake encapsulator interface
1084 ifp = &multicast_decap_if[vifcp->vifc_vifi];
1086 * Prepare cached route entry
1088 bzero(&vifp->v_route, sizeof(vifp->v_route));
1090 log(LOG_ERR, "source routed tunnels not supported\n");
1095 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
1096 ifp = &multicast_register_if;
1098 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
1099 (void *)&multicast_register_if);
1100 if (reg_vif_num == VIFI_INVALID) {
1101 if_initname(&multicast_register_if, "register_vif", 0);
1102 multicast_register_if.if_flags = IFF_LOOPBACK;
1103 bzero(&vifp->v_route, sizeof(vifp->v_route));
1104 reg_vif_num = vifcp->vifc_vifi;
1107 } else { /* Make sure the interface supports multicast */
1108 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1113 /* Enable promiscuous reception of all IP multicasts from the if */
1114 error = if_allmulti(ifp, 1);
1121 /* define parameters for the tbf structure */
1122 vifp->v_tbf = v_tbf;
1123 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
1124 vifp->v_tbf->tbf_n_tok = 0;
1125 vifp->v_tbf->tbf_q_len = 0;
1126 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
1127 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
1129 vifp->v_flags = vifcp->vifc_flags;
1130 vifp->v_threshold = vifcp->vifc_threshold;
1131 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1132 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1134 /* scaling up here allows division by 1024 in critical code */
1135 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
1136 vifp->v_rsvp_on = 0;
1137 vifp->v_rsvpd = NULL;
1138 /* initialize per vif pkt counters */
1140 vifp->v_pkt_out = 0;
1141 vifp->v_bytes_in = 0;
1142 vifp->v_bytes_out = 0;
1144 /* Adjust numvifs up if the vifi is higher than numvifs */
1145 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
1150 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
1152 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
1153 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1154 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
1155 vifcp->vifc_threshold,
1156 vifcp->vifc_rate_limit);
1162 * Delete a vif from the vif table
1165 del_vif_locked(vifi_t vifi)
1171 if (vifi >= numvifs) {
1174 vifp = &viftable[vifi];
1175 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1176 return EADDRNOTAVAIL;
1179 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1180 if_allmulti(vifp->v_ifp, 0);
1182 if (vifp == last_encap_vif) {
1183 last_encap_vif = NULL;
1184 last_encap_src = INADDR_ANY;
1188 * Free packets queued at the interface
1190 while (vifp->v_tbf->tbf_q) {
1191 struct mbuf *m = vifp->v_tbf->tbf_q;
1193 vifp->v_tbf->tbf_q = m->m_act;
1198 if (vifp->v_flags & VIFF_REGISTER)
1199 reg_vif_num = VIFI_INVALID;
1202 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
1203 bzero((caddr_t)vifp, sizeof (*vifp));
1206 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1208 /* Adjust numvifs down */
1209 for (vifi = numvifs; vifi > 0; vifi--)
1210 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1218 del_vif(vifi_t vifi)
1223 cc = del_vif_locked(vifi);
1230 * update an mfc entry without resetting counters and S,G addresses.
1233 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1237 rt->mfc_parent = mfccp->mfcc_parent;
1238 for (i = 0; i < numvifs; i++) {
1239 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1240 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1243 /* set the RP address */
1244 if (mrt_api_config & MRT_MFC_RP)
1245 rt->mfc_rp = mfccp->mfcc_rp;
1247 rt->mfc_rp.s_addr = INADDR_ANY;
1251 * fully initialize an mfc entry from the parameter.
1254 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1256 rt->mfc_origin = mfccp->mfcc_origin;
1257 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1259 update_mfc_params(rt, mfccp);
1261 /* initialize pkt counters per src-grp */
1262 rt->mfc_pkt_cnt = 0;
1263 rt->mfc_byte_cnt = 0;
1264 rt->mfc_wrong_if = 0;
1265 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1273 add_mfc(struct mfcctl2 *mfccp)
1283 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1285 /* If an entry already exists, just update the fields */
1287 if (mrtdebug & DEBUG_MFC)
1288 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1289 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1290 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1291 mfccp->mfcc_parent);
1293 update_mfc_params(rt, mfccp);
1300 * Find the entry for which the upcall was made and update
1302 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1303 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1305 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1306 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1307 (rt->mfc_stall != NULL)) {
1310 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1311 "multiple kernel entries",
1312 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1313 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1314 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1316 if (mrtdebug & DEBUG_MFC)
1317 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1318 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1319 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1320 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1322 init_mfc_params(rt, mfccp);
1324 rt->mfc_expire = 0; /* Don't clean this guy up */
1327 /* free packets Qed at the end of this entry */
1328 for (rte = rt->mfc_stall; rte != NULL; ) {
1329 struct rtdetq *n = rte->next;
1331 ip_mdq(rte->m, rte->ifp, rt, -1);
1333 free(rte, M_MRTABLE);
1336 rt->mfc_stall = NULL;
1341 * It is possible that an entry is being inserted without an upcall
1344 if (mrtdebug & DEBUG_MFC)
1345 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1346 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1347 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1348 mfccp->mfcc_parent);
1350 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1351 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1352 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1353 init_mfc_params(rt, mfccp);
1360 if (rt == NULL) { /* no upcall, so make a new entry */
1361 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1368 init_mfc_params(rt, mfccp);
1370 rt->mfc_stall = NULL;
1372 rt->mfc_bw_meter = NULL;
1373 /* insert new entry at head of hash chain */
1374 rt->mfc_next = mfctable[hash];
1375 mfctable[hash] = rt;
1384 * Delete an mfc entry
1387 del_mfc(struct mfcctl2 *mfccp)
1389 struct in_addr origin;
1390 struct in_addr mcastgrp;
1394 struct bw_meter *list;
1396 origin = mfccp->mfcc_origin;
1397 mcastgrp = mfccp->mfcc_mcastgrp;
1399 if (mrtdebug & DEBUG_MFC)
1400 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1401 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1405 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1406 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1407 if (origin.s_addr == rt->mfc_origin.s_addr &&
1408 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1409 rt->mfc_stall == NULL)
1413 return EADDRNOTAVAIL;
1416 *nptr = rt->mfc_next;
1419 * free the bw_meter entries
1421 list = rt->mfc_bw_meter;
1422 rt->mfc_bw_meter = NULL;
1424 free(rt, M_MRTABLE);
1434 * Send a message to the routing daemon on the multicast routing socket
1437 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1440 SOCKBUF_LOCK(&s->so_rcv);
1441 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1443 sorwakeup_locked(s);
1446 SOCKBUF_UNLOCK(&s->so_rcv);
1453 * IP multicast forwarding function. This function assumes that the packet
1454 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1455 * pointed to by "ifp", and the packet is to be relayed to other networks
1456 * that have members of the packet's destination IP multicast group.
1458 * The packet is returned unscathed to the caller, unless it is
1459 * erroneous, in which case a non-zero return value tells the caller to
1463 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1466 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1467 struct ip_moptions *imo)
1473 if (mrtdebug & DEBUG_FORWARD)
1474 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1475 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1478 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1479 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1481 * Packet arrived via a physical interface or
1482 * an encapsulated tunnel or a register_vif.
1486 * Packet arrived through a source-route tunnel.
1487 * Source-route tunnels are no longer supported.
1489 static int last_log;
1490 if (last_log != time_uptime) {
1491 last_log = time_uptime;
1493 "ip_mforward: received source-routed packet from %lx\n",
1494 (u_long)ntohl(ip->ip_src.s_addr));
1501 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1502 if (ip->ip_ttl < 255)
1503 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1504 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1505 struct vif *vifp = viftable + vifi;
1507 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1508 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1510 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1511 vifp->v_ifp->if_xname);
1513 error = ip_mdq(m, ifp, NULL, vifi);
1518 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1519 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1520 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1522 printf("In fact, no options were specified at all\n");
1526 * Don't forward a packet with time-to-live of zero or one,
1527 * or a packet destined to a local-only group.
1529 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1536 * Determine forwarding vifs from the forwarding cache table
1538 ++mrtstat.mrts_mfc_lookups;
1539 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1541 /* Entry exists, so forward if necessary */
1543 error = ip_mdq(m, ifp, rt, -1);
1549 * If we don't have a route for packet's origin,
1550 * Make a copy of the packet & send message to routing daemon
1556 int hlen = ip->ip_hl << 2;
1558 ++mrtstat.mrts_mfc_misses;
1560 mrtstat.mrts_no_route++;
1561 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1562 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1563 (u_long)ntohl(ip->ip_src.s_addr),
1564 (u_long)ntohl(ip->ip_dst.s_addr));
1567 * Allocate mbufs early so that we don't do extra work if we are
1568 * just going to fail anyway. Make sure to pullup the header so
1569 * that other people can't step on it.
1571 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1577 mb0 = m_copypacket(m, M_DONTWAIT);
1578 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1579 mb0 = m_pullup(mb0, hlen);
1581 free(rte, M_MRTABLE);
1587 /* is there an upcall waiting for this flow ? */
1588 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1589 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1590 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1591 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1592 (rt->mfc_stall != NULL))
1599 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1603 * Locate the vifi for the incoming interface for this packet.
1604 * If none found, drop packet.
1606 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1608 if (vifi >= numvifs) /* vif not found, drop packet */
1611 /* no upcall, so make a new entry */
1612 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1615 /* Make a copy of the header to send to the user level process */
1616 mm = m_copy(mb0, 0, hlen);
1621 * Send message to routing daemon to install
1622 * a route into the kernel table
1625 im = mtod(mm, struct igmpmsg *);
1626 im->im_msgtype = IGMPMSG_NOCACHE;
1630 mrtstat.mrts_upcalls++;
1632 k_igmpsrc.sin_addr = ip->ip_src;
1633 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1634 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1635 ++mrtstat.mrts_upq_sockfull;
1637 free(rt, M_MRTABLE);
1639 free(rte, M_MRTABLE);
1646 /* insert new entry at head of hash chain */
1647 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1648 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1649 rt->mfc_expire = UPCALL_EXPIRE;
1651 for (i = 0; i < numvifs; i++) {
1652 rt->mfc_ttls[i] = 0;
1653 rt->mfc_flags[i] = 0;
1655 rt->mfc_parent = -1;
1657 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1659 rt->mfc_bw_meter = NULL;
1661 /* link into table */
1662 rt->mfc_next = mfctable[hash];
1663 mfctable[hash] = rt;
1664 rt->mfc_stall = rte;
1667 /* determine if q has overflowed */
1672 * XXX ouch! we need to append to the list, but we
1673 * only have a pointer to the front, so we have to
1674 * scan the entire list every time.
1676 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1679 if (npkts > MAX_UPQ) {
1680 mrtstat.mrts_upq_ovflw++;
1682 free(rte, M_MRTABLE);
1689 /* Add this entry to the end of the queue */
1705 * Clean up the cache entry if upcall is not serviced
1708 expire_upcalls(void *unused)
1711 struct mfc *mfc, **nptr;
1715 for (i = 0; i < MFCTBLSIZ; i++) {
1716 if (nexpire[i] == 0)
1718 nptr = &mfctable[i];
1719 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1721 * Skip real cache entries
1722 * Make sure it wasn't marked to not expire (shouldn't happen)
1725 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1726 --mfc->mfc_expire == 0) {
1727 if (mrtdebug & DEBUG_EXPIRE)
1728 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1729 (u_long)ntohl(mfc->mfc_origin.s_addr),
1730 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1732 * drop all the packets
1733 * free the mbuf with the pkt, if, timing info
1735 for (rte = mfc->mfc_stall; rte; ) {
1736 struct rtdetq *n = rte->next;
1739 free(rte, M_MRTABLE);
1742 ++mrtstat.mrts_cache_cleanups;
1746 * free the bw_meter entries
1748 while (mfc->mfc_bw_meter != NULL) {
1749 struct bw_meter *x = mfc->mfc_bw_meter;
1751 mfc->mfc_bw_meter = x->bm_mfc_next;
1755 *nptr = mfc->mfc_next;
1756 free(mfc, M_MRTABLE);
1758 nptr = &mfc->mfc_next;
1764 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1768 * Packet forwarding routine once entry in the cache is made
1771 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1773 struct ip *ip = mtod(m, struct ip *);
1775 int plen = ip->ip_len;
1779 * Macro to send packet on vif. Since RSVP packets don't get counted on
1780 * input, they shouldn't get counted on output, so statistics keeping is
1783 #define MC_SEND(ip,vifp,m) { \
1784 if ((vifp)->v_flags & VIFF_TUNNEL) \
1785 encap_send((ip), (vifp), (m)); \
1787 phyint_send((ip), (vifp), (m)); \
1791 * If xmt_vif is not -1, send on only the requested vif.
1793 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1795 if (xmt_vif < numvifs) {
1797 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1798 pim_register_send(ip, viftable + xmt_vif, m, rt);
1801 MC_SEND(ip, viftable + xmt_vif, m);
1806 * Don't forward if it didn't arrive from the parent vif for its origin.
1808 vifi = rt->mfc_parent;
1809 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1810 /* came in the wrong interface */
1811 if (mrtdebug & DEBUG_FORWARD)
1812 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1813 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1814 ++mrtstat.mrts_wrong_if;
1817 * If we are doing PIM assert processing, send a message
1818 * to the routing daemon.
1820 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1821 * can complete the SPT switch, regardless of the type
1822 * of the iif (broadcast media, GRE tunnel, etc).
1824 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1829 if (ifp == &multicast_register_if)
1830 pimstat.pims_rcv_registers_wrongiif++;
1833 /* Get vifi for the incoming packet */
1834 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1836 if (vifi >= numvifs)
1837 return 0; /* The iif is not found: ignore the packet. */
1839 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1840 return 0; /* WRONGVIF disabled: ignore the packet */
1844 TV_DELTA(now, rt->mfc_last_assert, delta);
1846 if (delta > ASSERT_MSG_TIME) {
1847 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1849 int hlen = ip->ip_hl << 2;
1850 struct mbuf *mm = m_copy(m, 0, hlen);
1852 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1853 mm = m_pullup(mm, hlen);
1857 rt->mfc_last_assert = now;
1859 im = mtod(mm, struct igmpmsg *);
1860 im->im_msgtype = IGMPMSG_WRONGVIF;
1864 mrtstat.mrts_upcalls++;
1866 k_igmpsrc.sin_addr = im->im_src;
1867 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1869 "ip_mforward: ip_mrouter socket queue full\n");
1870 ++mrtstat.mrts_upq_sockfull;
1878 /* If I sourced this packet, it counts as output, else it was input. */
1879 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1880 viftable[vifi].v_pkt_out++;
1881 viftable[vifi].v_bytes_out += plen;
1883 viftable[vifi].v_pkt_in++;
1884 viftable[vifi].v_bytes_in += plen;
1887 rt->mfc_byte_cnt += plen;
1890 * For each vif, decide if a copy of the packet should be forwarded.
1892 * - the ttl exceeds the vif's threshold
1893 * - there are group members downstream on interface
1895 for (vifi = 0; vifi < numvifs; vifi++)
1896 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1897 viftable[vifi].v_pkt_out++;
1898 viftable[vifi].v_bytes_out += plen;
1900 if (viftable[vifi].v_flags & VIFF_REGISTER)
1901 pim_register_send(ip, viftable + vifi, m, rt);
1904 MC_SEND(ip, viftable+vifi, m);
1908 * Perform upcall-related bw measuring.
1910 if (rt->mfc_bw_meter != NULL) {
1916 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1917 bw_meter_receive_packet(x, plen, &now);
1924 * check if a vif number is legal/ok. This is used by ip_output.
1927 X_legal_vif_num(int vif)
1929 /* XXX unlocked, matter? */
1930 return (vif >= 0 && vif < numvifs);
1934 * Return the local address used by this vif
1937 X_ip_mcast_src(int vifi)
1939 /* XXX unlocked, matter? */
1940 if (vifi >= 0 && vifi < numvifs)
1941 return viftable[vifi].v_lcl_addr.s_addr;
1947 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1949 struct mbuf *mb_copy;
1950 int hlen = ip->ip_hl << 2;
1955 * Make a new reference to the packet; make sure that
1956 * the IP header is actually copied, not just referenced,
1957 * so that ip_output() only scribbles on the copy.
1959 mb_copy = m_copypacket(m, M_DONTWAIT);
1960 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1961 mb_copy = m_pullup(mb_copy, hlen);
1962 if (mb_copy == NULL)
1965 if (vifp->v_rate_limit == 0)
1966 tbf_send_packet(vifp, mb_copy);
1968 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1972 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1974 struct mbuf *mb_copy;
1976 int i, len = ip->ip_len;
1980 /* Take care of delayed checksums */
1981 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1982 in_delayed_cksum(m);
1983 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1987 * copy the old packet & pullup its IP header into the
1988 * new mbuf so we can modify it. Try to fill the new
1989 * mbuf since if we don't the ethernet driver will.
1991 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1992 if (mb_copy == NULL)
1995 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1997 mb_copy->m_data += max_linkhdr;
1998 mb_copy->m_len = sizeof(multicast_encap_iphdr);
2000 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
2004 i = MHLEN - M_LEADINGSPACE(mb_copy);
2007 mb_copy = m_pullup(mb_copy, i);
2008 if (mb_copy == NULL)
2010 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
2013 * fill in the encapsulating IP header.
2015 ip_copy = mtod(mb_copy, struct ip *);
2016 *ip_copy = multicast_encap_iphdr;
2017 ip_copy->ip_id = ip_newid();
2018 ip_copy->ip_len += len;
2019 ip_copy->ip_src = vifp->v_lcl_addr;
2020 ip_copy->ip_dst = vifp->v_rmt_addr;
2023 * turn the encapsulated IP header back into a valid one.
2025 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
2027 ip->ip_len = htons(ip->ip_len);
2028 ip->ip_off = htons(ip->ip_off);
2030 mb_copy->m_data += sizeof(multicast_encap_iphdr);
2031 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2032 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
2034 if (vifp->v_rate_limit == 0)
2035 tbf_send_packet(vifp, mb_copy);
2037 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
2041 * Token bucket filter module
2045 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
2047 struct tbf *t = vifp->v_tbf;
2051 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
2052 mrtstat.mrts_pkt2large++;
2057 tbf_update_tokens(vifp);
2059 if (t->tbf_q_len == 0) { /* queue empty... */
2060 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
2061 t->tbf_n_tok -= p_len;
2062 tbf_send_packet(vifp, m);
2063 } else { /* no, queue packet and try later */
2065 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS,
2066 tbf_reprocess_q, vifp);
2068 } else if (t->tbf_q_len < t->tbf_max_q_len) {
2069 /* finite queue length, so queue pkts and process queue */
2071 tbf_process_q(vifp);
2073 /* queue full, try to dq and queue and process */
2074 if (!tbf_dq_sel(vifp, ip)) {
2075 mrtstat.mrts_q_overflow++;
2079 tbf_process_q(vifp);
2085 * adds a packet to the queue at the interface
2088 tbf_queue(struct vif *vifp, struct mbuf *m)
2090 struct tbf *t = vifp->v_tbf;
2094 if (t->tbf_t == NULL) /* Queue was empty */
2096 else /* Insert at tail */
2097 t->tbf_t->m_act = m;
2099 t->tbf_t = m; /* Set new tail pointer */
2102 /* Make sure we didn't get fed a bogus mbuf */
2104 panic("tbf_queue: m_act");
2112 * processes the queue at the interface
2115 tbf_process_q(struct vif *vifp)
2117 struct tbf *t = vifp->v_tbf;
2121 /* loop through the queue at the interface and send as many packets
2124 while (t->tbf_q_len > 0) {
2125 struct mbuf *m = t->tbf_q;
2126 int len = mtod(m, struct ip *)->ip_len;
2128 /* determine if the packet can be sent */
2129 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
2131 /* ok, reduce no of tokens, dequeue and send the packet. */
2132 t->tbf_n_tok -= len;
2134 t->tbf_q = m->m_act;
2135 if (--t->tbf_q_len == 0)
2139 tbf_send_packet(vifp, m);
2144 tbf_reprocess_q(void *xvifp)
2146 struct vif *vifp = xvifp;
2148 if (ip_mrouter == NULL)
2151 tbf_update_tokens(vifp);
2152 tbf_process_q(vifp);
2153 if (vifp->v_tbf->tbf_q_len)
2154 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS, tbf_reprocess_q, vifp);
2158 /* function that will selectively discard a member of the queue
2159 * based on the precedence value and the priority
2162 tbf_dq_sel(struct vif *vifp, struct ip *ip)
2165 struct mbuf *m, *last;
2167 struct tbf *t = vifp->v_tbf;
2171 p = priority(vifp, ip);
2175 while ((m = *np) != NULL) {
2176 if (p > priority(vifp, mtod(m, struct ip *))) {
2178 /* If we're removing the last packet, fix the tail pointer */
2182 /* It's impossible for the queue to be empty, but check anyways. */
2183 if (--t->tbf_q_len == 0)
2185 mrtstat.mrts_drop_sel++;
2195 tbf_send_packet(struct vif *vifp, struct mbuf *m)
2199 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
2200 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
2202 struct ip_moptions imo;
2203 struct in_multi *imm[2];
2205 static struct route ro; /* XXX check this */
2207 imo.imo_multicast_ifp = vifp->v_ifp;
2208 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2209 imo.imo_multicast_loop = 1;
2210 imo.imo_multicast_vif = -1;
2211 imo.imo_num_memberships = 0;
2212 imo.imo_max_memberships = 2;
2213 imo.imo_membership = &imm[0];
2216 * Re-entrancy should not be a problem here, because
2217 * the packets that we send out and are looped back at us
2218 * should get rejected because they appear to come from
2219 * the loopback interface, thus preventing looping.
2221 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
2223 if (mrtdebug & DEBUG_XMIT)
2224 log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
2225 vifp - viftable, error);
2229 /* determine the current time and then
2230 * the elapsed time (between the last time and time now)
2231 * in milliseconds & update the no. of tokens in the bucket
2234 tbf_update_tokens(struct vif *vifp)
2238 struct tbf *t = vifp->v_tbf;
2244 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2247 * This formula is actually
2248 * "time in seconds" * "bytes/second".
2250 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2252 * The (1000/1024) was introduced in add_vif to optimize
2253 * this divide into a shift.
2255 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2256 t->tbf_last_pkt_t = tp;
2258 if (t->tbf_n_tok > MAX_BKT_SIZE)
2259 t->tbf_n_tok = MAX_BKT_SIZE;
2263 priority(struct vif *vifp, struct ip *ip)
2265 int prio = 50; /* the lowest priority -- default case */
2267 /* temporary hack; may add general packet classifier some day */
2270 * The UDP port space is divided up into four priority ranges:
2271 * [0, 16384) : unclassified - lowest priority
2272 * [16384, 32768) : audio - highest priority
2273 * [32768, 49152) : whiteboard - medium priority
2274 * [49152, 65536) : video - low priority
2276 * Everything else gets lowest priority.
2278 if (ip->ip_p == IPPROTO_UDP) {
2279 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2280 switch (ntohs(udp->uh_dport) & 0xc000) {
2296 * End of token bucket filter modifications
2300 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2304 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2307 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2313 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2315 return EADDRNOTAVAIL;
2318 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2319 /* Check if socket is available. */
2320 if (viftable[vifi].v_rsvpd != NULL) {
2325 viftable[vifi].v_rsvpd = so;
2326 /* This may seem silly, but we need to be sure we don't over-increment
2327 * the RSVP counter, in case something slips up.
2329 if (!viftable[vifi].v_rsvp_on) {
2330 viftable[vifi].v_rsvp_on = 1;
2333 } else { /* must be VIF_OFF */
2335 * XXX as an additional consistency check, one could make sure
2336 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2337 * first parameter is pretty useless.
2339 viftable[vifi].v_rsvpd = NULL;
2341 * This may seem silly, but we need to be sure we don't over-decrement
2342 * the RSVP counter, in case something slips up.
2344 if (viftable[vifi].v_rsvp_on) {
2345 viftable[vifi].v_rsvp_on = 0;
2354 X_ip_rsvp_force_done(struct socket *so)
2358 /* Don't bother if it is not the right type of socket. */
2359 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2364 /* The socket may be attached to more than one vif...this
2365 * is perfectly legal.
2367 for (vifi = 0; vifi < numvifs; vifi++) {
2368 if (viftable[vifi].v_rsvpd == so) {
2369 viftable[vifi].v_rsvpd = NULL;
2370 /* This may seem silly, but we need to be sure we don't
2371 * over-decrement the RSVP counter, in case something slips up.
2373 if (viftable[vifi].v_rsvp_on) {
2374 viftable[vifi].v_rsvp_on = 0;
2384 X_rsvp_input(struct mbuf *m, int off)
2387 struct ip *ip = mtod(m, struct ip *);
2388 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2392 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2394 /* Can still get packets with rsvp_on = 0 if there is a local member
2395 * of the group to which the RSVP packet is addressed. But in this
2396 * case we want to throw the packet away.
2404 printf("rsvp_input: check vifs\n");
2410 ifp = m->m_pkthdr.rcvif;
2413 /* Find which vif the packet arrived on. */
2414 for (vifi = 0; vifi < numvifs; vifi++)
2415 if (viftable[vifi].v_ifp == ifp)
2418 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2420 * Drop the lock here to avoid holding it across rip_input.
2421 * This could make rsvpdebug printfs wrong. If you care,
2422 * record the state of stuff before dropping the lock.
2426 * If the old-style non-vif-associated socket is set,
2427 * then use it. Otherwise, drop packet since there
2428 * is no specific socket for this vif.
2430 if (ip_rsvpd != NULL) {
2432 printf("rsvp_input: Sending packet up old-style socket\n");
2433 rip_input(m, off); /* xxx */
2435 if (rsvpdebug && vifi == numvifs)
2436 printf("rsvp_input: Can't find vif for packet.\n");
2437 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2438 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2443 rsvp_src.sin_addr = ip->ip_src;
2446 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2447 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2449 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2451 printf("rsvp_input: Failed to append to socket\n");
2454 printf("rsvp_input: send packet up\n");
2460 * Code for bandwidth monitors
2464 * Define common interface for timeval-related methods
2466 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2467 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2468 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2471 compute_bw_meter_flags(struct bw_upcall *req)
2475 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2476 flags |= BW_METER_UNIT_PACKETS;
2477 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2478 flags |= BW_METER_UNIT_BYTES;
2479 if (req->bu_flags & BW_UPCALL_GEQ)
2480 flags |= BW_METER_GEQ;
2481 if (req->bu_flags & BW_UPCALL_LEQ)
2482 flags |= BW_METER_LEQ;
2488 * Add a bw_meter entry
2491 add_bw_upcall(struct bw_upcall *req)
2494 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2495 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2500 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2503 /* Test if the flags are valid */
2504 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2506 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2508 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2509 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2512 /* Test if the threshold time interval is valid */
2513 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2516 flags = compute_bw_meter_flags(req);
2519 * Find if we have already same bw_meter entry
2522 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2525 return EADDRNOTAVAIL;
2527 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2528 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2529 &req->bu_threshold.b_time, ==)) &&
2530 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2531 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2532 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2534 return 0; /* XXX Already installed */
2538 /* Allocate the new bw_meter entry */
2539 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2545 /* Set the new bw_meter entry */
2546 x->bm_threshold.b_time = req->bu_threshold.b_time;
2548 x->bm_start_time = now;
2549 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2550 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2551 x->bm_measured.b_packets = 0;
2552 x->bm_measured.b_bytes = 0;
2553 x->bm_flags = flags;
2554 x->bm_time_next = NULL;
2555 x->bm_time_hash = BW_METER_BUCKETS;
2557 /* Add the new bw_meter entry to the front of entries for this MFC */
2559 x->bm_mfc_next = mfc->mfc_bw_meter;
2560 mfc->mfc_bw_meter = x;
2561 schedule_bw_meter(x, &now);
2568 free_bw_list(struct bw_meter *list)
2570 while (list != NULL) {
2571 struct bw_meter *x = list;
2573 list = list->bm_mfc_next;
2574 unschedule_bw_meter(x);
2580 * Delete one or multiple bw_meter entries
2583 del_bw_upcall(struct bw_upcall *req)
2588 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2592 /* Find the corresponding MFC entry */
2593 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2596 return EADDRNOTAVAIL;
2597 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2599 * Delete all bw_meter entries for this mfc
2601 struct bw_meter *list;
2603 list = mfc->mfc_bw_meter;
2604 mfc->mfc_bw_meter = NULL;
2608 } else { /* Delete a single bw_meter entry */
2609 struct bw_meter *prev;
2612 flags = compute_bw_meter_flags(req);
2614 /* Find the bw_meter entry to delete */
2615 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2616 prev = x, x = x->bm_mfc_next) {
2617 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2618 &req->bu_threshold.b_time, ==)) &&
2619 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2620 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2621 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2624 if (x != NULL) { /* Delete entry from the list for this MFC */
2626 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2628 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2630 unschedule_bw_meter(x);
2632 /* Free the bw_meter entry */
2644 * Perform bandwidth measurement processing that may result in an upcall
2647 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2649 struct timeval delta;
2654 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2656 if (x->bm_flags & BW_METER_GEQ) {
2658 * Processing for ">=" type of bw_meter entry
2660 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2661 /* Reset the bw_meter entry */
2662 x->bm_start_time = *nowp;
2663 x->bm_measured.b_packets = 0;
2664 x->bm_measured.b_bytes = 0;
2665 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2668 /* Record that a packet is received */
2669 x->bm_measured.b_packets++;
2670 x->bm_measured.b_bytes += plen;
2673 * Test if we should deliver an upcall
2675 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2676 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2677 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2678 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2679 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2680 /* Prepare an upcall for delivery */
2681 bw_meter_prepare_upcall(x, nowp);
2682 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2685 } else if (x->bm_flags & BW_METER_LEQ) {
2687 * Processing for "<=" type of bw_meter entry
2689 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2691 * We are behind time with the multicast forwarding table
2692 * scanning for "<=" type of bw_meter entries, so test now
2693 * if we should deliver an upcall.
2695 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2696 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2697 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2698 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2699 /* Prepare an upcall for delivery */
2700 bw_meter_prepare_upcall(x, nowp);
2702 /* Reschedule the bw_meter entry */
2703 unschedule_bw_meter(x);
2704 schedule_bw_meter(x, nowp);
2707 /* Record that a packet is received */
2708 x->bm_measured.b_packets++;
2709 x->bm_measured.b_bytes += plen;
2712 * Test if we should restart the measuring interval
2714 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2715 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2716 (x->bm_flags & BW_METER_UNIT_BYTES &&
2717 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2718 /* Don't restart the measuring interval */
2720 /* Do restart the measuring interval */
2722 * XXX: note that we don't unschedule and schedule, because this
2723 * might be too much overhead per packet. Instead, when we process
2724 * all entries for a given timer hash bin, we check whether it is
2725 * really a timeout. If not, we reschedule at that time.
2727 x->bm_start_time = *nowp;
2728 x->bm_measured.b_packets = 0;
2729 x->bm_measured.b_bytes = 0;
2730 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2736 * Prepare a bandwidth-related upcall
2739 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2741 struct timeval delta;
2742 struct bw_upcall *u;
2747 * Compute the measured time interval
2750 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2753 * If there are too many pending upcalls, deliver them now
2755 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2759 * Set the bw_upcall entry
2761 u = &bw_upcalls[bw_upcalls_n++];
2762 u->bu_src = x->bm_mfc->mfc_origin;
2763 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2764 u->bu_threshold.b_time = x->bm_threshold.b_time;
2765 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2766 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2767 u->bu_measured.b_time = delta;
2768 u->bu_measured.b_packets = x->bm_measured.b_packets;
2769 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2771 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2772 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2773 if (x->bm_flags & BW_METER_UNIT_BYTES)
2774 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2775 if (x->bm_flags & BW_METER_GEQ)
2776 u->bu_flags |= BW_UPCALL_GEQ;
2777 if (x->bm_flags & BW_METER_LEQ)
2778 u->bu_flags |= BW_UPCALL_LEQ;
2782 * Send the pending bandwidth-related upcalls
2785 bw_upcalls_send(void)
2788 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2789 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2790 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2792 IGMPMSG_BW_UPCALL,/* im_msgtype */
2797 { 0 } }; /* im_dst */
2801 if (bw_upcalls_n == 0)
2802 return; /* No pending upcalls */
2807 * Allocate a new mbuf, initialize it with the header and
2808 * the payload for the pending calls.
2810 MGETHDR(m, M_DONTWAIT, MT_DATA);
2812 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2816 m->m_len = m->m_pkthdr.len = 0;
2817 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2818 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2822 * XXX do we need to set the address in k_igmpsrc ?
2824 mrtstat.mrts_upcalls++;
2825 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2826 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2827 ++mrtstat.mrts_upq_sockfull;
2832 * Compute the timeout hash value for the bw_meter entries
2834 #define BW_METER_TIMEHASH(bw_meter, hash) \
2836 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2838 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2839 (hash) = next_timeval.tv_sec; \
2840 if (next_timeval.tv_usec) \
2841 (hash)++; /* XXX: make sure we don't timeout early */ \
2842 (hash) %= BW_METER_BUCKETS; \
2846 * Schedule a timer to process periodically bw_meter entry of type "<="
2847 * by linking the entry in the proper hash bucket.
2850 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2856 if (!(x->bm_flags & BW_METER_LEQ))
2857 return; /* XXX: we schedule timers only for "<=" entries */
2860 * Reset the bw_meter entry
2862 x->bm_start_time = *nowp;
2863 x->bm_measured.b_packets = 0;
2864 x->bm_measured.b_bytes = 0;
2865 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2868 * Compute the timeout hash value and insert the entry
2870 BW_METER_TIMEHASH(x, time_hash);
2871 x->bm_time_next = bw_meter_timers[time_hash];
2872 bw_meter_timers[time_hash] = x;
2873 x->bm_time_hash = time_hash;
2877 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2878 * by removing the entry from the proper hash bucket.
2881 unschedule_bw_meter(struct bw_meter *x)
2884 struct bw_meter *prev, *tmp;
2888 if (!(x->bm_flags & BW_METER_LEQ))
2889 return; /* XXX: we schedule timers only for "<=" entries */
2892 * Compute the timeout hash value and delete the entry
2894 time_hash = x->bm_time_hash;
2895 if (time_hash >= BW_METER_BUCKETS)
2896 return; /* Entry was not scheduled */
2898 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2899 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2904 panic("unschedule_bw_meter: bw_meter entry not found");
2907 prev->bm_time_next = x->bm_time_next;
2909 bw_meter_timers[time_hash] = x->bm_time_next;
2911 x->bm_time_next = NULL;
2912 x->bm_time_hash = BW_METER_BUCKETS;
2917 * Process all "<=" type of bw_meter that should be processed now,
2918 * and for each entry prepare an upcall if necessary. Each processed
2919 * entry is rescheduled again for the (periodic) processing.
2921 * This is run periodically (once per second normally). On each round,
2922 * all the potentially matching entries are in the hash slot that we are
2928 static uint32_t last_tv_sec; /* last time we processed this */
2932 struct timeval now, process_endtime;
2935 if (last_tv_sec == now.tv_sec)
2936 return; /* nothing to do */
2938 loops = now.tv_sec - last_tv_sec;
2939 last_tv_sec = now.tv_sec;
2940 if (loops > BW_METER_BUCKETS)
2941 loops = BW_METER_BUCKETS;
2945 * Process all bins of bw_meter entries from the one after the last
2946 * processed to the current one. On entry, i points to the last bucket
2947 * visited, so we need to increment i at the beginning of the loop.
2949 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2950 struct bw_meter *x, *tmp_list;
2952 if (++i >= BW_METER_BUCKETS)
2955 /* Disconnect the list of bw_meter entries from the bin */
2956 tmp_list = bw_meter_timers[i];
2957 bw_meter_timers[i] = NULL;
2959 /* Process the list of bw_meter entries */
2960 while (tmp_list != NULL) {
2962 tmp_list = tmp_list->bm_time_next;
2964 /* Test if the time interval is over */
2965 process_endtime = x->bm_start_time;
2966 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2967 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2968 /* Not yet: reschedule, but don't reset */
2971 BW_METER_TIMEHASH(x, time_hash);
2972 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2974 * XXX: somehow the bin processing is a bit ahead of time.
2975 * Put the entry in the next bin.
2977 if (++time_hash >= BW_METER_BUCKETS)
2980 x->bm_time_next = bw_meter_timers[time_hash];
2981 bw_meter_timers[time_hash] = x;
2982 x->bm_time_hash = time_hash;
2988 * Test if we should deliver an upcall
2990 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2991 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2992 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2993 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2994 /* Prepare an upcall for delivery */
2995 bw_meter_prepare_upcall(x, &now);
2999 * Reschedule for next processing
3001 schedule_bw_meter(x, &now);
3005 /* Send all upcalls that are pending delivery */
3012 * A periodic function for sending all upcalls that are pending delivery
3015 expire_bw_upcalls_send(void *unused)
3021 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
3022 expire_bw_upcalls_send, NULL);
3026 * A periodic function for periodic scanning of the multicast forwarding
3027 * table for processing all "<=" bw_meter entries.
3030 expire_bw_meter_process(void *unused)
3032 if (mrt_api_config & MRT_MFC_BW_UPCALL)
3035 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
3039 * End of bandwidth monitoring code
3044 * Send the packet up to the user daemon, or eventually do kernel encapsulation
3048 pim_register_send(struct ip *ip, struct vif *vifp,
3049 struct mbuf *m, struct mfc *rt)
3051 struct mbuf *mb_copy, *mm;
3053 if (mrtdebug & DEBUG_PIM)
3054 log(LOG_DEBUG, "pim_register_send: ");
3056 mb_copy = pim_register_prepare(ip, m);
3057 if (mb_copy == NULL)
3061 * Send all the fragments. Note that the mbuf for each fragment
3062 * is freed by the sending machinery.
3064 for (mm = mb_copy; mm; mm = mb_copy) {
3065 mb_copy = mm->m_nextpkt;
3067 mm = m_pullup(mm, sizeof(struct ip));
3069 ip = mtod(mm, struct ip *);
3070 if ((mrt_api_config & MRT_MFC_RP) &&
3071 (rt->mfc_rp.s_addr != INADDR_ANY)) {
3072 pim_register_send_rp(ip, vifp, mm, rt);
3074 pim_register_send_upcall(ip, vifp, mm, rt);
3083 * Return a copy of the data packet that is ready for PIM Register
3085 * XXX: Note that in the returned copy the IP header is a valid one.
3087 static struct mbuf *
3088 pim_register_prepare(struct ip *ip, struct mbuf *m)
3090 struct mbuf *mb_copy = NULL;
3093 /* Take care of delayed checksums */
3094 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3095 in_delayed_cksum(m);
3096 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3100 * Copy the old packet & pullup its IP header into the
3101 * new mbuf so we can modify it.
3103 mb_copy = m_copypacket(m, M_DONTWAIT);
3104 if (mb_copy == NULL)
3106 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3107 if (mb_copy == NULL)
3110 /* take care of the TTL */
3111 ip = mtod(mb_copy, struct ip *);
3114 /* Compute the MTU after the PIM Register encapsulation */
3115 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3117 if (ip->ip_len <= mtu) {
3118 /* Turn the IP header into a valid one */
3119 ip->ip_len = htons(ip->ip_len);
3120 ip->ip_off = htons(ip->ip_off);
3122 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3124 /* Fragment the packet */
3125 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
3134 * Send an upcall with the data packet to the user-level process.
3137 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3138 struct mbuf *mb_copy, struct mfc *rt)
3140 struct mbuf *mb_first;
3141 int len = ntohs(ip->ip_len);
3143 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3148 * Add a new mbuf with an upcall header
3150 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
3151 if (mb_first == NULL) {
3155 mb_first->m_data += max_linkhdr;
3156 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3157 mb_first->m_len = sizeof(struct igmpmsg);
3158 mb_first->m_next = mb_copy;
3160 /* Send message to routing daemon */
3161 im = mtod(mb_first, struct igmpmsg *);
3162 im->im_msgtype = IGMPMSG_WHOLEPKT;
3164 im->im_vif = vifp - viftable;
3165 im->im_src = ip->ip_src;
3166 im->im_dst = ip->ip_dst;
3168 k_igmpsrc.sin_addr = ip->ip_src;
3170 mrtstat.mrts_upcalls++;
3172 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3173 if (mrtdebug & DEBUG_PIM)
3175 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3176 ++mrtstat.mrts_upq_sockfull;
3180 /* Keep statistics */
3181 pimstat.pims_snd_registers_msgs++;
3182 pimstat.pims_snd_registers_bytes += len;
3188 * Encapsulate the data packet in PIM Register message and send it to the RP.
3191 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3192 struct mbuf *mb_copy, struct mfc *rt)
3194 struct mbuf *mb_first;
3195 struct ip *ip_outer;
3196 struct pim_encap_pimhdr *pimhdr;
3197 int len = ntohs(ip->ip_len);
3198 vifi_t vifi = rt->mfc_parent;
3202 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
3204 return EADDRNOTAVAIL; /* The iif vif is invalid */
3208 * Add a new mbuf with the encapsulating header
3210 MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
3211 if (mb_first == NULL) {
3215 mb_first->m_data += max_linkhdr;
3216 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3217 mb_first->m_next = mb_copy;
3219 mb_first->m_pkthdr.len = len + mb_first->m_len;
3222 * Fill in the encapsulating IP and PIM header
3224 ip_outer = mtod(mb_first, struct ip *);
3225 *ip_outer = pim_encap_iphdr;
3226 ip_outer->ip_id = ip_newid();
3227 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3228 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3229 ip_outer->ip_dst = rt->mfc_rp;
3231 * Copy the inner header TOS to the outer header, and take care of the
3234 ip_outer->ip_tos = ip->ip_tos;
3235 if (ntohs(ip->ip_off) & IP_DF)
3236 ip_outer->ip_off |= IP_DF;
3237 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3238 + sizeof(pim_encap_iphdr));
3239 *pimhdr = pim_encap_pimhdr;
3240 /* If the iif crosses a border, set the Border-bit */
3241 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3242 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3244 mb_first->m_data += sizeof(pim_encap_iphdr);
3245 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3246 mb_first->m_data -= sizeof(pim_encap_iphdr);
3248 if (vifp->v_rate_limit == 0)
3249 tbf_send_packet(vifp, mb_first);
3251 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3253 /* Keep statistics */
3254 pimstat.pims_snd_registers_msgs++;
3255 pimstat.pims_snd_registers_bytes += len;
3261 * PIM-SMv2 and PIM-DM messages processing.
3262 * Receives and verifies the PIM control messages, and passes them
3263 * up to the listening socket, using rip_input().
3264 * The only message with special processing is the PIM_REGISTER message
3265 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3266 * is passed to if_simloop().
3269 pim_input(struct mbuf *m, int off)
3271 struct ip *ip = mtod(m, struct ip *);
3274 int datalen = ip->ip_len;
3278 /* Keep statistics */
3279 pimstat.pims_rcv_total_msgs++;
3280 pimstat.pims_rcv_total_bytes += datalen;
3285 if (datalen < PIM_MINLEN) {
3286 pimstat.pims_rcv_tooshort++;
3287 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3288 datalen, (u_long)ip->ip_src.s_addr);
3294 * If the packet is at least as big as a REGISTER, go agead
3295 * and grab the PIM REGISTER header size, to avoid another
3296 * possible m_pullup() later.
3298 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3299 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3301 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3303 * Get the IP and PIM headers in contiguous memory, and
3304 * possibly the PIM REGISTER header.
3306 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3307 (m = m_pullup(m, minlen)) == 0) {
3308 log(LOG_ERR, "pim_input: m_pullup failure\n");
3311 /* m_pullup() may have given us a new mbuf so reset ip. */
3312 ip = mtod(m, struct ip *);
3313 ip_tos = ip->ip_tos;
3315 /* adjust mbuf to point to the PIM header */
3316 m->m_data += iphlen;
3318 pim = mtod(m, struct pim *);
3321 * Validate checksum. If PIM REGISTER, exclude the data packet.
3323 * XXX: some older PIMv2 implementations don't make this distinction,
3324 * so for compatibility reason perform the checksum over part of the
3325 * message, and if error, then over the whole message.
3327 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3328 /* do nothing, checksum okay */
3329 } else if (in_cksum(m, datalen)) {
3330 pimstat.pims_rcv_badsum++;
3331 if (mrtdebug & DEBUG_PIM)
3332 log(LOG_DEBUG, "pim_input: invalid checksum");
3337 /* PIM version check */
3338 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3339 pimstat.pims_rcv_badversion++;
3340 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3341 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3346 /* restore mbuf back to the outer IP */
3347 m->m_data -= iphlen;
3350 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3352 * Since this is a REGISTER, we'll make a copy of the register
3353 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3356 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3358 struct ip *encap_ip;
3363 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3365 if (mrtdebug & DEBUG_PIM)
3367 "pim_input: register vif not set: %d\n", reg_vif_num);
3371 /* XXX need refcnt? */
3372 vifp = viftable[reg_vif_num].v_ifp;
3378 if (datalen < PIM_REG_MINLEN) {
3379 pimstat.pims_rcv_tooshort++;
3380 pimstat.pims_rcv_badregisters++;
3382 "pim_input: register packet size too small %d from %lx\n",
3383 datalen, (u_long)ip->ip_src.s_addr);
3388 reghdr = (u_int32_t *)(pim + 1);
3389 encap_ip = (struct ip *)(reghdr + 1);
3391 if (mrtdebug & DEBUG_PIM) {
3393 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3394 (u_long)ntohl(encap_ip->ip_src.s_addr),
3395 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3396 ntohs(encap_ip->ip_len));
3399 /* verify the version number of the inner packet */
3400 if (encap_ip->ip_v != IPVERSION) {
3401 pimstat.pims_rcv_badregisters++;
3402 if (mrtdebug & DEBUG_PIM) {
3403 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3404 "of the inner packet\n", encap_ip->ip_v);
3410 /* verify the inner packet is destined to a mcast group */
3411 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3412 pimstat.pims_rcv_badregisters++;
3413 if (mrtdebug & DEBUG_PIM)
3415 "pim_input: inner packet of register is not "
3417 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3422 /* If a NULL_REGISTER, pass it to the daemon */
3423 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3424 goto pim_input_to_daemon;
3427 * Copy the TOS from the outer IP header to the inner IP header.
3429 if (encap_ip->ip_tos != ip_tos) {
3430 /* Outer TOS -> inner TOS */
3431 encap_ip->ip_tos = ip_tos;
3432 /* Recompute the inner header checksum. Sigh... */
3434 /* adjust mbuf to point to the inner IP header */
3435 m->m_data += (iphlen + PIM_MINLEN);
3436 m->m_len -= (iphlen + PIM_MINLEN);
3438 encap_ip->ip_sum = 0;
3439 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3441 /* restore mbuf to point back to the outer IP header */
3442 m->m_data -= (iphlen + PIM_MINLEN);
3443 m->m_len += (iphlen + PIM_MINLEN);
3447 * Decapsulate the inner IP packet and loopback to forward it
3448 * as a normal multicast packet. Also, make a copy of the
3449 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3450 * to pass to the daemon later, so it can take the appropriate
3451 * actions (e.g., send back PIM_REGISTER_STOP).
3452 * XXX: here m->m_data points to the outer IP header.
3454 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3457 "pim_input: pim register: could not copy register head\n");
3462 /* Keep statistics */
3463 /* XXX: registers_bytes include only the encap. mcast pkt */
3464 pimstat.pims_rcv_registers_msgs++;
3465 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3468 * forward the inner ip packet; point m_data at the inner ip.
3470 m_adj(m, iphlen + PIM_MINLEN);
3472 if (mrtdebug & DEBUG_PIM) {
3474 "pim_input: forwarding decapsulated register: "
3475 "src %lx, dst %lx, vif %d\n",
3476 (u_long)ntohl(encap_ip->ip_src.s_addr),
3477 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3480 /* NB: vifp was collected above; can it change on us? */
3481 if_simloop(vifp, m, dst.sin_family, 0);
3483 /* prepare the register head to send to the mrouting daemon */
3487 pim_input_to_daemon:
3489 * Pass the PIM message up to the daemon; if it is a Register message,
3490 * pass the 'head' only up to the daemon. This includes the
3491 * outer IP header, PIM header, PIM-Register header and the
3493 * XXX: the outer IP header pkt size of a Register is not adjust to
3494 * reflect the fact that the inner multicast data is truncated.
3496 rip_input(m, iphlen);
3503 ip_mroute_modevent(module_t mod, int type, void *unused)
3507 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3511 ip_mcast_src = X_ip_mcast_src;
3512 ip_mforward = X_ip_mforward;
3513 ip_mrouter_done = X_ip_mrouter_done;
3514 ip_mrouter_get = X_ip_mrouter_get;
3515 ip_mrouter_set = X_ip_mrouter_set;
3516 ip_rsvp_force_done = X_ip_rsvp_force_done;
3517 ip_rsvp_vif = X_ip_rsvp_vif;
3518 legal_vif_num = X_legal_vif_num;
3519 mrt_ioctl = X_mrt_ioctl;
3520 rsvp_input_p = X_rsvp_input;
3525 * Typically module unload happens after the user-level
3526 * process has shutdown the kernel services (the check
3527 * below insures someone can't just yank the module out
3528 * from under a running process). But if the module is
3529 * just loaded and then unloaded w/o starting up a user
3530 * process we still need to cleanup.
3535 X_ip_mrouter_done();
3536 ip_mcast_src = NULL;
3538 ip_mrouter_done = NULL;
3539 ip_mrouter_get = NULL;
3540 ip_mrouter_set = NULL;
3541 ip_rsvp_force_done = NULL;
3543 legal_vif_num = NULL;
3545 rsvp_input_p = NULL;
3548 mtx_destroy(&mrouter_mtx);
3556 static moduledata_t ip_mroutemod = {
3561 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);