nft_masq.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
  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. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/netlink.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_nat.h>
  16. #include <net/netfilter/nft_masq.h>
  17. const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
  18. [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
  19. };
  20. EXPORT_SYMBOL_GPL(nft_masq_policy);
  21. int nft_masq_validate(const struct nft_ctx *ctx,
  22. const struct nft_expr *expr,
  23. const struct nft_data **data)
  24. {
  25. int err;
  26. err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  27. if (err < 0)
  28. return err;
  29. return nft_chain_validate_hooks(ctx->chain,
  30. (1 << NF_INET_POST_ROUTING));
  31. }
  32. EXPORT_SYMBOL_GPL(nft_masq_validate);
  33. int nft_masq_init(const struct nft_ctx *ctx,
  34. const struct nft_expr *expr,
  35. const struct nlattr * const tb[])
  36. {
  37. struct nft_masq *priv = nft_expr_priv(expr);
  38. int err;
  39. err = nft_masq_validate(ctx, expr, NULL);
  40. if (err)
  41. return err;
  42. if (tb[NFTA_MASQ_FLAGS] == NULL)
  43. return 0;
  44. priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
  45. if (priv->flags & ~NF_NAT_RANGE_MASK)
  46. return -EINVAL;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(nft_masq_init);
  50. int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
  51. {
  52. const struct nft_masq *priv = nft_expr_priv(expr);
  53. if (priv->flags == 0)
  54. return 0;
  55. if (nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
  56. goto nla_put_failure;
  57. return 0;
  58. nla_put_failure:
  59. return -1;
  60. }
  61. EXPORT_SYMBOL_GPL(nft_masq_dump);
  62. MODULE_LICENSE("GPL");
  63. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");