rt: introduce routing expression
PATCH 1/1 rt: introduce routing expression
From: Anders K. Pedersen <[email protected]>
Introduce rt expression for routing related data with support for nexthop (i.e. the directly connected IP address that an outgoing packet is sent to), which can be used either for matching or accounting, eg.
# nft add rule filter postrouting \ ip daddr 192.168.1.0/24 rt ip nexthop != 192.168.0.1 drop
This will drop any traffic to 192.168.1.0/24 that is not routed via 192.168.0.1.
# nft add rule filter postrouting \
flow table acct { rt ip nexthop timeout 600s counter }
# nft add rule ip6 filter postrouting \
flow table acct { rt ip6 nexthop6 timeout 600s counter }These rules count outgoing traffic per nexthop. Note that the timeout releases an entry if no traffic is seen for this nexthop within 10 minutes.
# nft add rule inet filter postrouting \
ether type ip \
flow table acct { rt ip nexthop timeout 600s counter }
# nft add rule inet filter postrouting \
ether type ip6 \
flow table acct { rt ip6 nexthop6 timeout 600s counter }Same as above, but via the inet family, where the ether type must be specified explicitly.
"rt classid" is also implemented identical to "meta rtclassid", since it is more logical to have this match in the routing expression going forward.
--- doc/nft.xml | 106 ++++++++++++++++++++++++++++ include/expression.h | 8 +++ include/linux/netfilter/nf_tables.h | 26 +++++++ include/rt.h | 35 ++++++++++ src/Makefile.am | 1 + src/evaluate.c | 27 ++++++++ src/netlink_delinearize.c | 18 +++++ src/netlink_linearize.c | 15 ++++ src/parser_bison.y | 28 ++++++++ src/rt.c | 135 ++++++++++++++++++++++++++++++++++++ src/scanner.l | 3 + tests/files/expr-rt | 21 ++++++ 12 files changed, 423 insertions(+)
doc/nft.xml +106 -0
@@ -1222,6 +1222,112 @@ filter output oif eth0 </example> </para> </refsect2> + + <refsect2> + <title>Routing expressions</title> + <para> + <cmdsynopsis> + <command>rt</command> + <group choice="req"> + <arg>classid</arg> + </group> + </cmdsynopsis> + <cmdsynopsis> + <command>rt</command> + <group choice="req"> + <arg>ip</arg> + <arg>ip6</arg> + </group> + <group choice="req"> + <arg>nexthop</arg> + </group> + </cmdsynopsis> + </para> + <para> + A routing expression refers to routing data associated with a packet. + </para> + <para> + There are two types of routing expressions: unqualified and qualified routing + expressions. Qualified routing expressions require either the <command>ip</command> or the <command>ip6</command> + address family before the routing key, while unqualified routing expressions are + specified directly after the <command>rt</command> key. + </para> + <para> + <table frame="all"> + <title>Routing expression types</title> + <tgroup cols='4' align='left' colsep='1' rowsep='1'> + <colspec colname='c1'/> + <colspec colname='c2'/> + <colspec colname='c3'/> + <colspec colname='c4'/> + <thead> + <row> + <entry>Address Family</entry> + <entry>Keyword</entry> + <entry>Description</entry> + <entry>Type</entry> + </row> + </thead> + <tbody> + <row> + <entry></entry> + <entry>classid</entry> + <entry>Routing realm</entry> + <entry>realm</entry> + </row> + <row> + <entry>ip</entry> + <entry>nexthop</entry> + <entry>Routing nexthop</entry> + <entry>ipv4_addr</entry> + </row> + <row> + <entry>ip6</entry> + <entry>nexthop</entry> + <entry>Routing nexthop</entry> + <entry>ipv6_addr</entry> + </row> + </tbody> + </tgroup> + </table> + </para> + <para> + <table frame="all"> + <title>Routing expression specific types</title> + <tgroup cols='2' align='left' colsep='1' rowsep='1'> + <colspec colname='c1'/> + <colspec colname='c2'/> + <thead> + <row> + <entry>Type</entry> + <entry>Description</entry> + </row> + </thead> + <tbody> + <row> + <entry>realm</entry> + <entry> + Routing Realm (32 bit number). Can be specified numerically + or as symbolic name defined in /etc/iproute2/rt_realms. + </entry> + </row> + </tbody> + </tgroup> + </table> + </para> + <para> + <example> + <title>Using routing expressions</title> + <programlisting> +# qualified meta expressions +filter output rt ip nexthop 192.168.0.1 + +# unqualified meta expression +filter output rt classid 10 + </programlisting> + </example> + </para> + </refsect2> </refsect1> <refsect1>
include/expression.h +8 -0
@@ -35,6 +35,7 @@* @EXPR_RELATIONAL: equality and relational expressions * @EXPR_NUMGEN: number generation expression * @EXPR_HASH: hash expression +* @EXPR_RT: routing expression */ enum expr_types {EXPR_INVALID ,
@@ -59,6 +60,7 @@ enum expr_types {EXPR_RELATIONAL ,EXPR_NUMGEN ,EXPR_HASH , + EXPR_RT, }; enum ops {
@@ -180,6 +182,7 @@ enum expr_flags {#include <exthdr.h> #include <numgen.h> #include <meta.h> +#include <rt.h> #include <hash.h> #include <ct.h>
@@ -283,6 +286,11 @@ struct expr {enum proto_bases base; } meta;struct { +/* EXPR_RT */ +enum nft_rt_keys key; +uint32_t family; + } rt; +struct {/* EXPR_CT */ enum nft_ct_keys key;int8_t direction;
include/linux/netfilter/nf_tables.h +26 -0
@@ -724,6 +724,16 @@ enum nft_meta_keys { };/** +* enum nft_rt_keys - nf_tables routing expression keys +* +* @NFT_META_NEXTHOP: routing nexthop +*/ +enum nft_rt_keys { +NFT_RT_CLASSID , +NFT_RT_NEXTHOP , +}; + +/** * enum nft_hash_attributes - nf_tables hash expression netlink attributes * * @NFTA_HASH_SREG: source register (NLA_U32)
@@ -760,6 +770,22 @@ enum nft_meta_attributes {#define NFTA_META_MAX (__NFTA_META_MAX - 1)/** +* enum nft_rt_attributes - nf_tables routing expression netlink attributes +* +* @NFTA_RT_DREG: destination register (NLA_U32) +* @NFTA_RT_KEY: meta data item to load (NLA_U32: nft_rt_keys) +* @NFTA_RT_FAMILY: Address family (NLA_U32) +*/ +enum nft_rt_attributes { +NFTA_RT_UNSPEC , +NFTA_RT_DREG , +NFTA_RT_KEY , +NFTA_RT_FAMILY , + __NFTA_RT_MAX +}; +#define NFTA_RT_MAX (__NFTA_RT_MAX - 1) + +/** * enum nft_ct_keys - nf_tables ct expression keys * * @NFT_CT_STATE: conntrack state (bitmask of enum ip_conntrack_info)
include/rt.h new file +35 -0
@@ -0,0 +1,35 @@ +#ifndef NFTABLES_RT_H +#define NFTABLES_RT_H + +/** +* struct rt_template - template for routing expressions +* +* @token: parser token for the expression +* @dtype_ip: data type of the ip expression +* @dtype_ip6: data type of the ip6 expression +* @len_ip: length of the ip expression +* @len_ip6: length of the ip6 expression +* @byteorder: byteorder +*/ +struct rt_template { +const char *token; +const struct datatype *dtype_ip, *dtype_ip6; +unsigned int len_ip, len_ip6; +enum byteorder byteorder; +}; + +#define RT_TEMPLATE (__token, __dtype_ip, __dtype_ip6, \ + __len_ip, __len_ip6, __byteorder) { \ + .token = (__token), \ + .dtype_ip = (__dtype_ip), \ + .dtype_ip6 = (__dtype_ip6), \ + .len_ip = (__len_ip), \ + .len_ip6 = (__len_ip6), \ + .byteorder = (__byteorder), \ +} + +extern struct expr *rt_expr_alloc (const struct location *loc, +enum nft_rt_keys key, +int family); + +#endif /* NFTABLES_RT_H */
src/Makefile.am +1 -0
@@ -38,6 +38,7 @@ nft_SOURCES = main.c \ exthdr.c \ hash.c \ meta.c \ + rt.c \ numgen.c \ ct.c \ netlink.c \
src/evaluate.c +27 -0
@@ -612,6 +612,31 @@ static int expr_evaluate_payload(struct eval_ctx *ctx, struct expr **exprp) }/* +* RT expression: validate protocol dependencies. +*/ +static int expr_evaluate_rt (struct eval_ctx *ctx,struct expr **expr) +{ +const struct proto_desc *base; +struct expr *rt = *expr; + + base = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR ].desc; + +if ((rt->rt.family ==NFPROTO_IPV4 && base == &proto_ip) || + (rt->rt.family ==NFPROTO_IPV6 && base == &proto_ip6) || + rt->rt.family ==NFPROTO_UNSPEC ) +return expr_evaluate_primary (ctx, expr); + +if (base) +return expr_error (ctx->msgs, rt, +"routing family %s cannot be used with %s" , +family2str (rt->rt.family), base->name); + +return expr_error (ctx->msgs, rt, +"ether type must be specified " +"before routing family" ); +} + +/* * CT expression: update the protocol dependant types bases on the protocol * context. */
@@ -1609,6 +1634,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)return expr_evaluate_primary (ctx, expr);case EXPR_PAYLOAD :return expr_evaluate_payload (ctx, expr); +case EXPR_RT : +return expr_evaluate_rt (ctx, expr);case EXPR_CT :return expr_evaluate_ct (ctx, expr);case EXPR_PREFIX :
src/netlink_delinearize.c +18 -0
@@ -545,6 +545,22 @@ static void netlink_parse_meta(struct netlink_parse_ctx *ctx,netlink_parse_meta_stmt (ctx, loc, nle); } +static void netlink_parse_rt (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle) +{ +enum nft_registers dreg; +uint32_t key, fam; +struct expr *expr; + + key =nftnl_expr_get_u32 (nle,NFTNL_EXPR_RT_KEY ); + fam =nftnl_expr_get_u32 (nle,NFTNL_EXPR_RT_FAMILY ); + expr =rt_expr_alloc (loc, key, fam); + + dreg =netlink_parse_register (nle,NFTNL_EXPR_RT_DREG ); +netlink_set_register (ctx, dreg, expr); +} +static void netlink_parse_numgen (struct netlink_parse_ctx *ctx,const struct location *loc,const struct nftnl_expr *nle)
@@ -1045,6 +1061,7 @@ static const struct { { .name = "payload", .parse = netlink_parse_payload }, { .name = "exthdr", .parse = netlink_parse_exthdr }, { .name = "meta", .parse = netlink_parse_meta }, + { .name = "rt", .parse = netlink_parse_rt }, { .name = "ct", .parse = netlink_parse_ct }, { .name = "counter", .parse = netlink_parse_counter }, { .name = "log", .parse = netlink_parse_log },
@@ -1678,6 +1695,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)break ;case EXPR_SET_REF :case EXPR_META : +case EXPR_RT :case EXPR_VERDICT :case EXPR_NUMGEN :break ;
src/netlink_linearize.c +15 -0
@@ -172,6 +172,19 @@ static void netlink_gen_meta(struct netlink_linearize_ctx *ctx,nftnl_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_rt (struct netlink_linearize_ctx *ctx, +const struct expr *expr, +enum nft_registers dreg) +{ +struct nftnl_expr *nle; + + nle =alloc_nft_expr ("rt" ); +netlink_put_register (nle,NFTNL_EXPR_RT_DREG , dreg); +nftnl_expr_set_u32 (nle,NFTNL_EXPR_RT_KEY , expr->rt.key); +nftnl_expr_set_u32 (nle,NFTNL_EXPR_RT_FAMILY , expr->rt.family); +nftnl_rule_add_expr (ctx->nlr, nle); +} +static void netlink_gen_numgen (struct netlink_linearize_ctx *ctx,const struct expr *expr,enum nft_registers dreg)
@@ -644,6 +657,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,return netlink_gen_exthdr (ctx, expr, dreg);case EXPR_META :return netlink_gen_meta (ctx, expr, dreg); +case EXPR_RT : +return netlink_gen_rt (ctx, expr, dreg);case EXPR_CT :return netlink_gen_ct (ctx, expr, dreg);case EXPR_SET_ELEM :
src/parser_bison.y +28 -0
@@ -340,6 +340,9 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token OIFGROUP "oifgroup" %token CGROUP "cgroup" +%token CLASSID "classid" +%token NEXTHOP "nexthop" + %token CT "ct" %token DIRECTION "direction" %token STATE "state"
@@ -591,6 +594,10 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } meta_expr %type <val> meta_key meta_key_qualified meta_key_unqualified numgen_type +%type <expr> rt_expr +%destructor { expr_free($$); } rt_expr +%type <val> rt_family rt_key_qualified rt_key_unqualified + %type <expr> ct_expr %destructor { expr_free($$); } ct_expr %type <val> ct_key ct_key_dir ct_key_counters
@@ -1980,6 +1987,7 @@ primary_expr : symbol_expr { $$ = $1; } | payload_expr { $$ = $1; } | exthdr_expr { $$ = $1; } | meta_expr { $$ = $1; } + | rt_expr { $$ = $1; } | ct_expr { $$ = $1; } | numgen_expr { $$ = $1; } | hash_expr { $$ = $1; }
@@ -2470,6 +2478,26 @@ meta_stmt : META meta_key SET expr } ; +rt_expr : RT rt_family rt_key_qualified + { + $$ = rt_expr_alloc(&@$, $3, $2); + } + | RT rt_key_unqualified + { + $$ = rt_expr_alloc(&@$, $2, NFPROTO_UNSPEC); + } + ; + +rt_family : IP { $$ = NFPROTO_IPV4; } + | IP6 { $$ = NFPROTO_IPV6; } + ; + +rt_key_qualified : NEXTHOP { $$ = NFT_RT_NEXTHOP; } + ; + +rt_key_unqualified : CLASSID { $$ = NFT_RT_CLASSID; } + ; + numgen_type : INC { $$ = NFT_NG_INCREMENTAL; } | RANDOM { $$ = NFT_NG_RANDOM; } ;
src/rt.c new file +135 -0
135 lines not on this page · raw message
src/scanner.l +3 -0
@@ -456,6 +456,9 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "oifgroup" { return OIFGROUP; } "cgroup" { return CGROUP; } +"classid" { return CLASSID; } +"nexthop" { return NEXTHOP; } + "ct" { return CT; } "direction" { return DIRECTION; } "state" { return STATE; }
tests/files/expr-rt new file +21 -0
@@ -0,0 +1,21 @@ +#! nft -f + +add table ip filter +add chain ip filter output { type filter hook output priority 0 ; } + +add table ip6 filter +add chain ip6 filter output { type filter hook output priority 0 ; } + +add table inet filter +add chain inet filter output { type filter hook output priority 0 ; } + +# rt: classid (see /etc/iproute2/rt_realms) +add rule ip filter output rt classid cosmos counter +add rule ip6 filter output rt classid cosmos counter +add rule inet filter output rt classid cosmos counter + +# rt: nexthop +add rule ip filter output rt ip nexthop 127.0.0.1 counter +add rule ip6 filter output rt ip6 nexthop ::1 counter +add rule inet filter output ether type ip rt ip nexthop 127.0.0.1 counter +add rule inet filter output ether type ip6 rt ip6 nexthop ::1 counter