snmp_msg.h 9.3 KB

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