]>
git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_snmp.c
2 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <net-snmp/net-snmp-config.h>
24 #include <net-snmp/net-snmp-includes.h>
34 #include "lib/version.h"
36 #include "ripd/ripd.h"
39 #define RIPV2MIB 1,3,6,1,2,1,23
41 /* RIPv2-MIB rip2Globals values. */
42 #define RIP2GLOBALROUTECHANGES 1
43 #define RIP2GLOBALQUERIES 2
45 /* RIPv2-MIB rip2IfStatEntry. */
46 #define RIP2IFSTATENTRY 1
48 /* RIPv2-MIB rip2IfStatTable. */
49 #define RIP2IFSTATADDRESS 1
50 #define RIP2IFSTATRCVBADPACKETS 2
51 #define RIP2IFSTATRCVBADROUTES 3
52 #define RIP2IFSTATSENTUPDATES 4
53 #define RIP2IFSTATSTATUS 5
55 /* RIPv2-MIB rip2IfConfTable. */
56 #define RIP2IFCONFADDRESS 1
57 #define RIP2IFCONFDOMAIN 2
58 #define RIP2IFCONFAUTHTYPE 3
59 #define RIP2IFCONFAUTHKEY 4
60 #define RIP2IFCONFSEND 5
61 #define RIP2IFCONFRECEIVE 6
62 #define RIP2IFCONFDEFAULTMETRIC 7
63 #define RIP2IFCONFSTATUS 8
64 #define RIP2IFCONFSRCADDRESS 9
66 /* RIPv2-MIB rip2PeerTable. */
67 #define RIP2PEERADDRESS 1
68 #define RIP2PEERDOMAIN 2
69 #define RIP2PEERLASTUPDATE 3
70 #define RIP2PEERVERSION 4
71 #define RIP2PEERRCVBADPACKETS 5
72 #define RIP2PEERRCVBADROUTES 6
74 /* SNMP value hack. */
75 #define COUNTER ASN_COUNTER
76 #define INTEGER ASN_INTEGER
77 #define TIMETICKS ASN_TIMETICKS
78 #define IPADDRESS ASN_IPADDRESS
79 #define STRING ASN_OCTET_STR
81 /* Define SNMP local variables. */
84 /* RIP-MIB instances. */
85 static oid rip_oid
[] = {RIPV2MIB
};
87 /* Interface cache table sorted by interface's address. */
88 static struct route_table
*rip_ifaddr_table
;
91 static uint8_t *rip2Globals(struct variable
*, oid
[], size_t *, int, size_t *,
93 static uint8_t *rip2IfStatEntry(struct variable
*, oid
[], size_t *, int,
94 size_t *, WriteMethod
**);
95 static uint8_t *rip2IfConfAddress(struct variable
*, oid
[], size_t *, int,
96 size_t *, WriteMethod
**);
97 static uint8_t *rip2PeerTable(struct variable
*, oid
[], size_t *, int, size_t *,
100 static struct variable rip_variables
[] = {
101 /* RIP Global Counters. */
102 {RIP2GLOBALROUTECHANGES
, COUNTER
, RONLY
, rip2Globals
, 2, {1, 1}},
103 {RIP2GLOBALQUERIES
, COUNTER
, RONLY
, rip2Globals
, 2, {1, 2}},
104 /* RIP Interface Tables. */
105 {RIP2IFSTATADDRESS
, IPADDRESS
, RONLY
, rip2IfStatEntry
, 3, {2, 1, 1}},
106 {RIP2IFSTATRCVBADPACKETS
,
112 {RIP2IFSTATRCVBADROUTES
, COUNTER
, RONLY
, rip2IfStatEntry
, 3, {2, 1, 3}},
113 {RIP2IFSTATSENTUPDATES
, COUNTER
, RONLY
, rip2IfStatEntry
, 3, {2, 1, 4}},
114 {RIP2IFSTATSTATUS
, COUNTER
, RWRITE
, rip2IfStatEntry
, 3, {2, 1, 5}},
119 /* RIP Interface Configuration Table. */
122 {RIP2IFCONFDOMAIN
, STRING
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 2}},
123 {RIP2IFCONFAUTHTYPE
, COUNTER
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 3}},
124 {RIP2IFCONFAUTHKEY
, STRING
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 4}},
125 {RIP2IFCONFSEND
, COUNTER
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 5}},
126 {RIP2IFCONFRECEIVE
, COUNTER
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 6}},
127 {RIP2IFCONFDEFAULTMETRIC
,
133 {RIP2IFCONFSTATUS
, COUNTER
, RONLY
, rip2IfConfAddress
, 3, {3, 1, 8}},
134 {RIP2IFCONFSRCADDRESS
,
144 /* RIP Peer Table. */
147 {RIP2PEERDOMAIN
, STRING
, RONLY
, rip2PeerTable
, 3, {4, 1, 2}},
148 {RIP2PEERLASTUPDATE
, TIMETICKS
, RONLY
, rip2PeerTable
, 3, {4, 1, 3}},
149 {RIP2PEERVERSION
, INTEGER
, RONLY
, rip2PeerTable
, 3, {4, 1, 4}},
150 {RIP2PEERRCVBADPACKETS
, COUNTER
, RONLY
, rip2PeerTable
, 3, {4, 1, 5}},
151 {RIP2PEERRCVBADROUTES
, COUNTER
, RONLY
, rip2PeerTable
, 3, {4, 1, 6}}};
153 extern struct thread_master
*master
;
155 static uint8_t *rip2Globals(struct variable
*v
, oid name
[], size_t *length
,
156 int exact
, size_t *var_len
,
157 WriteMethod
**write_method
)
161 if (smux_header_generic(v
, name
, length
, exact
, var_len
, write_method
)
165 rip
= rip_lookup_by_vrf_id(VRF_DEFAULT
);
169 /* Retrun global counter. */
171 case RIP2GLOBALROUTECHANGES
:
172 return SNMP_INTEGER(rip
->counters
.route_changes
);
173 case RIP2GLOBALQUERIES
:
174 return SNMP_INTEGER(rip
->counters
.queries
);
181 static int rip_snmp_ifaddr_add(struct connected
*ifc
)
183 struct interface
*ifp
= ifc
->ifp
;
185 struct route_node
*rn
;
189 if (p
->family
!= AF_INET
)
192 rn
= route_node_get(rip_ifaddr_table
, p
);
197 static int rip_snmp_ifaddr_del(struct connected
*ifc
)
199 struct interface
*ifp
= ifc
->ifp
;
201 struct route_node
*rn
;
206 if (p
->family
!= AF_INET
)
209 rn
= route_node_lookup(rip_ifaddr_table
, p
);
213 if (!strncmp(i
->name
, ifp
->name
, INTERFACE_NAMSIZ
)) {
215 route_unlock_node(rn
);
216 route_unlock_node(rn
);
221 static struct interface
*rip_ifaddr_lookup_next(struct in_addr
*addr
)
223 struct prefix_ipv4 p
;
224 struct route_node
*rn
;
225 struct interface
*ifp
;
228 p
.prefixlen
= IPV4_MAX_BITLEN
;
231 rn
= route_node_get(rip_ifaddr_table
, (struct prefix
*)&p
);
233 for (rn
= route_next(rn
); rn
; rn
= route_next(rn
))
237 if (rn
&& rn
->info
) {
239 *addr
= rn
->p
.u
.prefix4
;
240 route_unlock_node(rn
);
246 static struct interface
*rip2IfLookup(struct variable
*v
, oid name
[],
247 size_t *length
, struct in_addr
*addr
,
251 struct interface
*ifp
;
254 /* Check the length. */
255 if (*length
- v
->namelen
!= sizeof(struct in_addr
))
258 oid2in_addr(name
+ v
->namelen
, sizeof(struct in_addr
), addr
);
260 return if_lookup_exact_address((void *)addr
, AF_INET
,
263 len
= *length
- v
->namelen
;
267 oid2in_addr(name
+ v
->namelen
, len
, addr
);
269 ifp
= rip_ifaddr_lookup_next(addr
);
274 oid_copy_in_addr(name
+ v
->namelen
, addr
);
276 *length
= v
->namelen
+ sizeof(struct in_addr
);
283 static struct rip_peer
*rip2PeerLookup(struct variable
*v
, oid name
[],
284 size_t *length
, struct in_addr
*addr
,
289 struct rip_peer
*peer
;
291 rip
= rip_lookup_by_vrf_id(VRF_DEFAULT
);
296 /* Check the length. */
297 if (*length
- v
->namelen
!= sizeof(struct in_addr
) + 1)
300 oid2in_addr(name
+ v
->namelen
, sizeof(struct in_addr
), addr
);
302 peer
= rip_peer_lookup(rip
, addr
);
305 == (int)name
[v
->namelen
+ sizeof(struct in_addr
)])
310 len
= *length
- v
->namelen
;
314 oid2in_addr(name
+ v
->namelen
, len
, addr
);
316 len
= *length
- v
->namelen
;
317 peer
= rip_peer_lookup(rip
, addr
);
319 if ((len
< (int)sizeof(struct in_addr
) + 1)
321 > (int)name
[v
->namelen
322 + sizeof(struct in_addr
)])) {
323 oid_copy_in_addr(name
+ v
->namelen
,
325 name
[v
->namelen
+ sizeof(struct in_addr
)] =
328 sizeof(struct in_addr
) + v
->namelen
+ 1;
332 peer
= rip_peer_lookup_next(rip
, addr
);
337 oid_copy_in_addr(name
+ v
->namelen
, &peer
->addr
);
338 name
[v
->namelen
+ sizeof(struct in_addr
)] = peer
->domain
;
339 *length
= sizeof(struct in_addr
) + v
->namelen
+ 1;
346 static uint8_t *rip2IfStatEntry(struct variable
*v
, oid name
[], size_t *length
,
347 int exact
, size_t *var_len
,
348 WriteMethod
**write_method
)
350 struct interface
*ifp
;
351 struct rip_interface
*ri
;
352 static struct in_addr addr
;
353 static long valid
= SNMP_VALID
;
355 if (smux_header_table(v
, name
, length
, exact
, var_len
, write_method
)
359 memset(&addr
, 0, sizeof(struct in_addr
));
361 /* Lookup interface. */
362 ifp
= rip2IfLookup(v
, name
, length
, &addr
, exact
);
366 /* Fetch rip_interface information. */
370 case RIP2IFSTATADDRESS
:
371 return SNMP_IPADDRESS(addr
);
372 case RIP2IFSTATRCVBADPACKETS
:
373 *var_len
= sizeof(long);
374 return (uint8_t *)&ri
->recv_badpackets
;
376 case RIP2IFSTATRCVBADROUTES
:
377 *var_len
= sizeof(long);
378 return (uint8_t *)&ri
->recv_badroutes
;
380 case RIP2IFSTATSENTUPDATES
:
381 *var_len
= sizeof(long);
382 return (uint8_t *)&ri
->sent_updates
;
384 case RIP2IFSTATSTATUS
:
385 *var_len
= sizeof(long);
386 v
->type
= ASN_INTEGER
;
387 return (uint8_t *)&valid
;
395 static long rip2IfConfSend(struct rip_interface
*ri
)
398 #define ripVersion1 2
399 #define rip1Compatible 3
400 #define ripVersion2 4
401 #define ripV1Demand 5
402 #define ripV2Demand 6
407 if (ri
->ri_send
& RIPv2
)
409 else if (ri
->ri_send
& RIPv1
)
412 if (ri
->rip
->version_send
== RIPv2
)
414 else if (ri
->rip
->version_send
== RIPv1
)
420 static long rip2IfConfReceive(struct rip_interface
*ri
)
425 #define doNotReceive 4
432 recvv
= (ri
->ri_receive
== RI_RIP_UNSPEC
) ? ri
->rip
->version_recv
434 if (recvv
== RI_RIP_VERSION_1_AND_2
)
436 else if (recvv
& RIPv2
)
438 else if (recvv
& RIPv1
)
444 static uint8_t *rip2IfConfAddress(struct variable
*v
, oid name
[],
445 size_t *length
, int exact
, size_t *val_len
,
446 WriteMethod
**write_method
)
448 static struct in_addr addr
;
449 static long valid
= SNMP_INVALID
;
450 static long domain
= 0;
451 static long config
= 0;
452 static unsigned int auth
= 0;
453 struct interface
*ifp
;
454 struct rip_interface
*ri
;
456 if (smux_header_table(v
, name
, length
, exact
, val_len
, write_method
)
460 memset(&addr
, 0, sizeof(struct in_addr
));
462 /* Lookup interface. */
463 ifp
= rip2IfLookup(v
, name
, length
, &addr
, exact
);
467 /* Fetch rip_interface information. */
471 case RIP2IFCONFADDRESS
:
472 *val_len
= sizeof(struct in_addr
);
473 return (uint8_t *)&addr
;
475 case RIP2IFCONFDOMAIN
:
477 return (uint8_t *)&domain
;
479 case RIP2IFCONFAUTHTYPE
:
480 auth
= ri
->auth_type
;
481 *val_len
= sizeof(long);
482 v
->type
= ASN_INTEGER
;
483 return (uint8_t *)&auth
;
485 case RIP2IFCONFAUTHKEY
:
487 return (uint8_t *)&domain
;
489 config
= rip2IfConfSend(ri
);
490 *val_len
= sizeof(long);
491 v
->type
= ASN_INTEGER
;
492 return (uint8_t *)&config
;
493 case RIP2IFCONFRECEIVE
:
494 config
= rip2IfConfReceive(ri
);
495 *val_len
= sizeof(long);
496 v
->type
= ASN_INTEGER
;
497 return (uint8_t *)&config
;
499 case RIP2IFCONFDEFAULTMETRIC
:
500 *val_len
= sizeof(long);
501 v
->type
= ASN_INTEGER
;
502 return (uint8_t *)&ifp
->metric
;
503 case RIP2IFCONFSTATUS
:
504 *val_len
= sizeof(long);
505 v
->type
= ASN_INTEGER
;
506 return (uint8_t *)&valid
;
507 case RIP2IFCONFSRCADDRESS
:
508 *val_len
= sizeof(struct in_addr
);
509 return (uint8_t *)&addr
;
517 static uint8_t *rip2PeerTable(struct variable
*v
, oid name
[], size_t *length
,
518 int exact
, size_t *val_len
,
519 WriteMethod
**write_method
)
521 static struct in_addr addr
;
522 static int domain
= 0;
524 /* static time_t uptime; */
526 struct rip_peer
*peer
;
528 if (smux_header_table(v
, name
, length
, exact
, val_len
, write_method
)
532 memset(&addr
, 0, sizeof(struct in_addr
));
534 /* Lookup interface. */
535 peer
= rip2PeerLookup(v
, name
, length
, &addr
, exact
);
540 case RIP2PEERADDRESS
:
541 *val_len
= sizeof(struct in_addr
);
542 return (uint8_t *)&peer
->addr
;
546 return (uint8_t *)&domain
;
548 case RIP2PEERLASTUPDATE
:
549 return (uint8_t *)NULL
;
551 case RIP2PEERVERSION
:
552 *val_len
= sizeof(int);
553 version
= peer
->version
;
554 return (uint8_t *)&version
;
556 case RIP2PEERRCVBADPACKETS
:
557 *val_len
= sizeof(int);
558 return (uint8_t *)&peer
->recv_badpackets
;
560 case RIP2PEERRCVBADROUTES
:
561 *val_len
= sizeof(int);
562 return (uint8_t *)&peer
->recv_badroutes
;
570 /* Register RIPv2-MIB. */
571 static int rip_snmp_init(struct thread_master
*master
)
573 rip_ifaddr_table
= route_table_init();
576 REGISTER_MIB("mibII/rip", rip_variables
, variable
, rip_oid
);
580 static int rip_snmp_module_init(void)
582 hook_register(rip_ifaddr_add
, rip_snmp_ifaddr_add
);
583 hook_register(rip_ifaddr_del
, rip_snmp_ifaddr_del
);
585 hook_register(frr_late_init
, rip_snmp_init
);
589 FRR_MODULE_SETUP(.name
= "ripd_snmp", .version
= FRR_VERSION
,
590 .description
= "ripd AgentX SNMP module",
591 .init
= rip_snmp_module_init
,