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 <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <errno.h>
43#include <unistd.h>
44#include <err.h>
45#include <ctype.h>
46#include <net/if.h>
47#include <net/if_dl.h>
48#include <net/if_mib.h>
49#include <net/route.h>
50#include <netinet/in.h>
51#include <arpa/inet.h>
52
53#include "asn1.h"
54#include "snmp.h"
55#include "snmpmod.h"
56#include "snmp_mibII.h"
57#include "mibII_tree.h"
58
59/*
60 * Interface list and flags.
61 */
62TAILQ_HEAD(mibif_list, mibif);
63enum {
64	MIBIF_FOUND		= 0x0001,
65	MIBIF_HIGHSPEED		= 0x0002,
66	MIBIF_VERYHIGHSPEED	= 0x0004,
67};
68
69/*
70 * Private mibif data - hang off from the mibif.
71 */
72struct mibif_private {
73	uint64_t	hc_inoctets;
74	uint64_t	hc_outoctets;
75	uint64_t	hc_omcasts;
76	uint64_t	hc_opackets;
77	uint64_t	hc_imcasts;
78	uint64_t	hc_ipackets;
79};
80#define	MIBIF_PRIV(IFP) ((struct mibif_private *)((IFP)->private))
81
82/*
83 * Interface addresses.
84 */
85TAILQ_HEAD(mibifa_list, mibifa);
86enum {
87	MIBIFA_FOUND	 = 0x0001,
88	MIBIFA_DESTROYED = 0x0002,
89};
90
91/*
92 * Receive addresses
93 */
94TAILQ_HEAD(mibrcvaddr_list, mibrcvaddr);
95enum {
96	MIBRCVADDR_FOUND	= 0x00010000,
97};
98
99/*
100 * Interface index mapping. The problem here is, that if the same interface
101 * is reinstantiated (for examble by unloading and loading the hardware driver)
102 * we must use the same index for this interface. For dynamic interfaces
103 * (clip, lane) we must use a fresh index, each time a new interface is created.
104 * To differentiate between these types of interfaces we use the following table
105 * which contains an entry for each dynamic interface type. All other interface
106 * types are supposed to be static. The mibindexmap contains an entry for
107 * all interfaces. The mibif pointer is NULL, if the interface doesn't exist
108 * anymore.
109 */
110struct mibdynif {
111	SLIST_ENTRY(mibdynif) link;
112	char	name[IFNAMSIZ];
113};
114SLIST_HEAD(mibdynif_list, mibdynif);
115
116struct mibindexmap {
117	STAILQ_ENTRY(mibindexmap) link;
118	u_short		sysindex;
119	u_int		ifindex;
120	struct mibif	*mibif;		/* may be NULL */
121	char		name[IFNAMSIZ];
122};
123STAILQ_HEAD(mibindexmap_list, mibindexmap);
124
125/*
126 * Interface stacking. The generic code cannot know how the interfaces stack.
127 * For this reason it instantiates only the x.0 and 0.x table elements. All
128 * others have to be instantiated by the interface specific modules.
129 * The table is read-only.
130 */
131struct mibifstack {
132	TAILQ_ENTRY(mibifstack) link;
133	struct asn_oid index;
134};
135TAILQ_HEAD(mibifstack_list, mibifstack);
136
137/*
138 * NetToMediaTable (ArpTable)
139 */
140struct mibarp {
141	TAILQ_ENTRY(mibarp) link;
142	struct asn_oid	index;		/* contains both the ifindex and addr */
143	u_char		phys[128];	/* the physical address */
144	u_int		physlen;	/* and its length */
145	u_int		flags;
146};
147TAILQ_HEAD(mibarp_list, mibarp);
148enum {
149	MIBARP_FOUND	= 0x00010000,
150	MIBARP_PERM	= 0x00000001,
151};
152
153/*
154 * New if registrations
155 */
156struct newifreg {
157	TAILQ_ENTRY(newifreg) link;
158	const struct lmodule *mod;
159	int	(*func)(struct mibif *);
160};
161TAILQ_HEAD(newifreg_list, newifreg);
162
163/* list of all IP addresses */
164extern struct mibifa_list mibifa_list;
165
166/* list of all interfaces */
167extern struct mibif_list mibif_list;
168
169/* list of dynamic interface names */
170extern struct mibdynif_list mibdynif_list;
171
172/* list of all interface index mappings */
173extern struct mibindexmap_list mibindexmap_list;
174
175/* list of all stacking entries */
176extern struct mibifstack_list mibifstack_list;
177
178/* list of all receive addresses */
179extern struct mibrcvaddr_list mibrcvaddr_list;
180
181/* list of all NetToMedia entries */
182extern struct mibarp_list mibarp_list;
183
184/* number of interfaces */
185extern int32_t mib_if_number;
186
187/* last change of interface table */
188extern uint64_t mib_iftable_last_change;
189
190/* last change of stack table */
191extern uint64_t mib_ifstack_last_change;
192
193/* if this is set, one of our lists may be bad. refresh them when idle */
194extern int mib_iflist_bad;
195
196/* last time refreshed */
197extern uint64_t mibarpticks;
198
199/* info on system clocks */
200extern struct clockinfo clockinfo;
201
202/* baud rate of fastest interface */
203extern uint64_t mibif_maxspeed;
204
205/* user-forced update interval */
206extern u_int mibif_force_hc_update_interval;
207
208/* current update interval */
209extern u_int mibif_hc_update_interval;
210
211/* re-compute update interval */
212void mibif_reset_hc_timer(void);
213
214/* interfaces' data poll interval */
215extern u_int mibII_poll_ticks;
216
217/* restart the data poll timer */
218void mibif_restart_mibII_poll_timer(void);
219
220#define MIBII_POLL_TICKS	100
221
222/* get interfaces and interface addresses. */
223void mib_fetch_interfaces(void);
224
225/* check whether this interface(type) is dynamic */
226int mib_if_is_dyn(const char *name);
227
228/* destroy an interface address */
229int mib_destroy_ifa(struct mibifa *);
230
231/* restituate a deleted interface address */
232void mib_undestroy_ifa(struct mibifa *);
233
234/* change interface address */
235int mib_modify_ifa(struct mibifa *);
236
237/* undo if address modification */
238void mib_unmodify_ifa(struct mibifa *);
239
240/* create an interface address */
241struct mibifa * mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask, struct in_addr bcast);
242
243/* delete a freshly created address */
244void mib_uncreate_ifa(struct mibifa *);
245
246/* create/delete arp entries */
247struct mibarp *mib_arp_create(const struct mibif *, struct in_addr, const u_char *, size_t);
248void mib_arp_delete(struct mibarp *);
249
250/* find arp entry */
251struct mibarp *mib_find_arp(const struct mibif *, struct in_addr);
252
253/* update arp table */
254void mib_arp_update(void);
255
256/* fetch routing table */
257u_char *mib_fetch_rtab(int af, int info, int arg, size_t *lenp);
258
259/* process routing message */
260void mib_sroute_process(struct rt_msghdr *, struct sockaddr *,
261    struct sockaddr *, struct sockaddr *);
262
263/* send a routing message */
264void mib_send_rtmsg(struct rt_msghdr *, struct sockaddr *,
265    struct sockaddr *, struct sockaddr *);
266
267/* extract addresses from routing message */
268void mib_extract_addrs(int, u_char *, struct sockaddr **);
269
270/* fetch routing table */
271int mib_fetch_route(void);
272