]>
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
)
36 .fi_code
= FI_PRINT_AND_DIE
,
39 .i_FI_PRINT_AND_DIE
= { .fret
= F_ACCEPT
, },
43 .fi_code
= FI_PRINT_AND_DIE
,
46 .i_FI_PRINT_AND_DIE
= { .fret
= F_REJECT
, },
50 .fi_code
= FI_CONDITION
,
52 .size
= 3 + where
->size
,
60 struct filter
*f
= cfg_allocz(sizeof(struct filter
));
61 f
->root
= f_linearize(&i
);
65 #define CA_KEY(n) n->name, n->fda.type
66 #define CA_NEXT(n) n->next
67 #define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
68 #define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
69 #define CA_ORDER 8 /* Fixed */
72 struct ca_storage
*next
;
73 struct f_dynamic_attr fda
;
78 HASH(struct ca_storage
) ca_hash
;
80 static struct idm ca_idm
;
81 static struct ca_storage
**ca_storage
;
82 static uint ca_storage_max
;
87 struct custom_attribute
*ca
= (void *) r
;
88 struct ca_storage
*cas
= HASH_FIND(ca_hash
, CA
, ca
->name
, ca
->fda
->type
);
94 uint id
= EA_CUSTOM_ID(cas
->fda
.ea_code
);
95 idm_free(&ca_idm
, id
);
96 HASH_REMOVE(ca_hash
, CA
, cas
);
97 ca_storage
[id
] = NULL
;
105 struct custom_attribute
*ca
= (void *) r
;
106 debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
107 ca
->name
, ca
->fda
->ea_code
, ca
->fda
->type
, ca
->fda
->f_type
);
110 static struct resclass ca_class
= {
111 .name
= "Custom attribute",
112 .size
= sizeof(struct custom_attribute
),
119 struct custom_attribute
*
120 ca_lookup(pool
*p
, const char *name
, int f_type
)
126 ea_type
= EAF_TYPE_INT
;
129 ea_type
= EAF_TYPE_IP_ADDRESS
;
132 ea_type
= EAF_TYPE_ROUTER_ID
;
135 ea_type
= EAF_TYPE_AS_PATH
;
138 ea_type
= EAF_TYPE_INT_SET
;
141 ea_type
= EAF_TYPE_EC_SET
;
144 ea_type
= EAF_TYPE_LC_SET
;
147 cf_error("Custom route attribute of unsupported type");
150 static int inited
= 0;
152 idm_init(&ca_idm
, &root_pool
, 8);
153 HASH_INIT(ca_hash
, &root_pool
, CA_ORDER
);
155 ca_storage_max
= 256;
156 ca_storage
= mb_allocz(&root_pool
, sizeof(struct ca_storage
*) * ca_storage_max
);
161 struct ca_storage
*cas
= HASH_FIND(ca_hash
, CA
, name
, ea_type
);
166 uint id
= idm_alloc(&ca_idm
);
168 if (id
>= EA_CUSTOM_BIT
)
169 cf_error("Too many custom attributes.");
171 if (id
>= ca_storage_max
) {
173 ca_storage
= mb_realloc(ca_storage
, sizeof(struct ca_storage
*) * ca_storage_max
* 2);
176 cas
= mb_allocz(&root_pool
, sizeof(struct ca_storage
) + strlen(name
) + 1);
177 cas
->fda
= f_new_dynamic_attr(ea_type
, f_type
, EA_CUSTOM(id
));
180 strcpy(cas
->name
, name
);
181 ca_storage
[id
] = cas
;
183 HASH_INSERT(ca_hash
, CA
, cas
);
186 struct custom_attribute
*ca
= ralloc(p
, &ca_class
);
187 ca
->fda
= &(cas
->fda
);
188 ca
->name
= cas
->name
;
193 ea_custom_name(uint ea
)
195 uint id
= EA_CUSTOM_ID(ea
);
196 if (id
>= ca_storage_max
)
202 return ca_storage
[id
]->name
;