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