xref: /openbsd/libexec/snmpd/snmpd_metrics/snmpd.h (revision 3bef86f7)
1 /*	$OpenBSD: snmpd.h,v 1.1.1.1 2022/09/01 14:20:33 martijn Exp $	*/
2 
3 /*
4  * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef SNMPD_H
21 #define SNMPD_H
22 
23 #include <sys/tree.h>
24 
25 #include <net/if.h>
26 #include <net/if_dl.h>
27 #include <netinet/in.h>
28 #include <netinet/if_ether.h>
29 #include <netinet/ip.h>
30 #include <arpa/inet.h>
31 #include <net/pfvar.h>
32 #include <net/route.h>
33 
34 #include <ber.h>
35 #include <stdio.h>
36 #include <imsg.h>
37 
38 #include "log.h"
39 
40 #ifndef nitems
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #endif
43 
44 /*
45  * common definitions for snmpd
46  */
47 
48 #define CONF_FILE		"/etc/snmpd.conf"
49 #define SNMPD_SOCKET		"/var/run/snmpd.sock"
50 #define SNMPD_USER		"_snmpd"
51 #define SNMP_PORT		"161"
52 #define SNMPTRAP_PORT		"162"
53 
54 #define SNMPD_MAXSTRLEN		484
55 #define SNMPD_MAXCOMMUNITYLEN	SNMPD_MAXSTRLEN
56 #define SNMPD_MAXVARBIND	0x7fffffff
57 #define SNMPD_MAXVARBINDLEN	1210
58 #define SNMPD_MAXENGINEIDLEN	32
59 #define SNMPD_MAXUSERNAMELEN	32
60 #define SNMPD_MAXCONTEXNAMELEN	32
61 
62 #define SNMP_USM_MAXDIGESTLEN	48
63 #define SNMP_USM_SALTLEN	8
64 #define SNMP_USM_KEYLEN		64
65 #define SNMP_CIPHER_KEYLEN	16
66 
67 #define SMALL_READ_BUF_SIZE	1024
68 #define READ_BUF_SIZE		65535
69 #define	RT_BUF_SIZE		16384
70 #define	MAX_RTSOCK_BUF		(2 * 1024 * 1024)
71 
72 #define SNMP_ENGINEID_OLD	0x00
73 #define SNMP_ENGINEID_NEW	0x80	/* RFC3411 */
74 
75 #define SNMP_ENGINEID_FMT_IPv4	1
76 #define SNMP_ENGINEID_FMT_IPv6	2
77 #define SNMP_ENGINEID_FMT_MAC	3
78 #define SNMP_ENGINEID_FMT_TEXT	4
79 #define SNMP_ENGINEID_FMT_OCT	5
80 #define SNMP_ENGINEID_FMT_HH	129
81 
82 #define PEN_OPENBSD		30155
83 
84 #if DEBUG
85 #define DPRINTF		log_debug
86 #else
87 #define DPRINTF(x...)	do {} while(0)
88 #endif
89 
90 /*
91  * kroute
92  */
93 
94 struct kroute_node;
95 struct kroute6_node;
96 RB_HEAD(kroute_tree, kroute_node);
97 RB_HEAD(kroute6_tree, kroute6_node);
98 
99 struct ktable {
100 	struct kroute_tree	 krt;
101 	struct kroute6_tree	 krt6;
102 	u_int			 rtableid;
103 	u_int			 rdomain;
104 };
105 
106 union kaddr {
107 	struct sockaddr		sa;
108 	struct sockaddr_in	sin;
109 	struct sockaddr_in6	sin6;
110 	struct sockaddr_dl	sdl;
111 	char			pad[32];
112 };
113 
114 struct kroute {
115 	struct in_addr	prefix;
116 	struct in_addr	nexthop;
117 	u_long		ticks;
118 	u_int16_t	flags;
119 	u_short		if_index;
120 	u_int8_t	prefixlen;
121 	u_int8_t	priority;
122 };
123 
124 struct kroute6 {
125 	struct in6_addr	prefix;
126 	struct in6_addr	nexthop;
127 	u_long		ticks;
128 	u_int16_t	flags;
129 	u_short		if_index;
130 	u_int8_t	prefixlen;
131 	u_int8_t	priority;
132 };
133 
134 struct kif_addr {
135 	u_short			 if_index;
136 	union kaddr		 addr;
137 	union kaddr		 mask;
138 	union kaddr		 dstbrd;
139 
140 	TAILQ_ENTRY(kif_addr)	 entry;
141 	RB_ENTRY(kif_addr)	 node;
142 };
143 
144 struct kif_arp {
145 	u_short			 flags;
146 	u_short			 if_index;
147 	union kaddr		 addr;
148 	union kaddr		 target;
149 
150 	TAILQ_ENTRY(kif_arp)	 entry;
151 };
152 
153 struct kif {
154 	char			 if_name[IF_NAMESIZE];
155 	char			 if_descr[IFDESCRSIZE];
156 	u_int8_t		 if_lladdr[ETHER_ADDR_LEN];
157 	struct if_data		 if_data;
158 	u_long			 if_ticks;
159 	int			 if_flags;
160 	u_short			 if_index;
161 };
162 #define	if_mtu		if_data.ifi_mtu
163 #define	if_type		if_data.ifi_type
164 #define	if_addrlen	if_data.ifi_addrlen
165 #define	if_hdrlen	if_data.ifi_hdrlen
166 #define	if_metric	if_data.ifi_metric
167 #define	if_link_state	if_data.ifi_link_state
168 #define	if_baudrate	if_data.ifi_baudrate
169 #define	if_ipackets	if_data.ifi_ipackets
170 #define	if_ierrors	if_data.ifi_ierrors
171 #define	if_opackets	if_data.ifi_opackets
172 #define	if_oerrors	if_data.ifi_oerrors
173 #define	if_collisions	if_data.ifi_collisions
174 #define	if_ibytes	if_data.ifi_ibytes
175 #define	if_obytes	if_data.ifi_obytes
176 #define	if_imcasts	if_data.ifi_imcasts
177 #define	if_omcasts	if_data.ifi_omcasts
178 #define	if_iqdrops	if_data.ifi_iqdrops
179 #define	if_oqdrops	if_data.ifi_oqdrops
180 #define	if_noproto	if_data.ifi_noproto
181 #define	if_lastchange	if_data.ifi_lastchange
182 #define	if_capabilities	if_data.ifi_capabilities
183 
184 #define F_CONNECTED		0x0001
185 #define F_STATIC		0x0002
186 #define F_BLACKHOLE		0x0004
187 #define F_REJECT		0x0008
188 #define F_DYNAMIC		0x0010
189 
190 /*
191  * pf
192  */
193 
194 enum {	PFRB_TABLES = 1, PFRB_TSTATS, PFRB_ADDRS, PFRB_ASTATS,
195 	PFRB_IFACES, PFRB_TRANS, PFRB_MAX };
196 
197 enum {  IN, OUT };
198 enum {  IPV4, IPV6 };
199 enum {  PASS, BLOCK };
200 
201 enum {  PFI_IFTYPE_GROUP, PFI_IFTYPE_INSTANCE };
202 
203 struct pfr_buffer {
204 	int	 pfrb_type;	/* type of content, see enum above */
205 	int	 pfrb_size;	/* number of objects in buffer */
206 	int	 pfrb_msize;	/* maximum number of objects in buffer */
207 	void	*pfrb_caddr;	/* malloc'ated memory area */
208 };
209 
210 #define PFRB_FOREACH(var, buf)				\
211 	for ((var) = pfr_buf_next((buf), NULL);		\
212 	    (var) != NULL;				\
213 	    (var) = pfr_buf_next((buf), (var)))
214 
215 /*
216  * daemon structures
217  */
218 
219 struct snmpd {
220 	int			 sc_ncpu;
221 	int64_t			*sc_cpustates;
222 	int			 sc_rtfilter;
223 };
224 
225 extern struct snmpd *snmpd_env;
226 
227 /* mib.c */
228 u_long   smi_getticks(void);
229 
230 /* kroute.c */
231 void		 kr_init(void);
232 void		 kr_shutdown(void);
233 
234 u_int		 kr_ifnumber(void);
235 u_long		 kr_iflastchange(void);
236 int		 kr_updateif(u_int);
237 u_long		 kr_routenumber(void);
238 
239 struct kif	*kr_getif(u_short);
240 struct kif	*kr_getnextif(u_short);
241 struct kif_addr *kr_getaddr(struct sockaddr *);
242 struct kif_addr *kr_getnextaddr(struct sockaddr *);
243 
244 struct kroute	*kroute_first(void);
245 struct kroute	*kroute_getaddr(in_addr_t, u_int8_t, u_int8_t, int);
246 
247 struct kif_arp	*karp_first(u_short);
248 struct kif_arp	*karp_getaddr(struct sockaddr *, u_short, int);
249 
250 /* pf.c */
251 void			 pf_init(void);
252 int			 pf_get_stats(struct pf_status *);
253 int			 pfr_get_astats(struct pfr_table *, struct pfr_astats *,
254 			    int *, int);
255 int			 pfr_get_tstats(struct pfr_table *, struct pfr_tstats *,
256 			    int *, int);
257 int			 pfr_buf_grow(struct pfr_buffer *, int);
258 const void		*pfr_buf_next(struct pfr_buffer *, const void *);
259 int			 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
260 int			 pfi_get(struct pfr_buffer *, const char *);
261 int			 pfi_count(void);
262 int			 pfi_get_if(struct pfi_kif *, int);
263 int			 pft_get(struct pfr_buffer *, struct pfr_table *);
264 int			 pft_count(void);
265 int			 pft_get_table(struct pfr_tstats *, int);
266 int			 pfta_get(struct pfr_buffer *, struct pfr_table *);
267 int			 pfta_get_addr(struct pfr_astats *, int);
268 int			 pfta_get_nextaddr(struct pfr_astats *, int *);
269 int			 pfta_get_first(struct pfr_astats *);
270 
271 /* timer.c */
272 void		 timer_init(void);
273 
274 /* util.c */
275 ssize_t	 sendtofrom(int, void *, size_t, int, struct sockaddr *,
276 	    socklen_t, struct sockaddr *, socklen_t);
277 ssize_t	 recvfromto(int, void *, size_t, int, struct sockaddr *,
278 	    socklen_t *, struct sockaddr *, socklen_t *);
279 const char *log_in6addr(const struct in6_addr *);
280 const char *print_host(struct sockaddr_storage *, char *, size_t);
281 char	*tohexstr(u_int8_t *, int);
282 uint8_t *fromhexstr(uint8_t *, const char *, size_t);
283 
284 #endif /* SNMPD_H */
285