nft_redir.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_nat.h>
  15. #include <net/netfilter/nf_tables.h>
  16. #include <net/netfilter/nft_redir.h>
  17. const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = {
  18. [NFTA_REDIR_REG_PROTO_MIN] = { .type = NLA_U32 },
  19. [NFTA_REDIR_REG_PROTO_MAX] = { .type = NLA_U32 },
  20. [NFTA_REDIR_FLAGS] = { .type = NLA_U32 },
  21. };
  22. EXPORT_SYMBOL_GPL(nft_redir_policy);
  23. int nft_redir_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_PRE_ROUTING) |
  33. (1 << NF_INET_LOCAL_OUT));
  34. }
  35. EXPORT_SYMBOL_GPL(nft_redir_validate);
  36. int nft_redir_init(const struct nft_ctx *ctx,
  37. const struct nft_expr *expr,
  38. const struct nlattr * const tb[])
  39. {
  40. struct nft_redir *priv = nft_expr_priv(expr);
  41. unsigned int plen;
  42. int err;
  43. err = nft_redir_validate(ctx, expr, NULL);
  44. if (err < 0)
  45. return err;
  46. plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
  47. if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
  48. priv->sreg_proto_min =
  49. nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
  50. err = nft_validate_register_load(priv->sreg_proto_min, plen);
  51. if (err < 0)
  52. return err;
  53. if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
  54. priv->sreg_proto_max =
  55. nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
  56. err = nft_validate_register_load(priv->sreg_proto_max,
  57. plen);
  58. if (err < 0)
  59. return err;
  60. } else {
  61. priv->sreg_proto_max = priv->sreg_proto_min;
  62. }
  63. }
  64. if (tb[NFTA_REDIR_FLAGS]) {
  65. priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
  66. if (priv->flags & ~NF_NAT_RANGE_MASK)
  67. return -EINVAL;
  68. }
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(nft_redir_init);
  72. int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
  73. {
  74. const struct nft_redir *priv = nft_expr_priv(expr);
  75. if (priv->sreg_proto_min) {
  76. if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
  77. priv->sreg_proto_min))
  78. goto nla_put_failure;
  79. if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
  80. priv->sreg_proto_max))
  81. goto nla_put_failure;
  82. }
  83. if (priv->flags != 0 &&
  84. nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
  85. goto nla_put_failure;
  86. return 0;
  87. nla_put_failure:
  88. return -1;
  89. }
  90. EXPORT_SYMBOL_GPL(nft_redir_dump);
  91. MODULE_LICENSE("GPL");
  92. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");