xref: /freebsd/sys/netinet/ip_mroute.h (revision efe014e6b177ba1b0d70f7df751f5066d4eafe9b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989 Stephen Deering.
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Stephen Deering of Stanford University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)ip_mroute.h	8.1 (Berkeley) 6/10/93
36  * $FreeBSD$
37  */
38 
39 #ifndef _NETINET_IP_MROUTE_H_
40 #define _NETINET_IP_MROUTE_H_
41 
42 /*
43  * Definitions for IP multicast forwarding.
44  *
45  * Written by David Waitzman, BBN Labs, August 1988.
46  * Modified by Steve Deering, Stanford, February 1989.
47  * Modified by Ajit Thyagarajan, PARC, August 1993.
48  * Modified by Ajit Thyagarajan, PARC, August 1994.
49  * Modified by Ahmed Helmy, SGI, June 1996.
50  * Modified by Pavlin Radoslavov, ICSI, October 2002.
51  *
52  * MROUTING Revision: 3.3.1.3
53  * and PIM-SMv2 and PIM-DM support, advanced API support,
54  * bandwidth metering and signaling.
55  */
56 
57 /*
58  * Multicast Routing set/getsockopt commands.
59  */
60 #define	MRT_INIT	100	/* initialize forwarder */
61 #define	MRT_DONE	101	/* shut down forwarder */
62 #define	MRT_ADD_VIF	102	/* create virtual interface */
63 #define	MRT_DEL_VIF	103	/* delete virtual interface */
64 #define MRT_ADD_MFC	104	/* insert forwarding cache entry */
65 #define MRT_DEL_MFC	105	/* delete forwarding cache entry */
66 #define MRT_VERSION	106	/* get kernel version number */
67 #define MRT_ASSERT      107     /* enable assert processing */
68 #define MRT_PIM		MRT_ASSERT /* enable PIM processing */
69 #define MRT_API_SUPPORT	109	/* supported MRT API */
70 #define MRT_API_CONFIG	110	/* config MRT API */
71 #define MRT_ADD_BW_UPCALL 111	/* create bandwidth monitor */
72 #define MRT_DEL_BW_UPCALL 112	/* delete bandwidth monitor */
73 
74 /*
75  * Types and macros for handling bitmaps with one bit per virtual interface.
76  */
77 #define	MAXVIFS 32
78 typedef u_long vifbitmap_t;
79 typedef u_short vifi_t;		/* type of a vif index */
80 #define ALL_VIFS (vifi_t)-1
81 
82 #define	VIFM_SET(n, m)		((m) |= (1 << (n)))
83 #define	VIFM_CLR(n, m)		((m) &= ~(1 << (n)))
84 #define	VIFM_ISSET(n, m)	((m) & (1 << (n)))
85 #define	VIFM_CLRALL(m)		((m) = 0x00000000)
86 #define	VIFM_COPY(mfrom, mto)	((mto) = (mfrom))
87 #define	VIFM_SAME(m1, m2)	((m1) == (m2))
88 
89 struct mfc;
90 
91 /*
92  * Argument structure for MRT_ADD_VIF.
93  * (MRT_DEL_VIF takes a single vifi_t argument.)
94  */
95 struct vifctl {
96 	vifi_t	vifc_vifi;		/* the index of the vif to be added */
97 	u_char	vifc_flags;		/* VIFF_ flags defined below */
98 	u_char	vifc_threshold;		/* min ttl required to forward on vif */
99 	u_int	vifc_rate_limit;	/* max rate */
100 	struct	in_addr vifc_lcl_addr;	/* local interface address */
101 	struct	in_addr vifc_rmt_addr;	/* remote address (tunnels only) */
102 };
103 
104 #define	VIFF_TUNNEL	0x1		/* no-op; retained for old source */
105 #define VIFF_SRCRT	0x2		/* no-op; retained for old source */
106 #define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
107 
108 /*
109  * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC
110  * XXX if you change this, make sure to change struct mfcctl2 as well.
111  */
112 struct mfcctl {
113     struct in_addr  mfcc_origin;		/* ip origin of mcasts       */
114     struct in_addr  mfcc_mcastgrp;		/* multicast group associated*/
115     vifi_t	    mfcc_parent;		/* incoming vif              */
116     u_char	    mfcc_ttls[MAXVIFS];		/* forwarding ttls on vifs   */
117 };
118 
119 /*
120  * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
121  * and extends the old struct mfcctl.
122  */
123 struct mfcctl2 {
124 	/* the mfcctl fields */
125 	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
126 	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
127 	vifi_t		mfcc_parent;		/* incoming vif		     */
128 	u_char		mfcc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
129 
130 	/* extension fields */
131 	uint8_t		mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
132 	struct in_addr	mfcc_rp;		/* the RP address            */
133 };
134 /*
135  * The advanced-API flags.
136  *
137  * The MRT_MFC_FLAGS_XXX API flags are also used as flags
138  * for the mfcc_flags field.
139  */
140 #define	MRT_MFC_FLAGS_DISABLE_WRONGVIF	(1 << 0) /* disable WRONGVIF signals */
141 #define	MRT_MFC_FLAGS_BORDER_VIF	(1 << 1) /* border vif		     */
142 #define MRT_MFC_RP			(1 << 8) /* enable RP address	     */
143 #define MRT_MFC_BW_UPCALL		(1 << 9) /* enable bw upcalls	     */
144 #define MRT_MFC_FLAGS_ALL		(MRT_MFC_FLAGS_DISABLE_WRONGVIF |    \
145 					 MRT_MFC_FLAGS_BORDER_VIF)
146 #define MRT_API_FLAGS_ALL		(MRT_MFC_FLAGS_ALL |		     \
147 					 MRT_MFC_RP |			     \
148 					 MRT_MFC_BW_UPCALL)
149 
150 /*
151  * Structure for installing or delivering an upcall if the
152  * measured bandwidth is above or below a threshold.
153  *
154  * User programs (e.g. daemons) may have a need to know when the
155  * bandwidth used by some data flow is above or below some threshold.
156  * This interface allows the userland to specify the threshold (in
157  * bytes and/or packets) and the measurement interval. Flows are
158  * all packet with the same source and destination IP address.
159  * At the moment the code is only used for multicast destinations
160  * but there is nothing that prevents its use for unicast.
161  *
162  * The measurement interval cannot be shorter than some Tmin (currently, 3s).
163  * The threshold is set in packets and/or bytes per_interval.
164  *
165  * Measurement works as follows:
166  *
167  * For >= measurements:
168  * The first packet marks the start of a measurement interval.
169  * During an interval we count packets and bytes, and when we
170  * pass the threshold we deliver an upcall and we are done.
171  * The first packet after the end of the interval resets the
172  * count and restarts the measurement.
173  *
174  * For <= measurement:
175  * We start a timer to fire at the end of the interval, and
176  * then for each incoming packet we count packets and bytes.
177  * When the timer fires, we compare the value with the threshold,
178  * schedule an upcall if we are below, and restart the measurement
179  * (reschedule timer and zero counters).
180  */
181 
182 struct bw_data {
183 	struct timeval	b_time;
184 	uint64_t	b_packets;
185 	uint64_t	b_bytes;
186 };
187 
188 struct bw_upcall {
189 	struct in_addr	bu_src;			/* source address            */
190 	struct in_addr	bu_dst;			/* destination address       */
191 	uint32_t	bu_flags;		/* misc flags (see below)    */
192 #define BW_UPCALL_UNIT_PACKETS   (1 << 0)	/* threshold (in packets)    */
193 #define BW_UPCALL_UNIT_BYTES     (1 << 1)	/* threshold (in bytes)      */
194 #define BW_UPCALL_GEQ            (1 << 2)	/* upcall if bw >= threshold */
195 #define BW_UPCALL_LEQ            (1 << 3)	/* upcall if bw <= threshold */
196 #define BW_UPCALL_DELETE_ALL     (1 << 4)	/* delete all upcalls for s,d*/
197 	struct bw_data	bu_threshold;		/* the bw threshold	     */
198 	struct bw_data	bu_measured;		/* the measured bw	     */
199 };
200 
201 /* max. number of upcalls to deliver together */
202 #define BW_UPCALLS_MAX				1024
203 /* min. threshold time interval for bandwidth measurement */
204 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC	3
205 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC	0
206 
207 /*
208  * The kernel's multicast routing statistics.
209  */
210 struct mrtstat {
211 	uint64_t mrts_mfc_lookups;    /* # forw. cache hash table hits   */
212 	uint64_t mrts_mfc_misses;     /* # forw. cache hash table misses */
213 	uint64_t mrts_upcalls;	      /* # calls to multicast routing daemon */
214 	uint64_t mrts_no_route;	      /* no route for packet's origin    */
215 	uint64_t mrts_bad_tunnel;     /* malformed tunnel options        */
216 	uint64_t mrts_cant_tunnel;    /* no room for tunnel options      */
217 	uint64_t mrts_wrong_if;	      /* arrived on wrong interface	 */
218 	uint64_t mrts_upq_ovflw;      /* upcall Q overflow		 */
219 	uint64_t mrts_cache_cleanups; /* # entries with no upcalls	 */
220 	uint64_t mrts_drop_sel;	      /* pkts dropped selectively        */
221 	uint64_t mrts_q_overflow;     /* pkts dropped - Q overflow       */
222 	uint64_t mrts_pkt2large;      /* pkts dropped - size > BKT SIZE  */
223 	uint64_t mrts_upq_sockfull;   /* upcalls dropped - socket full   */
224 };
225 
226 #ifdef _KERNEL
227 #define	MRTSTAT_ADD(name, val)	\
228     VNET_PCPUSTAT_ADD(struct mrtstat, mrtstat, name, (val))
229 #define	MRTSTAT_INC(name)	MRTSTAT_ADD(name, 1)
230 #endif
231 
232 /*
233  * Argument structure used by mrouted to get src-grp pkt counts
234  */
235 struct sioc_sg_req {
236     struct in_addr src;
237     struct in_addr grp;
238     u_long pktcnt;
239     u_long bytecnt;
240     u_long wrong_if;
241 };
242 
243 /*
244  * Argument structure used by mrouted to get vif pkt counts
245  */
246 struct sioc_vif_req {
247     vifi_t vifi;		/* vif number				*/
248     u_long icount;		/* Input packet count on vif		*/
249     u_long ocount;		/* Output packet count on vif		*/
250     u_long ibytes;		/* Input byte count on vif		*/
251     u_long obytes;		/* Output byte count on vif		*/
252 };
253 
254 /*
255  * The kernel's virtual-interface structure.
256  */
257 struct vif {
258     u_char		v_flags;	/* VIFF_ flags defined above         */
259     u_char		v_threshold;	/* min ttl required to forward on vif*/
260     struct in_addr	v_lcl_addr;	/* local interface address           */
261     struct in_addr	v_rmt_addr;	/* remote address (tunnels only)     */
262     struct ifnet       *v_ifp;		/* pointer to interface              */
263     u_long		v_pkt_in;	/* # pkts in on interface            */
264     u_long		v_pkt_out;	/* # pkts out on interface           */
265     u_long		v_bytes_in;	/* # bytes in on interface	     */
266     u_long		v_bytes_out;	/* # bytes out on interface	     */
267 #ifdef _KERNEL
268     struct mtx		v_spin;		/* Spin mutex for pkt stats          */
269     char		v_spin_name[32];
270 #endif
271 };
272 
273 #if defined(_KERNEL) || defined (_NETSTAT)
274 /*
275  * The kernel's multicast forwarding cache entry structure
276  */
277 struct mfc {
278 	LIST_ENTRY(mfc)	mfc_hash;
279 	struct in_addr	mfc_origin;		/* IP origin of mcasts	     */
280 	struct in_addr  mfc_mcastgrp;		/* multicast group associated*/
281 	vifi_t		mfc_parent;		/* incoming vif              */
282 	u_char		mfc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
283 	u_long		mfc_pkt_cnt;		/* pkt count for src-grp     */
284 	u_long		mfc_byte_cnt;		/* byte count for src-grp    */
285 	u_long		mfc_wrong_if;		/* wrong if for src-grp	     */
286 	int		mfc_expire;		/* time to clean entry up    */
287 	struct timeval	mfc_last_assert;	/* last time I sent an assert*/
288 	uint8_t		mfc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
289 	struct in_addr	mfc_rp;			/* the RP address	     */
290 	struct bw_meter	*mfc_bw_meter_leq;	/* list of bandwidth meters
291 						   for Lower-or-EQual case   */
292 	struct bw_meter *mfc_bw_meter_geq;	/* list of bandwidth meters
293 						   for Greater-or-EQual case */
294 	struct buf_ring *mfc_stall_ring;	/* ring of awaiting mfc      */
295 };
296 #endif /* _KERNEL */
297 
298 /*
299  * Struct used to communicate from kernel to multicast router
300  * note the convenient similarity to an IP packet
301  */
302 struct igmpmsg {
303     uint32_t	    unused1;
304     uint32_t	    unused2;
305     u_char	    im_msgtype;			/* what type of message	    */
306 #define IGMPMSG_NOCACHE		1	/* no MFC in the kernel		    */
307 #define IGMPMSG_WRONGVIF	2	/* packet came from wrong interface */
308 #define	IGMPMSG_WHOLEPKT	3	/* PIM pkt for user level encap.    */
309 #define	IGMPMSG_BW_UPCALL	4	/* BW monitoring upcall		    */
310     u_char	    im_mbz;			/* must be zero		    */
311     u_char	    im_vif;			/* vif rec'd on		    */
312     u_char	    unused3;
313     struct in_addr  im_src, im_dst;
314 };
315 
316 #ifdef _KERNEL
317 /*
318  * Argument structure used for pkt info. while upcall is made
319  */
320 struct rtdetq {
321     TAILQ_ENTRY(rtdetq)	rte_link;
322     struct mbuf		*m;		/* A copy of the packet		    */
323     struct ifnet	*ifp;		/* Interface pkt came in on	    */
324     vifi_t		xmt_vif;	/* Saved copy of imo_multicast_vif  */
325 };
326 #define MAX_UPQ	4		/* max. no of pkts in upcall Q */
327 #endif /* _KERNEL */
328 
329 /*
330  * Structure for measuring the bandwidth and sending an upcall if the
331  * measured bandwidth is above or below a threshold.
332  */
333 struct bw_meter {
334 	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
335 	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
336 	struct mfc	*bm_mfc;		/* the corresponding mfc     */
337 	uint32_t	bm_flags;		/* misc flags (see below)    */
338 #define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
339 #define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
340 #define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
341 #define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
342 #define BW_METER_USER_FLAGS	(BW_METER_UNIT_PACKETS |		\
343 				 BW_METER_UNIT_BYTES |			\
344 				 BW_METER_GEQ |				\
345 				 BW_METER_LEQ)
346 
347 #define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
348 
349 	struct bw_data	bm_threshold;		/* the upcall threshold	     */
350 	struct bw_data	bm_measured;		/* the measured bw	     */
351 	struct timeval	bm_start_time;		/* abs. time		     */
352 #ifdef _KERNEL
353 	struct callout	bm_meter_callout;	/* Periodic callout          */
354 	void*		arg;			/* custom argument           */
355 	struct mtx 	bm_spin;		/* meter spin lock           */
356 	char		bm_spin_name[32];
357 #endif
358 };
359 
360 #ifdef _KERNEL
361 
362 struct sockopt;
363 
364 extern int	(*ip_mrouter_set)(struct socket *, struct sockopt *);
365 extern int	(*ip_mrouter_get)(struct socket *, struct sockopt *);
366 extern int	(*ip_mrouter_done)(void);
367 extern int	(*mrt_ioctl)(u_long, caddr_t, int);
368 
369 #define	MROUTER_RLOCK_TRACKER	struct epoch_tracker mrouter_et
370 #define	MROUTER_RLOCK()	epoch_enter_preempt(net_epoch_preempt, &mrouter_et)
371 #define	MROUTER_RUNLOCK()	epoch_exit_preempt(net_epoch_preempt, &mrouter_et)
372 #define	MROUTER_WAIT()	epoch_wait_preempt(net_epoch_preempt)
373 
374 #endif /* _KERNEL */
375 
376 #endif /* _NETINET_IP_MROUTE_H_ */
377