libipt_ULOG.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Shared library add-on to iptables to add ULOG support.
  2. *
  3. * (C) 2000 by Harald Welte <laforge@gnumonks.org>
  4. *
  5. * multipart netlink support based on ideas by Sebastian Zander
  6. * <zander@fokus.gmd.de>
  7. *
  8. * This software is released under the terms of GNU GPL
  9. *
  10. * libipt_ULOG.c,v 1.7 2001/01/30 11:55:02 laforge Exp
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <xtables.h>
  15. /* For 64bit kernel / 32bit userspace */
  16. #include <linux/netfilter_ipv4/ipt_ULOG.h>
  17. enum {
  18. O_ULOG_NLGROUP = 0,
  19. O_ULOG_PREFIX,
  20. O_ULOG_CPRANGE,
  21. O_ULOG_QTHR,
  22. };
  23. static void ULOG_help(void)
  24. {
  25. printf("ULOG target options:\n"
  26. " --ulog-nlgroup nlgroup NETLINK group used for logging\n"
  27. " --ulog-cprange size Bytes of each packet to be passed\n"
  28. " --ulog-qthreshold Threshold of in-kernel queue\n"
  29. " --ulog-prefix prefix Prefix log messages with this prefix.\n");
  30. }
  31. static const struct xt_option_entry ULOG_opts[] = {
  32. {.name = "ulog-nlgroup", .id = O_ULOG_NLGROUP, .type = XTTYPE_UINT8,
  33. .min = 1, .max = 32},
  34. {.name = "ulog-prefix", .id = O_ULOG_PREFIX, .type = XTTYPE_STRING,
  35. .flags = XTOPT_PUT, XTOPT_POINTER(struct ipt_ulog_info, prefix),
  36. .min = 1},
  37. {.name = "ulog-cprange", .id = O_ULOG_CPRANGE, .type = XTTYPE_UINT64},
  38. {.name = "ulog-qthreshold", .id = O_ULOG_QTHR, .type = XTTYPE_UINT64,
  39. .min = 1, .max = ULOG_MAX_QLEN},
  40. XTOPT_TABLEEND,
  41. };
  42. static void ULOG_init(struct xt_entry_target *t)
  43. {
  44. struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) t->data;
  45. loginfo->nl_group = ULOG_DEFAULT_NLGROUP;
  46. loginfo->qthreshold = ULOG_DEFAULT_QTHRESHOLD;
  47. }
  48. static void ULOG_parse(struct xt_option_call *cb)
  49. {
  50. struct ipt_ulog_info *loginfo = cb->data;
  51. xtables_option_parse(cb);
  52. switch (cb->entry->id) {
  53. case O_ULOG_NLGROUP:
  54. loginfo->nl_group = 1 << (cb->val.u8 - 1);
  55. break;
  56. case O_ULOG_PREFIX:
  57. if (strchr(cb->arg, '\n') != NULL)
  58. xtables_error(PARAMETER_PROBLEM,
  59. "Newlines not allowed in --ulog-prefix");
  60. break;
  61. case O_ULOG_CPRANGE:
  62. loginfo->copy_range = cb->val.u64;
  63. break;
  64. case O_ULOG_QTHR:
  65. loginfo->qthreshold = cb->val.u64;
  66. break;
  67. }
  68. }
  69. static void ULOG_save(const void *ip, const struct xt_entry_target *target)
  70. {
  71. const struct ipt_ulog_info *loginfo
  72. = (const struct ipt_ulog_info *) target->data;
  73. if (strcmp(loginfo->prefix, "") != 0) {
  74. fputs(" --ulog-prefix", stdout);
  75. xtables_save_string(loginfo->prefix);
  76. }
  77. if (loginfo->nl_group != ULOG_DEFAULT_NLGROUP)
  78. printf(" --ulog-nlgroup %d", ffs(loginfo->nl_group));
  79. if (loginfo->copy_range)
  80. printf(" --ulog-cprange %u", (unsigned int)loginfo->copy_range);
  81. if (loginfo->qthreshold != ULOG_DEFAULT_QTHRESHOLD)
  82. printf(" --ulog-qthreshold %u", (unsigned int)loginfo->qthreshold);
  83. }
  84. static void ULOG_print(const void *ip, const struct xt_entry_target *target,
  85. int numeric)
  86. {
  87. const struct ipt_ulog_info *loginfo
  88. = (const struct ipt_ulog_info *) target->data;
  89. printf(" ULOG ");
  90. printf("copy_range %u nlgroup %d", (unsigned int)loginfo->copy_range,
  91. ffs(loginfo->nl_group));
  92. if (strcmp(loginfo->prefix, "") != 0)
  93. printf(" prefix \"%s\"", loginfo->prefix);
  94. printf(" queue_threshold %u", (unsigned int)loginfo->qthreshold);
  95. }
  96. static struct xtables_target ulog_tg_reg = {
  97. .name = "ULOG",
  98. .version = XTABLES_VERSION,
  99. .family = NFPROTO_IPV4,
  100. .size = XT_ALIGN(sizeof(struct ipt_ulog_info)),
  101. .userspacesize = XT_ALIGN(sizeof(struct ipt_ulog_info)),
  102. .help = ULOG_help,
  103. .init = ULOG_init,
  104. .print = ULOG_print,
  105. .save = ULOG_save,
  106. .x6_parse = ULOG_parse,
  107. .x6_options = ULOG_opts,
  108. };
  109. void _init(void)
  110. {
  111. xtables_register_target(&ulog_tg_reg);
  112. }