xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII.h (revision fa9012aef1ad596a99950f986cf1fb31111c034a)
1 /*
2  * Copyright (c) 2001-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *	All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmp_mibII/mibII.h,v 1.16 2006/02/14 09:04:19 brandt_h Exp $
30  *
31  * Implementation of the interfaces and IP groups of MIB-II.
32  */
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/syslog.h>
38 #include <sys/time.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <err.h>
46 #include <ctype.h>
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_mib.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 
54 #include "asn1.h"
55 #include "snmp.h"
56 #include "snmpmod.h"
57 #include "snmp_mibII.h"
58 #include "mibII_tree.h"
59 
60 /* maximum size of interface alias unless overridden with net.ifdescr_maxlen */
61 #define	MIBIF_ALIAS_SIZE	(64 + 1)
62 #define	MIBIF_ALIAS_SIZE_MAX	1024
63 
64 /*
65  * Interface list and flags.
66  */
67 TAILQ_HEAD(mibif_list, mibif);
68 enum {
69 	MIBIF_FOUND		= 0x0001,
70 	MIBIF_HIGHSPEED		= 0x0002,
71 	MIBIF_VERYHIGHSPEED	= 0x0004,
72 };
73 
74 /*
75  * Private mibif data - hang off from the mibif.
76  */
77 struct mibif_private {
78 	uint64_t	hc_inoctets;
79 	uint64_t	hc_outoctets;
80 	uint64_t	hc_omcasts;
81 	uint64_t	hc_opackets;
82 	uint64_t	hc_imcasts;
83 	uint64_t	hc_ipackets;
84 };
85 #define	MIBIF_PRIV(IFP) ((struct mibif_private *)((IFP)->private))
86 
87 /*
88  * Interface addresses.
89  */
90 TAILQ_HEAD(mibifa_list, mibifa);
91 enum {
92 	MIBIFA_FOUND	 = 0x0001,
93 	MIBIFA_DESTROYED = 0x0002,
94 };
95 
96 /*
97  * Receive addresses
98  */
99 TAILQ_HEAD(mibrcvaddr_list, mibrcvaddr);
100 enum {
101 	MIBRCVADDR_FOUND	= 0x00010000,
102 };
103 
104 /*
105  * Interface index mapping. The problem here is, that if the same interface
106  * is reinstantiated (for examble by unloading and loading the hardware driver)
107  * we must use the same index for this interface. For dynamic interfaces
108  * (clip, lane) we must use a fresh index, each time a new interface is created.
109  * To differentiate between these types of interfaces we use the following table
110  * which contains an entry for each dynamic interface type. All other interface
111  * types are supposed to be static. The mibindexmap contains an entry for
112  * all interfaces. The mibif pointer is NULL, if the interface doesn't exist
113  * anymore.
114  */
115 struct mibdynif {
116 	SLIST_ENTRY(mibdynif) link;
117 	char	name[IFNAMSIZ];
118 };
119 SLIST_HEAD(mibdynif_list, mibdynif);
120 
121 struct mibindexmap {
122 	STAILQ_ENTRY(mibindexmap) link;
123 	u_short		sysindex;
124 	u_int		ifindex;
125 	struct mibif	*mibif;		/* may be NULL */
126 	char		name[IFNAMSIZ];
127 };
128 STAILQ_HEAD(mibindexmap_list, mibindexmap);
129 
130 /*
131  * Interface stacking. The generic code cannot know how the interfaces stack.
132  * For this reason it instantiates only the x.0 and 0.x table elements. All
133  * others have to be instantiated by the interface specific modules.
134  * The table is read-only.
135  */
136 struct mibifstack {
137 	TAILQ_ENTRY(mibifstack) link;
138 	struct asn_oid index;
139 };
140 TAILQ_HEAD(mibifstack_list, mibifstack);
141 
142 /*
143  * NetToMediaTable (ArpTable)
144  */
145 struct mibarp {
146 	TAILQ_ENTRY(mibarp) link;
147 	struct asn_oid	index;		/* contains both the ifindex and addr */
148 	u_char		phys[128];	/* the physical address */
149 	u_int		physlen;	/* and its length */
150 	u_int		flags;
151 };
152 TAILQ_HEAD(mibarp_list, mibarp);
153 enum {
154 	MIBARP_FOUND	= 0x00010000,
155 	MIBARP_PERM	= 0x00000001,
156 };
157 
158 /*
159  * New if registrations
160  */
161 struct newifreg {
162 	TAILQ_ENTRY(newifreg) link;
163 	const struct lmodule *mod;
164 	int	(*func)(struct mibif *);
165 };
166 TAILQ_HEAD(newifreg_list, newifreg);
167 
168 /* list of all IP addresses */
169 extern struct mibifa_list mibifa_list;
170 
171 /* list of all interfaces */
172 extern struct mibif_list mibif_list;
173 
174 /* list of dynamic interface names */
175 extern struct mibdynif_list mibdynif_list;
176 
177 /* list of all interface index mappings */
178 extern struct mibindexmap_list mibindexmap_list;
179 
180 /* list of all stacking entries */
181 extern struct mibifstack_list mibifstack_list;
182 
183 /* list of all receive addresses */
184 extern struct mibrcvaddr_list mibrcvaddr_list;
185 
186 /* list of all NetToMedia entries */
187 extern struct mibarp_list mibarp_list;
188 
189 /* number of interfaces */
190 extern int32_t mib_if_number;
191 
192 /* last change of interface table */
193 extern uint64_t mib_iftable_last_change;
194 
195 /* last change of stack table */
196 extern uint64_t mib_ifstack_last_change;
197 
198 /* if this is set, one of our lists may be bad. refresh them when idle */
199 extern int mib_iflist_bad;
200 
201 /* last time refreshed */
202 extern uint64_t mibarpticks;
203 
204 /* info on system clocks */
205 extern struct clockinfo clockinfo;
206 
207 /* baud rate of fastest interface */
208 extern uint64_t mibif_maxspeed;
209 
210 /* user-forced update interval */
211 extern u_int mibif_force_hc_update_interval;
212 
213 /* current update interval */
214 extern u_int mibif_hc_update_interval;
215 
216 /* re-compute update interval */
217 void mibif_reset_hc_timer(void);
218 
219 /* interfaces' data poll interval */
220 extern u_int mibII_poll_ticks;
221 
222 /* restart the data poll timer */
223 void mibif_restart_mibII_poll_timer(void);
224 
225 #define MIBII_POLL_TICKS	100
226 
227 /* get interfaces and interface addresses. */
228 void mib_fetch_interfaces(void);
229 
230 /* check whether this interface(type) is dynamic */
231 int mib_if_is_dyn(const char *name);
232 
233 /* destroy an interface address */
234 int mib_destroy_ifa(struct mibifa *);
235 
236 /* restituate a deleted interface address */
237 void mib_undestroy_ifa(struct mibifa *);
238 
239 /* change interface address */
240 int mib_modify_ifa(struct mibifa *);
241 
242 /* undo if address modification */
243 void mib_unmodify_ifa(struct mibifa *);
244 
245 /* create an interface address */
246 struct mibifa * mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask, struct in_addr bcast);
247 
248 /* delete a freshly created address */
249 void mib_uncreate_ifa(struct mibifa *);
250 
251 /* create/delete arp entries */
252 struct mibarp *mib_arp_create(const struct mibif *, struct in_addr, const u_char *, size_t);
253 void mib_arp_delete(struct mibarp *);
254 
255 /* find arp entry */
256 struct mibarp *mib_find_arp(const struct mibif *, struct in_addr);
257 
258 /* update arp table */
259 void mib_arp_update(void);
260 
261 /* fetch routing table */
262 u_char *mib_fetch_rtab(int af, int info, int arg, size_t *lenp);
263 
264 /* process routing message */
265 void mib_sroute_process(struct rt_msghdr *, struct sockaddr *,
266     struct sockaddr *, struct sockaddr *);
267 
268 /* send a routing message */
269 void mib_send_rtmsg(struct rt_msghdr *, struct sockaddr *,
270     struct sockaddr *, struct sockaddr *);
271 
272 /* extract addresses from routing message */
273 void mib_extract_addrs(int, u_char *, struct sockaddr **);
274 
275 /* fetch routing table */
276 int mib_fetch_route(void);
277