0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

Merge commit 'fc354788' into thread-next

This commit is contained in:
Maria Matejka 2023-10-13 11:32:53 +02:00
commit e7acdf6428
13 changed files with 180 additions and 64 deletions

View File

@ -34,6 +34,7 @@ class BIRDFValPrinter(BIRDPrinter):
"T_IP": "ip", "T_IP": "ip",
"T_NET": "net", "T_NET": "net",
"T_STRING": "s", "T_STRING": "s",
"T_BYTESTRING": "bs",
"T_PATH_MASK": "path_mask", "T_PATH_MASK": "path_mask",
"T_PATH": "ad", "T_PATH": "ad",
"T_CLIST": "ad", "T_CLIST": "ad",

View File

@ -259,39 +259,23 @@ WHITE [ \t]
} }
({XIGIT}{2}){16,}|{XIGIT}{2}(:{XIGIT}{2}){15,}|hex:({XIGIT}{2}(:?{XIGIT}{2})*)? { ({XIGIT}{2}){16,}|{XIGIT}{2}(:{XIGIT}{2}){15,}|hex:({XIGIT}{2}(:?{XIGIT}{2})*)? {
char *s, *sb = yytext; char *s = yytext;
size_t len = 0, i; struct bytestring *bs;
struct bytestring *bytes;
byte *b;
/* skip 'hex:' prefix */ /* Skip 'hex:' prefix */
if (sb[0] == 'h' && sb[1] == 'e' && sb[2] == 'x' && sb[3] == ':') if (s[0] == 'h' && s[1] == 'e' && s[2] == 'x' && s[3] == ':')
sb += 4; s += 4;
s = sb; int len = bstrhextobin(s, NULL);
while (*s) { if (len < 0)
len++;
s += 2;
if (*s == ':')
s++;
}
bytes = cfg_allocz(sizeof(*bytes) + len);
bytes->length = len;
b = &bytes->data[0];
s = sb;
errno = 0;
for (i = 0; i < len; i++) {
*b = bstrtobyte16(s);
if (errno == ERANGE)
cf_error("Invalid hex string"); cf_error("Invalid hex string");
b++;
s += 2; bs = cfg_allocz(sizeof(struct bytestring) + len);
if (*s == ':') bs->length = bstrhextobin(s, bs->data);
s++; ASSERT(bs->length == len);
}
cf_lval.bs = bytes; cf_lval.bs = bs;
return BYTESTRING; return BYTETEXT;
} }
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) { ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {

View File

@ -108,7 +108,7 @@ CF_DECLS
%token <i64> VPN_RD %token <i64> VPN_RD
%token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED %token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED
%token <t> TEXT %token <t> TEXT
%token <bs> BYTESTRING %token <bs> BYTETEXT
%type <iface> ipa_scope %type <iface> ipa_scope
%type <i> expr bool pxlen4 %type <i> expr bool pxlen4

View File

@ -315,7 +315,7 @@ CF_DECLS
CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
ACCEPT, REJECT, ERROR, ACCEPT, REJECT, ERROR,
INT, BOOL, IP, TYPE, PREFIX, RD, PAIR, QUAD, EC, LC, INT, BOOL, IP, TYPE, PREFIX, RD, PAIR, QUAD, EC, LC,
SET, STRING, BGPMASK, BGPPATH, CLIST, ECLIST, LCLIST, SET, STRING, BYTESTRING, BGPMASK, BGPPATH, CLIST, ECLIST, LCLIST,
IF, THEN, ELSE, CASE, IF, THEN, ELSE, CASE,
FOR, IN, DO, FOR, IN, DO,
TRUE, FALSE, RT, RO, UNKNOWN, GENERIC, TRUE, FALSE, RT, RO, UNKNOWN, GENERIC,
@ -330,13 +330,14 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
MIN, MAX, MIN, MAX,
EMPTY, EMPTY,
FILTER, WHERE, EVAL, ATTRIBUTE, FILTER, WHERE, EVAL, ATTRIBUTE,
FROM_HEX,
BT_ASSERT, BT_TEST_SUITE, BT_CHECK_ASSIGN, BT_TEST_SAME, FORMAT, STACKS) BT_ASSERT, BT_TEST_SUITE, BT_CHECK_ASSIGN, BT_TEST_SAME, FORMAT, STACKS)
%nonassoc THEN %nonassoc THEN
%nonassoc ELSE %nonassoc ELSE
%type <xp> cmds_int cmd_prep %type <xp> cmds_int cmd_prep
%type <x> term cmd cmd_var cmds cmds_scoped constant constructor print_list var var_init var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail %type <x> term term_bs cmd cmd_var cmds cmds_scoped constant constructor print_list var var_init var_list function_call symbol_value bgp_path_expr bgp_path bgp_path_tail
%type <fsa> static_attr %type <fsa> static_attr
%type <f> filter where_filter %type <f> filter where_filter
%type <fl> filter_body function_body %type <fl> filter_body function_body
@ -429,6 +430,7 @@ type:
| EC { $$ = T_EC; } | EC { $$ = T_EC; }
| LC { $$ = T_LC; } | LC { $$ = T_LC; }
| STRING { $$ = T_STRING; } | STRING { $$ = T_STRING; }
| BYTESTRING { $$ = T_BYTESTRING; }
| BGPMASK { $$ = T_PATH_MASK; } | BGPMASK { $$ = T_PATH_MASK; }
| BGPPATH { $$ = T_PATH; } | BGPPATH { $$ = T_PATH; }
| CLIST { $$ = T_CLIST; } | CLIST { $$ = T_CLIST; }
@ -743,6 +745,7 @@ constant:
| TRUE { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_BOOL, .val.i = 1, }); } | TRUE { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_BOOL, .val.i = 1, }); }
| FALSE { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_BOOL, .val.i = 0, }); } | FALSE { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_BOOL, .val.i = 0, }); }
| TEXT { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_STRING, .val.s = $1, }); } | TEXT { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_STRING, .val.s = $1, }); }
| BYTETEXT { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_BYTESTRING, .val.bs = $1, }); }
| fipa { $$ = f_new_inst(FI_CONSTANT, $1); } | fipa { $$ = f_new_inst(FI_CONSTANT, $1); }
| VPN_RD { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_RD, .val.ec = $1, }); } | VPN_RD { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_RD, .val.ec = $1, }); }
| net_ { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_NET, .val.net = $1, }); } | net_ { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_NET, .val.net = $1, }); }
@ -886,9 +889,14 @@ term:
| FORMAT '(' term ')' { $$ = f_new_inst(FI_FORMAT, $3); } | FORMAT '(' term ')' { $$ = f_new_inst(FI_FORMAT, $3); }
| term_bs
| function_call | function_call
; ;
term_bs:
FROM_HEX '(' term ')' { $$ = f_new_inst(FI_FROM_HEX, $3); }
;
break_command: break_command:
ACCEPT { $$ = F_ACCEPT; } ACCEPT { $$ = F_ACCEPT; }
| REJECT { $$ = F_REJECT; } | REJECT { $$ = F_REJECT; }

View File

@ -47,6 +47,7 @@ static const char * const f_type_str[] = {
[T_IP] = "ip", [T_IP] = "ip",
[T_NET] = "prefix", [T_NET] = "prefix",
[T_STRING] = "string", [T_STRING] = "string",
[T_BYTESTRING] = "bytestring",
[T_PATH_MASK] = "bgpmask", [T_PATH_MASK] = "bgpmask",
[T_PATH_MASK_ITEM] = "bgpmask item", [T_PATH_MASK_ITEM] = "bgpmask item",
[T_PATH] = "bgppath", [T_PATH] = "bgppath",
@ -213,6 +214,12 @@ val_compare(const struct f_val *v1, const struct f_val *v2)
} }
} }
static inline int
bs_same(const struct bytestring *bs1, const struct bytestring *bs2)
{
return (bs1->length == bs2->length) && !memcmp(bs1->data, bs2->data, bs1->length);
}
static inline int static inline int
pmi_same(const struct f_path_mask_item *mi1, const struct f_path_mask_item *mi2) pmi_same(const struct f_path_mask_item *mi1, const struct f_path_mask_item *mi2)
{ {
@ -277,6 +284,8 @@ val_same(const struct f_val *v1, const struct f_val *v2)
return 0; return 0;
switch (v1->type) { switch (v1->type) {
case T_BYTESTRING:
return bs_same(v1->val.bs, v2->val.bs);
case T_PATH_MASK: case T_PATH_MASK:
return pm_same(v1->val.path_mask, v2->val.path_mask); return pm_same(v1->val.path_mask, v2->val.path_mask);
case T_PATH_MASK_ITEM: case T_PATH_MASK_ITEM:
@ -576,6 +585,7 @@ val_format(const struct f_val *v, buffer *buf)
case T_BOOL: buffer_puts(buf, v->val.i ? "TRUE" : "FALSE"); return; case T_BOOL: buffer_puts(buf, v->val.i ? "TRUE" : "FALSE"); return;
case T_INT: buffer_print(buf, "%u", v->val.i); return; case T_INT: buffer_print(buf, "%u", v->val.i); return;
case T_STRING: buffer_print(buf, "%s", v->val.s); return; case T_STRING: buffer_print(buf, "%s", v->val.s); return;
case T_BYTESTRING: bstrbintohex(v->val.bs->data, v->val.bs->length, buf2, 1000, ':'); buffer_print(buf, "%s", buf2); return;
case T_IP: buffer_print(buf, "%I", v->val.ip); return; case T_IP: buffer_print(buf, "%I", v->val.ip); return;
case T_NET: buffer_print(buf, "%N", v->val.net); return; case T_NET: buffer_print(buf, "%N", v->val.net); return;
case T_PAIR: buffer_print(buf, "(%u,%u)", v->val.i >> 16, v->val.i & 0xffff); return; case T_PAIR: buffer_print(buf, "(%u,%u)", v->val.i >> 16, v->val.i & 0xffff); return;

View File

@ -1500,6 +1500,21 @@
} }
INST(FI_FROM_HEX, 1, 1) { /* Convert hex text to bytestring */
ARG(1, T_STRING);
int len = bstrhextobin(v1.val.s, NULL);
if (len < 0)
runtime("Invalid hex string");
struct bytestring *bs;
bs = falloc(sizeof(struct bytestring) + len);
bs->length = bstrhextobin(v1.val.s, bs->data);
ASSERT(bs->length == (size_t) len);
RESULT(T_BYTESTRING, bs, bs);
}
INST(FI_FORMAT, 1, 1) { /* Format */ INST(FI_FORMAT, 1, 1) { /* Format */
ARG_ANY(1); ARG_ANY(1);
RESULT(T_STRING, s, val_format_str(fpool, &v1)); RESULT(T_STRING, s, val_format_str(fpool, &v1));

View File

@ -19,6 +19,7 @@
#include "filter/data.h" #include "filter/data.h"
#include "lib/buffer.h" #include "lib/buffer.h"
#include "lib/flowspec.h" #include "lib/flowspec.h"
#include "lib/string.h"
/* Flags for instructions */ /* Flags for instructions */
enum f_instruction_flags { enum f_instruction_flags {

View File

@ -347,6 +347,36 @@ bt_test_suite(t_string, "Testing string matching");
/*
* Testing bytestings
* ------------------
*/
function t_bytestring()
{
bytestring bs1 = hex:;
bytestring bs2 = hex:0112233445566778899aabbccddeeff0;
bt_assert(format(bs1) = "");
bt_assert(format(bs2) = "01:12:23:34:45:56:67:78:89:9a:ab:bc:cd:de:ef:f0");
bt_assert(hex:01:12:23:34:45:56:67:78:89:9a:ab:bc:cd:de:ef:f0 = bs2);
bt_assert(01:12:23:34:45:56:67:78:89:9a:ab:bc:cd:de:ef:f0 = bs2);
bt_assert(0112233445566778899aabbccddeeff0 = bs2);
bt_assert(hex:01234567 = hex:01:23:45:67);
bt_assert(hex:0123456789abcdef != bs2);
bt_assert(hex:0123456789abcdef != hex:0123);
bt_assert(format(hex:0123456789abcdef) = "01:23:45:67:89:ab:cd:ef");
bt_assert(from_hex(" ") = bs1);
bt_assert(from_hex("01:12:23:34:45:56:67:78:89:9a:ab:bc:cd:de:ef:f0") = bs2);
bt_assert(from_hex(format(bs2)) = bs2);
bt_assert(from_hex(" 0112:23-34455667 78-89 - 9a-ab bc:cd : de:eff0 ") = bs2);
}
bt_test_suite(t_bytestring, "Testing bytestrings");
/* /*
* Testing pairs * Testing pairs
* ------------- * -------------

View File

@ -33,6 +33,9 @@ u64 bstrtoul10(const char *str, char **end);
u64 bstrtoul16(const char *str, char **end); u64 bstrtoul16(const char *str, char **end);
byte bstrtobyte16(const char *str); byte bstrtobyte16(const char *str);
int bstrhextobin(const char *s, byte *b);
int bstrbintohex(const byte *b, size_t len, char *buf, size_t size, char delim);
int patmatch(const byte *pat, const byte *str); int patmatch(const byte *pat, const byte *str);
static inline char *xbasename(const char *str) static inline char *xbasename(const char *str)

View File

@ -60,29 +60,91 @@ bstrtoul16(const char *str, char **end)
return UINT64_MAX; return UINT64_MAX;
} }
byte static int
bstrtobyte16(const char *str) fromxdigit(char c)
{
switch (c)
{ {
byte out = 0;
for (int i=0; i<2; i++) {
switch (str[i]) {
case '0' ... '9': case '0' ... '9':
out *= 16; return c - '0';
out += str[i] - '0';
break;
case 'a' ... 'f': case 'a' ... 'f':
out *= 16; return c + 10 - 'a';
out += str[i] + 10 - 'a';
break;
case 'A' ... 'F': case 'A' ... 'F':
out *= 16; return c + 10 - 'A';
out += str[i] + 10 - 'A';
break;
default: default:
errno = ERANGE;
return -1; return -1;
} }
} }
return out; int
bstrhextobin(const char *s, byte *b)
{
int len = 0;
int hi = 0;
for (; *s; s++)
{
int v = fromxdigit(*s);
if (v < 0)
{
if (strchr(" :-", *s) && !hi)
continue;
else
return -1;
}
if (len == INT32_MAX)
return -1;
if (b)
{
if (!hi)
b[len] = (v << 4);
else
b[len] |= v;
}
len += hi;
hi = !hi;
}
return !hi ? len : -1;
}
static char
toxdigit(uint b)
{
if (b < 10)
return ('0' + b);
else if (b < 16)
return ('a' + b - 10);
else
return 0;
}
int
bstrbintohex(const byte *b, size_t len, char *buf, size_t size, char delim)
{
ASSERT(size >= 6);
char *bound = buf + size - 3;
size_t i;
for (i = 0; i < len; i++)
{
if (buf > bound)
{
strcpy(buf - 4, "...");
return -1;
}
uint x = b[i];
buf[0] = toxdigit(x >> 4);
buf[1] = toxdigit(x & 0xF);
buf[2] = delim;
buf += 3;
}
buf[i ? -1 : 0] = 0;
return 0;
} }

View File

@ -37,6 +37,7 @@ union bval_long {
ip_addr ip; ip_addr ip;
const net_addr *net; const net_addr *net;
const char *s; const char *s;
const struct bytestring *bs;
const struct f_tree *t; const struct f_tree *t;
const struct f_trie *ti; const struct f_trie *ti;
const struct f_path_mask *path_mask; const struct f_path_mask *path_mask;
@ -99,6 +100,7 @@ enum btype {
T_LC = 0xc0, /* Large community value, lcomm */ T_LC = 0xc0, /* Large community value, lcomm */
T_RD = 0xc4, /* Route distinguisher for VPN addresses */ T_RD = 0xc4, /* Route distinguisher for VPN addresses */
T_PATH_MASK_ITEM = 0xc8, /* Path mask item for path mask constructors */ T_PATH_MASK_ITEM = 0xc8, /* Path mask item for path mask constructors */
T_BYTESTRING = 0xcc,
T_SET = 0x80, T_SET = 0x80,
T_PREFIX_SET = 0x84, T_PREFIX_SET = 0x84,

View File

@ -571,7 +571,7 @@ pass_key: PASSWORD | KEY;
password_item_begin: password_item_begin:
pass_key text { init_password_list(); init_password($2, strlen($2), password_id++); } pass_key text { init_password_list(); init_password($2, strlen($2), password_id++); }
| pass_key BYTESTRING { init_password_list(); init_password($2->data, $2->length, password_id++); } | pass_key BYTETEXT { init_password_list(); init_password($2->data, $2->length, password_id++); }
; ;
password_item_params: password_item_params:

View File

@ -71,7 +71,7 @@ radv_proto_item:
| PREFIX radv_prefix { add_tail(&RADV_CFG->pref_list, NODE this_radv_prefix); } | PREFIX radv_prefix { add_tail(&RADV_CFG->pref_list, NODE this_radv_prefix); }
| RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_CFG->rdnss_list, &radv_dns_list); } | RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_CFG->rdnss_list, &radv_dns_list); }
| DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_CFG->dnssl_list, &radv_dns_list); } | DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_CFG->dnssl_list, &radv_dns_list); }
| CUSTOM OPTION TYPE expr VALUE BYTESTRING { radv_add_to_custom_list(&RADV_CFG->custom_list, $4, $6); } | CUSTOM OPTION TYPE expr VALUE BYTETEXT { radv_add_to_custom_list(&RADV_CFG->custom_list, $4, $6); }
| TRIGGER net_ip6 { RADV_CFG->trigger = $2; } | TRIGGER net_ip6 { RADV_CFG->trigger = $2; }
| PROPAGATE ROUTES bool { RADV_CFG->propagate_routes = $3; } | PROPAGATE ROUTES bool { RADV_CFG->propagate_routes = $3; }
; ;
@ -136,7 +136,7 @@ radv_iface_item:
| PREFIX radv_prefix { add_tail(&RADV_IFACE->pref_list, NODE this_radv_prefix); } | PREFIX radv_prefix { add_tail(&RADV_IFACE->pref_list, NODE this_radv_prefix); }
| RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_IFACE->rdnss_list, &radv_dns_list); } | RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_IFACE->rdnss_list, &radv_dns_list); }
| DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_IFACE->dnssl_list, &radv_dns_list); } | DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_IFACE->dnssl_list, &radv_dns_list); }
| CUSTOM OPTION TYPE expr VALUE BYTESTRING { radv_add_to_custom_list(&RADV_IFACE->custom_list, $4, $6); } | CUSTOM OPTION TYPE expr VALUE BYTETEXT { radv_add_to_custom_list(&RADV_IFACE->custom_list, $4, $6); }
| RDNSS LOCAL bool { RADV_IFACE->rdnss_local = $3; } | RDNSS LOCAL bool { RADV_IFACE->rdnss_local = $3; }
| DNSSL LOCAL bool { RADV_IFACE->dnssl_local = $3; } | DNSSL LOCAL bool { RADV_IFACE->dnssl_local = $3; }
| CUSTOM OPTION LOCAL bool { RADV_IFACE->custom_local = $4; } | CUSTOM OPTION LOCAL bool { RADV_IFACE->custom_local = $4; }