libxt_quota.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Shared library add-on to iptables to add quota support
  3. *
  4. * Sam Johnston <samj@samj.net>
  5. */
  6. #include <stdio.h>
  7. #include <xtables.h>
  8. #include <linux/netfilter/xt_quota.h>
  9. enum {
  10. O_QUOTA = 0,
  11. };
  12. static const struct xt_option_entry quota_opts[] = {
  13. {.name = "quota", .id = O_QUOTA, .type = XTTYPE_UINT64,
  14. .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT,
  15. XTOPT_POINTER(struct xt_quota_info, quota)},
  16. XTOPT_TABLEEND,
  17. };
  18. static void quota_help(void)
  19. {
  20. printf("quota match options:\n"
  21. "[!] --quota quota quota (bytes)\n");
  22. }
  23. static void
  24. quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
  25. {
  26. const struct xt_quota_info *q = (const void *)match->data;
  27. printf(" quota: %llu bytes", (unsigned long long)q->quota);
  28. }
  29. static void
  30. quota_save(const void *ip, const struct xt_entry_match *match)
  31. {
  32. const struct xt_quota_info *q = (const void *)match->data;
  33. if (q->flags & XT_QUOTA_INVERT)
  34. printf("! ");
  35. printf(" --quota %llu", (unsigned long long) q->quota);
  36. }
  37. static void quota_parse(struct xt_option_call *cb)
  38. {
  39. struct xt_quota_info *info = cb->data;
  40. xtables_option_parse(cb);
  41. if (cb->invert)
  42. info->flags |= XT_QUOTA_INVERT;
  43. }
  44. static struct xtables_match quota_match = {
  45. .family = NFPROTO_UNSPEC,
  46. .name = "quota",
  47. .version = XTABLES_VERSION,
  48. .size = XT_ALIGN(sizeof (struct xt_quota_info)),
  49. .userspacesize = offsetof(struct xt_quota_info, master),
  50. .help = quota_help,
  51. .print = quota_print,
  52. .save = quota_save,
  53. .x6_parse = quota_parse,
  54. .x6_options = quota_opts,
  55. };
  56. void
  57. _init(void)
  58. {
  59. xtables_register_match(&quota_match);
  60. }