libxt_NFLOG.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdbool.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <getopt.h>
  6. #include <xtables.h>
  7. #include <linux/netfilter/x_tables.h>
  8. #include <linux/netfilter/xt_NFLOG.h>
  9. enum {
  10. O_GROUP = 0,
  11. O_PREFIX,
  12. O_RANGE,
  13. O_THRESHOLD,
  14. };
  15. #define s struct xt_nflog_info
  16. static const struct xt_option_entry NFLOG_opts[] = {
  17. {.name = "nflog-group", .id = O_GROUP, .type = XTTYPE_UINT16,
  18. .flags = XTOPT_PUT, XTOPT_POINTER(s, group)},
  19. {.name = "nflog-prefix", .id = O_PREFIX, .type = XTTYPE_STRING,
  20. .min = 1, .flags = XTOPT_PUT, XTOPT_POINTER(s, prefix)},
  21. {.name = "nflog-range", .id = O_RANGE, .type = XTTYPE_UINT32,
  22. .flags = XTOPT_PUT, XTOPT_POINTER(s, len)},
  23. {.name = "nflog-threshold", .id = O_THRESHOLD, .type = XTTYPE_UINT16,
  24. .flags = XTOPT_PUT, XTOPT_POINTER(s, threshold)},
  25. XTOPT_TABLEEND,
  26. };
  27. #undef s
  28. static void NFLOG_help(void)
  29. {
  30. printf("NFLOG target options:\n"
  31. " --nflog-group NUM NETLINK group used for logging\n"
  32. " --nflog-range NUM Number of byte to copy\n"
  33. " --nflog-threshold NUM Message threshold of in-kernel queue\n"
  34. " --nflog-prefix STRING Prefix string for log messages\n");
  35. }
  36. static void NFLOG_init(struct xt_entry_target *t)
  37. {
  38. struct xt_nflog_info *info = (struct xt_nflog_info *)t->data;
  39. info->threshold = XT_NFLOG_DEFAULT_THRESHOLD;
  40. }
  41. static void NFLOG_parse(struct xt_option_call *cb)
  42. {
  43. xtables_option_parse(cb);
  44. switch (cb->entry->id) {
  45. case O_PREFIX:
  46. if (strchr(cb->arg, '\n') != NULL)
  47. xtables_error(PARAMETER_PROBLEM,
  48. "Newlines not allowed in --log-prefix");
  49. break;
  50. }
  51. }
  52. static void nflog_print(const struct xt_nflog_info *info, char *prefix)
  53. {
  54. if (info->prefix[0] != '\0') {
  55. printf(" %snflog-prefix ", prefix);
  56. xtables_save_string(info->prefix);
  57. }
  58. if (info->group)
  59. printf(" %snflog-group %u", prefix, info->group);
  60. if (info->len)
  61. printf(" %snflog-range %u", prefix, info->len);
  62. if (info->threshold != XT_NFLOG_DEFAULT_THRESHOLD)
  63. printf(" %snflog-threshold %u", prefix, info->threshold);
  64. }
  65. static void NFLOG_print(const void *ip, const struct xt_entry_target *target,
  66. int numeric)
  67. {
  68. const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data;
  69. nflog_print(info, "");
  70. }
  71. static void NFLOG_save(const void *ip, const struct xt_entry_target *target)
  72. {
  73. const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data;
  74. nflog_print(info, "--");
  75. }
  76. static struct xtables_target nflog_target = {
  77. .family = NFPROTO_UNSPEC,
  78. .name = "NFLOG",
  79. .version = XTABLES_VERSION,
  80. .size = XT_ALIGN(sizeof(struct xt_nflog_info)),
  81. .userspacesize = XT_ALIGN(sizeof(struct xt_nflog_info)),
  82. .help = NFLOG_help,
  83. .init = NFLOG_init,
  84. .x6_parse = NFLOG_parse,
  85. .print = NFLOG_print,
  86. .save = NFLOG_save,
  87. .x6_options = NFLOG_opts,
  88. };
  89. void _init(void)
  90. {
  91. xtables_register_target(&nflog_target);
  92. }