nft_exthdr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. u32 offset, len;
  56. int err;
  57. if (tb[NFTA_EXTHDR_DREG] == NULL ||
  58. tb[NFTA_EXTHDR_TYPE] == NULL ||
  59. tb[NFTA_EXTHDR_OFFSET] == NULL ||
  60. tb[NFTA_EXTHDR_LEN] == NULL)
  61. return -EINVAL;
  62. err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
  63. if (err < 0)
  64. return err;
  65. err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
  66. if (err < 0)
  67. return err;
  68. priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
  69. priv->offset = offset;
  70. priv->len = len;
  71. priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
  72. return nft_validate_register_store(ctx, priv->dreg, NULL,
  73. NFT_DATA_VALUE, priv->len);
  74. }
  75. static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
  76. {
  77. const struct nft_exthdr *priv = nft_expr_priv(expr);
  78. if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
  79. goto nla_put_failure;
  80. if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
  81. goto nla_put_failure;
  82. if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
  83. goto nla_put_failure;
  84. if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
  85. goto nla_put_failure;
  86. return 0;
  87. nla_put_failure:
  88. return -1;
  89. }
  90. static struct nft_expr_type nft_exthdr_type;
  91. static const struct nft_expr_ops nft_exthdr_ops = {
  92. .type = &nft_exthdr_type,
  93. .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
  94. .eval = nft_exthdr_eval,
  95. .init = nft_exthdr_init,
  96. .dump = nft_exthdr_dump,
  97. };
  98. static struct nft_expr_type nft_exthdr_type __read_mostly = {
  99. .name = "exthdr",
  100. .ops = &nft_exthdr_ops,
  101. .policy = nft_exthdr_policy,
  102. .maxattr = NFTA_EXTHDR_MAX,
  103. .owner = THIS_MODULE,
  104. };
  105. static int __init nft_exthdr_module_init(void)
  106. {
  107. return nft_register_expr(&nft_exthdr_type);
  108. }
  109. static void __exit nft_exthdr_module_exit(void)
  110. {
  111. nft_unregister_expr(&nft_exthdr_type);
  112. }
  113. module_init(nft_exthdr_module_init);
  114. module_exit(nft_exthdr_module_exit);
  115. MODULE_LICENSE("GPL");
  116. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  117. MODULE_ALIAS_NFT_EXPR("exthdr");