]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
SNMP: Remove transmit hook, new macros
authorVojtech Vilimek <vojtech.vilimek@nic.cz>
Mon, 4 Sep 2023 11:46:02 +0000 (13:46 +0200)
committerVojtech Vilimek <vojtech.vilimek@nic.cz>
Mon, 4 Sep 2023 11:46:02 +0000 (13:46 +0200)
proto/snmp/bgp_mib.c
proto/snmp/bgp_mib.h
proto/snmp/snmp.c
proto/snmp/snmp.h
proto/snmp/subagent.c
proto/snmp/subagent.h

index 5247acbd6371867a0b4f8c58d901ec3700d932a4..093b329fc2229a27eefd038223f721820b922d64 100644 (file)
@@ -49,6 +49,26 @@ static const char * const debug_bgp_states[] UNUSED = {
   [BGP_INTERNAL_NO_VALUE]               = "BGP_INTERNAL_NO_VALUE",
 };
 
+static inline uint
+snmp_bgp_fsm_state(struct bgp_proto *bgp_proto)
+{
+  const struct bgp_conn *bgp_conn = bgp_proto->conn;
+  const struct bgp_conn *bgp_in = &bgp_proto->incoming_conn;
+  const struct bgp_conn *bgp_out = &bgp_proto->outgoing_conn;
+
+  if (bgp_conn)
+    return bgp_conn->state;
+
+  if (MAX(bgp_in->state, bgp_out->state) == BS_CLOSE &&
+      MIN(bgp_in->state, bgp_out->state) != BS_CLOSE)
+    return MIN(bgp_in->state, bgp_out->state);
+  if (MIN(bgp_in->state, bgp_out->state) == BS_CLOSE)
+    return BS_IDLE;
+
+  return MAX(bgp_in->state, bgp_out->state);
+}
+
+
 void
 snmp_bgp_register(struct snmp_proto *p)
 {
@@ -881,20 +901,9 @@ bgp_fill_dynamic(struct snmp_proto UNUSED *p, struct agentx_varbind *vb,
   struct bgp_stats *bgp_stats = &bgp_proto->stats;
   const struct bgp_config *bgp_conf = bgp_proto->cf;
 
-  uint bgp_state;
-
-  if (bgp_conn)
-    bgp_state = bgp_conn->state;
-  else if (MAX(bgp_in->state, bgp_out->state) == BS_CLOSE &&
-          MIN(bgp_in->state, bgp_out->state) != BS_CLOSE)
-    bgp_state = MIN(bgp_in->state, bgp_out->state);
-  else if (MIN(bgp_in->state, bgp_out->state) == BS_CLOSE)
-    bgp_state = BS_IDLE;
-  else
-    bgp_state = MAX(bgp_in->state, bgp_out->state);
+  uint bgp_state = snmp_bgp_fsm_state(bgp_proto);
 
-  char last_error[2] = { bgp_proto->last_error_code & 0x00FF0000 >> 16,
-                        bgp_proto->last_error_code & 0x000000FF };
+  char last_error[2] = SNMP_BGP_LAST_ERROR(bgp_proto);
   switch (state)
   {
     case BGP_INTERNAL_IDENTIFIER:
index c4416180f3062916fb9d6a82a6dff0ebadbc59c4..1bca891f3a3934593f22ec60975d033c02f415f4 100644 (file)
@@ -90,4 +90,8 @@ enum BGP_INTERNAL_STATES {
   BGP_INTERNAL_NO_VALUE = 255,
 } PACKED;
 
+#define SNMP_BGP_LAST_ERROR(bgp_proto)                                       \
+  { bgp_proto->last_error_code & 0x00FF0000 >> 16,                           \
+    bgp_proto->last_error_code & 0x000000FF };
+
 #endif
index b46cc0ba6b5c5f80cefd36e1efc3ccb9ce7e2a61..ece8f47c1cbd2c237dbbc994530657648488b6f1 100644 (file)
@@ -267,7 +267,8 @@ snmp_connected(sock *sk)
   byte *buf UNUSED = sk->rpos;
 
   sk->rx_hook = snmp_rx;
-  sk->tx_hook = snmp_tx;
+  sk->tx_hook = NULL;
+  //sk->tx_hook = snmp_tx;
 
   snmp_start_subagent(p);
 
index e7b5a441651edcf28607d932918cf0f6ddf48330..c6edc14bde18df0d02aac12d7b0592b4ed0bef70 100644 (file)
@@ -149,6 +149,6 @@ struct snmp_proto {
   struct agentx_response *partial_response;
 };
 
-void snmp_tx(sock *sk);
+//void snmp_tx(sock *sk);
 
 #endif
index 0d0ab8bc319d2e95621db00f0049d808a3f75d97..864496293fa9830ce3e1a309ed9b2fc9124e08f0 100644 (file)
@@ -81,10 +81,7 @@ open_pdu(struct snmp_proto *p, struct oid *oid)
 {
   sock *sk = p->sock;
 
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size = sk->tbuf + sk->tbsize - sk->tpos,
-  };
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
   byte *buf = c.buffer;
 
   // TODO should be configurable; with check on string length
@@ -102,6 +99,7 @@ open_pdu(struct snmp_proto *p, struct oid *oid)
   struct agentx_header *h = (struct agentx_header *) c.buffer;
   ADVANCE(c.buffer, c.size, AGENTX_HEADER_SIZE);
   SNMP_BLANK_HEADER(h, AGENTX_OPEN_PDU);
+  c.byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER;
 
   STORE_U32(h->session_id, 1);
   STORE_U32(h->transaction_id, 1);
@@ -127,22 +125,7 @@ notify_pdu(struct snmp_proto *p, struct oid *oid, void *opaque, uint size, int i
 {
   sock *sk = p->sock;
 
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size = sk->tbuf + sk->tbsize - sk->tpos,
-  };
-
-  if (c.size < AGENTX_HEADER_SIZE)
-  {
-    snmp_manage_tbuf(p, &c);
-  }
-
-  struct agentx_header *h = (struct agentx_header *) c.buffer;
-  ADVANCE(c.buffer, c.size, AGENTX_HEADER_SIZE);
-  SNMP_BLANK_HEADER(h, AGENTX_NOTIFY_PDU);
-  SNMP_SESSION(h, p);
-
-  c.byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER;
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
 
 #define UPTIME_SIZE \
   (6 * sizeof(u32)) /* sizeof( { u32 vb_type, u32 oid_hdr, u32 ids[4] } )*/
@@ -150,14 +133,20 @@ notify_pdu(struct snmp_proto *p, struct oid *oid, void *opaque, uint size, int i
   (7 * sizeof(u32)) /* sizeof( { u32 vb_type, u32 oid_hdr, u32 ids[6] } ) */
 
   uint sz = AGENTX_HEADER_SIZE + TRAP0_HEADER_SIZE + snmp_oid_size(oid) \
-    + snmp_varbind_hdr_size_from_oid(oid) + size;
+    + size;
 
   if (include_uptime)
     sz += UPTIME_SIZE;
-    
+
   if (c.size < sz)
     snmp_manage_tbuf(p, &c);
 
+  struct agentx_header *h = (struct agentx_header *) c.buffer;
+  ADVANCE(c.buffer, c.size, AGENTX_HEADER_SIZE);
+  SNMP_BLANK_HEADER(h, AGENTX_NOTIFY_PDU);
+  SNMP_SESSION(h, p);
+  c.byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER;
+
   if (include_uptime)
   {
     /* sysUpTime.0 oid */
@@ -245,10 +234,7 @@ static void
 un_register_pdu(struct snmp_proto *p, struct oid *oid, uint index, uint len, u8 type, u8 is_instance)
 {
   sock *sk = p->sock;
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size =  sk->tbuf + sk->tbsize - sk->tpos,
-  };
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
   byte *buf = c.buffer;
 
   /* conditional +4 for upper-bound (optinal field) */
@@ -269,6 +255,7 @@ un_register_pdu(struct snmp_proto *p, struct oid *oid, uint index, uint len, u8
   /* use new transactionID, reset packetID */
   p->packet_id++;
   SNMP_SESSION(h, p);
+  c.byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER;
 
   /* do not override timeout */
   STORE_U32(ur->timeout, 15);
@@ -317,10 +304,7 @@ static void
 close_pdu(struct snmp_proto *p, u8 reason)
 {
   sock *sk = p->sock;
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size = sk->tbuf + sk->tbsize - sk->tpos,
-  };
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
   byte *buf = c.buffer;
 
   snmp_log("close_pdu() size: %u %c %u", c.size, (c.size > AGENTX_HEADER_SIZE + 4)
@@ -338,6 +322,7 @@ close_pdu(struct snmp_proto *p, u8 reason)
   SNMP_BLANK_HEADER(h, AGENTX_CLOSE_PDU);
   p->packet_id++;
   SNMP_SESSION(h, p);
+  c.byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER;
 
   snmp_put_fbyte(c.buffer, reason);
   ADVANCE(c.buffer, c.size, 4);
@@ -665,7 +650,7 @@ do_response(struct snmp_proto *p, byte *buf, uint size UNUSED)
       break;
 
     case SNMP_STOP:
-      snmp_shutdown();
+      snmp_down(p);
       break;
 
     default:
@@ -914,13 +899,9 @@ parse_gets2_pdu(struct snmp_proto *p, byte * const pkt_start, uint size, uint *s
   uint pkt_size = LOAD_U32(h->payload, h->flags & AGENTX_NETWORK_BYTE_ORDER);
 
   sock *sk = p->sock;
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size = sk->tbuf + sk->tbsize - sk->tpos,
-    .byte_ord = h->flags & AGENTX_NETWORK_BYTE_ORDER,
-    .error = AGENTX_RES_NO_ERROR,
-    .context = 0,
-  };
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
+  // TODO better handling of endianness
+  c.byte_ord = 0; /* use little-endian */
 
   uint clen;     /* count of characters in context (without last '\0') */
   char *context;  /* newly allocated string of character */
@@ -1305,10 +1286,7 @@ snmp_ping(struct snmp_proto *p)
   sock *sk = p->sock;
   snmp_dump_packet(sk->tpos, AGENTX_HEADER_SIZE + 4);
   snmp_log("snmp_ping sk->tpos 0x%p", sk->tpos);
-  struct snmp_pdu_context c = {
-    .buffer = sk->tpos,
-    .size = sk->tbuf + sk->tbsize - sk->tpos,
-  };
+  struct snmp_pdu_context c = SNMP_PDU_CONTEXT(sk);
 
   if (c.size < AGENTX_HEADER_SIZE)
     snmp_manage_tbuf(p, &c);
@@ -1319,6 +1297,7 @@ snmp_ping(struct snmp_proto *p)
   SNMP_BLANK_HEADER(h, AGENTX_PING_PDU);
   p->packet_id++;
   SNMP_SESSION(h, p);
+  c.byte_ord = AGENTX_NETWORK_BYTE_ORDER;
 
   snmp_log("sending ping packet ... tpos 0x%p", sk->tpos);
   snmp_dump_packet(sk->tpos, AGENTX_HEADER_SIZE + 4);
@@ -1612,21 +1591,13 @@ send_remaining_buffers(sock *sk)
 }
 #endif
 
+/*
 void
 snmp_tx(sock UNUSED *sk)
 {
   snmp_log("snmp_tx");
-  //struct snmp_proto *p = sk->data;
-
-  return;
-
-#if 0
-  if (!p->partial_response)
-    return;
-
-  send_remaining_buffers(sk);
-#endif
 }
+*/
 
 
 static struct agentx_response *
index 3a5c8cc5d9ab78e8e2ce07151e060add51afac59..296589816b320e038ce0f20b5defa46754fad712 100644 (file)
@@ -125,6 +125,13 @@ enum snmp_search_res {
 #define SNMP_VB_DATA(varbind)                                                \
   (((void *)(varbind)) + snmp_varbind_header_size(varbind))
 
+#define SNMP_PDU_CONTEXT(sk) {                                               \
+    .buffer = sk->tpos,                                                              \
+    .size = sk->tbuf + sk->tbsize - sk->tpos,                                \
+    .error = AGENTX_RES_NO_ERROR,                                            \
+    .context = 0,                                                            \
+  }
+
 struct agentx_header {
   u8 version;
   u8 type;