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"
|
2019-02-07 20:25:38 +00:00
|
|
|
#include "filter/f-inst.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)
|
|
|
|
|
2019-02-15 12:53:17 +00:00
|
|
|
const char *
|
|
|
|
filter_name(const struct filter *filter)
|
1999-04-05 20:10:31 +00:00
|
|
|
{
|
|
|
|
if (!filter)
|
|
|
|
return "ACCEPT";
|
|
|
|
else if (filter == FILTER_REJECT)
|
|
|
|
return "REJECT";
|
2019-02-26 15:44:24 +00:00
|
|
|
else if (!filter->sym)
|
2012-03-15 11:50:49 +00:00
|
|
|
return "(unnamed)";
|
1999-04-05 20:10:31 +00:00
|
|
|
else
|
2019-02-26 15:44:24 +00:00
|
|
|
return filter->sym->name;
|
1999-04-05 20:10:31 +00:00
|
|
|
}
|
2018-11-21 19:37:11 +00:00
|
|
|
|
2019-06-28 09:08:48 +00:00
|
|
|
struct filter *f_new_where(struct f_inst *where)
|
2019-01-21 08:17:54 +00:00
|
|
|
{
|
2019-09-10 11:45:18 +00:00
|
|
|
struct f_inst *cond = f_new_inst(FI_CONDITION, where,
|
|
|
|
f_new_inst(FI_DIE, F_ACCEPT),
|
|
|
|
f_new_inst(FI_DIE, F_REJECT));
|
2019-01-21 08:17:54 +00:00
|
|
|
|
2019-02-26 15:44:24 +00:00
|
|
|
struct filter *f = cfg_allocz(sizeof(struct filter));
|
2019-09-10 11:45:18 +00:00
|
|
|
f->root = f_linearize(cond);
|
2019-01-21 08:17:54 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
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;
|
2022-03-26 10:56:02 +00:00
|
|
|
debug("name \"%s\" id 0x%04x ea_type 0x%02x\n",
|
|
|
|
ca->name, ca->fda->ea_code, ca->fda->type);
|
2018-11-21 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 *
|
2022-03-26 10:56:02 +00:00
|
|
|
ca_lookup(pool *p, const char *name, btype type)
|
2018-11-21 19:37:11 +00:00
|
|
|
{
|
2022-03-26 10:56:02 +00:00
|
|
|
switch (type) {
|
2018-11-21 19:37:11 +00:00
|
|
|
case T_INT:
|
|
|
|
case T_IP:
|
|
|
|
case T_QUAD:
|
|
|
|
case T_PATH:
|
|
|
|
case T_CLIST:
|
|
|
|
case T_ECLIST:
|
|
|
|
case T_LCLIST:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cf_error("Custom route attribute of unsupported type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inited = 0;
|
|
|
|
if (!inited) {
|
|
|
|
idm_init(&ca_idm, &root_pool, 8);
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2022-03-26 10:56:02 +00:00
|
|
|
struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, type);
|
2018-11-21 19:37:11 +00:00
|
|
|
if (cas) {
|
|
|
|
cas->uc++;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
uint id = idm_alloc(&ca_idm);
|
|
|
|
|
|
|
|
if (id >= EA_CUSTOM_BIT)
|
|
|
|
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);
|
2022-03-26 10:56:02 +00:00
|
|
|
cas->fda = f_new_dynamic_attr(type, EA_CUSTOM(id));
|
2018-11-21 19:37:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|