nft_payload.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/if_vlan.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables_core.h>
  18. #include <net/netfilter/nf_tables.h>
  19. /* add vlan header into the user buffer for if tag was removed by offloads */
  20. static bool
  21. nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len)
  22. {
  23. int mac_off = skb_mac_header(skb) - skb->data;
  24. u8 vlan_len, *vlanh, *dst_u8 = (u8 *) d;
  25. struct vlan_ethhdr veth;
  26. vlanh = (u8 *) &veth;
  27. if (offset < ETH_HLEN) {
  28. u8 ethlen = min_t(u8, len, ETH_HLEN - offset);
  29. if (skb_copy_bits(skb, mac_off, &veth, ETH_HLEN))
  30. return false;
  31. veth.h_vlan_proto = skb->vlan_proto;
  32. memcpy(dst_u8, vlanh + offset, ethlen);
  33. len -= ethlen;
  34. if (len == 0)
  35. return true;
  36. dst_u8 += ethlen;
  37. offset = ETH_HLEN;
  38. } else if (offset >= VLAN_ETH_HLEN) {
  39. offset -= VLAN_HLEN;
  40. goto skip;
  41. }
  42. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  43. veth.h_vlan_encapsulated_proto = skb->protocol;
  44. vlanh += offset;
  45. vlan_len = min_t(u8, len, VLAN_ETH_HLEN - offset);
  46. memcpy(dst_u8, vlanh, vlan_len);
  47. len -= vlan_len;
  48. if (!len)
  49. return true;
  50. dst_u8 += vlan_len;
  51. skip:
  52. return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0;
  53. }
  54. static void nft_payload_eval(const struct nft_expr *expr,
  55. struct nft_regs *regs,
  56. const struct nft_pktinfo *pkt)
  57. {
  58. const struct nft_payload *priv = nft_expr_priv(expr);
  59. const struct sk_buff *skb = pkt->skb;
  60. u32 *dest = &regs->data[priv->dreg];
  61. int offset;
  62. dest[priv->len / NFT_REG32_SIZE] = 0;
  63. switch (priv->base) {
  64. case NFT_PAYLOAD_LL_HEADER:
  65. if (!skb_mac_header_was_set(skb))
  66. goto err;
  67. if (skb_vlan_tag_present(skb)) {
  68. if (!nft_payload_copy_vlan(dest, skb,
  69. priv->offset, priv->len))
  70. goto err;
  71. return;
  72. }
  73. offset = skb_mac_header(skb) - skb->data;
  74. break;
  75. case NFT_PAYLOAD_NETWORK_HEADER:
  76. offset = skb_network_offset(skb);
  77. break;
  78. case NFT_PAYLOAD_TRANSPORT_HEADER:
  79. offset = pkt->xt.thoff;
  80. break;
  81. default:
  82. BUG();
  83. }
  84. offset += priv->offset;
  85. if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
  86. goto err;
  87. return;
  88. err:
  89. regs->verdict.code = NFT_BREAK;
  90. }
  91. static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
  92. [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 },
  93. [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 },
  94. [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 },
  95. [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 },
  96. };
  97. static int nft_payload_init(const struct nft_ctx *ctx,
  98. const struct nft_expr *expr,
  99. const struct nlattr * const tb[])
  100. {
  101. struct nft_payload *priv = nft_expr_priv(expr);
  102. priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
  103. priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
  104. priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
  105. priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
  106. return nft_validate_register_store(ctx, priv->dreg, NULL,
  107. NFT_DATA_VALUE, priv->len);
  108. }
  109. static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
  110. {
  111. const struct nft_payload *priv = nft_expr_priv(expr);
  112. if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
  113. nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
  114. nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
  115. nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
  116. goto nla_put_failure;
  117. return 0;
  118. nla_put_failure:
  119. return -1;
  120. }
  121. static struct nft_expr_type nft_payload_type;
  122. static const struct nft_expr_ops nft_payload_ops = {
  123. .type = &nft_payload_type,
  124. .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
  125. .eval = nft_payload_eval,
  126. .init = nft_payload_init,
  127. .dump = nft_payload_dump,
  128. };
  129. const struct nft_expr_ops nft_payload_fast_ops = {
  130. .type = &nft_payload_type,
  131. .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
  132. .eval = nft_payload_eval,
  133. .init = nft_payload_init,
  134. .dump = nft_payload_dump,
  135. };
  136. static const struct nft_expr_ops *
  137. nft_payload_select_ops(const struct nft_ctx *ctx,
  138. const struct nlattr * const tb[])
  139. {
  140. enum nft_payload_bases base;
  141. unsigned int offset, len;
  142. if (tb[NFTA_PAYLOAD_DREG] == NULL ||
  143. tb[NFTA_PAYLOAD_BASE] == NULL ||
  144. tb[NFTA_PAYLOAD_OFFSET] == NULL ||
  145. tb[NFTA_PAYLOAD_LEN] == NULL)
  146. return ERR_PTR(-EINVAL);
  147. base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
  148. switch (base) {
  149. case NFT_PAYLOAD_LL_HEADER:
  150. case NFT_PAYLOAD_NETWORK_HEADER:
  151. case NFT_PAYLOAD_TRANSPORT_HEADER:
  152. break;
  153. default:
  154. return ERR_PTR(-EOPNOTSUPP);
  155. }
  156. offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
  157. len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
  158. if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
  159. base != NFT_PAYLOAD_LL_HEADER)
  160. return &nft_payload_fast_ops;
  161. else
  162. return &nft_payload_ops;
  163. }
  164. static struct nft_expr_type nft_payload_type __read_mostly = {
  165. .name = "payload",
  166. .select_ops = nft_payload_select_ops,
  167. .policy = nft_payload_policy,
  168. .maxattr = NFTA_PAYLOAD_MAX,
  169. .owner = THIS_MODULE,
  170. };
  171. int __init nft_payload_module_init(void)
  172. {
  173. return nft_register_expr(&nft_payload_type);
  174. }
  175. void nft_payload_module_exit(void)
  176. {
  177. nft_unregister_expr(&nft_payload_type);
  178. }