xt_u32.c 2.7 KB

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