nft_redir_ipv6.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_redir.h>
  17. #include <net/netfilter/nf_nat_redirect.h>
  18. static void nft_redir_ipv6_eval(const struct nft_expr *expr,
  19. struct nft_regs *regs,
  20. const struct nft_pktinfo *pkt)
  21. {
  22. struct nft_redir *priv = nft_expr_priv(expr);
  23. struct nf_nat_range range;
  24. memset(&range, 0, sizeof(range));
  25. if (priv->sreg_proto_min) {
  26. range.min_proto.all =
  27. *(__be16 *)&regs->data[priv->sreg_proto_min],
  28. range.max_proto.all =
  29. *(__be16 *)&regs->data[priv->sreg_proto_max],
  30. range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  31. }
  32. range.flags |= priv->flags;
  33. regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range, pkt->hook);
  34. }
  35. static struct nft_expr_type nft_redir_ipv6_type;
  36. static const struct nft_expr_ops nft_redir_ipv6_ops = {
  37. .type = &nft_redir_ipv6_type,
  38. .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
  39. .eval = nft_redir_ipv6_eval,
  40. .init = nft_redir_init,
  41. .dump = nft_redir_dump,
  42. .validate = nft_redir_validate,
  43. };
  44. static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
  45. .family = NFPROTO_IPV6,
  46. .name = "redir",
  47. .ops = &nft_redir_ipv6_ops,
  48. .policy = nft_redir_policy,
  49. .maxattr = NFTA_REDIR_MAX,
  50. .owner = THIS_MODULE,
  51. };
  52. static int __init nft_redir_ipv6_module_init(void)
  53. {
  54. return nft_register_expr(&nft_redir_ipv6_type);
  55. }
  56. static void __exit nft_redir_ipv6_module_exit(void)
  57. {
  58. nft_unregister_expr(&nft_redir_ipv6_type);
  59. }
  60. module_init(nft_redir_ipv6_module_init);
  61. module_exit(nft_redir_ipv6_module_exit);
  62. MODULE_LICENSE("GPL");
  63. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");
  64. MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "redir");