blob: b270bcf66f50276e9cb0aecdf748e1348e1c6d17 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001#include <stdio.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00002#include <string.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00003#include <syslog.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01004#include <xtables.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00005#include <linux/netfilter_ipv4/ipt_LOG.h>
6
7#define LOG_DEFAULT_LEVEL LOG_WARNING
8
Martin Josefsson2b9a5772005-01-05 15:21:15 +00009#ifndef IPT_LOG_UID /* Old kernel */
10#define IPT_LOG_UID 0x08 /* Log UID owning local socket */
11#undef IPT_LOG_MASK
12#define IPT_LOG_MASK 0x0f
13#endif
14
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010015enum {
16 O_LOG_LEVEL = 0,
17 O_LOG_PREFIX,
18 O_LOG_TCPSEQ,
19 O_LOG_TCPOPTS,
20 O_LOG_IPOPTS,
21 O_LOG_UID,
22 O_LOG_MAC,
23};
24
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000025static void LOG_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000026{
27 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020028"LOG target options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000029" --log-level level Level of logging (numeric or see syslog.conf)\n"
30" --log-prefix prefix Prefix log messages with this prefix.\n\n"
31" --log-tcp-sequence Log TCP sequence numbers.\n\n"
32" --log-tcp-options Log TCP options.\n\n"
John Langef46e1af2005-01-02 23:33:12 +000033" --log-ip-options Log IP options.\n\n"
Patrick McHardy12764782010-06-28 14:51:35 +020034" --log-uid Log UID owning the local socket.\n\n"
35" --log-macdecode Decode MAC addresses and protocol.\n\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000036}
37
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010038#define s struct ipt_log_info
39static const struct xt_option_entry LOG_opts[] = {
40 {.name = "log-level", .id = O_LOG_LEVEL, .type = XTTYPE_SYSLOGLEVEL,
41 .flags = XTOPT_PUT, XTOPT_POINTER(s, level)},
42 {.name = "log-prefix", .id = O_LOG_PREFIX, .type = XTTYPE_STRING,
43 .flags = XTOPT_PUT, XTOPT_POINTER(s, prefix), .min = 1},
44 {.name = "log-tcp-sequence", .id = O_LOG_TCPSEQ, .type = XTTYPE_NONE},
45 {.name = "log-tcp-options", .id = O_LOG_TCPOPTS, .type = XTTYPE_NONE},
46 {.name = "log-ip-options", .id = O_LOG_IPOPTS, .type = XTTYPE_NONE},
47 {.name = "log-uid", .id = O_LOG_UID, .type = XTTYPE_NONE},
48 {.name = "log-macdecode", .id = O_LOG_MAC, .type = XTTYPE_NONE},
49 XTOPT_TABLEEND,
Marc Bouchere6869a82000-03-20 06:03:29 +000050};
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010051#undef s
Marc Bouchere6869a82000-03-20 06:03:29 +000052
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000053static void LOG_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000054{
55 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
56
57 loginfo->level = LOG_DEFAULT_LEVEL;
58
Marc Bouchere6869a82000-03-20 06:03:29 +000059}
60
61struct ipt_log_names {
62 const char *name;
63 unsigned int level;
64};
65
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000066static const struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000067= { { .name = "alert", .level = LOG_ALERT },
68 { .name = "crit", .level = LOG_CRIT },
69 { .name = "debug", .level = LOG_DEBUG },
70 { .name = "emerg", .level = LOG_EMERG },
71 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
72 { .name = "info", .level = LOG_INFO },
73 { .name = "notice", .level = LOG_NOTICE },
74 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
75 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000076};
77
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010078static void LOG_parse(struct xt_option_call *cb)
Marc Bouchere6869a82000-03-20 06:03:29 +000079{
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010080 struct ipt_log_info *info = cb->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000081
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010082 xtables_option_parse(cb);
83 switch (cb->entry->id) {
84 case O_LOG_PREFIX:
85 if (strchr(cb->arg, '\n') != NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010086 xtables_error(PARAMETER_PROBLEM,
Phil Oester182f3f62005-04-01 07:07:00 +000087 "Newlines not allowed in --log-prefix");
Marc Bouchere6869a82000-03-20 06:03:29 +000088 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010089 case O_LOG_TCPSEQ:
90 info->logflags = IPT_LOG_TCPSEQ;
Marc Bouchere6869a82000-03-20 06:03:29 +000091 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010092 case O_LOG_TCPOPTS:
93 info->logflags = IPT_LOG_TCPOPT;
Marc Bouchere6869a82000-03-20 06:03:29 +000094 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010095 case O_LOG_IPOPTS:
96 info->logflags = IPT_LOG_IPOPT;
Marc Bouchere6869a82000-03-20 06:03:29 +000097 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010098 case O_LOG_UID:
99 info->logflags = IPT_LOG_UID;
John Langef46e1af2005-01-02 23:33:12 +0000100 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100101 case O_LOG_MAC:
102 info->logflags = IPT_LOG_MACDECODE;
Patrick McHardy12764782010-06-28 14:51:35 +0200103 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000104 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000105}
106
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000107static void LOG_print(const void *ip, const struct xt_entry_target *target,
108 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000109{
110 const struct ipt_log_info *loginfo
111 = (const struct ipt_log_info *)target->data;
112 unsigned int i = 0;
113
Jan Engelhardt73866352010-12-18 02:04:59 +0100114 printf(" LOG");
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 if (numeric)
Jan Engelhardt73866352010-12-18 02:04:59 +0100116 printf(" flags %u level %u",
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 loginfo->logflags, loginfo->level);
118 else {
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200119 for (i = 0; i < ARRAY_SIZE(ipt_log_names); ++i)
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 if (loginfo->level == ipt_log_names[i].level) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100121 printf(" level %s", ipt_log_names[i].name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000122 break;
123 }
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200124 if (i == ARRAY_SIZE(ipt_log_names))
Jan Engelhardt73866352010-12-18 02:04:59 +0100125 printf(" UNKNOWN level %u", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000126 if (loginfo->logflags & IPT_LOG_TCPSEQ)
Jan Engelhardt73866352010-12-18 02:04:59 +0100127 printf(" tcp-sequence");
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 if (loginfo->logflags & IPT_LOG_TCPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100129 printf(" tcp-options");
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 if (loginfo->logflags & IPT_LOG_IPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100131 printf(" ip-options");
John Langef46e1af2005-01-02 23:33:12 +0000132 if (loginfo->logflags & IPT_LOG_UID)
Jan Engelhardt73866352010-12-18 02:04:59 +0100133 printf(" uid");
Patrick McHardy12764782010-06-28 14:51:35 +0200134 if (loginfo->logflags & IPT_LOG_MACDECODE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100135 printf(" macdecode");
Marc Bouchere6869a82000-03-20 06:03:29 +0000136 if (loginfo->logflags & ~(IPT_LOG_MASK))
Jan Engelhardt73866352010-12-18 02:04:59 +0100137 printf(" unknown-flags");
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 }
139
140 if (strcmp(loginfo->prefix, "") != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100141 printf(" prefix \"%s\"", loginfo->prefix);
Marc Bouchere6869a82000-03-20 06:03:29 +0000142}
143
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000144static void LOG_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000145{
146 const struct ipt_log_info *loginfo
147 = (const struct ipt_log_info *)target->data;
148
Max Kellermanna5d09942008-01-29 13:44:34 +0000149 if (strcmp(loginfo->prefix, "") != 0) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100150 printf(" --log-prefix");
Jan Engelhardta0baae82009-01-30 04:32:50 +0100151 xtables_save_string(loginfo->prefix);
Max Kellermanna5d09942008-01-29 13:44:34 +0000152 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000153
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000154 if (loginfo->level != LOG_DEFAULT_LEVEL)
Jan Engelhardt73866352010-12-18 02:04:59 +0100155 printf(" --log-level %d", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000156
157 if (loginfo->logflags & IPT_LOG_TCPSEQ)
Jan Engelhardt73866352010-12-18 02:04:59 +0100158 printf(" --log-tcp-sequence");
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 if (loginfo->logflags & IPT_LOG_TCPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100160 printf(" --log-tcp-options");
Marc Bouchere6869a82000-03-20 06:03:29 +0000161 if (loginfo->logflags & IPT_LOG_IPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100162 printf(" --log-ip-options");
John Langef46e1af2005-01-02 23:33:12 +0000163 if (loginfo->logflags & IPT_LOG_UID)
Jan Engelhardt73866352010-12-18 02:04:59 +0100164 printf(" --log-uid");
Patrick McHardy12764782010-06-28 14:51:35 +0200165 if (loginfo->logflags & IPT_LOG_MACDECODE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100166 printf(" --log-macdecode");
Marc Bouchere6869a82000-03-20 06:03:29 +0000167}
168
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200169static struct xtables_target log_tg_reg = {
Jan Engelhardte1df2212011-02-15 12:02:51 +0100170 .name = "LOG",
171 .version = XTABLES_VERSION,
172 .family = NFPROTO_IPV4,
173 .size = XT_ALIGN(sizeof(struct ipt_log_info)),
174 .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
175 .help = LOG_help,
176 .init = LOG_init,
Jan Engelhardte1df2212011-02-15 12:02:51 +0100177 .print = LOG_print,
178 .save = LOG_save,
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100179 .x6_parse = LOG_parse,
180 .x6_options = LOG_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000181};
182
183void _init(void)
184{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200185 xtables_register_target(&log_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000186}