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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
|
/* BGP4 SNMP support
Copyright (C) 1999, 2000 Kunihiro Ishiguro
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 GNU Zebra; see the file COPYING. If not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#include <zebra.h>
#ifdef HAVE_SNMP
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include "if.h"
#include "log.h"
#include "prefix.h"
#include "command.h"
#include "thread.h"
#include "smux.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_snmp.h"
/* BGP4-MIB described in RFC1657. */
#define BGP4MIB 1,3,6,1,2,1,15
/* BGP TRAP. */
#define BGPESTABLISHED 1
#define BGPBACKWARDTRANSITION 2
/* BGP MIB bgpVersion. */
#define BGPVERSION 0
/* BGP MIB bgpLocalAs. */
#define BGPLOCALAS 0
/* BGP MIB bgpPeerTable. */
#define BGPPEERIDENTIFIER 1
#define BGPPEERSTATE 2
#define BGPPEERADMINSTATUS 3
#define BGPPEERNEGOTIATEDVERSION 4
#define BGPPEERLOCALADDR 5
#define BGPPEERLOCALPORT 6
#define BGPPEERREMOTEADDR 7
#define BGPPEERREMOTEPORT 8
#define BGPPEERREMOTEAS 9
#define BGPPEERINUPDATES 10
#define BGPPEEROUTUPDATES 11
#define BGPPEERINTOTALMESSAGES 12
#define BGPPEEROUTTOTALMESSAGES 13
#define BGPPEERLASTERROR 14
#define BGPPEERFSMESTABLISHEDTRANSITIONS 15
#define BGPPEERFSMESTABLISHEDTIME 16
#define BGPPEERCONNECTRETRYINTERVAL 17
#define BGPPEERHOLDTIME 18
#define BGPPEERKEEPALIVE 19
#define BGPPEERHOLDTIMECONFIGURED 20
#define BGPPEERKEEPALIVECONFIGURED 21
#define BGPPEERMINROUTEADVERTISEMENTINTERVAL 22
#define BGPPEERINUPDATEELAPSEDTIME 23
/* BGP MIB bgpIdentifier. */
#define BGPIDENTIFIER 0
/* BGP MIB bgpRcvdPathAttrTable */
#define BGPPATHATTRPEER 1
#define BGPPATHATTRDESTNETWORK 2
#define BGPPATHATTRORIGIN 3
#define BGPPATHATTRASPATH 4
#define BGPPATHATTRNEXTHOP 5
#define BGPPATHATTRINTERASMETRIC 6
/* BGP MIB bgp4PathAttrTable. */
#define BGP4PATHATTRPEER 1
#define BGP4PATHATTRIPADDRPREFIXLEN 2
#define BGP4PATHATTRIPADDRPREFIX 3
#define BGP4PATHATTRORIGIN 4
#define BGP4PATHATTRASPATHSEGMENT 5
#define BGP4PATHATTRNEXTHOP 6
#define BGP4PATHATTRMULTIEXITDISC 7
#define BGP4PATHATTRLOCALPREF 8
#define BGP4PATHATTRATOMICAGGREGATE 9
#define BGP4PATHATTRAGGREGATORAS 10
#define BGP4PATHATTRAGGREGATORADDR 11
#define BGP4PATHATTRCALCLOCALPREF 12
#define BGP4PATHATTRBEST 13
#define BGP4PATHATTRUNKNOWN 14
/* SNMP value hack. */
#define INTEGER ASN_INTEGER
#define INTEGER32 ASN_INTEGER
#define COUNTER32 ASN_COUNTER
#define OCTET_STRING ASN_OCTET_STR
#define IPADDRESS ASN_IPADDRESS
#define GAUGE32 ASN_UNSIGNED
/* Declare static local variables for convenience. */
SNMP_LOCAL_VARIABLES
/* BGP-MIB instances. */
oid bgp_oid [] = { BGP4MIB };
oid bgp_trap_oid [] = { BGP4MIB, 0 };
/* IP address 0.0.0.0. */
static struct in_addr bgp_empty_addr = { .s_addr = 0 };
/* Hook functions. */
static u_char *bgpVersion (struct variable *, oid [], size_t *, int,
size_t *, WriteMethod **);
static u_char *bgpLocalAs (struct variable *, oid [], size_t *,
int, size_t *, WriteMethod **);
static u_char *bgpPeerTable (struct variable *, oid [], size_t *,
int, size_t *, WriteMethod **);
static u_char *bgpRcvdPathAttrTable (struct variable *, oid [], size_t *,
int, size_t *, WriteMethod **);
static u_char *bgpIdentifier (struct variable *, oid [], size_t *,
int, size_t *, WriteMethod **);
static u_char *bgp4PathAttrTable (struct variable *, oid [], size_t *,
int, size_t *, WriteMethod **);
/* static u_char *bgpTraps (); */
struct variable bgp_variables[] =
{
/* BGP version. */
{BGPVERSION, OCTET_STRING, RONLY, bgpVersion,
1, {1}},
/* BGP local AS. */
{BGPLOCALAS, INTEGER, RONLY, bgpLocalAs,
1, {2}},
/* BGP peer table. */
{BGPPEERIDENTIFIER, IPADDRESS, RONLY, bgpPeerTable,
3, {3, 1, 1}},
{BGPPEERSTATE, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 2}},
{BGPPEERADMINSTATUS, INTEGER, RWRITE, bgpPeerTable,
3, {3, 1, 3}},
{BGPPEERNEGOTIATEDVERSION, INTEGER32, RONLY, bgpPeerTable,
3, {3, 1, 4}},
{BGPPEERLOCALADDR, IPADDRESS, RONLY, bgpPeerTable,
3, {3, 1, 5}},
{BGPPEERLOCALPORT, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 6}},
{BGPPEERREMOTEADDR, IPADDRESS, RONLY, bgpPeerTable,
3, {3, 1, 7}},
{BGPPEERREMOTEPORT, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 8}},
{BGPPEERREMOTEAS, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 9}},
{BGPPEERINUPDATES, COUNTER32, RONLY, bgpPeerTable,
3, {3, 1, 10}},
{BGPPEEROUTUPDATES, COUNTER32, RONLY, bgpPeerTable,
3, {3, 1, 11}},
{BGPPEERINTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
3, {3, 1, 12}},
{BGPPEEROUTTOTALMESSAGES, COUNTER32, RONLY, bgpPeerTable,
3, {3, 1, 13}},
{BGPPEERLASTERROR, OCTET_STRING, RONLY, bgpPeerTable,
3, {3, 1, 14}},
{BGPPEERFSMESTABLISHEDTRANSITIONS, COUNTER32, RONLY, bgpPeerTable,
3, {3, 1, 15}},
{BGPPEERFSMESTABLISHEDTIME, GAUGE32, RONLY, bgpPeerTable,
3, {3, 1, 16}},
{BGPPEERCONNECTRETRYINTERVAL, INTEGER, RWRITE, bgpPeerTable,
3, {3, 1, 17}},
{BGPPEERHOLDTIME, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 18}},
{BGPPEERKEEPALIVE, INTEGER, RONLY, bgpPeerTable,
3, {3, 1, 19}},
{BGPPEERHOLDTIMECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
3, {3, 1, 20}},
{BGPPEERKEEPALIVECONFIGURED, INTEGER, RWRITE, bgpPeerTable,
3, {3, 1, 21}},
{BGPPEERMINROUTEADVERTISEMENTINTERVAL, INTEGER, RWRITE, bgpPeerTable,
3, {3, 1, 23}},
{BGPPEERINUPDATEELAPSEDTIME, GAUGE32, RONLY, bgpPeerTable,
3, {3, 1, 24}},
/* BGP identifier. */
{BGPIDENTIFIER, IPADDRESS, RONLY, bgpIdentifier,
1, {4}},
/* BGP received path attribute table. */
{BGPPATHATTRPEER, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 1}},
{BGPPATHATTRDESTNETWORK, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 2}},
{BGPPATHATTRORIGIN, INTEGER, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 3}},
{BGPPATHATTRASPATH, OCTET_STRING, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 4}},
{BGPPATHATTRNEXTHOP, IPADDRESS, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 5}},
{BGPPATHATTRINTERASMETRIC, INTEGER32, RONLY, bgpRcvdPathAttrTable,
3, {5, 1, 6}},
/* BGP-4 received path attribute table. */
{BGP4PATHATTRPEER, IPADDRESS, RONLY, bgp4PathAttrTable,
3, {6, 1, 1}},
{BGP4PATHATTRIPADDRPREFIXLEN, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 2}},
{BGP4PATHATTRIPADDRPREFIX, IPADDRESS, RONLY, bgp4PathAttrTable,
3, {6, 1, 3}},
{BGP4PATHATTRORIGIN, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 4}},
{BGP4PATHATTRASPATHSEGMENT, OCTET_STRING, RONLY, bgp4PathAttrTable,
3, {6, 1, 5}},
{BGP4PATHATTRNEXTHOP, IPADDRESS, RONLY, bgp4PathAttrTable,
3, {6, 1, 6}},
{BGP4PATHATTRMULTIEXITDISC, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 7}},
{BGP4PATHATTRLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 8}},
{BGP4PATHATTRATOMICAGGREGATE, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 9}},
{BGP4PATHATTRAGGREGATORAS, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 10}},
{BGP4PATHATTRAGGREGATORADDR, IPADDRESS, RONLY, bgp4PathAttrTable,
3, {6, 1, 11}},
{BGP4PATHATTRCALCLOCALPREF, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 12}},
{BGP4PATHATTRBEST, INTEGER, RONLY, bgp4PathAttrTable,
3, {6, 1, 13}},
{BGP4PATHATTRUNKNOWN, OCTET_STRING, RONLY, bgp4PathAttrTable,
3, {6, 1, 14}},
};
static u_char *
bgpVersion (struct variable *v, oid name[], size_t *length, int exact,
size_t *var_len, WriteMethod **write_method)
{
static u_char version;
if (smux_header_generic(v, name, length, exact, var_len, write_method)
== MATCH_FAILED)
return NULL;
/* Retrun BGP version. Zebra bgpd only support version 4. */
version = (0x80 >> (BGP_VERSION_4 - 1));
/* Return octet string length 1. */
*var_len = 1;
return (u_char *)&version;
}
static u_char *
bgpLocalAs (struct variable *v, oid name[], size_t *length,
int exact, size_t *var_len, WriteMethod **write_method)
{
struct bgp *bgp;
if (smux_header_generic(v, name, length, exact, var_len, write_method)
== MATCH_FAILED)
return NULL;
/* Get BGP structure. */
bgp = bgp_get_default ();
if (! bgp)
return NULL;
return SNMP_INTEGER (bgp->as);
}
static struct peer *
peer_lookup_addr_ipv4 (struct in_addr *src)
{
struct bgp *bgp;
struct peer *peer;
struct listnode *node;
struct in_addr addr;
int ret;
bgp = bgp_get_default ();
if (! bgp)
return NULL;
for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
{
ret = inet_pton (AF_INET, peer->host, &addr);
if (ret > 0)
{
if (IPV4_ADDR_SAME (&addr, src))
return peer;
}
}
return NULL;
}
static struct peer *
bgp_peer_lookup_next (struct in_addr *src)
{
struct bgp *bgp;
struct peer *peer;
struct listnode *node;
struct in_addr *p;
union sockunion su;
int ret;
sockunion_init (&su);
bgp = bgp_get_default ();
if (! bgp)
return NULL;
for (ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
{
ret = inet_pton (AF_INET, peer->host, &su.sin.sin_addr);
if (ret > 0)
{
p = &su.sin.sin_addr;
if (ntohl (p->s_addr) > ntohl (src->s_addr))
{
src->s_addr = p->s_addr;
return peer;
}
}
}
return NULL;
}
/* 1.3.6.1.2.1.15.3.1.x = 10 */
#define PEERTAB_NAMELEN 10
static struct peer *
bgpPeerTable_lookup (struct variable *v, oid name[], size_t *length,
struct in_addr *addr, int exact)
{
struct peer *peer = NULL;
size_t namelen = v ? v->namelen : PEERTAB_NAMELEN;
int len;
if (exact)
{
/* Check the length. */
if (*length - namelen != sizeof (struct in_addr))
return NULL;
oid2in_addr (name + namelen, IN_ADDR_SIZE, addr);
peer = peer_lookup_addr_ipv4 (addr);
return peer;
}
else
{
len = *length - namelen;
if (len > 4) len = 4;
oid2in_addr (name + namelen, len, addr);
peer = bgp_peer_lookup_next (addr);
if (peer == NULL)
return NULL;
oid_copy_addr (name + namelen, addr, sizeof (struct in_addr));
*length = sizeof (struct in_addr) + namelen;
return peer;
}
return NULL;
}
/* BGP write methods. */
static int
write_bgpPeerTable (int action, u_char *var_val,
u_char var_val_type, size_t var_val_len,
u_char *statP, oid *name, size_t length)
{
struct in_addr addr;
struct peer *peer;
long intval;
if (var_val_type != ASN_INTEGER)
{
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len != sizeof (long))
{
return SNMP_ERR_WRONGLENGTH;
}
intval = *(long *)var_val;
memset (&addr, 0, sizeof (struct in_addr));
peer = bgpPeerTable_lookup (NULL, name, &length, &addr, 1);
if (! peer)
return SNMP_ERR_NOSUCHNAME;
if (action != SNMP_MSG_INTERNAL_SET_COMMIT)
return SNMP_ERR_NOERROR;
zlog_info ("%s: SNMP write .%ld = %ld",
peer->host, (long)name[PEERTAB_NAMELEN - 1], intval);
switch (name[PEERTAB_NAMELEN - 1])
{
case BGPPEERADMINSTATUS:
#define BGP_PeerAdmin_stop 1
#define BGP_PeerAdmin_start 2
/* When the peer is established, */
if (intval == BGP_PeerAdmin_stop)
BGP_EVENT_ADD (peer, BGP_Stop);
else if (intval == BGP_PeerAdmin_start)
; /* Do nothing. */
else
return SNMP_ERR_NOSUCHNAME;
break;
case BGPPEERCONNECTRETRYINTERVAL:
SET_FLAG (peer->config, PEER_CONFIG_CONNECT);
peer->connect = intval;
peer->v_connect = intval;
break;
case BGPPEERHOLDTIMECONFIGURED:
SET_FLAG (peer->config, PEER_CONFIG_TIMER);
peer->holdtime = intval;
peer->v_holdtime = intval;
break;
case BGPPEERKEEPALIVECONFIGURED:
SET_FLAG (peer->config, PEER_CONFIG_TIMER);
peer->keepalive = intval;
peer->v_keepalive = intval;
break;
case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
peer->v_routeadv = intval;
break;
}
return SNMP_ERR_NOERROR;
}
static u_char *
bgpPeerTable (struct variable *v, oid name[], size_t *length,
int exact, size_t *var_len, WriteMethod **write_method)
{
static struct in_addr addr;
struct peer *peer;
if (smux_header_table(v, name, length, exact, var_len, write_method)
== MATCH_FAILED)
return NULL;
memset (&addr, 0, sizeof (struct in_addr));
peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
if (! peer)
return NULL;
switch (v->magic)
{
case BGPPEERIDENTIFIER:
return SNMP_IPADDRESS (peer->remote_id);
break;
case BGPPEERSTATE:
return SNMP_INTEGER (peer->status);
break;
case BGPPEERADMINSTATUS:
*write_method = write_bgpPeerTable;
#define BGP_PeerAdmin_stop 1
#define BGP_PeerAdmin_start 2
if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN))
return SNMP_INTEGER (BGP_PeerAdmin_stop);
else
return SNMP_INTEGER (BGP_PeerAdmin_start);
break;
case BGPPEERNEGOTIATEDVERSION:
return SNMP_INTEGER (BGP_VERSION_4);
break;
case BGPPEERLOCALADDR:
if (peer->su_local)
return SNMP_IPADDRESS (peer->su_local->sin.sin_addr);
else
return SNMP_IPADDRESS (bgp_empty_addr);
break;
case BGPPEERLOCALPORT:
if (peer->su_local)
return SNMP_INTEGER (ntohs (peer->su_local->sin.sin_port));
else
return SNMP_INTEGER (0);
break;
case BGPPEERREMOTEADDR:
if (peer->su_remote)
return SNMP_IPADDRESS (peer->su_remote->sin.sin_addr);
else
return SNMP_IPADDRESS (bgp_empty_addr);
break;
case BGPPEERREMOTEPORT:
if (peer->su_remote)
return SNMP_INTEGER (ntohs (peer->su_remote->sin.sin_port));
else
return SNMP_INTEGER (0);
break;
case BGPPEERREMOTEAS:
return SNMP_INTEGER (peer->as);
break;
case BGPPEERINUPDATES:
return SNMP_INTEGER (peer->update_in);
break;
case BGPPEEROUTUPDATES:
return SNMP_INTEGER (peer->update_out);
break;
case BGPPEERINTOTALMESSAGES:
return SNMP_INTEGER (peer->open_in + peer->update_in
+ peer->keepalive_in + peer->notify_in
+ peer->refresh_in + peer->dynamic_cap_in);
break;
case BGPPEEROUTTOTALMESSAGES:
return SNMP_INTEGER (peer->open_out + peer->update_out
+ peer->keepalive_out + peer->notify_out
+ peer->refresh_out + peer->dynamic_cap_out);
break;
case BGPPEERLASTERROR:
{
static u_char lasterror[2];
lasterror[0] = peer->notify.code;
lasterror[1] = peer->notify.subcode;
*var_len = 2;
return (u_char *)&lasterror;
}
break;
case BGPPEERFSMESTABLISHEDTRANSITIONS:
return SNMP_INTEGER (peer->established);
break;
case BGPPEERFSMESTABLISHEDTIME:
if (peer->uptime == 0)
return SNMP_INTEGER (0);
else
return SNMP_INTEGER (bgp_clock () - peer->uptime);
break;
case BGPPEERCONNECTRETRYINTERVAL:
*write_method = write_bgpPeerTable;
return SNMP_INTEGER (peer->v_connect);
break;
case BGPPEERHOLDTIME:
return SNMP_INTEGER (peer->v_holdtime);
break;
case BGPPEERKEEPALIVE:
return SNMP_INTEGER (peer->v_keepalive);
break;
case BGPPEERHOLDTIMECONFIGURED:
*write_method = write_bgpPeerTable;
if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
return SNMP_INTEGER (peer->holdtime);
else
return SNMP_INTEGER (peer->v_holdtime);
break;
case BGPPEERKEEPALIVECONFIGURED:
*write_method = write_bgpPeerTable;
if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
return SNMP_INTEGER (peer->keepalive);
else
return SNMP_INTEGER (peer->v_keepalive);
break;
case BGPPEERMINROUTEADVERTISEMENTINTERVAL:
*write_method = write_bgpPeerTable;
return SNMP_INTEGER (peer->v_routeadv);
break;
case BGPPEERINUPDATEELAPSEDTIME:
if (peer->update_time == 0)
return SNMP_INTEGER (0);
else
return SNMP_INTEGER (bgp_clock () - peer->update_time);
break;
default:
return NULL;
break;
}
return NULL;
}
static u_char *
bgpIdentifier (struct variable *v, oid name[], size_t *length,
int exact, size_t *var_len, WriteMethod **write_method)
{
struct bgp *bgp;
if (smux_header_generic(v, name, length, exact, var_len, write_method)
== MATCH_FAILED)
return NULL;
bgp = bgp_get_default ();
if (!bgp)
return NULL;
return SNMP_IPADDRESS (bgp->router_id);
}
static u_char *
bgpRcvdPathAttrTable (struct variable *v, oid name[], size_t *length,
int exact, size_t *var_len, WriteMethod **write_method)
{
/* Received Path Attribute Table. This table contains, one entry
per path to a network, path attributes received from all peers
running BGP version 3 or less. This table is obsolete, having
been replaced in functionality with the bgp4PathAttrTable. */
return NULL;
}
static struct bgp_info *
bgp4PathAttrLookup (struct variable *v, oid name[], size_t *length,
struct bgp *bgp, struct prefix_ipv4 *addr, int exact)
{
oid *offset;
int offsetlen;
struct bgp_info *binfo;
struct bgp_info *min;
struct bgp_node *rn;
union sockunion su;
unsigned int len;
struct in_addr paddr;
sockunion_init (&su);
#define BGP_PATHATTR_ENTRY_OFFSET \
(IN_ADDR_SIZE + 1 + IN_ADDR_SIZE)
if (exact)
{
if (*length - v->namelen != BGP_PATHATTR_ENTRY_OFFSET)
return NULL;
/* Set OID offset for prefix. */
offset = name + v->namelen;
oid2in_addr (offset, IN_ADDR_SIZE, &addr->prefix);
offset += IN_ADDR_SIZE;
/* Prefix length. */
addr->prefixlen = *offset;
offset++;
/* Peer address. */
su.sin.sin_family = AF_INET;
oid2in_addr (offset, IN_ADDR_SIZE, &su.sin.sin_addr);
/* Lookup node. */
rn = bgp_node_lookup (bgp->rib[AFI_IP][SAFI_UNICAST],
(struct prefix *) addr);
if (rn)
{
bgp_unlock_node (rn);
for (binfo = rn->info; binfo; binfo = binfo->next)
if (sockunion_same (&binfo->peer->su, &su))
return binfo;
}
}
else
{
offset = name + v->namelen;
offsetlen = *length - v->namelen;
len = offsetlen;
if (offsetlen == 0)
rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_UNICAST]);
else
{
if (len > IN_ADDR_SIZE)
len = IN_ADDR_SIZE;
oid2in_addr (offset, len, &addr->prefix);
offset += IN_ADDR_SIZE;
offsetlen -= IN_ADDR_SIZE;
if (offsetlen > 0)
addr->prefixlen = *offset;
else
addr->prefixlen = len * 8;
rn = bgp_node_get (bgp->rib[AFI_IP][SAFI_UNICAST],
(struct prefix *) addr);
offset++;
offsetlen--;
}
if (offsetlen > 0)
{
len = offsetlen;
if (len > IN_ADDR_SIZE)
len = IN_ADDR_SIZE;
oid2in_addr (offset, len, &paddr);
}
else
paddr.s_addr = 0;
if (! rn)
return NULL;
do
{
min = NULL;
for (binfo = rn->info; binfo; binfo = binfo->next)
{
if (binfo->peer->su.sin.sin_family == AF_INET
&& ntohl (paddr.s_addr)
< ntohl (binfo->peer->su.sin.sin_addr.s_addr))
{
if (min)
{
if (ntohl (binfo->peer->su.sin.sin_addr.s_addr)
< ntohl (min->peer->su.sin.sin_addr.s_addr))
min = binfo;
}
else
min = binfo;
}
}
if (min)
{
*length = v->namelen + BGP_PATHATTR_ENTRY_OFFSET;
offset = name + v->namelen;
oid_copy_addr (offset, &rn->p.u.prefix4, IN_ADDR_SIZE);
offset += IN_ADDR_SIZE;
*offset = rn->p.prefixlen;
offset++;
oid_copy_addr (offset, &min->peer->su.sin.sin_addr,
IN_ADDR_SIZE);
addr->prefix = rn->p.u.prefix4;
addr->prefixlen = rn->p.prefixlen;
bgp_unlock_node (rn);
return min;
}
paddr.s_addr = 0;
}
while ((rn = bgp_route_next (rn)) != NULL);
}
return NULL;
}
static u_char *
bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
int exact, size_t *var_len, WriteMethod **write_method)
{
struct bgp *bgp;
struct bgp_info *binfo;
struct prefix_ipv4 addr;
bgp = bgp_get_default ();
if (! bgp)
return NULL;
if (smux_header_table(v, name, length, exact, var_len, write_method)
== MATCH_FAILED)
return NULL;
memset (&addr, 0, sizeof (struct prefix_ipv4));
binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
if (! binfo)
return NULL;
switch (v->magic)
{
case BGP4PATHATTRPEER: /* 1 */
return SNMP_IPADDRESS (binfo->peer->su.sin.sin_addr);
break;
case BGP4PATHATTRIPADDRPREFIXLEN: /* 2 */
return SNMP_INTEGER (addr.prefixlen);
break;
case BGP4PATHATTRIPADDRPREFIX: /* 3 */
return SNMP_IPADDRESS (addr.prefix);
break;
case BGP4PATHATTRORIGIN: /* 4 */
return SNMP_INTEGER (binfo->attr->origin);
break;
case BGP4PATHATTRASPATHSEGMENT: /* 5 */
return aspath_snmp_pathseg (binfo->attr->aspath, var_len);
break;
case BGP4PATHATTRNEXTHOP: /* 6 */
return SNMP_IPADDRESS (binfo->attr->nexthop);
break;
case BGP4PATHATTRMULTIEXITDISC: /* 7 */
return SNMP_INTEGER (binfo->attr->med);
break;
case BGP4PATHATTRLOCALPREF: /* 8 */
return SNMP_INTEGER (binfo->attr->local_pref);
break;
case BGP4PATHATTRATOMICAGGREGATE: /* 9 */
return SNMP_INTEGER (1);
break;
case BGP4PATHATTRAGGREGATORAS: /* 10 */
if (binfo->attr->extra)
return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
else
return SNMP_INTEGER (0);
break;
case BGP4PATHATTRAGGREGATORADDR: /* 11 */
if (binfo->attr->extra)
return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
else
return SNMP_INTEGER (0);
break;
case BGP4PATHATTRCALCLOCALPREF: /* 12 */
return SNMP_INTEGER (-1);
break;
case BGP4PATHATTRBEST: /* 13 */
#define BGP4_PathAttrBest_false 1
#define BGP4_PathAttrBest_true 2
if (CHECK_FLAG (binfo->flags, BGP_INFO_SELECTED))
return SNMP_INTEGER (BGP4_PathAttrBest_true);
else
return SNMP_INTEGER (BGP4_PathAttrBest_false);
break;
case BGP4PATHATTRUNKNOWN: /* 14 */
*var_len = 0;
return NULL;
break;
}
return NULL;
}
/* BGP Traps. */
struct trap_object bgpTrapList[] =
{
{3, {3, 1, BGPPEERLASTERROR}},
{3, {3, 1, BGPPEERSTATE}}
};
void
bgpTrapEstablished (struct peer *peer)
{
int ret;
struct in_addr addr;
oid index[sizeof (oid) * IN_ADDR_SIZE];
ret = inet_aton (peer->host, &addr);
if (ret == 0)
return;
oid_copy_addr (index, &addr, IN_ADDR_SIZE);
smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
bgp_oid, sizeof bgp_oid / sizeof (oid),
index, IN_ADDR_SIZE,
bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
BGPESTABLISHED);
}
void
bgpTrapBackwardTransition (struct peer *peer)
{
int ret;
struct in_addr addr;
oid index[sizeof (oid) * IN_ADDR_SIZE];
ret = inet_aton (peer->host, &addr);
if (ret == 0)
return;
oid_copy_addr (index, &addr, IN_ADDR_SIZE);
smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
bgp_oid, sizeof bgp_oid / sizeof (oid),
index, IN_ADDR_SIZE,
bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
BGPBACKWARDTRANSITION);
}
void
bgp_snmp_init (void)
{
smux_init (bm->master);
REGISTER_MIB("mibII/bgp", bgp_variables, variable, bgp_oid);
}
#endif /* HAVE_SNMP */
|