/release/src/router/iptables/extensions/libip6t_LOG.c

https://gitlab.com/envieidoc/advancedtomato2 · C · 314 lines · 258 code · 48 blank · 8 comment · 46 complexity · 1f8c20b5455d8d0a47ca21a4d1f66489 MD5 · raw file

  1. /* Shared library add-on to iptables to add LOG support. */
  2. #include <stdio.h>
  3. #include <netdb.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <syslog.h>
  7. #include <getopt.h>
  8. #include <ip6tables.h>
  9. #include <linux/netfilter_ipv6/ip6_tables.h>
  10. #include <linux/netfilter_ipv6/ip6t_LOG.h>
  11. #ifndef IP6T_LOG_UID /* Old kernel */
  12. #define IP6T_LOG_UID 0x08
  13. #undef IP6T_LOG_MASK
  14. #define IP6T_LOG_MASK 0x0f
  15. #endif
  16. #ifndef IPT_LOG_MACDECODE /* Old kernel */
  17. #define IPT_LOG_MACDECODE 0x20
  18. #undef IPT_LOG_MASK
  19. #define IPT_LOG_MASK 0x2f
  20. #endif
  21. #define LOG_DEFAULT_LEVEL LOG_WARNING
  22. /* Function which prints out usage message. */
  23. static void
  24. help(void)
  25. {
  26. printf(
  27. "LOG v%s options:\n"
  28. " --log-level level Level of logging (numeric or see syslog.conf)\n"
  29. " --log-prefix prefix Prefix log messages with this prefix.\n\n"
  30. " --log-tcp-sequence Log TCP sequence numbers.\n\n"
  31. " --log-tcp-options Log TCP options.\n\n"
  32. " --log-ip-options Log IP options.\n\n"
  33. " --log-uid Log UID owning the local socket.\n\n"
  34. " --log-macdecode Decode MAC addresses and protocol.\n\n",
  35. IPTABLES_VERSION);
  36. }
  37. static struct option opts[] = {
  38. { .name = "log-level", .has_arg = 1, .flag = 0, .val = '!' },
  39. { .name = "log-prefix", .has_arg = 1, .flag = 0, .val = '#' },
  40. { .name = "log-tcp-sequence", .has_arg = 0, .flag = 0, .val = '1' },
  41. { .name = "log-tcp-options", .has_arg = 0, .flag = 0, .val = '2' },
  42. { .name = "log-ip-options", .has_arg = 0, .flag = 0, .val = '3' },
  43. { .name = "log-uid", .has_arg = 0, .flag = 0, .val = '4' },
  44. { .name = "log-macdecode", .has_arg = 0, .flag = 0, .val = '5' },
  45. { .name = 0 }
  46. };
  47. /* Initialize the target. */
  48. static void
  49. init(struct ip6t_entry_target *t, unsigned int *nfcache)
  50. {
  51. struct ip6t_log_info *loginfo = (struct ip6t_log_info *)t->data;
  52. loginfo->level = LOG_DEFAULT_LEVEL;
  53. }
  54. struct ip6t_log_names {
  55. const char *name;
  56. unsigned int level;
  57. };
  58. static struct ip6t_log_names ip6t_log_names[]
  59. = { { .name = "alert", .level = LOG_ALERT },
  60. { .name = "crit", .level = LOG_CRIT },
  61. { .name = "debug", .level = LOG_DEBUG },
  62. { .name = "emerg", .level = LOG_EMERG },
  63. { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
  64. { .name = "info", .level = LOG_INFO },
  65. { .name = "notice", .level = LOG_NOTICE },
  66. { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
  67. { .name = "warning", .level = LOG_WARNING }
  68. };
  69. static u_int8_t
  70. parse_level(const char *level)
  71. {
  72. unsigned int lev = -1;
  73. unsigned int set = 0;
  74. if (string_to_number(level, 0, 7, &lev) == -1) {
  75. unsigned int i = 0;
  76. for (i = 0;
  77. i < sizeof(ip6t_log_names) / sizeof(struct ip6t_log_names);
  78. i++) {
  79. if (strncasecmp(level, ip6t_log_names[i].name,
  80. strlen(level)) == 0) {
  81. if (set++)
  82. exit_error(PARAMETER_PROBLEM,
  83. "log-level `%s' ambiguous",
  84. level);
  85. lev = ip6t_log_names[i].level;
  86. }
  87. }
  88. if (!set)
  89. exit_error(PARAMETER_PROBLEM,
  90. "log-level `%s' unknown", level);
  91. }
  92. return (u_int8_t)lev;
  93. }
  94. #define IP6T_LOG_OPT_LEVEL 0x01
  95. #define IP6T_LOG_OPT_PREFIX 0x02
  96. #define IP6T_LOG_OPT_TCPSEQ 0x04
  97. #define IP6T_LOG_OPT_TCPOPT 0x08
  98. #define IP6T_LOG_OPT_IPOPT 0x10
  99. #define IP6T_LOG_OPT_UID 0x20
  100. #define IPT_LOG_OPT_MACDECODE 0x40
  101. /* Function which parses command options; returns true if it
  102. ate an option */
  103. static int
  104. parse(int c, char **argv, int invert, unsigned int *flags,
  105. const struct ip6t_entry *entry,
  106. struct ip6t_entry_target **target)
  107. {
  108. struct ip6t_log_info *loginfo = (struct ip6t_log_info *)(*target)->data;
  109. switch (c) {
  110. case '!':
  111. if (*flags & IP6T_LOG_OPT_LEVEL)
  112. exit_error(PARAMETER_PROBLEM,
  113. "Can't specify --log-level twice");
  114. if (check_inverse(optarg, &invert, NULL, 0))
  115. exit_error(PARAMETER_PROBLEM,
  116. "Unexpected `!' after --log-level");
  117. loginfo->level = parse_level(optarg);
  118. *flags |= IP6T_LOG_OPT_LEVEL;
  119. break;
  120. case '#':
  121. if (*flags & IP6T_LOG_OPT_PREFIX)
  122. exit_error(PARAMETER_PROBLEM,
  123. "Can't specify --log-prefix twice");
  124. if (check_inverse(optarg, &invert, NULL, 0))
  125. exit_error(PARAMETER_PROBLEM,
  126. "Unexpected `!' after --log-prefix");
  127. if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
  128. exit_error(PARAMETER_PROBLEM,
  129. "Maximum prefix length %u for --log-prefix",
  130. (unsigned int)sizeof(loginfo->prefix) - 1);
  131. if (strlen(optarg) == 0)
  132. exit_error(PARAMETER_PROBLEM,
  133. "No prefix specified for --log-prefix");
  134. if (strlen(optarg) != strlen(strtok(optarg, "\n")))
  135. exit_error(PARAMETER_PROBLEM,
  136. "Newlines not allowed in --log-prefix");
  137. strcpy(loginfo->prefix, optarg);
  138. *flags |= IP6T_LOG_OPT_PREFIX;
  139. break;
  140. case '1':
  141. if (*flags & IP6T_LOG_OPT_TCPSEQ)
  142. exit_error(PARAMETER_PROBLEM,
  143. "Can't specify --log-tcp-sequence "
  144. "twice");
  145. loginfo->logflags |= IP6T_LOG_TCPSEQ;
  146. *flags |= IP6T_LOG_OPT_TCPSEQ;
  147. break;
  148. case '2':
  149. if (*flags & IP6T_LOG_OPT_TCPOPT)
  150. exit_error(PARAMETER_PROBLEM,
  151. "Can't specify --log-tcp-options twice");
  152. loginfo->logflags |= IP6T_LOG_TCPOPT;
  153. *flags |= IP6T_LOG_OPT_TCPOPT;
  154. break;
  155. case '3':
  156. if (*flags & IP6T_LOG_OPT_IPOPT)
  157. exit_error(PARAMETER_PROBLEM,
  158. "Can't specify --log-ip-options twice");
  159. loginfo->logflags |= IP6T_LOG_IPOPT;
  160. *flags |= IP6T_LOG_OPT_IPOPT;
  161. break;
  162. case '4':
  163. if (*flags & IP6T_LOG_OPT_UID)
  164. exit_error(PARAMETER_PROBLEM,
  165. "Can't specify --log-uid twice");
  166. loginfo->logflags |= IP6T_LOG_UID;
  167. *flags |= IP6T_LOG_OPT_UID;
  168. break;
  169. case '5':
  170. if (*flags & IPT_LOG_OPT_MACDECODE)
  171. exit_error(PARAMETER_PROBLEM,
  172. "Can't specify --log-macdecode twice");
  173. loginfo->logflags |= IPT_LOG_MACDECODE;
  174. *flags |= IPT_LOG_OPT_MACDECODE;
  175. break;
  176. default:
  177. return 0;
  178. }
  179. return 1;
  180. }
  181. /* Final check; nothing. */
  182. static void final_check(unsigned int flags)
  183. {
  184. }
  185. /* Prints out the targinfo. */
  186. static void
  187. print(const struct ip6t_ip6 *ip,
  188. const struct ip6t_entry_target *target,
  189. int numeric)
  190. {
  191. const struct ip6t_log_info *loginfo
  192. = (const struct ip6t_log_info *)target->data;
  193. unsigned int i = 0;
  194. printf("LOG ");
  195. if (numeric)
  196. printf("flags %u level %u ",
  197. loginfo->logflags, loginfo->level);
  198. else {
  199. for (i = 0;
  200. i < sizeof(ip6t_log_names) / sizeof(struct ip6t_log_names);
  201. i++) {
  202. if (loginfo->level == ip6t_log_names[i].level) {
  203. printf("level %s ", ip6t_log_names[i].name);
  204. break;
  205. }
  206. }
  207. if (i == sizeof(ip6t_log_names) / sizeof(struct ip6t_log_names))
  208. printf("UNKNOWN level %u ", loginfo->level);
  209. if (loginfo->logflags & IP6T_LOG_TCPSEQ)
  210. printf("tcp-sequence ");
  211. if (loginfo->logflags & IP6T_LOG_TCPOPT)
  212. printf("tcp-options ");
  213. if (loginfo->logflags & IP6T_LOG_IPOPT)
  214. printf("ip-options ");
  215. if (loginfo->logflags & IP6T_LOG_UID)
  216. printf("uid ");
  217. if (loginfo->logflags & IPT_LOG_MACDECODE)
  218. printf("macdecode ");
  219. if (loginfo->logflags & ~(IP6T_LOG_MASK))
  220. printf("unknown-flags ");
  221. }
  222. if (strcmp(loginfo->prefix, "") != 0)
  223. printf("prefix `%s' ", loginfo->prefix);
  224. }
  225. /* Saves the union ip6t_targinfo in parsable form to stdout. */
  226. static void
  227. save(const struct ip6t_ip6 *ip, const struct ip6t_entry_target *target)
  228. {
  229. #ifdef IPTABLES_SAVE
  230. const struct ip6t_log_info *loginfo
  231. = (const struct ip6t_log_info *)target->data;
  232. if (strcmp(loginfo->prefix, "") != 0)
  233. printf("--log-prefix \"%s\" ", loginfo->prefix);
  234. if (loginfo->level != LOG_DEFAULT_LEVEL)
  235. printf("--log-level %d ", loginfo->level);
  236. if (loginfo->logflags & IP6T_LOG_TCPSEQ)
  237. printf("--log-tcp-sequence ");
  238. if (loginfo->logflags & IP6T_LOG_TCPOPT)
  239. printf("--log-tcp-options ");
  240. if (loginfo->logflags & IP6T_LOG_IPOPT)
  241. printf("--log-ip-options ");
  242. if (loginfo->logflags & IP6T_LOG_UID)
  243. printf("--log-uid ");
  244. if (loginfo->logflags & IPT_LOG_MACDECODE)
  245. printf("--log-macdecode ");
  246. #endif
  247. }
  248. static
  249. struct ip6tables_target log
  250. = {
  251. .name = "LOG",
  252. .version = IPTABLES_VERSION,
  253. .size = IP6T_ALIGN(sizeof(struct ip6t_log_info)),
  254. .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_log_info)),
  255. .help = &help,
  256. .init = &init,
  257. .parse = &parse,
  258. .final_check = &final_check,
  259. .print = &print,
  260. .save = &save,
  261. .extra_opts = opts
  262. };
  263. void _init(void)
  264. {
  265. register_target6(&log);
  266. }