blob: 36e2e73b7e360785b75ecf3a73a5932a79dd41ad [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
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +053066struct ipt_log_xlate {
67 const char *name;
68 unsigned int level;
69};
70
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000071static const struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000072= { { .name = "alert", .level = LOG_ALERT },
73 { .name = "crit", .level = LOG_CRIT },
74 { .name = "debug", .level = LOG_DEBUG },
75 { .name = "emerg", .level = LOG_EMERG },
76 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
77 { .name = "info", .level = LOG_INFO },
78 { .name = "notice", .level = LOG_NOTICE },
79 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
80 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000081};
82
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010083static void LOG_parse(struct xt_option_call *cb)
Marc Bouchere6869a82000-03-20 06:03:29 +000084{
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010085 struct ipt_log_info *info = cb->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000086
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010087 xtables_option_parse(cb);
88 switch (cb->entry->id) {
89 case O_LOG_PREFIX:
90 if (strchr(cb->arg, '\n') != NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010091 xtables_error(PARAMETER_PROBLEM,
Phil Oester182f3f62005-04-01 07:07:00 +000092 "Newlines not allowed in --log-prefix");
Marc Bouchere6869a82000-03-20 06:03:29 +000093 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010094 case O_LOG_TCPSEQ:
Jan Engelhardt4e98e812011-06-21 17:00:45 +020095 info->logflags |= IPT_LOG_TCPSEQ;
Marc Bouchere6869a82000-03-20 06:03:29 +000096 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +010097 case O_LOG_TCPOPTS:
Jan Engelhardt4e98e812011-06-21 17:00:45 +020098 info->logflags |= IPT_LOG_TCPOPT;
Marc Bouchere6869a82000-03-20 06:03:29 +000099 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100100 case O_LOG_IPOPTS:
Jan Engelhardt4e98e812011-06-21 17:00:45 +0200101 info->logflags |= IPT_LOG_IPOPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000102 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100103 case O_LOG_UID:
Jan Engelhardt4e98e812011-06-21 17:00:45 +0200104 info->logflags |= IPT_LOG_UID;
John Langef46e1af2005-01-02 23:33:12 +0000105 break;
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100106 case O_LOG_MAC:
Jan Engelhardt4e98e812011-06-21 17:00:45 +0200107 info->logflags |= IPT_LOG_MACDECODE;
Patrick McHardy12764782010-06-28 14:51:35 +0200108 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000109 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000110}
111
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000112static void LOG_print(const void *ip, const struct xt_entry_target *target,
113 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000114{
115 const struct ipt_log_info *loginfo
116 = (const struct ipt_log_info *)target->data;
117 unsigned int i = 0;
118
Jan Engelhardt73866352010-12-18 02:04:59 +0100119 printf(" LOG");
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 if (numeric)
Jan Engelhardt73866352010-12-18 02:04:59 +0100121 printf(" flags %u level %u",
Marc Bouchere6869a82000-03-20 06:03:29 +0000122 loginfo->logflags, loginfo->level);
123 else {
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200124 for (i = 0; i < ARRAY_SIZE(ipt_log_names); ++i)
Marc Bouchere6869a82000-03-20 06:03:29 +0000125 if (loginfo->level == ipt_log_names[i].level) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100126 printf(" level %s", ipt_log_names[i].name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 break;
128 }
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200129 if (i == ARRAY_SIZE(ipt_log_names))
Jan Engelhardt73866352010-12-18 02:04:59 +0100130 printf(" UNKNOWN level %u", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000131 if (loginfo->logflags & IPT_LOG_TCPSEQ)
Jan Engelhardt73866352010-12-18 02:04:59 +0100132 printf(" tcp-sequence");
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 if (loginfo->logflags & IPT_LOG_TCPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100134 printf(" tcp-options");
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 if (loginfo->logflags & IPT_LOG_IPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100136 printf(" ip-options");
John Langef46e1af2005-01-02 23:33:12 +0000137 if (loginfo->logflags & IPT_LOG_UID)
Jan Engelhardt73866352010-12-18 02:04:59 +0100138 printf(" uid");
Patrick McHardy12764782010-06-28 14:51:35 +0200139 if (loginfo->logflags & IPT_LOG_MACDECODE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100140 printf(" macdecode");
Marc Bouchere6869a82000-03-20 06:03:29 +0000141 if (loginfo->logflags & ~(IPT_LOG_MASK))
Jan Engelhardt73866352010-12-18 02:04:59 +0100142 printf(" unknown-flags");
Marc Bouchere6869a82000-03-20 06:03:29 +0000143 }
144
145 if (strcmp(loginfo->prefix, "") != 0)
Jan Engelhardt73866352010-12-18 02:04:59 +0100146 printf(" prefix \"%s\"", loginfo->prefix);
Marc Bouchere6869a82000-03-20 06:03:29 +0000147}
148
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000149static void LOG_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000150{
151 const struct ipt_log_info *loginfo
152 = (const struct ipt_log_info *)target->data;
153
Max Kellermanna5d09942008-01-29 13:44:34 +0000154 if (strcmp(loginfo->prefix, "") != 0) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100155 printf(" --log-prefix");
Jan Engelhardta0baae82009-01-30 04:32:50 +0100156 xtables_save_string(loginfo->prefix);
Max Kellermanna5d09942008-01-29 13:44:34 +0000157 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000158
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000159 if (loginfo->level != LOG_DEFAULT_LEVEL)
Jan Engelhardt73866352010-12-18 02:04:59 +0100160 printf(" --log-level %d", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000161
162 if (loginfo->logflags & IPT_LOG_TCPSEQ)
Jan Engelhardt73866352010-12-18 02:04:59 +0100163 printf(" --log-tcp-sequence");
Marc Bouchere6869a82000-03-20 06:03:29 +0000164 if (loginfo->logflags & IPT_LOG_TCPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100165 printf(" --log-tcp-options");
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 if (loginfo->logflags & IPT_LOG_IPOPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100167 printf(" --log-ip-options");
John Langef46e1af2005-01-02 23:33:12 +0000168 if (loginfo->logflags & IPT_LOG_UID)
Jan Engelhardt73866352010-12-18 02:04:59 +0100169 printf(" --log-uid");
Patrick McHardy12764782010-06-28 14:51:35 +0200170 if (loginfo->logflags & IPT_LOG_MACDECODE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100171 printf(" --log-macdecode");
Marc Bouchere6869a82000-03-20 06:03:29 +0000172}
173
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530174static const struct ipt_log_xlate ipt_log_xlate_names[] = {
175 {"alert", LOG_ALERT },
176 {"crit", LOG_CRIT },
177 {"debug", LOG_DEBUG },
178 {"emerg", LOG_EMERG },
179 {"err", LOG_ERR },
180 {"info", LOG_INFO },
181 {"notice", LOG_NOTICE },
182 {"warn", LOG_WARNING }
183};
184
Pablo Neira Ayuso7a0992d2016-07-24 12:45:53 +0200185static int LOG_xlate(struct xt_xlate *xl,
186 const struct xt_xlate_tg_params *params)
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530187{
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530188 const struct ipt_log_info *loginfo =
Pablo Neira Ayuso7a0992d2016-07-24 12:45:53 +0200189 (const struct ipt_log_info *)params->target->data;
190 unsigned int i = 0;
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530191
Liping Zhangbb509422016-11-27 20:08:29 +0800192 xt_xlate_add(xl, "log");
Pablo M. Bermudo Garay68c57e82016-07-26 18:45:24 +0200193 if (strcmp(loginfo->prefix, "") != 0) {
194 if (params->escape_quotes)
Liping Zhangbb509422016-11-27 20:08:29 +0800195 xt_xlate_add(xl, " prefix \\\"%s\\\"", loginfo->prefix);
Pablo M. Bermudo Garay68c57e82016-07-26 18:45:24 +0200196 else
Liping Zhangbb509422016-11-27 20:08:29 +0800197 xt_xlate_add(xl, " prefix \"%s\"", loginfo->prefix);
Pablo M. Bermudo Garay68c57e82016-07-26 18:45:24 +0200198 }
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530199
200 for (i = 0; i < ARRAY_SIZE(ipt_log_xlate_names); ++i)
Laura Garcia Liebana75a7f702016-03-10 19:15:40 +0100201 if (loginfo->level != LOG_DEFAULT_LEVEL &&
202 loginfo->level == ipt_log_xlate_names[i].level) {
Liping Zhangbb509422016-11-27 20:08:29 +0800203 xt_xlate_add(xl, " level %s",
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530204 ipt_log_xlate_names[i].name);
205 break;
206 }
207
Liping Zhangbb509422016-11-27 20:08:29 +0800208 if ((loginfo->logflags & IPT_LOG_MASK) == IPT_LOG_MASK) {
209 xt_xlate_add(xl, " flags all");
210 } else {
211 if (loginfo->logflags & (IPT_LOG_TCPSEQ | IPT_LOG_TCPOPT)) {
212 const char *delim = " ";
213
214 xt_xlate_add(xl, " flags tcp");
215 if (loginfo->logflags & IPT_LOG_TCPSEQ) {
216 xt_xlate_add(xl, " sequence");
217 delim = ",";
218 }
219 if (loginfo->logflags & IPT_LOG_TCPOPT)
220 xt_xlate_add(xl, "%soptions", delim);
221 }
222 if (loginfo->logflags & IPT_LOG_IPOPT)
223 xt_xlate_add(xl, " flags ip options");
224 if (loginfo->logflags & IPT_LOG_UID)
225 xt_xlate_add(xl, " flags skuid");
226 if (loginfo->logflags & IPT_LOG_MACDECODE)
227 xt_xlate_add(xl, " flags ether");
228 }
229
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530230 return 1;
231}
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200232static struct xtables_target log_tg_reg = {
Jan Engelhardte1df2212011-02-15 12:02:51 +0100233 .name = "LOG",
234 .version = XTABLES_VERSION,
235 .family = NFPROTO_IPV4,
236 .size = XT_ALIGN(sizeof(struct ipt_log_info)),
237 .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
238 .help = LOG_help,
239 .init = LOG_init,
Jan Engelhardte1df2212011-02-15 12:02:51 +0100240 .print = LOG_print,
241 .save = LOG_save,
Jan Engelhardt0dd344a2011-02-15 12:05:12 +0100242 .x6_parse = LOG_parse,
243 .x6_options = LOG_opts,
Shivani Bhardwaja3ea8922016-01-05 23:32:29 +0530244 .xlate = LOG_xlate,
Marc Bouchere6869a82000-03-20 06:03:29 +0000245};
246
247void _init(void)
248{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200249 xtables_register_target(&log_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000250}