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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
|
/* 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 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>
#if defined HAVE_SNMP && defined SNMP_SMUX
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include "log.h"
#include "thread.h"
#include "linklist.h"
#include "command.h"
#include <lib/version.h>
#include "memory.h"
#include "sockunion.h"
#include "smux.h"
#define SMUX_PORT_DEFAULT 199
#define SMUXMAXPKTSIZE 1500
#define SMUXMAXSTRLEN 256
#define SMUX_OPEN (ASN_APPLICATION | ASN_CONSTRUCTOR | 0)
#define SMUX_CLOSE (ASN_APPLICATION | ASN_PRIMITIVE | 1)
#define SMUX_RREQ (ASN_APPLICATION | ASN_CONSTRUCTOR | 2)
#define SMUX_RRSP (ASN_APPLICATION | ASN_PRIMITIVE | 3)
#define SMUX_SOUT (ASN_APPLICATION | ASN_PRIMITIVE | 4)
#define SMUX_GET (ASN_CONTEXT | ASN_CONSTRUCTOR | 0)
#define SMUX_GETNEXT (ASN_CONTEXT | ASN_CONSTRUCTOR | 1)
#define SMUX_GETRSP (ASN_CONTEXT | ASN_CONSTRUCTOR | 2)
#define SMUX_SET (ASN_CONTEXT | ASN_CONSTRUCTOR | 3)
#define SMUX_TRAP (ASN_CONTEXT | ASN_CONSTRUCTOR | 4)
#define SMUX_MAX_FAILURE 3
/* SNMP tree. */
struct subtree
{
/* Tree's oid. */
oid name[MAX_OID_LEN];
u_char name_len;
/* List of the variables. */
struct variable *variables;
/* Length of the variables list. */
int variables_num;
/* Width of the variables list. */
int variables_width;
/* Registered flag. */
int registered;
};
#define min(A,B) ((A) < (B) ? (A) : (B))
enum smux_event {SMUX_SCHEDULE, SMUX_CONNECT, SMUX_READ};
void smux_event (enum smux_event, int);
/* SMUX socket. */
int smux_sock = -1;
/* SMUX subtree list. */
struct list *treelist;
/* SMUX oid. */
oid *smux_oid = NULL;
size_t smux_oid_len;
/* SMUX password. */
char *smux_passwd = NULL;
/* SMUX read threads. */
struct thread *smux_read_thread;
/* SMUX connect thrads. */
struct thread *smux_connect_thread;
/* SMUX debug flag. */
int debug_smux = 0;
/* SMUX failure count. */
int fail = 0;
/* SMUX node. */
static struct cmd_node smux_node =
{
SMUX_NODE,
"" /* SMUX has no interface. */
};
/* thread master */
static struct thread_master *smux_master;
static int
oid_compare_part (oid *o1, int o1_len, oid *o2, int o2_len)
{
int i;
for (i = 0; i < min (o1_len, o2_len); i++)
{
if (o1[i] < o2[i])
return -1;
else if (o1[i] > o2[i])
return 1;
}
if (o1_len < o2_len)
return -1;
return 0;
}
static void
smux_oid_dump (const char *prefix, const oid *oid, size_t oid_len)
{
unsigned int i;
int first = 1;
char buf[MAX_OID_LEN * 3];
buf[0] = '\0';
for (i = 0; i < oid_len; i++)
{
sprintf (buf + strlen (buf), "%s%d", first ? "" : ".", (int) oid[i]);
first = 0;
}
zlog_debug ("%s: %s", prefix, buf);
}
static int
smux_socket (void)
{
int ret;
struct addrinfo hints, *res0, *res;
int gai;
int sock = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
gai = getaddrinfo(NULL, "smux", &hints, &res0);
if (gai == EAI_SERVICE)
{
char servbuf[NI_MAXSERV];
sprintf(servbuf,"%d",SMUX_PORT_DEFAULT);
servbuf[sizeof (servbuf) - 1] = '\0';
gai = getaddrinfo(NULL, servbuf, &hints, &res0);
}
if (gai)
{
zlog_warn("Cannot locate loopback service smux");
return -1;
}
for(res=res0; res; res=res->ai_next)
{
if (res->ai_family != AF_INET
&& res->ai_family != AF_INET6
)
continue;
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock < 0)
continue;
sockopt_reuseaddr (sock);
sockopt_reuseport (sock);
ret = connect (sock, res->ai_addr, res->ai_addrlen);
if (ret < 0)
{
close(sock);
sock = -1;
continue;
}
break;
}
freeaddrinfo(res0);
if (sock < 0)
zlog_warn ("Can't connect to SNMP agent with SMUX");
return sock;
}
static void
smux_getresp_send (oid objid[], size_t objid_len, long reqid, long errstat,
long errindex, u_char val_type, void *arg, size_t arg_len)
{
u_char buf[BUFSIZ];
u_char *ptr, *h1, *h1e, *h2, *h2e;
size_t len, length;
ptr = buf;
len = BUFSIZ;
length = len;
if (debug_smux)
{
zlog_debug ("SMUX GETRSP send");
zlog_debug ("SMUX GETRSP reqid: %ld", reqid);
}
h1 = ptr;
/* Place holder h1 for complete sequence */
ptr = asn_build_sequence (ptr, &len, (u_char) SMUX_GETRSP, 0);
h1e = ptr;
ptr = asn_build_int (ptr, &len,
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&reqid, sizeof (reqid));
if (debug_smux)
zlog_debug ("SMUX GETRSP errstat: %ld", errstat);
ptr = asn_build_int (ptr, &len,
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&errstat, sizeof (errstat));
if (debug_smux)
zlog_debug ("SMUX GETRSP errindex: %ld", errindex);
ptr = asn_build_int (ptr, &len,
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&errindex, sizeof (errindex));
h2 = ptr;
/* Place holder h2 for one variable */
ptr = asn_build_sequence (ptr, &len,
(u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR),
0);
h2e = ptr;
ptr = snmp_build_var_op (ptr, objid, &objid_len,
val_type, arg_len, arg, &len);
/* Now variable size is known, fill in size */
asn_build_sequence(h2,&length,(u_char)(ASN_SEQUENCE|ASN_CONSTRUCTOR),ptr-h2e);
/* Fill in size of whole sequence */
asn_build_sequence(h1,&length,(u_char)SMUX_GETRSP,ptr-h1e);
if (debug_smux)
zlog_debug ("SMUX getresp send: %td", (ptr - buf));
send (smux_sock, buf, (ptr - buf), 0);
}
static u_char *
smux_var (u_char *ptr, size_t len, oid objid[], size_t *objid_len,
size_t *var_val_len,
u_char *var_val_type,
void **var_value)
{
u_char type;
u_char val_type;
size_t val_len;
u_char *val;
if (debug_smux)
zlog_debug ("SMUX var parse: len %zd", len);
/* Parse header. */
ptr = asn_parse_header (ptr, &len, &type);
if (debug_smux)
{
zlog_debug ("SMUX var parse: type %d len %zd", type, len);
zlog_debug ("SMUX var parse: type must be %d",
(ASN_SEQUENCE | ASN_CONSTRUCTOR));
}
/* Parse var option. */
*objid_len = MAX_OID_LEN;
ptr = snmp_parse_var_op(ptr, objid, objid_len, &val_type,
&val_len, &val, &len);
if (var_val_len)
*var_val_len = val_len;
if (var_value)
*var_value = (void*) val;
if (var_val_type)
*var_val_type = val_type;
/* Requested object id length is objid_len. */
if (debug_smux)
smux_oid_dump ("Request OID", objid, *objid_len);
if (debug_smux)
zlog_debug ("SMUX val_type: %d", val_type);
/* Check request value type. */
if (debug_smux)
switch (val_type)
{
case ASN_NULL:
/* In case of SMUX_GET or SMUX_GET_NEXT val_type is set to
ASN_NULL. */
zlog_debug ("ASN_NULL");
break;
case ASN_INTEGER:
zlog_debug ("ASN_INTEGER");
break;
case ASN_COUNTER:
case ASN_GAUGE:
case ASN_TIMETICKS:
case ASN_UINTEGER:
zlog_debug ("ASN_COUNTER");
break;
case ASN_COUNTER64:
zlog_debug ("ASN_COUNTER64");
break;
case ASN_IPADDRESS:
zlog_debug ("ASN_IPADDRESS");
break;
case ASN_OCTET_STR:
zlog_debug ("ASN_OCTET_STR");
break;
case ASN_OPAQUE:
case ASN_NSAP:
case ASN_OBJECT_ID:
zlog_debug ("ASN_OPAQUE");
break;
case SNMP_NOSUCHOBJECT:
zlog_debug ("SNMP_NOSUCHOBJECT");
break;
case SNMP_NOSUCHINSTANCE:
zlog_debug ("SNMP_NOSUCHINSTANCE");
break;
case SNMP_ENDOFMIBVIEW:
zlog_debug ("SNMP_ENDOFMIBVIEW");
break;
case ASN_BIT_STR:
zlog_debug ("ASN_BIT_STR");
break;
default:
zlog_debug ("Unknown type");
break;
}
return ptr;
}
/* NOTE: all 3 functions (smux_set, smux_get & smux_getnext) are based on
ucd-snmp smux and as such suppose, that the peer receives in the message
only one variable. Fortunately, IBM seems to do the same in AIX. */
static int
smux_set (oid *reqid, size_t *reqid_len,
u_char val_type, void *val, size_t val_len, int action)
{
int j;
struct subtree *subtree;
struct variable *v;
int subresult;
oid *suffix;
size_t suffix_len;
int result;
u_char *statP = NULL;
WriteMethod *write_method = NULL;
struct listnode *node, *nnode;
/* Check */
for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree))
{
subresult = oid_compare_part (reqid, *reqid_len,
subtree->name, subtree->name_len);
/* Subtree matched. */
if (subresult == 0)
{
/* Prepare suffix. */
suffix = reqid + subtree->name_len;
suffix_len = *reqid_len - subtree->name_len;
result = subresult;
/* Check variables. */
for (j = 0; j < subtree->variables_num; j++)
{
v = &subtree->variables[j];
/* Always check suffix */
result = oid_compare_part (suffix, suffix_len,
v->name, v->namelen);
/* This is exact match so result must be zero. */
if (result == 0)
{
if (debug_smux)
zlog_debug ("SMUX function call index is %d", v->magic);
statP = (*v->findVar) (v, suffix, &suffix_len, 1,
&val_len, &write_method);
if (write_method)
{
return (*write_method)(action, val, val_type, val_len,
statP, suffix, suffix_len);
}
else
{
return SNMP_ERR_READONLY;
}
}
/* If above execution is failed or oid is small (so
there is no further match). */
if (result < 0)
return SNMP_ERR_NOSUCHNAME;
}
}
}
return SNMP_ERR_NOSUCHNAME;
}
static int
smux_get (oid *reqid, size_t *reqid_len, int exact,
u_char *val_type,void **val, size_t *val_len)
{
int j;
struct subtree *subtree;
struct variable *v;
int subresult;
oid *suffix;
size_t suffix_len;
int result;
WriteMethod *write_method=NULL;
struct listnode *node, *nnode;
/* Check */
for (ALL_LIST_ELEMENTS (treelist, node, nnode,subtree))
{
subresult = oid_compare_part (reqid, *reqid_len,
subtree->name, subtree->name_len);
/* Subtree matched. */
if (subresult == 0)
{
/* Prepare suffix. */
suffix = reqid + subtree->name_len;
suffix_len = *reqid_len - subtree->name_len;
result = subresult;
/* Check variables. */
for (j = 0; j < subtree->variables_num; j++)
{
v = &subtree->variables[j];
/* Always check suffix */
result = oid_compare_part (suffix, suffix_len,
v->name, v->namelen);
/* This is exact match so result must be zero. */
if (result == 0)
{
if (debug_smux)
zlog_debug ("SMUX function call index is %d", v->magic);
*val = (*v->findVar) (v, suffix, &suffix_len, exact,
val_len, &write_method);
/* There is no instance. */
if (*val == NULL)
return SNMP_NOSUCHINSTANCE;
/* Call is suceed. */
*val_type = v->type;
return 0;
}
/* If above execution is failed or oid is small (so
there is no further match). */
if (result < 0)
return SNMP_ERR_NOSUCHNAME;
}
}
}
return SNMP_ERR_NOSUCHNAME;
}
static int
smux_getnext (oid *reqid, size_t *reqid_len, int exact,
u_char *val_type,void **val, size_t *val_len)
{
int j;
oid save[MAX_OID_LEN];
int savelen = 0;
struct subtree *subtree;
struct variable *v;
int subresult;
oid *suffix;
size_t suffix_len;
int result;
WriteMethod *write_method=NULL;
struct listnode *node, *nnode;
/* Save incoming request. */
oid_copy (save, reqid, *reqid_len);
savelen = *reqid_len;
/* Check */
for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree))
{
subresult = oid_compare_part (reqid, *reqid_len,
subtree->name, subtree->name_len);
/* If request is in the tree. The agent has to make sure we
only receive requests we have registered for. */
/* Unfortunately, that's not true. In fact, a SMUX subagent has to
behave as if it manages the whole SNMP MIB tree itself. It's the
duty of the master agent to collect the best answer and return it
to the manager. See RFC 1227 chapter 3.1.6 for the glory details
:-). ucd-snmp really behaves bad here as it actually might ask
multiple times for the same GETNEXT request as it throws away the
answer when it expects it in a different subtree and might come
back later with the very same request. --jochen */
if (subresult <= 0)
{
/* Prepare suffix. */
suffix = reqid + subtree->name_len;
suffix_len = *reqid_len - subtree->name_len;
if (subresult < 0)
{
oid_copy(reqid, subtree->name, subtree->name_len);
*reqid_len = subtree->name_len;
}
for (j = 0; j < subtree->variables_num; j++)
{
result = subresult;
v = &subtree->variables[j];
/* Next then check result >= 0. */
if (result == 0)
result = oid_compare_part (suffix, suffix_len,
v->name, v->namelen);
if (result <= 0)
{
if (debug_smux)
zlog_debug ("SMUX function call index is %d", v->magic);
if(result<0)
{
oid_copy(suffix, v->name, v->namelen);
suffix_len = v->namelen;
}
*val = (*v->findVar) (v, suffix, &suffix_len, exact,
val_len, &write_method);
*reqid_len = suffix_len + subtree->name_len;
if (*val)
{
*val_type = v->type;
return 0;
}
}
}
}
}
memcpy (reqid, save, savelen * sizeof(oid));
*reqid_len = savelen;
return SNMP_ERR_NOSUCHNAME;
}
/* GET message header. */
static u_char *
smux_parse_get_header (u_char *ptr, size_t *len, long *reqid)
{
u_char type;
long errstat;
long errindex;
/* Request ID. */
ptr = asn_parse_int (ptr, len, &type, reqid, sizeof (*reqid));
if (debug_smux)
zlog_debug ("SMUX GET reqid: %d len: %d", (int) *reqid, (int) *len);
/* Error status. */
ptr = asn_parse_int (ptr, len, &type, &errstat, sizeof (errstat));
if (debug_smux)
zlog_debug ("SMUX GET errstat %ld len: %zd", errstat, *len);
/* Error index. */
ptr = asn_parse_int (ptr, len, &type, &errindex, sizeof (errindex));
if (debug_smux)
zlog_debug ("SMUX GET errindex %ld len: %zd", errindex, *len);
return ptr;
}
static void
smux_parse_set (u_char *ptr, size_t len, int action)
{
long reqid;
oid oid[MAX_OID_LEN];
size_t oid_len;
u_char val_type;
void *val;
size_t val_len;
int ret;
if (debug_smux)
zlog_debug ("SMUX SET(%s) message parse: len %zd",
(RESERVE1 == action) ? "RESERVE1" : ((FREE == action) ? "FREE" : "COMMIT"),
len);
/* Parse SET message header. */
ptr = smux_parse_get_header (ptr, &len, &reqid);
/* Parse SET message object ID. */
ptr = smux_var (ptr, len, oid, &oid_len, &val_len, &val_type, &val);
ret = smux_set (oid, &oid_len, val_type, val, val_len, action);
if (debug_smux)
zlog_debug ("SMUX SET ret %d", ret);
/* Return result. */
if (RESERVE1 == action)
smux_getresp_send (oid, oid_len, reqid, ret, 3, ASN_NULL, NULL, 0);
}
static void
smux_parse_get (u_char *ptr, size_t len, int exact)
{
long reqid;
oid oid[MAX_OID_LEN];
size_t oid_len;
u_char val_type;
void *val;
size_t val_len;
int ret;
if (debug_smux)
zlog_debug ("SMUX GET message parse: len %zd", len);
/* Parse GET message header. */
ptr = smux_parse_get_header (ptr, &len, &reqid);
/* Parse GET message object ID. We needn't the value come */
ptr = smux_var (ptr, len, oid, &oid_len, NULL, NULL, NULL);
/* Traditional getstatptr. */
if (exact)
ret = smux_get (oid, &oid_len, exact, &val_type, &val, &val_len);
else
ret = smux_getnext (oid, &oid_len, exact, &val_type, &val, &val_len);
/* Return result. */
if (ret == 0)
smux_getresp_send (oid, oid_len, reqid, 0, 0, val_type, val, val_len);
else
smux_getresp_send (oid, oid_len, reqid, ret, 3, ASN_NULL, NULL, 0);
}
/* Parse SMUX_CLOSE message. */
static void
smux_parse_close (u_char *ptr, int len)
{
long reason = 0;
while (len--)
{
reason = (reason << 8) | (long) *ptr;
ptr++;
}
zlog_info ("SMUX_CLOSE with reason: %ld", reason);
}
/* SMUX_RRSP message. */
static void
smux_parse_rrsp (u_char *ptr, size_t len)
{
u_char val;
long errstat;
ptr = asn_parse_int (ptr, &len, &val, &errstat, sizeof (errstat));
if (debug_smux)
zlog_debug ("SMUX_RRSP value: %d errstat: %ld", val, errstat);
}
/* Parse SMUX message. */
static int
smux_parse (u_char *ptr, size_t len)
{
/* This buffer we'll use for SOUT message. We could allocate it with
malloc and save only static pointer/lenght, but IMHO static
buffer is a faster solusion. */
static u_char sout_save_buff[SMUXMAXPKTSIZE];
static int sout_save_len = 0;
int len_income = len; /* see note below: YYY */
u_char type;
u_char rollback;
rollback = ptr[2]; /* important only for SMUX_SOUT */
process_rest: /* see note below: YYY */
/* Parse SMUX message type and subsequent length. */
ptr = asn_parse_header (ptr, &len, &type);
if (debug_smux)
zlog_debug ("SMUX message received type: %d rest len: %zd", type, len);
switch (type)
{
case SMUX_OPEN:
/* Open must be not send from SNMP agent. */
zlog_warn ("SMUX_OPEN received: resetting connection.");
return -1;
break;
case SMUX_RREQ:
/* SMUX_RREQ message is invalid for us. */
zlog_warn ("SMUX_RREQ received: resetting connection.");
return -1;
break;
case SMUX_SOUT:
/* SMUX_SOUT message is now valied for us. */
if (debug_smux)
zlog_debug ("SMUX_SOUT(%s)", rollback ? "rollback" : "commit");
if (sout_save_len > 0)
{
smux_parse_set (sout_save_buff, sout_save_len, rollback ? FREE : COMMIT);
sout_save_len = 0;
}
else
zlog_warn ("SMUX_SOUT sout_save_len=%d - invalid", (int) sout_save_len);
if (len_income > 3)
{
/* YYY: this strange code has to solve the "slow peer"
problem: When agent sends SMUX_SOUT message it doesn't
wait any responce and may send some next message to
subagent. Then the peer in 'smux_read()' will recieve
from socket the 'concatenated' buffer, contaning both
SMUX_SOUT message and the next one
(SMUX_GET/SMUX_GETNEXT/SMUX_GET). So we should check: if
the buffer is longer than 3 ( length of SMUX_SOUT ), we
must process the rest of it. This effect may be observed
if 'debug_smux' is set to '1' */
ptr++;
len = len_income - 3;
goto process_rest;
}
break;
case SMUX_GETRSP:
/* SMUX_GETRSP message is invalid for us. */
zlog_warn ("SMUX_GETRSP received: resetting connection.");
return -1;
break;
case SMUX_CLOSE:
/* Close SMUX connection. */
if (debug_smux)
zlog_debug ("SMUX_CLOSE");
smux_parse_close (ptr, len);
return -1;
break;
case SMUX_RRSP:
/* This is response for register message. */
if (debug_smux)
zlog_debug ("SMUX_RRSP");
smux_parse_rrsp (ptr, len);
break;
case SMUX_GET:
/* Exact request for object id. */
if (debug_smux)
zlog_debug ("SMUX_GET");
smux_parse_get (ptr, len, 1);
break;
case SMUX_GETNEXT:
/* Next request for object id. */
if (debug_smux)
zlog_debug ("SMUX_GETNEXT");
smux_parse_get (ptr, len, 0);
break;
case SMUX_SET:
/* SMUX_SET is supported with some limitations. */
if (debug_smux)
zlog_debug ("SMUX_SET");
/* save the data for future SMUX_SOUT */
memcpy (sout_save_buff, ptr, len);
sout_save_len = len;
smux_parse_set (ptr, len, RESERVE1);
break;
default:
zlog_info ("Unknown type: %d", type);
break;
}
return 0;
}
/* SMUX message read function. */
static int
smux_read (struct thread *t)
{
int sock;
int len;
u_char buf[SMUXMAXPKTSIZE];
int ret;
/* Clear thread. */
sock = THREAD_FD (t);
smux_read_thread = NULL;
if (debug_smux)
zlog_debug ("SMUX read start");
/* Read message from SMUX socket. */
len = recv (sock, buf, SMUXMAXPKTSIZE, 0);
if (len < 0)
{
zlog_warn ("Can't read all SMUX packet: %s", safe_strerror (errno));
close (sock);
smux_sock = -1;
smux_event (SMUX_CONNECT, 0);
return -1;
}
if (len == 0)
{
zlog_warn ("SMUX connection closed: %d", sock);
close (sock);
smux_sock = -1;
smux_event (SMUX_CONNECT, 0);
return -1;
}
if (debug_smux)
zlog_debug ("SMUX read len: %d", len);
/* Parse the message. */
ret = smux_parse (buf, len);
if (ret < 0)
{
close (sock);
smux_sock = -1;
smux_event (SMUX_CONNECT, 0);
return -1;
}
/* Regiser read thread. */
smux_event (SMUX_READ, sock);
return 0;
}
static int
smux_open (int sock)
{
u_char buf[BUFSIZ];
u_char *ptr;
size_t len;
long version;
const char progname[] = FRR_SMUX_NAME "-" FRR_VERSION;
if (debug_smux)
{
smux_oid_dump ("SMUX open oid", smux_oid, smux_oid_len);
zlog_debug ("SMUX open progname: %s", progname);
zlog_debug ("SMUX open password: %s", smux_passwd);
}
ptr = buf;
len = BUFSIZ;
/* SMUX Header. As placeholder. */
ptr = asn_build_header (ptr, &len, (u_char) SMUX_OPEN, 0);
/* SMUX Open. */
version = 0;
ptr = asn_build_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&version, sizeof (version));
/* SMUX connection oid. */
ptr = asn_build_objid (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
smux_oid, smux_oid_len);
/* SMUX connection description. */
ptr = asn_build_string (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR),
(const u_char *) progname, strlen (progname));
/* SMUX connection password. */
ptr = asn_build_string (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR),
(u_char *)smux_passwd, strlen (smux_passwd));
/* Fill in real SMUX header. We exclude ASN header size (2). */
len = BUFSIZ;
asn_build_header (buf, &len, (u_char) SMUX_OPEN, (ptr - buf) - 2);
return send (sock, buf, (ptr - buf), 0);
}
/* `ename` is ignored. Instead of using the provided enterprise OID,
the SMUX peer is used. This keep compatibility with the previous
versions of Quagga.
All other fields are used as they are intended. */
int
smux_trap (struct variable *vp, size_t vp_len,
const oid *ename, size_t enamelen,
const oid *name, size_t namelen,
const oid *iname, size_t inamelen,
const struct trap_object *trapobj, size_t trapobjlen,
u_char sptrap)
{
unsigned int i;
u_char buf[BUFSIZ];
u_char *ptr;
size_t len, length;
struct in_addr addr;
unsigned long val;
u_char *h1, *h1e;
ptr = buf;
len = BUFSIZ;
length = len;
/* When SMUX connection is not established. */
if (smux_sock < 0)
return 0;
/* SMUX header. */
ptr = asn_build_header (ptr, &len, (u_char) SMUX_TRAP, 0);
/* Sub agent enterprise oid. */
ptr = asn_build_objid (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
smux_oid, smux_oid_len);
/* IP address. */
addr.s_addr = 0;
ptr = asn_build_string (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_IPADDRESS),
(u_char *)&addr, sizeof (addr));
/* Generic trap integer. */
val = SNMP_TRAP_ENTERPRISESPECIFIC;
ptr = asn_build_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
(long *)&val, sizeof (val));
/* Specific trap integer. */
val = sptrap;
ptr = asn_build_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
(long *)&val, sizeof (val));
/* Timeticks timestamp. */
val = 0;
ptr = asn_build_unsigned_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_TIMETICKS),
&val, sizeof (val));
/* Variables. */
h1 = ptr;
ptr = asn_build_sequence (ptr, &len,
(u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
0);
/* Iteration for each objects. */
h1e = ptr;
for (i = 0; i < trapobjlen; i++)
{
int ret;
oid oid[MAX_OID_LEN];
size_t oid_len;
void *val;
size_t val_len;
u_char val_type;
/* Make OID. */
if (trapobj[i].namelen > 0)
{
oid_copy (oid, name, namelen);
oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen);
oid_copy (oid + namelen + trapobj[i].namelen, iname, inamelen);
oid_len = namelen + trapobj[i].namelen + inamelen;
}
else
{
oid_copy (oid, name, namelen);
oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen * (-1));
oid_len = namelen + trapobj[i].namelen * (-1) ;
}
if (debug_smux)
{
smux_oid_dump ("Trap", name, namelen);
if (trapobj[i].namelen < 0)
smux_oid_dump ("Trap",
trapobj[i].name, (- 1) * (trapobj[i].namelen));
else
{
smux_oid_dump ("Trap", trapobj[i].name, (trapobj[i].namelen));
smux_oid_dump ("Trap", iname, inamelen);
}
smux_oid_dump ("Trap", oid, oid_len);
zlog_info ("BUFSIZ: %d // oid_len: %lu", BUFSIZ, (u_long)oid_len);
}
ret = smux_get (oid, &oid_len, 1, &val_type, &val, &val_len);
if (debug_smux)
zlog_debug ("smux_get result %d", ret);
if (ret == 0)
ptr = snmp_build_var_op (ptr, oid, &oid_len,
val_type, val_len, val, &len);
}
/* Now variable size is known, fill in size */
asn_build_sequence(h1, &length,
(u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
ptr - h1e);
/* Fill in size of whole sequence */
len = BUFSIZ;
asn_build_header (buf, &len, (u_char) SMUX_TRAP, (ptr - buf) - 2);
return send (smux_sock, buf, (ptr - buf), 0);
}
static int
smux_register (int sock)
{
u_char buf[BUFSIZ];
u_char *ptr;
int ret;
size_t len;
long priority;
long operation;
struct subtree *subtree;
struct listnode *node, *nnode;
ret = 0;
for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree))
{
ptr = buf;
len = BUFSIZ;
/* SMUX RReq Header. */
ptr = asn_build_header (ptr, &len, (u_char) SMUX_RREQ, 0);
/* Register MIB tree. */
ptr = asn_build_objid (ptr, &len,
(u_char)
(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
subtree->name, subtree->name_len);
/* Priority. */
priority = -1;
ptr = asn_build_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&priority, sizeof (priority));
/* Operation. */
operation = 2; /* Register R/W */
ptr = asn_build_int (ptr, &len,
(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
&operation, sizeof (operation));
if (debug_smux)
{
smux_oid_dump ("SMUX register oid", subtree->name, subtree->name_len);
zlog_debug ("SMUX register priority: %ld", priority);
zlog_debug ("SMUX register operation: %ld", operation);
}
len = BUFSIZ;
asn_build_header (buf, &len, (u_char) SMUX_RREQ, (ptr - buf) - 2);
ret = send (sock, buf, (ptr - buf), 0);
if (ret < 0)
return ret;
}
return ret;
}
/* Try to connect to SNMP agent. */
static int
smux_connect (struct thread *t)
{
int ret;
if (debug_smux)
zlog_debug ("SMUX connect try %d", fail + 1);
/* Clear thread poner of myself. */
smux_connect_thread = NULL;
/* Make socket. Try to connect. */
smux_sock = smux_socket ();
if (smux_sock < 0)
{
if (++fail < SMUX_MAX_FAILURE)
smux_event (SMUX_CONNECT, 0);
return 0;
}
/* Send OPEN PDU. */
ret = smux_open (smux_sock);
if (ret < 0)
{
zlog_warn ("SMUX open message send failed: %s", safe_strerror (errno));
close (smux_sock);
smux_sock = -1;
if (++fail < SMUX_MAX_FAILURE)
smux_event (SMUX_CONNECT, 0);
return -1;
}
/* Send any outstanding register PDUs. */
ret = smux_register (smux_sock);
if (ret < 0)
{
zlog_warn ("SMUX register message send failed: %s", safe_strerror (errno));
close (smux_sock);
smux_sock = -1;
if (++fail < SMUX_MAX_FAILURE)
smux_event (SMUX_CONNECT, 0);
return -1;
}
/* Everything goes fine. */
smux_event (SMUX_READ, smux_sock);
return 0;
}
/* Clear all SMUX related resources. */
static void
smux_stop (void)
{
if (smux_read_thread)
{
thread_cancel (smux_read_thread);
smux_read_thread = NULL;
}
if (smux_connect_thread)
{
thread_cancel (smux_connect_thread);
smux_connect_thread = NULL;
}
if (smux_sock >= 0)
{
close (smux_sock);
smux_sock = -1;
}
}
void
smux_event (enum smux_event event, int sock)
{
switch (event)
{
case SMUX_SCHEDULE:
smux_connect_thread = thread_add_event (smux_master, smux_connect, NULL, 0);
break;
case SMUX_CONNECT:
smux_connect_thread = thread_add_timer (smux_master, smux_connect, NULL, 10);
break;
case SMUX_READ:
smux_read_thread = thread_add_read (smux_master, smux_read, NULL, sock);
break;
default:
break;
}
}
static int
smux_str2oid (const char *str, oid *oid, size_t *oid_len)
{
int len;
int val;
len = 0;
val = 0;
*oid_len = 0;
if (*str == '.')
str++;
if (*str == '\0')
return 0;
while (1)
{
if (! isdigit (*str))
return -1;
while (isdigit (*str))
{
val *= 10;
val += (*str - '0');
str++;
}
if (*str == '\0')
break;
if (*str != '.')
return -1;
oid[len++] = val;
val = 0;
str++;
}
oid[len++] = val;
*oid_len = len;
return 0;
}
static oid *
smux_oid_dup (oid *objid, size_t objid_len)
{
oid *new;
new = XMALLOC (MTYPE_TMP, sizeof (oid) * objid_len);
oid_copy (new, objid, objid_len);
return new;
}
static int
smux_peer_oid (struct vty *vty, const char *oid_str, const char *passwd_str)
{
int ret;
oid oid[MAX_OID_LEN];
size_t oid_len;
ret = smux_str2oid (oid_str, oid, &oid_len);
if (ret != 0)
{
vty_out (vty, "object ID malformed%s", VTY_NEWLINE);
return CMD_WARNING;
}
if (smux_oid)
{
free (smux_oid);
smux_oid = NULL;
}
/* careful, smux_passwd might point to string constant */
if (smux_passwd)
{
free (smux_passwd);
smux_passwd = NULL;
}
smux_oid = smux_oid_dup (oid, oid_len);
smux_oid_len = oid_len;
if (passwd_str)
smux_passwd = strdup (passwd_str);
else
smux_passwd = strdup ("");
return 0;
}
static int
smux_peer_default (void)
{
if (smux_oid)
{
free (smux_oid);
smux_oid = NULL;
}
/* careful, smux_passwd might be pointing at string constant */
if (smux_passwd)
{
free (smux_passwd);
smux_passwd = NULL;
}
return CMD_SUCCESS;
}
DEFUN (smux_peer,
smux_peer_cmd,
"smux peer OID",
"SNMP MUX protocol settings\n"
"SNMP MUX peer settings\n"
"Object ID used in SMUX peering\n")
{
int idx_oid = 2;
if (smux_peer_oid (vty, argv[idx_oid]->arg, NULL) == 0)
{
smux_start();
return CMD_SUCCESS;
}
else
return CMD_WARNING;
}
DEFUN (smux_peer_password,
smux_peer_password_cmd,
"smux peer OID PASSWORD",
"SNMP MUX protocol settings\n"
"SNMP MUX peer settings\n"
"SMUX peering object ID\n"
"SMUX peering password\n")
{
int idx_oid = 2;
if (smux_peer_oid (vty, argv[idx_oid]->arg, argv[3]->rg) == 0)
{
smux_start();
return CMD_SUCCESS;
}
else
return CMD_WARNING;
}
DEFUN (no_smux_peer,
no_smux_peer_cmd,
"no smux peer [OID [PASSWORD]]",
NO_STR
"SNMP MUX protocol settings\n"
"SNMP MUX peer settings\n"
"SMUX peering object ID\n"
"SMUX peering password\n")
{
smux_stop();
return smux_peer_default ();
}
static int
config_write_smux (struct vty *vty)
{
int first = 1;
unsigned int i;
if (smux_oid)
{
vty_out (vty, "smux peer ");
for (i = 0; i < smux_oid_len; i++)
{
vty_out (vty, "%s%d", first ? "" : ".", (int) smux_oid[i]);
first = 0;
}
vty_out (vty, " %s%s", smux_passwd, VTY_NEWLINE);
}
return 0;
}
/* Register subtree to smux master tree. */
void
smux_register_mib (const char *descr, struct variable *var,
size_t width, int num,
oid name[], size_t namelen)
{
struct subtree *tree;
tree = (struct subtree *)malloc(sizeof(struct subtree));
oid_copy (tree->name, name, namelen);
tree->name_len = namelen;
tree->variables = var;
tree->variables_num = num;
tree->variables_width = width;
tree->registered = 0;
listnode_add_sort(treelist, tree);
}
/* Compare function to keep treelist sorted */
static int
smux_tree_cmp(struct subtree *tree1, struct subtree *tree2)
{
return oid_compare(tree1->name, tree1->name_len,
tree2->name, tree2->name_len);
}
/* Initialize some values then schedule first SMUX connection. */
void
smux_init (struct thread_master *tm)
{
assert (tm);
/* copy callers thread master */
smux_master = tm;
/* Make MIB tree. */
treelist = list_new();
treelist->cmp = (int (*)(void *, void *))smux_tree_cmp;
/* Install commands. */
install_node (&smux_node, config_write_smux);
install_element (CONFIG_NODE, &smux_peer_cmd);
install_element (CONFIG_NODE, &smux_peer_password_cmd);
install_element (CONFIG_NODE, &no_smux_peer_cmd);
install_element (CONFIG_NODE, &no_smux_peer_oid_cmd);
install_element (CONFIG_NODE, &no_smux_peer_oid_password_cmd);
}
void
smux_start(void)
{
/* Close any existing connections. */
smux_stop();
/* Schedule first connection. */
smux_event (SMUX_SCHEDULE, 0);
}
#endif /* HAVE_SNMP */
|