1 /* $OpenBSD: snmpd.h,v 1.20 2008/07/18 12:30:06 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2007, 2008 Reyk Floeter <reyk@vantronix.net> 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 <netinet/in.h> 24 #include <netinet/if_ether.h> 25 #include <net/route.h> 26 27 #include <ber.h> 28 #include <snmp.h> 29 30 /* 31 * common definitions for snmpd 32 */ 33 34 #define CONF_FILE "/etc/snmpd.conf" 35 #define SNMPD_SOCKET "/var/run/snmpd.sock" 36 #define SNMPD_USER "_snmpd" 37 #define SNMPD_PORT 161 38 #define SNMPD_TRAPPORT 162 39 40 #define SNMPD_MAXSTRLEN 484 41 #define SNMPD_MAXCOMMUNITYLEN SNMPD_MAXSTRLEN 42 #define SNMPD_MAXVARBIND 0x7fffffff 43 #define SNMPD_MAXVARBINDLEN 1210 44 45 #define SMALL_READ_BUF_SIZE 1024 46 #define READ_BUF_SIZE 65535 47 #define RT_BUF_SIZE 16384 48 #define MAX_RTSOCK_BUF (128 * 1024) 49 50 /* 51 * imsg framework and privsep 52 */ 53 54 struct buf { 55 TAILQ_ENTRY(buf) entry; 56 u_char *buf; 57 size_t size; 58 size_t max; 59 size_t wpos; 60 size_t rpos; 61 int fd; 62 }; 63 64 struct msgbuf { 65 TAILQ_HEAD(, buf) bufs; 66 u_int32_t queued; 67 int fd; 68 }; 69 70 #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr) 71 #define MAX_IMSGSIZE 8192 72 73 struct buf_read { 74 u_char buf[READ_BUF_SIZE]; 75 u_char *rptr; 76 size_t wpos; 77 }; 78 79 struct imsg_fd { 80 TAILQ_ENTRY(imsg_fd) entry; 81 int fd; 82 }; 83 84 struct imsgbuf { 85 TAILQ_HEAD(, imsg_fd) fds; 86 struct buf_read r; 87 struct msgbuf w; 88 struct event ev; 89 void (*handler)(int, short, void *); 90 int fd; 91 pid_t pid; 92 short events; 93 }; 94 95 enum imsg_type { 96 IMSG_NONE, 97 IMSG_CTL_OK, /* answer to snmpctl requests */ 98 IMSG_CTL_FAIL, 99 IMSG_CTL_END, 100 IMSG_CTL_NOTIFY 101 }; 102 103 struct imsg_hdr { 104 u_int16_t type; 105 u_int16_t len; 106 u_int32_t peerid; 107 pid_t pid; 108 }; 109 110 struct imsg { 111 struct imsg_hdr hdr; 112 void *data; 113 }; 114 115 enum { 116 PROC_PARENT, /* Parent process and application interface */ 117 PROC_SNMPE /* SNMP engine */ 118 } snmpd_process; 119 120 /* initially control.h */ 121 struct { 122 struct event ev; 123 int fd; 124 } control_state; 125 126 enum blockmodes { 127 BM_NORMAL, 128 BM_NONBLOCK 129 }; 130 131 struct ctl_conn { 132 TAILQ_ENTRY(ctl_conn) entry; 133 u_int8_t flags; 134 #define CTL_CONN_NOTIFY 0x01 135 #define CTL_CONN_LOCKED 0x02 /* restricted mode */ 136 struct imsgbuf ibuf; 137 138 }; 139 TAILQ_HEAD(ctl_connlist, ctl_conn); 140 extern struct ctl_connlist ctl_conns; 141 142 /* 143 * kroute 144 */ 145 146 struct kroute { 147 struct in_addr prefix; 148 struct in_addr nexthop; 149 u_int16_t flags; 150 u_int16_t rtlabel; 151 u_short if_index; 152 u_int8_t prefixlen; 153 u_long ticks; 154 }; 155 156 struct kif_addr { 157 u_short if_index; 158 struct in_addr addr; 159 struct in_addr mask; 160 struct in_addr dstbrd; 161 162 TAILQ_ENTRY(kif_addr) entry; 163 RB_ENTRY(kif_addr) node; 164 }; 165 166 struct kif { 167 char if_name[IF_NAMESIZE]; 168 char if_descr[IFDESCRSIZE]; 169 u_int8_t if_lladdr[ETHER_ADDR_LEN]; 170 int if_flags; 171 u_short if_index; 172 u_int8_t if_nhreachable; /* for nexthop verification */ 173 u_long if_ticks; 174 struct if_data if_data; 175 }; 176 177 #define F_OSPFD_INSERTED 0x0001 178 #define F_KERNEL 0x0002 179 #define F_BGPD_INSERTED 0x0004 180 #define F_CONNECTED 0x0008 181 #define F_DOWN 0x0010 182 #define F_STATIC 0x0020 183 #define F_DYNAMIC 0x0040 184 #define F_REDISTRIBUTED 0x0100 185 186 /* 187 * Message Processing Subsystem (mps) 188 */ 189 190 struct oid { 191 struct ber_oid o_id; 192 #define o_oid o_id.bo_id 193 #define o_oidlen o_id.bo_n 194 195 char *o_name; 196 197 u_int o_flags; 198 199 int (*o_get)(struct oid *, struct ber_oid *, 200 struct ber_element **); 201 int (*o_set)(struct oid *, struct ber_oid *, 202 struct ber_element **); 203 struct ber_oid *(*o_table)(struct oid *, struct ber_oid *, 204 struct ber_oid *); 205 206 long long o_val; 207 void *o_data; 208 209 RB_ENTRY(oid) o_element; 210 }; 211 212 #define OID_ROOT 0x00 213 #define OID_RD 0x01 214 #define OID_WR 0x02 215 #define OID_IFSET 0x04 /* only if user-specified value */ 216 #define OID_DYNAMIC 0x08 /* free allocated data */ 217 #define OID_TABLE 0x10 /* dynamic sub-elements */ 218 #define OID_MIB 0x20 /* root-OID of a supported MIB */ 219 #define OID_KEY 0x40 /* lookup tables */ 220 221 #define OID_RS (OID_RD|OID_IFSET) 222 #define OID_WS (OID_WR|OID_IFSET) 223 #define OID_RW (OID_RD|OID_WR) 224 #define OID_RWS (OID_RW|OID_IFSET) 225 226 #define OID_TRD (OID_RD|OID_TABLE) 227 #define OID_TWR (OID_WR|OID_TABLE) 228 #define OID_TRS (OID_RD|OID_IFSET|OID_TABLE) 229 #define OID_TWS (OID_WR|OID_IFSET|OID_TABLE) 230 #define OID_TRW (OID_RD|OID_WR|OID_TABLE) 231 #define OID_TRWS (OID_RW|OID_IFSET|OID_TABLE) 232 233 #define OID_NOTSET(_oid) \ 234 (((_oid)->o_flags & OID_IFSET) && \ 235 ((_oid)->o_data == NULL) && ((_oid)->o_val == 0)) 236 237 #define OID(...) { { __VA_ARGS__ } } 238 #define MIBDECL(...) { { MIB_##__VA_ARGS__ } }, #__VA_ARGS__ 239 #define MIB(...) { { MIB_##__VA_ARGS__ } }, NULL 240 #define MIBEND { { 0 } }, NULL 241 242 /* 243 * daemon structures 244 */ 245 246 struct snmp_message { 247 u_int sm_version; 248 char sm_community[SNMPD_MAXCOMMUNITYLEN]; 249 u_int sm_context; 250 251 struct ber_element *sm_header; 252 struct ber_element *sm_headerend; 253 254 long long sm_request; 255 256 long long sm_error; 257 #define sm_nonrepeaters sm_error 258 long long sm_errorindex; 259 #define sm_maxrepetitions sm_errorindex 260 261 struct ber_element *sm_pdu; 262 struct ber_element *sm_pduend; 263 264 struct ber_element *sm_varbind; 265 struct ber_element *sm_varbindresp; 266 }; 267 268 /* Defined in SNMPv2-MIB.txt (RFC 3418) */ 269 struct snmp_stats { 270 u_int32_t snmp_inpkts; 271 u_int32_t snmp_outpkts; 272 u_int32_t snmp_inbadversions; 273 u_int32_t snmp_inbadcommunitynames; 274 u_int32_t snmp_inbadcommunityuses; 275 u_int32_t snmp_inasnparseerrs; 276 u_int32_t snmp_intoobigs; 277 u_int32_t snmp_innosuchnames; 278 u_int32_t snmp_inbadvalues; 279 u_int32_t snmp_inreadonlys; 280 u_int32_t snmp_ingenerrs; 281 u_int32_t snmp_intotalreqvars; 282 u_int32_t snmp_intotalsetvars; 283 u_int32_t snmp_ingetrequests; 284 u_int32_t snmp_ingetnexts; 285 u_int32_t snmp_insetrequests; 286 u_int32_t snmp_ingetresponses; 287 u_int32_t snmp_intraps; 288 u_int32_t snmp_outtoobigs; 289 u_int32_t snmp_outnosuchnames; 290 u_int32_t snmp_outbadvalues; 291 u_int32_t snmp_outgenerrs; 292 u_int32_t snmp_outgetrequests; 293 u_int32_t snmp_outgetnexts; 294 u_int32_t snmp_outsetrequests; 295 u_int32_t snmp_outgetresponses; 296 u_int32_t snmp_outtraps; 297 int snmp_enableauthentraps; 298 u_int32_t snmp_silentdrops; 299 u_int32_t snmp_proxydrops; 300 }; 301 302 struct address { 303 struct sockaddr_storage ss; 304 in_port_t port; 305 306 TAILQ_ENTRY(address) entry; 307 308 /* For SNMP trap receivers etc. */ 309 char *sa_community; 310 struct ber_oid *sa_oid; 311 }; 312 TAILQ_HEAD(addresslist, address); 313 314 struct snmpd { 315 u_int8_t sc_flags; 316 #define SNMPD_F_VERBOSE 0x01 317 #define SNMPD_F_NONAMES 0x02 318 319 const char *sc_confpath; 320 struct address sc_address; 321 int sc_sock; 322 struct event sc_ev; 323 struct timeval sc_starttime; 324 325 char sc_rdcommunity[SNMPD_MAXCOMMUNITYLEN]; 326 char sc_rwcommunity[SNMPD_MAXCOMMUNITYLEN]; 327 char sc_trcommunity[SNMPD_MAXCOMMUNITYLEN]; 328 329 struct snmp_stats sc_stats; 330 331 struct addresslist sc_trapreceivers; 332 }; 333 334 /* control.c */ 335 int control_init(void); 336 int control_listen(struct snmpd *, struct imsgbuf *); 337 void control_accept(int, short, void *); 338 void control_dispatch_imsg(int, short, void *); 339 void control_imsg_forward(struct imsg *); 340 void control_cleanup(void); 341 342 void session_socket_blockmode(int, enum blockmodes); 343 344 /* parse.y */ 345 struct snmpd *parse_config(const char *, u_int); 346 int cmdline_symset(char *); 347 348 /* log.c */ 349 void log_init(int); 350 void log_warn(const char *, ...); 351 void log_warnx(const char *, ...); 352 void log_info(const char *, ...); 353 void log_debug(const char *, ...); 354 __dead void fatal(const char *); 355 __dead void fatalx(const char *); 356 const char *print_host(struct sockaddr_storage *, char *, size_t); 357 358 /* buffer.c */ 359 struct buf *buf_open(size_t); 360 struct buf *buf_dynamic(size_t, size_t); 361 int buf_add(struct buf *, void *, size_t); 362 void *buf_reserve(struct buf *, size_t); 363 int buf_close(struct msgbuf *, struct buf *); 364 void buf_free(struct buf *); 365 void msgbuf_init(struct msgbuf *); 366 void msgbuf_clear(struct msgbuf *); 367 int msgbuf_write(struct msgbuf *); 368 369 /* imsg.c */ 370 void imsg_init(struct imsgbuf *, int, void (*)(int, short, void *)); 371 ssize_t imsg_read(struct imsgbuf *); 372 ssize_t imsg_get(struct imsgbuf *, struct imsg *); 373 int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, 374 pid_t, int, void *, u_int16_t); 375 int imsg_composev(struct imsgbuf *, enum imsg_type , u_int32_t, 376 pid_t, int, const struct iovec *, int); 377 struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, 378 pid_t, u_int16_t); 379 int imsg_add(struct buf *, void *, u_int16_t); 380 int imsg_close(struct imsgbuf *, struct buf *); 381 void imsg_free(struct imsg *); 382 void imsg_event_add(struct imsgbuf *); /* provided externally */ 383 int imsg_get_fd(struct imsgbuf *); 384 385 /* kroute.c */ 386 int kr_init(void); 387 void kr_shutdown(void); 388 389 int kr_updateif(u_int); 390 u_int kr_ifnumber(void); 391 u_long kr_iflastchange(void); 392 struct kif *kr_getif(u_short); 393 struct kif *kr_getnextif(u_short); 394 struct kif_addr *kr_getaddr(struct in_addr *); 395 struct kif_addr *kr_getnextaddr(struct in_addr *); 396 397 /* snmpe.c */ 398 pid_t snmpe(struct snmpd *, int [2]); 399 void snmpe_debug_elements(struct ber_element *); 400 401 /* trap.c */ 402 void trap_init(void); 403 int trap_imsg(struct imsgbuf *, pid_t); 404 int trap_send(struct ber_oid *, struct ber_element *); 405 406 /* mps.c */ 407 struct ber_element * 408 mps_getreq(struct ber_element *, struct ber_oid *); 409 struct ber_element * 410 mps_getnextreq(struct ber_element *, struct ber_oid *); 411 int mps_setreq(struct ber_element *, struct ber_oid *); 412 int mps_set(struct ber_oid *, void *, long long); 413 int mps_getstr(struct oid *, struct ber_oid *, 414 struct ber_element **); 415 int mps_setstr(struct oid *, struct ber_oid *, 416 struct ber_element **); 417 int mps_getint(struct oid *, struct ber_oid *, 418 struct ber_element **); 419 int mps_setint(struct oid *, struct ber_oid *, 420 struct ber_element **); 421 int mps_getts(struct oid *, struct ber_oid *, 422 struct ber_element **); 423 void mps_encodeinaddr(struct ber_oid *, struct in_addr *, int); 424 void mps_decodeinaddr(struct ber_oid *, struct in_addr *, int); 425 426 /* smi.c */ 427 int smi_init(void); 428 u_long smi_getticks(void); 429 void smi_mibtree(struct oid *); 430 struct oid *smi_find(struct oid *); 431 struct oid *smi_next(struct oid *); 432 struct oid *smi_foreach(struct oid *, u_int); 433 void smi_oidlen(struct ber_oid *); 434 char *smi_oidstring(struct ber_oid *, char *, size_t); 435 void smi_delete(struct oid *); 436 void smi_insert(struct oid *); 437 int smi_oid_cmp(struct oid *, struct oid *); 438 439 /* snmpd.c */ 440 int snmpd_socket_af(struct sockaddr_storage *, in_port_t); 441 442 #endif /* _SNMPD_H */ 443