ip_mroute.h revision 330897
1132233Srwatson/*-
2107626Schris * SPDX-License-Identifier: BSD-3-Clause
3108317Sschweikh *
4108937Schris * Copyright (c) 1989 Stephen Deering.
5108937Schris * Copyright (c) 1992, 1993
6108937Schris *	The Regents of the University of California.  All rights reserved.
7108937Schris *
8108937Schris * This code is derived from software contributed to Berkeley by
9108317Sschweikh * Stephen Deering of Stanford University.
10107626Schris *
11107626Schris * Redistribution and use in source and binary forms, with or without
12107626Schris * modification, are permitted provided that the following conditions
13107626Schris * are met:
14107626Schris * 1. Redistributions of source code must retain the above copyright
15107626Schris *    notice, this list of conditions and the following disclaimer.
16107626Schris * 2. Redistributions in binary form must reproduce the above copyright
17107626Schris *    notice, this list of conditions and the following disclaimer in the
18108317Sschweikh *    documentation and/or other materials provided with the distribution.
19107626Schris * 4. Neither the name of the University nor the names of its contributors
20107626Schris *    may be used to endorse or promote products derived from this software
21107626Schris *    without specific prior written permission.
22107626Schris *
23107626Schris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24107626Schris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25107626Schris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26107626Schris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27107626Schris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28107626Schris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29107626Schris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30108317Sschweikh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31107626Schris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32115643Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33115643Sru * SUCH DAMAGE.
34206622Suqs *
35107626Schris *	@(#)ip_mroute.h	8.1 (Berkeley) 6/10/93
36107626Schris * $FreeBSD: stable/11/sys/netinet/ip_mroute.h 330897 2018-03-14 03:19:51Z eadler $
37107626Schris */
38115643Sru
39107626Schris#ifndef _NETINET_IP_MROUTE_H_
40107717Schris#define _NETINET_IP_MROUTE_H_
41107717Schris
42115643Sru/*
43107626Schris * Definitions for IP multicast forwarding.
44107717Schris *
45115643Sru * Written by David Waitzman, BBN Labs, August 1988.
46107717Schris * Modified by Steve Deering, Stanford, February 1989.
47107717Schris * Modified by Ajit Thyagarajan, PARC, August 1993.
48107717Schris * Modified by Ajit Thyagarajan, PARC, August 1994.
49115643Sru * Modified by Ahmed Helmy, SGI, June 1996.
50107717Schris * Modified by Pavlin Radoslavov, ICSI, October 2002.
51115643Sru *
52107717Schris * MROUTING Revision: 3.3.1.3
53107717Schris * and PIM-SMv2 and PIM-DM support, advanced API support,
54107717Schris * bandwidth metering and signaling.
55115643Sru */
56115643Sru
57115643Sru/*
58107626Schris * Multicast Routing set/getsockopt commands.
59107626Schris */
60107626Schris#define	MRT_INIT	100	/* initialize forwarder */
61107626Schris#define	MRT_DONE	101	/* shut down forwarder */
62107626Schris#define	MRT_ADD_VIF	102	/* create virtual interface */
63107626Schris#define	MRT_DEL_VIF	103	/* delete virtual interface */
64107626Schris#define MRT_ADD_MFC	104	/* insert forwarding cache entry */
65107626Schris#define MRT_DEL_MFC	105	/* delete forwarding cache entry */
66107626Schris#define MRT_VERSION	106	/* get kernel version number */
67107626Schris#define MRT_ASSERT      107     /* enable assert processing */
68107626Schris#define MRT_PIM		MRT_ASSERT /* enable PIM processing */
69107626Schris#define MRT_API_SUPPORT	109	/* supported MRT API */
70107626Schris#define MRT_API_CONFIG	110	/* config MRT API */
71107626Schris#define MRT_ADD_BW_UPCALL 111	/* create bandwidth monitor */
72107626Schris#define MRT_DEL_BW_UPCALL 112	/* delete bandwidth monitor */
73107626Schris
74107626Schris/*
75107626Schris * Types and macros for handling bitmaps with one bit per virtual interface.
76115643Sru */
77109598Schris#define	MAXVIFS 32
78115643Srutypedef u_long vifbitmap_t;
79115643Srutypedef u_short vifi_t;		/* type of a vif index */
80115643Sru#define ALL_VIFS (vifi_t)-1
81107626Schris
82107626Schris#define	VIFM_SET(n, m)		((m) |= (1 << (n)))
83107626Schris#define	VIFM_CLR(n, m)		((m) &= ~(1 << (n)))
84115643Sru#define	VIFM_ISSET(n, m)	((m) & (1 << (n)))
85107949Strhodes#define	VIFM_CLRALL(m)		((m) = 0x00000000)
86107626Schris#define	VIFM_COPY(mfrom, mto)	((mto) = (mfrom))
87115643Sru#define	VIFM_SAME(m1, m2)	((m1) == (m2))
88115643Sru
89115643Srustruct mfc;
90107626Schris
91107626Schris/*
92107626Schris * Argument structure for MRT_ADD_VIF.
93107626Schris * (MRT_DEL_VIF takes a single vifi_t argument.)
94115643Sru */
95107626Schrisstruct vifctl {
96115643Sru	vifi_t	vifc_vifi;		/* the index of the vif to be added */
97107626Schris	u_char	vifc_flags;		/* VIFF_ flags defined below */
98107626Schris	u_char	vifc_threshold;		/* min ttl required to forward on vif */
99115643Sru	u_int	vifc_rate_limit;	/* max rate */
100107626Schris	struct	in_addr vifc_lcl_addr;	/* local interface address */
101107626Schris	struct	in_addr vifc_rmt_addr;	/* remote address (tunnels only) */
102111052Schris};
103111052Schris
104111052Schris#define	VIFF_TUNNEL	0x1		/* no-op; retained for old source */
105111052Schris#define VIFF_SRCRT	0x2		/* no-op; retained for old source */
106111052Schris#define VIFF_REGISTER	0x4		/* used for PIM Register encap/decap */
107111052Schris
108111052Schris/*
109111052Schris * Argument structure for MRT_ADD_MFC and MRT_DEL_MFC
110111052Schris * XXX if you change this, make sure to change struct mfcctl2 as well.
111111052Schris */
112111052Schrisstruct mfcctl {
113111052Schris    struct in_addr  mfcc_origin;		/* ip origin of mcasts       */
114111052Schris    struct in_addr  mfcc_mcastgrp;		/* multicast group associated*/
115111052Schris    vifi_t	    mfcc_parent;		/* incoming vif              */
116111052Schris    u_char	    mfcc_ttls[MAXVIFS];		/* forwarding ttls on vifs   */
117111052Schris};
118111052Schris
119111052Schris/*
120111052Schris * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays
121111052Schris * and extends the old struct mfcctl.
122111052Schris */
123111052Schrisstruct mfcctl2 {
124111052Schris	/* the mfcctl fields */
125111052Schris	struct in_addr	mfcc_origin;		/* ip origin of mcasts	     */
126111052Schris	struct in_addr	mfcc_mcastgrp;		/* multicast group associated*/
127111052Schris	vifi_t		mfcc_parent;		/* incoming vif		     */
128111052Schris	u_char		mfcc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
129141357Sbrueffer
130111052Schris	/* extension fields */
131111052Schris	uint8_t		mfcc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
132111052Schris	struct in_addr	mfcc_rp;		/* the RP address            */
133111052Schris};
134111052Schris/*
135111052Schris * The advanced-API flags.
136111052Schris *
137111052Schris * The MRT_MFC_FLAGS_XXX API flags are also used as flags
138111052Schris * for the mfcc_flags field.
139111052Schris */
140111052Schris#define	MRT_MFC_FLAGS_DISABLE_WRONGVIF	(1 << 0) /* disable WRONGVIF signals */
141111052Schris#define	MRT_MFC_FLAGS_BORDER_VIF	(1 << 1) /* border vif		     */
142111052Schris#define MRT_MFC_RP			(1 << 8) /* enable RP address	     */
143111052Schris#define MRT_MFC_BW_UPCALL		(1 << 9) /* enable bw upcalls	     */
144140111Sru#define MRT_MFC_FLAGS_ALL		(MRT_MFC_FLAGS_DISABLE_WRONGVIF |    \
145107626Schris					 MRT_MFC_FLAGS_BORDER_VIF)
146107626Schris#define MRT_API_FLAGS_ALL		(MRT_MFC_FLAGS_ALL |		     \
147107626Schris					 MRT_MFC_RP |			     \
148107626Schris					 MRT_MFC_BW_UPCALL)
149115643Sru
150115643Sru/*
151115643Sru * Structure for installing or delivering an upcall if the
152107626Schris * measured bandwidth is above or below a threshold.
153107626Schris *
154107626Schris * User programs (e.g. daemons) may have a need to know when the
155107799Schris * bandwidth used by some data flow is above or below some threshold.
156107626Schris * This interface allows the userland to specify the threshold (in
157107626Schris * bytes and/or packets) and the measurement interval. Flows are
158107626Schris * all packet with the same source and destination IP address.
159140111Sru * At the moment the code is only used for multicast destinations
160107626Schris * but there is nothing that prevents its use for unicast.
161107626Schris *
162107626Schris * The measurement interval cannot be shorter than some Tmin (currently, 3s).
163107626Schris * The threshold is set in packets and/or bytes per_interval.
164107626Schris *
165107626Schris * Measurement works as follows:
166107626Schris *
167115643Sru * For >= measurements:
168132233Srwatson * The first packet marks the start of a measurement interval.
169115643Sru * During an interval we count packets and bytes, and when we
170115643Sru * pass the threshold we deliver an upcall and we are done.
171107626Schris * The first packet after the end of the interval resets the
172107626Schris * count and restarts the measurement.
173107626Schris *
174111052Schris * For <= measurement:
175107626Schris * We start a timer to fire at the end of the interval, and
176107626Schris * then for each incoming packet we count packets and bytes.
177107626Schris * When the timer fires, we compare the value with the threshold,
178107626Schris * schedule an upcall if we are below, and restart the measurement
179107626Schris * (reschedule timer and zero counters).
180107626Schris */
181132233Srwatson
182107626Schrisstruct bw_data {
183107626Schris	struct timeval	b_time;
184132233Srwatson	uint64_t	b_packets;
185107626Schris	uint64_t	b_bytes;
186107626Schris};
187107626Schris
188111052Schrisstruct bw_upcall {
189111052Schris	struct in_addr	bu_src;			/* source address            */
190111052Schris	struct in_addr	bu_dst;			/* destination address       */
191111052Schris	uint32_t	bu_flags;		/* misc flags (see below)    */
192115643Sru#define BW_UPCALL_UNIT_PACKETS   (1 << 0)	/* threshold (in packets)    */
193111052Schris#define BW_UPCALL_UNIT_BYTES     (1 << 1)	/* threshold (in bytes)      */
194115643Sru#define BW_UPCALL_GEQ            (1 << 2)	/* upcall if bw >= threshold */
195115643Sru#define BW_UPCALL_LEQ            (1 << 3)	/* upcall if bw <= threshold */
196111052Schris#define BW_UPCALL_DELETE_ALL     (1 << 4)	/* delete all upcalls for s,d*/
197111052Schris	struct bw_data	bu_threshold;		/* the bw threshold	     */
198115643Sru	struct bw_data	bu_measured;		/* the measured bw	     */
199111052Schris};
200115643Sru
201115643Sru/* max. number of upcalls to deliver together */
202115643Sru#define BW_UPCALLS_MAX				128
203111052Schris/* min. threshold time interval for bandwidth measurement */
204115643Sru#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC	3
205115643Sru#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC	0
206107626Schris
207107626Schris/*
208109271Schris * The kernel's multicast routing statistics.
209107717Schris */
210107735Schrisstruct mrtstat {
211108934Schris	uint64_t mrts_mfc_lookups;    /* # forw. cache hash table hits   */
212107626Schris	uint64_t mrts_mfc_misses;     /* # forw. cache hash table misses */
213107717Schris	uint64_t mrts_upcalls;	      /* # calls to multicast routing daemon */
214107735Schris	uint64_t mrts_no_route;	      /* no route for packet's origin    */
215112860Schris	uint64_t mrts_bad_tunnel;     /* malformed tunnel options        */
216107717Schris	uint64_t mrts_cant_tunnel;    /* no room for tunnel options      */
217107717Schris	uint64_t mrts_wrong_if;	      /* arrived on wrong interface	 */
218108111Schris	uint64_t mrts_upq_ovflw;      /* upcall Q overflow		 */
219107626Schris	uint64_t mrts_cache_cleanups; /* # entries with no upcalls	 */
220107626Schris	uint64_t mrts_drop_sel;	      /* pkts dropped selectively        */
221107626Schris	uint64_t mrts_q_overflow;     /* pkts dropped - Q overflow       */
222107626Schris	uint64_t mrts_pkt2large;      /* pkts dropped - size > BKT SIZE  */
223107626Schris	uint64_t mrts_upq_sockfull;   /* upcalls dropped - socket full   */
224107626Schris};
225115643Sru
226115643Sru#ifdef _KERNEL
227115643Sru#define	MRTSTAT_ADD(name, val)	\
228107626Schris    VNET_PCPUSTAT_ADD(struct mrtstat, mrtstat, name, (val))
229107626Schris#define	MRTSTAT_INC(name)	MRTSTAT_ADD(name, 1)
230107626Schris#endif
231107626Schris
232107626Schris/*
233131530Sru * Argument structure used by mrouted to get src-grp pkt counts
234131530Sru */
235115643Srustruct sioc_sg_req {
236107626Schris    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 */
246struct 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/*
256 * The kernel's virtual-interface structure.
257 */
258struct vif {
259    u_char		v_flags;	/* VIFF_ flags defined above         */
260    u_char		v_threshold;	/* min ttl required to forward on vif*/
261    struct in_addr	v_lcl_addr;	/* local interface address           */
262    struct in_addr	v_rmt_addr;	/* remote address (tunnels only)     */
263    struct ifnet       *v_ifp;		/* pointer to interface              */
264    u_long		v_pkt_in;	/* # pkts in on interface            */
265    u_long		v_pkt_out;	/* # pkts out on interface           */
266    u_long		v_bytes_in;	/* # bytes in on interface	     */
267    u_long		v_bytes_out;	/* # bytes out on interface	     */
268};
269
270#ifdef _KERNEL
271/*
272 * The kernel's multicast forwarding cache entry structure
273 */
274struct mfc {
275	LIST_ENTRY(mfc)	mfc_hash;
276	struct in_addr	mfc_origin;		/* IP origin of mcasts	     */
277	struct in_addr  mfc_mcastgrp;		/* multicast group associated*/
278	vifi_t		mfc_parent;		/* incoming vif              */
279	u_char		mfc_ttls[MAXVIFS];	/* forwarding ttls on vifs   */
280	u_long		mfc_pkt_cnt;		/* pkt count for src-grp     */
281	u_long		mfc_byte_cnt;		/* byte count for src-grp    */
282	u_long		mfc_wrong_if;		/* wrong if for src-grp	     */
283	int		mfc_expire;		/* time to clean entry up    */
284	struct timeval	mfc_last_assert;	/* last time I sent an assert*/
285	uint8_t		mfc_flags[MAXVIFS];	/* the MRT_MFC_FLAGS_* flags */
286	struct in_addr	mfc_rp;			/* the RP address	     */
287	struct bw_meter	*mfc_bw_meter;		/* list of bandwidth meters  */
288	u_long		mfc_nstall;		/* # of packets awaiting mfc */
289	TAILQ_HEAD(, rtdetq) mfc_stall;		/* q of packets awaiting mfc */
290};
291#endif /* _KERNEL */
292
293/*
294 * Struct used to communicate from kernel to multicast router
295 * note the convenient similarity to an IP packet
296 */
297struct 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#ifdef _KERNEL
312/*
313 * Argument structure used for pkt info. while upcall is made
314 */
315struct rtdetq {
316    TAILQ_ENTRY(rtdetq)	rte_link;
317    struct mbuf		*m;		/* A copy of the packet		    */
318    struct ifnet	*ifp;		/* Interface pkt came in on	    */
319    vifi_t		xmt_vif;	/* Saved copy of imo_multicast_vif  */
320};
321#define MAX_UPQ	4		/* max. no of pkts in upcall Q */
322#endif /* _KERNEL */
323
324/*
325 * Structure for measuring the bandwidth and sending an upcall if the
326 * measured bandwidth is above or below a threshold.
327 */
328struct bw_meter {
329	struct bw_meter	*bm_mfc_next;		/* next bw meter (same mfc)  */
330	struct bw_meter	*bm_time_next;		/* next bw meter (same time) */
331	uint32_t	bm_time_hash;		/* the time hash value       */
332	struct mfc	*bm_mfc;		/* the corresponding mfc     */
333	uint32_t	bm_flags;		/* misc flags (see below)    */
334#define BW_METER_UNIT_PACKETS	(1 << 0)	/* threshold (in packets)    */
335#define BW_METER_UNIT_BYTES	(1 << 1)	/* threshold (in bytes)      */
336#define BW_METER_GEQ		(1 << 2)	/* upcall if bw >= threshold */
337#define BW_METER_LEQ		(1 << 3)	/* upcall if bw <= threshold */
338#define BW_METER_USER_FLAGS	(BW_METER_UNIT_PACKETS |		\
339				 BW_METER_UNIT_BYTES |			\
340				 BW_METER_GEQ |				\
341				 BW_METER_LEQ)
342
343#define BW_METER_UPCALL_DELIVERED (1 << 24)	/* upcall was delivered      */
344
345	struct bw_data	bm_threshold;		/* the upcall threshold	     */
346	struct bw_data	bm_measured;		/* the measured bw	     */
347	struct timeval	bm_start_time;		/* abs. time		     */
348};
349
350#ifdef _KERNEL
351
352struct sockopt;
353
354extern int	(*ip_mrouter_set)(struct socket *, struct sockopt *);
355extern int	(*ip_mrouter_get)(struct socket *, struct sockopt *);
356extern int	(*ip_mrouter_done)(void);
357extern int	(*mrt_ioctl)(u_long, caddr_t, int);
358
359#endif /* _KERNEL */
360
361#endif /* _NETINET_IP_MROUTE_H_ */
362