nft_exthdr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
  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. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/netlink.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter/nf_tables.h>
  16. #include <net/netfilter/nf_tables.h>
  17. // FIXME:
  18. #include <net/ipv6.h>
  19. struct nft_exthdr {
  20. u8 type;
  21. u8 offset;
  22. u8 len;
  23. enum nft_registers dreg:8;
  24. };
  25. static void nft_exthdr_eval(const struct nft_expr *expr,
  26. struct nft_regs *regs,
  27. const struct nft_pktinfo *pkt)
  28. {
  29. struct nft_exthdr *priv = nft_expr_priv(expr);
  30. u32 *dest = &regs->data[priv->dreg];
  31. unsigned int offset = 0;
  32. int err;
  33. err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
  34. if (err < 0)
  35. goto err;
  36. offset += priv->offset;
  37. dest[priv->len / NFT_REG32_SIZE] = 0;
  38. if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
  39. goto err;
  40. return;
  41. err:
  42. regs->verdict.code = NFT_BREAK;
  43. }
  44. static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
  45. [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
  46. [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
  47. [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
  48. [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
  49. };
  50. static int nft_exthdr_init(const struct nft_ctx *ctx,
  51. const struct nft_expr *expr,
  52. const struct nlattr * const tb[])
  53. {
  54. struct nft_exthdr *priv = nft_expr_priv(expr);
  55. if (tb[NFTA_EXTHDR_DREG] == NULL ||
  56. tb[NFTA_EXTHDR_TYPE] == NULL ||
  57. tb[NFTA_EXTHDR_OFFSET] == NULL ||
  58. tb[NFTA_EXTHDR_LEN] == NULL)
  59. return -EINVAL;
  60. priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
  61. priv->offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET]));
  62. priv->len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN]));
  63. priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
  64. return nft_validate_register_store(ctx, priv->dreg, NULL,
  65. NFT_DATA_VALUE, priv->len);
  66. }
  67. static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
  68. {
  69. const struct nft_exthdr *priv = nft_expr_priv(expr);
  70. if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
  71. goto nla_put_failure;
  72. if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
  73. goto nla_put_failure;
  74. if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
  75. goto nla_put_failure;
  76. if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
  77. goto nla_put_failure;
  78. return 0;
  79. nla_put_failure:
  80. return -1;
  81. }
  82. static struct nft_expr_type nft_exthdr_type;
  83. static const struct nft_expr_ops nft_exthdr_ops = {
  84. .type = &nft_exthdr_type,
  85. .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
  86. .eval = nft_exthdr_eval,
  87. .init = nft_exthdr_init,
  88. .dump = nft_exthdr_dump,
  89. };
  90. static struct nft_expr_type nft_exthdr_type __read_mostly = {
  91. .name = "exthdr",
  92. .ops = &nft_exthdr_ops,
  93. .policy = nft_exthdr_policy,
  94. .maxattr = NFTA_EXTHDR_MAX,
  95. .owner = THIS_MODULE,
  96. };
  97. static int __init nft_exthdr_module_init(void)
  98. {
  99. return nft_register_expr(&nft_exthdr_type);
  100. }
  101. static void __exit nft_exthdr_module_exit(void)
  102. {
  103. nft_unregister_expr(&nft_exthdr_type);
  104. }
  105. module_init(nft_exthdr_module_init);
  106. module_exit(nft_exthdr_module_exit);
  107. MODULE_LICENSE("GPL");
  108. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  109. MODULE_ALIAS_NFT_EXPR("exthdr");