summaryrefslogtreecommitdiffstats
path: root/ldpd/ldp_snmp.c
blob: dfc7d145fedffe391de2fa24344d2cc983447ef9 (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
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
/*
 * LDP SNMP support
 * Copyright (C) 2020 Volta Networks, Inc.
 *
 * This program 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 of the License, or (at your option)
 * any later version.
 *
 * This program 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
 */

/*
 * This is minimal read-only implementations providing
 * mplsLdpModuleReadOnlyCompliance as described in RFC 3815.
 */

#include <zebra.h>

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

#include "vrf.h"
#include "if.h"
#include "log.h"
#include "prefix.h"
#include "table.h"
#include "command.h"
#include "memory.h"
#include "smux.h"
#include "libfrr.h"
#include "lib/version.h"
#include "ldpd.h"
#include "ldpe.h"

/* SNMP value hack. */
#define COUNTER32 ASN_COUNTER
#define INTEGER ASN_INTEGER
#define UNSIGNED32 ASN_GAUGE
#define TIMESTAMP ASN_TIMETICKS
#define TIMETICKS ASN_TIMETICKS
#define STRING ASN_OCTET_STR
#define IPADDRESS ASN_IPADDRESS

#define LDP_LSRID_IDX_LEN 		6
#define LDP_ENTITY_IDX_LEN		1
#define LDP_ADJACENCY_IDX_LEN		1

/* MPLS-LDP-STD-MIB. */
#define MPLS_LDP_STD_MIB 1, 3, 6, 1, 2, 1, 10, 166, 4

#define MPLS_LDP_LSR_ID				0
#define MPLS_LDP_LSR_LOOP_DETECTION_CAPABLE	0
#define MPLS_LDP_ENTITY_LAST_CHANGE		0
#define MPLS_LDP_ENTITY_INDEX_NEXT		0

/* Declare static local variables for convenience. */
SNMP_LOCAL_VARIABLES

/* LDP-MIB instances. */
static oid ldp_oid[] = {MPLS_LDP_STD_MIB};
static oid ldp_trap_oid[] = {MPLS_LDP_STD_MIB, 0};

static uint8_t snmp_ldp_rtrid[6] =  {0, 0, 0, 0, 0};

#define LDP_DEFAULT_ENTITY_INDEX				1

#define MPLSLDPLSRLOOPDETECTIONCAPABLE_NONE              	1
#define MPLSLDPLSRLOOPDETECTIONCAPABLE_OTHER             	2
#define MPLSLDPLSRLOOPDETECTIONCAPABLE_HOPCOUNT          	3
#define MPLSLDPLSRLOOPDETECTIONCAPABLE_PATHVECTOR		4
#define MPLSLDPLSRLOOPDETECTIONCAPABLE_HOPCOUNTANDPATHVECTOR	5

/* MPLS LDP mplsLdpHelloAdjacencyTable. */
#define MPLSLDPHELLOADJACENCYINDEX          1
#define MPLSLDPHELLOADJACENCYHOLDTIMEREM    2
#define MPLSLDPHELLOADJACENCYHOLDTIME       3
#define MPLSLDPHELLOADJACENCYTYPE           4

/* enums for column mplsLdpHelloAdjacencyType */
#define MPLSLDPHELLOADJACENCYTYPE_LINK               1
#define MPLSLDPHELLOADJACENCYTYPE_TARGETED           2

#define MPLSLDPPEERTRANSPORTADDRTYPE_UNKNOWN          0
#define MPLSLDPPEERTRANSPORTADDRTYPE_IPV4             1
#define MPLSLDPPEERTRANSPORTADDRTYPE_IPV6             2
#define MPLSLDPPEERTRANSPORTADDRTYPE_IPV4Z            3
#define MPLSLDPPEERTRANSPORTADDRTYPE_IPV6Z            4
#define MPLSLDPPEERTRANSPORTADDRTYPE_DNS              16

#define DOWNSTREAMONDEMAND         1
#define DOWNSTREAMUNSOLICITED      2

#define CONSERVATIVERETENTION      1
#define LIBERALRETENTION           2

#define TRANSPORTADDRINTERFACE     1
#define TRANSPORTADDRLOOPBACK      2

#define LABELTYPEGENERIC           1

#define STORAGETYPENONVOLATILE     3

#define ROWSTATUSACTIVE            4

#define ADMINSTATUSENABLED         1

#define OPERSTATUSENABLED          2

/* MPLS LDP mplsLdpPeerTable */
#define MPLSLDPPEERLDPID                             1
#define MPLSLDPPEERLABELDISTMETHOD                   2
#define MPLSLDPPEERPATHVECTORLIMIT                   3
#define MPLSLDPPEERTRANSPORTADDRTYPE                 4
#define MPLSLDPPEERTRANSPORTADDR                     5

#define MPLSLDPSESSIONROLE_UNKNOWN                   1
#define MPLSLDPSESSIONROLE_ACTIVE                    2
#define MPLSLDPSESSIONROLE_PASSIVE                   3

#define MPLSLDPSESSIONSTATE_NONEXISTENT              1
#define MPLSLDPSESSIONSTATE_INITIALIZED              2
#define MPLSLDPSESSIONSTATE_OPENREC                  3
#define MPLSLDPSESSIONSTATE_OPENSENT                 4
#define MPLSLDPSESSIONSTATE_OPERATIONAL              5

/* MPLS LDP mplsLdpSessionTable */
#define MPLSLDPSESSIONSTATELASTCHANGE                1
#define MPLSLDPSESSIONSTATE                          2
#define MPLSLDPSESSIONROLE                           3
#define MPLSLDPSESSIONPROTOCOLVERSION                4
#define MPLSLDPSESSIONKEEPALIVEHOLDTIMEREM           5
#define MPLSLDPSESSIONKEEPALIVETIME                  6
#define MPLSLDPSESSIONMAXPDULENGTH                   7
#define MPLSLDPSESSIONDISCONTINUITYTIME              8

/* MPLS LDP mplsLdpEntityTable */
#define MPLSLDPENTITYLDPID                1
#define MPLSLDPENTITYINDEX                2
#define MPLSLDPENTITYPROTOCOLVERSION      3
#define MPLSLDPENTITYADMINSTATUS          4
#define MPLSLDPENTITYOPERSTATUS           5
#define MPLSLDPENTITYTCPPORT              6
#define MPLSLDPENTITYUDPDSCPORT           7
#define MPLSLDPENTITYMAXPDULENGTH         8
#define MPLSLDPENTITYKEEPALIVEHOLDTIMER   9
#define MPLSLDPENTITYHELLOHOLDTIMER       10
#define MPLSLDPENTITYINITSESSIONTHRESHOLD 11
#define MPLSLDPENTITYLABELDISTMETHOD      12
#define MPLSLDPENTITYLABELRETENTIONMODE   13
#define MPLSLDPENTITYPATHVECTORLIMIT      14
#define MPLSLDPENTITYHOPCOUNTLIMIT        15
#define MPLSLDPENTITYTRANSPORTADDRKIND    16
#define MPLSLDPENTITYTARGETPEER           17
#define MPLSLDPENTITYTARGETPEERADDRTYPE   18
#define MPLSLDPENTITYTARGETPEERADDR       19
#define MPLSLDPENTITYLABELTYPE            20
#define MPLSLDPENTITYDISCONTINUITYTIME    21
#define MPLSLDPENTITYSTORAGETYPE          22
#define MPLSLDPENTITYROWSTATUS            23

/* MPLS LDP mplsLdpEntityStatsTable */
#define MPLSLDPENTITYSTATSSESSIONATTEMPTS        1
#define MPLSLDPENTITYSTATSSESSIONREJHELLO        2
#define MPLSLDPENTITYSTATSSESSIONREJAD           3
#define MPLSLDPENTITYSTATSSESSIONREJMAXPDU       4
#define MPLSLDPENTITYSTATSSESSIONREJLR           5
#define MPLSLDPENTITYSTATSBADLDPID               6
#define MPLSLDPENTITYSTATSBADPDULENGTH           7
#define MPLSLDPENTITYSTATSBADMSGLENGTH           8
#define MPLSLDPENTITYSTATSBADTLVLENGTH           9
#define MPLSLDPENTITYSTATSMALFORMEDTLV           10
#define MPLSLDPENTITYSTATSKEEPALIVEEXP           11
#define MPLSLDPENTITYSTATSSHUTDOWNRCVNOTIFY      12
#define MPLSLDPENTITYSTATSSHUTDOWNSENTNOTIFY     13

#define MPLSLDPSESSIONSTATSUNKNOWNMESTYPEERRORS     1
#define MPLSLDPSESSIONSTATSUNKNOWNTLVERRORS         2

static uint8_t *ldpLsrId(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
        if (smux_header_generic(v, name, length, exact, var_len, write_method)
            == MATCH_FAILED)
                return NULL;

	*var_len = 4;
	return (uint8_t *)&leconf->rtr_id.s_addr;
}

static uint8_t *ldpLoopDetectCap(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
        if (smux_header_generic(v, name, length, exact, var_len, write_method)
            == MATCH_FAILED)
                return NULL;

        return SNMP_INTEGER(MPLSLDPLSRLOOPDETECTIONCAPABLE_NONE);
}

extern uint32_t ldp_start_time;
static uint8_t *ldpEntityLastChange(struct variable *v, oid name[],
				size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	if (smux_header_generic(v, name, length, exact, var_len, write_method)
            == MATCH_FAILED)
                return NULL;

	*var_len = sizeof(time_t);
	return (uint8_t *) &(leconf->config_change_time);

}

static uint8_t *ldpEntityIndexNext(struct variable *v, oid name[],
				   size_t *length,int exact, size_t *var_len,
				   WriteMethod **write_method)
{
        if (smux_header_generic(v, name, length, exact, var_len, write_method)
            == MATCH_FAILED)
                return NULL;

	return SNMP_INTEGER(0);
}

#define LDP_ENTITY_TOTAL_LEN 21
#define LDP_ENTITY_MAX_IDX_LEN 6

static struct ldpd_af_conf *ldpEntityTable_lookup(struct variable *v, oid *name,
						  size_t *length, int exact,
						  uint32_t *index)
{
	int len;
	struct ldpd_af_conf *af_v4, *af_v6;

	af_v4 = &leconf->ipv4;
	af_v6 = &leconf->ipv6;

	if (exact) {
		if (*length != LDP_ENTITY_TOTAL_LEN)
			return NULL;

		if (leconf->trans_pref == DUAL_STACK_LDPOV6 &&
		    af_v6->flags & F_LDPD_AF_ENABLED) {
			*index = 2;
			return af_v6;
		} else {
			*index = 1;
			return af_v4;
		}
	} else {
		/* only support one router id so can just skip */
		len = *length - v->namelen - LDP_ENTITY_MAX_IDX_LEN;
		if (len <= 0) {
			if (leconf->trans_pref == DUAL_STACK_LDPOV6 &&
			    af_v6->flags & F_LDPD_AF_ENABLED) {
				*index = 2;
				return af_v6;
			} else {
				*index = 1;
				return af_v4;
			}
		}
	}
	return NULL;
}

static uint8_t *ldpEntityTable(struct variable *v, oid name[], size_t *length,
			       int exact, size_t *var_len,
			       WriteMethod **write_method)
{
	struct ldpd_af_conf *af;
	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t index = 0;

	*write_method = NULL;

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

	af = ldpEntityTable_lookup(v, name, length, exact, &index);
	if (af == NULL)
	    return NULL;

	if (!exact) {
		entityLdpId.s_addr = ldp_rtr_id_get(leconf);

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		*length = LDP_ENTITY_TOTAL_LEN;
		oid_copy_in_addr(name + v->namelen, &entityLdpId);
		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = LDP_DEFAULT_ENTITY_INDEX;
	}

	/* Return the current value of the variable */
	switch (v->magic) {
	case MPLSLDPENTITYLDPID:
		*var_len =  6;
		memcpy (snmp_ldp_rtrid, &entityLdpId, IN_ADDR_SIZE);
		return (uint8_t *)snmp_ldp_rtrid;
	case MPLSLDPENTITYINDEX:
		return SNMP_INTEGER(LDP_DEFAULT_ENTITY_INDEX);
	case MPLSLDPENTITYPROTOCOLVERSION:
		return SNMP_INTEGER(LDP_VERSION);
	case MPLSLDPENTITYADMINSTATUS:
		return SNMP_INTEGER(ADMINSTATUSENABLED);
	case MPLSLDPENTITYOPERSTATUS:
		return SNMP_INTEGER(OPERSTATUSENABLED);
	case MPLSLDPENTITYTCPPORT:
		return SNMP_INTEGER(LDP_PORT);
	case MPLSLDPENTITYUDPDSCPORT:
		return SNMP_INTEGER(LDP_PORT);
	case MPLSLDPENTITYMAXPDULENGTH:
		return SNMP_INTEGER(LDP_MAX_LEN);
	case MPLSLDPENTITYKEEPALIVEHOLDTIMER:
		return SNMP_INTEGER(af->keepalive);
	case MPLSLDPENTITYHELLOHOLDTIMER:
		return SNMP_INTEGER(af->lhello_holdtime);
	case MPLSLDPENTITYINITSESSIONTHRESHOLD:
		return SNMP_INTEGER(0); /* not supported */
	case MPLSLDPENTITYLABELDISTMETHOD:
		return SNMP_INTEGER(DOWNSTREAMUNSOLICITED);
	case MPLSLDPENTITYLABELRETENTIONMODE:
		return SNMP_INTEGER(LIBERALRETENTION);
	case MPLSLDPENTITYPATHVECTORLIMIT:
		return SNMP_INTEGER(0); /* not supported */
	case MPLSLDPENTITYHOPCOUNTLIMIT:
		return SNMP_INTEGER(0);
	case MPLSLDPENTITYTRANSPORTADDRKIND:
		return SNMP_INTEGER(TRANSPORTADDRLOOPBACK);
	case MPLSLDPENTITYTARGETPEER:
		return SNMP_INTEGER(1);
	case MPLSLDPENTITYTARGETPEERADDRTYPE:
		if (index == 1)
			return SNMP_INTEGER(MPLSLDPPEERTRANSPORTADDRTYPE_IPV4);
		else
			return SNMP_INTEGER(MPLSLDPPEERTRANSPORTADDRTYPE_IPV6);
	case MPLSLDPENTITYTARGETPEERADDR:
		if (index == 1) {
			*var_len = sizeof(af->trans_addr.v4);
			return ((uint8_t *)&af->trans_addr.v4);
		}else {
			*var_len = sizeof(af->trans_addr.v6);
			return ((uint8_t *)&af->trans_addr.v6);
		}
	case MPLSLDPENTITYLABELTYPE:
		return SNMP_INTEGER(LABELTYPEGENERIC);
	case MPLSLDPENTITYDISCONTINUITYTIME:
		return SNMP_INTEGER(0);
	case MPLSLDPENTITYSTORAGETYPE:
		return SNMP_INTEGER(STORAGETYPENONVOLATILE);
	case MPLSLDPENTITYROWSTATUS:
		return SNMP_INTEGER(ROWSTATUSACTIVE);
	default:
		return NULL;
	}

	return NULL;
}

static uint8_t *ldpEntityStatsTable(struct variable *v, oid name[],
				    size_t *length, int exact, size_t *var_len,
				    WriteMethod **write_method)
{
	struct in_addr entityLdpId = {.s_addr = 0};
	int len;

	*write_method = NULL;

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

	if (exact) {
		if (*length != LDP_ENTITY_TOTAL_LEN)
			return NULL;
	} else {
		len = *length - v->namelen - LDP_ENTITY_MAX_IDX_LEN;
		if (len > 0)
			return NULL;

		entityLdpId.s_addr = ldp_rtr_id_get(leconf);

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		*length = LDP_ENTITY_TOTAL_LEN;
		oid_copy_in_addr(name + v->namelen, &entityLdpId);
		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = LDP_DEFAULT_ENTITY_INDEX;
	}

	/* Return the current value of the variable */
	switch (v->magic) {
	case MPLSLDPENTITYSTATSSESSIONATTEMPTS:
		return SNMP_INTEGER(leconf->stats.session_attempts);
	case MPLSLDPENTITYSTATSSESSIONREJHELLO:
		return SNMP_INTEGER(leconf->stats.session_rejects_hello);
	case MPLSLDPENTITYSTATSSESSIONREJAD:
		return SNMP_INTEGER(leconf->stats.session_rejects_ad);
	case MPLSLDPENTITYSTATSSESSIONREJMAXPDU:
		return SNMP_INTEGER(leconf->stats.session_rejects_max_pdu);
	case MPLSLDPENTITYSTATSSESSIONREJLR:
		return SNMP_INTEGER(leconf->stats.session_rejects_lr);
	case MPLSLDPENTITYSTATSBADLDPID:
		return SNMP_INTEGER(leconf->stats.bad_ldp_id);
	case MPLSLDPENTITYSTATSBADPDULENGTH:
		return SNMP_INTEGER(leconf->stats.bad_pdu_len);
	case MPLSLDPENTITYSTATSBADMSGLENGTH:
		return SNMP_INTEGER(leconf->stats.bad_msg_len);
	case MPLSLDPENTITYSTATSBADTLVLENGTH:
		return SNMP_INTEGER(leconf->stats.bad_tlv_len);
	case MPLSLDPENTITYSTATSMALFORMEDTLV:
		return SNMP_INTEGER(leconf->stats.malformed_tlv);
	case MPLSLDPENTITYSTATSKEEPALIVEEXP:
		return SNMP_INTEGER(leconf->stats.keepalive_timer_exp);
	case MPLSLDPENTITYSTATSSHUTDOWNRCVNOTIFY:
		return SNMP_INTEGER(leconf->stats.shutdown_rcv_notify);
	case MPLSLDPENTITYSTATSSHUTDOWNSENTNOTIFY:
		return SNMP_INTEGER(leconf->stats.shutdown_send_notify);
	default:
		return NULL;
	}

	return NULL;
}

#define LDP_ADJACENCY_ENTRY_MAX_IDX_LEN	14

static void ldpHelloAdjacencyTable_oid_to_index(
	struct variable *v, oid name[],
	size_t *length,
	struct in_addr *entityLdpId,
	uint32_t *entityIndex,
	struct in_addr *peerLdpId,
	uint32_t *adjacencyIndex)
{
	oid *offset = name + v->namelen;
	int offsetlen = *length - v->namelen;
	int len = offsetlen;

	if (len > LDP_ADJACENCY_ENTRY_MAX_IDX_LEN)
		len = LDP_ADJACENCY_ENTRY_MAX_IDX_LEN;

	if (len >= LDP_LSRID_IDX_LEN)
                oid2in_addr(offset, sizeof(struct in_addr), entityLdpId);

	offset += LDP_LSRID_IDX_LEN;
	offsetlen -= LDP_LSRID_IDX_LEN;
	len = offsetlen;

	if (len > LDP_ENTITY_IDX_LEN)
		len = LDP_ENTITY_IDX_LEN;

	if (len >= LDP_ENTITY_IDX_LEN)
		*entityIndex = offset[0];

	offset += LDP_ENTITY_IDX_LEN;
	offsetlen -= LDP_ENTITY_IDX_LEN;
	len = offsetlen;

	if (len > LDP_LSRID_IDX_LEN)
		len = LDP_LSRID_IDX_LEN;

	if (len >= LDP_LSRID_IDX_LEN)
                oid2in_addr(offset, sizeof(struct in_addr), peerLdpId);

	offset += LDP_LSRID_IDX_LEN;
	offsetlen -= LDP_LSRID_IDX_LEN;
	len = offsetlen;

	if (len > LDP_ADJACENCY_IDX_LEN)
		len = LDP_ADJACENCY_IDX_LEN;

	if (len >= LDP_ADJACENCY_IDX_LEN)
		*adjacencyIndex = offset[0];
}

static struct adj *
nbr_get_adj_by_index(struct nbr *nbr, uint32_t adjacencyIndex)
{
	struct adj      *adj;
	uint32_t	i = 0;

	RB_FOREACH(adj, nbr_adj_head, &nbr->adj_tree)
		if (++i == adjacencyIndex)
			return adj;

	return NULL;
}

static struct ctl_adj *
ldpHelloAdjacencyTable_lookup_helper(
	struct in_addr *entityLdpId,
	uint32_t *entityIndex,
	struct in_addr *peerLdpId,
	uint32_t *adjacencyIndex)
{
	struct ctl_adj *ctl_adj = NULL;
        struct adj *adj = NULL;
	struct nbr *cur_nbr = nbr_find_ldpid(peerLdpId->s_addr);

	if (cur_nbr)
		/* If found nbr, then look to see if the
		 * adjacency exists
		 */
		adj = nbr_get_adj_by_index(cur_nbr, *adjacencyIndex);

	if (adj)
		ctl_adj = adj_to_ctl(adj);

	return ctl_adj;
}

static struct ctl_adj *
ldpHelloAdjacencyTable_next_helper(
	int first,
	struct in_addr *entityLdpId,
	uint32_t *entityIndex,
	struct in_addr *peerLdpId,
	uint32_t *adjacencyIndex)
{
	struct ctl_adj *ctl_adj = NULL;
	struct nbr *nbr = NULL;
        struct adj *adj = NULL;

	if (first)
		nbr = nbr_get_first_ldpid();
	else {
		struct nbr *cur_nbr = nbr_find_ldpid(peerLdpId->s_addr);
		if (cur_nbr)
			/* If found nbr, then look to see if the
			 * adjacency exists
			 */
			adj = nbr_get_adj_by_index(cur_nbr, *adjacencyIndex + 1);
		if (adj)
			*adjacencyIndex += 1;
		else
			nbr = nbr_get_next_ldpid(peerLdpId->s_addr);
	}

	if (!adj && nbr) {
		adj = RB_MIN(nbr_adj_head, &nbr->adj_tree);
		*adjacencyIndex = 1;
	}

	if (adj)
		ctl_adj = adj_to_ctl(adj);

	return ctl_adj;
}

#define HELLO_ADJ_MAX_IDX_LEN           14

static struct ctl_adj *
ldpHelloAdjacencyTable_lookup(struct variable *v, oid name[],
	size_t *length, int exact,
	struct in_addr *entityLdpId,
	uint32_t *entityIndex,
	struct in_addr *peerLdpId,
	uint32_t *adjacencyIndex)
{
	struct ctl_adj *hello_adj = NULL;

	if (exact) {
		if (*length < HELLO_ADJ_MAX_IDX_LEN)
			return NULL;

		ldpHelloAdjacencyTable_oid_to_index(
			v, name, length,
			entityLdpId, entityIndex, peerLdpId, adjacencyIndex);

                hello_adj = ldpHelloAdjacencyTable_lookup_helper(
			entityLdpId, entityIndex, peerLdpId, adjacencyIndex);
	} else {
		int first = 0;
		int offsetlen = *length - v->namelen;

		if (offsetlen < HELLO_ADJ_MAX_IDX_LEN)
			first = 1;

		ldpHelloAdjacencyTable_oid_to_index(
			v, name, length,
			entityLdpId, entityIndex, peerLdpId, adjacencyIndex);

                hello_adj = ldpHelloAdjacencyTable_next_helper(first,
			entityLdpId, entityIndex, peerLdpId, adjacencyIndex);

	}
	return hello_adj;
}

static uint8_t *ldpHelloAdjacencyTable(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t entityIndex = 0;
	struct in_addr peerLdpId = {.s_addr = 0};
	uint32_t adjacencyIndex = 0;

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

	struct ctl_adj *ctl_adj = ldpHelloAdjacencyTable_lookup(v, name,
		length, exact,
		&entityLdpId, &entityIndex, &peerLdpId, &adjacencyIndex);

	if (!ctl_adj)
		return NULL;

	if (!exact) {

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		struct in_addr entityLdpId = {.s_addr = 0};
		entityLdpId.s_addr = ldp_rtr_id_get(leconf);

		struct in_addr peerLdpId = ctl_adj->id;

		oid_copy_in_addr(name + v->namelen, &entityLdpId);
		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = LDP_DEFAULT_ENTITY_INDEX;
		oid_copy_in_addr(name + v->namelen + 7, &peerLdpId);
		name[v->namelen + 11] = 0;
		name[v->namelen + 12] = 0;
		name[v->namelen + 13] = adjacencyIndex;

		/* Set length */
		*length = v->namelen + HELLO_ADJ_MAX_IDX_LEN;
	}

	switch (v->magic) {
	case MPLSLDPHELLOADJACENCYINDEX:
		return SNMP_INTEGER(adjacencyIndex);
	case MPLSLDPHELLOADJACENCYHOLDTIMEREM:
		return SNMP_INTEGER(ctl_adj->holdtime_remaining);
	case MPLSLDPHELLOADJACENCYHOLDTIME:
		return SNMP_INTEGER(ctl_adj->holdtime);
	case MPLSLDPHELLOADJACENCYTYPE:
		if (ctl_adj->type == HELLO_LINK)
			return SNMP_INTEGER(MPLSLDPHELLOADJACENCYTYPE_LINK);
		return SNMP_INTEGER(MPLSLDPHELLOADJACENCYTYPE_TARGETED);
	default:
		return NULL;
	}

	return NULL;
}

#define LDP_LSRID_IDX_LEN 		6
#define LDP_ENTITY_IDX_LEN		1
#define LDP_PEER_ENTRY_MAX_IDX_LEN	13

static void ldpPeerTable_oid_to_index(
	struct variable *v, oid name[],
	size_t *length,
	struct in_addr *entityLdpId,
	uint32_t *entityIndex,
	struct in_addr *peerLdpId)
{
	oid *offset = name + v->namelen;
	int offsetlen = *length - v->namelen;
	int len = offsetlen;

	if (len > LDP_PEER_ENTRY_MAX_IDX_LEN)
		len = LDP_PEER_ENTRY_MAX_IDX_LEN;

	if (len >= LDP_LSRID_IDX_LEN)
                oid2in_addr(offset, sizeof(struct in_addr), entityLdpId);

	offset += LDP_LSRID_IDX_LEN;
	offsetlen -= LDP_LSRID_IDX_LEN;
	len = offsetlen;

	if (len > LDP_ENTITY_IDX_LEN)
		len = LDP_ENTITY_IDX_LEN;

	if (len >= LDP_ENTITY_IDX_LEN)
		*entityIndex = offset[0];

	offset += LDP_ENTITY_IDX_LEN;
	offsetlen -= LDP_ENTITY_IDX_LEN;
	len = offsetlen;

	if (len > LDP_LSRID_IDX_LEN)
		len = LDP_LSRID_IDX_LEN;

	if (len >= LDP_LSRID_IDX_LEN)
                oid2in_addr(offset, sizeof(struct in_addr), peerLdpId);
}

static struct ctl_nbr *
ldpPeerTable_lookup_next(int first,
	struct in_addr peerLdpId)
{
	struct nbr *nbr = NULL;
        struct ctl_nbr *ctl_nbr = NULL;;

	if (first)
		nbr = nbr_get_first_ldpid();
	else
		nbr = nbr_get_next_ldpid(peerLdpId.s_addr);

	if (nbr)
		ctl_nbr = nbr_to_ctl(nbr);

	return ctl_nbr;
}

static struct ctl_nbr *
ldpPeerTable_lookup(struct variable *v, oid name[],
			size_t *length, int exact,
			struct in_addr *entityLdpId,
			uint32_t *entityIndex,
			struct in_addr *peerLdpId)
{
	struct ctl_nbr *ctl_nbr = NULL;
	struct nbr *nbr = NULL;
	int first = 0;

	if (exact) {
		if (*length < (long unsigned int)v->namelen
		    + LDP_PEER_ENTRY_MAX_IDX_LEN)
			return NULL;

		ldpPeerTable_oid_to_index(
			v, name, length,
			entityLdpId, entityIndex, peerLdpId);

                nbr = nbr_find_ldpid(peerLdpId->s_addr);
		if (nbr)
			ctl_nbr = nbr_to_ctl(nbr);

		return ctl_nbr;
	} else {

		int offsetlen = *length - v->namelen;
		if (offsetlen < LDP_LSRID_IDX_LEN)
			first = 1;

		ldpPeerTable_oid_to_index(
			v, name, length,
			entityLdpId, entityIndex, peerLdpId);

                ctl_nbr = ldpPeerTable_lookup_next(first, *peerLdpId);
		return ctl_nbr;
	}
	return NULL;
}

static uint8_t *ldpPeerTable(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t entityIndex = 0;
	struct in_addr peerLdpId = {.s_addr = 0};
	struct ctl_nbr *ctl_nbr;


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

	ctl_nbr = ldpPeerTable_lookup(v, name, length, exact, &entityLdpId,
				      &entityIndex, &peerLdpId);

	if (!ctl_nbr)
		return NULL;

	if (!exact) {

		entityLdpId.s_addr = ldp_rtr_id_get(leconf);
		entityIndex = LDP_DEFAULT_ENTITY_INDEX;
		peerLdpId = ctl_nbr->id;

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		oid_copy_in_addr(name + v->namelen, &entityLdpId);

		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = entityIndex;
		oid_copy_in_addr(name + v->namelen + 7, &peerLdpId);
		name[v->namelen + 11] = 0;
		name[v->namelen + 12] = 0;

		/* Set length */
		*length = v->namelen + LDP_PEER_ENTRY_MAX_IDX_LEN;
	}

	switch (v->magic) {
	case MPLSLDPPEERLDPID:
		*var_len = 6;
		memcpy(snmp_ldp_rtrid, &ctl_nbr->id, IN_ADDR_SIZE);
		return snmp_ldp_rtrid;
	case MPLSLDPPEERLABELDISTMETHOD:
		return SNMP_INTEGER(DOWNSTREAMUNSOLICITED);
	case MPLSLDPPEERPATHVECTORLIMIT:
		return SNMP_INTEGER(0);
	case MPLSLDPPEERTRANSPORTADDRTYPE:
		if (ctl_nbr->af == AF_INET)
			return SNMP_INTEGER(MPLSLDPPEERTRANSPORTADDRTYPE_IPV4);
		else
			return SNMP_INTEGER(MPLSLDPPEERTRANSPORTADDRTYPE_IPV6);
	case MPLSLDPPEERTRANSPORTADDR:
		if (ctl_nbr->af == AF_INET) {
			*var_len = sizeof(ctl_nbr->raddr.v4);
			return ((uint8_t *)&ctl_nbr->raddr.v4);
		} else {
			*var_len = sizeof(ctl_nbr->raddr.v6);
			return ((uint8_t *)&ctl_nbr->raddr.v6);
		}
	default:
		return NULL;
	}

	return NULL;
}
static uint8_t *ldpSessionTable(struct variable *v, oid name[], size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t entityIndex = 0;
	struct in_addr peerLdpId = {.s_addr = 0};
	struct ctl_nbr *ctl_nbr;

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

	ctl_nbr = ldpPeerTable_lookup(v, name, length, exact, &entityLdpId,
				      &entityIndex, &peerLdpId);

	if (!ctl_nbr)
		return NULL;

	if (!exact) {
		entityLdpId.s_addr = ldp_rtr_id_get(leconf);
		entityIndex = LDP_DEFAULT_ENTITY_INDEX;
		peerLdpId = ctl_nbr->id;

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		oid_copy_in_addr(name + v->namelen, &entityLdpId);

		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = entityIndex;
		oid_copy_in_addr(name + v->namelen + 7, &peerLdpId);
		name[v->namelen + 11] = 0;
		name[v->namelen + 12] = 0;

		/* Set length */
                *length = v->namelen + LDP_PEER_ENTRY_MAX_IDX_LEN;
	}

	switch (v->magic) {
	case MPLSLDPSESSIONSTATELASTCHANGE:
		*var_len = sizeof(time_t);
		return (uint8_t *) &(ctl_nbr->uptime);
	case MPLSLDPSESSIONSTATE:
		switch (ctl_nbr->nbr_state) {
		case NBR_STA_INITIAL:
			return SNMP_INTEGER(MPLSLDPSESSIONSTATE_INITIALIZED);
		case NBR_STA_OPENREC:
			return SNMP_INTEGER(MPLSLDPSESSIONSTATE_OPENREC);
		case NBR_STA_OPENSENT:
			return SNMP_INTEGER(MPLSLDPSESSIONSTATE_OPENSENT);
		case NBR_STA_OPER:
			return SNMP_INTEGER(MPLSLDPSESSIONSTATE_OPERATIONAL);
		default:
			return SNMP_INTEGER(MPLSLDPSESSIONSTATE_NONEXISTENT);
		}
	case MPLSLDPSESSIONROLE:
		if (ldp_addrcmp(ctl_nbr->af, &ctl_nbr->laddr, &ctl_nbr->raddr)
		    > 0)
			return SNMP_INTEGER(MPLSLDPSESSIONROLE_ACTIVE);
		else
			return SNMP_INTEGER(MPLSLDPSESSIONROLE_PASSIVE);
	case MPLSLDPSESSIONPROTOCOLVERSION:
		return SNMP_INTEGER(LDP_VERSION);
	case MPLSLDPSESSIONKEEPALIVEHOLDTIMEREM:
		return SNMP_INTEGER(ctl_nbr->hold_time_remaining);
	case MPLSLDPSESSIONKEEPALIVETIME:
		return SNMP_INTEGER(ctl_nbr->holdtime);
	case MPLSLDPSESSIONMAXPDULENGTH:
		if (ctl_nbr->nbr_state == NBR_STA_OPER)
			return SNMP_INTEGER(ctl_nbr->max_pdu_len);
		else
			return SNMP_INTEGER(LDP_MAX_LEN);
	case MPLSLDPSESSIONDISCONTINUITYTIME:
		return SNMP_INTEGER(0); /* not supported */
	default:
		return NULL;
	}

	return NULL;
}

static uint8_t *ldpSessionStatsTable(struct variable *v, oid name[],
				size_t *length,
				int exact, size_t *var_len,
				WriteMethod **write_method)
{
	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t entityIndex = 0;
	struct in_addr peerLdpId = {.s_addr = 0};

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

	struct ctl_nbr *ctl_nbr = ldpPeerTable_lookup(v, name, length, exact,
		&entityLdpId, &entityIndex, &peerLdpId);

	if (!ctl_nbr)
		return NULL;

	if (!exact) {
		entityLdpId.s_addr = ldp_rtr_id_get(leconf);
		entityIndex = LDP_DEFAULT_ENTITY_INDEX;
		peerLdpId = ctl_nbr->id;

		/* Copy the name out */
		memcpy(name, v->name, v->namelen * sizeof(oid));

		/* Append index */
		oid_copy_in_addr(name + v->namelen, &entityLdpId);
		name[v->namelen + 4] = 0;
		name[v->namelen + 5] = 0;
		name[v->namelen + 6] = entityIndex;
		oid_copy_in_addr(name + v->namelen + 7, &peerLdpId);
		name[v->namelen + 11] = 0;
		name[v->namelen + 12] = 0;

                *length = v->namelen + LDP_PEER_ENTRY_MAX_IDX_LEN;
	}

	switch (v->magic) {
	case MPLSLDPSESSIONSTATSUNKNOWNMESTYPEERRORS:
		return SNMP_INTEGER(ctl_nbr->stats.unknown_msg);
	case MPLSLDPSESSIONSTATSUNKNOWNTLVERRORS:
		return SNMP_INTEGER(ctl_nbr->stats.unknown_tlv);
	default:
		return NULL;
	}

	return NULL;
}

static struct variable ldpe_variables[] = {
	{MPLS_LDP_LSR_ID, STRING, RONLY, ldpLsrId, 3, {1, 1, 1}},
	{MPLS_LDP_LSR_LOOP_DETECTION_CAPABLE, INTEGER, RONLY,
	 ldpLoopDetectCap, 3, {1, 1, 2}},
	{MPLS_LDP_ENTITY_LAST_CHANGE, TIMESTAMP, RONLY, ldpEntityLastChange,
	 3, {1, 2, 1}},
	{MPLS_LDP_ENTITY_INDEX_NEXT, UNSIGNED32, RONLY, ldpEntityIndexNext,
	 3, {1, 2, 2}},

	/* MPLS LDP mplsLdpEntityTable. */
	{MPLSLDPENTITYLDPID, STRING, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 1}},
	{MPLSLDPENTITYINDEX, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 2}},
	{MPLSLDPENTITYPROTOCOLVERSION, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 3}},
	{MPLSLDPENTITYADMINSTATUS, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 4}},
	{MPLSLDPENTITYOPERSTATUS, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 5}},
	{MPLSLDPENTITYTCPPORT, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 6}},
	{MPLSLDPENTITYUDPDSCPORT, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 7}},
	{MPLSLDPENTITYMAXPDULENGTH, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 8}},
	{MPLSLDPENTITYKEEPALIVEHOLDTIMER, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 9}},
	{MPLSLDPENTITYHELLOHOLDTIMER, UNSIGNED32, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 10}},
	{MPLSLDPENTITYINITSESSIONTHRESHOLD, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 11}},
	{MPLSLDPENTITYLABELDISTMETHOD, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 12}},
	{MPLSLDPENTITYLABELRETENTIONMODE, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 13}},
	{MPLSLDPENTITYPATHVECTORLIMIT, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 14}},
	{MPLSLDPENTITYHOPCOUNTLIMIT, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 15}},
	{MPLSLDPENTITYTRANSPORTADDRKIND, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 16}},
	{MPLSLDPENTITYTARGETPEER, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 17}},
	{MPLSLDPENTITYTARGETPEERADDRTYPE, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 18}},
	{MPLSLDPENTITYTARGETPEERADDR, STRING, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 19}},
	{MPLSLDPENTITYLABELTYPE, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 20}},
	{MPLSLDPENTITYDISCONTINUITYTIME, TIMESTAMP, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 21}},
	{MPLSLDPENTITYSTORAGETYPE, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 22}},
	{MPLSLDPENTITYROWSTATUS, INTEGER, RONLY, ldpEntityTable,
	 5, {1, 2, 3, 1, 23}},

	/* MPLS LDP mplsLdpEntityStatsTable. */
	{ MPLSLDPENTITYSTATSSESSIONATTEMPTS, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 1}},
	{ MPLSLDPENTITYSTATSSESSIONREJHELLO, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 2}},
	{ MPLSLDPENTITYSTATSSESSIONREJAD, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 3}},
	{ MPLSLDPENTITYSTATSSESSIONREJMAXPDU, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 4}},
	{ MPLSLDPENTITYSTATSSESSIONREJLR, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 5}},
	{ MPLSLDPENTITYSTATSBADLDPID, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 6}},
	{ MPLSLDPENTITYSTATSBADPDULENGTH, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 7}},
	{ MPLSLDPENTITYSTATSBADMSGLENGTH, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 8}},
	{ MPLSLDPENTITYSTATSBADTLVLENGTH, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 9}},
	{ MPLSLDPENTITYSTATSMALFORMEDTLV, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 10}},
	{ MPLSLDPENTITYSTATSKEEPALIVEEXP, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 11}},
	{ MPLSLDPENTITYSTATSSHUTDOWNRCVNOTIFY, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 12}},
	{ MPLSLDPENTITYSTATSSHUTDOWNSENTNOTIFY, COUNTER32, RONLY,
	  ldpEntityStatsTable, 5, {1, 2, 4, 1, 13}},

	/* MPLS LDP mplsLdpPeerTable */
	{MPLSLDPPEERLDPID, STRING, RONLY, ldpPeerTable, 5, {1, 3, 2, 1, 1}},
	{MPLSLDPPEERLABELDISTMETHOD, INTEGER, RONLY, ldpPeerTable,
	 5, {1, 3, 2, 1, 2}},
	{MPLSLDPPEERPATHVECTORLIMIT, INTEGER, RONLY, ldpPeerTable,
	 5, {1, 3, 2, 1, 3}},
	{MPLSLDPPEERTRANSPORTADDRTYPE, INTEGER, RONLY, ldpPeerTable,
	 5, {1, 3, 2, 1, 4}},
	{MPLSLDPPEERTRANSPORTADDR, STRING, RONLY, ldpPeerTable,
	 5, {1, 3, 2, 1, 5}},

	/* MPLS LDP mplsLdpSessionTable */
	{MPLSLDPSESSIONSTATELASTCHANGE, TIMESTAMP, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 1}},
	{MPLSLDPSESSIONSTATE, INTEGER, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 2}},
	{MPLSLDPSESSIONROLE, INTEGER, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 3}},
	{MPLSLDPSESSIONPROTOCOLVERSION, UNSIGNED32, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 4}},
	{MPLSLDPSESSIONKEEPALIVEHOLDTIMEREM, INTEGER, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 5}},
	{MPLSLDPSESSIONKEEPALIVETIME, UNSIGNED32, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 6}},
	{MPLSLDPSESSIONMAXPDULENGTH, UNSIGNED32, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 7}},
	{MPLSLDPSESSIONDISCONTINUITYTIME, TIMESTAMP, RONLY, ldpSessionTable,
	 5, {1, 3, 3, 1, 8}},

	/* MPLS LDP mplsLdpSessionStatsTable */
	{MPLSLDPSESSIONSTATSUNKNOWNMESTYPEERRORS, COUNTER32, RONLY,
	 ldpSessionStatsTable, 5, {1, 3, 4, 1, 1}},
	{MPLSLDPSESSIONSTATSUNKNOWNTLVERRORS, COUNTER32, RONLY,
	 ldpSessionStatsTable, 5, {1, 3, 4, 1, 2}},

	/* MPLS LDP mplsLdpHelloAdjacencyTable. */
	{MPLSLDPHELLOADJACENCYINDEX, UNSIGNED32, RONLY,
	 ldpHelloAdjacencyTable, 6, {1, 3, 5, 1, 1, 1}},
	{MPLSLDPHELLOADJACENCYHOLDTIMEREM, INTEGER, RONLY,
	 ldpHelloAdjacencyTable, 6, {1, 3, 5, 1, 1, 2}},
	{MPLSLDPHELLOADJACENCYHOLDTIME, UNSIGNED32, RONLY,
	 ldpHelloAdjacencyTable, 6, {1, 3, 5, 1, 1, 3}},
	{MPLSLDPHELLOADJACENCYTYPE, INTEGER, RONLY,
	 ldpHelloAdjacencyTable, 6, {1, 3, 5, 1, 1, 4}},
};

static struct variable lde_variables[] = {
};

static struct trap_object ldpSessionTrapList[] = {
        {5, {1, 3, 3, 1, MPLSLDPSESSIONSTATE}},
        {5, {1, 3, 3, 1, MPLSLDPSESSIONDISCONTINUITYTIME}},
        {5, {1, 3, 4, 1, MPLSLDPSESSIONSTATSUNKNOWNMESTYPEERRORS}},
        {5, {1, 3, 4, 1, MPLSLDPSESSIONSTATSUNKNOWNTLVERRORS}}};

/* LDP TRAP. */
#define LDPINITSESSIONTHRESHOLDEXCEEDED	1
#define LDPPATHVECTORLIMITMISMATCH	2
#define LDPSESSIONUP			3
#define LDPSESSIONDOWN			4

static void
ldpTrapSession(struct nbr * nbr, unsigned int sptrap)
{
        oid index[sizeof(oid) * (LDP_PEER_ENTRY_MAX_IDX_LEN + 1)];

	struct in_addr entityLdpId = {.s_addr = 0};
	uint32_t entityIndex = 0;
	struct in_addr peerLdpId = {.s_addr = 0};

	struct ctl_nbr *ctl_nbr = nbr_to_ctl(nbr);

	entityLdpId.s_addr = ldp_rtr_id_get(leconf);
	entityIndex = LDP_DEFAULT_ENTITY_INDEX;
	peerLdpId = ctl_nbr->id;

	oid_copy_in_addr(index, &entityLdpId);
	index[4] = 0;
	index[5] = 0;
	index[6] = entityIndex;
	oid_copy_in_addr(&index[7], &peerLdpId);
	index[11] = 0;
	index[12] = 0;

	index[LDP_PEER_ENTRY_MAX_IDX_LEN] = 0;

        smux_trap(ldpe_variables, array_size(ldpe_variables), ldp_trap_oid,
                  array_size(ldp_trap_oid), ldp_oid,
                  sizeof(ldp_oid) / sizeof(oid), index,
		  LDP_PEER_ENTRY_MAX_IDX_LEN + 1,
                  ldpSessionTrapList, array_size(ldpSessionTrapList), sptrap);
}

static void
ldpTrapSessionUp(struct nbr * nbr)
{
	ldpTrapSession(nbr, LDPSESSIONUP);
}

static void
ldpTrapSessionDown(struct nbr * nbr)
{
	ldpTrapSession(nbr, LDPSESSIONDOWN);
}

static int ldp_snmp_agentx_enabled(void)
{
	main_imsg_compose_both(IMSG_AGENTX_ENABLED, NULL, 0);

	return 0;
}

static int ldp_snmp_nbr_state_change(struct nbr * nbr, int old_state)
{
	if (old_state == nbr->state)
		return 0;

	if (nbr->state == NBR_STA_OPER)
		ldpTrapSessionUp(nbr);
	else if (old_state == NBR_STA_OPER)
		ldpTrapSessionDown(nbr);

	return 0;
}

static int ldp_snmp_init(struct thread_master *tm)
{
	hook_register(agentx_enabled, ldp_snmp_agentx_enabled);

	smux_init(tm);

	return 0;
}

static int ldp_snmp_register_mib(struct thread_master *tm)
{
	static int registered = 0;

	if (registered)
		return 0;

	registered = 1;

	smux_init(tm);

	smux_agentx_enable();

	if (ldpd_process == PROC_LDE_ENGINE)
		REGISTER_MIB("mibII/ldp", lde_variables, variable, ldp_oid);
	else if (ldpd_process == PROC_LDP_ENGINE) {
		REGISTER_MIB("mibII/ldp", ldpe_variables, variable, ldp_oid);

		hook_register(ldp_nbr_state_change, ldp_snmp_nbr_state_change);
	}

	return 0;
}

static int ldp_snmp_module_init(void)
{
	if (ldpd_process == PROC_MAIN)
		hook_register(frr_late_init, ldp_snmp_init);
	else
		hook_register(ldp_register_mib, ldp_snmp_register_mib);

	return 0;
}

FRR_MODULE_SETUP(
	.name = "ldp_snmp",
	.version = FRR_VERSION,
	.description = "ldp AgentX SNMP module",
	.init = ldp_snmp_module_init,
);