1*45091ad0Smartijn /* $OpenBSD: snmp.h,v 1.7 2020/01/17 09:52:44 martijn Exp $ */ 2442e4f4fSmartijn 3442e4f4fSmartijn /* 4442e4f4fSmartijn * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org> 5442e4f4fSmartijn * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> 6442e4f4fSmartijn * 7442e4f4fSmartijn * Permission to use, copy, modify, and distribute this software for any 8442e4f4fSmartijn * purpose with or without fee is hereby granted, provided that the above 9442e4f4fSmartijn * copyright notice and this permission notice appear in all copies. 10442e4f4fSmartijn * 11442e4f4fSmartijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12442e4f4fSmartijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13442e4f4fSmartijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14442e4f4fSmartijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15442e4f4fSmartijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16442e4f4fSmartijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17442e4f4fSmartijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18442e4f4fSmartijn */ 19442e4f4fSmartijn 20442e4f4fSmartijn #ifndef SNMPD_SNMP_H 21442e4f4fSmartijn #define SNMPD_SNMP_H 22442e4f4fSmartijn 23442e4f4fSmartijn #include <sys/types.h> 24442e4f4fSmartijn #include <sys/queue.h> 25442e4f4fSmartijn #include <endian.h> 26442e4f4fSmartijn 27442e4f4fSmartijn #include <time.h> 28442e4f4fSmartijn 29442e4f4fSmartijn #define READ_BUF_SIZE 65535 30442e4f4fSmartijn 31442e4f4fSmartijn #define SNMP_MAX_OID_STRLEN 128 /* max size of the OID _string_ */ 32442e4f4fSmartijn 33442e4f4fSmartijn /* 34442e4f4fSmartijn * SNMP BER types 35442e4f4fSmartijn */ 36442e4f4fSmartijn 37442e4f4fSmartijn enum snmp_version { 38442e4f4fSmartijn SNMP_V1 = 0, 39442e4f4fSmartijn SNMP_V2C = 1, /* SNMPv2c */ 40442e4f4fSmartijn SNMP_V3 = 3 41442e4f4fSmartijn }; 42442e4f4fSmartijn 43442e4f4fSmartijn enum snmp_context { 44442e4f4fSmartijn SNMP_C_GETREQ = 0, 45442e4f4fSmartijn SNMP_C_GETNEXTREQ = 1, 46442e4f4fSmartijn SNMP_C_GETRESP = 2, 47442e4f4fSmartijn SNMP_C_SETREQ = 3, 48442e4f4fSmartijn SNMP_C_TRAP = 4, 49442e4f4fSmartijn 50442e4f4fSmartijn /* SNMPv2 */ 51442e4f4fSmartijn SNMP_C_GETBULKREQ = 5, 52442e4f4fSmartijn SNMP_C_INFORMREQ = 6, 53442e4f4fSmartijn SNMP_C_TRAPV2 = 7, 54442e4f4fSmartijn SNMP_C_REPORT = 8 55442e4f4fSmartijn }; 56442e4f4fSmartijn 57442e4f4fSmartijn enum snmp_application { 58442e4f4fSmartijn SNMP_T_IPADDR = 0, 59442e4f4fSmartijn SNMP_T_COUNTER32 = 1, 60442e4f4fSmartijn SNMP_T_GAUGE32 = 2, 61442e4f4fSmartijn SNMP_T_UNSIGNED32 = 2, 62442e4f4fSmartijn SNMP_T_TIMETICKS = 3, 63442e4f4fSmartijn SNMP_T_OPAQUE = 4, 64442e4f4fSmartijn SNMP_T_NSAPADDR = 5, 65442e4f4fSmartijn SNMP_T_COUNTER64 = 6, 66442e4f4fSmartijn SNMP_T_UINTEGER32 = 7 67442e4f4fSmartijn }; 68442e4f4fSmartijn 69442e4f4fSmartijn enum snmp_generic_trap { 70442e4f4fSmartijn SNMP_TRAP_COLDSTART = 0, 71442e4f4fSmartijn SNMP_TRAP_WARMSTART = 1, 72442e4f4fSmartijn SNMP_TRAP_LINKDOWN = 2, 73442e4f4fSmartijn SNMP_TRAP_LINKUP = 3, 74442e4f4fSmartijn SNMP_TRAP_AUTHFAILURE = 4, 75442e4f4fSmartijn SNMP_TRAP_EGPNEIGHLOSS = 5, 76442e4f4fSmartijn SNMP_TRAP_ENTERPRISE = 6 77442e4f4fSmartijn }; 78442e4f4fSmartijn 79442e4f4fSmartijn enum snmp_error { 80442e4f4fSmartijn SNMP_ERROR_NONE = 0, 81442e4f4fSmartijn SNMP_ERROR_TOOBIG = 1, 82442e4f4fSmartijn SNMP_ERROR_NOSUCHNAME = 2, 83442e4f4fSmartijn SNMP_ERROR_BADVALUE = 3, 84442e4f4fSmartijn SNMP_ERROR_READONLY = 4, 85442e4f4fSmartijn SNMP_ERROR_GENERR = 5, 86442e4f4fSmartijn 87442e4f4fSmartijn /* SNMPv2 */ 88442e4f4fSmartijn SNMP_ERROR_NOACCESS = 6, 89442e4f4fSmartijn SNMP_ERROR_WRONGTYPE = 7, 90442e4f4fSmartijn SNMP_ERROR_WRONGLENGTH = 8, 91442e4f4fSmartijn SNMP_ERROR_WRONGENC = 9, 92442e4f4fSmartijn SNMP_ERROR_WRONGVALUE = 10, 93442e4f4fSmartijn SNMP_ERROR_NOCREATION = 11, 94442e4f4fSmartijn SNMP_ERROR_INCONVALUE = 12, 95442e4f4fSmartijn SNMP_ERROR_RESUNAVAIL = 13, /* EGAIN */ 96442e4f4fSmartijn SNMP_ERROR_COMMITFAILED = 14, 97442e4f4fSmartijn SNMP_ERROR_UNDOFAILED = 15, 98442e4f4fSmartijn SNMP_ERROR_AUTHERROR = 16, 99442e4f4fSmartijn SNMP_ERROR_NOTWRITABLE = 17, 100442e4f4fSmartijn SNMP_ERROR_INCONNAME = 18 101442e4f4fSmartijn }; 102442e4f4fSmartijn 103442e4f4fSmartijn enum snmp_security_model { 104442e4f4fSmartijn SNMP_SEC_ANY = 0, 105442e4f4fSmartijn SNMP_SEC_SNMPv1 = 1, 106442e4f4fSmartijn SNMP_SEC_SNMPv2c = 2, 107442e4f4fSmartijn SNMP_SEC_USM = 3, 108442e4f4fSmartijn SNMP_SEC_TSM = 4 109442e4f4fSmartijn }; 110442e4f4fSmartijn 111*45091ad0Smartijn enum snmp_application_exception { 112*45091ad0Smartijn SNMP_E_NOSUCHOBJECT = 0, 113*45091ad0Smartijn SNMP_E_NOSUCHINSTANCE = 1, 114*45091ad0Smartijn SNMP_E_ENDOFMIB = 2 115*45091ad0Smartijn }; 116*45091ad0Smartijn 117b89ba26fSmartijn struct snmp_agent; 118b89ba26fSmartijn 119b89ba26fSmartijn struct snmp_sec { 120b89ba26fSmartijn enum snmp_security_model model; 121b89ba26fSmartijn int (*init)(struct snmp_agent *); 1224f098f75Smartijn char *(*genparams)(struct snmp_agent *, size_t *, void **); 123f5e30c24Smartijn struct ber_element *(*encpdu)(struct snmp_agent *, 124f5e30c24Smartijn struct ber_element *, void *); 1254f098f75Smartijn int (*finalparams)(struct snmp_agent *, char *, size_t, size_t, void *); 126b89ba26fSmartijn int (*parseparams)(struct snmp_agent *, char *, size_t, off_t, char *, 127f5e30c24Smartijn size_t, uint8_t, void **); 128f5e30c24Smartijn struct ber_element *(*decpdu)(struct snmp_agent *, char *, size_t, 129f5e30c24Smartijn void *); 130b89ba26fSmartijn void (*free)(void *); 1314f098f75Smartijn void (*freecookie)(void *); 132b89ba26fSmartijn void *data; 133b89ba26fSmartijn }; 134b89ba26fSmartijn 135b89ba26fSmartijn struct snmp_v3 { 136b89ba26fSmartijn uint8_t level; 137b89ba26fSmartijn char *ctxname; 138b89ba26fSmartijn size_t ctxnamelen; 139b89ba26fSmartijn int engineidset; 140b89ba26fSmartijn char *engineid; 141b89ba26fSmartijn size_t engineidlen; 142b89ba26fSmartijn struct snmp_sec *sec; 143b89ba26fSmartijn }; 144b89ba26fSmartijn 145442e4f4fSmartijn struct snmp_agent { 146442e4f4fSmartijn int fd; 147442e4f4fSmartijn int timeout; 148442e4f4fSmartijn int retries; 149b89ba26fSmartijn enum snmp_version version; 150b89ba26fSmartijn /* SNMP_V1 & SNMP_V2C */ 151b89ba26fSmartijn char *community; 152b89ba26fSmartijn /* SNMP_V3 */ 153b89ba26fSmartijn struct snmp_v3 *v3; 154442e4f4fSmartijn }; 155442e4f4fSmartijn 156442e4f4fSmartijn #define SNMP_MSGFLAG_AUTH 0x01 157442e4f4fSmartijn #define SNMP_MSGFLAG_PRIV 0x02 158442e4f4fSmartijn #define SNMP_MSGFLAG_SECMASK (SNMP_MSGFLAG_AUTH | SNMP_MSGFLAG_PRIV) 159442e4f4fSmartijn #define SNMP_MSGFLAG_REPORT 0x04 160442e4f4fSmartijn 161442e4f4fSmartijn #define SNMP_MAX_TIMEWINDOW 150 /* RFC3414 */ 162442e4f4fSmartijn 163b89ba26fSmartijn struct snmp_v3 *snmp_v3_init(int, const char *, size_t, struct snmp_sec *); 164b89ba26fSmartijn int snmp_v3_setengineid(struct snmp_v3 *, char *, size_t); 165442e4f4fSmartijn struct snmp_agent *snmp_connect_v12(int, enum snmp_version, const char *); 166b89ba26fSmartijn struct snmp_agent *snmp_connect_v3(int, struct snmp_v3 *); 167442e4f4fSmartijn void snmp_free_agent(struct snmp_agent *); 168442e4f4fSmartijn struct ber_element * 169442e4f4fSmartijn snmp_get(struct snmp_agent *agent, struct ber_oid *oid, size_t len); 170442e4f4fSmartijn struct ber_element *snmp_getnext(struct snmp_agent *, struct ber_oid *, size_t); 171442e4f4fSmartijn struct ber_element * 172442e4f4fSmartijn snmp_getbulk(struct snmp_agent *, struct ber_oid *, size_t, int, int); 173ecb1f8acSmartijn struct ber_element *snmp_set(struct snmp_agent *, struct ber_element *); 174442e4f4fSmartijn int snmp_trap(struct snmp_agent *, struct timespec *, struct ber_oid *, 175442e4f4fSmartijn struct ber_element *); 176442e4f4fSmartijn 177e13f0058Smartijn ssize_t ber_copy_writebuf(struct ber *, void **); 178e13f0058Smartijn 179442e4f4fSmartijn #endif /* SNMPD_SNMP_H */ 180