]>
git.ipfire.org Git - thirdparty/bird.git/blob - filter/filter.c
2 * Filters: utility functions
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
13 * You can find sources of the filter language in |filter/|
14 * directory. File |filter/config.Y| contains filter grammar and basically translates
15 * the source from user into a tree of &f_inst structures. These trees are
16 * later interpreted using code in |filter/filter.c|.
18 * A filter is represented by a tree of &f_inst structures, one structure per
19 * "instruction". Each &f_inst contains @code, @aux value which is
20 * usually the data type this instruction operates on and two generic
21 * arguments (@a1, @a2). Some instructions contain pointer(s) to other
22 * instructions in their (@a1, @a2) fields.
24 * Filters use a &f_val structure for their data. Each &f_val
25 * contains type and value (types are constants prefixed with %T_). Few
26 * of the types are special; %T_RETURN can be or-ed with a type to indicate
27 * that return from a function or from the whole filter should be
28 * forced. Important thing about &f_val's is that they may be copied
29 * with a simple |=|. That's fine for all currently defined types: strings
30 * are read-only (and therefore okay), paths are copied for each
31 * operation (okay too).
36 #include "nest/bird.h"
37 #include "lib/lists.h"
38 #include "lib/resource.h"
39 #include "lib/socket.h"
40 #include "lib/string.h"
41 #include "lib/unaligned.h"
42 #include "nest/route.h"
43 #include "nest/protocol.h"
44 #include "nest/iface.h"
45 #include "nest/attrs.h"
46 #include "conf/conf.h"
47 #include "filter/filter.h"
52 adata_empty(struct linpool
*pool
, int l
)
54 struct adata
*res
= lp_alloc(pool
, sizeof(struct adata
) + l
);
60 pm_format(struct f_path_mask
*p
, buffer
*buf
)
62 buffer_puts(buf
, "[= ");
69 buffer_print(buf
, "%u ", p
->val
);
73 buffer_puts(buf
, "? ");
77 buffer_puts(buf
, "* ");
81 buffer_print(buf
, "%u..%u ", p
->val
, p
->val2
);
85 buffer_print(buf
, "%u ", f_eval_asn((struct f_inst
*) p
->val
));
92 buffer_puts(buf
, "=]");
96 uint_cmp(uint i1
, uint i2
)
98 return (int)(i1
> i2
) - (int)(i1
< i2
);
102 u64_cmp(u64 i1
, u64 i2
)
104 return (int)(i1
> i2
) - (int)(i1
< i2
);
108 lcomm_cmp(lcomm v1
, lcomm v2
)
110 if (v1
.asn
!= v2
.asn
)
111 return (v1
.asn
> v2
.asn
) ? 1 : -1;
112 if (v1
.ldp1
!= v2
.ldp1
)
113 return (v1
.ldp1
> v2
.ldp1
) ? 1 : -1;
114 if (v1
.ldp2
!= v2
.ldp2
)
115 return (v1
.ldp2
> v2
.ldp2
) ? 1 : -1;
120 * val_compare - compare two values
124 * Compares two values and returns -1, 0, 1 on <, =, > or CMP_ERROR on
125 * error. Tree module relies on this giving consistent results so
126 * that it can be used for building balanced trees.
129 val_compare(struct f_val v1
, struct f_val v2
)
133 if (v1
.type
!= v2
.type
) {
134 if (v1
.type
== T_VOID
) /* Hack for else */
136 if (v2
.type
== T_VOID
)
140 /* IP->Quad implicit conversion */
141 if ((v1
.type
== T_QUAD
) && (v2
.type
== T_IP
))
142 return uint_cmp(v1
.val
.i
, ipa_to_u32(v2
.val
.px
.ip
));
143 if ((v1
.type
== T_IP
) && (v2
.type
== T_QUAD
))
144 return uint_cmp(ipa_to_u32(v1
.val
.px
.ip
), v2
.val
.i
);
147 debug( "Types do not match in val_compare\n" );
159 return uint_cmp(v1
.val
.i
, v2
.val
.i
);
161 return u64_cmp(v1
.val
.ec
, v2
.val
.ec
);
163 return lcomm_cmp(v1
.val
.lc
, v2
.val
.lc
);
165 return ipa_compare(v1
.val
.px
.ip
, v2
.val
.px
.ip
);
167 if (rc
= ipa_compare(v1
.val
.px
.ip
, v2
.val
.px
.ip
))
169 return uint_cmp(v1
.val
.px
.len
, v2
.val
.px
.len
);
171 return strcmp(v1
.val
.s
, v2
.val
.s
);
178 pm_same(struct f_path_mask
*m1
, struct f_path_mask
*m2
)
182 if (m1
->kind
!= m2
->kind
)
185 if (m1
->kind
== PM_ASN_EXPR
)
187 if (!i_same((struct f_inst
*) m1
->val
, (struct f_inst
*) m2
->val
))
192 if ((m1
->val
!= m2
->val
) || (m1
->val2
!= m2
->val2
))
204 * val_same - compare two values
208 * Compares two values and returns 1 if they are same and 0 if not.
209 * Comparison of values of different types is valid and returns 0.
212 val_same(struct f_val v1
, struct f_val v2
)
216 rc
= val_compare(v1
, v2
);
220 if (v1
.type
!= v2
.type
)
225 return pm_same(v1
.val
.path_mask
, v2
.val
.path_mask
);
230 return adata_same(v1
.val
.ad
, v2
.val
.ad
);
232 return same_tree(v1
.val
.t
, v2
.val
.t
);
234 return trie_same(v1
.val
.ti
, v2
.val
.ti
);
236 bug("Invalid type in val_same(): %x", v1
.type
);
241 fprefix_get_bounds(struct f_prefix
*px
, int *l
, int *h
)
243 *l
= *h
= px
->len
& LEN_MASK
;
245 if (px
->len
& LEN_MINUS
)
248 else if (px
->len
& LEN_PLUS
)
249 *h
= MAX_PREFIX_LENGTH
;
251 else if (px
->len
& LEN_RANGE
)
253 *l
= 0xff & (px
->len
>> 16);
254 *h
= 0xff & (px
->len
>> 8);
259 clist_set_type(struct f_tree
*set
, struct f_val
*v
)
261 switch (set
->from
.type
) {
279 eclist_set_type(struct f_tree
*set
)
280 { return set
->from
.type
== T_EC
; }
283 lclist_set_type(struct f_tree
*set
)
284 { return set
->from
.type
== T_LC
; }
287 clist_match_set(struct adata
*clist
, struct f_tree
*set
)
293 if (!clist_set_type(set
, &v
))
296 u32
*l
= (u32
*) clist
->data
;
297 u32
*end
= l
+ clist
->length
/4;
301 if (find_tree(set
, v
))
308 eclist_match_set(struct adata
*list
, struct f_tree
*set
)
313 if (!eclist_set_type(set
))
317 u32
*l
= int_set_get_data(list
);
318 int len
= int_set_get_size(list
);
322 for (i
= 0; i
< len
; i
+= 2) {
323 v
.val
.ec
= ec_get(l
, i
);
324 if (find_tree(set
, v
))
332 lclist_match_set(struct adata
*list
, struct f_tree
*set
)
337 if (!lclist_set_type(set
))
341 u32
*l
= int_set_get_data(list
);
342 int len
= int_set_get_size(list
);
346 for (i
= 0; i
< len
; i
+= 3) {
347 v
.val
.lc
= lc_get(l
, i
);
348 if (find_tree(set
, v
))
355 static struct adata
*
356 clist_filter(struct linpool
*pool
, struct adata
*list
, struct f_val set
, int pos
)
361 int tree
= (set
.type
== T_SET
); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
364 clist_set_type(set
.val
.t
, &v
);
368 int len
= int_set_get_size(list
);
369 u32
*l
= int_set_get_data(list
);
376 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
377 if ((tree
? !!find_tree(set
.val
.t
, v
) : int_set_contains(set
.val
.ad
, v
.val
.i
)) == pos
)
381 uint nl
= (k
- tmp
) * sizeof(u32
);
382 if (nl
== list
->length
)
385 struct adata
*res
= adata_empty(pool
, nl
);
386 memcpy(res
->data
, tmp
, nl
);
390 static struct adata
*
391 eclist_filter(struct linpool
*pool
, struct adata
*list
, struct f_val set
, int pos
)
396 int tree
= (set
.type
== T_SET
); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
399 int len
= int_set_get_size(list
);
400 u32
*l
= int_set_get_data(list
);
406 for (i
= 0; i
< len
; i
+= 2) {
407 v
.val
.ec
= ec_get(l
, i
);
408 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
409 if ((tree
? !!find_tree(set
.val
.t
, v
) : ec_set_contains(set
.val
.ad
, v
.val
.ec
)) == pos
) {
415 uint nl
= (k
- tmp
) * sizeof(u32
);
416 if (nl
== list
->length
)
419 struct adata
*res
= adata_empty(pool
, nl
);
420 memcpy(res
->data
, tmp
, nl
);
424 static struct adata
*
425 lclist_filter(struct linpool
*pool
, struct adata
*list
, struct f_val set
, int pos
)
430 int tree
= (set
.type
== T_SET
); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
433 int len
= int_set_get_size(list
);
434 u32
*l
= int_set_get_data(list
);
440 for (i
= 0; i
< len
; i
+= 3) {
441 v
.val
.lc
= lc_get(l
, i
);
442 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
443 if ((tree
? !!find_tree(set
.val
.t
, v
) : lc_set_contains(set
.val
.ad
, v
.val
.lc
)) == pos
)
447 uint nl
= (k
- tmp
) * sizeof(u32
);
448 if (nl
== list
->length
)
451 struct adata
*res
= adata_empty(pool
, nl
);
452 memcpy(res
->data
, tmp
, nl
);
457 * val_in_range - implement |~| operator
461 * Checks if @v1 is element (|~| operator) of @v2.
464 val_in_range(struct f_val v1
, struct f_val v2
)
466 if ((v1
.type
== T_PATH
) && (v2
.type
== T_PATH_MASK
))
467 return as_path_match(v1
.val
.ad
, v2
.val
.path_mask
);
469 if ((v1
.type
== T_INT
) && (v2
.type
== T_PATH
))
470 return as_path_contains(v2
.val
.ad
, v1
.val
.i
, 1);
472 if (((v1
.type
== T_PAIR
) || (v1
.type
== T_QUAD
)) && (v2
.type
== T_CLIST
))
473 return int_set_contains(v2
.val
.ad
, v1
.val
.i
);
475 /* IP->Quad implicit conversion */
476 if ((v1
.type
== T_IP
) && (v2
.type
== T_CLIST
))
477 return int_set_contains(v2
.val
.ad
, ipa_to_u32(v1
.val
.px
.ip
));
480 if ((v1
.type
== T_EC
) && (v2
.type
== T_ECLIST
))
481 return ec_set_contains(v2
.val
.ad
, v1
.val
.ec
);
483 if ((v1
.type
== T_LC
) && (v2
.type
== T_LCLIST
))
484 return lc_set_contains(v2
.val
.ad
, v1
.val
.lc
);
486 if ((v1
.type
== T_STRING
) && (v2
.type
== T_STRING
))
487 return patmatch(v2
.val
.s
, v1
.val
.s
);
489 if ((v1
.type
== T_IP
) && (v2
.type
== T_PREFIX
))
490 return ipa_in_net(v1
.val
.px
.ip
, v2
.val
.px
.ip
, v2
.val
.px
.len
);
492 if ((v1
.type
== T_PREFIX
) && (v2
.type
== T_PREFIX
))
493 return net_in_net(v1
.val
.px
.ip
, v1
.val
.px
.len
, v2
.val
.px
.ip
, v2
.val
.px
.len
);
495 if ((v1
.type
== T_PREFIX
) && (v2
.type
== T_PREFIX_SET
))
496 return trie_match_fprefix(v2
.val
.ti
, &v1
.val
.px
);
498 if (v2
.type
!= T_SET
)
501 /* With integrated Quad<->IP implicit conversion */
502 if ((v1
.type
== v2
.val
.t
->from
.type
) ||
503 ((IP_VERSION
== 4) && (v1
.type
== T_QUAD
) && (v2
.val
.t
->from
.type
== T_IP
)))
504 return !!find_tree(v2
.val
.t
, v1
);
506 if (v1
.type
== T_CLIST
)
507 return clist_match_set(v1
.val
.ad
, v2
.val
.t
);
509 if (v1
.type
== T_ECLIST
)
510 return eclist_match_set(v1
.val
.ad
, v2
.val
.t
);
512 if (v1
.type
== T_LCLIST
)
513 return lclist_match_set(v1
.val
.ad
, v2
.val
.t
);
515 if (v1
.type
== T_PATH
)
516 return as_path_match_set(v1
.val
.ad
, v2
.val
.t
);
522 * val_format - format filter value
525 val_format(struct f_val v
, buffer
*buf
)
530 case T_VOID
: buffer_puts(buf
, "(void)"); return;
531 case T_BOOL
: buffer_puts(buf
, v
.val
.i
? "TRUE" : "FALSE"); return;
532 case T_INT
: buffer_print(buf
, "%u", v
.val
.i
); return;
533 case T_STRING
: buffer_print(buf
, "%s", v
.val
.s
); return;
534 case T_IP
: buffer_print(buf
, "%I", v
.val
.px
.ip
); return;
535 case T_PREFIX
: buffer_print(buf
, "%I/%d", v
.val
.px
.ip
, v
.val
.px
.len
); return;
536 case T_PAIR
: buffer_print(buf
, "(%u,%u)", v
.val
.i
>> 16, v
.val
.i
& 0xffff); return;
537 case T_QUAD
: buffer_print(buf
, "%R", v
.val
.i
); return;
538 case T_EC
: ec_format(buf2
, v
.val
.ec
); buffer_print(buf
, "%s", buf2
); return;
539 case T_LC
: lc_format(buf2
, v
.val
.lc
); buffer_print(buf
, "%s", buf2
); return;
540 case T_PREFIX_SET
: trie_format(v
.val
.ti
, buf
); return;
541 case T_SET
: tree_format(v
.val
.t
, buf
); return;
542 case T_ENUM
: buffer_print(buf
, "(enum %x)%u", v
.type
, v
.val
.i
); return;
543 case T_PATH
: as_path_format(v
.val
.ad
, buf2
, 1000); buffer_print(buf
, "(path %s)", buf2
); return;
544 case T_CLIST
: int_set_format(v
.val
.ad
, 1, -1, buf2
, 1000); buffer_print(buf
, "(clist %s)", buf2
); return;
545 case T_ECLIST
: ec_set_format(v
.val
.ad
, -1, buf2
, 1000); buffer_print(buf
, "(eclist %s)", buf2
); return;
546 case T_LCLIST
: lc_set_format(v
.val
.ad
, -1, buf2
, 1000); buffer_print(buf
, "(lclist %s)", buf2
); return;
547 case T_PATH_MASK
: pm_format(v
.val
.path_mask
, buf
); return;
548 default: buffer_print(buf
, "[unknown type %x]", v
.type
); return;
552 static struct rte
**f_rte
;
553 static struct rta
*f_old_rta
;
554 static struct ea_list
**f_tmp_attrs
;
555 static struct linpool
*f_pool
;
556 static struct buffer f_buf
;
559 static inline void f_rte_cow(void)
561 *f_rte
= rte_cow(*f_rte
);
565 * rta_cow - prepare rta for modification by filter
570 if (!rta_is_cached((*f_rte
)->attrs
))
573 /* Prepare to modify rte */
576 /* Store old rta to free it later, it stores reference from rte_cow() */
577 f_old_rta
= (*f_rte
)->attrs
;
580 * Get shallow copy of rta. Fields eattrs and nexthops of rta are shared
581 * with f_old_rta (they will be copied when the cached rta will be obtained
582 * at the end of f_run()), also the lock of hostentry is inherited (we
583 * suppose hostentry is not changed by filters).
585 (*f_rte
)->attrs
= rta_do_cow((*f_rte
)->attrs
, f_pool
);
588 static struct tbf rl_runtime_err
= TBF_DEFAULT_LOG_LIMITS
;
590 #define runtime(x) do { \
591 if (!(f_flags & FF_SILENT)) \
592 log_rl(&rl_runtime_err, L_ERR "filters, line %d: %s", what->lineno, x); \
593 res.type = T_RETURN; \
594 res.val.i = F_ERROR; \
599 x = interpret(what->y); \
600 if (x.type & T_RETURN) \
603 #define ONEARG ARG(v1, a1.p)
604 #define TWOARGS ARG(v1, a1.p) \
606 #define TWOARGS_C TWOARGS \
607 if (v1.type != v2.type) \
608 runtime( "Can't operate with values of incompatible types" );
610 do { if (!f_rte) runtime("No route to access"); } while (0)
612 #define BITFIELD_MASK(what) \
613 (1u << (what->a2.i >> 24))
617 * @what: filter to interpret
619 * Interpret given tree of filter instructions. This is core function
620 * of filter system and does all the hard work.
622 * Each instruction has 4 fields: code (which is instruction code),
623 * aux (which is extension to instruction code, typically type),
624 * arg1 and arg2 - arguments. Depending on instruction, arguments
625 * are either integers, or pointers to instruction trees. Common
626 * instructions like +, that have two expressions as arguments use
627 * TWOARGS macro to get both of them evaluated.
629 * &f_val structures are copied around, so there are no problems with
633 interpret(struct f_inst
*what
)
636 struct f_val v1
, v2
, res
= { .type
= T_VOID
}, *vp
;
641 for ( ; what
; what
= what
->next
) {
643 switch(what
->fi_code
) {
648 /* Binary operators */
651 switch (res
.type
= v1
.type
) {
652 case T_VOID
: runtime( "Can't operate with values of type void" );
653 case T_INT
: res
.val
.i
= v1
.val
.i
+ v2
.val
.i
; break;
654 default: runtime( "Usage of unknown type" );
659 switch (res
.type
= v1
.type
) {
660 case T_VOID
: runtime( "Can't operate with values of type void" );
661 case T_INT
: res
.val
.i
= v1
.val
.i
- v2
.val
.i
; break;
662 default: runtime( "Usage of unknown type" );
667 switch (res
.type
= v1
.type
) {
668 case T_VOID
: runtime( "Can't operate with values of type void" );
669 case T_INT
: res
.val
.i
= v1
.val
.i
* v2
.val
.i
; break;
670 default: runtime( "Usage of unknown type" );
675 switch (res
.type
= v1
.type
) {
676 case T_VOID
: runtime( "Can't operate with values of type void" );
677 case T_INT
: if (v2
.val
.i
== 0) runtime( "Mother told me not to divide by 0" );
678 res
.val
.i
= v1
.val
.i
/ v2
.val
.i
; break;
679 default: runtime( "Usage of unknown type" );
686 if (v1
.type
!= T_BOOL
)
687 runtime( "Can't do boolean operation on non-booleans" );
688 if (v1
.val
.i
== (what
->fi_code
== FI_OR
)) {
690 res
.val
.i
= v1
.val
.i
;
695 if (v2
.type
!= T_BOOL
)
696 runtime( "Can't do boolean operation on non-booleans" );
698 res
.val
.i
= v2
.val
.i
;
701 case FI_PAIR_CONSTRUCT
:
703 if ((v1
.type
!= T_INT
) || (v2
.type
!= T_INT
))
704 runtime( "Can't operate with value of non-integer type in pair constructor" );
707 if ((u1
> 0xFFFF) || (u2
> 0xFFFF))
708 runtime( "Can't operate with value out of bounds in pair constructor" );
709 res
.val
.i
= (u1
<< 16) | u2
;
713 case FI_EC_CONSTRUCT
:
717 int check
, ipv4_used
;
720 if (v1
.type
== T_INT
) {
721 ipv4_used
= 0; key
= v1
.val
.i
;
723 else if (v1
.type
== T_QUAD
) {
724 ipv4_used
= 1; key
= v1
.val
.i
;
727 /* IP->Quad implicit conversion */
728 else if (v1
.type
== T_IP
) {
729 ipv4_used
= 1; key
= ipa_to_u32(v1
.val
.px
.ip
);
733 runtime("Can't operate with key of non-integer/IPv4 type in EC constructor");
735 if (v2
.type
!= T_INT
)
736 runtime("Can't operate with value of non-integer type in EC constructor");
742 if (what
->aux
== EC_GENERIC
) {
743 check
= 0; res
.val
.ec
= ec_generic(key
, val
);
745 else if (ipv4_used
) {
746 check
= 1; res
.val
.ec
= ec_ip4(what
->aux
, key
, val
);
748 else if (key
< 0x10000) {
749 check
= 0; res
.val
.ec
= ec_as2(what
->aux
, key
, val
);
752 check
= 1; res
.val
.ec
= ec_as4(what
->aux
, key
, val
);
755 if (check
&& (val
> 0xFFFF))
756 runtime("Can't operate with value out of bounds in EC constructor");
761 case FI_LC_CONSTRUCT
:
765 /* Third argument hack */
766 struct f_val v3
= interpret(INST3(what
).p
);
767 if (v3
.type
& T_RETURN
)
770 if ((v1
.type
!= T_INT
) || (v2
.type
!= T_INT
) || (v3
.type
!= T_INT
))
771 runtime( "Can't operate with value of non-integer type in LC constructor" );
774 res
.val
.lc
= (lcomm
) { v1
.val
.i
, v2
.val
.i
, v3
.val
.i
};
779 /* Relational operators */
783 i = val_compare(v1, v2); \
785 runtime( "Can't compare values of incompatible types" ); \
792 i = val_same(v1, v2); \
797 case FI_NEQ
: SAME(!i
);
799 case FI_LT
: COMPARE(i
==-1);
800 case FI_LTE
: COMPARE(i
!=1);
804 if (v1
.type
!= T_BOOL
)
805 runtime( "Not applied to non-boolean" );
807 res
.val
.i
= !res
.val
.i
;
813 res
.val
.i
= val_in_range(v1
, v2
);
814 if (res
.val
.i
== CMP_ERROR
)
815 runtime( "~ applied on unknown type pair" );
816 res
.val
.i
= !!res
.val
.i
;
822 res
.val
.i
= val_in_range(v1
, v2
);
823 if (res
.val
.i
== CMP_ERROR
)
824 runtime( "!~ applied on unknown type pair" );
825 res
.val
.i
= !res
.val
.i
;
831 res
.val
.i
= (v1
.type
!= T_VOID
);
834 /* Set to indirect value, a1 = variable, a2 = value */
839 if ((sym
->class != (SYM_VARIABLE
| v2
.type
)) && (v2
.type
!= T_VOID
)) {
841 /* IP->Quad implicit conversion */
842 if ((sym
->class == (SYM_VARIABLE
| T_QUAD
)) && (v2
.type
== T_IP
)) {
844 vp
->val
.i
= ipa_to_u32(v2
.val
.px
.ip
);
848 runtime( "Assigning to variable of incompatible type" );
853 /* some constants have value in a2, some in *a1.p, strange. */
854 case FI_CONSTANT
: /* integer (or simple type) constant, string, set, or prefix_set */
855 res
.type
= what
->aux
;
857 if (res
.type
== T_PREFIX_SET
)
858 res
.val
.ti
= what
->a2
.p
;
859 else if (res
.type
== T_SET
)
860 res
.val
.t
= what
->a2
.p
;
861 else if (res
.type
== T_STRING
)
862 res
.val
.s
= what
->a2
.p
;
864 res
.val
.i
= what
->a2
.i
;
867 case FI_CONSTANT_INDIRECT
:
868 res
= * ((struct f_val
*) what
->a1
.p
);
872 val_format(v1
, &f_buf
);
874 case FI_CONDITION
: /* ? has really strange error value, so we can implement if ... else nicely :-) */
876 if (v1
.type
!= T_BOOL
)
877 runtime( "If requires boolean expression" );
881 } else res
.val
.i
= 1;
885 debug( "No operation\n" );
887 case FI_PRINT_AND_DIE
:
889 if ((what
->a2
.i
== F_NOP
|| (what
->a2
.i
!= F_NONL
&& what
->a1
.p
)) &&
890 !(f_flags
& FF_SILENT
))
891 log_commit(*L_INFO
, &f_buf
);
893 switch (what
->a2
.i
) {
895 die( "Filter asked me to die" );
897 /* Should take care about turning ACCEPT into MODIFY */
899 case F_REJECT
: /* FIXME (noncritical) Should print complete route along with reason to reject route */
901 res
.val
.i
= what
->a2
.i
;
902 return res
; /* We have to return now, no more processing. */
907 bug( "unknown return type: Can't happen");
910 case FI_RTA_GET
: /* rta access */
913 struct rta
*rta
= (*f_rte
)->attrs
;
914 res
.type
= what
->aux
;
918 case SA_FROM
: res
.val
.px
.ip
= rta
->from
; break;
919 case SA_GW
: res
.val
.px
.ip
= rta
->gw
; break;
920 case SA_NET
: res
.val
.px
.ip
= (*f_rte
)->net
->n
.prefix
;
921 res
.val
.px
.len
= (*f_rte
)->net
->n
.pxlen
; break;
922 case SA_PROTO
: res
.val
.s
= rta
->src
->proto
->name
; break;
923 case SA_SOURCE
: res
.val
.i
= rta
->source
; break;
924 case SA_SCOPE
: res
.val
.i
= rta
->scope
; break;
925 case SA_CAST
: res
.val
.i
= rta
->cast
; break;
926 case SA_DEST
: res
.val
.i
= rta
->dest
; break;
927 case SA_IFNAME
: res
.val
.s
= rta
->iface
? rta
->iface
->name
: ""; break;
928 case SA_IFINDEX
: res
.val
.i
= rta
->iface
? rta
->iface
->index
: 0; break;
931 bug("Invalid static attribute access (%x)", res
.type
);
938 if (what
->aux
!= v1
.type
)
939 runtime( "Attempt to set static attribute to incompatible type" );
943 struct rta
*rta
= (*f_rte
)->attrs
;
948 rta
->from
= v1
.val
.px
.ip
;
953 ip_addr ip
= v1
.val
.px
.ip
;
954 neighbor
*n
= neigh_find(rta
->src
->proto
, &ip
, 0);
955 if (!n
|| (n
->scope
== SCOPE_HOST
))
956 runtime( "Invalid gw address" );
958 rta
->dest
= RTD_ROUTER
;
960 rta
->iface
= n
->iface
;
961 rta
->nexthops
= NULL
;
962 rta
->hostentry
= NULL
;
967 rta
->scope
= v1
.val
.i
;
972 if ((i
!= RTD_BLACKHOLE
) && (i
!= RTD_UNREACHABLE
) && (i
!= RTD_PROHIBIT
))
973 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
978 rta
->nexthops
= NULL
;
979 rta
->hostentry
= NULL
;
983 bug("Invalid static attribute access (%x)", res
.type
);
987 case FI_EA_GET
: /* Access to extended attributes */
991 u16 code
= what
->a2
.i
;
993 if (!(f_flags
& FF_FORCE_TMPATTR
))
994 e
= ea_find((*f_rte
)->attrs
->eattrs
, code
);
996 e
= ea_find((*f_tmp_attrs
), code
);
997 if ((!e
) && (f_flags
& FF_FORCE_TMPATTR
))
998 e
= ea_find((*f_rte
)->attrs
->eattrs
, code
);
1001 /* A special case: undefined int_set looks like empty int_set */
1002 if ((what
->aux
& EAF_TYPE_MASK
) == EAF_TYPE_INT_SET
) {
1004 res
.val
.ad
= adata_empty(f_pool
, 0);
1008 /* The same special case for ec_set */
1009 if ((what
->aux
& EAF_TYPE_MASK
) == EAF_TYPE_EC_SET
) {
1010 res
.type
= T_ECLIST
;
1011 res
.val
.ad
= adata_empty(f_pool
, 0);
1015 /* The same special case for lc_set */
1016 if ((what
->aux
& EAF_TYPE_MASK
) == EAF_TYPE_LC_SET
) {
1017 res
.type
= T_LCLIST
;
1018 res
.val
.ad
= adata_empty(f_pool
, 0);
1022 /* Undefined value */
1027 switch (what
->aux
& EAF_TYPE_MASK
) {
1030 res
.val
.i
= e
->u
.data
;
1032 case EAF_TYPE_ROUTER_ID
:
1034 res
.val
.i
= e
->u
.data
;
1036 case EAF_TYPE_OPAQUE
:
1037 res
.type
= T_ENUM_EMPTY
;
1040 case EAF_TYPE_IP_ADDRESS
:
1042 struct adata
* ad
= e
->u
.ptr
;
1043 res
.val
.px
.ip
= * (ip_addr
*) ad
->data
;
1045 case EAF_TYPE_AS_PATH
:
1047 res
.val
.ad
= e
->u
.ptr
;
1049 case EAF_TYPE_BITFIELD
:
1051 res
.val
.i
= !!(e
->u
.data
& BITFIELD_MASK(what
));
1053 case EAF_TYPE_INT_SET
:
1055 res
.val
.ad
= e
->u
.ptr
;
1057 case EAF_TYPE_EC_SET
:
1058 res
.type
= T_ECLIST
;
1059 res
.val
.ad
= e
->u
.ptr
;
1061 case EAF_TYPE_LC_SET
:
1062 res
.type
= T_LCLIST
;
1063 res
.val
.ad
= e
->u
.ptr
;
1065 case EAF_TYPE_UNDEF
:
1069 bug("Unknown type in e,a");
1077 struct ea_list
*l
= lp_alloc(f_pool
, sizeof(struct ea_list
) + sizeof(eattr
));
1078 u16 code
= what
->a2
.i
;
1081 l
->flags
= EALF_SORTED
;
1083 l
->attrs
[0].id
= code
;
1084 l
->attrs
[0].flags
= 0;
1085 l
->attrs
[0].type
= what
->aux
| EAF_ORIGINATED
;
1087 switch (what
->aux
& EAF_TYPE_MASK
) {
1089 // Enums are also ints, so allow them in.
1090 if (v1
.type
!= T_INT
&& (v1
.type
< T_ENUM_LO
|| v1
.type
> T_ENUM_HI
))
1091 runtime( "Setting int attribute to non-int value" );
1092 l
->attrs
[0].u
.data
= v1
.val
.i
;
1095 case EAF_TYPE_ROUTER_ID
:
1097 /* IP->Quad implicit conversion */
1098 if (v1
.type
== T_IP
) {
1099 l
->attrs
[0].u
.data
= ipa_to_u32(v1
.val
.px
.ip
);
1103 /* T_INT for backward compatibility */
1104 if ((v1
.type
!= T_QUAD
) && (v1
.type
!= T_INT
))
1105 runtime( "Setting quad attribute to non-quad value" );
1106 l
->attrs
[0].u
.data
= v1
.val
.i
;
1109 case EAF_TYPE_OPAQUE
:
1110 runtime( "Setting opaque attribute is not allowed" );
1112 case EAF_TYPE_IP_ADDRESS
:
1113 if (v1
.type
!= T_IP
)
1114 runtime( "Setting ip attribute to non-ip value" );
1115 int len
= sizeof(ip_addr
);
1116 struct adata
*ad
= lp_alloc(f_pool
, sizeof(struct adata
) + len
);
1118 (* (ip_addr
*) ad
->data
) = v1
.val
.px
.ip
;
1119 l
->attrs
[0].u
.ptr
= ad
;
1121 case EAF_TYPE_AS_PATH
:
1122 if (v1
.type
!= T_PATH
)
1123 runtime( "Setting path attribute to non-path value" );
1124 l
->attrs
[0].u
.ptr
= v1
.val
.ad
;
1126 case EAF_TYPE_BITFIELD
:
1127 if (v1
.type
!= T_BOOL
)
1128 runtime( "Setting bit in bitfield attribute to non-bool value" );
1130 /* First, we have to find the old value */
1132 if (!(f_flags
& FF_FORCE_TMPATTR
))
1133 e
= ea_find((*f_rte
)->attrs
->eattrs
, code
);
1135 e
= ea_find((*f_tmp_attrs
), code
);
1136 if ((!e
) && (f_flags
& FF_FORCE_TMPATTR
))
1137 e
= ea_find((*f_rte
)->attrs
->eattrs
, code
);
1138 u32 data
= e
? e
->u
.data
: 0;
1141 l
->attrs
[0].u
.data
= data
| BITFIELD_MASK(what
);
1143 l
->attrs
[0].u
.data
= data
& ~BITFIELD_MASK(what
);;
1146 case EAF_TYPE_INT_SET
:
1147 if (v1
.type
!= T_CLIST
)
1148 runtime( "Setting clist attribute to non-clist value" );
1149 l
->attrs
[0].u
.ptr
= v1
.val
.ad
;
1151 case EAF_TYPE_EC_SET
:
1152 if (v1
.type
!= T_ECLIST
)
1153 runtime( "Setting eclist attribute to non-eclist value" );
1154 l
->attrs
[0].u
.ptr
= v1
.val
.ad
;
1156 case EAF_TYPE_LC_SET
:
1157 if (v1
.type
!= T_LCLIST
)
1158 runtime( "Setting lclist attribute to non-lclist value" );
1159 l
->attrs
[0].u
.ptr
= v1
.val
.ad
;
1161 case EAF_TYPE_UNDEF
:
1162 if (v1
.type
!= T_VOID
)
1163 runtime( "Setting void attribute to non-void value" );
1164 l
->attrs
[0].u
.data
= 0;
1166 default: bug("Unknown type in e,S");
1169 if (!(what
->aux
& EAF_TEMP
) && (!(f_flags
& FF_FORCE_TMPATTR
))) {
1171 l
->next
= (*f_rte
)->attrs
->eattrs
;
1172 (*f_rte
)->attrs
->eattrs
= l
;
1174 l
->next
= (*f_tmp_attrs
);
1182 res
.val
.i
= (*f_rte
)->pref
;
1187 if (v1
.type
!= T_INT
)
1188 runtime( "Can't set preference to non-integer" );
1189 if (v1
.val
.i
> 0xFFFF)
1190 runtime( "Setting preference value out of bounds" );
1192 (*f_rte
)->pref
= v1
.val
.i
;
1194 case FI_LENGTH
: /* Get length of */
1198 case T_PREFIX
: res
.val
.i
= v1
.val
.px
.len
; break;
1199 case T_PATH
: res
.val
.i
= as_path_getlen(v1
.val
.ad
); break;
1200 case T_CLIST
: res
.val
.i
= int_set_get_size(v1
.val
.ad
); break;
1201 case T_ECLIST
: res
.val
.i
= ec_set_get_size(v1
.val
.ad
); break;
1202 case T_LCLIST
: res
.val
.i
= lc_set_get_size(v1
.val
.ad
); break;
1203 default: runtime( "Prefix, path, clist or eclist expected" );
1206 case FI_IP
: /* Convert prefix to ... */
1208 if (v1
.type
!= T_PREFIX
)
1209 runtime( "Prefix expected" );
1210 res
.type
= what
->aux
;
1212 /* case T_INT: res.val.i = v1.val.px.len; break; Not needed any more */
1213 case T_IP
: res
.val
.px
.ip
= v1
.val
.px
.ip
; break;
1214 default: bug( "Unknown prefix to conversion" );
1217 case FI_AS_PATH_FIRST
: /* Get first ASN from AS PATH */
1219 if (v1
.type
!= T_PATH
)
1220 runtime( "AS path expected" );
1223 as_path_get_first(v1
.val
.ad
, &as
);
1227 case FI_AS_PATH_LAST
: /* Get last ASN from AS PATH */
1229 if (v1
.type
!= T_PATH
)
1230 runtime( "AS path expected" );
1233 as_path_get_last(v1
.val
.ad
, &as
);
1237 case FI_AS_PATH_LAST_NAG
: /* Get last ASN from non-aggregated part of AS PATH */
1239 if (v1
.type
!= T_PATH
)
1240 runtime( "AS path expected" );
1243 res
.val
.i
= as_path_get_last_nonaggregated(v1
.val
.ad
);
1248 res
.type
|= T_RETURN
;
1250 case FI_CALL
: /* CALL: this is special: if T_RETURN and returning some value, mask it out */
1252 res
= interpret(what
->a2
.p
);
1253 if (res
.type
== T_RETURN
)
1255 res
.type
&= ~T_RETURN
;
1257 case FI_CLEAR_LOCAL_VARS
: /* Clear local variables */
1258 for (sym
= what
->a1
.p
; sym
!= NULL
; sym
= sym
->aux2
)
1259 ((struct f_val
*) sym
->def
)->type
= T_VOID
;
1264 struct f_tree
*t
= find_tree(what
->a2
.p
, v1
);
1267 t
= find_tree(what
->a2
.p
, v1
);
1269 debug( "No else statement?\n");
1273 /* It is actually possible to have t->data NULL */
1275 res
= interpret(t
->data
);
1276 if (res
.type
& T_RETURN
)
1280 case FI_IP_MASK
: /* IP.MASK(val) */
1282 if (v2
.type
!= T_INT
)
1283 runtime( "Integer expected");
1284 if (v1
.type
!= T_IP
)
1285 runtime( "You can mask only IP addresses" );
1287 ip_addr mask
= ipa_mkmask(v2
.val
.i
);
1289 res
.val
.px
.ip
= ipa_and(mask
, v1
.val
.px
.ip
);
1293 case FI_EMPTY
: /* Create empty attribute */
1294 res
.type
= what
->aux
;
1295 res
.val
.ad
= adata_empty(f_pool
, 0);
1297 case FI_PATH_PREPEND
: /* Path prepend */
1299 if (v1
.type
!= T_PATH
)
1300 runtime("Can't prepend to non-path");
1301 if (v2
.type
!= T_INT
)
1302 runtime("Can't prepend non-integer");
1305 res
.val
.ad
= as_path_prepend(f_pool
, v1
.val
.ad
, v2
.val
.i
);
1308 case FI_CLIST_ADD_DEL
: /* (Extended) Community list add or delete */
1310 if (v1
.type
== T_PATH
)
1312 struct f_tree
*set
= NULL
;
1316 if (v2
.type
== T_INT
)
1318 else if ((v2
.type
== T_SET
) && (v2
.val
.t
->from
.type
== T_INT
))
1321 runtime("Can't delete non-integer (set)");
1325 case 'a': runtime("Can't add to path");
1326 case 'd': pos
= 0; break;
1327 case 'f': pos
= 1; break;
1328 default: bug("unknown Ca operation");
1332 runtime("Can't filter integer");
1335 res
.val
.ad
= as_path_filter(f_pool
, v1
.val
.ad
, set
, key
, pos
);
1337 else if (v1
.type
== T_CLIST
)
1339 /* Community (or cluster) list */
1344 if ((v2
.type
== T_PAIR
) || (v2
.type
== T_QUAD
))
1347 /* IP->Quad implicit conversion */
1348 else if (v2
.type
== T_IP
)
1349 n
= ipa_to_u32(v2
.val
.px
.ip
);
1351 else if ((v2
.type
== T_SET
) && clist_set_type(v2
.val
.t
, &dummy
))
1353 else if (v2
.type
== T_CLIST
)
1356 runtime("Can't add/delete non-pair");
1363 runtime("Can't add set");
1365 res
.val
.ad
= int_set_add(f_pool
, v1
.val
.ad
, n
);
1367 res
.val
.ad
= int_set_union(f_pool
, v1
.val
.ad
, v2
.val
.ad
);
1372 res
.val
.ad
= int_set_del(f_pool
, v1
.val
.ad
, n
);
1374 res
.val
.ad
= clist_filter(f_pool
, v1
.val
.ad
, v2
, 0);
1379 runtime("Can't filter pair");
1380 res
.val
.ad
= clist_filter(f_pool
, v1
.val
.ad
, v2
, 1);
1384 bug("unknown Ca operation");
1387 else if (v1
.type
== T_ECLIST
)
1389 /* Extended community list */
1392 /* v2.val is either EC or EC-set */
1393 if ((v2
.type
== T_SET
) && eclist_set_type(v2
.val
.t
))
1395 else if (v2
.type
== T_ECLIST
)
1397 else if (v2
.type
!= T_EC
)
1398 runtime("Can't add/delete non-ec");
1400 res
.type
= T_ECLIST
;
1405 runtime("Can't add set");
1407 res
.val
.ad
= ec_set_add(f_pool
, v1
.val
.ad
, v2
.val
.ec
);
1409 res
.val
.ad
= ec_set_union(f_pool
, v1
.val
.ad
, v2
.val
.ad
);
1414 res
.val
.ad
= ec_set_del(f_pool
, v1
.val
.ad
, v2
.val
.ec
);
1416 res
.val
.ad
= eclist_filter(f_pool
, v1
.val
.ad
, v2
, 0);
1421 runtime("Can't filter ec");
1422 res
.val
.ad
= eclist_filter(f_pool
, v1
.val
.ad
, v2
, 1);
1426 bug("unknown Ca operation");
1429 else if (v1
.type
== T_LCLIST
)
1431 /* Large community list */
1434 /* v2.val is either LC or LC-set */
1435 if ((v2
.type
== T_SET
) && lclist_set_type(v2
.val
.t
))
1437 else if (v2
.type
== T_LCLIST
)
1439 else if (v2
.type
!= T_LC
)
1440 runtime("Can't add/delete non-lc");
1442 res
.type
= T_LCLIST
;
1447 runtime("Can't add set");
1449 res
.val
.ad
= lc_set_add(f_pool
, v1
.val
.ad
, v2
.val
.lc
);
1451 res
.val
.ad
= lc_set_union(f_pool
, v1
.val
.ad
, v2
.val
.ad
);
1456 res
.val
.ad
= lc_set_del(f_pool
, v1
.val
.ad
, v2
.val
.lc
);
1458 res
.val
.ad
= lclist_filter(f_pool
, v1
.val
.ad
, v2
, 0);
1463 runtime("Can't filter lc");
1464 res
.val
.ad
= lclist_filter(f_pool
, v1
.val
.ad
, v2
, 1);
1468 bug("unknown Ca operation");
1472 runtime("Can't add/delete to non-[e|l]clist");
1476 case FI_ROA_CHECK
: /* ROA Check */
1480 if ((v1
.type
!= T_PREFIX
) || (v2
.type
!= T_INT
))
1481 runtime("Invalid argument to roa_check()");
1488 v1
.val
.px
.ip
= (*f_rte
)->net
->n
.prefix
;
1489 v1
.val
.px
.len
= (*f_rte
)->net
->n
.pxlen
;
1491 /* We ignore temporary attributes, probably not a problem here */
1492 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
1493 eattr
*e
= ea_find((*f_rte
)->attrs
->eattrs
, EA_CODE(EAP_BGP
, 0x02));
1495 if (!e
|| e
->type
!= EAF_TYPE_AS_PATH
)
1496 runtime("Missing AS_PATH attribute");
1498 as_path_get_last(e
->u
.ptr
, &as
);
1501 struct roa_table_config
*rtc
= ((struct f_inst_roa_check
*) what
)->rtc
;
1503 runtime("Missing ROA table");
1505 res
.type
= T_ENUM_ROA
;
1506 res
.val
.i
= roa_check(rtc
->table
, v1
.val
.px
.ip
, v1
.val
.px
.len
, as
);
1510 bug( "Unknown instruction %d (%c)", what
->fi_code
, what
->fi_code
& 0xff);
1517 if (!i_same(f1->y, f2->y)) \
1520 #define ONEARG ARG(v1, a1.p)
1521 #define TWOARGS ARG(v1, a1.p) \
1524 #define A2_SAME if (f1->a2.i != f2->a2.i) return 0;
1527 * i_same - function that does real comparing of instruction trees, you should call filter_same from outside
1530 i_same(struct f_inst
*f1
, struct f_inst
*f2
)
1532 if ((!!f1
) != (!!f2
))
1536 if (f1
->aux
!= f2
->aux
)
1538 if (f1
->fi_code
!= f2
->fi_code
)
1540 if (f1
== f2
) /* It looks strange, but it is possible with call rewriting trickery */
1543 switch(f1
->fi_code
) {
1544 case FI_COMMA
: /* fall through */
1551 case FI_PAIR_CONSTRUCT
:
1552 case FI_EC_CONSTRUCT
:
1556 case FI_LTE
: TWOARGS
; break;
1558 case FI_NOT
: ONEARG
; break;
1560 case FI_MATCH
: TWOARGS
; break;
1561 case FI_DEFINED
: ONEARG
; break;
1563 case FI_LC_CONSTRUCT
:
1565 if (!i_same(INST3(f1
).p
, INST3(f2
).p
))
1572 struct symbol
*s1
, *s2
;
1575 if (strcmp(s1
->name
, s2
->name
))
1577 if (s1
->class != s2
->class)
1586 if (!trie_same(f1
->a2
.p
, f2
->a2
.p
))
1591 if (!same_tree(f1
->a2
.p
, f2
->a2
.p
))
1596 if (strcmp(f1
->a2
.p
, f2
->a2
.p
))
1605 case FI_CONSTANT_INDIRECT
:
1606 if (!val_same(* (struct f_val
*) f1
->a1
.p
, * (struct f_val
*) f2
->a1
.p
))
1611 if (strcmp((char *) f1
->a2
.p
, (char *) f2
->a2
.p
))
1614 case FI_PRINT
: case FI_LENGTH
: ONEARG
; break;
1615 case FI_CONDITION
: TWOARGS
; break;
1616 case FI_NOP
: case FI_EMPTY
: break;
1617 case FI_PRINT_AND_DIE
: ONEARG
; A2_SAME
; break;
1619 case FI_RTA_GET
: A2_SAME
; break;
1620 case FI_EA_GET
: A2_SAME
; break;
1623 case FI_EA_SET
: ONEARG
; A2_SAME
; break;
1625 case FI_RETURN
: ONEARG
; break;
1626 case FI_IP
: ONEARG
; break;
1627 case FI_CALL
: /* Call rewriting trickery to avoid exponential behaviour */
1629 if (!i_same(f1
->a2
.p
, f2
->a2
.p
))
1631 f2
->a2
.p
= f1
->a2
.p
;
1633 case FI_CLEAR_LOCAL_VARS
: break; /* internal instruction */
1634 case FI_SWITCH
: ONEARG
; if (!same_tree(f1
->a2
.p
, f2
->a2
.p
)) return 0; break;
1635 case FI_IP_MASK
: TWOARGS
; break;
1636 case FI_PATH_PREPEND
: TWOARGS
; break;
1637 case FI_CLIST_ADD_DEL
: TWOARGS
; break;
1638 case FI_AS_PATH_FIRST
:
1639 case FI_AS_PATH_LAST
:
1640 case FI_AS_PATH_LAST_NAG
: ONEARG
; break;
1643 /* Does not really make sense - ROA check resuls may change anyway */
1644 if (strcmp(((struct f_inst_roa_check
*) f1
)->rtc
->name
,
1645 ((struct f_inst_roa_check
*) f2
)->rtc
->name
))
1649 bug( "Unknown instruction %d in same (%c)", f1
->fi_code
, f1
->fi_code
& 0xff);
1651 return i_same(f1
->next
, f2
->next
);
1655 * f_run - run a filter for a route
1656 * @filter: filter to run
1657 * @rte: route being filtered, may be modified
1658 * @tmp_attrs: temporary attributes, prepared by caller or generated by f_run()
1659 * @tmp_pool: all filter allocations go from this pool
1662 * If filter needs to modify the route, there are several
1663 * posibilities. @rte might be read-only (with REF_COW flag), in that
1664 * case rw copy is obtained by rte_cow() and @rte is replaced. If
1665 * @rte is originally rw, it may be directly modified (and it is never
1668 * The returned rte may reuse the (possibly cached, cloned) rta, or
1669 * (if rta was modificied) contains a modified uncached rta, which
1670 * uses parts allocated from @tmp_pool and parts shared from original
1671 * rta. There is one exception - if @rte is rw but contains a cached
1672 * rta and that is modified, rta in returned rte is also cached.
1674 * Ownership of cached rtas is consistent with rte, i.e.
1675 * if a new rte is returned, it has its own clone of cached rta
1676 * (and cached rta of read-only source rte is intact), if rte is
1677 * modified in place, old cached rta is possibly freed.
1680 f_run(struct filter
*filter
, struct rte
**rte
, struct ea_list
**tmp_attrs
, struct linpool
*tmp_pool
, int flags
)
1682 if (filter
== FILTER_ACCEPT
)
1685 if (filter
== FILTER_REJECT
)
1688 int rte_cow
= ((*rte
)->flags
& REF_COW
);
1689 DBG( "Running filter `%s'...", filter
->name
);
1693 f_tmp_attrs
= tmp_attrs
;
1697 LOG_BUFFER_INIT(f_buf
);
1699 struct f_val res
= interpret(filter
->root
);
1703 * Cached rta was modified and f_rte contains now an uncached one,
1704 * sharing some part with the cached one. The cached rta should
1705 * be freed (if rte was originally COW, f_old_rta is a clone
1706 * obtained during rte_cow()).
1708 * This also implements the exception mentioned in f_run()
1709 * description. The reason for this is that rta reuses parts of
1710 * f_old_rta, and these may be freed during rta_free(f_old_rta).
1711 * This is not the problem if rte was COW, because original rte
1712 * also holds the same rta.
1715 (*f_rte
)->attrs
= rta_lookup((*f_rte
)->attrs
);
1717 rta_free(f_old_rta
);
1721 if (res
.type
!= T_RETURN
) {
1722 if (!(f_flags
& FF_SILENT
))
1723 log_rl(&rl_runtime_err
, L_ERR
"Filter %s did not return accept nor reject. Make up your mind", filter
->name
);
1726 DBG( "done (%u)\n", res
.val
.i
);
1730 /* TODO: perhaps we could integrate f_eval(), f_eval_rte() and f_run() */
1733 f_eval_rte(struct f_inst
*expr
, struct rte
**rte
, struct linpool
*tmp_pool
)
1735 struct ea_list
*tmp_attrs
= NULL
;
1739 f_tmp_attrs
= &tmp_attrs
;
1743 LOG_BUFFER_INIT(f_buf
);
1745 /* Note that in this function we assume that rte->attrs is private / uncached */
1746 struct f_val res
= interpret(expr
);
1748 /* Hack to include EAF_TEMP attributes to the main list */
1749 (*rte
)->attrs
->eattrs
= ea_append(tmp_attrs
, (*rte
)->attrs
->eattrs
);
1755 f_eval(struct f_inst
*expr
, struct linpool
*tmp_pool
)
1762 LOG_BUFFER_INIT(f_buf
);
1764 return interpret(expr
);
1768 f_eval_int(struct f_inst
*expr
)
1770 /* Called independently in parse-time to eval expressions */
1771 struct f_val res
= f_eval(expr
, cfg_mem
);
1773 if (res
.type
!= T_INT
)
1774 cf_error("Integer expression expected");
1780 f_eval_asn(struct f_inst
*expr
)
1782 /* Called as a part of another interpret call, therefore no log_reset() */
1783 struct f_val res
= interpret(expr
);
1784 return (res
.type
== T_INT
) ? res
.val
.i
: 0;
1788 * filter_same - compare two filters
1789 * @new: first filter to be compared
1790 * @old: second filter to be compared, notice that this filter is
1791 * damaged while comparing.
1793 * Returns 1 in case filters are same, otherwise 0. If there are
1794 * underlying bugs, it will rather say 0 on same filters than say
1798 filter_same(struct filter
*new, struct filter
*old
)
1800 if (old
== new) /* Handle FILTER_ACCEPT and FILTER_REJECT */
1802 if (old
== FILTER_ACCEPT
|| old
== FILTER_REJECT
||
1803 new == FILTER_ACCEPT
|| new == FILTER_REJECT
)
1805 return i_same(new->root
, old
->root
);