2000-04-17 15:53:29 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Path Operations
|
|
|
|
*
|
|
|
|
* (c) 2000 Martin Mares <mj@ucw.cz>
|
|
|
|
* (c) 2000 Pavel Machek <pavel@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/route.h"
|
2000-04-17 18:18:55 +08:00
|
|
|
#include "nest/attrs.h"
|
2000-04-17 15:53:29 +08:00
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/unaligned.h"
|
2000-04-17 18:18:55 +08:00
|
|
|
#include "lib/string.h"
|
2019-02-08 20:38:12 +08:00
|
|
|
#include "filter/data.h"
|
2000-04-17 15:53:29 +08:00
|
|
|
|
2009-12-15 06:31:25 +08:00
|
|
|
// static inline void put_as(byte *data, u32 as) { put_u32(data, as); }
|
|
|
|
// static inline u32 get_as(byte *data) { return get_u32(data); }
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2009-12-15 06:31:25 +08:00
|
|
|
#define put_as put_u32
|
|
|
|
#define get_as get_u32
|
2016-11-30 18:55:33 +08:00
|
|
|
#define BS 4 /* Default block size of ASN (autonomous system number) */
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
#define BAD(DSC, VAL) ({ err_dsc = DSC; err_val = VAL; goto bad; })
|
|
|
|
|
|
|
|
int
|
2017-01-22 23:32:42 +08:00
|
|
|
as_path_valid(byte *data, uint len, int bs, int confed, char *err, uint elen)
|
2000-04-17 15:53:29 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
byte *pos = data;
|
|
|
|
char *err_dsc = NULL;
|
|
|
|
uint err_val = 0;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
if (len < 2)
|
|
|
|
BAD("segment framing error", 0);
|
|
|
|
|
|
|
|
/* Process one AS path segment */
|
|
|
|
uint type = pos[0];
|
|
|
|
uint slen = 2 + bs * pos[1];
|
|
|
|
|
|
|
|
if (len < slen)
|
|
|
|
BAD("segment framing error", len);
|
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case AS_PATH_SET:
|
|
|
|
case AS_PATH_SEQUENCE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AS_PATH_CONFED_SEQUENCE:
|
|
|
|
case AS_PATH_CONFED_SET:
|
|
|
|
if (!confed)
|
|
|
|
BAD("AS_CONFED* segment", type);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-12-07 21:11:28 +08:00
|
|
|
BAD("unknown segment", type);
|
2017-01-22 23:32:42 +08:00
|
|
|
}
|
2016-12-07 21:11:28 +08:00
|
|
|
|
|
|
|
if (pos[1] == 0)
|
|
|
|
BAD("zero-length segment", type);
|
2000-04-17 15:53:29 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
pos += slen;
|
|
|
|
len -= slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if (err)
|
|
|
|
if (bsnprintf(err, elen, "%s (%u) at %d", err_dsc, err_val, (int) (pos - data)) < 0)
|
|
|
|
err[0] = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-12-27 21:26:11 +08:00
|
|
|
as_path_16to32(byte *dst, const byte *src, uint len)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
|
|
|
byte *dst0 = dst;
|
2018-12-27 21:26:11 +08:00
|
|
|
const byte *end = src + len;
|
2016-12-07 21:11:28 +08:00
|
|
|
uint i, n;
|
|
|
|
|
|
|
|
while (src < end)
|
|
|
|
{
|
|
|
|
n = src[1];
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst++ = *src++;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2000-04-17 15:53:29 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
put_u32(dst, get_u16(src));
|
|
|
|
src += 2;
|
|
|
|
dst += 4;
|
2000-04-17 15:53:29 +08:00
|
|
|
}
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst - dst0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-12-27 21:26:11 +08:00
|
|
|
as_path_32to16(byte *dst, const byte *src, uint len)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
|
|
|
byte *dst0 = dst;
|
2018-12-27 21:26:11 +08:00
|
|
|
const byte *end = src + len;
|
2016-12-07 21:11:28 +08:00
|
|
|
uint i, n;
|
|
|
|
|
|
|
|
while (src < end)
|
|
|
|
{
|
|
|
|
n = src[1];
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst++ = *src++;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2000-04-17 15:53:29 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
put_u16(dst, get_u32(src));
|
|
|
|
src += 4;
|
|
|
|
dst += 2;
|
2000-04-17 15:53:29 +08:00
|
|
|
}
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst - dst0;
|
2000-04-17 15:53:29 +08:00
|
|
|
}
|
2000-04-17 18:18:55 +08:00
|
|
|
|
2008-10-27 05:36:08 +08:00
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_contains_as4(const struct adata *path)
|
2008-10-27 05:36:08 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
uint i, n;
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
n = pos[1];
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2008-10-27 05:36:08 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
if (get_as(pos) > 0xFFFF)
|
|
|
|
return 1;
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
pos += BS;
|
2008-10-27 05:36:08 +08:00
|
|
|
}
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
return 0;
|
2008-10-27 05:36:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_contains_confed(const struct adata *path)
|
|
|
|
{
|
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
|
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint slen = 2 + BS * pos[1];
|
|
|
|
|
|
|
|
if ((type == AS_PATH_CONFED_SEQUENCE) ||
|
|
|
|
(type == AS_PATH_CONFED_SET))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
pos += slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
struct adata *
|
|
|
|
as_path_strip_confed(struct linpool *pool, const struct adata *path)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
struct adata *res = lp_alloc_adata(pool, path->length);
|
|
|
|
const byte *src = path->data;
|
|
|
|
const byte *end = src + path->length;
|
|
|
|
byte *dst = res->data;
|
2016-12-07 21:11:28 +08:00
|
|
|
|
|
|
|
while (src < end)
|
|
|
|
{
|
|
|
|
uint type = src[0];
|
|
|
|
uint slen = 2 + BS * src[1];
|
|
|
|
|
|
|
|
/* Copy regular segments */
|
|
|
|
if ((type == AS_PATH_SET) || (type == AS_PATH_SEQUENCE))
|
|
|
|
{
|
|
|
|
memcpy(dst, src, slen);
|
|
|
|
dst += slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += slen;
|
|
|
|
}
|
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
/* Fix the result length */
|
|
|
|
res->length = dst - res->data;
|
|
|
|
|
|
|
|
return res;
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct adata *
|
2017-01-22 23:32:42 +08:00
|
|
|
as_path_prepend2(struct linpool *pool, const struct adata *op, int seq, u32 as)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
|
|
|
struct adata *np;
|
|
|
|
const byte *pos = op->data;
|
|
|
|
uint len = op->length;
|
|
|
|
|
|
|
|
if (len && (pos[0] == seq) && (pos[1] < 255))
|
|
|
|
{
|
|
|
|
/* Starting with matching segment => just prepend the AS number */
|
|
|
|
np = lp_alloc_adata(pool, len + BS);
|
|
|
|
np->data[0] = seq;
|
|
|
|
np->data[1] = pos[1] + 1;
|
|
|
|
put_as(np->data + 2, as);
|
|
|
|
|
|
|
|
uint dlen = BS * pos[1];
|
|
|
|
memcpy(np->data + 2 + BS, pos + 2, dlen);
|
|
|
|
ADVANCE(pos, len, 2 + dlen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Create a new path segment */
|
|
|
|
np = lp_alloc_adata(pool, len + 2 + BS);
|
|
|
|
np->data[0] = seq;
|
|
|
|
np->data[1] = 1;
|
|
|
|
put_as(np->data + 2, as);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
byte *dst = np->data + 2 + BS * np->data[1];
|
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
memcpy(dst, pos, len);
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct adata *
|
|
|
|
as_path_to_old(struct linpool *pool, const struct adata *path)
|
2008-10-27 05:36:08 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
struct adata *res = lp_alloc_adata(pool, path->length);
|
|
|
|
byte *pos = res->data;
|
|
|
|
byte *end = pos + res->length;
|
|
|
|
uint i, n;
|
2008-10-27 05:36:08 +08:00
|
|
|
u32 as;
|
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
/* Copy the whole path */
|
|
|
|
memcpy(res->data, path->data, path->length);
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
/* Replace 32-bit AS numbers with AS_TRANS */
|
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
n = pos[1];
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2008-10-27 05:36:08 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
as = get_as(pos);
|
|
|
|
if (as > 0xFFFF)
|
|
|
|
put_as(pos, AS_TRANS);
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
pos += BS;
|
|
|
|
}
|
|
|
|
}
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
return res;
|
|
|
|
}
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
/*
|
|
|
|
* Cut the path to the length @num, measured to the usual path metric. Note that
|
|
|
|
* AS_CONFED_* segments have zero length and must be added if they are on edge.
|
|
|
|
*/
|
2018-12-27 21:26:11 +08:00
|
|
|
struct adata *
|
|
|
|
as_path_cut(struct linpool *pool, const struct adata *path, uint num)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
2018-12-27 21:26:11 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint t = pos[0];
|
|
|
|
uint l = pos[1];
|
|
|
|
uint n = 0;
|
|
|
|
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case AS_PATH_SET: n = 1; break;
|
|
|
|
case AS_PATH_SEQUENCE: n = l; break;
|
|
|
|
case AS_PATH_CONFED_SEQUENCE: n = 0; break;
|
|
|
|
case AS_PATH_CONFED_SET: n = 0; break;
|
|
|
|
default: bug("as_path_cut: Invalid path segment");
|
2008-10-27 05:36:08 +08:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
/* Cannot add whole segment, so try partial one and finish */
|
|
|
|
if (num < n)
|
|
|
|
{
|
2018-12-27 21:26:11 +08:00
|
|
|
const byte *nend = pos;
|
|
|
|
if (num)
|
|
|
|
nend += 2 + BS * num;
|
|
|
|
|
|
|
|
struct adata *res = lp_alloc_adata(pool, path->length);
|
|
|
|
res->length = nend - (const byte *) path->data;
|
|
|
|
memcpy(res->data, path->data, res->length);
|
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
if (num)
|
|
|
|
{
|
2018-12-27 21:26:11 +08:00
|
|
|
byte *dpos = ((byte *) res->data) + (pos - (const byte *) path->data);
|
|
|
|
dpos[1] = num;
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
2018-12-27 21:26:11 +08:00
|
|
|
return res;
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
num -= n;
|
|
|
|
pos += 2 + BS * l;
|
|
|
|
}
|
|
|
|
|
2018-12-27 21:26:11 +08:00
|
|
|
struct adata *res = lp_alloc_adata(pool, path->length);
|
|
|
|
res->length = path->length;
|
|
|
|
memcpy(res->data, path->data, res->length);
|
|
|
|
return res;
|
2016-12-07 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merge (concatenate) paths @p1 and @p2 and return the result.
|
|
|
|
* In contrast to other as_path_* functions, @p1 and @p2 may be reused.
|
|
|
|
*/
|
2018-12-27 21:26:11 +08:00
|
|
|
const struct adata *
|
|
|
|
as_path_merge(struct linpool *pool, const struct adata *p1, const struct adata *p2)
|
2016-12-07 21:11:28 +08:00
|
|
|
{
|
|
|
|
if (p1->length == 0)
|
|
|
|
return p2;
|
|
|
|
|
|
|
|
if (p2->length == 0)
|
|
|
|
return p1;
|
|
|
|
|
|
|
|
struct adata *res = lp_alloc_adata(pool, p1->length + p2->length);
|
|
|
|
memcpy(res->data, p1->data, p1->length);
|
|
|
|
memcpy(res->data + p1->length, p2->data, p2->length);
|
|
|
|
|
|
|
|
return res;
|
2008-10-27 05:36:08 +08:00
|
|
|
}
|
|
|
|
|
2000-04-17 18:18:55 +08:00
|
|
|
void
|
2017-01-22 23:32:42 +08:00
|
|
|
as_path_format(const struct adata *path, byte *bb, uint size)
|
2000-04-17 18:18:55 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
buffer buf = { .start = bb, .pos = bb, .end = bb + size }, *b = &buf;
|
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
const char *ops, *cls;
|
|
|
|
|
|
|
|
b->pos[0] = 0;
|
|
|
|
|
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint len = pos[1];
|
|
|
|
pos += 2;
|
2000-04-17 18:18:55 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
switch (type)
|
2000-04-17 18:18:55 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
case AS_PATH_SET: ops = "{"; cls = "}"; break;
|
|
|
|
case AS_PATH_SEQUENCE: ops = NULL; cls = NULL; break;
|
|
|
|
case AS_PATH_CONFED_SEQUENCE: ops = "("; cls = ")"; break;
|
|
|
|
case AS_PATH_CONFED_SET: ops = "({"; cls = "})"; break;
|
|
|
|
default: bug("Invalid path segment");
|
2000-04-17 18:18:55 +08:00
|
|
|
}
|
2017-01-22 23:32:42 +08:00
|
|
|
|
|
|
|
if (ops)
|
|
|
|
buffer_puts(b, ops);
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
{
|
|
|
|
buffer_print(b, len ? "%u " : "%u", get_as(pos));
|
|
|
|
pos += BS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cls)
|
|
|
|
buffer_puts(b, cls);
|
|
|
|
|
|
|
|
if (pos < end)
|
|
|
|
buffer_puts(b, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle overflow */
|
|
|
|
if (b->pos == b->end)
|
|
|
|
strcpy(b->end - 12, "...");
|
2000-04-17 18:18:55 +08:00
|
|
|
}
|
2000-04-17 19:06:39 +08:00
|
|
|
|
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_getlen(const struct adata *path)
|
2000-04-17 19:06:39 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
uint res = 0;
|
2009-08-26 01:01:37 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint t = pos[0];
|
|
|
|
uint l = pos[1];
|
|
|
|
uint n = 0;
|
2000-04-17 19:06:39 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
switch (t)
|
2000-04-17 19:22:24 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
case AS_PATH_SET: n = 1; break;
|
|
|
|
case AS_PATH_SEQUENCE: n = l; break;
|
|
|
|
case AS_PATH_CONFED_SEQUENCE: n = 0; break;
|
|
|
|
case AS_PATH_CONFED_SET: n = 0; break;
|
|
|
|
default: bug("as_path_getlen: Invalid path segment");
|
2000-04-17 19:06:39 +08:00
|
|
|
}
|
2016-12-07 21:11:28 +08:00
|
|
|
|
|
|
|
res += n;
|
|
|
|
pos += 2 + BS * l;
|
|
|
|
}
|
|
|
|
|
2000-04-17 19:06:39 +08:00
|
|
|
return res;
|
|
|
|
}
|
2000-04-17 19:11:33 +08:00
|
|
|
|
2000-05-19 19:01:06 +08:00
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_get_last(const struct adata *path, u32 *orig_as)
|
2000-05-19 19:01:06 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
2008-10-27 05:36:08 +08:00
|
|
|
int found = 0;
|
2017-01-22 23:32:42 +08:00
|
|
|
u32 val = 0;
|
2000-05-19 19:01:06 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint len = pos[1];
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (type)
|
2000-05-19 19:01:06 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
case AS_PATH_SET:
|
|
|
|
case AS_PATH_CONFED_SET:
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AS_PATH_SEQUENCE:
|
|
|
|
case AS_PATH_CONFED_SEQUENCE:
|
|
|
|
val = get_as(pos + BS * (len - 1));
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bug("Invalid path segment");
|
2000-05-19 19:01:06 +08:00
|
|
|
}
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
pos += BS * len;
|
|
|
|
}
|
|
|
|
|
2009-10-13 02:44:58 +08:00
|
|
|
if (found)
|
2017-01-22 23:32:42 +08:00
|
|
|
*orig_as = val;
|
2008-10-27 05:36:08 +08:00
|
|
|
return found;
|
2000-05-19 19:01:06 +08:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:33:58 +08:00
|
|
|
u32
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_get_last_nonaggregated(const struct adata *path)
|
2016-02-17 00:33:58 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
u32 val = 0;
|
2016-02-17 00:33:58 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint len = pos[1];
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (type)
|
2016-02-17 00:33:58 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
case AS_PATH_SET:
|
|
|
|
case AS_PATH_CONFED_SET:
|
|
|
|
return val;
|
2016-02-17 00:33:58 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
case AS_PATH_SEQUENCE:
|
|
|
|
case AS_PATH_CONFED_SEQUENCE:
|
|
|
|
val = get_as(pos + BS * (len - 1));
|
|
|
|
break;
|
2016-02-17 00:33:58 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
default:
|
|
|
|
bug("Invalid path segment");
|
2016-02-17 00:33:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
pos += BS * len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2016-02-17 00:33:58 +08:00
|
|
|
}
|
|
|
|
|
2008-10-27 05:59:21 +08:00
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_get_first(const struct adata *path, u32 *last_as)
|
2008-10-27 05:59:21 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const u8 *p = path->data;
|
2008-10-27 05:59:21 +08:00
|
|
|
|
|
|
|
if ((path->length == 0) || (p[0] != AS_PATH_SEQUENCE) || (p[1] == 0))
|
|
|
|
return 0;
|
2017-01-22 23:32:42 +08:00
|
|
|
|
|
|
|
*last_as = get_as(p+2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
as_path_get_first_regular(const struct adata *path, u32 *last_as)
|
|
|
|
{
|
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
|
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint len = pos[1];
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
switch (type)
|
2008-10-27 05:59:21 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
case AS_PATH_SET:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case AS_PATH_SEQUENCE:
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*last_as = get_as(pos);
|
2008-10-27 05:59:21 +08:00
|
|
|
return 1;
|
2017-01-22 23:32:42 +08:00
|
|
|
|
|
|
|
case AS_PATH_CONFED_SEQUENCE:
|
|
|
|
case AS_PATH_CONFED_SET:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bug("Invalid path segment");
|
2008-10-27 05:59:21 +08:00
|
|
|
}
|
2017-01-22 23:32:42 +08:00
|
|
|
|
|
|
|
pos += BS * len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-10-27 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
2008-10-27 05:36:08 +08:00
|
|
|
int
|
2016-12-07 21:11:28 +08:00
|
|
|
as_path_contains(const struct adata *path, u32 as, int min)
|
2008-10-27 05:36:08 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const u8 *p = path->data;
|
|
|
|
const u8 *q = p+path->length;
|
2013-10-21 20:58:32 +08:00
|
|
|
int num = 0;
|
2008-10-27 05:36:08 +08:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
while (p<q)
|
|
|
|
{
|
|
|
|
n = p[1];
|
|
|
|
p += 2;
|
|
|
|
for(i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
if (get_as(p) == as)
|
2013-10-21 20:58:32 +08:00
|
|
|
if (++num == min)
|
|
|
|
return 1;
|
2009-12-15 06:31:25 +08:00
|
|
|
p += BS;
|
2008-10-27 05:36:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-10 05:27:10 +08:00
|
|
|
int
|
2018-12-27 21:26:11 +08:00
|
|
|
as_path_match_set(const struct adata *path, const struct f_tree *set)
|
2013-07-10 05:27:10 +08:00
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const u8 *p = path->data;
|
|
|
|
const u8 *q = p+path->length;
|
2013-07-10 05:27:10 +08:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
while (p<q)
|
|
|
|
{
|
|
|
|
n = p[1];
|
|
|
|
p += 2;
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
struct f_val v = {T_INT, .val.i = get_as(p)};
|
2018-12-27 21:26:11 +08:00
|
|
|
if (find_tree(set, &v))
|
2013-07-10 05:27:10 +08:00
|
|
|
return 1;
|
|
|
|
p += BS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-27 21:26:11 +08:00
|
|
|
const struct adata *
|
|
|
|
as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int pos)
|
2013-08-15 07:06:47 +08:00
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
int len = path->length;
|
2016-12-07 21:11:28 +08:00
|
|
|
const u8 *p = path->data;
|
|
|
|
const u8 *q = path->data + len;
|
2013-08-15 07:06:47 +08:00
|
|
|
u8 *d, *d2;
|
|
|
|
int i, bt, sn, dn;
|
|
|
|
u8 buf[len];
|
|
|
|
|
|
|
|
d = buf;
|
|
|
|
while (p<q)
|
|
|
|
{
|
|
|
|
/* Read block header (type and length) */
|
|
|
|
bt = p[0];
|
|
|
|
sn = p[1];
|
|
|
|
dn = 0;
|
|
|
|
p += 2;
|
|
|
|
d2 = d + 2;
|
|
|
|
|
|
|
|
for (i = 0; i < sn; i++)
|
|
|
|
{
|
|
|
|
u32 as = get_as(p);
|
|
|
|
int match;
|
|
|
|
|
|
|
|
if (set)
|
2018-12-27 21:26:11 +08:00
|
|
|
{
|
|
|
|
struct f_val v = {T_INT, .val.i = as};
|
|
|
|
match = !!find_tree(set, &v);
|
|
|
|
}
|
2013-08-15 07:06:47 +08:00
|
|
|
else
|
|
|
|
match = (as == key);
|
|
|
|
|
|
|
|
if (match == pos)
|
|
|
|
{
|
|
|
|
put_as(d2, as);
|
|
|
|
d2 += BS;
|
|
|
|
dn++;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += BS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dn > 0)
|
|
|
|
{
|
|
|
|
/* Nonempty block, set block header and advance */
|
|
|
|
d[0] = bt;
|
|
|
|
d[1] = dn;
|
|
|
|
d = d2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:37:04 +08:00
|
|
|
uint nl = d - buf;
|
2013-08-15 07:06:47 +08:00
|
|
|
if (nl == path->length)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
struct adata *res = lp_alloc(pool, sizeof(struct adata) + nl);
|
|
|
|
res->length = nl;
|
|
|
|
memcpy(res->data, buf, nl);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-10-27 05:36:08 +08:00
|
|
|
|
2009-04-17 07:48:36 +08:00
|
|
|
struct pm_pos
|
|
|
|
{
|
|
|
|
u8 set;
|
|
|
|
u8 mark;
|
|
|
|
union
|
|
|
|
{
|
2016-12-07 21:11:28 +08:00
|
|
|
const char *sp;
|
2009-04-17 07:48:36 +08:00
|
|
|
u32 asn;
|
|
|
|
} val;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2017-01-22 23:32:42 +08:00
|
|
|
parse_path(const struct adata *path, struct pm_pos *pp)
|
2000-04-17 19:11:33 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
const byte *pos = path->data;
|
|
|
|
const byte *end = pos + path->length;
|
|
|
|
struct pm_pos *op = pp;
|
|
|
|
uint i;
|
2000-04-17 19:11:33 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
uint type = pos[0];
|
|
|
|
uint len = pos[1];
|
|
|
|
pos += 2;
|
2009-02-21 23:20:45 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case AS_PATH_SET:
|
|
|
|
case AS_PATH_CONFED_SET:
|
|
|
|
pp->set = 1;
|
|
|
|
pp->mark = 0;
|
|
|
|
pp->val.sp = pos - 1;
|
|
|
|
pp++;
|
|
|
|
|
|
|
|
pos += BS * len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AS_PATH_SEQUENCE:
|
|
|
|
case AS_PATH_CONFED_SEQUENCE:
|
|
|
|
for (i = 0; i < len; i++)
|
2000-04-17 19:11:33 +08:00
|
|
|
{
|
2017-01-22 23:32:42 +08:00
|
|
|
pp->set = 0;
|
|
|
|
pp->mark = 0;
|
|
|
|
pp->val.asn = get_as(pos);
|
|
|
|
pp++;
|
|
|
|
|
|
|
|
pos += BS;
|
2000-04-17 19:11:33 +08:00
|
|
|
}
|
2017-01-22 23:32:42 +08:00
|
|
|
break;
|
2016-12-07 21:11:28 +08:00
|
|
|
|
2017-01-22 23:32:42 +08:00
|
|
|
default:
|
|
|
|
bug("Invalid path segment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pp - op;
|
2009-04-17 07:48:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-06-08 22:22:44 +08:00
|
|
|
pm_match(struct pm_pos *pos, u32 asn, u32 asn2)
|
2009-04-17 07:48:36 +08:00
|
|
|
{
|
2016-06-08 22:22:44 +08:00
|
|
|
u32 gas;
|
2009-04-17 07:48:36 +08:00
|
|
|
if (! pos->set)
|
2016-06-08 22:22:44 +08:00
|
|
|
return ((pos->val.asn >= asn) && (pos->val.asn <= asn2));
|
2009-04-17 07:48:36 +08:00
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
const u8 *p = pos->val.sp;
|
2009-04-17 07:48:36 +08:00
|
|
|
int len = *p++;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
2016-06-08 22:22:44 +08:00
|
|
|
{
|
|
|
|
gas = get_as(p + i * BS);
|
|
|
|
|
|
|
|
if ((gas >= asn) && (gas <= asn2))
|
2009-04-17 07:48:36 +08:00
|
|
|
return 1;
|
2016-06-08 22:22:44 +08:00
|
|
|
}
|
2009-04-17 07:48:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:58:13 +08:00
|
|
|
static int
|
2019-08-07 00:54:19 +08:00
|
|
|
pm_match_set(struct pm_pos *pos, const struct f_tree *set)
|
2019-08-06 22:58:13 +08:00
|
|
|
{
|
|
|
|
struct f_val asn = { .type = T_INT };
|
|
|
|
|
|
|
|
if (! pos->set)
|
|
|
|
{
|
|
|
|
asn.val.i = pos->val.asn;
|
|
|
|
return !!find_tree(set, &asn);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8 *p = pos->val.sp;
|
|
|
|
int len = *p++;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
asn.val.i = get_as(p + i * BS);
|
|
|
|
if (find_tree(set, &asn))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-17 07:48:36 +08:00
|
|
|
static void
|
|
|
|
pm_mark(struct pm_pos *pos, int i, int plen, int *nl, int *nh)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (pos[i].set)
|
|
|
|
pos[i].mark = 1;
|
2016-12-07 21:11:28 +08:00
|
|
|
|
2009-04-17 07:48:36 +08:00
|
|
|
for (j = i + 1; (j < plen) && pos[j].set && (! pos[j].mark); j++)
|
|
|
|
pos[j].mark = 1;
|
|
|
|
pos[j].mark = 1;
|
|
|
|
|
|
|
|
/* We are going downwards, therefore every mark is
|
|
|
|
new low and just the first mark is new high */
|
|
|
|
|
|
|
|
*nl = i + (pos[i].set ? 0 : 1);
|
|
|
|
|
|
|
|
if (*nh < 0)
|
|
|
|
*nh = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AS path matching is nontrivial. Because AS path can
|
2017-01-22 23:32:42 +08:00
|
|
|
* contain sets, it is not a plain wildcard matching. A set
|
2009-04-17 07:48:36 +08:00
|
|
|
* in an AS path is interpreted as it might represent any
|
|
|
|
* sequence of AS numbers from that set (possibly with
|
|
|
|
* repetitions). So it is also a kind of a pattern,
|
|
|
|
* more complicated than a path mask.
|
|
|
|
*
|
|
|
|
* The algorithm for AS path matching is a variant
|
|
|
|
* of nondeterministic finite state machine, where
|
|
|
|
* positions in AS path are states, and items in
|
|
|
|
* path mask are input for that finite state machine.
|
|
|
|
* During execution of the algorithm we maintain a set
|
|
|
|
* of marked states - a state is marked if it can be
|
|
|
|
* reached by any walk through NFSM with regard to
|
|
|
|
* currently processed part of input. When we process
|
|
|
|
* next part of mask, we advance each marked state.
|
|
|
|
* We start with marked first position, when we
|
|
|
|
* run out of marked positions, we reject. When
|
2016-06-08 22:22:44 +08:00
|
|
|
* we process the whole mask, we accept if final position
|
2009-04-17 07:48:36 +08:00
|
|
|
* (auxiliary position after last real position in AS path)
|
|
|
|
* is marked.
|
|
|
|
*/
|
|
|
|
int
|
2018-12-27 21:26:11 +08:00
|
|
|
as_path_match(const struct adata *path, const struct f_path_mask *mask)
|
2009-04-17 07:48:36 +08:00
|
|
|
{
|
|
|
|
struct pm_pos pos[2048 + 1];
|
|
|
|
int plen = parse_path(path, pos);
|
|
|
|
int l, h, i, nh, nl;
|
2010-02-21 21:34:53 +08:00
|
|
|
u32 val = 0;
|
2016-06-08 22:22:44 +08:00
|
|
|
u32 val2 = 0;
|
2009-04-17 07:48:36 +08:00
|
|
|
|
|
|
|
/* l and h are bound of interval of positions where
|
|
|
|
are marked states */
|
|
|
|
|
|
|
|
pos[plen].set = 0;
|
|
|
|
pos[plen].mark = 0;
|
|
|
|
|
|
|
|
l = h = 0;
|
|
|
|
pos[0].mark = 1;
|
2017-01-22 23:32:42 +08:00
|
|
|
|
2018-12-27 21:26:11 +08:00
|
|
|
for (uint m=0; m < mask->len; m++)
|
2009-04-17 07:48:36 +08:00
|
|
|
{
|
|
|
|
/* We remove this mark to not step after pos[plen] */
|
|
|
|
pos[plen].mark = 0;
|
|
|
|
|
2018-12-27 21:26:11 +08:00
|
|
|
switch (mask->item[m].kind)
|
2009-04-17 07:48:36 +08:00
|
|
|
{
|
|
|
|
case PM_ASTERISK:
|
|
|
|
for (i = l; i <= plen; i++)
|
|
|
|
pos[i].mark = 1;
|
|
|
|
h = plen;
|
|
|
|
break;
|
|
|
|
|
2016-06-08 22:22:44 +08:00
|
|
|
case PM_ASN: /* Define single ASN as ASN..ASN - very narrow interval */
|
2018-12-27 21:26:11 +08:00
|
|
|
val2 = val = mask->item[m].asn;
|
2009-06-02 01:32:41 +08:00
|
|
|
goto step;
|
|
|
|
case PM_ASN_EXPR:
|
2018-09-18 20:21:11 +08:00
|
|
|
bug("Expressions should be evaluated on AS path mask construction.");
|
2016-06-08 22:22:44 +08:00
|
|
|
case PM_ASN_RANGE:
|
2018-12-27 21:26:11 +08:00
|
|
|
val = mask->item[m].from;
|
|
|
|
val2 = mask->item[m].to;
|
2018-02-07 00:43:55 +08:00
|
|
|
goto step;
|
2009-06-02 01:32:41 +08:00
|
|
|
case PM_QUESTION:
|
2019-08-06 22:58:13 +08:00
|
|
|
case PM_ASN_SET:
|
2009-06-02 01:32:41 +08:00
|
|
|
step:
|
2010-02-21 21:34:53 +08:00
|
|
|
nh = nl = -1;
|
2009-04-17 07:48:36 +08:00
|
|
|
for (i = h; i >= l; i--)
|
|
|
|
if (pos[i].mark)
|
|
|
|
{
|
|
|
|
pos[i].mark = 0;
|
2019-08-06 22:58:13 +08:00
|
|
|
if ((mask->item[m].kind == PM_QUESTION) ||
|
|
|
|
((mask->item[m].kind != PM_ASN_SET) ?
|
|
|
|
pm_match(pos + i, val, val2) :
|
|
|
|
pm_match_set(pos + i, mask->item[m].set)))
|
2009-04-17 07:48:36 +08:00
|
|
|
pm_mark(pos, i, plen, &nl, &nh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nh < 0)
|
2000-04-17 19:11:33 +08:00
|
|
|
return 0;
|
2009-04-17 07:48:36 +08:00
|
|
|
|
|
|
|
h = nh;
|
|
|
|
l = nl;
|
|
|
|
break;
|
2000-04-17 19:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
2000-04-26 17:37:07 +08:00
|
|
|
|
2009-04-17 07:48:36 +08:00
|
|
|
return pos[plen].mark;
|
|
|
|
}
|