1139823Simp/*-
21541Srgrimes * Copyright (c) 1989 Stephen Deering.
31541Srgrimes * Copyright (c) 1992, 1993
41541Srgrimes *	The Regents of the University of California.  All rights reserved.
51541Srgrimes *
61541Srgrimes * This code is derived from software contributed to Berkeley by
71541Srgrimes * Stephen Deering of Stanford University.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)ip_mroute.h	8.1 (Berkeley) 6/10/93
3450477Speter * $FreeBSD$
351541Srgrimes */
361541Srgrimes
372169Spaul#ifndef _NETINET_IP_MROUTE_H_
382169Spaul#define _NETINET_IP_MROUTE_H_
392169Spaul
401541Srgrimes/*
419209Swollman * Definitions for IP multicast forwarding.
421541Srgrimes *
431541Srgrimes * Written by David Waitzman, BBN Labs, August 1988.
441541Srgrimes * Modified by Steve Deering, Stanford, February 1989.
452531Swollman * Modified by Ajit Thyagarajan, PARC, August 1993.
462531Swollman * Modified by Ajit Thyagarajan, PARC, August 1994.
47118622Shsu * Modified by Ahmed Helmy, SGI, June 1996.
48118622Shsu * Modified by Pavlin Radoslavov, ICSI, October 2002.
491541Srgrimes *
509209Swollman * MROUTING Revision: 3.3.1.3
51118622Shsu * and PIM-SMv2 and PIM-DM support, advanced API support,
52118622Shsu * bandwidth metering and signaling.
531541Srgrimes */
541541Srgrimes
551541Srgrimes/*
569209Swollman * Multicast Routing set/getsockopt commands.
571541Srgrimes */
589209Swollman#define	MRT_INIT	100	/* initialize forwarder */
599209Swollman#define	MRT_DONE	101	/* shut down forwarder */
609209Swollman#define	MRT_ADD_VIF	102	/* create virtual interface */
619209Swollman#define	MRT_DEL_VIF	103	/* delete virtual interface */
629209Swollman#define MRT_ADD_MFC	104	/* insert forwarding cache entry */
639209Swollman#define MRT_DEL_MFC	105	/* delete forwarding cache entry */
649209Swollman#define MRT_VERSION	106	/* get kernel version number */
65118622Shsu#define MRT_ASSERT      107     /* enable assert processing */
66118622Shsu#define MRT_PIM		MRT_ASSERT /* enable PIM processing */
67118622Shsu#define MRT_API_SUPPORT	109	/* supported MRT API */
68118622Shsu#define MRT_API_CONFIG	110	/* config MRT API */
69118622Shsu#define MRT_ADD_BW_UPCALL 111	/* create bandwidth monitor */
70118622Shsu#define MRT_DEL_BW_UPCALL 112	/* delete bandwidth monitor */
711541Srgrimes
721541Srgrimes/*
731541Srgrimes * Types and macros for handling bitmaps with one bit per virtual interface.
741541Srgrimes */
751541Srgrimes#define	MAXVIFS 32
761541Srgrimestypedef u_long vifbitmap_t;
771541Srgrimestypedef u_short vifi_t;		/* type of a vif index */
789209Swollman#define ALL_VIFS (vifi_t)-1
791541Srgrimes
801541Srgrimes#define	VIFM_SET(n, m)		((m) |= (1 << (n)))
811541Srgrimes#define	VIFM_CLR(n, m)		((m) &= ~(1 << (n)))
821541Srgrimes#define	VIFM_ISSET(n, m)	((m) & (1 << (n)))
831541Srgrimes#define	VIFM_CLRALL(m)		((m) = 0x00000000)
841541Srgrimes#define	VIFM_COPY(mfrom, mto)	((mto) = (mfrom))
851541Srgrimes#define	VIFM_SAME(m1, m2)	((m1) == (m2))
861541Srgrimes
87191356Sbmsstruct mfc;
881541Srgrimes
891541Srgrimes/*
909209Swollman * Argument structure for MRT_ADD_VIF.
919209Swollman * (MRT_DEL_VIF takes a single vifi_t argument.)
921541Srgrimes */
931541Srgrimesstruct vifctl {
94133874Srwatson	vifi_t	vifc_vifi;		/* the index of the vif to be added */
95133874Srwatson	u_char	vifc_flags;		/* VIFF_ flags defined below */
96133874Srwatson	u_char	vifc_threshold;		/* min ttl required to forward on vif */
979209Swollman	u_int	vifc_rate_limit;	/* max rate */
981541Srgrimes	struct	in_addr vifc_lcl_addr;	/* local interface address */
991541Srgrimes	struct	in_addr vifc_rmt_addr;	/* remote address (tunnels only) */
1001541Srgrimes};
1011541Srgrimes
102166549Sbms#define	VIFF_TUNNEL	0x1		/* no-op; retained for old source */
103166549Sbms#define VIFF_SRCRT	0x2		/* no-op; retained for old source */
104118622Shsu#define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
1051541Srgrimes
1062531Swollman/*
1079209Swollman * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC
108118622Shsu * XXX if you change this, make sure to change struct mfcctl2 as well.
1092531Swollman */
1102531Swollmanstruct mfcctl {
1119209Swollman    struct in_addr  mfcc_origin;		/* ip origin of mcasts       */
112133874Srwatson    struct in_addr  mfcc_mcastgrp;		/* multicast group associated*/
113133874Srwatson    vifi_t	    mfcc_parent;		/* incoming vif              */
114133874Srwatson    u_char	    mfcc_ttls[MAXVIFS];		/* forwarding ttls on vifs   */
1152531Swollman};
1161541Srgrimes
1171541Srgrimes/*
118118622Shsu * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
119118622Shsu * and extends the old struct mfcctl.
120118622Shsu */
121118622Shsustruct mfcctl2 {
122118622Shsu	/* the mfcctl fields */
123118622Shsu	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
124118622Shsu	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
125118622Shsu	vifi_t		mfcc_parent;		/* incoming vif		     */
126133874Srwatson	u_char		mfcc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
127118622Shsu
128118622Shsu	/* extension fields */
129118622Shsu	uint8_t		mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
130118622Shsu	struct in_addr	mfcc_rp;		/* the RP address            */
131118622Shsu};
132118622Shsu/*
133118622Shsu * The advanced-API flags.
134118622Shsu *
135118622Shsu * The MRT_MFC_FLAGS_XXX API flags are also used as flags
136118622Shsu * for the mfcc_flags field.
137118622Shsu */
138118622Shsu#define	MRT_MFC_FLAGS_DISABLE_WRONGVIF	(1 << 0) /* disable WRONGVIF signals */
139118622Shsu#define	MRT_MFC_FLAGS_BORDER_VIF	(1 << 1) /* border vif		     */
140118622Shsu#define MRT_MFC_RP			(1 << 8) /* enable RP address	     */
141118622Shsu#define MRT_MFC_BW_UPCALL		(1 << 9) /* enable bw upcalls	     */
142118622Shsu#define MRT_MFC_FLAGS_ALL		(MRT_MFC_FLAGS_DISABLE_WRONGVIF |    \
143118622Shsu					 MRT_MFC_FLAGS_BORDER_VIF)
144118622Shsu#define MRT_API_FLAGS_ALL		(MRT_MFC_FLAGS_ALL |		     \
145118622Shsu					 MRT_MFC_RP |			     \
146118622Shsu					 MRT_MFC_BW_UPCALL)
147118622Shsu
148118622Shsu/*
149118622Shsu * Structure for installing or delivering an upcall if the
150118622Shsu * measured bandwidth is above or below a threshold.
151118622Shsu *
152118622Shsu * User programs (e.g. daemons) may have a need to know when the
153118622Shsu * bandwidth used by some data flow is above or below some threshold.
154118622Shsu * This interface allows the userland to specify the threshold (in
155118622Shsu * bytes and/or packets) and the measurement interval. Flows are
156118622Shsu * all packet with the same source and destination IP address.
157118622Shsu * At the moment the code is only used for multicast destinations
158118622Shsu * but there is nothing that prevents its use for unicast.
159118622Shsu *
160118622Shsu * The measurement interval cannot be shorter than some Tmin (currently, 3s).
161118622Shsu * The threshold is set in packets and/or bytes per_interval.
162118622Shsu *
163118622Shsu * Measurement works as follows:
164118622Shsu *
165133874Srwatson * For >= measurements:
166118622Shsu * The first packet marks the start of a measurement interval.
167118622Shsu * During an interval we count packets and bytes, and when we
168118622Shsu * pass the threshold we deliver an upcall and we are done.
169118622Shsu * The first packet after the end of the interval resets the
170118622Shsu * count and restarts the measurement.
171118622Shsu *
172118622Shsu * For <= measurement:
173118622Shsu * We start a timer to fire at the end of the interval, and
174118622Shsu * then for each incoming packet we count packets and bytes.
175118622Shsu * When the timer fires, we compare the value with the threshold,
176118622Shsu * schedule an upcall if we are below, and restart the measurement
177118622Shsu * (reschedule timer and zero counters).
178118622Shsu */
179118622Shsu
180118622Shsustruct bw_data {
181118622Shsu	struct timeval	b_time;
182118622Shsu	uint64_t	b_packets;
183118622Shsu	uint64_t	b_bytes;
184118622Shsu};
185118622Shsu
186118622Shsustruct bw_upcall {
187118622Shsu	struct in_addr	bu_src;			/* source address            */
188118622Shsu	struct in_addr	bu_dst;			/* destination address       */
189118622Shsu	uint32_t	bu_flags;		/* misc flags (see below)    */
190118622Shsu#define BW_UPCALL_UNIT_PACKETS   (1 << 0)	/* threshold (in packets)    */
191118622Shsu#define BW_UPCALL_UNIT_BYTES     (1 << 1)	/* threshold (in bytes)      */
192118622Shsu#define BW_UPCALL_GEQ            (1 << 2)	/* upcall if bw >= threshold */
193118622Shsu#define BW_UPCALL_LEQ            (1 << 3)	/* upcall if bw <= threshold */
194118622Shsu#define BW_UPCALL_DELETE_ALL     (1 << 4)	/* delete all upcalls for s,d*/
195118622Shsu	struct bw_data	bu_threshold;		/* the bw threshold	     */
196118622Shsu	struct bw_data	bu_measured;		/* the measured bw	     */
197118622Shsu};
198118622Shsu
199118622Shsu/* max. number of upcalls to deliver together */
200118622Shsu#define BW_UPCALLS_MAX				128
201118622Shsu/* min. threshold time interval for bandwidth measurement */
202118622Shsu#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC	3
203118622Shsu#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC	0
204118622Shsu
205118622Shsu/*
2062531Swollman * The kernel's multicast routing statistics.
2071541Srgrimes */
2082531Swollmanstruct mrtstat {
209253081Sae	uint64_t mrts_mfc_lookups;    /* # forw. cache hash table hits   */
210253081Sae	uint64_t mrts_mfc_misses;     /* # forw. cache hash table misses */
211253081Sae	uint64_t mrts_upcalls;	      /* # calls to multicast routing daemon */
212253081Sae	uint64_t mrts_no_route;	      /* no route for packet's origin    */
213253081Sae	uint64_t mrts_bad_tunnel;     /* malformed tunnel options        */
214253081Sae	uint64_t mrts_cant_tunnel;    /* no room for tunnel options      */
215253081Sae	uint64_t mrts_wrong_if;	      /* arrived on wrong interface	 */
216253081Sae	uint64_t mrts_upq_ovflw;      /* upcall Q overflow		 */
217253081Sae	uint64_t mrts_cache_cleanups; /* # entries with no upcalls	 */
218253081Sae	uint64_t mrts_drop_sel;	      /* pkts dropped selectively        */
219253081Sae	uint64_t mrts_q_overflow;     /* pkts dropped - Q overflow       */
220253081Sae	uint64_t mrts_pkt2large;      /* pkts dropped - size > BKT SIZE  */
221253081Sae	uint64_t mrts_upq_sockfull;   /* upcalls dropped - socket full   */
2221541Srgrimes};
2231541Srgrimes
224190966Srwatson#ifdef _KERNEL
225253084Sae#define	MRTSTAT_ADD(name, val)	\
226253084Sae    VNET_PCPUSTAT_ADD(struct mrtstat, mrtstat, name, (val))
227190966Srwatson#define	MRTSTAT_INC(name)	MRTSTAT_ADD(name, 1)
228190966Srwatson#endif
229190966Srwatson
2302531Swollman/*
2312531Swollman * Argument structure used by mrouted to get src-grp pkt counts
2322531Swollman */
2332531Swollmanstruct sioc_sg_req {
2342531Swollman    struct in_addr src;
2352531Swollman    struct in_addr grp;
2369209Swollman    u_long pktcnt;
2379209Swollman    u_long bytecnt;
2389209Swollman    u_long wrong_if;
2392531Swollman};
2401541Srgrimes
2412531Swollman/*
2422531Swollman * Argument structure used by mrouted to get vif pkt counts
2432531Swollman */
2442531Swollmanstruct sioc_vif_req {
2459209Swollman    vifi_t vifi;		/* vif number				*/
2469209Swollman    u_long icount;		/* Input packet count on vif		*/
2479209Swollman    u_long ocount;		/* Output packet count on vif		*/
2489209Swollman    u_long ibytes;		/* Input byte count on vif		*/
2499209Swollman    u_long obytes;		/* Output byte count on vif		*/
2502531Swollman};
2512531Swollman
252133874Srwatson
2539209Swollman/*
2549209Swollman * The kernel's virtual-interface structure.
2559209Swollman */
2562531Swollmanstruct vif {
257133874Srwatson    u_char		v_flags;	/* VIFF_ flags defined above         */
258133874Srwatson    u_char		v_threshold;	/* min ttl required to forward on vif*/
259133874Srwatson    struct in_addr	v_lcl_addr;	/* local interface address           */
260133874Srwatson    struct in_addr	v_rmt_addr;	/* remote address (tunnels only)     */
261133874Srwatson    struct ifnet       *v_ifp;		/* pointer to interface              */
2622531Swollman    u_long		v_pkt_in;	/* # pkts in on interface            */
2632531Swollman    u_long		v_pkt_out;	/* # pkts out on interface           */
2649209Swollman    u_long		v_bytes_in;	/* # bytes in on interface	     */
2659209Swollman    u_long		v_bytes_out;	/* # bytes out on interface	     */
2662531Swollman};
2672531Swollman
268191356Sbms#ifdef _KERNEL
2691541Srgrimes/*
270133874Srwatson * The kernel's multicast forwarding cache entry structure
2711541Srgrimes */
2722531Swollmanstruct mfc {
273190012Sbms	LIST_ENTRY(mfc)	mfc_hash;
274118622Shsu	struct in_addr	mfc_origin;		/* IP origin of mcasts	     */
275133874Srwatson	struct in_addr  mfc_mcastgrp;		/* multicast group associated*/
276133874Srwatson	vifi_t		mfc_parent;		/* incoming vif              */
277133874Srwatson	u_char		mfc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
278118622Shsu	u_long		mfc_pkt_cnt;		/* pkt count for src-grp     */
279118622Shsu	u_long		mfc_byte_cnt;		/* byte count for src-grp    */
280118622Shsu	u_long		mfc_wrong_if;		/* wrong if for src-grp	     */
281118622Shsu	int		mfc_expire;		/* time to clean entry up    */
282118622Shsu	struct timeval	mfc_last_assert;	/* last time I sent an assert*/
283118622Shsu	uint8_t		mfc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
284118622Shsu	struct in_addr	mfc_rp;			/* the RP address	     */
285118622Shsu	struct bw_meter	*mfc_bw_meter;		/* list of bandwidth meters  */
286190012Sbms	u_long		mfc_nstall;		/* # of packets awaiting mfc */
287190012Sbms	TAILQ_HEAD(, rtdetq) mfc_stall;		/* q of packets awaiting mfc */
2881541Srgrimes};
289191356Sbms#endif /* _KERNEL */
2901541Srgrimes
2911541Srgrimes/*
2929209Swollman * Struct used to communicate from kernel to multicast router
2939209Swollman * note the convenient similarity to an IP packet
2949209Swollman */
2959209Swollmanstruct igmpmsg {
296155166Sru    uint32_t	    unused1;
297155166Sru    uint32_t	    unused2;
2989209Swollman    u_char	    im_msgtype;			/* what type of message	    */
299118622Shsu#define IGMPMSG_NOCACHE		1	/* no MFC in the kernel		    */
300118622Shsu#define IGMPMSG_WRONGVIF	2	/* packet came from wrong interface */
301118622Shsu#define	IGMPMSG_WHOLEPKT	3	/* PIM pkt for user level encap.    */
302118622Shsu#define	IGMPMSG_BW_UPCALL	4	/* BW monitoring upcall		    */
3039209Swollman    u_char	    im_mbz;			/* must be zero		    */
3049209Swollman    u_char	    im_vif;			/* vif rec'd on		    */
3059209Swollman    u_char	    unused3;
3069209Swollman    struct in_addr  im_src, im_dst;
3079209Swollman};
3089209Swollman
309191356Sbms#ifdef _KERNEL
3109209Swollman/*
3112531Swollman * Argument structure used for pkt info. while upcall is made
3121541Srgrimes */
3132531Swollmanstruct rtdetq {
314190012Sbms    TAILQ_ENTRY(rtdetq)	rte_link;
315133874Srwatson    struct mbuf		*m;		/* A copy of the packet		    */
3169209Swollman    struct ifnet	*ifp;		/* Interface pkt came in on	    */
3179209Swollman    vifi_t		xmt_vif;	/* Saved copy of imo_multicast_vif  */
3181541Srgrimes};
3192531Swollman#define MAX_UPQ	4		/* max. no of pkts in upcall Q */
320191356Sbms#endif /* _KERNEL */
3212531Swollman
3221541Srgrimes/*
323118622Shsu * Structure for measuring the bandwidth and sending an upcall if the
324118622Shsu * measured bandwidth is above or below a threshold.
325118622Shsu */
326118622Shsustruct bw_meter {
327118622Shsu	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
328118622Shsu	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
329118622Shsu	uint32_t	bm_time_hash;		/* the time hash value       */
330118622Shsu	struct mfc	*bm_mfc;		/* the corresponding mfc     */
331118622Shsu	uint32_t	bm_flags;		/* misc flags (see below)    */
332118622Shsu#define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
333118622Shsu#define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
334118622Shsu#define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
335118622Shsu#define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
336133874Srwatson#define BW_METER_USER_FLAGS	(BW_METER_UNIT_PACKETS |		\
337118622Shsu				 BW_METER_UNIT_BYTES |			\
338118622Shsu				 BW_METER_GEQ |				\
339118622Shsu				 BW_METER_LEQ)
340118622Shsu
341118622Shsu#define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
342118622Shsu
343118622Shsu	struct bw_data	bm_threshold;		/* the upcall threshold	     */
344118622Shsu	struct bw_data	bm_measured;		/* the measured bw	     */
345118622Shsu	struct timeval	bm_start_time;		/* abs. time		     */
346118622Shsu};
347118622Shsu
34855205Speter#ifdef _KERNEL
34921261Swollman
35038482Swollmanstruct sockopt;
35138482Swollman
35292723Salfredextern int	(*ip_mrouter_set)(struct socket *, struct sockopt *);
35392723Salfredextern int	(*ip_mrouter_get)(struct socket *, struct sockopt *);
35492723Salfredextern int	(*ip_mrouter_done)(void);
355194581Srdivackyextern int	(*mrt_ioctl)(u_long, caddr_t, int);
3561541Srgrimes
35755205Speter#endif /* _KERNEL */
3581541Srgrimes
3592531Swollman#endif /* _NETINET_IP_MROUTE_H_ */
360