blob: 4da412c14908b15ef79298fb55d99e7585754705 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01008#include <xtables.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00009#include <linux/netfilter_ipv4/ip_tables.h>
10#include <linux/netfilter_ipv4/ipt_LOG.h>
11
12#define LOG_DEFAULT_LEVEL LOG_WARNING
13
Martin Josefsson2b9a5772005-01-05 15:21:15 +000014#ifndef IPT_LOG_UID /* Old kernel */
15#define IPT_LOG_UID 0x08 /* Log UID owning local socket */
16#undef IPT_LOG_MASK
17#define IPT_LOG_MASK 0x0f
18#endif
19
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000020static void LOG_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000021{
22 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023"LOG target options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000024" --log-level level Level of logging (numeric or see syslog.conf)\n"
25" --log-prefix prefix Prefix log messages with this prefix.\n\n"
26" --log-tcp-sequence Log TCP sequence numbers.\n\n"
27" --log-tcp-options Log TCP options.\n\n"
John Langef46e1af2005-01-02 23:33:12 +000028" --log-ip-options Log IP options.\n\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020029" --log-uid Log UID owning the local socket.\n\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000030}
31
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000032static const struct option LOG_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000033 { .name = "log-level", .has_arg = 1, .val = '!' },
34 { .name = "log-prefix", .has_arg = 1, .val = '#' },
35 { .name = "log-tcp-sequence", .has_arg = 0, .val = '1' },
36 { .name = "log-tcp-options", .has_arg = 0, .val = '2' },
37 { .name = "log-ip-options", .has_arg = 0, .val = '3' },
38 { .name = "log-uid", .has_arg = 0, .val = '4' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000039 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000040};
41
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000042static void LOG_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000043{
44 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
45
46 loginfo->level = LOG_DEFAULT_LEVEL;
47
Marc Bouchere6869a82000-03-20 06:03:29 +000048}
49
50struct ipt_log_names {
51 const char *name;
52 unsigned int level;
53};
54
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000055static const struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000056= { { .name = "alert", .level = LOG_ALERT },
57 { .name = "crit", .level = LOG_CRIT },
58 { .name = "debug", .level = LOG_DEBUG },
59 { .name = "emerg", .level = LOG_EMERG },
60 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
61 { .name = "info", .level = LOG_INFO },
62 { .name = "notice", .level = LOG_NOTICE },
63 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
64 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000065};
66
67static u_int8_t
68parse_level(const char *level)
69{
Marc Boucher459357f2001-09-08 02:16:51 +000070 unsigned int lev = -1;
Harald Welte3e44c502001-10-22 08:16:24 +000071 unsigned int set = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000072
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010073 if (!xtables_strtoui(level, NULL, &lev, 0, 7)) {
Marc Bouchere6869a82000-03-20 06:03:29 +000074 unsigned int i = 0;
75
76 for (i = 0;
77 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
78 i++) {
79 if (strncasecmp(level, ipt_log_names[i].name,
80 strlen(level)) == 0) {
Harald Welte3e44c502001-10-22 08:16:24 +000081 if (set++)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010082 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000083 "log-level `%s' ambiguous",
84 level);
85 lev = ipt_log_names[i].level;
86 }
87 }
88
Harald Welte3e44c502001-10-22 08:16:24 +000089 if (!set)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010090 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000091 "log-level `%s' unknown", level);
92 }
93
Jan Engelhardt213e1852009-01-27 17:24:34 +010094 return lev;
Marc Bouchere6869a82000-03-20 06:03:29 +000095}
96
97#define IPT_LOG_OPT_LEVEL 0x01
98#define IPT_LOG_OPT_PREFIX 0x02
99#define IPT_LOG_OPT_TCPSEQ 0x04
100#define IPT_LOG_OPT_TCPOPT 0x08
101#define IPT_LOG_OPT_IPOPT 0x10
John Langef46e1af2005-01-02 23:33:12 +0000102#define IPT_LOG_OPT_UID 0x20
Marc Bouchere6869a82000-03-20 06:03:29 +0000103
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000104static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
105 const void *entry, struct xt_entry_target **target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000106{
107 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
108
109 switch (c) {
110 case '!':
111 if (*flags & IPT_LOG_OPT_LEVEL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100112 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000113 "Can't specify --log-level twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000114
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100115 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100116 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 "Unexpected `!' after --log-level");
118
119 loginfo->level = parse_level(optarg);
120 *flags |= IPT_LOG_OPT_LEVEL;
121 break;
122
123 case '#':
124 if (*flags & IPT_LOG_OPT_PREFIX)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100125 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000126 "Can't specify --log-prefix twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000127
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100128 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100129 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 "Unexpected `!' after --log-prefix");
131
132 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100133 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000134 "Maximum prefix length %u for --log-prefix",
Martin Josefssona28d4952004-05-26 16:04:48 +0000135 (unsigned int)sizeof(loginfo->prefix) - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000136
Mike Frysingered26b7b2006-10-10 06:18:40 +0000137 if (strlen(optarg) == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100138 xtables_error(PARAMETER_PROBLEM,
Mike Frysingered26b7b2006-10-10 06:18:40 +0000139 "No prefix specified for --log-prefix");
140
Phil Oester182f3f62005-04-01 07:07:00 +0000141 if (strlen(optarg) != strlen(strtok(optarg, "\n")))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100142 xtables_error(PARAMETER_PROBLEM,
Phil Oester182f3f62005-04-01 07:07:00 +0000143 "Newlines not allowed in --log-prefix");
144
Marc Bouchere6869a82000-03-20 06:03:29 +0000145 strcpy(loginfo->prefix, optarg);
146 *flags |= IPT_LOG_OPT_PREFIX;
147 break;
148
149 case '1':
150 if (*flags & IPT_LOG_OPT_TCPSEQ)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100151 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000152 "Can't specify --log-tcp-sequence "
153 "twice");
154
155 loginfo->logflags |= IPT_LOG_TCPSEQ;
156 *flags |= IPT_LOG_OPT_TCPSEQ;
157 break;
158
159 case '2':
160 if (*flags & IPT_LOG_OPT_TCPOPT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100161 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000162 "Can't specify --log-tcp-options twice");
163
164 loginfo->logflags |= IPT_LOG_TCPOPT;
165 *flags |= IPT_LOG_OPT_TCPOPT;
166 break;
167
168 case '3':
169 if (*flags & IPT_LOG_OPT_IPOPT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100170 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000171 "Can't specify --log-ip-options twice");
172
173 loginfo->logflags |= IPT_LOG_IPOPT;
174 *flags |= IPT_LOG_OPT_IPOPT;
175 break;
176
John Langef46e1af2005-01-02 23:33:12 +0000177 case '4':
178 if (*flags & IPT_LOG_OPT_UID)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100179 xtables_error(PARAMETER_PROBLEM,
John Langef46e1af2005-01-02 23:33:12 +0000180 "Can't specify --log-uid twice");
181
182 loginfo->logflags |= IPT_LOG_UID;
183 *flags |= IPT_LOG_OPT_UID;
184 break;
185
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 default:
187 return 0;
188 }
189
190 return 1;
191}
192
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000193static void LOG_print(const void *ip, const struct xt_entry_target *target,
194 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000195{
196 const struct ipt_log_info *loginfo
197 = (const struct ipt_log_info *)target->data;
198 unsigned int i = 0;
199
200 printf("LOG ");
201 if (numeric)
202 printf("flags %u level %u ",
203 loginfo->logflags, loginfo->level);
204 else {
205 for (i = 0;
206 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
207 i++) {
208 if (loginfo->level == ipt_log_names[i].level) {
209 printf("level %s ", ipt_log_names[i].name);
210 break;
211 }
212 }
213 if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
214 printf("UNKNOWN level %u ", loginfo->level);
215 if (loginfo->logflags & IPT_LOG_TCPSEQ)
216 printf("tcp-sequence ");
217 if (loginfo->logflags & IPT_LOG_TCPOPT)
218 printf("tcp-options ");
219 if (loginfo->logflags & IPT_LOG_IPOPT)
220 printf("ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000221 if (loginfo->logflags & IPT_LOG_UID)
222 printf("uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000223 if (loginfo->logflags & ~(IPT_LOG_MASK))
224 printf("unknown-flags ");
225 }
226
227 if (strcmp(loginfo->prefix, "") != 0)
228 printf("prefix `%s' ", loginfo->prefix);
229}
230
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000231static void LOG_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000232{
233 const struct ipt_log_info *loginfo
234 = (const struct ipt_log_info *)target->data;
235
Max Kellermanna5d09942008-01-29 13:44:34 +0000236 if (strcmp(loginfo->prefix, "") != 0) {
237 printf("--log-prefix ");
Jan Engelhardta0baae82009-01-30 04:32:50 +0100238 xtables_save_string(loginfo->prefix);
Max Kellermanna5d09942008-01-29 13:44:34 +0000239 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000240
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000241 if (loginfo->level != LOG_DEFAULT_LEVEL)
242 printf("--log-level %d ", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000243
244 if (loginfo->logflags & IPT_LOG_TCPSEQ)
245 printf("--log-tcp-sequence ");
246 if (loginfo->logflags & IPT_LOG_TCPOPT)
247 printf("--log-tcp-options ");
248 if (loginfo->logflags & IPT_LOG_IPOPT)
249 printf("--log-ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000250 if (loginfo->logflags & IPT_LOG_UID)
251 printf("--log-uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000252}
253
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200254static struct xtables_target log_tg_reg = {
Stephane Ouellette2be28ab2003-08-11 19:58:56 +0000255 .name = "LOG",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200256 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100257 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200258 .size = XT_ALIGN(sizeof(struct ipt_log_info)),
259 .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000260 .help = LOG_help,
261 .init = LOG_init,
262 .parse = LOG_parse,
263 .print = LOG_print,
264 .save = LOG_save,
265 .extra_opts = LOG_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000266};
267
268void _init(void)
269{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200270 xtables_register_target(&log_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000271}