Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Intel Corporation |
| 3 | * |
| 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. |
| 7 | * |
| 8 | */ |
| 9 | |
| 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> |
| 18 | |
| 19 | #include "../br_private.h" |
| 20 | |
| 21 | static void nft_meta_bridge_get_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 22 | struct nft_regs *regs, |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 23 | const struct nft_pktinfo *pkt) |
| 24 | { |
| 25 | const struct nft_meta *priv = nft_expr_priv(expr); |
| 26 | const struct net_device *in = pkt->in, *out = pkt->out; |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 27 | u32 *dest = ®s->data[priv->dreg]; |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 28 | const struct net_bridge_port *p; |
| 29 | |
| 30 | switch (priv->key) { |
| 31 | case NFT_META_BRI_IIFNAME: |
| 32 | if (in == NULL || (p = br_port_get_rcu(in)) == NULL) |
| 33 | goto err; |
| 34 | break; |
| 35 | case NFT_META_BRI_OIFNAME: |
| 36 | if (out == NULL || (p = br_port_get_rcu(out)) == NULL) |
| 37 | goto err; |
| 38 | break; |
| 39 | default: |
| 40 | goto out; |
| 41 | } |
| 42 | |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 43 | strncpy((char *)dest, p->br->dev->name, IFNAMSIZ); |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 44 | return; |
| 45 | out: |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 46 | return nft_meta_get_eval(expr, regs, pkt); |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 47 | err: |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 48 | regs->verdict.code = NFT_BREAK; |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 49 | } |
| 50 | |
| 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[]) |
| 54 | { |
| 55 | struct nft_meta *priv = nft_expr_priv(expr); |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 56 | unsigned int len; |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 57 | |
| 58 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); |
| 59 | switch (priv->key) { |
| 60 | case NFT_META_BRI_IIFNAME: |
| 61 | case NFT_META_BRI_OIFNAME: |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 62 | len = IFNAMSIZ; |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 63 | break; |
| 64 | default: |
| 65 | return nft_meta_get_init(ctx, expr, tb); |
| 66 | } |
| 67 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 68 | priv->dreg = nft_parse_register(tb[NFTA_META_DREG]); |
Patrick McHardy | 27e6d20 | 2015-04-11 02:27:29 +0100 | [diff] [blame] | 69 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
| 70 | NFT_DATA_VALUE, len); |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static struct nft_expr_type nft_meta_bridge_type; |
| 74 | static const struct nft_expr_ops nft_meta_bridge_get_ops = { |
| 75 | .type = &nft_meta_bridge_type, |
| 76 | .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)), |
| 77 | .eval = nft_meta_bridge_get_eval, |
| 78 | .init = nft_meta_bridge_get_init, |
| 79 | .dump = nft_meta_get_dump, |
| 80 | }; |
| 81 | |
| 82 | static const struct nft_expr_ops nft_meta_bridge_set_ops = { |
| 83 | .type = &nft_meta_bridge_type, |
| 84 | .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)), |
| 85 | .eval = nft_meta_set_eval, |
| 86 | .init = nft_meta_set_init, |
Florian Westphal | e639f7a | 2015-11-28 21:53:05 +0100 | [diff] [blame] | 87 | .destroy = nft_meta_set_destroy, |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 88 | .dump = nft_meta_set_dump, |
Liping Zhang | 960fa72 | 2016-08-22 22:57:56 +0800 | [diff] [blame] | 89 | .validate = nft_meta_set_validate, |
Tomasz Bursztyka | f5efc69 | 2014-04-14 15:41:28 +0300 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | static const struct nft_expr_ops * |
| 93 | nft_meta_bridge_select_ops(const struct nft_ctx *ctx, |
| 94 | const struct nlattr * const tb[]) |
| 95 | { |
| 96 | if (tb[NFTA_META_KEY] == NULL) |
| 97 | return ERR_PTR(-EINVAL); |
| 98 | |
| 99 | if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG]) |
| 100 | return ERR_PTR(-EINVAL); |
| 101 | |
| 102 | if (tb[NFTA_META_DREG]) |
| 103 | return &nft_meta_bridge_get_ops; |
| 104 | |
| 105 | if (tb[NFTA_META_SREG]) |
| 106 | return &nft_meta_bridge_set_ops; |
| 107 | |
| 108 | return ERR_PTR(-EINVAL); |
| 109 | } |
| 110 | |
| 111 | static struct nft_expr_type nft_meta_bridge_type __read_mostly = { |
| 112 | .family = NFPROTO_BRIDGE, |
| 113 | .name = "meta", |
| 114 | .select_ops = &nft_meta_bridge_select_ops, |
| 115 | .policy = nft_meta_policy, |
| 116 | .maxattr = NFTA_META_MAX, |
| 117 | .owner = THIS_MODULE, |
| 118 | }; |
| 119 | |
| 120 | static int __init nft_meta_bridge_module_init(void) |
| 121 | { |
| 122 | return nft_register_expr(&nft_meta_bridge_type); |
| 123 | } |
| 124 | |
| 125 | static void __exit nft_meta_bridge_module_exit(void) |
| 126 | { |
| 127 | nft_unregister_expr(&nft_meta_bridge_type); |
| 128 | } |
| 129 | |
| 130 | module_init(nft_meta_bridge_module_init); |
| 131 | module_exit(nft_meta_bridge_module_exit); |
| 132 | |
| 133 | MODULE_LICENSE("GPL"); |
| 134 | MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>"); |
| 135 | MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "meta"); |