xref: /netbsd-src/sys/netinet/ip_mroute.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ip_mroute.h,v 1.28 2007/07/09 21:11:11 ad 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 /*
43  * Types and macros for handling bitmaps with one bit per virtual interface.
44  */
45 #define	MAXVIFS 32
46 typedef u_int32_t vifbitmap_t;
47 typedef u_int16_t vifi_t;		/* type of a vif index */
48 
49 #define	VIFM_SET(n, m)			((m) |= (1 << (n)))
50 #define	VIFM_CLR(n, m)			((m) &= ~(1 << (n)))
51 #define	VIFM_ISSET(n, m)		((m) & (1 << (n)))
52 #define	VIFM_SETALL(m)			((m) = 0xffffffff)
53 #define	VIFM_CLRALL(m)			((m) = 0x00000000)
54 #define	VIFM_COPY(mfrom, mto)		((mto) = (mfrom))
55 #define	VIFM_SAME(m1, m2)		((m1) == (m2))
56 
57 #define	VIFF_TUNNEL	0x1		/* vif represents a tunnel end-point */
58 #define	VIFF_SRCRT	0x2		/* tunnel uses IP src routing */
59 #define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
60 
61 /*
62  * Argument structure for MRT_ADD_VIF.
63  * (MRT_DEL_VIF takes a single vifi_t argument.)
64  */
65 struct vifctl {
66 	vifi_t	  vifc_vifi;	    	/* the index of the vif to be added */
67 	u_int8_t  vifc_flags;     	/* VIFF_ flags defined below */
68 	u_int8_t  vifc_threshold; 	/* min ttl required to forward on vif */
69 	u_int32_t vifc_rate_limit;	/* max rate */
70 	struct	  in_addr vifc_lcl_addr;/* local interface address */
71 	struct	  in_addr vifc_rmt_addr;/* remote address (tunnels only) */
72 };
73 
74 /*
75  * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC.
76  * XXX if you change this, make sure to change struct mfcctl2 as well.
77  */
78 struct mfcctl {
79 	struct	 in_addr mfcc_origin;	/* ip origin of mcasts */
80 	struct	 in_addr mfcc_mcastgrp;	/* multicast group associated */
81 	vifi_t	 mfcc_parent;		/* incoming vif */
82 	u_int8_t mfcc_ttls[MAXVIFS];	/* forwarding ttls on vifs */
83 };
84 
85 /*
86  * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
87  * and extends the old struct mfcctl.
88  */
89 struct mfcctl2 {
90 	/* the mfcctl fields */
91 	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
92 	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
93 	vifi_t		mfcc_parent;		/* incoming vif		     */
94 	u_int8_t	mfcc_ttls[MAXVIFS]; 	/* forwarding ttls on vifs   */
95 
96 	/* extension fields */
97 	u_int8_t	mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
98 	struct in_addr	mfcc_rp;		/* the RP address            */
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 /*
197  * The kernel's multicast routing statistics.
198  */
199 struct mrtstat {
200 	u_long	mrts_mfc_lookups;	/* # forw. cache hash table hits */
201 	u_long	mrts_mfc_misses;	/* # forw. cache hash table misses */
202 	u_long	mrts_upcalls;		/* # calls to mrouted */
203 	u_long	mrts_no_route;		/* no route for packet's origin */
204 	u_long	mrts_bad_tunnel;	/* malformed tunnel options */
205 	u_long	mrts_cant_tunnel;	/* no room for tunnel options */
206 	u_long	mrts_wrong_if;		/* arrived on wrong interface */
207 	u_long	mrts_upq_ovflw;		/* upcall Q overflow */
208 	u_long	mrts_cache_cleanups;	/* # entries with no upcalls */
209 	u_long	mrts_drop_sel;     	/* pkts dropped selectively */
210 	u_long	mrts_q_overflow;    	/* pkts dropped - Q overflow */
211 	u_long	mrts_pkt2large;     	/* pkts dropped - size > BKT SIZE */
212 	u_long	mrts_upq_sockfull;	/* upcalls dropped - socket full */
213 };
214 
215 
216 #ifdef _KERNEL
217 
218 /*
219  * The kernel's virtual-interface structure.
220  */
221 struct encaptab;
222 struct vif {
223 	struct	  mbuf *tbf_q, **tbf_t;	/* packet queue */
224 	struct	  timeval tbf_last_pkt_t; /* arr. time of last pkt */
225 	u_int32_t tbf_n_tok;		/* no of tokens in bucket */
226 	u_int32_t tbf_q_len;		/* length of queue at this vif */
227 	u_int32_t tbf_max_q_len;	/* max. queue length */
228 
229 	u_int8_t  v_flags;		/* VIFF_ flags defined above */
230 	u_int8_t  v_threshold;		/* min ttl required to forward on vif */
231 	u_int32_t v_rate_limit;		/* max rate */
232 	struct	  in_addr v_lcl_addr;	/* local interface address */
233 	struct	  in_addr v_rmt_addr;	/* remote address (tunnels only) */
234 	struct	  ifnet *v_ifp;		/* pointer to interface */
235 	u_long	  v_pkt_in;		/* # pkts in on interface */
236 	u_long	  v_pkt_out;		/* # pkts out on interface */
237 	u_long	  v_bytes_in;		/* # bytes in on interface */
238 	u_long	  v_bytes_out;		/* # bytes out on interface */
239 	struct	  route v_route;	/* cached route if this is a tunnel */
240 	callout_t v_repq_ch;		/* for tbf_reprocess_q() */
241 #ifdef RSVP_ISI
242 	int	  v_rsvp_on;		/* # RSVP listening on this vif */
243 	struct	  socket *v_rsvpd;	/* # RSVPD daemon */
244 #endif /* RSVP_ISI */
245 	const struct encaptab *v_encap_cookie;
246 };
247 
248 /*
249  * The kernel's multicast forwarding cache entry structure.
250  * (A field for the type of service (mfc_tos) is to be added
251  * at a future point.)
252  */
253 struct mfc {
254 	LIST_ENTRY(mfc) mfc_hash;
255 	struct	 in_addr mfc_origin;	 	/* ip origin of mcasts */
256 	struct	 in_addr mfc_mcastgrp;  	/* multicast group associated */
257 	vifi_t	 mfc_parent;			/* incoming vif */
258 	u_int8_t mfc_ttls[MAXVIFS]; 		/* forwarding ttls on vifs */
259 	u_long	 mfc_pkt_cnt;			/* pkt count for src-grp */
260 	u_long	 mfc_byte_cnt;			/* byte count for src-grp */
261 	u_long	 mfc_wrong_if;			/* wrong if for src-grp	*/
262 	int	 mfc_expire;			/* time to clean entry up */
263 	struct	 timeval mfc_last_assert;	/* last time I sent an assert */
264 	struct	 rtdetq *mfc_stall;		/* pkts waiting for route */
265 	u_int8_t mfc_flags[MAXVIFS];		/* the MRT_MFC_FLAGS_* flags */
266 	struct	 in_addr mfc_rp;		/* the RP address	     */
267 	struct	 bw_meter *mfc_bw_meter;	/* list of bandwidth meters  */
268 };
269 
270 /*
271  * Structure used to communicate from kernel to multicast router.
272  * (Note the convenient similarity to an IP packet.)
273  */
274 struct igmpmsg {
275 	u_int32_t unused1;
276 	u_int32_t unused2;
277 	u_int8_t  im_msgtype;		/* what type of message */
278 #define IGMPMSG_NOCACHE		1	/* no MFC in the kernel		    */
279 #define IGMPMSG_WRONGVIF	2	/* packet came from wrong interface */
280 #define	IGMPMSG_WHOLEPKT	3	/* PIM pkt for user level encap.    */
281 #define	IGMPMSG_BW_UPCALL	4	/* BW monitoring upcall		    */
282 	u_int8_t  im_mbz;		/* must be zero */
283 	u_int8_t  im_vif;		/* vif rec'd on */
284 	u_int8_t  unused3;
285 	struct	  in_addr im_src, im_dst;
286 } __attribute__((__packed__));
287 
288 /*
289  * Argument structure used for pkt info. while upcall is made.
290  */
291 struct rtdetq {
292 	struct	mbuf *m;		/* a copy of the packet */
293 	struct	ifnet *ifp;		/* interface pkt came in on */
294 #ifdef UPCALL_TIMING
295 	struct	timeval t;		/* timestamp */
296 #endif /* UPCALL_TIMING */
297 	struct	rtdetq *next;
298 };
299 
300 #define	MFCTBLSIZ	256
301 #define	MAX_UPQ		4		/* max. no of pkts in upcall Q */
302 
303 /*
304  * Token bucket filter code
305  */
306 #define	MAX_BKT_SIZE    10000		/* 10K bytes size */
307 #define	MAXQSIZE        10		/* max. no of pkts in token queue */
308 
309 /*
310  * Structure for measuring the bandwidth and sending an upcall if the
311  * measured bandwidth is above or below a threshold.
312  */
313 struct bw_meter {
314 	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
315 	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
316 	uint32_t	bm_time_hash;		/* the time hash value       */
317 	struct mfc	*bm_mfc;		/* the corresponding mfc     */
318 	uint32_t	bm_flags;		/* misc flags (see below)    */
319 #define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
320 #define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
321 #define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
322 #define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
323 #define BW_METER_USER_FLAGS 	(BW_METER_UNIT_PACKETS |		\
324 				 BW_METER_UNIT_BYTES |			\
325 				 BW_METER_GEQ |				\
326 				 BW_METER_LEQ)
327 
328 #define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
329 
330 	struct bw_data	bm_threshold;		/* the upcall threshold	     */
331 	struct bw_data	bm_measured;		/* the measured bw	     */
332 	struct timeval	bm_start_time;		/* abs. time		     */
333 };
334 
335 int	ip_mrouter_set(struct socket *, int, struct mbuf **);
336 int	ip_mrouter_get(struct socket *, int, struct mbuf **);
337 int	mrt_ioctl(struct socket *, u_long, void *);
338 int	ip_mrouter_done(void);
339 void	ip_mrouter_detach(struct ifnet *);
340 void	reset_vif(struct vif *);
341 #ifdef RSVP_ISI
342 int	ip_mforward(struct mbuf *, struct ifnet *, struct ip_moptions *);
343 int	legal_vif_num(int);
344 int	ip_rsvp_vif_init(struct socket *, struct mbuf *);
345 int	ip_rsvp_vif_done(struct socket *, struct mbuf *);
346 void	ip_rsvp_force_done(struct socket *);
347 void	rsvp_input(struct mbuf *, int, int);
348 #else
349 int	ip_mforward(struct mbuf *, struct ifnet *);
350 #endif
351 
352 #endif /* _KERNEL */
353 
354 #endif /* !_NETINET_IP_MROUTE_H_ */
355