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