nft_payload.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2008-2009 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_core.h>
  17. #include <net/netfilter/nf_tables.h>
  18. static void nft_payload_eval(const struct nft_expr *expr,
  19. struct nft_data data[NFT_REG_MAX + 1],
  20. const struct nft_pktinfo *pkt)
  21. {
  22. const struct nft_payload *priv = nft_expr_priv(expr);
  23. const struct sk_buff *skb = pkt->skb;
  24. struct nft_data *dest = &data[priv->dreg];
  25. int offset;
  26. switch (priv->base) {
  27. case NFT_PAYLOAD_LL_HEADER:
  28. if (!skb_mac_header_was_set(skb))
  29. goto err;
  30. offset = skb_mac_header(skb) - skb->data;
  31. break;
  32. case NFT_PAYLOAD_NETWORK_HEADER:
  33. offset = skb_network_offset(skb);
  34. break;
  35. case NFT_PAYLOAD_TRANSPORT_HEADER:
  36. offset = pkt->xt.thoff;
  37. break;
  38. default:
  39. BUG();
  40. }
  41. offset += priv->offset;
  42. if (skb_copy_bits(skb, offset, dest->data, priv->len) < 0)
  43. goto err;
  44. return;
  45. err:
  46. data[NFT_REG_VERDICT].verdict = NFT_BREAK;
  47. }
  48. static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
  49. [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 },
  50. [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 },
  51. [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 },
  52. [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 },
  53. };
  54. static int nft_payload_init(const struct nft_ctx *ctx,
  55. const struct nft_expr *expr,
  56. const struct nlattr * const tb[])
  57. {
  58. struct nft_payload *priv = nft_expr_priv(expr);
  59. int err;
  60. priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
  61. priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
  62. priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
  63. priv->dreg = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_DREG]));
  64. err = nft_validate_output_register(priv->dreg);
  65. if (err < 0)
  66. return err;
  67. return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
  68. }
  69. static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
  70. {
  71. const struct nft_payload *priv = nft_expr_priv(expr);
  72. if (nla_put_be32(skb, NFTA_PAYLOAD_DREG, htonl(priv->dreg)) ||
  73. nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
  74. nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
  75. nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
  76. goto nla_put_failure;
  77. return 0;
  78. nla_put_failure:
  79. return -1;
  80. }
  81. static struct nft_expr_type nft_payload_type;
  82. static const struct nft_expr_ops nft_payload_ops = {
  83. .type = &nft_payload_type,
  84. .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
  85. .eval = nft_payload_eval,
  86. .init = nft_payload_init,
  87. .dump = nft_payload_dump,
  88. };
  89. const struct nft_expr_ops nft_payload_fast_ops = {
  90. .type = &nft_payload_type,
  91. .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
  92. .eval = nft_payload_eval,
  93. .init = nft_payload_init,
  94. .dump = nft_payload_dump,
  95. };
  96. static const struct nft_expr_ops *
  97. nft_payload_select_ops(const struct nft_ctx *ctx,
  98. const struct nlattr * const tb[])
  99. {
  100. enum nft_payload_bases base;
  101. unsigned int offset, len;
  102. if (tb[NFTA_PAYLOAD_DREG] == NULL ||
  103. tb[NFTA_PAYLOAD_BASE] == NULL ||
  104. tb[NFTA_PAYLOAD_OFFSET] == NULL ||
  105. tb[NFTA_PAYLOAD_LEN] == NULL)
  106. return ERR_PTR(-EINVAL);
  107. base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
  108. switch (base) {
  109. case NFT_PAYLOAD_LL_HEADER:
  110. case NFT_PAYLOAD_NETWORK_HEADER:
  111. case NFT_PAYLOAD_TRANSPORT_HEADER:
  112. break;
  113. default:
  114. return ERR_PTR(-EOPNOTSUPP);
  115. }
  116. offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
  117. len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
  118. if (len == 0 || len > FIELD_SIZEOF(struct nft_data, data))
  119. return ERR_PTR(-EINVAL);
  120. if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
  121. base != NFT_PAYLOAD_LL_HEADER)
  122. return &nft_payload_fast_ops;
  123. else
  124. return &nft_payload_ops;
  125. }
  126. static struct nft_expr_type nft_payload_type __read_mostly = {
  127. .name = "payload",
  128. .select_ops = nft_payload_select_ops,
  129. .policy = nft_payload_policy,
  130. .maxattr = NFTA_PAYLOAD_MAX,
  131. .owner = THIS_MODULE,
  132. };
  133. int __init nft_payload_module_init(void)
  134. {
  135. return nft_register_expr(&nft_payload_type);
  136. }
  137. void nft_payload_module_exit(void)
  138. {
  139. nft_unregister_expr(&nft_payload_type);
  140. }