2 * Copyright (c) 2014 Intel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nft_meta.h>
19 #include "../br_private.h"
21 static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
22 struct nft_data data[NFT_REG_MAX + 1],
23 const struct nft_pktinfo *pkt)
25 const struct nft_meta *priv = nft_expr_priv(expr);
26 const struct net_device *in = pkt->in, *out = pkt->out;
27 struct nft_data *dest = &data[priv->dreg];
28 const struct net_bridge_port *p;
31 case NFT_META_BRI_IIFNAME:
32 if (in == NULL || (p = br_port_get_rcu(in)) == NULL)
35 case NFT_META_BRI_OIFNAME:
36 if (out == NULL || (p = br_port_get_rcu(out)) == NULL)
43 strncpy((char *)dest->data, p->br->dev->name, sizeof(dest->data));
46 return nft_meta_get_eval(expr, data, pkt);
48 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
51 static int nft_meta_bridge_get_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
55 struct nft_meta *priv = nft_expr_priv(expr);
58 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
60 case NFT_META_BRI_IIFNAME:
61 case NFT_META_BRI_OIFNAME:
64 return nft_meta_get_init(ctx, expr, tb);
67 priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG]));
68 err = nft_validate_output_register(priv->dreg);
72 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
79 static struct nft_expr_type nft_meta_bridge_type;
80 static const struct nft_expr_ops nft_meta_bridge_get_ops = {
81 .type = &nft_meta_bridge_type,
82 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
83 .eval = nft_meta_bridge_get_eval,
84 .init = nft_meta_bridge_get_init,
85 .dump = nft_meta_get_dump,
88 static const struct nft_expr_ops nft_meta_bridge_set_ops = {
89 .type = &nft_meta_bridge_type,
90 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
91 .eval = nft_meta_set_eval,
92 .init = nft_meta_set_init,
93 .dump = nft_meta_set_dump,
96 static const struct nft_expr_ops *
97 nft_meta_bridge_select_ops(const struct nft_ctx *ctx,
98 const struct nlattr * const tb[])
100 if (tb[NFTA_META_KEY] == NULL)
101 return ERR_PTR(-EINVAL);
103 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
104 return ERR_PTR(-EINVAL);
106 if (tb[NFTA_META_DREG])
107 return &nft_meta_bridge_get_ops;
109 if (tb[NFTA_META_SREG])
110 return &nft_meta_bridge_set_ops;
112 return ERR_PTR(-EINVAL);
115 static struct nft_expr_type nft_meta_bridge_type __read_mostly = {
116 .family = NFPROTO_BRIDGE,
118 .select_ops = &nft_meta_bridge_select_ops,
119 .policy = nft_meta_policy,
120 .maxattr = NFTA_META_MAX,
121 .owner = THIS_MODULE,
124 static int __init nft_meta_bridge_module_init(void)
126 return nft_register_expr(&nft_meta_bridge_type);
129 static void __exit nft_meta_bridge_module_exit(void)
131 nft_unregister_expr(&nft_meta_bridge_type);
134 module_init(nft_meta_bridge_module_init);
135 module_exit(nft_meta_bridge_module_exit);
137 MODULE_LICENSE("GPL");
138 MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
139 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "meta");