nft_redir.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
  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. plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
  44. if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
  45. priv->sreg_proto_min =
  46. nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
  47. err = nft_validate_register_load(priv->sreg_proto_min, plen);
  48. if (err < 0)
  49. return err;
  50. if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
  51. priv->sreg_proto_max =
  52. nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
  53. err = nft_validate_register_load(priv->sreg_proto_max,
  54. plen);
  55. if (err < 0)
  56. return err;
  57. } else {
  58. priv->sreg_proto_max = priv->sreg_proto_min;
  59. }
  60. }
  61. if (tb[NFTA_REDIR_FLAGS]) {
  62. priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
  63. if (priv->flags & ~NF_NAT_RANGE_MASK)
  64. return -EINVAL;
  65. }
  66. return nf_ct_netns_get(ctx->net, ctx->family);
  67. }
  68. EXPORT_SYMBOL_GPL(nft_redir_init);
  69. int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
  70. {
  71. const struct nft_redir *priv = nft_expr_priv(expr);
  72. if (priv->sreg_proto_min) {
  73. if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
  74. priv->sreg_proto_min))
  75. goto nla_put_failure;
  76. if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
  77. priv->sreg_proto_max))
  78. goto nla_put_failure;
  79. }
  80. if (priv->flags != 0 &&
  81. nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
  82. goto nla_put_failure;
  83. return 0;
  84. nla_put_failure:
  85. return -1;
  86. }
  87. EXPORT_SYMBOL_GPL(nft_redir_dump);
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");