* [PATCH nf-next] netfilter: nf_tables: add vlan support
@ 2019-10-18 10:02 wenxu
2019-10-18 10:24 ` Florian Westphal
0 siblings, 1 reply; 4+ messages in thread
From: wenxu @ 2019-10-18 10:02 UTC (permalink / raw)
To: pablo; +Cc: netfilter-devel
From: wenxu <wenxu@ucloud.cn>
This patch implements the vlan expr type that can be used to
configure vlan tci and vlan proto
Signed-off-by: wenxu <wenxu@ucloud.cn>
---
include/uapi/linux/netfilter/nf_tables.h | 24 +++++
net/netfilter/Kconfig | 6 ++
net/netfilter/Makefile | 1 +
net/netfilter/nft_vlan.c | 152 +++++++++++++++++++++++++++++++
4 files changed, 183 insertions(+)
create mode 100644 net/netfilter/nft_vlan.c
diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
index 81fed16..eb2e635 100644
--- a/include/uapi/linux/netfilter/nf_tables.h
+++ b/include/uapi/linux/netfilter/nf_tables.h
@@ -1796,4 +1796,28 @@ enum nft_tunnel_attributes {
};
#define NFTA_TUNNEL_MAX (__NFTA_TUNNEL_MAX - 1)
+enum nft_vlan_action {
+ NFT_VLAN_ACTION_PUSH,
+ NFT_VLAN_ACTION_POP,
+ NFT_VLAN_ACTION_MODIFY,
+ __NFT_VLAN_ACTION_MAX
+};
+#define NFT_VLAN_ACTION_MAX (__NFT_VLAN_ACTION_MAX - 1)
+
+/**
+ * enum nft_vlan_attributes - nf_tables vlan expression netlink attributes
+ *
+ * @NFTA_VLAN_ACTION: vlan data item to load (NLA_U32: nft_vlan_action)
+ * @NFTA_VLAN_SREG: source register (NLA_U16)
+ * @NFTA_VLAN_SREG2: source register (NLA_U16)
+ */
+enum nft_vlan_attributes {
+ NFTA_VLAN_UNSPEC,
+ NFTA_VLAN_ACTION,
+ NFTA_VLAN_SREG,
+ NFTA_VLAN_SREG2,
+ __NFTA_VLAN_MAX
+};
+#define NFTA_VLAN_MAX (__NFTA_VLAN_MAX - 1)
+
#endif /* _LINUX_NF_TABLES_H */
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index 91efae8..6583f30 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -556,6 +556,12 @@ config NFT_TUNNEL
This option adds the "tunnel" expression that you can use to set
tunneling policies.
+config NFT_VLAN
+ tristate "Netfilter nf_tables vlan module"
+ help
+ This option adds the "vlan" expression that you can use to set
+ vlan policies.
+
config NFT_OBJREF
tristate "Netfilter nf_tables stateful object reference module"
help
diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
index 4fc075b..e91f93e 100644
--- a/net/netfilter/Makefile
+++ b/net/netfilter/Makefile
@@ -111,6 +111,7 @@ obj-$(CONFIG_NFT_OSF) += nft_osf.o
obj-$(CONFIG_NFT_TPROXY) += nft_tproxy.o
obj-$(CONFIG_NFT_XFRM) += nft_xfrm.o
obj-$(CONFIG_NFT_SYNPROXY) += nft_synproxy.o
+obj-$(CONFIG_NFT_VLAN) += nft_vlan.o
obj-$(CONFIG_NFT_NAT) += nft_chain_nat.o
diff --git a/net/netfilter/nft_vlan.c b/net/netfilter/nft_vlan.c
new file mode 100644
index 0000000..1521b97
--- /dev/null
+++ b/net/netfilter/nft_vlan.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/kernel.h>
+#include <linux/netlink.h>
+#include <linux/module.h>
+#include <linux/if_vlan.h>
+#include <linux/netfilter.h>
+#include <linux/netfilter/nf_tables.h>
+#include <net/netfilter/nf_tables.h>
+#include <net/netfilter/nf_tables_core.h>
+
+struct nft_vlan {
+ enum nft_vlan_action action;
+ enum nft_registers sreg:8;
+ enum nft_registers sreg2:8;
+};
+
+static void nft_vlan_set_eval(const struct nft_expr *expr,
+ struct nft_regs *regs,
+ const struct nft_pktinfo *pkt)
+{
+ const struct nft_vlan *p_vlan = nft_expr_priv(expr);
+ u32 *sreg2 = ®s->data[p_vlan->sreg2];
+ u16 vlan_proto = nft_reg_load16(sreg2);
+ u32 *sreg = ®s->data[p_vlan->sreg];
+ u16 vlan_tci = nft_reg_load16(sreg);
+ struct sk_buff *skb = pkt->skb;
+
+ switch (p_vlan->action) {
+ case NFT_VLAN_ACTION_POP:
+ skb_vlan_pop(skb);
+ break;
+ case NFT_VLAN_ACTION_PUSH: {
+ if (vlan_proto != ETH_P_8021Q && vlan_proto != ETH_P_8021AD)
+ return;
+
+ skb_push_rcsum(skb, skb->mac_len);
+ skb_vlan_push(skb, htons(vlan_proto), vlan_tci & VLAN_VID_MASK);
+ skb_pull_rcsum(skb, skb->mac_len);
+ break;
+ }
+ case NFT_VLAN_ACTION_MODIFY: {
+ u16 tci;
+
+ /* No-op if no vlan tag (either hw-accel or in-payload) */
+ if (!skb_vlan_tagged(skb))
+ return;
+
+ /* extract existing tag (and guarantee no hw-accel tag) */
+ if (skb_vlan_tag_present(skb)) {
+ tci = skb_vlan_tag_get(skb);
+ __vlan_hwaccel_clear_tag(skb);
+ } else {
+ /* in-payload vlan tag, pop it */
+ if (__skb_vlan_pop(skb, &tci))
+ return;
+ }
+
+ /* replace the vid */
+ vlan_tci = vlan_tci | VLAN_VID_MASK;
+ vlan_tci = (tci & ~VLAN_VID_MASK) | vlan_tci;
+
+ /* put updated tci as hwaccel tag */
+ __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
+ break;
+ }
+ default:
+ WARN_ON(1);
+ }
+}
+
+static const struct nla_policy nft_vlan_policy[NFTA_VLAN_MAX + 1] = {
+ [NFTA_VLAN_ACTION] = { .type = NLA_U32 },
+ [NFTA_VLAN_SREG] = { .type = NLA_U32 },
+ [NFTA_VLAN_SREG2] = { .type = NLA_U32 },
+};
+
+static int nft_vlan_set_init(const struct nft_ctx *ctx,
+ const struct nft_expr *expr,
+ const struct nlattr * const tb[])
+{
+ struct nft_vlan *priv = nft_expr_priv(expr);
+ int err;
+
+ priv->action = ntohl(nla_get_be32(tb[NFTA_VLAN_ACTION]));
+ switch (priv->action) {
+ case NFT_VLAN_ACTION_PUSH:
+ case NFT_VLAN_ACTION_POP:
+ case NFT_VLAN_ACTION_MODIFY:
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ priv->sreg = nft_parse_register(tb[NFTA_VLAN_SREG]);
+ err = nft_validate_register_load(priv->sreg, sizeof(u16));
+ if (err < 0)
+ return err;
+
+ priv->sreg2 = nft_parse_register(tb[NFTA_VLAN_SREG2]);
+ err = nft_validate_register_load(priv->sreg2, sizeof(u16));
+ if (err < 0)
+ return err;
+
+ return 0;
+}
+
+static int nft_vlan_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
+{
+ const struct nft_vlan *priv = nft_expr_priv(expr);
+
+ if (nla_put_be32(skb, NFTA_VLAN_ACTION, htonl(priv->action)))
+ goto nla_put_failure;
+ if (nft_dump_register(skb, NFTA_VLAN_SREG, priv->sreg))
+ goto nla_put_failure;
+ if (nft_dump_register(skb, NFTA_VLAN_SREG2, priv->sreg2))
+ goto nla_put_failure;
+
+ return 0;
+
+nla_put_failure:
+ return -1;
+}
+
+static struct nft_expr_type nft_vlan_type;
+static const struct nft_expr_ops nft_vlan_set_ops = {
+ .type = &nft_vlan_type,
+ .size = NFT_EXPR_SIZE(sizeof(struct nft_vlan)),
+ .eval = nft_vlan_set_eval,
+ .init = nft_vlan_set_init,
+ .dump = nft_vlan_set_dump,
+};
+
+static struct nft_expr_type nft_vlan_type __read_mostly = {
+ .name = "vlan",
+ .ops = &nft_vlan_set_ops,
+ .policy = nft_vlan_policy,
+ .maxattr = NFTA_VLAN_MAX,
+ .owner = THIS_MODULE,
+};
+
+static int __init nft_vlan_module_init(void)
+{
+ return nft_register_expr(&nft_vlan_type);
+}
+
+static void __exit nft_vlan_module_exit(void)
+{
+ nft_unregister_expr(&nft_vlan_type);
+}
+
+module_init(nft_vlan_module_init);
+module_exit(nft_vlan_module_exit);
--
1.8.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH nf-next] netfilter: nf_tables: add vlan support
2019-10-18 10:02 [PATCH nf-next] netfilter: nf_tables: add vlan support wenxu
@ 2019-10-18 10:24 ` Florian Westphal
2019-10-18 10:40 ` wenxu
0 siblings, 1 reply; 4+ messages in thread
From: Florian Westphal @ 2019-10-18 10:24 UTC (permalink / raw)
To: wenxu; +Cc: pablo, netfilter-devel
wenxu@ucloud.cn <wenxu@ucloud.cn> wrote:
> From: wenxu <wenxu@ucloud.cn>
>
> This patch implements the vlan expr type that can be used to
> configure vlan tci and vlan proto
Looks like a very small module with no external dependencies,
I think you could make this a nft-builtin feature and just add
nft_vlan.o to 'nf_tables-objs' in net/netfilter/Makefile, similar to
nft_rt.
What do you think?
If you plan to extend this in the future then I'm fine with keeping it
as a module.
> +static int nft_vlan_set_init(const struct nft_ctx *ctx,
> + const struct nft_expr *expr,
> + const struct nlattr * const tb[])
> +{
> + struct nft_vlan *priv = nft_expr_priv(expr);
> + int err;
I think you need to add
if (!tb[NFTA_VLAN_ACTION] ||
!tb[NFTA_VLAN_SREG] ||
!tb[NFTA_VLAN_SREG2])
return -EINVAL;
Other than that this looks good to me.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nf-next] netfilter: nf_tables: add vlan support
2019-10-18 10:24 ` Florian Westphal
@ 2019-10-18 10:40 ` wenxu
2019-10-18 10:51 ` Florian Westphal
0 siblings, 1 reply; 4+ messages in thread
From: wenxu @ 2019-10-18 10:40 UTC (permalink / raw)
To: Florian Westphal; +Cc: pablo, netfilter-devel
On 10/18/2019 6:24 PM, Florian Westphal wrote:
> wenxu@ucloud.cn <wenxu@ucloud.cn> wrote:
>> From: wenxu <wenxu@ucloud.cn>
>>
>> This patch implements the vlan expr type that can be used to
>> configure vlan tci and vlan proto
> Looks like a very small module with no external dependencies,
> I think you could make this a nft-builtin feature and just add
> nft_vlan.o to 'nf_tables-objs' in net/netfilter/Makefile, similar to
> nft_rt.
>
> What do you think?
>
> If you plan to extend this in the future then I'm fine with keeping it
> as a module.
It can add vlan tci / proto "get" expr and It also can support offload things in the future.
>> +static int nft_vlan_set_init(const struct nft_ctx *ctx,
>> + const struct nft_expr *expr,
>> + const struct nlattr * const tb[])
>> +{
>> + struct nft_vlan *priv = nft_expr_priv(expr);
>> + int err;
> I think you need to add
>
> if (!tb[NFTA_VLAN_ACTION] ||
> !tb[NFTA_VLAN_SREG] ||
> !tb[NFTA_VLAN_SREG2])
> return -EINVAL;
Will do.
>
> Other than that this looks good to me.
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH nf-next] netfilter: nf_tables: add vlan support
2019-10-18 10:40 ` wenxu
@ 2019-10-18 10:51 ` Florian Westphal
0 siblings, 0 replies; 4+ messages in thread
From: Florian Westphal @ 2019-10-18 10:51 UTC (permalink / raw)
To: wenxu; +Cc: Florian Westphal, pablo, netfilter-devel
wenxu <wenxu@ucloud.cn> wrote:
> > If you plan to extend this in the future then I'm fine with keeping it
> > as a module.
> It can add vlan tci / proto "get" expr and It also can support offload things in the future.
Ok, lets keep it as a module then.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2019-10-18 10:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-18 10:02 [PATCH nf-next] netfilter: nf_tables: add vlan support wenxu
2019-10-18 10:24 ` Florian Westphal
2019-10-18 10:40 ` wenxu
2019-10-18 10:51 ` Florian Westphal
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.