nft_masq.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
  20. [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
  21. };
  22. EXPORT_SYMBOL_GPL(nft_masq_policy);
  23. int nft_masq_validate(const struct nft_ctx *ctx,
  24. const struct nft_expr *expr,
  25. const struct nft_data **data)
  26. {
  27. int err;
  28. err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  29. if (err < 0)
  30. return err;
  31. return nft_chain_validate_hooks(ctx->chain,
  32. (1 << NF_INET_POST_ROUTING));
  33. }
  34. EXPORT_SYMBOL_GPL(nft_masq_validate);
  35. int nft_masq_init(const struct nft_ctx *ctx,
  36. const struct nft_expr *expr,
  37. const struct nlattr * const tb[])
  38. {
  39. u32 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
  40. struct nft_masq *priv = nft_expr_priv(expr);
  41. int err;
  42. err = nft_masq_validate(ctx, expr, NULL);
  43. if (err)
  44. return err;
  45. if (tb[NFTA_MASQ_FLAGS]) {
  46. priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
  47. if (priv->flags & ~NF_NAT_RANGE_MASK)
  48. return -EINVAL;
  49. }
  50. if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
  51. priv->sreg_proto_min =
  52. nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MIN]);
  53. err = nft_validate_register_load(priv->sreg_proto_min, plen);
  54. if (err < 0)
  55. return err;
  56. if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
  57. priv->sreg_proto_max =
  58. nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MAX]);
  59. err = nft_validate_register_load(priv->sreg_proto_max,
  60. plen);
  61. if (err < 0)
  62. return err;
  63. } else {
  64. priv->sreg_proto_max = priv->sreg_proto_min;
  65. }
  66. }
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(nft_masq_init);
  70. int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
  71. {
  72. const struct nft_masq *priv = nft_expr_priv(expr);
  73. if (priv->flags != 0 &&
  74. nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
  75. goto nla_put_failure;
  76. if (priv->sreg_proto_min) {
  77. if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
  78. priv->sreg_proto_min) ||
  79. nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
  80. priv->sreg_proto_max))
  81. goto nla_put_failure;
  82. }
  83. return 0;
  84. nla_put_failure:
  85. return -1;
  86. }
  87. EXPORT_SYMBOL_GPL(nft_masq_dump);
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");