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
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
|
/* $OpenBSD: parse.y,v 1.105 2004/05/08 18:04:14 henning Exp $ */
/*
* Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
* Copyright (c) 2001 Markus Friedl. All rights reserved.
* Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
* Copyright (c) 2001 Theo de Raadt. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
%{
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include "bgpd.h"
#include "mrt.h"
#include "session.h"
static struct bgpd_config *conf;
static struct mrt_head *mrtconf;
static struct network_head *netconf;
static struct peer *peer_l, *peer_l_old;
static struct peer *curpeer;
static struct peer *curgroup;
static struct filter_head *filter_l;
static FILE *fin = NULL;
static int lineno = 1;
static int errors = 0;
static int pdebug = 1;
static u_int32_t id;
char *infile;
int yyerror(const char *, ...);
int yyparse(void);
int kw_cmp(const void *, const void *);
int lookup(char *);
int lgetc(FILE *);
int lungetc(int);
int findeol(void);
int yylex(void);
struct peer *alloc_peer(void);
struct peer *new_peer(void);
struct peer *new_group(void);
int add_mrtconfig(enum mrt_type, char *, time_t, struct peer *);
int get_id(struct peer *);
int expand_rule(struct filter_rule *, struct filter_peers *,
struct filter_match *, struct filter_set *);
int str2key(char *, char *, size_t);
TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
struct sym {
TAILQ_ENTRY(sym) entries;
int used;
int persist;
char *nam;
char *val;
};
int symset(const char *, const char *, int);
char *symget(const char *);
int atoul(char *, u_long *);
int getcommunity(char *);
typedef struct {
union {
u_int32_t number;
char *string;
struct bgpd_addr addr;
u_int8_t u8;
struct filter_peers filter_peers;
struct filter_match filter_match;
struct filter_set filter_set;
struct {
struct bgpd_addr prefix;
u_int8_t len;
} prefix;
struct {
u_int8_t enc_alg;
char enc_key[IPSEC_ENC_KEY_LEN];
u_int8_t enc_key_len;
} encspec;
} v;
int lineno;
} YYSTYPE;
%}
%token AS ROUTERID HOLDTIME YMIN LISTEN ON FIBUPDATE
%token GROUP NEIGHBOR NETWORK
%token REMOTEAS DESCR LOCALADDR MULTIHOP PASSIVE MAXPREFIX ANNOUNCE
%token ENFORCE NEIGHBORAS CAPABILITIES
%token DUMP TABLE IN OUT
%token LOG ROUTECOLL
%token TCP MD5SIG PASSWORD KEY
%token ALLOW DENY MATCH
%token QUICK
%token FROM TO ANY
%token PREFIX PREFIXLEN SOURCEAS TRANSITAS COMMUNITY
%token SET LOCALPREF MED NEXTHOP PREPEND PFTABLE
%token ERROR
%token IPSEC ESP AH SPI IKE
%token <v.string> STRING
%type <v.number> number asnumber optnumber yesno inout espah
%type <v.string> string
%type <v.addr> address
%type <v.prefix> prefix addrspec
%type <v.u8> action quick direction
%type <v.filter_peers> filter_peer
%type <v.filter_match> filter_match prefixlenop
%type <v.filter_set> filter_set filter_set_l filter_set_opt
%type <v.u8> unaryop binaryop filter_as
%type <v.encspec> encspec;
%%
grammar : /* empty */
| grammar '\n'
| grammar conf_main '\n'
| grammar varset '\n'
| grammar neighbor '\n'
| grammar group '\n'
| grammar filterrule '\n'
| grammar error '\n' { errors++; }
;
number : STRING {
u_long ulval;
if (atoul($1, &ulval) == -1) {
yyerror("\"%s\" is not a number", $1);
free($1);
YYERROR;
} else
$$ = ulval;
free($1);
}
;
asnumber : number {
if ($1 > USHRT_MAX) {
yyerror("AS too big: max %u", USHRT_MAX);
YYERROR;
}
}
string : string STRING {
if (asprintf(&$$, "%s %s", $1, $2) == -1)
fatal("string: asprintf");
free($1);
free($2);
}
| STRING
;
yesno : STRING {
if (!strcmp($1, "yes"))
$$ = 1;
else if (!strcmp($1, "no"))
$$ = 0;
else {
free($1);
YYERROR;
}
free($1);
}
;
varset : STRING '=' string {
if (conf->opts & BGPD_OPT_VERBOSE)
printf("%s = \"%s\"\n", $1, $3);
if (symset($1, $3, 0) == -1)
fatal("cannot store variable");
free($1);
free($3);
}
;
conf_main : AS asnumber {
conf->as = $2;
}
| ROUTERID address {
if ($2.af != AF_INET) {
yyerror("router-id must be an IPv4 address");
YYERROR;
}
conf->bgpid = $2.v4.s_addr;
}
| HOLDTIME number {
if ($2 < MIN_HOLDTIME) {
yyerror("holdtime must be at least %u",
MIN_HOLDTIME);
YYERROR;
}
conf->holdtime = $2;
}
| HOLDTIME YMIN number {
if ($3 < MIN_HOLDTIME) {
yyerror("holdtime min must be at least %u",
MIN_HOLDTIME);
YYERROR;
}
conf->min_holdtime = $3;
}
| LISTEN ON address {
switch ($3.af) {
case AF_INET:
conf->listen_addr.sin_addr.s_addr =
$3.v4.s_addr;
break;
case AF_INET6:
memcpy(&conf->listen6_addr.sin6_addr, &$3.v6,
sizeof(conf->listen6_addr.sin6_addr));
break;
default:
yyerror("king bula does not like family %u",
$3.af);
YYERROR;
}
}
| FIBUPDATE yesno {
if ($2 == 0)
conf->flags |= BGPD_FLAG_NO_FIB_UPDATE;
else
conf->flags &= ~BGPD_FLAG_NO_FIB_UPDATE;
}
| ROUTECOLL yesno {
if ($2 == 1)
conf->flags |= BGPD_FLAG_NO_EVALUATE;
else
conf->flags &= ~BGPD_FLAG_NO_EVALUATE;
}
| LOG string {
if (!strcmp($2, "updates"))
conf->log |= BGPD_LOG_UPDATES;
else {
free($2);
YYERROR;
}
free($2);
}
| NETWORK prefix filter_set {
struct network *n;
if ($2.prefix.af != AF_INET) {
yyerror("king bula sez: AF_INET only for now");
YYERROR;
}
if ((n = calloc(1, sizeof(struct network))) == NULL)
fatal("new_network");
memcpy(&n->net.prefix, &$2.prefix,
sizeof(n->net.prefix));
n->net.prefixlen = $2.len;
memcpy(&n->net.attrset, &$3,
sizeof(n->net.attrset));
TAILQ_INSERT_TAIL(netconf, n, network_l);
}
| DUMP TABLE STRING optnumber {
if (add_mrtconfig(MRT_TABLE_DUMP, $3, $4, NULL) == -1) {
free($3);
YYERROR;
}
free($3);
}
| mrtdump
;
mrtdump : DUMP STRING inout STRING optnumber {
int action;
if (!strcmp($2, "all"))
action = $3 ? MRT_ALL_IN : MRT_ALL_OUT;
else if (!strcmp($2, "updates"))
action = $3 ? MRT_UPDATE_IN : MRT_UPDATE_OUT;
else {
yyerror("unknown mrt msg dump type");
free($2);
free($4);
YYERROR;
}
if (add_mrtconfig(action, $4, $5, curpeer) == -1) {
free($2);
free($4);
YYERROR;
}
free($2);
free($4);
}
;
inout : IN { $$ = 1; }
| OUT { $$ = 0; }
;
address : STRING {
u_int8_t len;
if (!host($1, &$$, &len)) {
yyerror("could not parse address spec \"%s\"",
$1);
free($1);
YYERROR;
}
free($1);
if (($$.af == AF_INET && len != 32) ||
($$.af == AF_INET6 && len != 128)) {
/* unreachable */
yyerror("got prefixlen %u, expected %u",
len, $$.af == AF_INET ? 32 : 128);
YYERROR;
}
}
;
prefix : STRING '/' number {
char *s;
if (asprintf(&s, "%s/%u", $1, $3) == -1)
fatal(NULL);
if (!host(s, &$$.prefix, &$$.len)) {
yyerror("could not parse address \"%s/%u\"",
$1, $3);
free(s);
free($1);
YYERROR;
}
free($1);
free(s);
}
;
addrspec : address {
memcpy(&$$.prefix, &$1, sizeof(struct bgpd_addr));
if ($$.prefix.af == AF_INET)
$$.len = 32;
else
$$.len = 128;
}
| prefix
;
optnl : '\n' optnl
|
;
nl : '\n' optnl /* one newline or more */
;
optnumber : /* empty */ { $$ = 0; }
| number
;
neighbor : { curpeer = new_peer(); }
NEIGHBOR addrspec {
memcpy(&curpeer->conf.remote_addr, &$3.prefix,
sizeof(curpeer->conf.remote_addr));
curpeer->conf.remote_masklen = $3.len;
if (($3.prefix.af == AF_INET && $3.len != 32) ||
($3.prefix.af == AF_INET6 && $3.len != 128))
curpeer->conf.template = 1;
if (get_id(curpeer)) {
yyerror("get_id failed");
YYERROR;
}
}
peeropts_h {
if (curpeer->conf.local_addr.af &&
curpeer->conf.local_addr.af !=
curpeer->conf.remote_addr.af) {
yyerror("local-address and neighbor address "
"must be of the same address family");
YYERROR;
}
curpeer->next = peer_l;
peer_l = curpeer;
curpeer = curgroup;
}
;
group : GROUP string optnl '{' optnl {
curgroup = curpeer = new_group();
if (strlcpy(curgroup->conf.group, $2,
sizeof(curgroup->conf.group)) >=
sizeof(curgroup->conf.group)) {
yyerror("group name \"%s\" too long: max %u",
$2, sizeof(curgroup->conf.group) - 1);
free($2);
YYERROR;
}
free($2);
if (get_id(curgroup)) {
yyerror("get_id failed");
YYERROR;
}
}
groupopts_l '}' {
free(curgroup);
curgroup = NULL;
}
;
groupopts_l : groupopts_l groupoptsl
| groupoptsl
;
groupoptsl : peeropts nl
| neighbor nl
| error nl
;
peeropts_h : '{' optnl peeropts_l '}'
| /* empty */
;
peeropts_l : peeropts_l peeroptsl
| peeroptsl
;
peeroptsl : peeropts nl
| error nl
;
peeropts : REMOTEAS asnumber {
curpeer->conf.remote_as = $2;
}
| DESCR string {
if (strlcpy(curpeer->conf.descr, $2,
sizeof(curpeer->conf.descr)) >=
sizeof(curpeer->conf.descr)) {
yyerror("descr \"%s\" too long: max %u",
$2, sizeof(curpeer->conf.descr) - 1);
free($2);
YYERROR;
}
free($2);
}
| LOCALADDR address {
memcpy(&curpeer->conf.local_addr, &$2,
sizeof(curpeer->conf.local_addr));
}
| MULTIHOP number {
if ($2 < 2 || $2 > 255) {
yyerror("invalid multihop distance %d", $2);
YYERROR;
}
curpeer->conf.distance = $2;
}
| PASSIVE {
curpeer->conf.passive = 1;
}
| HOLDTIME number {
if ($2 < MIN_HOLDTIME) {
yyerror("holdtime must be at least %u",
MIN_HOLDTIME);
YYERROR;
}
curpeer->conf.holdtime = $2;
}
| HOLDTIME YMIN number {
if ($3 < MIN_HOLDTIME) {
yyerror("holdtime min must be at least %u",
MIN_HOLDTIME);
YYERROR;
}
curpeer->conf.min_holdtime = $3;
}
| ANNOUNCE STRING {
if (!strcmp($2, "self"))
curpeer->conf.announce_type = ANNOUNCE_SELF;
else if (!strcmp($2, "none"))
curpeer->conf.announce_type = ANNOUNCE_NONE;
else if (!strcmp($2, "all"))
curpeer->conf.announce_type = ANNOUNCE_ALL;
else {
free($2);
YYERROR;
}
free($2);
}
| ENFORCE NEIGHBORAS yesno {
if ($3)
curpeer->conf.enforce_as = ENFORCE_AS_ON;
else
curpeer->conf.enforce_as = ENFORCE_AS_OFF;
}
| MAXPREFIX number {
curpeer->conf.max_prefix = $2;
}
| TCP MD5SIG PASSWORD string {
if (curpeer->conf.auth.method) {
yyerror("auth method cannot be redefined");
YYERROR;
}
if (strlcpy(curpeer->conf.auth.md5key, $4,
sizeof(curpeer->conf.auth.md5key)) >=
sizeof(curpeer->conf.auth.md5key)) {
yyerror("tcp md5sig password too long: max %u",
sizeof(curpeer->conf.auth.md5key) - 1);
free($4);
YYERROR;
}
curpeer->conf.auth.method = AUTH_MD5SIG;
curpeer->conf.auth.md5key_len = strlen($4);
free($4);
}
| TCP MD5SIG KEY string {
if (curpeer->conf.auth.method) {
yyerror("auth method cannot be redefined");
YYERROR;
}
if (str2key($4, curpeer->conf.auth.md5key,
sizeof(curpeer->conf.auth.md5key)) == -1) {
free($4);
YYERROR;
}
curpeer->conf.auth.method = AUTH_MD5SIG;
curpeer->conf.auth.md5key_len = strlen($4) / 2;
free($4);
}
| IPSEC espah IKE {
if (curpeer->conf.auth.method) {
yyerror("auth method cannot be redefined");
YYERROR;
}
if ($2)
curpeer->conf.auth.method = AUTH_IPSEC_IKE_ESP;
else
curpeer->conf.auth.method = AUTH_IPSEC_IKE_AH;
}
| IPSEC espah inout SPI number STRING STRING encspec {
u_int32_t auth_alg;
u_int8_t keylen;
if (curpeer->conf.auth.method) {
yyerror("auth method cannot be redefined");
YYERROR;
}
if (!strcmp($6, "sha1")) {
auth_alg = SADB_AALG_SHA1HMAC;
keylen = 20;
} else if (!strcmp($6, "md5")) {
auth_alg = SADB_AALG_MD5HMAC;
keylen = 16;
} else {
yyerror("unknown auth algorithm \"%s\"", $6);
free($7);
free($6);
YYERROR;
}
free($6);
if (strlen($7) / 2 != keylen) {
yyerror("auth key len: must be %u bytes, "
"is %u bytes", keylen, strlen($7) / 2);
free($7);
YYERROR;
}
if ($2)
curpeer->conf.auth.method = AUTH_IPSEC_MANUAL_ESP;
else {
if ($8.enc_alg) {
yyerror("\"ipsec ah\" doesn't take encryption keys");
free($7);
YYERROR;
}
curpeer->conf.auth.method = AUTH_IPSEC_MANUAL_AH;
}
if ($3 == 1) {
if (str2key($7, curpeer->conf.auth.auth_key_in,
sizeof(curpeer->conf.auth.auth_key_in)) ==
-1) {
free($7);
YYERROR;
}
curpeer->conf.auth.spi_in = $5;
curpeer->conf.auth.auth_alg_in = auth_alg;
curpeer->conf.auth.enc_alg_in = $8.enc_alg;
memcpy(&curpeer->conf.auth.enc_key_in,
&$8.enc_key,
sizeof(curpeer->conf.auth.enc_key_in));
curpeer->conf.auth.enc_keylen_in =
$8.enc_key_len;
curpeer->conf.auth.auth_keylen_in = keylen;
} else {
if (str2key($7, curpeer->conf.auth.auth_key_out,
sizeof(curpeer->conf.auth.auth_key_out)) ==
-1) {
free($7);
YYERROR;
}
curpeer->conf.auth.spi_out = $5;
curpeer->conf.auth.auth_alg_out = auth_alg;
curpeer->conf.auth.enc_alg_out = $8.enc_alg;
memcpy(&curpeer->conf.auth.enc_key_out,
&$8.enc_key,
sizeof(curpeer->conf.auth.enc_key_out));
curpeer->conf.auth.enc_keylen_out =
$8.enc_key_len;
curpeer->conf.auth.auth_keylen_out = keylen;
}
free($7);
}
| ANNOUNCE CAPABILITIES yesno {
curpeer->conf.capabilities = $3;
}
| SET filter_set_opt {
memcpy(&curpeer->conf.attrset, &$2,
sizeof(curpeer->conf.attrset));
}
| SET optnl "{" optnl filter_set_l optnl "}" {
memcpy(&curpeer->conf.attrset, &$5,
sizeof(curpeer->conf.attrset));
}
| mrtdump
;
espah : ESP { $$ = 1; }
| AH { $$ = 0; }
;
encspec : /* nada */ {
bzero(&$$, sizeof($$));
}
| STRING STRING {
bzero(&$$, sizeof($$));
if (!strcmp($1, "3des") || !strcmp($1, "3des-cbc")) {
$$.enc_alg = SADB_EALG_3DESCBC;
$$.enc_key_len = 21; /* XXX verify */
} else if (!strcmp($1, "aes") ||
!strcmp($1, "aes-128-cbc")) {
$$.enc_alg = SADB_X_EALG_AES;
$$.enc_key_len = 16;
} else {
yyerror("unknown enc algorithm \"%s\"", $1);
free($1);
free($2);
YYERROR;
}
free($1);
if (strlen($2) / 2 != $$.enc_key_len) {
yyerror("enc key length wrong: should be %u "
"bytes, is %u bytes",
$$.enc_key_len * 2, strlen($2));
free($2);
YYERROR;
}
if (str2key($2, $$.enc_key, sizeof($$.enc_key)) == -1) {
free($2);
YYERROR;
}
free($2);
}
;
filterrule : action quick direction filter_peer filter_match filter_set
{
struct filter_rule r;
if ($5.prefix.addr.af && $5.prefix.addr.af != AF_INET) {
yyerror("king bula sez: AF_INET only for now");
YYERROR;
}
bzero(&r, sizeof(r));
r.action = $1;
r.quick = $2;
r.dir = $3;
if (expand_rule(&r, &$4, &$5, &$6) == -1)
YYERROR;
}
;
action : ALLOW { $$ = ACTION_ALLOW; }
| DENY { $$ = ACTION_DENY; }
| MATCH { $$ = ACTION_NONE; }
;
quick : /* empty */ { $$ = 0; }
| QUICK { $$ = 1; }
;
direction : FROM { $$ = DIR_IN; }
| TO { $$ = DIR_OUT; }
;
filter_peer : ANY { $$.peerid = $$.groupid = 0; }
| address {
struct peer *p;
$$.groupid = $$.peerid = 0;
for (p = peer_l; p != NULL; p = p->next)
if (!memcmp(&p->conf.remote_addr,
&$1, sizeof(p->conf.remote_addr))) {
$$.peerid = p->conf.id;
break;
}
if ($$.peerid == 0) {
yyerror("no such peer: %s", log_addr(&$1));
YYERROR;
}
}
| GROUP string {
struct peer *p;
$$.peerid = 0;
for (p = peer_l; p != NULL; p = p->next)
if (!strcmp(p->conf.group, $2)) {
$$.groupid = p->conf.groupid;
break;
}
if ($$.groupid == 0) {
yyerror("no such group: \"%s\"", $2);
free($2);
YYERROR;
}
free($2);
}
;
filter_match : /* empty */ { bzero(&$$, sizeof($$)); }
| PREFIX prefix {
bzero(&$$, sizeof($$));
memcpy(&$$.prefix.addr, &$2.prefix,
sizeof($$.prefix.addr));
$$.prefix.len = $2.len;
}
| PREFIX prefix PREFIXLEN prefixlenop {
bzero(&$$, sizeof($$));
memcpy(&$$.prefix.addr, &$2.prefix,
sizeof($$.prefix.addr));
$$.prefix.len = $2.len;
$$.prefixlen = $4.prefixlen;
$$.prefixlen.af = $2.prefix.af;
if ($$.prefixlen.af == AF_INET)
if ($$.prefixlen.len_max > 32 ||
$$.prefixlen.len_min > 32) {
yyerror("prefixlen must be <= 32");
YYERROR;
}
if ($$.prefixlen.af == AF_INET6)
if ($$.prefixlen.len_max > 128 ||
$$.prefixlen.len_min > 128) {
yyerror("prefixlen must be <= 128");
YYERROR;
}
}
| PREFIXLEN prefixlenop {
bzero(&$$, sizeof($$));
$$.prefixlen = $2.prefixlen;
$$.prefixlen.af = AF_INET;
}
| filter_as asnumber {
bzero(&$$, sizeof($$));
$$.as.as = $2;
$$.as.type = $1;
}
| COMMUNITY STRING {
char *p;
int i;
bzero(&$$, sizeof($$));
if ((p = strchr($2, ':')) == NULL) {
free($2);
yyerror("Bad community syntax");
YYERROR;
}
*p++ = 0;
if ((i = getcommunity($2)) == COMMUNITY_ERROR) {
free($2);
YYERROR;
}
if (i == 0 || i == USHRT_MAX) {
free($2);
yyerror("Bad community AS number");
YYERROR;
}
$$.community.as = i;
if ((i = getcommunity(p)) == COMMUNITY_ERROR) {
free($2);
YYERROR;
}
free($2);
$$.community.type = i;
}
;
prefixlenop : unaryop number {
bzero(&$$, sizeof($$));
if ($2 > 128) {
yyerror("prefixlen must be < 128");
YYERROR;
}
$$.prefixlen.op = $1;
$$.prefixlen.len_min = $2;
}
| number binaryop number {
bzero(&$$, sizeof($$));
if ($1 > 128 || $3 > 128) {
yyerror("prefixlen must be < 128");
YYERROR;
}
if ($1 >= $3) {
yyerror("start prefixlen is bigger that end");
YYERROR;
}
$$.prefixlen.op = $2;
$$.prefixlen.len_min = $1;
$$.prefixlen.len_max = $3;
}
;
filter_as : AS { $$ = AS_ALL; }
| SOURCEAS { $$ = AS_SOURCE; }
| TRANSITAS { $$ = AS_TRANSIT; }
;
filter_set : /* empty */ {
bzero(&$$, sizeof($$));
}
| SET filter_set_opt { $$ = $2; }
| SET optnl "{" optnl filter_set_l optnl "}" { $$ = $5; }
;
filter_set_l : filter_set_l comma filter_set_opt {
$$ = $1;
if ($$.flags & $3.flags) {
yyerror("redefining set shitz is not fluffy");
YYERROR;
}
$$.flags |= $3.flags;
if ($3.flags & SET_LOCALPREF)
$$.localpref = $3.localpref;
if ($3.flags & SET_MED)
$$.med = $3.med;
if ($3.flags & SET_NEXTHOP)
memcpy(&$$.nexthop, &$3.nexthop,
sizeof($$.nexthop));
if ($3.flags & SET_PREPEND)
$$.prepend = $3.prepend;
if ($3.flags & SET_PFTABLE)
strlcpy($$.pftable, $3.pftable,
sizeof($$.pftable));
}
| filter_set_opt
;
filter_set_opt : LOCALPREF number {
$$.flags = SET_LOCALPREF;
$$.localpref = $2;
}
| MED number {
$$.flags = SET_MED;
$$.med = $2;
}
| NEXTHOP address {
if ($2.af == AF_INET) {
$$.flags = SET_NEXTHOP;
$$.nexthop.s_addr = $2.v4.s_addr;
} else {
yyerror("king bula sez: AF_INET only for now");
YYERROR;
}
}
| PREPEND number {
$$.flags = SET_PREPEND;
$$.prepend = $2;
}
| PFTABLE string {
$$.flags = SET_PFTABLE;
if (pftable_exists($2) != 0) {
yyerror("pftable name does not exist");
YYERROR;
}
if (strlcpy($$.pftable, $2, sizeof($$.pftable)) >=
sizeof($$.pftable)) {
yyerror("pftable name too long");
YYERROR;
}
if (pftable_add($2) != 0) {
yyerror("Couldn't register table");
YYERROR;
}
}
;
comma : ","
| /* empty */
;
unaryop : '=' { $$ = OP_EQ; }
| '!' '=' { $$ = OP_NE; }
| '<' '=' { $$ = OP_LE; }
| '<' { $$ = OP_LT; }
| '>' '=' { $$ = OP_GE; }
| '>' { $$ = OP_GT; }
;
binaryop : '-' { $$ = OP_RANGE; }
| '>' '<' { $$ = OP_XRANGE; }
;
%%
struct keywords {
const char *k_name;
int k_val;
};
int
yyerror(const char *fmt, ...)
{
va_list ap;
char *nfmt;
errors = 1;
va_start(ap, fmt);
if (asprintf(&nfmt, "%s:%d: %s", infile, yylval.lineno, fmt) == -1)
fatalx("yyerror asprintf");
vlog(LOG_CRIT, nfmt, ap);
va_end(ap);
free(nfmt);
return (0);
}
int
kw_cmp(const void *k, const void *e)
{
return (strcmp(k, ((const struct keywords *)e)->k_name));
}
int
lookup(char *s)
{
/* this has to be sorted always */
static const struct keywords keywords[] = {
{ "AS", AS},
{ "ah", AH},
{ "allow", ALLOW},
{ "announce", ANNOUNCE},
{ "any", ANY},
{ "capabilities", CAPABILITIES},
{ "community", COMMUNITY},
{ "deny", DENY},
{ "descr", DESCR},
{ "dump", DUMP},
{ "enforce", ENFORCE},
{ "esp", ESP},
{ "fib-update", FIBUPDATE},
{ "from", FROM},
{ "group", GROUP},
{ "holdtime", HOLDTIME},
{ "ike", IKE},
{ "in", IN},
{ "ipsec", IPSEC},
{ "key", KEY},
{ "listen", LISTEN},
{ "local-address", LOCALADDR},
{ "localpref", LOCALPREF},
{ "log", LOG},
{ "match", MATCH},
{ "max-prefix", MAXPREFIX},
{ "md5sig", MD5SIG},
{ "med", MED},
{ "min", YMIN},
{ "multihop", MULTIHOP},
{ "neighbor", NEIGHBOR},
{ "neighbor-as", NEIGHBORAS},
{ "network", NETWORK},
{ "nexthop", NEXTHOP},
{ "on", ON},
{ "out", OUT},
{ "passive", PASSIVE},
{ "password", PASSWORD},
{ "pftable", PFTABLE},
{ "prefix", PREFIX},
{ "prefixlen", PREFIXLEN},
{ "prepend-self", PREPEND},
{ "quick", QUICK},
{ "remote-as", REMOTEAS},
{ "route-collector", ROUTECOLL},
{ "router-id", ROUTERID},
{ "set", SET},
{ "source-AS", SOURCEAS},
{ "spi", SPI},
{ "table", TABLE},
{ "tcp", TCP},
{ "to", TO},
{ "transit-AS", TRANSITAS}
};
const struct keywords *p;
p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
sizeof(keywords[0]), kw_cmp);
if (p) {
if (pdebug > 1)
fprintf(stderr, "%s: %d\n", s, p->k_val);
return (p->k_val);
} else {
if (pdebug > 1)
fprintf(stderr, "string: %s\n", s);
return (STRING);
}
}
#define MAXPUSHBACK 128
char *parsebuf;
int parseindex;
char pushback_buffer[MAXPUSHBACK];
int pushback_index = 0;
int
lgetc(FILE *f)
{
int c, next;
if (parsebuf) {
/* Read character from the parsebuffer instead of input. */
if (parseindex >= 0) {
c = parsebuf[parseindex++];
if (c != '\0')
return (c);
parsebuf = NULL;
} else
parseindex++;
}
if (pushback_index)
return (pushback_buffer[--pushback_index]);
while ((c = getc(f)) == '\\') {
next = getc(f);
if (next != '\n') {
if (isspace(next))
yyerror("whitespace after \\");
ungetc(next, f);
break;
}
yylval.lineno = lineno;
lineno++;
}
if (c == '\t' || c == ' ') {
/* Compress blanks to a single space. */
do {
c = getc(f);
} while (c == '\t' || c == ' ');
ungetc(c, f);
c = ' ';
}
return (c);
}
int
lungetc(int c)
{
if (c == EOF)
return (EOF);
if (parsebuf) {
parseindex--;
if (parseindex >= 0)
return (c);
}
if (pushback_index < MAXPUSHBACK-1)
return (pushback_buffer[pushback_index++] = c);
else
return (EOF);
}
int
findeol(void)
{
int c;
parsebuf = NULL;
pushback_index = 0;
/* skip to either EOF or the first real EOL */
while (1) {
c = lgetc(fin);
if (c == '\n') {
lineno++;
break;
}
if (c == EOF)
break;
}
return (ERROR);
}
int
yylex(void)
{
char buf[8096];
char *p, *val;
int endc, c;
int token;
top:
p = buf;
while ((c = lgetc(fin)) == ' ')
; /* nothing */
yylval.lineno = lineno;
if (c == '#')
while ((c = lgetc(fin)) != '\n' && c != EOF)
; /* nothing */
if (c == '$' && parsebuf == NULL) {
while (1) {
if ((c = lgetc(fin)) == EOF)
return (0);
if (p + 1 >= buf + sizeof(buf) - 1) {
yyerror("string too long");
return (findeol());
}
if (isalnum(c) || c == '_') {
*p++ = (char)c;
continue;
}
*p = '\0';
lungetc(c);
break;
}
val = symget(buf);
if (val == NULL) {
yyerror("macro \"%s\" not defined", buf);
return (findeol());
}
parsebuf = val;
parseindex = 0;
goto top;
}
switch (c) {
case '\'':
case '"':
endc = c;
while (1) {
if ((c = lgetc(fin)) == EOF)
return (0);
if (c == endc) {
*p = '\0';
break;
}
if (c == '\n') {
lineno++;
continue;
}
if (p + 1 >= buf + sizeof(buf) - 1) {
yyerror("string too long");
return (findeol());
}
*p++ = (char)c;
}
yylval.v.string = strdup(buf);
if (yylval.v.string == NULL)
fatal("yylex: strdup");
return (STRING);
}
#define allowed_in_string(x) \
(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
x != '{' && x != '}' && x != '<' && x != '>' && \
x != '!' && x != '=' && x != '/' && x != '#' && \
x != ','))
if (isalnum(c) || c == ':' || c == '_' || c == '*') {
do {
*p++ = c;
if ((unsigned)(p-buf) >= sizeof(buf)) {
yyerror("string too long");
return (findeol());
}
} while ((c = lgetc(fin)) != EOF && (allowed_in_string(c)));
lungetc(c);
*p = '\0';
if ((token = lookup(buf)) == STRING)
if ((yylval.v.string = strdup(buf)) == NULL)
fatal("yylex: strdup");
return (token);
}
if (c == '\n') {
yylval.lineno = lineno;
lineno++;
}
if (c == EOF)
return (0);
return (c);
}
int
parse_config(char *filename, struct bgpd_config *xconf,
struct mrt_head *xmconf, struct peer **xpeers, struct network_head *nc,
struct filter_head *xfilter_l)
{
struct sym *sym, *next;
struct peer *p, *pnext;
if ((conf = calloc(1, sizeof(struct bgpd_config))) == NULL)
fatal(NULL);
if ((mrtconf = calloc(1, sizeof(struct mrt_head))) == NULL)
fatal(NULL);
LIST_INIT(mrtconf);
netconf = nc;
TAILQ_INIT(netconf);
peer_l = NULL;
peer_l_old = *xpeers;
curpeer = NULL;
curgroup = NULL;
lineno = 1;
errors = 0;
id = 1;
filter_l = xfilter_l;
TAILQ_INIT(filter_l);
conf->listen_addr.sin_len = sizeof(conf->listen_addr);
conf->listen_addr.sin_family = AF_INET;
conf->listen_addr.sin_addr.s_addr = INADDR_ANY;
conf->listen_addr.sin_port = htons(BGP_PORT);
bzero(&conf->listen6_addr, sizeof(conf->listen6_addr));
conf->listen6_addr.sin6_len = sizeof(conf->listen6_addr);
conf->listen6_addr.sin6_family = AF_INET6;
conf->listen6_addr.sin6_port = htons(BGP_PORT);
if ((fin = fopen(filename, "r")) == NULL) {
warn("%s", filename);
free(conf);
free(mrtconf);
return (-1);
}
infile = filename;
if (check_file_secrecy(fileno(fin), filename)) {
fclose(fin);
free(conf);
free(mrtconf);
return (-1);
}
yyparse();
fclose(fin);
/* Free macros and check which have not been used. */
for (sym = TAILQ_FIRST(&symhead); sym != NULL; sym = next) {
next = TAILQ_NEXT(sym, entries);
if ((conf->opts & BGPD_OPT_VERBOSE2) && !sym->used)
fprintf(stderr, "warning: macro \"%s\" not "
"used\n", sym->nam);
if (!sym->persist) {
free(sym->nam);
free(sym->val);
TAILQ_REMOVE(&symhead, sym, entries);
free(sym);
}
}
errors += merge_config(xconf, conf, peer_l);
errors += mrt_mergeconfig(xmconf, mrtconf);
*xpeers = peer_l;
for (p = peer_l_old; p != NULL; p = pnext) {
pnext = p->next;
free(p);
}
free(conf);
free(mrtconf);
return (errors ? -1 : 0);
}
int
symset(const char *nam, const char *val, int persist)
{
struct sym *sym;
for (sym = TAILQ_FIRST(&symhead); sym && strcmp(nam, sym->nam);
sym = TAILQ_NEXT(sym, entries))
; /* nothing */
if (sym != NULL) {
if (sym->persist == 1)
return (0);
else {
free(sym->nam);
free(sym->val);
TAILQ_REMOVE(&symhead, sym, entries);
free(sym);
}
}
if ((sym = calloc(1, sizeof(*sym))) == NULL)
return (-1);
sym->nam = strdup(nam);
if (sym->nam == NULL) {
free(sym);
return (-1);
}
sym->val = strdup(val);
if (sym->val == NULL) {
free(sym->nam);
free(sym);
return (-1);
}
sym->used = 0;
sym->persist = persist;
TAILQ_INSERT_TAIL(&symhead, sym, entries);
return (0);
}
int
cmdline_symset(char *s)
{
char *sym, *val;
int ret;
size_t len;
if ((val = strrchr(s, '=')) == NULL)
return (-1);
len = strlen(s) - strlen(val) + 1;
if ((sym = malloc(len)) == NULL)
fatal("cmdline_symset: malloc");
strlcpy(sym, s, len);
ret = symset(sym, val + 1, 1);
free(sym);
return (ret);
}
char *
symget(const char *nam)
{
struct sym *sym;
TAILQ_FOREACH(sym, &symhead, entries)
if (strcmp(nam, sym->nam) == 0) {
sym->used = 1;
return (sym->val);
}
return (NULL);
}
int
atoul(char *s, u_long *ulvalp)
{
u_long ulval;
char *ep;
errno = 0;
ulval = strtoul(s, &ep, 0);
if (s[0] == '\0' || *ep != '\0')
return (-1);
if (errno == ERANGE && ulval == ULONG_MAX)
return (-1);
*ulvalp = ulval;
return (0);
}
int
getcommunity(char *s)
{
u_long ulval;
if (strcmp(s, "*") == 0)
return (COMMUNITY_ANY);
if (atoul(s, &ulval) == -1) {
yyerror("\"%s\" is not a number", s);
return (COMMUNITY_ERROR);
}
if (ulval > USHRT_MAX) {
yyerror("Community too big: max %u", USHRT_MAX);
return (COMMUNITY_ERROR);
}
return (ulval);
}
struct peer *
alloc_peer(void)
{
struct peer *p;
if ((p = calloc(1, sizeof(struct peer))) == NULL)
fatal("new_peer");
/* some sane defaults */
p->state = STATE_NONE;
p->next = NULL;
p->conf.distance = 1;
p->conf.announce_type = ANNOUNCE_UNDEF;
p->conf.capabilities = 1;
return (p);
}
struct peer *
new_peer(void)
{
struct peer *p;
p = alloc_peer();
if (curgroup != NULL) {
memcpy(p, curgroup, sizeof(struct peer));
if (strlcpy(p->conf.group, curgroup->conf.group,
sizeof(p->conf.group)) >= sizeof(p->conf.group))
fatalx("new_peer group strlcpy");
if (strlcpy(p->conf.descr, curgroup->conf.descr,
sizeof(p->conf.descr)) >= sizeof(p->conf.descr))
fatalx("new_peer descr strlcpy");
p->conf.groupid = curgroup->conf.id;
}
p->next = NULL;
return (p);
}
struct peer *
new_group(void)
{
return (alloc_peer());
}
int
add_mrtconfig(enum mrt_type type, char *name, time_t timeout, struct peer *p)
{
struct mrt *m, *n;
LIST_FOREACH(m, mrtconf, list) {
if (p == NULL) {
if (m->conf.peer_id != 0 || m->conf.group_id != 0)
continue;
} else {
if (m->conf.peer_id != p->conf.id ||
m->conf.group_id != p->conf.groupid)
continue;
}
if (m->conf.type == type) {
yyerror("only one mrtdump per type allowed.");
return (-1);
}
}
if ((n = calloc(1, sizeof(struct mrt))) == NULL)
fatal("add_mrtconfig");
n->conf.type = type;
n->msgbuf.fd = -1;
if (strlcpy(n->name, name, sizeof(n->name)) >= sizeof(n->name)) {
yyerror("filename \"%s\" too long: max %u",
name, sizeof(n->name) - 1);
free(n);
return (-1);
}
n->ReopenTimerInterval = timeout;
if (p != NULL) {
if (curgroup == p) {
n->conf.peer_id = 0;
n->conf.group_id = p->conf.id;
} else {
n->conf.peer_id = p->conf.id;
n->conf.group_id = 0;
}
}
LIST_INSERT_HEAD(mrtconf, n, list);
return (0);
}
int
get_id(struct peer *newpeer)
{
struct peer *p;
if (newpeer->conf.remote_addr.af)
for (p = peer_l_old; p != NULL; p = p->next)
if (!memcmp(&p->conf.remote_addr,
&newpeer->conf.remote_addr,
sizeof(p->conf.remote_addr))) {
newpeer->conf.id = p->conf.id;
return (0);
}
/* new one */
for (; id < UINT_MAX / 2; id++) {
for (p = peer_l_old; p != NULL && p->conf.id != id; p = p->next)
; /* nothing */
if (p == NULL) { /* we found a free id */
newpeer->conf.id = id++;
return (0);
}
}
return (-1);
}
int
expand_rule(struct filter_rule *rule, struct filter_peers *peer,
struct filter_match *match, struct filter_set *set)
{
struct filter_rule *r;
if ((r = calloc(1, sizeof(struct filter_rule))) == NULL) {
log_warn("expand_rule");
return (-1);
}
memcpy(r, rule, sizeof(struct filter_rule));
memcpy(&r->peer, peer, sizeof(struct filter_peers));
memcpy(&r->match, match, sizeof(struct filter_match));
memcpy(&r->set, set, sizeof(struct filter_set));
TAILQ_INSERT_TAIL(filter_l, r, entries);
return (0);
}
int
str2key(char *s, char *dest, size_t max_len)
{
unsigned i;
char t[3];
if (strlen(s) / 2 >= max_len) {
yyerror("key too long");
return (-1);
}
if (strlen(s) % 2) {
yyerror("key must be of even length");
return (-1);
}
for (i = 0; i < strlen(s) / 2; i++) {
t[0] = s[2*i];
t[1] = s[2*i + 1];
t[2] = 0;
if (!isxdigit(t[0]) || !isxdigit(t[1])) {
yyerror("key must be specified in hex");
return (-1);
}
dest[i] = strtoul(t, NULL, 16);
}
return (0);
}
|