1999-01-15 16:49:17 +00:00
|
|
|
/*
|
|
|
|
* Filters: utility functions
|
|
|
|
*
|
|
|
|
* Copyright 1998 Pavel Machek <pavel@ucw.cz>
|
2017-10-19 10:39:44 +00:00
|
|
|
* 2017 Jan Maria Matejka <mq@ucw.cz>
|
1999-01-15 16:49:17 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "filter/filter.h"
|
2018-11-21 19:37:11 +00:00
|
|
|
#include "lib/idm.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/route.h"
|
1999-01-15 16:49:17 +00:00
|
|
|
|
2000-04-20 10:25:51 +00:00
|
|
|
#define P(a,b) ((a<<8) | b)
|
|
|
|
|
1999-03-08 20:30:06 +00:00
|
|
|
struct f_inst *
|
2017-10-19 10:39:44 +00:00
|
|
|
f_new_inst(enum f_instruction_code fi_code)
|
1999-03-02 19:49:28 +00:00
|
|
|
{
|
1999-03-08 20:30:06 +00:00
|
|
|
struct f_inst * ret;
|
2017-10-19 10:39:44 +00:00
|
|
|
ret = cfg_allocz(sizeof(struct f_inst));
|
|
|
|
ret->fi_code = fi_code;
|
2012-07-18 17:29:33 +00:00
|
|
|
ret->lineno = ifs->lino;
|
1999-03-02 19:49:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-03-01 11:32:23 +00:00
|
|
|
struct f_inst *
|
2017-10-19 10:39:44 +00:00
|
|
|
f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
|
2000-03-01 11:32:23 +00:00
|
|
|
{
|
2017-10-19 10:39:44 +00:00
|
|
|
struct f_inst *ret = f_new_inst(fi_code);
|
2018-03-13 15:51:04 +00:00
|
|
|
ret->aux = (da.f_type << 8) | da.type;
|
2017-10-19 10:39:44 +00:00
|
|
|
ret->a2.i = da.ea_code;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct f_inst *
|
|
|
|
f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa)
|
|
|
|
{
|
|
|
|
struct f_inst *ret = f_new_inst(fi_code);
|
|
|
|
ret->aux = sa.f_type;
|
|
|
|
ret->a2.i = sa.sa_code;
|
|
|
|
ret->a1.i = sa.readonly;
|
|
|
|
return ret;
|
2000-03-01 11:32:23 +00:00
|
|
|
}
|
|
|
|
|
2000-04-20 10:25:51 +00:00
|
|
|
/*
|
|
|
|
* Generate set_dynamic( operation( get_dynamic(), argument ) )
|
|
|
|
*/
|
|
|
|
struct f_inst *
|
2017-10-19 10:39:44 +00:00
|
|
|
f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
|
2000-04-20 10:25:51 +00:00
|
|
|
{
|
2017-10-19 10:39:44 +00:00
|
|
|
struct f_inst *set_dyn = f_new_inst_da(FI_EA_SET, da),
|
|
|
|
*oper = f_new_inst(operation),
|
|
|
|
*get_dyn = f_new_inst_da(FI_EA_GET, da);
|
2000-04-20 10:25:51 +00:00
|
|
|
|
|
|
|
oper->aux = operation_aux;
|
|
|
|
oper->a1.p = get_dyn;
|
|
|
|
oper->a2.p = argument;
|
2017-10-19 10:39:44 +00:00
|
|
|
|
2000-04-20 10:25:51 +00:00
|
|
|
set_dyn->a1.p = oper;
|
|
|
|
return set_dyn;
|
|
|
|
}
|
|
|
|
|
2012-03-18 16:32:30 +00:00
|
|
|
struct f_inst *
|
2016-01-20 14:38:37 +00:00
|
|
|
f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn)
|
2012-03-18 16:32:30 +00:00
|
|
|
{
|
|
|
|
struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
|
2017-10-19 10:39:44 +00:00
|
|
|
ret->i.fi_code = FI_ROA_CHECK;
|
2012-07-18 17:29:33 +00:00
|
|
|
ret->i.lineno = ifs->lino;
|
2012-03-18 16:32:30 +00:00
|
|
|
ret->i.arg1 = prefix;
|
|
|
|
ret->i.arg2 = asn;
|
|
|
|
/* prefix == NULL <-> asn == NULL */
|
|
|
|
|
2016-01-20 14:38:37 +00:00
|
|
|
if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
|
|
|
|
cf_error("%s is not a ROA table", table->name);
|
|
|
|
ret->rtc = table;
|
2012-03-18 16:32:30 +00:00
|
|
|
|
|
|
|
return &ret->i;
|
|
|
|
}
|
|
|
|
|
2018-04-30 10:49:22 +00:00
|
|
|
static const char * const f_instruction_name_str[] = {
|
|
|
|
#define F(c,a,b) \
|
|
|
|
[c] = #c,
|
|
|
|
FI__LIST
|
|
|
|
#undef F
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *
|
|
|
|
f_instruction_name(enum f_instruction_code fi)
|
|
|
|
{
|
|
|
|
if (fi < FI__MAX)
|
|
|
|
return f_instruction_name_str[fi];
|
|
|
|
else
|
|
|
|
bug("Got unknown instruction code: %d", fi);
|
|
|
|
}
|
|
|
|
|
1999-04-05 20:10:31 +00:00
|
|
|
char *
|
|
|
|
filter_name(struct filter *filter)
|
|
|
|
{
|
|
|
|
if (!filter)
|
|
|
|
return "ACCEPT";
|
|
|
|
else if (filter == FILTER_REJECT)
|
|
|
|
return "REJECT";
|
2012-03-15 11:50:49 +00:00
|
|
|
else if (!filter->name)
|
|
|
|
return "(unnamed)";
|
1999-04-05 20:10:31 +00:00
|
|
|
else
|
|
|
|
return filter->name;
|
|
|
|
}
|
2018-11-21 19:37:11 +00:00
|
|
|
|
|
|
|
#define CA_KEY(n) n->name, n->fda.type
|
|
|
|
#define CA_NEXT(n) n->next
|
|
|
|
#define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
|
|
|
|
#define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
|
|
|
|
#define CA_ORDER 8 /* Fixed */
|
|
|
|
|
|
|
|
struct ca_storage {
|
|
|
|
struct ca_storage *next;
|
|
|
|
struct f_dynamic_attr fda;
|
|
|
|
u32 uc;
|
|
|
|
char name[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
HASH(struct ca_storage) ca_hash;
|
|
|
|
|
|
|
|
static struct idm ca_idm;
|
|
|
|
static struct ca_storage **ca_storage;
|
|
|
|
static uint ca_storage_max;
|
|
|
|
|
|
|
|
static void
|
|
|
|
ca_free(resource *r)
|
|
|
|
{
|
|
|
|
struct custom_attribute *ca = (void *) r;
|
|
|
|
struct ca_storage *cas = HASH_FIND(ca_hash, CA, ca->name, ca->fda->type);
|
|
|
|
ASSERT(cas);
|
|
|
|
|
|
|
|
ca->name = NULL;
|
|
|
|
ca->fda = NULL;
|
|
|
|
if (!--cas->uc) {
|
|
|
|
uint id = EA_CUSTOM_ID(cas->fda.ea_code);
|
|
|
|
idm_free(&ca_idm, id);
|
|
|
|
HASH_REMOVE(ca_hash, CA, cas);
|
|
|
|
ca_storage[id] = NULL;
|
|
|
|
mb_free(cas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ca_dump(resource *r)
|
|
|
|
{
|
|
|
|
struct custom_attribute *ca = (void *) r;
|
|
|
|
debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
|
|
|
|
ca->name, ca->fda->ea_code, ca->fda->type, ca->fda->f_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resclass ca_class = {
|
|
|
|
.name = "Custom attribute",
|
|
|
|
.size = sizeof(struct custom_attribute),
|
|
|
|
.free = ca_free,
|
|
|
|
.dump = ca_dump,
|
|
|
|
.lookup = NULL,
|
|
|
|
.memsize = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct custom_attribute *
|
|
|
|
ca_lookup(pool *p, const char *name, int f_type)
|
|
|
|
{
|
|
|
|
int ea_type;
|
|
|
|
|
|
|
|
switch (f_type) {
|
|
|
|
case T_INT:
|
|
|
|
ea_type = EAF_TYPE_INT;
|
|
|
|
break;
|
|
|
|
case T_IP:
|
|
|
|
ea_type = EAF_TYPE_IP_ADDRESS;
|
|
|
|
break;
|
|
|
|
case T_QUAD:
|
|
|
|
ea_type = EAF_TYPE_ROUTER_ID;
|
|
|
|
break;
|
|
|
|
case T_PATH:
|
|
|
|
ea_type = EAF_TYPE_AS_PATH;
|
|
|
|
break;
|
|
|
|
case T_CLIST:
|
|
|
|
ea_type = EAF_TYPE_INT_SET;
|
|
|
|
break;
|
|
|
|
case T_ECLIST:
|
|
|
|
ea_type = EAF_TYPE_EC_SET;
|
|
|
|
break;
|
|
|
|
case T_LCLIST:
|
|
|
|
ea_type = EAF_TYPE_LC_SET;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cf_error("Custom route attribute of unsupported type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inited = 0;
|
|
|
|
if (!inited) {
|
2019-04-09 11:38:39 +00:00
|
|
|
idm_init(&ca_idm, &root_pool, 8, EA_CUSTOM_BIT);
|
2018-11-21 19:37:11 +00:00
|
|
|
HASH_INIT(ca_hash, &root_pool, CA_ORDER);
|
|
|
|
|
|
|
|
ca_storage_max = 256;
|
|
|
|
ca_storage = mb_allocz(&root_pool, sizeof(struct ca_storage *) * ca_storage_max);
|
|
|
|
|
|
|
|
inited++;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, ea_type);
|
|
|
|
if (cas) {
|
|
|
|
cas->uc++;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
uint id = idm_alloc(&ca_idm);
|
|
|
|
|
2019-04-09 11:38:39 +00:00
|
|
|
if (!id)
|
2018-11-21 19:37:11 +00:00
|
|
|
cf_error("Too many custom attributes.");
|
|
|
|
|
|
|
|
if (id >= ca_storage_max) {
|
|
|
|
ca_storage_max *= 2;
|
|
|
|
ca_storage = mb_realloc(ca_storage, sizeof(struct ca_storage *) * ca_storage_max * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
cas = mb_allocz(&root_pool, sizeof(struct ca_storage) + strlen(name) + 1);
|
|
|
|
cas->fda = f_new_dynamic_attr(ea_type, f_type, EA_CUSTOM(id));
|
|
|
|
cas->uc = 1;
|
|
|
|
|
|
|
|
strcpy(cas->name, name);
|
|
|
|
ca_storage[id] = cas;
|
|
|
|
|
|
|
|
HASH_INSERT(ca_hash, CA, cas);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct custom_attribute *ca = ralloc(p, &ca_class);
|
|
|
|
ca->fda = &(cas->fda);
|
|
|
|
ca->name = cas->name;
|
|
|
|
return ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ea_custom_name(uint ea)
|
|
|
|
{
|
|
|
|
uint id = EA_CUSTOM_ID(ea);
|
|
|
|
if (id >= ca_storage_max)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!ca_storage[id])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ca_storage[id]->name;
|
|
|
|
}
|
|
|
|
|