1 /** 2 * @file 3 * SNMP Agent message handling structures (internal API, do not use in client code). 4 */ 5 6 /* 7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. 8 * Copyright (c) 2016 Elias Oenal. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without modification, 12 * are permitted provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, 15 * this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright notice, 17 * this list of conditions and the following disclaimer in the documentation 18 * and/or other materials provided with the distribution. 19 * 3. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 * OF SUCH DAMAGE. 32 * 33 * Author: Christiaan Simons <christiaan.simons@axon.tv> 34 * Martin Hentschel <info@cl-soft.de> 35 * Elias Oenal <lwip@eliasoenal.com> 36 */ 37 38 #ifndef LWIP_HDR_APPS_SNMP_MSG_H 39 #define LWIP_HDR_APPS_SNMP_MSG_H 40 41 #include "lwip/apps/snmp_opts.h" 42 43 #if LWIP_SNMP 44 45 #include "lwip/apps/snmp.h" 46 #include "lwip/apps/snmp_core.h" 47 #include "snmp_pbuf_stream.h" 48 #include "lwip/ip_addr.h" 49 #include "lwip/err.h" 50 51 #if LWIP_SNMP_V3 52 #include "snmpv3_priv.h" 53 #endif 54 55 56 #ifdef __cplusplus 57 extern "C" { 58 #endif 59 60 /* The listen port of the SNMP agent. Clients have to make their requests to 61 this port. Most standard clients won't work if you change this! */ 62 #ifndef SNMP_IN_PORT 63 #define SNMP_IN_PORT 161 64 #endif 65 /* The remote port the SNMP agent sends traps to. Most standard trap sinks won't 66 work if you change this! */ 67 #ifndef SNMP_TRAP_PORT 68 #define SNMP_TRAP_PORT 162 69 #endif 70 71 /* version defines used in PDU */ 72 #define SNMP_VERSION_1 0 73 #define SNMP_VERSION_2c 1 74 #define SNMP_VERSION_3 3 75 76 struct snmp_varbind_enumerator 77 { 78 struct snmp_pbuf_stream pbuf_stream; 79 u16_t varbind_count; 80 }; 81 82 typedef enum { 83 SNMP_VB_ENUMERATOR_ERR_OK = 0, 84 SNMP_VB_ENUMERATOR_ERR_EOVB = 1, 85 SNMP_VB_ENUMERATOR_ERR_ASN1ERROR = 2, 86 SNMP_VB_ENUMERATOR_ERR_INVALIDLENGTH = 3 87 } snmp_vb_enumerator_err_t; 88 89 void snmp_vb_enumerator_init(struct snmp_varbind_enumerator* enumerator, struct pbuf* p, u16_t offset, u16_t length); 90 snmp_vb_enumerator_err_t snmp_vb_enumerator_get_next(struct snmp_varbind_enumerator* enumerator, struct snmp_varbind* varbind); 91 92 struct snmp_request 93 { 94 /* Communication handle */ 95 void *handle; 96 /* source IP address */ 97 const ip_addr_t *source_ip; 98 /* source UDP port */ 99 u16_t source_port; 100 /* incoming snmp version */ 101 u8_t version; 102 /* community name (zero terminated) */ 103 u8_t community[SNMP_MAX_COMMUNITY_STR_LEN + 1]; 104 /* community string length (exclusive zero term) */ 105 u16_t community_strlen; 106 /* request type */ 107 u8_t request_type; 108 /* request ID */ 109 s32_t request_id; 110 /* error status */ 111 s32_t error_status; 112 /* error index */ 113 s32_t error_index; 114 /* non-repeaters (getBulkRequest (SNMPv2c)) */ 115 s32_t non_repeaters; 116 /* max-repetitions (getBulkRequest (SNMPv2c)) */ 117 s32_t max_repetitions; 118 119 /* Usually response-pdu (2). When snmpv3 errors are detected report-pdu(8) */ 120 u8_t request_out_type; 121 122 #if LWIP_SNMP_V3 123 s32_t msg_id; 124 s32_t msg_max_size; 125 u8_t msg_flags; 126 s32_t msg_security_model; 127 u8_t msg_authoritative_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH]; 128 u8_t msg_authoritative_engine_id_len; 129 s32_t msg_authoritative_engine_boots; 130 s32_t msg_authoritative_engine_time; 131 u8_t msg_user_name[SNMP_V3_MAX_USER_LENGTH]; 132 u8_t msg_user_name_len; 133 u8_t msg_authentication_parameters[SNMP_V3_MAX_AUTH_PARAM_LENGTH]; 134 u8_t msg_authentication_parameters_len; 135 u8_t msg_privacy_parameters[SNMP_V3_MAX_PRIV_PARAM_LENGTH]; 136 u8_t msg_privacy_parameters_len; 137 u8_t context_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH]; 138 u8_t context_engine_id_len; 139 u8_t context_name[SNMP_V3_MAX_ENGINE_ID_LENGTH]; 140 u8_t context_name_len; 141 #endif 142 143 struct pbuf *inbound_pbuf; 144 struct snmp_varbind_enumerator inbound_varbind_enumerator; 145 u16_t inbound_varbind_offset; 146 u16_t inbound_varbind_len; 147 u16_t inbound_padding_len; 148 149 struct pbuf *outbound_pbuf; 150 struct snmp_pbuf_stream outbound_pbuf_stream; 151 u16_t outbound_pdu_offset; 152 u16_t outbound_error_status_offset; 153 u16_t outbound_error_index_offset; 154 u16_t outbound_varbind_offset; 155 #if LWIP_SNMP_V3 156 u16_t outbound_msg_global_data_offset; 157 u16_t outbound_msg_global_data_end; 158 u16_t outbound_msg_security_parameters_str_offset; 159 u16_t outbound_msg_security_parameters_seq_offset; 160 u16_t outbound_msg_security_parameters_end; 161 u16_t outbound_msg_authentication_parameters_offset; 162 u16_t outbound_scoped_pdu_seq_offset; 163 u16_t outbound_scoped_pdu_string_offset; 164 #endif 165 166 u8_t value_buffer[SNMP_MAX_VALUE_SIZE]; 167 }; 168 169 /** A helper struct keeping length information about varbinds */ 170 struct snmp_varbind_len 171 { 172 u8_t vb_len_len; 173 u16_t vb_value_len; 174 u8_t oid_len_len; 175 u16_t oid_value_len; 176 u8_t value_len_len; 177 u16_t value_value_len; 178 }; 179 180 /** Agent community string */ 181 extern const char *snmp_community; 182 /** Agent community string for write access */ 183 extern const char *snmp_community_write; 184 /** handle for sending traps */ 185 extern void* snmp_traps_handle; 186 187 void snmp_receive(void *handle, struct pbuf *p, const ip_addr_t *source_ip, u16_t port); 188 err_t snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port); 189 u8_t snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result); 190 err_t snmp_varbind_length(struct snmp_varbind *varbind, struct snmp_varbind_len *len); 191 err_t snmp_append_outbound_varbind(struct snmp_pbuf_stream *pbuf_stream, struct snmp_varbind* varbind); 192 193 #ifdef __cplusplus 194 } 195 #endif 196 197 #endif /* LWIP_SNMP */ 198 199 #endif /* LWIP_HDR_APPS_SNMP_MSG_H */ 200