Home | History | Annotate | Line # | Download | only in netinet
      1 /*	$NetBSD: ip_mroute.h,v 1.36 2025/07/28 21:25:00 kim Exp $	*/
      2 
      3 #ifndef _NETINET_IP_MROUTE_H_
      4 #define _NETINET_IP_MROUTE_H_
      5 
      6 /*
      7  * Definitions for IP multicast forwarding.
      8  *
      9  * Written by David Waitzman, BBN Labs, August 1988.
     10  * Modified by Steve Deering, Stanford, February 1989.
     11  * Modified by Ajit Thyagarajan, PARC, August 1993.
     12  * Modified by Ajit Thyagarajan, PARC, August 1994.
     13  * Modified by Ahmed Helmy, SGI, June 1996.
     14  * Modified by Pavlin Radoslavov, ICSI, October 2002.
     15  *
     16  * MROUTING Revision: 1.2
     17  * and PIM-SMv2 and PIM-DM support, advanced API support,
     18  * bandwidth metering and signaling.
     19  */
     20 
     21 #include <sys/queue.h>
     22 #include <sys/callout.h>
     23 
     24 /*
     25  * Multicast Routing set/getsockopt commands.
     26  */
     27 #define	MRT_INIT		100	/* initialize forwarder */
     28 #define	MRT_DONE		101	/* shut down forwarder */
     29 #define	MRT_ADD_VIF		102	/* create virtual interface */
     30 #define	MRT_DEL_VIF		103	/* delete virtual interface */
     31 #define	MRT_ADD_MFC		104	/* insert forwarding cache entry */
     32 #define	MRT_DEL_MFC		105	/* delete forwarding cache entry */
     33 #define	MRT_VERSION		106	/* get kernel version number */
     34 #define	MRT_ASSERT		107	/* enable assert processing */
     35 #define MRT_PIM			MRT_ASSERT /* enable PIM processing */
     36 #define MRT_API_SUPPORT		109	/* supported MRT API */
     37 #define MRT_API_CONFIG		110	/* config MRT API */
     38 #define MRT_ADD_BW_UPCALL	111	/* create bandwidth monitor */
     39 #define MRT_DEL_BW_UPCALL	112	/* delete bandwidth monitor */
     40 
     41 /*
     42  * Types and macros for handling bitmaps with one bit per virtual interface.
     43  */
     44 #define	MAXVIFS 32
     45 typedef u_int32_t vifbitmap_t;
     46 typedef u_int16_t vifi_t;		/* type of a vif index */
     47 
     48 #define	VIFM_SET(n, m)			((m) |= (1 << (n)))
     49 #define	VIFM_CLR(n, m)			((m) &= ~(1 << (n)))
     50 #define	VIFM_ISSET(n, m)		((m) & (1 << (n)))
     51 #define	VIFM_SETALL(m)			((m) = 0xffffffff)
     52 #define	VIFM_CLRALL(m)			((m) = 0x00000000)
     53 #define	VIFM_COPY(mfrom, mto)		((mto) = (mfrom))
     54 #define	VIFM_SAME(m1, m2)		((m1) == (m2))
     55 
     56 #define	VIFF_TUNNEL	0x1		/* vif represents a tunnel end-point */
     57 #define	VIFF_SRCRT	0x2		/* tunnel uses IP src routing */
     58 #define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
     59 
     60 /*
     61  * Argument structure for MRT_ADD_VIF.
     62  * (MRT_DEL_VIF takes a single vifi_t argument.)
     63  */
     64 struct vifctl {
     65 	vifi_t	  vifc_vifi;	    	/* the index of the vif to be added */
     66 	u_int8_t  vifc_flags;     	/* VIFF_ flags defined below */
     67 	u_int8_t  vifc_threshold; 	/* min ttl required to forward on vif */
     68 	u_int32_t vifc_rate_limit;	/* max rate */
     69 	struct	  in_addr vifc_lcl_addr;/* local interface address */
     70 	struct	  in_addr vifc_rmt_addr;/* remote address (tunnels only) */
     71 };
     72 
     73 /*
     74  * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC.
     75  * XXX if you change this, make sure to change struct mfcctl2 as well.
     76  */
     77 struct mfcctl {
     78 	struct	 in_addr mfcc_origin;	/* ip origin of mcasts */
     79 	struct	 in_addr mfcc_mcastgrp;	/* multicast group associated */
     80 	vifi_t	 mfcc_parent;		/* incoming vif */
     81 	u_int8_t mfcc_ttls[MAXVIFS];	/* forwarding ttls on vifs */
     82 };
     83 
     84 /*
     85  * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
     86  * and extends the old struct mfcctl.
     87  */
     88 struct mfcctl2 {
     89 	/* the mfcctl fields */
     90 	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
     91 	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
     92 	vifi_t		mfcc_parent;		/* incoming vif		     */
     93 	u_int8_t	mfcc_ttls[MAXVIFS]; 	/* forwarding ttls on vifs   */
     94 
     95 	/* extension fields */
     96 	u_int8_t	mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
     97 	struct in_addr	mfcc_rp;		/* the RP address            */
     98 };
     99 
    100 /*
    101  * The advanced-API flags.
    102  *
    103  * The MRT_MFC_FLAGS_XXX API flags are also used as flags
    104  * for the mfcc_flags field.
    105  */
    106 #define	MRT_MFC_FLAGS_DISABLE_WRONGVIF	(1 << 0) /* disable WRONGVIF signals */
    107 #define	MRT_MFC_FLAGS_BORDER_VIF	(1 << 1) /* border vif		     */
    108 #define MRT_MFC_RP			(1 << 8) /* enable RP address	     */
    109 #define MRT_MFC_BW_UPCALL		(1 << 9) /* enable bw upcalls	     */
    110 #define MRT_MFC_FLAGS_ALL		(MRT_MFC_FLAGS_DISABLE_WRONGVIF |    \
    111 					 MRT_MFC_FLAGS_BORDER_VIF)
    112 #define MRT_API_FLAGS_ALL		(MRT_MFC_FLAGS_ALL |		     \
    113 					 MRT_MFC_RP |			     \
    114 					 MRT_MFC_BW_UPCALL)
    115 
    116 /*
    117  * Structure for installing or delivering an upcall if the
    118  * measured bandwidth is above or below a threshold.
    119  *
    120  * User programs (e.g. daemons) may have a need to know when the
    121  * bandwidth used by some data flow is above or below some threshold.
    122  * This interface allows the userland to specify the threshold (in
    123  * bytes and/or packets) and the measurement interval. Flows are
    124  * all packet with the same source and destination IP address.
    125  * At the moment the code is only used for multicast destinations
    126  * but there is nothing that prevents its use for unicast.
    127  *
    128  * The measurement interval cannot be shorter than some Tmin (currently, 3s).
    129  * The threshold is set in packets and/or bytes per_interval.
    130  *
    131  * Measurement works as follows:
    132  *
    133  * For >= measurements:
    134  * The first packet marks the start of a measurement interval.
    135  * During an interval we count packets and bytes, and when we
    136  * pass the threshold we deliver an upcall and we are done.
    137  * The first packet after the end of the interval resets the
    138  * count and restarts the measurement.
    139  *
    140  * For <= measurement:
    141  * We start a timer to fire at the end of the interval, and
    142  * then for each incoming packet we count packets and bytes.
    143  * When the timer fires, we compare the value with the threshold,
    144  * schedule an upcall if we are below, and restart the measurement
    145  * (reschedule timer and zero counters).
    146  */
    147 
    148 struct bw_data {
    149 	struct timeval	b_time;
    150 	u_int64_t	b_packets;
    151 	u_int64_t	b_bytes;
    152 };
    153 
    154 struct bw_upcall {
    155 	struct in_addr	bu_src;			/* source address            */
    156 	struct in_addr	bu_dst;			/* destination address       */
    157 	u_int32_t	bu_flags;		/* misc flags (see below)    */
    158 #define BW_UPCALL_UNIT_PACKETS   (1 << 0)	/* threshold (in packets)    */
    159 #define BW_UPCALL_UNIT_BYTES     (1 << 1)	/* threshold (in bytes)      */
    160 #define BW_UPCALL_GEQ            (1 << 2)	/* upcall if bw >= threshold */
    161 #define BW_UPCALL_LEQ            (1 << 3)	/* upcall if bw <= threshold */
    162 #define BW_UPCALL_DELETE_ALL     (1 << 4)	/* delete all upcalls for s,d*/
    163 	struct bw_data	bu_threshold;		/* the bw threshold	     */
    164 	struct bw_data	bu_measured;		/* the measured bw	     */
    165 };
    166 
    167 /* max. number of upcalls to deliver together */
    168 #define BW_UPCALLS_MAX				128
    169 /* min. threshold time interval for bandwidth measurement */
    170 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC	3
    171 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC	0
    172 
    173 /*
    174  * Argument structure used by mrouted to get src-grp pkt counts.
    175  */
    176 struct sioc_sg_req {
    177 	struct	in_addr src;
    178 	struct	in_addr grp;
    179 	u_long	pktcnt;
    180 	u_long	bytecnt;
    181 	u_long	wrong_if;
    182 };
    183 
    184 /*
    185  * Argument structure used by mrouted to get vif pkt counts.
    186  */
    187 struct sioc_vif_req {
    188 	vifi_t	vifi;			/* vif number */
    189 	u_long	icount;			/* input packet count on vif */
    190 	u_long	ocount;			/* output packet count on vif */
    191 	u_long	ibytes;			/* input byte count on vif */
    192 	u_long	obytes;			/* output byte count on vif */
    193 };
    194 
    195 /*
    196  * The kernel's multicast routing statistics.
    197  */
    198 struct mrtstat {
    199 	u_long	mrts_mfc_lookups;	/* # forw. cache hash table hits */
    200 	u_long	mrts_mfc_misses;	/* # forw. cache hash table misses */
    201 	u_long	mrts_upcalls;		/* # calls to mrouted */
    202 	u_long	mrts_no_route;		/* no route for packet's origin */
    203 	u_long	mrts_bad_tunnel;	/* malformed tunnel options */
    204 	u_long	mrts_cant_tunnel;	/* no room for tunnel options */
    205 	u_long	mrts_wrong_if;		/* arrived on wrong interface */
    206 	u_long	mrts_upq_ovflw;		/* upcall Q overflow */
    207 	u_long	mrts_cache_cleanups;	/* # entries with no upcalls */
    208 	u_long	mrts_drop_sel;     	/* pkts dropped selectively */
    209 	u_long	mrts_q_overflow;    	/* pkts dropped - Q overflow */
    210 	u_long	mrts_pkt2large;     	/* pkts dropped - size > BKT SIZE */
    211 	u_long	mrts_upq_sockfull;	/* upcalls dropped - socket full */
    212 };
    213 
    214 /*
    215  * Structure used to communicate from kernel to multicast router.
    216  * (Note the convenient similarity to an IP packet.)
    217  */
    218 struct igmpmsg {
    219 	u_int32_t unused1;
    220 	u_int32_t unused2;
    221 	u_int8_t  im_msgtype;		/* what type of message */
    222 #define IGMPMSG_NOCACHE		1	/* no MFC in the kernel		    */
    223 #define IGMPMSG_WRONGVIF	2	/* packet came from wrong interface */
    224 #define	IGMPMSG_WHOLEPKT	3	/* PIM pkt for user level encap.    */
    225 #define	IGMPMSG_BW_UPCALL	4	/* BW monitoring upcall		    */
    226 	u_int8_t  im_mbz;		/* must be zero */
    227 	u_int8_t  im_vif;		/* vif rec'd on */
    228 	u_int8_t  unused3;
    229 	struct	  in_addr im_src, im_dst;
    230 };
    231 #ifdef __CTASSERT
    232 __CTASSERT(sizeof(struct igmpmsg) == 20);
    233 #endif
    234 
    235 #ifdef _KERNEL
    236 
    237 /*
    238  * The kernel's virtual-interface structure.
    239  */
    240 struct encaptab;
    241 struct vif {
    242 	struct	  mbuf *tbf_q, **tbf_t;	/* packet queue */
    243 	struct	  timeval tbf_last_pkt_t; /* arr. time of last pkt */
    244 	u_int32_t tbf_n_tok;		/* no of tokens in bucket */
    245 	u_int32_t tbf_q_len;		/* length of queue at this vif */
    246 	u_int32_t tbf_max_q_len;	/* max. queue length */
    247 
    248 	u_int8_t  v_flags;		/* VIFF_ flags defined above */
    249 	u_int8_t  v_threshold;		/* min ttl required to forward on vif */
    250 	u_int32_t v_rate_limit;		/* max rate */
    251 	struct	  in_addr v_lcl_addr;	/* local interface address */
    252 	struct	  in_addr v_rmt_addr;	/* remote address (tunnels only) */
    253 	struct	  ifnet *v_ifp;		/* pointer to interface */
    254 	u_long	  v_pkt_in;		/* # pkts in on interface */
    255 	u_long	  v_pkt_out;		/* # pkts out on interface */
    256 	u_long	  v_bytes_in;		/* # bytes in on interface */
    257 	u_long	  v_bytes_out;		/* # bytes out on interface */
    258 	struct	  route v_route;	/* cached route if this is a tunnel */
    259 	callout_t v_repq_ch;		/* for tbf_reprocess_q() */
    260 	const struct encaptab *v_encap_cookie;
    261 };
    262 
    263 /*
    264  * The kernel's multicast forwarding cache entry structure.
    265  * (A field for the type of service (mfc_tos) is to be added
    266  * at a future point.)
    267  */
    268 struct mfc {
    269 	LIST_ENTRY(mfc) mfc_hash;
    270 	struct	 in_addr mfc_origin;	 	/* ip origin of mcasts */
    271 	struct	 in_addr mfc_mcastgrp;  	/* multicast group associated */
    272 	vifi_t	 mfc_parent;			/* incoming vif */
    273 	u_int8_t mfc_ttls[MAXVIFS]; 		/* forwarding ttls on vifs */
    274 	u_long	 mfc_pkt_cnt;			/* pkt count for src-grp */
    275 	u_long	 mfc_byte_cnt;			/* byte count for src-grp */
    276 	u_long	 mfc_wrong_if;			/* wrong if for src-grp	*/
    277 	int	 mfc_expire;			/* time to clean entry up */
    278 	struct	 timeval mfc_last_assert;	/* last time I sent an assert */
    279 	struct	 rtdetq *mfc_stall;		/* pkts waiting for route */
    280 	u_int8_t mfc_flags[MAXVIFS];		/* the MRT_MFC_FLAGS_* flags */
    281 	struct	 in_addr mfc_rp;		/* the RP address	     */
    282 	struct	 bw_meter *mfc_bw_meter;	/* list of bandwidth meters  */
    283 };
    284 
    285 /*
    286  * Argument structure used for pkt info. while upcall is made.
    287  */
    288 struct rtdetq {
    289 	struct	mbuf *m;		/* a copy of the packet */
    290 	struct	ifnet *ifp;		/* interface pkt came in on */
    291 #ifdef UPCALL_TIMING
    292 	struct	timeval t;		/* timestamp */
    293 #endif /* UPCALL_TIMING */
    294 	struct	rtdetq *next;
    295 };
    296 
    297 #define	MFCTBLSIZ	256
    298 #define	MAX_UPQ		4		/* max. no of pkts in upcall Q */
    299 
    300 /*
    301  * Token bucket filter code
    302  */
    303 #define	MAX_BKT_SIZE    10000		/* 10K bytes size */
    304 #define	MAXQSIZE        10		/* max. no of pkts in token queue */
    305 
    306 /*
    307  * Structure for measuring the bandwidth and sending an upcall if the
    308  * measured bandwidth is above or below a threshold.
    309  */
    310 struct bw_meter {
    311 	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
    312 	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
    313 	uint32_t	bm_time_hash;		/* the time hash value       */
    314 	struct mfc	*bm_mfc;		/* the corresponding mfc     */
    315 	uint32_t	bm_flags;		/* misc flags (see below)    */
    316 #define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
    317 #define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
    318 #define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
    319 #define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
    320 #define BW_METER_USER_FLAGS 	(BW_METER_UNIT_PACKETS |		\
    321 				 BW_METER_UNIT_BYTES |			\
    322 				 BW_METER_GEQ |				\
    323 				 BW_METER_LEQ)
    324 
    325 #define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
    326 
    327 	struct bw_data	bm_threshold;		/* the upcall threshold	     */
    328 	struct bw_data	bm_measured;		/* the measured bw	     */
    329 	struct timeval	bm_start_time;		/* abs. time		     */
    330 };
    331 
    332 struct sockopt; /* from <sys/socketvar.h> */
    333 
    334 int	ip_mrouter_set(struct socket *, struct sockopt *);
    335 int	ip_mrouter_get(struct socket *, struct sockopt *);
    336 int	mrt_ioctl(struct socket *, u_long, void *);
    337 int	ip_mrouter_done(void);
    338 void	ip_mrouter_detach(struct ifnet *);
    339 void	reset_vif(struct vif *);
    340 int	ip_mforward(struct mbuf *, struct ifnet *);
    341 
    342 #endif /* _KERNEL */
    343 
    344 #endif /* !_NETINET_IP_MROUTE_H_ */
    345