netfilter: nft: introduce routing expression
Subsystems (2)
v3 cover netfilter: nft: introduce routing expression
Version 3 changes:
- nft_rt_inet: Use init and eval functions from nft_rt_ipv4 and nft_rt_ipv6
Version 2 changes:
- Fix comments for enum nft_rt_keys
- Remove nft_rt*_select_ops
This patch series introduces an nftables 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 dropThis 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.
Regards,
Anders K. Pedersen
--- include/net/netfilter/ipv4/nft_rt.h | 12 +++ include/net/netfilter/ipv6/nft_rt.h | 12 +++ include/net/netfilter/nft_rt.h | 23 ++++++ include/uapi/linux/netfilter/nf_tables.h | 27 ++++++ net/ipv4/netfilter/Kconfig | 4 + net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nft_rt_ipv4.c | 112 +++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 4 + net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nft_rt_ipv6.c | 113 +++++++++++++++++++++++++ net/netfilter/Kconfig | 13 +++ net/netfilter/Makefile | 2 + net/netfilter/nft_rt.c | 137 +++++++++++++++++++++++++++++++ net/netfilter/nft_rt_inet.c | 99 ++++++++++++++++++++++ 14 files changed, 560 insertions(+)
PATCH v3 1/5 netfilter: nft: UAPI headers for routing expression
From: Anders K. Pedersen <[email protected]>
Add new UAPI header definitions for nftables "rt" expression, which will enable usage of routing related data.
---
v3
- no changes
v2
- fix comments for enum nft_rt_keys
include/uapi/linux/netfilter/nf_tables.h | 27 ++++++ 1 files changed, 27 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +27 -0
@@ -759,6 +759,17 @@ enum nft_meta_keys { };/** +* enum nft_rt_keys - nf_tables routing expression keys +* +* @NFT_RT_CLASSID: realm value of packet's route (skb->dst->tclassid) +* @NFT_RT_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)
@@ -797,6 +808,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)
PATCH v3 2/5 netfilter: nft: basic routing expression
In reply to Anders K. Pedersen | CohaesioFrom: Anders K. Pedersen <[email protected]>
Introduce basic infrastructure for nftables rt expression for routing related data. Initially "rt classid" is implemented identical to "meta rtclassid", since it is more logical to have this match in the routing expression going forward.
---
v3
- no changes
v2
- remove nft_rt_select_ops
include/net/netfilter/nft_rt.h | 23 ++++++ net/netfilter/Kconfig | 6 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_rt.c | 137 +++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+)
include/net/netfilter/nft_rt.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef _NFT_RT_H_ +#define _NFT_RT_H_ + +struct nft_rt { +enum nft_rt_keys key:8 ; +enum nft_registers dreg:8 ; +u8 family; +}; + +extern const struct nla_policy nft_rt_policy[]; + +int nft_rt_get_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +int nft_rt_get_dump (struct sk_buff *skb, +const struct nft_expr *expr); + +void nft_rt_get_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt); + +#endif
net/netfilter/Kconfig +6 -0
@@ -474,6 +474,12 @@ config NFT_META This option adds the "meta" expression that you can use to match and to set packet metainformation such as the packet mark. +config NFT_RT + tristate "Netfilter nf_tables routing module" + help + This option adds the "rt" expression that you can use to match + packet routing information such as the packet nexthop. + config NFT_NUMGEN tristate "Netfilter nf_tables number generator module" help
net/netfilter/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_NF_TABLES_NETDEV) += nf_tables_netdev.o obj-$(CONFIG_NFT_COMPAT) += nft_compat.o obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o obj-$(CONFIG_NFT_META) += nft_meta.o +obj-$(CONFIG_NFT_RT) += nft_rt.o obj-$(CONFIG_NFT_NUMGEN) += nft_numgen.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o
net/netfilter/nft_rt.c new file +137 -0
137 lines not on this page · raw message
Re: [PATCH v3 nf-next 2/5] netfilter: nft: basic routing expression
Hi Anders,
2016-10-21 3:09 GMT+08:00 Anders K. Pedersen | Cohaesio <[email protected]>:
net/netfilter/nft_rt.c line 66
Quoted 10 lines
net/netfilter/nft_rt.c new file +10
-0+int nft_rt_get_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_rt *priv =nft_expr_priv (expr); +unsigned int len; + +if (tb[NFTA_RT_KEY ] ==NULL || + tb[NFTA_RT_DREG ] ==NULL || + tb[NFTA_RT_FAMILY ] ==NULL )
The judgement here is also confusing to me, we do not use family in NFT_RT_CLASSID key, so why we force the user that they must specify the NFTA_RT_FAMILY attribute?
As I mentioned in another reply, I think the NFTA_RT_FAMILY attr is redundant.
Quoted 18 lines
net/netfilter/nft_rt.c new file +18
-0+return -EINVAL ; + + priv->key =ntohl (nla_get_be32 (tb[NFTA_RT_KEY ])); +switch (priv->key) { +#ifdef CONFIG_IP_ROUTE_CLASSID +case NFT_RT_CLASSID : + len =sizeof (u32); +break ; +#endif +default : +return -EOPNOTSUPP ; + } + + priv->family =ntohl (nla_get_be32 (tb[NFTA_RT_FAMILY ])); + priv->dreg =nft_parse_register (tb[NFTA_RT_DREG ]); +return nft_validate_register_store (ctx, priv->dreg,NULL , +NFT_DATA_VALUE , len); +}
Re: [PATCH v3 nf-next 2/5] netfilter: nft: basic routing expression
Hi Liping,
Quoted 16 lines · Liping Zhang
2016-10-21 3:09 GMT+08:00 Anders K. Pedersen | Cohaesio <akp@cohaesio .com>:
+int nft_rt_get_init(const structnft_ctx*ctx,Review
Quoted 9 lines
+ const struct
nft_expr*expr, + const struct nlattr * const tb[])(quoted diff) +7
-0+{ + struct nft_rt *priv = nft_expr_priv(expr); + unsigned int len; + + if (tb[NFTA_RT_KEY] == NULL || + tb[NFTA_RT_DREG] == NULL || + tb[NFTA_RT_FAMILY] == NULL)The judgement here is also confusing to me, we do not use family in
NFT_RT_CLASSIDkey, so why we force the user that they must specify theNFTA_RT_FAMILYattribute?
I will change this to
nft_rt.c 2016-10-21 06:22:27.752000000 +0200 renamed from nft_rt.c~ 2016-10-19 20:22:42.000000000 +0200 +6 -3
@@ -62,8 +62,7 @@ unsigned int len; if (tb[NFTA_RT_KEY] == NULL || - tb[NFTA_RT_DREG] == NULL || - tb[NFTA_RT_FAMILY] == NULL) + tb[NFTA_RT_DREG] == NULL) return -EINVAL; priv->key = ntohl(nla_get_be32(tb[NFTA_RT_KEY]));
@@ -77,7 +76,11 @@ return -EOPNOTSUPP; } - priv->family = ntohl(nla_get_be32(tb[NFTA_RT_FAMILY])); + if (tb[NFTA_RT_FAMILY] == NULL) + priv->family = NFPROTO_UNSPEC; + else + priv->family = ntohl(nla_get_be32(tb[NFTA_RT_FAMILY])); + priv->dreg = nft_parse_register(tb[NFTA_RT_DREG]); return nft_validate_register_store(ctx, priv->dreg, NULL, NFT_DATA_VALUE, len);
Quoted 2 lines
As I mentioned in another reply, I think the
NFTA_RT_FAMILYattr is redundant.
I answered that in the other thread.
Regards,
Anders
PATCH v3 3/5 netfilter: nft: rt nexthop for IPv4 family
In reply to Anders K. Pedersen | CohaesioFrom: Anders K. Pedersen <[email protected]>
Add nftables IPv4 family support for an "rt ip nexthop" expression allowing usage of the routing nexthop (i.e. the directly connected IP address that an outgoing packet is sent to) 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 }This rule counts outgoing traffic per nexthop. Note that the timeout releases an entry if no traffic is seen for this nexthop within 10 minutes.
---
v3
- export init and eval functions for use from nft_rt_inet v2
- remove nft_rt_ipv4_select_ops
include/net/netfilter/ipv4/nft_rt.h | 12 +++ net/ipv4/netfilter/Kconfig | 4 + net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nft_rt_ipv4.c | 112 +++++++++++++++++++++++++ 4 files changed, 129 insertions(+)
include/net/netfilter/ipv4/nft_rt.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef _NFT_RT_IPV4_H_ +#define _NFT_RT_IPV4_H_ + +int nft_rt_ipv4_get_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +void nft_rt_ipv4_get_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt); + +#endif
net/ipv4/netfilter/Kconfig +4 -0
@@ -42,6 +42,10 @@ config NFT_CHAIN_ROUTE_IPV4 fields such as the source, destination, type of service and the packet mark. +config NFT_RT_IPV4 + default NFT_RT + tristate + config NFT_REJECT_IPV4 select NF_REJECT_IPV4 default NFT_REJECT
net/ipv4/netfilter/Makefile +1 -0
@@ -33,6 +33,7 @@ obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o +obj-$(CONFIG_NFT_RT_IPV4) += nft_rt_ipv4.o obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o obj-$(CONFIG_NFT_MASQ_IPV4) += nft_masq_ipv4.o obj-$(CONFIG_NFT_REDIR_IPV4) += nft_redir_ipv4.o
net/ipv4/netfilter/nft_rt_ipv4.c new file +112 -0
112 lines not on this page · raw message
PATCH v3 4/5 netfilter: nft: rt nexthop for IPv6 family
In reply to Anders K. Pedersen | CohaesioFrom: Anders K. Pedersen <[email protected]>
Add nftables IPv6 family support for an "rt ip6 nexthop" expression allowing usage of the routing nexthop (i.e. the directly connected IP address that an outgoing packet is sent to) for matching or accounting, eg.
# nft add rule ip6 filter postrouting \ ip6 daddr fd01::/16 rt ip6 nexthop != fd00::1 drop
This will drop any traffic to fd01::/16 that is not routed via fd00::1.
# nft add rule ip6 filter postrouting \
flow table acct { rt ip6 nexthop timeout 600s counter }This rule counts outgoing traffic per nexthop. Note that the timeout releases an entry if no traffic is seen for this nexthop within 10 minutes.
---
v3
- export init and eval functions for use from nft_rt_inet v2
- remove nft_rt_ipv6_select_ops
include/net/netfilter/ipv6/nft_rt.h | 12 +++ net/ipv6/netfilter/Kconfig | 4 + net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nft_rt_ipv6.c | 113 +++++++++++++++++++++++++ 4 files changed, 130 insertions(+)
include/net/netfilter/ipv6/nft_rt.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef _NFT_RT_IPV6_H_ +#define _NFT_RT_IPV6_H_ + +int nft_rt_ipv6_get_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +void nft_rt_ipv6_get_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt); + +#endif
net/ipv6/netfilter/Kconfig +4 -0
@@ -42,6 +42,10 @@ config NFT_CHAIN_ROUTE_IPV6 fields such as the source, destination, flowlabel, hop-limit and the packet mark. +config NFT_RT_IPV6 + default NFT_RT + tristate + config NFT_REJECT_IPV6 select NF_REJECT_IPV6 default NFT_REJECT
net/ipv6/netfilter/Makefile +1 -0
@@ -36,6 +36,7 @@ obj-$(CONFIG_NF_DUP_IPV6) += nf_dup_ipv6.o obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV6) += nft_chain_nat_ipv6.o +obj-$(CONFIG_NFT_RT_IPV6) += nft_rt_ipv6.o obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o obj-$(CONFIG_NFT_MASQ_IPV6) += nft_masq_ipv6.o obj-$(CONFIG_NFT_REDIR_IPV6) += nft_redir_ipv6.o
net/ipv6/netfilter/nft_rt_ipv6.c new file +113 -0
113 lines not on this page · raw message
PATCH v3 5/5 netfilter: nft: rt nexthop for inet family
In reply to Anders K. Pedersen | CohaesioFrom: Anders K. Pedersen <[email protected]>
Add nftables inet family support for an rt nexthop expression allowing usage of the routing nexthop (i.e. the directly connected IP address that an outgoing packet is sent to) for matching or accounting, eg.
# nft add rule inet filter postrouting \ ether type ip6 \ ip6 daddr fd01::/16 rt ip6 nexthop != fd00::1 drop
This will drop any traffic to fd01::/16 that is not routed via fd00::1. Note that "ether type" must be specified explicitly, when rt nexthop is used from inet family tables.
# 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 }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.
---
v3
- use init and eval functions from nft_rt_ipv4 and nft_rt_ipv6 v2
- remove nft_rt_inet_select_ops
net/netfilter/Kconfig | 7 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_rt_inet.c | 99 ++++++++++++++++++++++ 3 files changed, 107 insertions(+)
net/netfilter/Kconfig +7 -0
@@ -480,6 +480,13 @@ config NFT_META This option adds the "rt" expression that you can use to match packet routing information such as the packet nexthop. +config NFT_RT_INET + depends on NF_TABLES_INET + default NFT_RT + select NFT_RT_IPV4 + select NFT_RT_IPV6 + tristate + config NFT_NUMGEN tristate "Netfilter nf_tables number generator module" help
net/netfilter/Makefile +1 -0
@@ -82,6 +82,7 @@ obj-$(CONFIG_NF_TABLES_NETDEV) += nf_tables_netdev.o obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_RT) += nft_rt.o +obj-$(CONFIG_NFT_RT_INET) += nft_rt_inet.o obj-$(CONFIG_NFT_NUMGEN) += nft_numgen.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o
net/netfilter/nft_rt_inet.c new file +99 -0
99 lines not on this page · raw message