nft_meta_bridge.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/netlink.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_tables.h>
  16. #include <net/netfilter/nft_meta.h>
  17. #include "../br_private.h"
  18. static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
  19. struct nft_regs *regs,
  20. const struct nft_pktinfo *pkt)
  21. {
  22. const struct nft_meta *priv = nft_expr_priv(expr);
  23. const struct net_device *in = pkt->in, *out = pkt->out;
  24. u32 *dest = &regs->data[priv->dreg];
  25. const struct net_bridge_port *p;
  26. switch (priv->key) {
  27. case NFT_META_BRI_IIFNAME:
  28. if (in == NULL || (p = br_port_get_rcu(in)) == NULL)
  29. goto err;
  30. break;
  31. case NFT_META_BRI_OIFNAME:
  32. if (out == NULL || (p = br_port_get_rcu(out)) == NULL)
  33. goto err;
  34. break;
  35. default:
  36. goto out;
  37. }
  38. strncpy((char *)dest, p->br->dev->name, IFNAMSIZ);
  39. return;
  40. out:
  41. return nft_meta_get_eval(expr, regs, pkt);
  42. err:
  43. regs->verdict.code = NFT_BREAK;
  44. }
  45. static int nft_meta_bridge_get_init(const struct nft_ctx *ctx,
  46. const struct nft_expr *expr,
  47. const struct nlattr * const tb[])
  48. {
  49. struct nft_meta *priv = nft_expr_priv(expr);
  50. unsigned int len;
  51. priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
  52. switch (priv->key) {
  53. case NFT_META_BRI_IIFNAME:
  54. case NFT_META_BRI_OIFNAME:
  55. len = IFNAMSIZ;
  56. break;
  57. default:
  58. return nft_meta_get_init(ctx, expr, tb);
  59. }
  60. priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
  61. return nft_validate_register_store(ctx, priv->dreg, NULL,
  62. NFT_DATA_VALUE, len);
  63. }
  64. static struct nft_expr_type nft_meta_bridge_type;
  65. static const struct nft_expr_ops nft_meta_bridge_get_ops = {
  66. .type = &nft_meta_bridge_type,
  67. .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
  68. .eval = nft_meta_bridge_get_eval,
  69. .init = nft_meta_bridge_get_init,
  70. .dump = nft_meta_get_dump,
  71. };
  72. static const struct nft_expr_ops nft_meta_bridge_set_ops = {
  73. .type = &nft_meta_bridge_type,
  74. .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
  75. .eval = nft_meta_set_eval,
  76. .init = nft_meta_set_init,
  77. .dump = nft_meta_set_dump,
  78. };
  79. static const struct nft_expr_ops *
  80. nft_meta_bridge_select_ops(const struct nft_ctx *ctx,
  81. const struct nlattr * const tb[])
  82. {
  83. if (tb[NFTA_META_KEY] == NULL)
  84. return ERR_PTR(-EINVAL);
  85. if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
  86. return ERR_PTR(-EINVAL);
  87. if (tb[NFTA_META_DREG])
  88. return &nft_meta_bridge_get_ops;
  89. if (tb[NFTA_META_SREG])
  90. return &nft_meta_bridge_set_ops;
  91. return ERR_PTR(-EINVAL);
  92. }
  93. static struct nft_expr_type nft_meta_bridge_type __read_mostly = {
  94. .family = NFPROTO_BRIDGE,
  95. .name = "meta",
  96. .select_ops = &nft_meta_bridge_select_ops,
  97. .policy = nft_meta_policy,
  98. .maxattr = NFTA_META_MAX,
  99. .owner = THIS_MODULE,
  100. };
  101. static int __init nft_meta_bridge_module_init(void)
  102. {
  103. return nft_register_expr(&nft_meta_bridge_type);
  104. }
  105. static void __exit nft_meta_bridge_module_exit(void)
  106. {
  107. nft_unregister_expr(&nft_meta_bridge_type);
  108. }
  109. module_init(nft_meta_bridge_module_init);
  110. module_exit(nft_meta_bridge_module_exit);
  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
  113. MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "meta");