]>
git.ipfire.org Git - thirdparty/bird.git/blob - filter/f-util.c
2 * Filters: utility functions
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
5 * 2017 Jan Maria Matejka <mq@ucw.cz>
7 * Can be freely distributed and used under the terms of the GNU GPL.
10 #include "nest/bird.h"
11 #include "conf/conf.h"
12 #include "filter/filter.h"
13 #include "filter/f-inst.h"
15 #include "nest/protocol.h"
16 #include "nest/route.h"
18 #define P(a,b) ((a<<8) | b)
21 filter_name(const struct filter
*filter
)
25 else if (filter
== FILTER_REJECT
)
27 else if (!filter
->sym
)
30 return filter
->sym
->name
;
33 struct filter
*f_new_where(struct f_inst
*where
)
35 struct f_inst
*cond
= f_new_inst(FI_CONDITION
, where
,
36 f_new_inst(FI_DIE
, F_ACCEPT
),
37 f_new_inst(FI_DIE
, F_REJECT
));
39 struct filter
*f
= cfg_allocz(sizeof(struct filter
));
40 f
->root
= f_linearize(cond
, 0);
44 #define CA_KEY(n) n->name, n->fda.type
45 #define CA_NEXT(n) n->next
46 #define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
47 #define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
48 #define CA_ORDER 8 /* Fixed */
51 struct ca_storage
*next
;
52 struct f_dynamic_attr fda
;
57 HASH(struct ca_storage
) ca_hash
;
59 static struct idm ca_idm
;
60 static struct ca_storage
**ca_storage
;
61 static uint ca_storage_max
;
66 struct custom_attribute
*ca
= (void *) r
;
67 struct ca_storage
*cas
= HASH_FIND(ca_hash
, CA
, ca
->name
, ca
->fda
->type
);
73 uint id
= EA_CUSTOM_ID(cas
->fda
.ea_code
);
74 idm_free(&ca_idm
, id
);
75 HASH_REMOVE(ca_hash
, CA
, cas
);
76 ca_storage
[id
] = NULL
;
84 struct custom_attribute
*ca
= (void *) r
;
85 debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
86 ca
->name
, ca
->fda
->ea_code
, ca
->fda
->type
, ca
->fda
->f_type
);
89 static struct resclass ca_class
= {
90 .name
= "Custom attribute",
91 .size
= sizeof(struct custom_attribute
),
98 struct custom_attribute
*
99 ca_lookup(pool
*p
, const char *name
, int f_type
)
105 ea_type
= EAF_TYPE_INT
;
108 ea_type
= EAF_TYPE_IP_ADDRESS
;
111 ea_type
= EAF_TYPE_ROUTER_ID
;
114 ea_type
= EAF_TYPE_AS_PATH
;
117 ea_type
= EAF_TYPE_INT_SET
;
120 ea_type
= EAF_TYPE_EC_SET
;
123 ea_type
= EAF_TYPE_LC_SET
;
126 cf_error("Custom route attribute of unsupported type");
129 static int inited
= 0;
131 idm_init(&ca_idm
, config_pool
, 8);
132 HASH_INIT(ca_hash
, config_pool
, CA_ORDER
);
134 ca_storage_max
= 256;
135 ca_storage
= mb_allocz(config_pool
, sizeof(struct ca_storage
*) * ca_storage_max
);
140 struct ca_storage
*cas
= HASH_FIND(ca_hash
, CA
, name
, ea_type
);
145 uint id
= idm_alloc(&ca_idm
);
147 if (id
>= EA_CUSTOM_BIT
)
148 cf_error("Too many custom attributes.");
150 if (id
>= ca_storage_max
) {
152 ca_storage
= mb_realloc(ca_storage
, sizeof(struct ca_storage
*) * ca_storage_max
* 2);
155 cas
= mb_allocz(config_pool
, sizeof(struct ca_storage
) + strlen(name
) + 1);
156 cas
->fda
= f_new_dynamic_attr(ea_type
, f_type
, EA_CUSTOM(id
));
159 strcpy(cas
->name
, name
);
160 ca_storage
[id
] = cas
;
162 HASH_INSERT(ca_hash
, CA
, cas
);
165 struct custom_attribute
*ca
= ralloc(p
, &ca_class
);
166 ca
->fda
= &(cas
->fda
);
167 ca
->name
= cas
->name
;
172 ea_custom_name(uint ea
)
174 uint id
= EA_CUSTOM_ID(ea
);
175 if (id
>= ca_storage_max
)
181 return ca_storage
[id
]->name
;