1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 Shteryana Shopova <syrinx@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * Bridge MIB implementation for SNMPd.
29 *
30 * $FreeBSD: stable/11/usr.sbin/bsnmpd/modules/snmp_bridge/bridge_snmp.h 330449 2018-03-05 07:26:05Z eadler $
31 */
32
33#ifndef	SNMP_BRIDGE_H
34#define	SNMP_BRIDGE_H
35
36#define	SNMP_BRIDGE_ID_LEN	8
37
38typedef uint8_t	port_id[2];
39typedef u_char	bridge_id[SNMP_BRIDGE_ID_LEN];
40
41#define	SNMP_BRIDGE_MAX_PRIORITY	65535
42
43#define	SNMP_BRIDGE_MIN_AGE_TIME	10
44#define	SNMP_BRIDGE_MAX_AGE_TIME	1000000
45
46#define	SNMP_BRIDGE_MIN_TXHC		1
47#define	SNMP_BRIDGE_MAX_TXHC		10
48
49#define	SNMP_BRIDGE_MIN_MAGE		600
50#define	SNMP_BRIDGE_MAX_MAGE		4000
51
52#define	SNMP_BRIDGE_MIN_HTIME		100
53#define	SNMP_BRIDGE_MAX_HTIME		1000
54
55#define	SNMP_BRIDGE_MIN_FDELAY		400
56#define	SNMP_BRIDGE_MAX_FDELAY		3000
57
58#define	SNMP_PORT_PATHCOST_OBSOLETE	65535
59#define	SNMP_PORT_MIN_PATHCOST		0
60#define	SNMP_PORT_MAX_PATHCOST		200000000
61#define	SNMP_PORT_PATHCOST_AUTO		0
62
63#define	SNMP_BRIDGE_DATA_MAXAGE		10
64#define	SNMP_BRIDGE_DATA_MAXAGE_MIN	1
65#define	SNMP_BRIDGE_DATA_MAXAGE_MAX	300
66
67/* By default poll kernel data every 5 minutes. */
68#define	SNMP_BRIDGE_POLL_INTERVAL	(5 * 60)
69#define	SNMP_BRIDGE_POLL_INTERVAL_MIN	1
70#define	SNMP_BRIDGE_POLL_INTERVAL_MAX	3600
71
72/* Poll for a topology change once every 30 seconds. */
73#define	SNMP_BRIDGE_TC_POLL_INTERVAL	30
74
75struct bridge_if *bridge_get_default(void);
76
77void bridge_set_default(struct bridge_if *bif);
78
79const char *bridge_get_default_name(void);
80
81int bridge_get_data_maxage(void);
82
83/*
84 * Bridge Addresses Table.
85 */
86struct tp_entry {
87	uint32_t		sysindex;	/* The bridge if sysindex. */
88	int32_t			port_no;
89	enum TpFdbStatus	status;
90	uint8_t			tp_addr[ETHER_ADDR_LEN];
91	uint8_t			flags;
92	TAILQ_ENTRY(tp_entry)	tp_e;
93};
94
95/*
96 * Bridge ports.
97 * The bridge port system interface index is used for a
98 * port number. Transparent bridging statistics and STP
99 * information for a port are also contained here.
100 */
101struct bridge_port {
102	/* dot1dBase subtree objects. */
103	uint32_t	sysindex;	/* The bridge interface sysindex. */
104	int32_t		port_no;	/* The bridge member system index. */
105	int32_t		if_idx;		/* SNMP ifIndex from mibII. */
106	int8_t		span_enable;	/* Span flag set - private MIB. */
107	struct asn_oid	circuit;	/* Unused. */
108	uint32_t	dly_ex_drops;	/* Drops on output. */
109	uint32_t	dly_mtu_drops;	/* MTU exceeded drops. */
110	int32_t		status;		/* The entry status. */
111	enum TruthValue	priv_set;	/* The private flag. */
112
113	/* dot1dStp subtree objects. */
114	int32_t		path_cost;
115	int32_t		priority;
116	int32_t		design_cost;
117	uint32_t	fwd_trans;
118	char		p_name[IFNAMSIZ]; /* Not in BRIDGE-MIB. */
119	enum StpPortState	state;
120	enum dot1dStpPortEnable	enable;
121	port_id		design_port;
122	bridge_id	design_root;
123	bridge_id	design_bridge;
124
125	/* rstpMib extensions. */
126	int32_t		admin_path_cost;
127	enum TruthValue	proto_migr;
128	enum TruthValue	admin_edge;
129	enum TruthValue	oper_edge;
130	enum TruthValue	oper_ptp;
131	enum StpPortAdminPointToPointType	admin_ptp;
132
133	/* dot1dTp subtree objects. */
134	int32_t		max_info;
135	int32_t		in_frames;
136	int32_t		out_frames;
137	int32_t		in_drops;
138
139	uint8_t		flags;
140	TAILQ_ENTRY(bridge_port) b_p;
141};
142
143/*
144 * A bridge interface.
145 * The system interface index of the bridge is not required neither by the
146 * standard BRIDGE-MIB nor by the private BEGEMOT-BRIDGE-MIB, but is used
147 * as key for looking up the other info for this bridge.
148 */
149struct bridge_if {
150	/* dot1dBase subtree objects. */
151	uint32_t	sysindex;	/* The system interface index. */
152	int32_t		num_ports;	/* Number of ports. */
153	enum BaseType	br_type;	/* Bridge type. */
154	enum RowStatus	if_status;	/* Bridge status. */
155	char		bif_name[IFNAMSIZ]; /* Bridge interface name. */
156	struct ether_addr br_addr;	/* Bridge address. */
157	struct bridge_port *f_bp;	/* This bridge's first entry
158					 * in the base ports TAILQ. */
159	/* dot1dStp subtree objects. */
160	int32_t		priority;
161	int32_t		root_cost;
162	int32_t		root_port;
163	int32_t		max_age;	/* Current max age. */
164	int32_t		hello_time;	/* Current hello time. */
165	int32_t		fwd_delay;	/* Current forward delay. */
166	int32_t		hold_time;
167	int32_t		bridge_max_age;	/* Configured max age. */
168	int32_t		bridge_hello_time; /* Configured hello time. */
169	int32_t		bridge_fwd_delay; /* Configured forward delay. */
170	int32_t		tx_hold_count;
171	uint32_t	top_changes;
172	enum dot1dStpVersion	stp_version;
173	enum dot1dStpProtocolSpecification prot_spec;
174	struct timeval	last_tc_time;
175	bridge_id	design_root;
176
177	/* dot1dTp subtree objects. */
178	int32_t		lrnt_drops;	/* Dropped addresses. */
179	int32_t		age_time;	/* Address entry timeout. */
180	int32_t		num_addrs;	/* Current # of addresses in cache. */
181	int32_t		max_addrs;	/* Max # of addresses in cache. */
182	struct tp_entry	 *f_tpa;	/* This bridge's first entry in
183					 * the tp addresses TAILQ. */
184
185	time_t		entry_age;
186	time_t		ports_age;
187	time_t		addrs_age;
188	TAILQ_ENTRY(bridge_if) b_if;
189};
190
191void bridge_ifs_fini(void);
192
193struct bridge_if *bridge_if_find_ifs(uint32_t sysindex);
194
195struct bridge_if *bridge_if_find_ifname(const char *b_name);
196
197const char *bridge_if_find_name(uint32_t sysindex);
198
199int bridge_compare_sysidx(uint32_t i1, uint32_t i2);
200
201int bridge_attach_newif(struct mibif *ifp);
202
203struct bridge_if *bridge_first_bif(void);
204
205struct bridge_if *bridge_next_bif(struct bridge_if *b_pr);
206
207void bridge_remove_bif(struct bridge_if *bif);
208
209void bridge_update_all_ports(void);
210
211void bridge_update_all_addrs(void);
212
213void bridge_update_all_ifs(void);
214
215void bridge_update_all(void *arg);
216
217void bridge_update_tc_time(void *arg);
218
219void bridge_ifs_dump(void);
220
221/* Bridge ports. */
222void bridge_ports_update_listage(void);
223
224void bridge_ports_fini(void);
225
226void bridge_members_free(struct bridge_if *bif);
227
228struct bridge_port *bridge_new_port(struct mibif *mif, struct bridge_if *bif);
229
230void bridge_port_remove(struct bridge_port *bp, struct bridge_if *bif);
231
232struct bridge_port *bridge_port_bif_first(struct bridge_if *bif);
233
234struct bridge_port *bridge_port_bif_next(struct bridge_port *bp);
235
236struct bridge_port *bridge_port_find(int32_t if_idx, struct bridge_if *bif);
237
238void bridge_port_getinfo_mibif(struct mibif *m_if, struct bridge_port *bp);
239
240int bridge_getinfo_bif_ports(struct bridge_if *bif);
241
242int bridge_update_memif(struct bridge_if *bif);
243
244void bridge_ports_dump(struct bridge_if *bif);
245
246/* Bridge addresses. */
247void bridge_addrs_update_listage(void);
248
249void bridge_addrs_fini(void);
250
251void bridge_addrs_free(struct bridge_if *bif);
252
253struct tp_entry *bridge_new_addrs(uint8_t *mac, struct bridge_if *bif);
254
255void bridge_addrs_remove(struct tp_entry *te, struct bridge_if *bif);
256
257struct tp_entry *bridge_addrs_find(uint8_t *mac, struct bridge_if *bif);
258
259struct tp_entry *bridge_addrs_bif_first(struct bridge_if *bif);
260
261struct tp_entry *bridge_addrs_bif_next(struct tp_entry *te);
262
263int bridge_getinfo_bif_addrs(struct bridge_if *bif);
264
265int bridge_update_addrs(struct bridge_if *bif);
266
267void bridge_addrs_dump(struct bridge_if *bif);
268
269/* Bridge PF. */
270
271void bridge_pf_dump(void);
272
273/* System specific. */
274
275/* Open the socket for the ioctls. */
276int bridge_ioctl_init(void);
277
278/* Load bridge kernel module. */
279int bridge_kmod_load(void);
280
281/* Get the bridge interface information. */
282int bridge_getinfo_bif(struct bridge_if *bif);
283
284/* Get the bridge interface STP parameters. */
285int bridge_get_op_param(struct bridge_if *bif);
286
287/* Set the bridge priority. */
288int bridge_set_priority(struct bridge_if *bif, int32_t priority);
289
290/* Set the bridge max age. */
291int bridge_set_maxage(struct bridge_if *bif, int32_t max_age);
292
293/* Set the bridge hello time.*/
294int bridge_set_hello_time(struct bridge_if *bif, int32_t hello_time);
295
296/* Set the bridge forward delay.*/
297int bridge_set_forward_delay(struct bridge_if *bif, int32_t fwd_delay);
298
299/* Set the bridge address cache max age. */
300int bridge_set_aging_time(struct bridge_if *bif, int32_t age_time);
301
302/* Set the max number of entries in the bridge address cache. */
303int bridge_set_max_cache(struct bridge_if *bif, int32_t max_cache);
304
305/* Set the bridge TX hold count. */
306int bridge_set_tx_hold_count(struct bridge_if *bif, int32_t tx_hc);
307
308/* Set the bridge STP protocol version. */
309int bridge_set_stp_version(struct bridge_if *bif, int32_t stp_proto);
310
311/* Set the bridge interface status to up/down. */
312int bridge_set_if_up(const char* b_name, int8_t up);
313
314/* Create a bridge interface. */
315int bridge_create(const char *b_name);
316
317/* Destroy a bridge interface. */
318int bridge_destroy(const char *b_name);
319
320/* Fetch the bridge mac address. */
321u_char *bridge_get_basemac(const char *bif_name, u_char *mac, size_t mlen);
322
323/* Set a bridge member priority. */
324int bridge_port_set_priority(const char *bif_name, struct bridge_port *bp,
325    int32_t priority);
326
327/* Set a bridge member STP-enabled flag. */
328int bridge_port_set_stp_enable(const char *bif_name, struct bridge_port *bp,
329    uint32_t enable);
330
331/* Set a bridge member STP path cost. */
332int bridge_port_set_path_cost(const char *bif_name, struct bridge_port *bp,
333    int32_t path_cost);
334
335/* Set admin point-to-point link. */
336int bridge_port_set_admin_ptp(const char *bif_name, struct bridge_port *bp,
337    uint32_t admin_ptp);
338
339/* Set admin edge. */
340int bridge_port_set_admin_edge(const char *bif_name, struct bridge_port *bp,
341    uint32_t enable);
342
343/* Set 'private' flag. */
344int bridge_port_set_private(const char *bif_name, struct bridge_port *bp,
345    uint32_t priv_set);
346
347/* Add a bridge member port. */
348int bridge_port_addm(struct bridge_port *bp, const char *b_name);
349
350/* Delete a bridge member port. */
351int bridge_port_delm(struct bridge_port *bp, const char *b_name);
352
353/* Get the current value from the module for bridge PF control. */
354int32_t bridge_get_pfval(uint8_t which);
355
356/* Get/Set a bridge PF control. */
357int32_t bridge_do_pfctl(int32_t bridge_ctl, enum snmp_op op, int32_t *val);
358
359#endif /* SNMP_BRIDGE_H */
360