1 /* $OpenBSD: snmp.h,v 1.7 2020/01/17 09:52:44 martijn Exp $ */ 2 3 /* 4 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org> 5 * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@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_SNMP_H 21 #define SNMPD_SNMP_H 22 23 #include <sys/types.h> 24 #include <sys/queue.h> 25 #include <endian.h> 26 27 #include <time.h> 28 29 #define READ_BUF_SIZE 65535 30 31 #define SNMP_MAX_OID_STRLEN 128 /* max size of the OID _string_ */ 32 33 /* 34 * SNMP BER types 35 */ 36 37 enum snmp_version { 38 SNMP_V1 = 0, 39 SNMP_V2C = 1, /* SNMPv2c */ 40 SNMP_V3 = 3 41 }; 42 43 enum snmp_context { 44 SNMP_C_GETREQ = 0, 45 SNMP_C_GETNEXTREQ = 1, 46 SNMP_C_GETRESP = 2, 47 SNMP_C_SETREQ = 3, 48 SNMP_C_TRAP = 4, 49 50 /* SNMPv2 */ 51 SNMP_C_GETBULKREQ = 5, 52 SNMP_C_INFORMREQ = 6, 53 SNMP_C_TRAPV2 = 7, 54 SNMP_C_REPORT = 8 55 }; 56 57 enum snmp_application { 58 SNMP_T_IPADDR = 0, 59 SNMP_T_COUNTER32 = 1, 60 SNMP_T_GAUGE32 = 2, 61 SNMP_T_UNSIGNED32 = 2, 62 SNMP_T_TIMETICKS = 3, 63 SNMP_T_OPAQUE = 4, 64 SNMP_T_NSAPADDR = 5, 65 SNMP_T_COUNTER64 = 6, 66 SNMP_T_UINTEGER32 = 7 67 }; 68 69 enum snmp_generic_trap { 70 SNMP_TRAP_COLDSTART = 0, 71 SNMP_TRAP_WARMSTART = 1, 72 SNMP_TRAP_LINKDOWN = 2, 73 SNMP_TRAP_LINKUP = 3, 74 SNMP_TRAP_AUTHFAILURE = 4, 75 SNMP_TRAP_EGPNEIGHLOSS = 5, 76 SNMP_TRAP_ENTERPRISE = 6 77 }; 78 79 enum snmp_error { 80 SNMP_ERROR_NONE = 0, 81 SNMP_ERROR_TOOBIG = 1, 82 SNMP_ERROR_NOSUCHNAME = 2, 83 SNMP_ERROR_BADVALUE = 3, 84 SNMP_ERROR_READONLY = 4, 85 SNMP_ERROR_GENERR = 5, 86 87 /* SNMPv2 */ 88 SNMP_ERROR_NOACCESS = 6, 89 SNMP_ERROR_WRONGTYPE = 7, 90 SNMP_ERROR_WRONGLENGTH = 8, 91 SNMP_ERROR_WRONGENC = 9, 92 SNMP_ERROR_WRONGVALUE = 10, 93 SNMP_ERROR_NOCREATION = 11, 94 SNMP_ERROR_INCONVALUE = 12, 95 SNMP_ERROR_RESUNAVAIL = 13, /* EGAIN */ 96 SNMP_ERROR_COMMITFAILED = 14, 97 SNMP_ERROR_UNDOFAILED = 15, 98 SNMP_ERROR_AUTHERROR = 16, 99 SNMP_ERROR_NOTWRITABLE = 17, 100 SNMP_ERROR_INCONNAME = 18 101 }; 102 103 enum snmp_security_model { 104 SNMP_SEC_ANY = 0, 105 SNMP_SEC_SNMPv1 = 1, 106 SNMP_SEC_SNMPv2c = 2, 107 SNMP_SEC_USM = 3, 108 SNMP_SEC_TSM = 4 109 }; 110 111 enum snmp_application_exception { 112 SNMP_E_NOSUCHOBJECT = 0, 113 SNMP_E_NOSUCHINSTANCE = 1, 114 SNMP_E_ENDOFMIB = 2 115 }; 116 117 struct snmp_agent; 118 119 struct snmp_sec { 120 enum snmp_security_model model; 121 int (*init)(struct snmp_agent *); 122 char *(*genparams)(struct snmp_agent *, size_t *, void **); 123 struct ber_element *(*encpdu)(struct snmp_agent *, 124 struct ber_element *, void *); 125 int (*finalparams)(struct snmp_agent *, char *, size_t, size_t, void *); 126 int (*parseparams)(struct snmp_agent *, char *, size_t, off_t, char *, 127 size_t, uint8_t, void **); 128 struct ber_element *(*decpdu)(struct snmp_agent *, char *, size_t, 129 void *); 130 void (*free)(void *); 131 void (*freecookie)(void *); 132 void *data; 133 }; 134 135 struct snmp_v3 { 136 uint8_t level; 137 char *ctxname; 138 size_t ctxnamelen; 139 int engineidset; 140 char *engineid; 141 size_t engineidlen; 142 struct snmp_sec *sec; 143 }; 144 145 struct snmp_agent { 146 int fd; 147 int timeout; 148 int retries; 149 enum snmp_version version; 150 /* SNMP_V1 & SNMP_V2C */ 151 char *community; 152 /* SNMP_V3 */ 153 struct snmp_v3 *v3; 154 }; 155 156 #define SNMP_MSGFLAG_AUTH 0x01 157 #define SNMP_MSGFLAG_PRIV 0x02 158 #define SNMP_MSGFLAG_SECMASK (SNMP_MSGFLAG_AUTH | SNMP_MSGFLAG_PRIV) 159 #define SNMP_MSGFLAG_REPORT 0x04 160 161 #define SNMP_MAX_TIMEWINDOW 150 /* RFC3414 */ 162 163 struct snmp_v3 *snmp_v3_init(int, const char *, size_t, struct snmp_sec *); 164 int snmp_v3_setengineid(struct snmp_v3 *, char *, size_t); 165 struct snmp_agent *snmp_connect_v12(int, enum snmp_version, const char *); 166 struct snmp_agent *snmp_connect_v3(int, struct snmp_v3 *); 167 void snmp_free_agent(struct snmp_agent *); 168 struct ber_element * 169 snmp_get(struct snmp_agent *agent, struct ber_oid *oid, size_t len); 170 struct ber_element *snmp_getnext(struct snmp_agent *, struct ber_oid *, size_t); 171 struct ber_element * 172 snmp_getbulk(struct snmp_agent *, struct ber_oid *, size_t, int, int); 173 struct ber_element *snmp_set(struct snmp_agent *, struct ber_element *); 174 int snmp_trap(struct snmp_agent *, struct timespec *, struct ber_oid *, 175 struct ber_element *); 176 177 ssize_t ber_copy_writebuf(struct ber *, void **); 178 179 #endif /* SNMPD_SNMP_H */ 180