summaryrefslogtreecommitdiffstats
path: root/ripd/rip_snmp.c
blob: 5a6b71fbaaacaa1953b002047b341a9531857d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* RIP SNMP support
 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

#include "if.h"
#include "vrf.h"
#include "log.h"
#include "prefix.h"
#include "command.h"
#include "table.h"
#include "smux.h"
#include "libfrr.h"
#include "version.h"

#include "ripd/ripd.h"

/* RIPv2-MIB. */
#define RIPV2MIB 1,3,6,1,2,1,23

/* RIPv2-MIB rip2Globals values. */
#define RIP2GLOBALROUTECHANGES  1
#define RIP2GLOBALQUERIES       2

/* RIPv2-MIB rip2IfStatEntry. */
#define RIP2IFSTATENTRY         1

/* RIPv2-MIB rip2IfStatTable. */
#define RIP2IFSTATADDRESS       1
#define RIP2IFSTATRCVBADPACKETS 2
#define RIP2IFSTATRCVBADROUTES  3
#define RIP2IFSTATSENTUPDATES   4
#define RIP2IFSTATSTATUS        5

/* RIPv2-MIB rip2IfConfTable. */
#define RIP2IFCONFADDRESS       1
#define RIP2IFCONFDOMAIN        2
#define RIP2IFCONFAUTHTYPE      3
#define RIP2IFCONFAUTHKEY       4
#define RIP2IFCONFSEND          5
#define RIP2IFCONFRECEIVE       6
#define RIP2IFCONFDEFAULTMETRIC 7
#define RIP2IFCONFSTATUS        8
#define RIP2IFCONFSRCADDRESS    9

/* RIPv2-MIB rip2PeerTable. */
#define RIP2PEERADDRESS         1
#define RIP2PEERDOMAIN          2
#define RIP2PEERLASTUPDATE      3
#define RIP2PEERVERSION         4
#define RIP2PEERRCVBADPACKETS   5
#define RIP2PEERRCVBADROUTES    6

/* SNMP value hack. */
#define COUNTER     ASN_COUNTER
#define INTEGER     ASN_INTEGER
#define TIMETICKS   ASN_TIMETICKS
#define IPADDRESS   ASN_IPADDRESS
#define STRING      ASN_OCTET_STR

/* Define SNMP local variables. */
SNMP_LOCAL_VARIABLES

/* RIP-MIB instances. */
static oid rip_oid[] = {RIPV2MIB};

/* Interface cache table sorted by interface's address. */
static struct route_table *rip_ifaddr_table;

/* Hook functions. */
static uint8_t *rip2Globals(struct variable *, oid[], size_t *, int, size_t *,
			    WriteMethod **);
static uint8_t *rip2IfStatEntry(struct variable *, oid[], size_t *, int,
				size_t *, WriteMethod **);
static uint8_t *rip2IfConfAddress(struct variable *, oid[], size_t *, int,
				  size_t *, WriteMethod **);
static uint8_t *rip2PeerTable(struct variable *, oid[], size_t *, int, size_t *,
			      WriteMethod **);

static struct variable rip_variables[] = {
	/* RIP Global Counters. */
	{RIP2GLOBALROUTECHANGES, COUNTER, RONLY, rip2Globals, 2, {1, 1}},
	{RIP2GLOBALQUERIES, COUNTER, RONLY, rip2Globals, 2, {1, 2}},
	/* RIP Interface Tables. */
	{RIP2IFSTATADDRESS, IPADDRESS, RONLY, rip2IfStatEntry, 3, {2, 1, 1}},
	{RIP2IFSTATRCVBADPACKETS,
	 COUNTER,
	 RONLY,
	 rip2IfStatEntry,
	 3,
	 {2, 1, 2}},
	{RIP2IFSTATRCVBADROUTES, COUNTER, RONLY, rip2IfStatEntry, 3, {2, 1, 3}},
	{RIP2IFSTATSENTUPDATES, COUNTER, RONLY, rip2IfStatEntry, 3, {2, 1, 4}},
	{RIP2IFSTATSTATUS, COUNTER, RWRITE, rip2IfStatEntry, 3, {2, 1, 5}},
	{RIP2IFCONFADDRESS,
	 IPADDRESS,
	 RONLY,
	 rip2IfConfAddress,
	 /* RIP Interface Configuration Table. */
	 3,
	 {3, 1, 1}},
	{RIP2IFCONFDOMAIN, STRING, RONLY, rip2IfConfAddress, 3, {3, 1, 2}},
	{RIP2IFCONFAUTHTYPE, COUNTER, RONLY, rip2IfConfAddress, 3, {3, 1, 3}},
	{RIP2IFCONFAUTHKEY, STRING, RONLY, rip2IfConfAddress, 3, {3, 1, 4}},
	{RIP2IFCONFSEND, COUNTER, RONLY, rip2IfConfAddress, 3, {3, 1, 5}},
	{RIP2IFCONFRECEIVE, COUNTER, RONLY, rip2IfConfAddress, 3, {3, 1, 6}},
	{RIP2IFCONFDEFAULTMETRIC,
	 COUNTER,
	 RONLY,
	 rip2IfConfAddress,
	 3,
	 {3, 1, 7}},
	{RIP2IFCONFSTATUS, COUNTER, RONLY, rip2IfConfAddress, 3, {3, 1, 8}},
	{RIP2IFCONFSRCADDRESS,
	 IPADDRESS,
	 RONLY,
	 rip2IfConfAddress,
	 3,
	 {3, 1, 9}},
	{RIP2PEERADDRESS,
	 IPADDRESS,
	 RONLY,
	 rip2PeerTable,
	 /* RIP Peer Table. */
	 3,
	 {4, 1, 1}},
	{RIP2PEERDOMAIN, STRING, RONLY, rip2PeerTable, 3, {4, 1, 2}},
	{RIP2PEERLASTUPDATE, TIMETICKS, RONLY, rip2PeerTable, 3, {4, 1, 3}},
	{RIP2PEERVERSION, INTEGER, RONLY, rip2PeerTable, 3, {4, 1, 4}},
	{RIP2PEERRCVBADPACKETS, COUNTER, RONLY, rip2PeerTable, 3, {4, 1, 5}},
	{RIP2PEERRCVBADROUTES, COUNTER, RONLY, rip2PeerTable, 3, {4, 1, 6}}};

extern struct thread_master *master;

static uint8_t *rip2Globals(struct variable *v, oid name[], size_t *length,
			    int exact, size_t *var_len,
			    WriteMethod **write_method)
{
	struct rip *rip;

	if (smux_header_generic(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	rip = rip_lookup_by_vrf_id(VRF_DEFAULT);
	if (!rip)
		return NULL;

	/* Retrun global counter. */
	switch (v->magic) {
	case RIP2GLOBALROUTECHANGES:
		return SNMP_INTEGER(rip->counters.route_changes);
		break;
	case RIP2GLOBALQUERIES:
		return SNMP_INTEGER(rip->counters.queries);
		break;
	default:
		return NULL;
		break;
	}
	return NULL;
}

static int rip_snmp_ifaddr_add(struct connected *ifc)
{
	struct interface *ifp = ifc->ifp;
	struct prefix *p;
	struct route_node *rn;

	p = ifc->address;

	if (p->family != AF_INET)
		return 0;

	rn = route_node_get(rip_ifaddr_table, p);
	rn->info = ifp;
	return 0;
}

static int rip_snmp_ifaddr_del(struct connected *ifc)
{
	struct interface *ifp = ifc->ifp;
	struct prefix *p;
	struct route_node *rn;
	struct interface *i;

	p = ifc->address;

	if (p->family != AF_INET)
		return 0;

	rn = route_node_lookup(rip_ifaddr_table, p);
	if (!rn)
		return 0;
	i = rn->info;
	if (!strncmp(i->name, ifp->name, INTERFACE_NAMSIZ)) {
		rn->info = NULL;
		route_unlock_node(rn);
		route_unlock_node(rn);
	}
	return 0;
}

static struct interface *rip_ifaddr_lookup_next(struct in_addr *addr)
{
	struct prefix_ipv4 p;
	struct route_node *rn;
	struct interface *ifp;

	p.family = AF_INET;
	p.prefixlen = IPV4_MAX_BITLEN;
	p.prefix = *addr;

	rn = route_node_get(rip_ifaddr_table, (struct prefix *)&p);

	for (rn = route_next(rn); rn; rn = route_next(rn))
		if (rn->info)
			break;

	if (rn && rn->info) {
		ifp = rn->info;
		*addr = rn->p.u.prefix4;
		route_unlock_node(rn);
		return ifp;
	}
	return NULL;
}

static struct interface *rip2IfLookup(struct variable *v, oid name[],
				      size_t *length, struct in_addr *addr,
				      int exact)
{
	int len;
	struct interface *ifp;

	if (exact) {
		/* Check the length. */
		if (*length - v->namelen != sizeof(struct in_addr))
			return NULL;

		oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);

		return if_lookup_exact_address((void *)addr, AF_INET,
					       VRF_DEFAULT);
	} else {
		len = *length - v->namelen;
		if (len > 4)
			len = 4;

		oid2in_addr(name + v->namelen, len, addr);

		ifp = rip_ifaddr_lookup_next(addr);

		if (ifp == NULL)
			return NULL;

		oid_copy_addr(name + v->namelen, addr, sizeof(struct in_addr));

		*length = v->namelen + sizeof(struct in_addr);

		return ifp;
	}
	return NULL;
}

static struct rip_peer *rip2PeerLookup(struct variable *v, oid name[],
				       size_t *length, struct in_addr *addr,
				       int exact)
{
	struct rip *rip;
	int len;
	struct rip_peer *peer;

	rip = rip_lookup_by_vrf_id(VRF_DEFAULT);
	if (!rip)
		return NULL;

	if (exact) {
		/* Check the length. */
		if (*length - v->namelen != sizeof(struct in_addr) + 1)
			return NULL;

		oid2in_addr(name + v->namelen, sizeof(struct in_addr), addr);

		peer = rip_peer_lookup(rip, addr);

		if (peer->domain
		    == (int)name[v->namelen + sizeof(struct in_addr)])
			return peer;

		return NULL;
	} else {
		len = *length - v->namelen;
		if (len > 4)
			len = 4;

		oid2in_addr(name + v->namelen, len, addr);

		len = *length - v->namelen;
		peer = rip_peer_lookup(rip, addr);
		if (peer) {
			if ((len < (int)sizeof(struct in_addr) + 1)
			    || (peer->domain
				> (int)name[v->namelen
					    + sizeof(struct in_addr)])) {
				oid_copy_addr(name + v->namelen, &peer->addr,
					      sizeof(struct in_addr));
				name[v->namelen + sizeof(struct in_addr)] =
					peer->domain;
				*length =
					sizeof(struct in_addr) + v->namelen + 1;
				return peer;
			}
		}
		peer = rip_peer_lookup_next(rip, addr);

		if (!peer)
			return NULL;

		oid_copy_addr(name + v->namelen, &peer->addr,
			      sizeof(struct in_addr));
		name[v->namelen + sizeof(struct in_addr)] = peer->domain;
		*length = sizeof(struct in_addr) + v->namelen + 1;

		return peer;
	}
	return NULL;
}

static uint8_t *rip2IfStatEntry(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct interface *ifp;
	struct rip_interface *ri;
	static struct in_addr addr;
	static long valid = SNMP_VALID;

	if (smux_header_table(v, name, length, exact, var_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&addr, 0, sizeof(struct in_addr));

	/* Lookup interface. */
	ifp = rip2IfLookup(v, name, length, &addr, exact);
	if (!ifp)
		return NULL;

	/* Fetch rip_interface information. */
	ri = ifp->info;

	switch (v->magic) {
	case RIP2IFSTATADDRESS:
		return SNMP_IPADDRESS(addr);
		break;
	case RIP2IFSTATRCVBADPACKETS:
		*var_len = sizeof(long);
		return (uint8_t *)&ri->recv_badpackets;

	case RIP2IFSTATRCVBADROUTES:
		*var_len = sizeof(long);
		return (uint8_t *)&ri->recv_badroutes;

	case RIP2IFSTATSENTUPDATES:
		*var_len = sizeof(long);
		return (uint8_t *)&ri->sent_updates;

	case RIP2IFSTATSTATUS:
		*var_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&valid;

	default:
		return NULL;
	}
	return NULL;
}

static long rip2IfConfSend(struct rip_interface *ri)
{
#define doNotSend       1
#define ripVersion1     2
#define rip1Compatible  3
#define ripVersion2     4
#define ripV1Demand     5
#define ripV2Demand     6

	if (!ri->running)
		return doNotSend;

	if (ri->ri_send & RIPv2)
		return ripVersion2;
	else if (ri->ri_send & RIPv1)
		return ripVersion1;
	else if (ri->rip) {
		if (ri->rip->version_send == RIPv2)
			return ripVersion2;
		else if (ri->rip->version_send == RIPv1)
			return ripVersion1;
	}
	return doNotSend;
}

static long rip2IfConfReceive(struct rip_interface *ri)
{
#define rip1            1
#define rip2            2
#define rip1OrRip2      3
#define doNotReceive    4

	int recvv;

	if (!ri->running)
		return doNotReceive;

	recvv = (ri->ri_receive == RI_RIP_UNSPEC) ? ri->rip->version_recv
						  : ri->ri_receive;
	if (recvv == RI_RIP_VERSION_1_AND_2)
		return rip1OrRip2;
	else if (recvv & RIPv2)
		return rip2;
	else if (recvv & RIPv1)
		return rip1;
	else
		return doNotReceive;
}

static uint8_t *rip2IfConfAddress(struct variable *v, oid name[],
				  size_t *length, int exact, size_t *val_len,
				  WriteMethod **write_method)
{
	static struct in_addr addr;
	static long valid = SNMP_INVALID;
	static long domain = 0;
	static long config = 0;
	static unsigned int auth = 0;
	struct interface *ifp;
	struct rip_interface *ri;

	if (smux_header_table(v, name, length, exact, val_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&addr, 0, sizeof(struct in_addr));

	/* Lookup interface. */
	ifp = rip2IfLookup(v, name, length, &addr, exact);
	if (!ifp)
		return NULL;

	/* Fetch rip_interface information. */
	ri = ifp->info;

	switch (v->magic) {
	case RIP2IFCONFADDRESS:
		*val_len = sizeof(struct in_addr);
		return (uint8_t *)&addr;

	case RIP2IFCONFDOMAIN:
		*val_len = 2;
		return (uint8_t *)&domain;

	case RIP2IFCONFAUTHTYPE:
		auth = ri->auth_type;
		*val_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&auth;

	case RIP2IFCONFAUTHKEY:
		*val_len = 0;
		return (uint8_t *)&domain;
	case RIP2IFCONFSEND:
		config = rip2IfConfSend(ri);
		*val_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&config;
	case RIP2IFCONFRECEIVE:
		config = rip2IfConfReceive(ri);
		*val_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&config;

	case RIP2IFCONFDEFAULTMETRIC:
		*val_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&ifp->metric;
	case RIP2IFCONFSTATUS:
		*val_len = sizeof(long);
		v->type = ASN_INTEGER;
		return (uint8_t *)&valid;
	case RIP2IFCONFSRCADDRESS:
		*val_len = sizeof(struct in_addr);
		return (uint8_t *)&addr;

	default:
		return NULL;
	}
	return NULL;
}

static uint8_t *rip2PeerTable(struct variable *v, oid name[], size_t *length,
			      int exact, size_t *val_len,
			      WriteMethod **write_method)
{
	static struct in_addr addr;
	static int domain = 0;
	static int version;
	/* static time_t uptime; */

	struct rip_peer *peer;

	if (smux_header_table(v, name, length, exact, val_len, write_method)
	    == MATCH_FAILED)
		return NULL;

	memset(&addr, 0, sizeof(struct in_addr));

	/* Lookup interface. */
	peer = rip2PeerLookup(v, name, length, &addr, exact);
	if (!peer)
		return NULL;

	switch (v->magic) {
	case RIP2PEERADDRESS:
		*val_len = sizeof(struct in_addr);
		return (uint8_t *)&peer->addr;

	case RIP2PEERDOMAIN:
		*val_len = 2;
		return (uint8_t *)&domain;

	case RIP2PEERLASTUPDATE:
#if 0 
      /* We don't know the SNMP agent startup time. We have two choices here:
       * - assume ripd startup time equals SNMP agent startup time
       * - don't support this variable, at all
       * Currently, we do the latter...
       */
      *val_len = sizeof (time_t);
      uptime = peer->uptime; /* now - snmp_agent_startup - peer->uptime */
      return (uint8_t *) &uptime;
#else
		return (uint8_t *)NULL;
#endif

	case RIP2PEERVERSION:
		*val_len = sizeof(int);
		version = peer->version;
		return (uint8_t *)&version;

	case RIP2PEERRCVBADPACKETS:
		*val_len = sizeof(int);
		return (uint8_t *)&peer->recv_badpackets;

	case RIP2PEERRCVBADROUTES:
		*val_len = sizeof(int);
		return (uint8_t *)&peer->recv_badroutes;

	default:
		return NULL;
	}
	return NULL;
}

/* Register RIPv2-MIB. */
static int rip_snmp_init(struct thread_master *master)
{
	rip_ifaddr_table = route_table_init();

	smux_init(master);
	REGISTER_MIB("mibII/rip", rip_variables, variable, rip_oid);
	return 0;
}

static int rip_snmp_module_init(void)
{
	hook_register(rip_ifaddr_add, rip_snmp_ifaddr_add);
	hook_register(rip_ifaddr_del, rip_snmp_ifaddr_del);

	hook_register(frr_late_init, rip_snmp_init);
	return 0;
}

FRR_MODULE_SETUP(.name = "ripd_snmp", .version = FRR_VERSION,
		 .description = "ripd AgentX SNMP module",
		 .init = rip_snmp_module_init, )