libipt_LOG.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <syslog.h>
  4. #include <xtables.h>
  5. #include <linux/netfilter_ipv4/ipt_LOG.h>
  6. #define LOG_DEFAULT_LEVEL LOG_WARNING
  7. #ifndef IPT_LOG_UID /* Old kernel */
  8. #define IPT_LOG_UID 0x08 /* Log UID owning local socket */
  9. #undef IPT_LOG_MASK
  10. #define IPT_LOG_MASK 0x0f
  11. #endif
  12. enum {
  13. O_LOG_LEVEL = 0,
  14. O_LOG_PREFIX,
  15. O_LOG_TCPSEQ,
  16. O_LOG_TCPOPTS,
  17. O_LOG_IPOPTS,
  18. O_LOG_UID,
  19. O_LOG_MAC,
  20. };
  21. static void LOG_help(void)
  22. {
  23. printf(
  24. "LOG target options:\n"
  25. " --log-level level Level of logging (numeric or see syslog.conf)\n"
  26. " --log-prefix prefix Prefix log messages with this prefix.\n\n"
  27. " --log-tcp-sequence Log TCP sequence numbers.\n\n"
  28. " --log-tcp-options Log TCP options.\n\n"
  29. " --log-ip-options Log IP options.\n\n"
  30. " --log-uid Log UID owning the local socket.\n\n"
  31. " --log-macdecode Decode MAC addresses and protocol.\n\n");
  32. }
  33. #define s struct ipt_log_info
  34. static const struct xt_option_entry LOG_opts[] = {
  35. {.name = "log-level", .id = O_LOG_LEVEL, .type = XTTYPE_SYSLOGLEVEL,
  36. .flags = XTOPT_PUT, XTOPT_POINTER(s, level)},
  37. {.name = "log-prefix", .id = O_LOG_PREFIX, .type = XTTYPE_STRING,
  38. .flags = XTOPT_PUT, XTOPT_POINTER(s, prefix), .min = 1},
  39. {.name = "log-tcp-sequence", .id = O_LOG_TCPSEQ, .type = XTTYPE_NONE},
  40. {.name = "log-tcp-options", .id = O_LOG_TCPOPTS, .type = XTTYPE_NONE},
  41. {.name = "log-ip-options", .id = O_LOG_IPOPTS, .type = XTTYPE_NONE},
  42. {.name = "log-uid", .id = O_LOG_UID, .type = XTTYPE_NONE},
  43. {.name = "log-macdecode", .id = O_LOG_MAC, .type = XTTYPE_NONE},
  44. XTOPT_TABLEEND,
  45. };
  46. #undef s
  47. static void LOG_init(struct xt_entry_target *t)
  48. {
  49. struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
  50. loginfo->level = LOG_DEFAULT_LEVEL;
  51. }
  52. struct ipt_log_names {
  53. const char *name;
  54. unsigned int level;
  55. };
  56. static const struct ipt_log_names ipt_log_names[]
  57. = { { .name = "alert", .level = LOG_ALERT },
  58. { .name = "crit", .level = LOG_CRIT },
  59. { .name = "debug", .level = LOG_DEBUG },
  60. { .name = "emerg", .level = LOG_EMERG },
  61. { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
  62. { .name = "info", .level = LOG_INFO },
  63. { .name = "notice", .level = LOG_NOTICE },
  64. { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
  65. { .name = "warning", .level = LOG_WARNING }
  66. };
  67. static void LOG_parse(struct xt_option_call *cb)
  68. {
  69. struct ipt_log_info *info = cb->data;
  70. xtables_option_parse(cb);
  71. switch (cb->entry->id) {
  72. case O_LOG_PREFIX:
  73. if (strchr(cb->arg, '\n') != NULL)
  74. xtables_error(PARAMETER_PROBLEM,
  75. "Newlines not allowed in --log-prefix");
  76. break;
  77. case O_LOG_TCPSEQ:
  78. info->logflags |= IPT_LOG_TCPSEQ;
  79. break;
  80. case O_LOG_TCPOPTS:
  81. info->logflags |= IPT_LOG_TCPOPT;
  82. break;
  83. case O_LOG_IPOPTS:
  84. info->logflags |= IPT_LOG_IPOPT;
  85. break;
  86. case O_LOG_UID:
  87. info->logflags |= IPT_LOG_UID;
  88. break;
  89. case O_LOG_MAC:
  90. info->logflags |= IPT_LOG_MACDECODE;
  91. break;
  92. }
  93. }
  94. static void LOG_print(const void *ip, const struct xt_entry_target *target,
  95. int numeric)
  96. {
  97. const struct ipt_log_info *loginfo
  98. = (const struct ipt_log_info *)target->data;
  99. unsigned int i = 0;
  100. printf(" LOG");
  101. if (numeric)
  102. printf(" flags %u level %u",
  103. loginfo->logflags, loginfo->level);
  104. else {
  105. for (i = 0; i < ARRAY_SIZE(ipt_log_names); ++i)
  106. if (loginfo->level == ipt_log_names[i].level) {
  107. printf(" level %s", ipt_log_names[i].name);
  108. break;
  109. }
  110. if (i == ARRAY_SIZE(ipt_log_names))
  111. printf(" UNKNOWN level %u", loginfo->level);
  112. if (loginfo->logflags & IPT_LOG_TCPSEQ)
  113. printf(" tcp-sequence");
  114. if (loginfo->logflags & IPT_LOG_TCPOPT)
  115. printf(" tcp-options");
  116. if (loginfo->logflags & IPT_LOG_IPOPT)
  117. printf(" ip-options");
  118. if (loginfo->logflags & IPT_LOG_UID)
  119. printf(" uid");
  120. if (loginfo->logflags & IPT_LOG_MACDECODE)
  121. printf(" macdecode");
  122. if (loginfo->logflags & ~(IPT_LOG_MASK))
  123. printf(" unknown-flags");
  124. }
  125. if (strcmp(loginfo->prefix, "") != 0)
  126. printf(" prefix \"%s\"", loginfo->prefix);
  127. }
  128. static void LOG_save(const void *ip, const struct xt_entry_target *target)
  129. {
  130. const struct ipt_log_info *loginfo
  131. = (const struct ipt_log_info *)target->data;
  132. if (strcmp(loginfo->prefix, "") != 0) {
  133. printf(" --log-prefix");
  134. xtables_save_string(loginfo->prefix);
  135. }
  136. if (loginfo->level != LOG_DEFAULT_LEVEL)
  137. printf(" --log-level %d", loginfo->level);
  138. if (loginfo->logflags & IPT_LOG_TCPSEQ)
  139. printf(" --log-tcp-sequence");
  140. if (loginfo->logflags & IPT_LOG_TCPOPT)
  141. printf(" --log-tcp-options");
  142. if (loginfo->logflags & IPT_LOG_IPOPT)
  143. printf(" --log-ip-options");
  144. if (loginfo->logflags & IPT_LOG_UID)
  145. printf(" --log-uid");
  146. if (loginfo->logflags & IPT_LOG_MACDECODE)
  147. printf(" --log-macdecode");
  148. }
  149. static struct xtables_target log_tg_reg = {
  150. .name = "LOG",
  151. .version = XTABLES_VERSION,
  152. .family = NFPROTO_IPV4,
  153. .size = XT_ALIGN(sizeof(struct ipt_log_info)),
  154. .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
  155. .help = LOG_help,
  156. .init = LOG_init,
  157. .print = LOG_print,
  158. .save = LOG_save,
  159. .x6_parse = LOG_parse,
  160. .x6_options = LOG_opts,
  161. };
  162. void _init(void)
  163. {
  164. xtables_register_target(&log_tg_reg);
  165. }