xref: /dragonfly/sys/net/ip_mroute/ip_mroute.h (revision bfb67b1b)
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  * 3. 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: src/sys/netinet/ip_mroute.h,v 1.17.2.3 2003/08/24 08:24:38 hsu Exp $
35  * $DragonFly: src/sys/net/ip_mroute/ip_mroute.h,v 1.3 2003/08/24 23:07:07 hsu Exp $
36  */
37 
38 #ifndef _NETINET_IP_MROUTE_H_
39 #define _NETINET_IP_MROUTE_H_
40 
41 /*
42  * Definitions for IP multicast forwarding.
43  *
44  * Written by David Waitzman, BBN Labs, August 1988.
45  * Modified by Steve Deering, Stanford, February 1989.
46  * Modified by Ajit Thyagarajan, PARC, August 1993.
47  * Modified by Ajit Thyagarajan, PARC, August 1994.
48  * Modified by Ahmed Helmy, SGI, June 1996.
49  * Modified by Pavlin Radoslavov, ICSI, October 2002.
50  *
51  * MROUTING Revision: 3.3.1.3
52  * and PIM-SMv2 and PIM-DM support, advanced API support,
53  * bandwidth metering and signaling.
54  */
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 #define GET_TIME(t)	microtime(&t)
76 
77 /*
78  * Types and macros for handling bitmaps with one bit per virtual interface.
79  */
80 #define	MAXVIFS 32
81 typedef u_long vifbitmap_t;
82 typedef u_short vifi_t;		/* type of a vif index */
83 #define ALL_VIFS (vifi_t)-1
84 
85 #define	VIFM_SET(n, m)		((m) |= (1 << (n)))
86 #define	VIFM_CLR(n, m)		((m) &= ~(1 << (n)))
87 #define	VIFM_ISSET(n, m)	((m) & (1 << (n)))
88 #define	VIFM_CLRALL(m)		((m) = 0x00000000)
89 #define	VIFM_COPY(mfrom, mto)	((mto) = (mfrom))
90 #define	VIFM_SAME(m1, m2)	((m1) == (m2))
91 
92 
93 /*
94  * Argument structure for MRT_ADD_VIF.
95  * (MRT_DEL_VIF takes a single vifi_t argument.)
96  */
97 struct vifctl {
98 	vifi_t	vifc_vifi;	    	/* the index of the vif to be added */
99 	u_char	vifc_flags;     	/* VIFF_ flags defined below */
100 	u_char	vifc_threshold; 	/* min ttl required to forward on vif */
101 	u_int	vifc_rate_limit;	/* max rate */
102 	struct	in_addr vifc_lcl_addr;	/* local interface address */
103 	struct	in_addr vifc_rmt_addr;	/* remote address (tunnels only) */
104 };
105 
106 #define	VIFF_TUNNEL	0x1		/* vif represents a tunnel end-point */
107 #define VIFF_SRCRT	0x2		/* tunnel uses IP source routing */
108 #define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
109 
110 /*
111  * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC
112  * XXX if you change this, make sure to change struct mfcctl2 as well.
113  */
114 struct mfcctl {
115     struct in_addr  mfcc_origin;		/* ip origin of mcasts       */
116     struct in_addr  mfcc_mcastgrp; 		/* multicast group associated*/
117     vifi_t	    mfcc_parent;   		/* incoming vif              */
118     u_char	    mfcc_ttls[MAXVIFS]; 	/* forwarding ttls on vifs   */
119 };
120 
121 /*
122  * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
123  * and extends the old struct mfcctl.
124  */
125 struct mfcctl2 {
126 	/* the mfcctl fields */
127 	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
128 	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
129 	vifi_t		mfcc_parent;		/* incoming vif		     */
130 	u_char		mfcc_ttls[MAXVIFS]; 	/* forwarding ttls on vifs   */
131 
132 	/* extension fields */
133 	uint8_t		mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
134 	struct in_addr	mfcc_rp;		/* the RP address            */
135 };
136 /*
137  * The advanced-API flags.
138  *
139  * The MRT_MFC_FLAGS_XXX API flags are also used as flags
140  * for the mfcc_flags field.
141  */
142 #define	MRT_MFC_FLAGS_DISABLE_WRONGVIF	(1 << 0) /* disable WRONGVIF signals */
143 #define	MRT_MFC_FLAGS_BORDER_VIF	(1 << 1) /* border vif		     */
144 #define MRT_MFC_RP			(1 << 8) /* enable RP address	     */
145 #define MRT_MFC_BW_UPCALL		(1 << 9) /* enable bw upcalls	     */
146 #define MRT_MFC_FLAGS_ALL		(MRT_MFC_FLAGS_DISABLE_WRONGVIF |    \
147 					 MRT_MFC_FLAGS_BORDER_VIF)
148 #define MRT_API_FLAGS_ALL		(MRT_MFC_FLAGS_ALL |		     \
149 					 MRT_MFC_RP |			     \
150 					 MRT_MFC_BW_UPCALL)
151 
152 /*
153  * Structure for installing or delivering an upcall if the
154  * measured bandwidth is above or below a threshold.
155  *
156  * User programs (e.g. daemons) may have a need to know when the
157  * bandwidth used by some data flow is above or below some threshold.
158  * This interface allows the userland to specify the threshold (in
159  * bytes and/or packets) and the measurement interval. Flows are
160  * all packet with the same source and destination IP address.
161  * At the moment the code is only used for multicast destinations
162  * but there is nothing that prevents its use for unicast.
163  *
164  * The measurement interval cannot be shorter than some Tmin (currently, 3s).
165  * The threshold is set in packets and/or bytes per_interval.
166  *
167  * Measurement works as follows:
168  *
169  * For >= measurements:
170  * The first packet marks the start of a measurement interval.
171  * During an interval we count packets and bytes, and when we
172  * pass the threshold we deliver an upcall and we are done.
173  * The first packet after the end of the interval resets the
174  * count and restarts the measurement.
175  *
176  * For <= measurement:
177  * We start a timer to fire at the end of the interval, and
178  * then for each incoming packet we count packets and bytes.
179  * When the timer fires, we compare the value with the threshold,
180  * schedule an upcall if we are below, and restart the measurement
181  * (reschedule timer and zero counters).
182  */
183 
184 struct bw_data {
185 	struct timeval	b_time;
186 	uint64_t	b_packets;
187 	uint64_t	b_bytes;
188 };
189 
190 struct bw_upcall {
191 	struct in_addr	bu_src;			/* source address            */
192 	struct in_addr	bu_dst;			/* destination address       */
193 	uint32_t	bu_flags;		/* misc flags (see below)    */
194 #define BW_UPCALL_UNIT_PACKETS   (1 << 0)	/* threshold (in packets)    */
195 #define BW_UPCALL_UNIT_BYTES     (1 << 1)	/* threshold (in bytes)      */
196 #define BW_UPCALL_GEQ            (1 << 2)	/* upcall if bw >= threshold */
197 #define BW_UPCALL_LEQ            (1 << 3)	/* upcall if bw <= threshold */
198 #define BW_UPCALL_DELETE_ALL     (1 << 4)	/* delete all upcalls for s,d*/
199 	struct bw_data	bu_threshold;		/* the bw threshold	     */
200 	struct bw_data	bu_measured;		/* the measured bw	     */
201 };
202 
203 /* max. number of upcalls to deliver together */
204 #define BW_UPCALLS_MAX				128
205 /* min. threshold time interval for bandwidth measurement */
206 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC	3
207 #define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC	0
208 
209 /*
210  * The kernel's multicast routing statistics.
211  */
212 struct mrtstat {
213     u_long	mrts_mfc_lookups;	/* # forw. cache hash table hits   */
214     u_long	mrts_mfc_misses;	/* # forw. cache hash table misses */
215     u_long	mrts_upcalls;		/* # calls to mrouted              */
216     u_long	mrts_no_route;		/* no route for packet's origin    */
217     u_long	mrts_bad_tunnel;	/* malformed tunnel options        */
218     u_long	mrts_cant_tunnel;	/* no room for tunnel options      */
219     u_long	mrts_wrong_if;		/* arrived on wrong interface	   */
220     u_long	mrts_upq_ovflw;		/* upcall Q overflow		   */
221     u_long	mrts_cache_cleanups;	/* # entries with no upcalls 	   */
222     u_long  	mrts_drop_sel;     	/* pkts dropped selectively        */
223     u_long  	mrts_q_overflow;    	/* pkts dropped - Q overflow       */
224     u_long  	mrts_pkt2large;     	/* pkts dropped - size > BKT SIZE  */
225     u_long	mrts_upq_sockfull;	/* upcalls dropped - socket full */
226 };
227 
228 /*
229  * Argument structure used by mrouted to get src-grp pkt counts
230  */
231 struct sioc_sg_req {
232     struct in_addr src;
233     struct in_addr grp;
234     u_long pktcnt;
235     u_long bytecnt;
236     u_long wrong_if;
237 };
238 
239 /*
240  * Argument structure used by mrouted to get vif pkt counts
241  */
242 struct sioc_vif_req {
243     vifi_t vifi;		/* vif number				*/
244     u_long icount;		/* Input packet count on vif		*/
245     u_long ocount;		/* Output packet count on vif		*/
246     u_long ibytes;		/* Input byte count on vif		*/
247     u_long obytes;		/* Output byte count on vif		*/
248 };
249 
250 
251 /*
252  * The kernel's virtual-interface structure.
253  */
254 struct vif {
255     u_char   		v_flags;     	/* VIFF_ flags defined above         */
256     u_char   		v_threshold;	/* min ttl required to forward on vif*/
257     u_int      		v_rate_limit; 	/* max rate			     */
258     struct tbf 	       *v_tbf;       	/* token bucket structure at intf.   */
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     struct route	v_route;	/* cached route if this is a tunnel */
267     u_int		v_rsvp_on;	/* RSVP listening on this vif */
268     struct socket      *v_rsvpd;	/* RSVP daemon socket */
269 };
270 
271 /*
272  * The kernel's multicast forwarding cache entry structure
273  * (A field for the type of service (mfc_tos) is to be added
274  * at a future point)
275  */
276 struct mfc {
277 	struct in_addr	mfc_origin;		/* IP origin of mcasts	     */
278 	struct in_addr  mfc_mcastgrp;  		/* multicast group associated*/
279 	vifi_t		mfc_parent; 		/* incoming vif              */
280 	u_char		mfc_ttls[MAXVIFS]; 	/* forwarding ttls on vifs   */
281 	u_long		mfc_pkt_cnt;		/* pkt count for src-grp     */
282 	u_long		mfc_byte_cnt;		/* byte count for src-grp    */
283 	u_long		mfc_wrong_if;		/* wrong if for src-grp	     */
284 	int		mfc_expire;		/* time to clean entry up    */
285 	struct timeval	mfc_last_assert;	/* last time I sent an assert*/
286 	struct rtdetq	*mfc_stall;		/* q of packets awaiting mfc */
287 	struct mfc	*mfc_next;		/* next mfc entry            */
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;		/* list of bandwidth meters  */
291 };
292 
293 /*
294  * Struct used to communicate from kernel to multicast router
295  * note the convenient similarity to an IP packet
296  */
297 struct igmpmsg {
298     uint32_t	    unused1;
299     uint32_t	    unused2;
300     u_char	    im_msgtype;			/* what type of message	    */
301 #define IGMPMSG_NOCACHE		1	/* no MFC in the kernel		    */
302 #define IGMPMSG_WRONGVIF	2	/* packet came from wrong interface */
303 #define	IGMPMSG_WHOLEPKT	3	/* PIM pkt for user level encap.    */
304 #define	IGMPMSG_BW_UPCALL	4	/* BW monitoring upcall		    */
305     u_char	    im_mbz;			/* must be zero		    */
306     u_char	    im_vif;			/* vif rec'd on		    */
307     u_char	    unused3;
308     struct in_addr  im_src, im_dst;
309 };
310 
311 /*
312  * Argument structure used for pkt info. while upcall is made
313  */
314 struct rtdetq {
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     struct rtdetq	*next;		/* Next in list of packets          */
319 };
320 
321 #define MFCTBLSIZ	256
322 #if (MFCTBLSIZ & (MFCTBLSIZ - 1)) == 0	  /* from sys:route.h */
323 #define MFCHASHMOD(h)	((h) & (MFCTBLSIZ - 1))
324 #else
325 #define MFCHASHMOD(h)	((h) % MFCTBLSIZ)
326 #endif
327 
328 #define MAX_UPQ	4		/* max. no of pkts in upcall Q */
329 
330 /*
331  * Token Bucket filter code
332  */
333 #define MAX_BKT_SIZE    10000             /* 10K bytes size 		*/
334 #define MAXQSIZE        10                /* max # of pkts in queue 	*/
335 
336 /*
337  * the token bucket filter at each vif
338  */
339 struct tbf
340 {
341     struct timeval tbf_last_pkt_t; /* arr. time of last pkt 	*/
342     u_long tbf_n_tok;      	/* no of tokens in bucket 	*/
343     u_long tbf_q_len;    	/* length of queue at this vif	*/
344     u_long tbf_max_q_len;	/* max. queue length		*/
345     struct mbuf *tbf_q;		/* Packet queue			*/
346     struct mbuf *tbf_t;		/* tail-insertion pointer	*/
347 };
348 
349 /*
350  * Structure for measuring the bandwidth and sending an upcall if the
351  * measured bandwidth is above or below a threshold.
352  */
353 struct bw_meter {
354 	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
355 	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
356 	uint32_t	bm_time_hash;		/* the time hash value       */
357 	struct mfc	*bm_mfc;		/* the corresponding mfc     */
358 	uint32_t	bm_flags;		/* misc flags (see below)    */
359 #define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
360 #define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
361 #define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
362 #define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
363 #define BW_METER_USER_FLAGS 	(BW_METER_UNIT_PACKETS |		\
364 				 BW_METER_UNIT_BYTES |			\
365 				 BW_METER_GEQ |				\
366 				 BW_METER_LEQ)
367 
368 #define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
369 
370 	struct bw_data	bm_threshold;		/* the upcall threshold	     */
371 	struct bw_data	bm_measured;		/* the measured bw	     */
372 	struct timeval	bm_start_time;		/* abs. time		     */
373 };
374 
375 #ifdef _KERNEL
376 
377 struct sockopt;
378 
379 extern int	(*ip_mrouter_set)(struct socket *, struct sockopt *);
380 extern int	(*ip_mrouter_get)(struct socket *, struct sockopt *);
381 extern int	(*ip_mrouter_done)(void);
382 extern int	(*mrt_ioctl)(u_long, caddr_t);
383 
384 #endif /* _KERNEL */
385 
386 #endif /* _NETINET_IP_MROUTE_H_ */
387