1 /** 2 * @file 3 * SNMP Agent message handling structures. 4 */ 5 6 /* 7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * Author: Christiaan Simons <christiaan.simons@axon.tv> 33 */ 34 35 #ifndef __LWIP_SNMP_MSG_H__ 36 #define __LWIP_SNMP_MSG_H__ 37 38 #include "lwip/opt.h" 39 #include "lwip/snmp.h" 40 #include "lwip/snmp_structs.h" 41 42 #if LWIP_SNMP 43 44 #if SNMP_PRIVATE_MIB 45 #include "private_mib.h" 46 #endif 47 48 #ifdef __cplusplus 49 extern "C" { 50 #endif 51 52 /* The listen port of the SNMP agent. Clients have to make their requests to 53 this port. Most standard clients won't work if you change this! */ 54 #ifndef SNMP_IN_PORT 55 #define SNMP_IN_PORT 161 56 #endif 57 /* The remote port the SNMP agent sends traps to. Most standard trap sinks won't 58 work if you change this! */ 59 #ifndef SNMP_TRAP_PORT 60 #define SNMP_TRAP_PORT 162 61 #endif 62 63 #define SNMP_ES_NOERROR 0 64 #define SNMP_ES_TOOBIG 1 65 #define SNMP_ES_NOSUCHNAME 2 66 #define SNMP_ES_BADVALUE 3 67 #define SNMP_ES_READONLY 4 68 #define SNMP_ES_GENERROR 5 69 70 #define SNMP_GENTRAP_COLDSTART 0 71 #define SNMP_GENTRAP_WARMSTART 1 72 #define SNMP_GENTRAP_AUTHFAIL 4 73 #define SNMP_GENTRAP_ENTERPRISESPC 6 74 75 struct snmp_varbind 76 { 77 /* next pointer, NULL for last in list */ 78 struct snmp_varbind *next; 79 /* previous pointer, NULL for first in list */ 80 struct snmp_varbind *prev; 81 82 /* object identifier length (in s32_t) */ 83 u8_t ident_len; 84 /* object identifier array */ 85 s32_t *ident; 86 87 /* object value ASN1 type */ 88 u8_t value_type; 89 /* object value length (in u8_t) */ 90 u8_t value_len; 91 /* object value */ 92 void *value; 93 94 /* encoding varbind seq length length */ 95 u8_t seqlenlen; 96 /* encoding object identifier length length */ 97 u8_t olenlen; 98 /* encoding object value length length */ 99 u8_t vlenlen; 100 /* encoding varbind seq length */ 101 u16_t seqlen; 102 /* encoding object identifier length */ 103 u16_t olen; 104 /* encoding object value length */ 105 u16_t vlen; 106 }; 107 108 struct snmp_varbind_root 109 { 110 struct snmp_varbind *head; 111 struct snmp_varbind *tail; 112 /* number of variable bindings in list */ 113 u8_t count; 114 /* encoding varbind-list seq length length */ 115 u8_t seqlenlen; 116 /* encoding varbind-list seq length */ 117 u16_t seqlen; 118 }; 119 120 /** output response message header length fields */ 121 struct snmp_resp_header_lengths 122 { 123 /* encoding error-index length length */ 124 u8_t erridxlenlen; 125 /* encoding error-status length length */ 126 u8_t errstatlenlen; 127 /* encoding request id length length */ 128 u8_t ridlenlen; 129 /* encoding pdu length length */ 130 u8_t pdulenlen; 131 /* encoding community length length */ 132 u8_t comlenlen; 133 /* encoding version length length */ 134 u8_t verlenlen; 135 /* encoding sequence length length */ 136 u8_t seqlenlen; 137 138 /* encoding error-index length */ 139 u16_t erridxlen; 140 /* encoding error-status length */ 141 u16_t errstatlen; 142 /* encoding request id length */ 143 u16_t ridlen; 144 /* encoding pdu length */ 145 u16_t pdulen; 146 /* encoding community length */ 147 u16_t comlen; 148 /* encoding version length */ 149 u16_t verlen; 150 /* encoding sequence length */ 151 u16_t seqlen; 152 }; 153 154 /** output response message header length fields */ 155 struct snmp_trap_header_lengths 156 { 157 /* encoding timestamp length length */ 158 u8_t tslenlen; 159 /* encoding specific-trap length length */ 160 u8_t strplenlen; 161 /* encoding generic-trap length length */ 162 u8_t gtrplenlen; 163 /* encoding agent-addr length length */ 164 u8_t aaddrlenlen; 165 /* encoding enterprise-id length length */ 166 u8_t eidlenlen; 167 /* encoding pdu length length */ 168 u8_t pdulenlen; 169 /* encoding community length length */ 170 u8_t comlenlen; 171 /* encoding version length length */ 172 u8_t verlenlen; 173 /* encoding sequence length length */ 174 u8_t seqlenlen; 175 176 /* encoding timestamp length */ 177 u16_t tslen; 178 /* encoding specific-trap length */ 179 u16_t strplen; 180 /* encoding generic-trap length */ 181 u16_t gtrplen; 182 /* encoding agent-addr length */ 183 u16_t aaddrlen; 184 /* encoding enterprise-id length */ 185 u16_t eidlen; 186 /* encoding pdu length */ 187 u16_t pdulen; 188 /* encoding community length */ 189 u16_t comlen; 190 /* encoding version length */ 191 u16_t verlen; 192 /* encoding sequence length */ 193 u16_t seqlen; 194 }; 195 196 /* Accepting new SNMP messages. */ 197 #define SNMP_MSG_EMPTY 0 198 /* Search for matching object for variable binding. */ 199 #define SNMP_MSG_SEARCH_OBJ 1 200 /* Perform SNMP operation on in-memory object. 201 Pass-through states, for symmetry only. */ 202 #define SNMP_MSG_INTERNAL_GET_OBJDEF 2 203 #define SNMP_MSG_INTERNAL_GET_VALUE 3 204 #define SNMP_MSG_INTERNAL_SET_TEST 4 205 #define SNMP_MSG_INTERNAL_GET_OBJDEF_S 5 206 #define SNMP_MSG_INTERNAL_SET_VALUE 6 207 /* Perform SNMP operation on object located externally. 208 In theory this could be used for building a proxy agent. 209 Practical use is for an enterprise spc. app. gateway. */ 210 #define SNMP_MSG_EXTERNAL_GET_OBJDEF 7 211 #define SNMP_MSG_EXTERNAL_GET_VALUE 8 212 #define SNMP_MSG_EXTERNAL_SET_TEST 9 213 #define SNMP_MSG_EXTERNAL_GET_OBJDEF_S 10 214 #define SNMP_MSG_EXTERNAL_SET_VALUE 11 215 216 #define SNMP_COMMUNITY_STR_LEN 64 217 struct snmp_msg_pstat 218 { 219 /* lwIP local port (161) binding */ 220 struct udp_pcb *pcb; 221 /* source IP address */ 222 struct ip_addr sip; 223 /* source UDP port */ 224 u16_t sp; 225 /* request type */ 226 u8_t rt; 227 /* request ID */ 228 s32_t rid; 229 /* error status */ 230 s32_t error_status; 231 /* error index */ 232 s32_t error_index; 233 /* community name (zero terminated) */ 234 u8_t community[SNMP_COMMUNITY_STR_LEN + 1]; 235 /* community string length (exclusive zero term) */ 236 u8_t com_strlen; 237 /* one out of MSG_EMPTY, MSG_DEMUX, MSG_INTERNAL, MSG_EXTERNAL_x */ 238 u8_t state; 239 /* saved arguments for MSG_EXTERNAL_x */ 240 struct mib_external_node *ext_mib_node; 241 struct snmp_name_ptr ext_name_ptr; 242 struct obj_def ext_object_def; 243 struct snmp_obj_id ext_oid; 244 /* index into input variable binding list */ 245 u8_t vb_idx; 246 /* ptr into input variable binding list */ 247 struct snmp_varbind *vb_ptr; 248 /* list of variable bindings from input */ 249 struct snmp_varbind_root invb; 250 /* list of variable bindings to output */ 251 struct snmp_varbind_root outvb; 252 /* output response lengths used in ASN encoding */ 253 struct snmp_resp_header_lengths rhl; 254 }; 255 256 struct snmp_msg_trap 257 { 258 /* lwIP local port (161) binding */ 259 struct udp_pcb *pcb; 260 /* destination IP address in network order */ 261 struct ip_addr dip; 262 263 /* source enterprise ID (sysObjectID) */ 264 struct snmp_obj_id *enterprise; 265 /* source IP address, raw network order format */ 266 u8_t sip_raw[4]; 267 /* generic trap code */ 268 u32_t gen_trap; 269 /* specific trap code */ 270 u32_t spc_trap; 271 /* timestamp */ 272 u32_t ts; 273 /* list of variable bindings to output */ 274 struct snmp_varbind_root outvb; 275 /* output trap lengths used in ASN encoding */ 276 struct snmp_trap_header_lengths thl; 277 }; 278 279 /** Agent Version constant, 0 = v1 oddity */ 280 extern const s32_t snmp_version; 281 /** Agent default "public" community string */ 282 extern const char snmp_publiccommunity[7]; 283 284 extern struct snmp_msg_trap trap_msg; 285 286 /** Agent setup, start listening to port 161. */ 287 void snmp_init(void); 288 void snmp_trap_dst_enable(u8_t dst_idx, u8_t enable); 289 void snmp_trap_dst_ip_set(u8_t dst_idx, struct ip_addr *dst); 290 291 /** Varbind-list functions. */ 292 struct snmp_varbind* snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len); 293 void snmp_varbind_free(struct snmp_varbind *vb); 294 void snmp_varbind_list_free(struct snmp_varbind_root *root); 295 void snmp_varbind_tail_add(struct snmp_varbind_root *root, struct snmp_varbind *vb); 296 struct snmp_varbind* snmp_varbind_tail_remove(struct snmp_varbind_root *root); 297 298 /** Handle an internal (recv) or external (private response) event. */ 299 void snmp_msg_event(u8_t request_id); 300 err_t snmp_send_response(struct snmp_msg_pstat *m_stat); 301 err_t snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap); 302 void snmp_coldstart_trap(void); 303 void snmp_authfail_trap(void); 304 305 #ifdef __cplusplus 306 } 307 #endif 308 309 #endif /* LWIP_SNMP */ 310 311 #endif /* __LWIP_SNMP_MSG_H__ */ 312