xt_u32.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * xt_u32 - kernel module to match u32 packet content
  4. *
  5. * Original author: Don Cohen <don@isis.cs3-inc.com>
  6. * (C) CC Computer Consultants GmbH, 2007
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/types.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter/xt_u32.h>
  15. static bool u32_match_it(const struct xt_u32 *data,
  16. const struct sk_buff *skb)
  17. {
  18. const struct xt_u32_test *ct;
  19. unsigned int testind;
  20. unsigned int nnums;
  21. unsigned int nvals;
  22. unsigned int i;
  23. __be32 n;
  24. u_int32_t pos;
  25. u_int32_t val;
  26. u_int32_t at;
  27. /*
  28. * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
  29. * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
  30. */
  31. for (testind = 0; testind < data->ntests; ++testind) {
  32. ct = &data->tests[testind];
  33. at = 0;
  34. pos = ct->location[0].number;
  35. if (skb->len < 4 || pos > skb->len - 4)
  36. return false;
  37. if (skb_copy_bits(skb, pos, &n, sizeof(n)) < 0)
  38. BUG();
  39. val = ntohl(n);
  40. nnums = ct->nnums;
  41. /* Inner loop runs over "&", "<<", ">>" and "@" operands */
  42. for (i = 1; i < nnums; ++i) {
  43. u_int32_t number = ct->location[i].number;
  44. switch (ct->location[i].nextop) {
  45. case XT_U32_AND:
  46. val &= number;
  47. break;
  48. case XT_U32_LEFTSH:
  49. val <<= number;
  50. break;
  51. case XT_U32_RIGHTSH:
  52. val >>= number;
  53. break;
  54. case XT_U32_AT:
  55. if (at + val < at)
  56. return false;
  57. at += val;
  58. pos = number;
  59. if (at + 4 < at || skb->len < at + 4 ||
  60. pos > skb->len - at - 4)
  61. return false;
  62. if (skb_copy_bits(skb, at + pos, &n,
  63. sizeof(n)) < 0)
  64. BUG();
  65. val = ntohl(n);
  66. break;
  67. }
  68. }
  69. /* Run over the "," and ":" operands */
  70. nvals = ct->nvalues;
  71. for (i = 0; i < nvals; ++i)
  72. if (ct->value[i].min <= val && val <= ct->value[i].max)
  73. break;
  74. if (i >= ct->nvalues)
  75. return false;
  76. }
  77. return true;
  78. }
  79. static bool u32_mt(const struct sk_buff *skb, struct xt_action_param *par)
  80. {
  81. const struct xt_u32 *data = par->matchinfo;
  82. bool ret;
  83. ret = u32_match_it(data, skb);
  84. return ret ^ data->invert;
  85. }
  86. static struct xt_match xt_u32_mt_reg __read_mostly = {
  87. .name = "u32",
  88. .revision = 0,
  89. .family = NFPROTO_UNSPEC,
  90. .match = u32_mt,
  91. .matchsize = sizeof(struct xt_u32),
  92. .me = THIS_MODULE,
  93. };
  94. static int __init u32_mt_init(void)
  95. {
  96. return xt_register_match(&xt_u32_mt_reg);
  97. }
  98. static void __exit u32_mt_exit(void)
  99. {
  100. xt_unregister_match(&xt_u32_mt_reg);
  101. }
  102. module_init(u32_mt_init);
  103. module_exit(u32_mt_exit);
  104. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  105. MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
  106. MODULE_LICENSE("GPL");
  107. MODULE_ALIAS("ipt_u32");
  108. MODULE_ALIAS("ip6t_u32");