]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_snmp.c
ripd: remove dead code
[mirror_frr.git] / ripd / rip_snmp.c
CommitLineData
718e3744 1/* RIP SNMP support
2 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
07661cb5 23#include <net-snmp/net-snmp-config.h>
fb62a3ce 24#include <net-snmp/net-snmp-includes.h>
718e3744 25
26#include "if.h"
d6b1bfba 27#include "vrf.h"
718e3744 28#include "log.h"
29#include "prefix.h"
30#include "command.h"
31#include "table.h"
32#include "smux.h"
5986b66b
DL
33#include "libfrr.h"
34#include "version.h"
718e3744 35
36#include "ripd/ripd.h"
6b0655a2 37
718e3744 38/* RIPv2-MIB. */
39#define RIPV2MIB 1,3,6,1,2,1,23
40
718e3744 41/* RIPv2-MIB rip2Globals values. */
42#define RIP2GLOBALROUTECHANGES 1
43#define RIP2GLOBALQUERIES 2
44
45/* RIPv2-MIB rip2IfStatEntry. */
46#define RIP2IFSTATENTRY 1
47
48/* RIPv2-MIB rip2IfStatTable. */
49#define RIP2IFSTATADDRESS 1
50#define RIP2IFSTATRCVBADPACKETS 2
51#define RIP2IFSTATRCVBADROUTES 3
52#define RIP2IFSTATSENTUPDATES 4
53#define RIP2IFSTATSTATUS 5
54
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
65
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
73
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
6b0655a2 80
718e3744 81/* Define SNMP local variables. */
82SNMP_LOCAL_VARIABLES
83
84/* RIP-MIB instances. */
d62a17ae 85static oid rip_oid[] = {RIPV2MIB};
718e3744 86
87/* Interface cache table sorted by interface's address. */
1c6f50bf 88static struct route_table *rip_ifaddr_table;
718e3744 89
90/* Hook functions. */
d7c0a89a
QY
91static uint8_t *rip2Globals(struct variable *, oid[], size_t *, int, size_t *,
92 WriteMethod **);
93static uint8_t *rip2IfStatEntry(struct variable *, oid[], size_t *, int,
94 size_t *, WriteMethod **);
95static uint8_t *rip2IfConfAddress(struct variable *, oid[], size_t *, int,
96 size_t *, WriteMethod **);
97static uint8_t *rip2PeerTable(struct variable *, oid[], size_t *, int, size_t *,
98 WriteMethod **);
d62a17ae 99
100static 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,
107 COUNTER,
108 RONLY,
109 rip2IfStatEntry,
110 3,
111 {2, 1, 2}},
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}},
115 {RIP2IFCONFADDRESS,
116 IPADDRESS,
117 RONLY,
118 rip2IfConfAddress,
119 /* RIP Interface Configuration Table. */
120 3,
121 {3, 1, 1}},
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,
128 COUNTER,
129 RONLY,
130 rip2IfConfAddress,
131 3,
132 {3, 1, 7}},
133 {RIP2IFCONFSTATUS, COUNTER, RONLY, rip2IfConfAddress, 3, {3, 1, 8}},
134 {RIP2IFCONFSRCADDRESS,
135 IPADDRESS,
136 RONLY,
137 rip2IfConfAddress,
138 3,
139 {3, 1, 9}},
140 {RIP2PEERADDRESS,
141 IPADDRESS,
142 RONLY,
143 rip2PeerTable,
144 /* RIP Peer Table. */
145 3,
146 {4, 1, 1}},
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}}};
dd488a78 152
153extern struct thread_master *master;
6b0655a2 154
d7c0a89a
QY
155static uint8_t *rip2Globals(struct variable *v, oid name[], size_t *length,
156 int exact, size_t *var_len,
157 WriteMethod **write_method)
718e3744 158{
d62a17ae 159 if (smux_header_generic(v, name, length, exact, var_len, write_method)
160 == MATCH_FAILED)
161 return NULL;
162
163 /* Retrun global counter. */
164 switch (v->magic) {
165 case RIP2GLOBALROUTECHANGES:
166 return SNMP_INTEGER(rip_global_route_changes);
167 break;
168 case RIP2GLOBALQUERIES:
169 return SNMP_INTEGER(rip_global_queries);
170 break;
171 default:
172 return NULL;
173 break;
174 }
175 return NULL;
718e3744 176}
177
d62a17ae 178static int rip_snmp_ifaddr_add(struct connected *ifc)
718e3744 179{
d62a17ae 180 struct interface *ifp = ifc->ifp;
181 struct prefix *p;
182 struct route_node *rn;
718e3744 183
d62a17ae 184 p = ifc->address;
718e3744 185
d62a17ae 186 if (p->family != AF_INET)
187 return 0;
718e3744 188
d62a17ae 189 rn = route_node_get(rip_ifaddr_table, p);
190 rn->info = ifp;
191 return 0;
718e3744 192}
193
d62a17ae 194static int rip_snmp_ifaddr_del(struct connected *ifc)
718e3744 195{
d62a17ae 196 struct interface *ifp = ifc->ifp;
197 struct prefix *p;
198 struct route_node *rn;
199 struct interface *i;
200
201 p = ifc->address;
202
203 if (p->family != AF_INET)
204 return 0;
205
206 rn = route_node_lookup(rip_ifaddr_table, p);
207 if (!rn)
208 return 0;
209 i = rn->info;
47e5a60f 210 if (!strncmp(i->name, ifp->name, INTERFACE_NAMSIZ)) {
d62a17ae 211 rn->info = NULL;
212 route_unlock_node(rn);
213 route_unlock_node(rn);
214 }
215 return 0;
718e3744 216}
217
d62a17ae 218static struct interface *rip_ifaddr_lookup_next(struct in_addr *addr)
718e3744 219{
d62a17ae 220 struct prefix_ipv4 p;
221 struct route_node *rn;
222 struct interface *ifp;
223
224 p.family = AF_INET;
225 p.prefixlen = IPV4_MAX_BITLEN;
226 p.prefix = *addr;
227
228 rn = route_node_get(rip_ifaddr_table, (struct prefix *)&p);
229
230 for (rn = route_next(rn); rn; rn = route_next(rn))
231 if (rn->info)
232 break;
233
234 if (rn && rn->info) {
235 ifp = rn->info;
236 *addr = rn->p.u.prefix4;
237 route_unlock_node(rn);
238 return ifp;
239 }
240 return NULL;
718e3744 241}
242
d62a17ae 243static struct interface *rip2IfLookup(struct variable *v, oid name[],
244 size_t *length, struct in_addr *addr,
245 int exact)
718e3744 246{
d62a17ae 247 int len;
248 struct interface *ifp;
718e3744 249
d62a17ae 250 if (exact) {
251 /* Check the length. */
252 if (*length - v->namelen != sizeof(struct in_addr))
253 return NULL;
718e3744 254
d62a17ae 255 oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);
718e3744 256
d62a17ae 257 return if_lookup_exact_address((void *)addr, AF_INET,
258 VRF_DEFAULT);
259 } else {
260 len = *length - v->namelen;
261 if (len > 4)
262 len = 4;
718e3744 263
d62a17ae 264 oid2in_addr(name + v->namelen, len, addr);
718e3744 265
d62a17ae 266 ifp = rip_ifaddr_lookup_next(addr);
718e3744 267
d62a17ae 268 if (ifp == NULL)
269 return NULL;
718e3744 270
d62a17ae 271 oid_copy_addr(name + v->namelen, addr, sizeof(struct in_addr));
718e3744 272
d62a17ae 273 *length = v->namelen + sizeof(struct in_addr);
718e3744 274
d62a17ae 275 return ifp;
276 }
718e3744 277 return NULL;
d62a17ae 278}
718e3744 279
d62a17ae 280static struct rip_peer *rip2PeerLookup(struct variable *v, oid name[],
281 size_t *length, struct in_addr *addr,
282 int exact)
283{
284 int len;
285 struct rip_peer *peer;
286
29e897ad
RW
287 if (!rip)
288 return NULL;
289
d62a17ae 290 if (exact) {
291 /* Check the length. */
292 if (*length - v->namelen != sizeof(struct in_addr) + 1)
293 return NULL;
294
295 oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);
296
297 peer = rip_peer_lookup(addr);
298
299 if (peer->domain
300 == (int)name[v->namelen + sizeof(struct in_addr)])
301 return peer;
302
303 return NULL;
304 } else {
305 len = *length - v->namelen;
306 if (len > 4)
307 len = 4;
308
309 oid2in_addr(name + v->namelen, len, addr);
310
311 len = *length - v->namelen;
312 peer = rip_peer_lookup(addr);
313 if (peer) {
314 if ((len < (int)sizeof(struct in_addr) + 1)
315 || (peer->domain
316 > (int)name[v->namelen
317 + sizeof(struct in_addr)])) {
318 oid_copy_addr(name + v->namelen, &peer->addr,
319 sizeof(struct in_addr));
320 name[v->namelen + sizeof(struct in_addr)] =
321 peer->domain;
322 *length =
323 sizeof(struct in_addr) + v->namelen + 1;
324 return peer;
325 }
326 }
327 peer = rip_peer_lookup_next(addr);
328
329 if (!peer)
330 return NULL;
331
332 oid_copy_addr(name + v->namelen, &peer->addr,
333 sizeof(struct in_addr));
334 name[v->namelen + sizeof(struct in_addr)] = peer->domain;
335 *length = sizeof(struct in_addr) + v->namelen + 1;
336
337 return peer;
338 }
718e3744 339 return NULL;
718e3744 340}
341
d7c0a89a
QY
342static uint8_t *rip2IfStatEntry(struct variable *v, oid name[], size_t *length,
343 int exact, size_t *var_len,
344 WriteMethod **write_method)
718e3744 345{
d62a17ae 346 struct interface *ifp;
347 struct rip_interface *ri;
348 static struct in_addr addr;
349 static long valid = SNMP_VALID;
350
351 if (smux_header_table(v, name, length, exact, var_len, write_method)
352 == MATCH_FAILED)
353 return NULL;
354
355 memset(&addr, 0, sizeof(struct in_addr));
356
357 /* Lookup interface. */
358 ifp = rip2IfLookup(v, name, length, &addr, exact);
359 if (!ifp)
360 return NULL;
361
362 /* Fetch rip_interface information. */
363 ri = ifp->info;
364
365 switch (v->magic) {
366 case RIP2IFSTATADDRESS:
367 return SNMP_IPADDRESS(addr);
368 break;
369 case RIP2IFSTATRCVBADPACKETS:
370 *var_len = sizeof(long);
d7c0a89a 371 return (uint8_t *)&ri->recv_badpackets;
d62a17ae 372
373 case RIP2IFSTATRCVBADROUTES:
374 *var_len = sizeof(long);
d7c0a89a 375 return (uint8_t *)&ri->recv_badroutes;
d62a17ae 376
377 case RIP2IFSTATSENTUPDATES:
378 *var_len = sizeof(long);
d7c0a89a 379 return (uint8_t *)&ri->sent_updates;
d62a17ae 380
381 case RIP2IFSTATSTATUS:
382 *var_len = sizeof(long);
383 v->type = ASN_INTEGER;
d7c0a89a 384 return (uint8_t *)&valid;
d62a17ae 385
386 default:
387 return NULL;
388 }
389 return NULL;
718e3744 390}
391
d62a17ae 392static long rip2IfConfSend(struct rip_interface *ri)
718e3744 393{
394#define doNotSend 1
395#define ripVersion1 2
396#define rip1Compatible 3
397#define ripVersion2 4
398#define ripV1Demand 5
399#define ripV2Demand 6
400
d62a17ae 401 if (!ri->running)
402 return doNotSend;
403
404 if (ri->ri_send & RIPv2)
405 return ripVersion2;
406 else if (ri->ri_send & RIPv1)
407 return ripVersion1;
408 else if (rip) {
409 if (rip->version_send == RIPv2)
410 return ripVersion2;
411 else if (rip->version_send == RIPv1)
412 return ripVersion1;
413 }
414 return doNotSend;
718e3744 415}
416
d62a17ae 417static long rip2IfConfReceive(struct rip_interface *ri)
718e3744 418{
419#define rip1 1
420#define rip2 2
421#define rip1OrRip2 3
422#define doNotReceive 4
423
d62a17ae 424 int recvv;
425
426 if (!ri->running)
427 return doNotReceive;
428
429 recvv = (ri->ri_receive == RI_RIP_UNSPEC) ? rip->version_recv
430 : ri->ri_receive;
431 if (recvv == RI_RIP_VERSION_1_AND_2)
432 return rip1OrRip2;
433 else if (recvv & RIPv2)
434 return rip2;
435 else if (recvv & RIPv1)
436 return rip1;
437 else
438 return doNotReceive;
718e3744 439}
440
d7c0a89a
QY
441static uint8_t *rip2IfConfAddress(struct variable *v, oid name[],
442 size_t *length, int exact, size_t *val_len,
443 WriteMethod **write_method)
718e3744 444{
d62a17ae 445 static struct in_addr addr;
446 static long valid = SNMP_INVALID;
447 static long domain = 0;
448 static long config = 0;
d7c0a89a 449 static unsigned int auth = 0;
d62a17ae 450 struct interface *ifp;
451 struct rip_interface *ri;
452
453 if (smux_header_table(v, name, length, exact, val_len, write_method)
454 == MATCH_FAILED)
455 return NULL;
456
457 memset(&addr, 0, sizeof(struct in_addr));
458
459 /* Lookup interface. */
460 ifp = rip2IfLookup(v, name, length, &addr, exact);
461 if (!ifp)
462 return NULL;
463
464 /* Fetch rip_interface information. */
465 ri = ifp->info;
466
467 switch (v->magic) {
468 case RIP2IFCONFADDRESS:
469 *val_len = sizeof(struct in_addr);
d7c0a89a 470 return (uint8_t *)&addr;
d62a17ae 471
472 case RIP2IFCONFDOMAIN:
473 *val_len = 2;
d7c0a89a 474 return (uint8_t *)&domain;
d62a17ae 475
476 case RIP2IFCONFAUTHTYPE:
477 auth = ri->auth_type;
478 *val_len = sizeof(long);
479 v->type = ASN_INTEGER;
d7c0a89a 480 return (uint8_t *)&auth;
d62a17ae 481
482 case RIP2IFCONFAUTHKEY:
483 *val_len = 0;
d7c0a89a 484 return (uint8_t *)&domain;
d62a17ae 485 case RIP2IFCONFSEND:
486 config = rip2IfConfSend(ri);
487 *val_len = sizeof(long);
488 v->type = ASN_INTEGER;
d7c0a89a 489 return (uint8_t *)&config;
d62a17ae 490 case RIP2IFCONFRECEIVE:
491 config = rip2IfConfReceive(ri);
492 *val_len = sizeof(long);
493 v->type = ASN_INTEGER;
d7c0a89a 494 return (uint8_t *)&config;
d62a17ae 495
496 case RIP2IFCONFDEFAULTMETRIC:
497 *val_len = sizeof(long);
498 v->type = ASN_INTEGER;
d7c0a89a 499 return (uint8_t *)&ifp->metric;
d62a17ae 500 case RIP2IFCONFSTATUS:
501 *val_len = sizeof(long);
502 v->type = ASN_INTEGER;
d7c0a89a 503 return (uint8_t *)&valid;
d62a17ae 504 case RIP2IFCONFSRCADDRESS:
505 *val_len = sizeof(struct in_addr);
d7c0a89a 506 return (uint8_t *)&addr;
d62a17ae 507
508 default:
509 return NULL;
510 }
511 return NULL;
718e3744 512}
513
d7c0a89a
QY
514static uint8_t *rip2PeerTable(struct variable *v, oid name[], size_t *length,
515 int exact, size_t *val_len,
516 WriteMethod **write_method)
718e3744 517{
d62a17ae 518 static struct in_addr addr;
519 static int domain = 0;
520 static int version;
521 /* static time_t uptime; */
522
523 struct rip_peer *peer;
524
525 if (smux_header_table(v, name, length, exact, val_len, write_method)
526 == MATCH_FAILED)
527 return NULL;
528
529 memset(&addr, 0, sizeof(struct in_addr));
530
531 /* Lookup interface. */
532 peer = rip2PeerLookup(v, name, length, &addr, exact);
533 if (!peer)
534 return NULL;
535
536 switch (v->magic) {
537 case RIP2PEERADDRESS:
538 *val_len = sizeof(struct in_addr);
d7c0a89a 539 return (uint8_t *)&peer->addr;
d62a17ae 540
541 case RIP2PEERDOMAIN:
542 *val_len = 2;
d7c0a89a 543 return (uint8_t *)&domain;
d62a17ae 544
545 case RIP2PEERLASTUPDATE:
718e3744 546#if 0
547 /* We don't know the SNMP agent startup time. We have two choices here:
548 * - assume ripd startup time equals SNMP agent startup time
549 * - don't support this variable, at all
550 * Currently, we do the latter...
551 */
552 *val_len = sizeof (time_t);
553 uptime = peer->uptime; /* now - snmp_agent_startup - peer->uptime */
d7c0a89a 554 return (uint8_t *) &uptime;
718e3744 555#else
d7c0a89a 556 return (uint8_t *)NULL;
718e3744 557#endif
558
d62a17ae 559 case RIP2PEERVERSION:
560 *val_len = sizeof(int);
561 version = peer->version;
d7c0a89a 562 return (uint8_t *)&version;
718e3744 563
d62a17ae 564 case RIP2PEERRCVBADPACKETS:
565 *val_len = sizeof(int);
d7c0a89a 566 return (uint8_t *)&peer->recv_badpackets;
718e3744 567
d62a17ae 568 case RIP2PEERRCVBADROUTES:
569 *val_len = sizeof(int);
d7c0a89a 570 return (uint8_t *)&peer->recv_badroutes;
718e3744 571
d62a17ae 572 default:
573 return NULL;
574 }
575 return NULL;
718e3744 576}
577
578/* Register RIPv2-MIB. */
d62a17ae 579static int rip_snmp_init(struct thread_master *master)
718e3744 580{
d62a17ae 581 rip_ifaddr_table = route_table_init();
718e3744 582
d62a17ae 583 smux_init(master);
584 REGISTER_MIB("mibII/rip", rip_variables, variable, rip_oid);
585 return 0;
718e3744 586}
5986b66b 587
d62a17ae 588static int rip_snmp_module_init(void)
5986b66b 589{
d62a17ae 590 hook_register(rip_ifaddr_add, rip_snmp_ifaddr_add);
591 hook_register(rip_ifaddr_del, rip_snmp_ifaddr_del);
5986b66b 592
d62a17ae 593 hook_register(frr_late_init, rip_snmp_init);
594 return 0;
5986b66b
DL
595}
596
d62a17ae 597FRR_MODULE_SETUP(.name = "ripd_snmp", .version = FRR_VERSION,
598 .description = "ripd AgentX SNMP module",
599 .init = rip_snmp_module_init, )