libxt_length.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdio.h>
  2. #include <xtables.h>
  3. #include <linux/netfilter/xt_length.h>
  4. enum {
  5. O_LENGTH = 0,
  6. };
  7. static void length_help(void)
  8. {
  9. printf(
  10. "length match options:\n"
  11. "[!] --length length[:length] Match packet length against value or range\n"
  12. " of values (inclusive)\n");
  13. }
  14. static const struct xt_option_entry length_opts[] = {
  15. {.name = "length", .id = O_LENGTH, .type = XTTYPE_UINT16RC,
  16. .flags = XTOPT_MAND | XTOPT_INVERT},
  17. XTOPT_TABLEEND,
  18. };
  19. static void length_parse(struct xt_option_call *cb)
  20. {
  21. struct xt_length_info *info = cb->data;
  22. xtables_option_parse(cb);
  23. info->min = cb->val.u16_range[0];
  24. info->max = cb->val.u16_range[0];
  25. if (cb->nvals >= 2)
  26. info->max = cb->val.u16_range[1];
  27. if (cb->invert)
  28. info->invert = 1;
  29. }
  30. static void
  31. length_print(const void *ip, const struct xt_entry_match *match, int numeric)
  32. {
  33. const struct xt_length_info *info = (void *)match->data;
  34. printf(" length %s", info->invert ? "!" : "");
  35. if (info->min == info->max)
  36. printf("%u", info->min);
  37. else
  38. printf("%u:%u", info->min, info->max);
  39. }
  40. static void length_save(const void *ip, const struct xt_entry_match *match)
  41. {
  42. const struct xt_length_info *info = (void *)match->data;
  43. printf("%s --length ", info->invert ? " !" : "");
  44. if (info->min == info->max)
  45. printf("%u", info->min);
  46. else
  47. printf("%u:%u", info->min, info->max);
  48. }
  49. static struct xtables_match length_match = {
  50. .family = NFPROTO_UNSPEC,
  51. .name = "length",
  52. .version = XTABLES_VERSION,
  53. .size = XT_ALIGN(sizeof(struct xt_length_info)),
  54. .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
  55. .help = length_help,
  56. .print = length_print,
  57. .save = length_save,
  58. .x6_parse = length_parse,
  59. .x6_options = length_opts,
  60. };
  61. void _init(void)
  62. {
  63. xtables_register_match(&length_match);
  64. }