blob: 6b08f586e5c8c616a8d8845a70d1f136bb0d98fa [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>
8#include <iptables.h>
9#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
Marc Bouchere6869a82000-03-20 06:03:29 +000020/* Function which prints out usage message. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000021static void LOG_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000022{
23 printf(
24"LOG v%s 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"
John Langef46e1af2005-01-02 23:33:12 +000029" --log-ip-options Log IP options.\n\n"
30" --log-uid Log UID owning the local socket.\n\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000031IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000032}
33
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000034static const struct option LOG_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000035 { .name = "log-level", .has_arg = 1, .val = '!' },
36 { .name = "log-prefix", .has_arg = 1, .val = '#' },
37 { .name = "log-tcp-sequence", .has_arg = 0, .val = '1' },
38 { .name = "log-tcp-options", .has_arg = 0, .val = '2' },
39 { .name = "log-ip-options", .has_arg = 0, .val = '3' },
40 { .name = "log-uid", .has_arg = 0, .val = '4' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000041 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000042};
43
44/* Initialize the target. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000045static void LOG_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000046{
47 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
48
49 loginfo->level = LOG_DEFAULT_LEVEL;
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051}
52
53struct ipt_log_names {
54 const char *name;
55 unsigned int level;
56};
57
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000058static const struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000059= { { .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 }
Marc Bouchere6869a82000-03-20 06:03:29 +000068};
69
70static u_int8_t
71parse_level(const char *level)
72{
Marc Boucher459357f2001-09-08 02:16:51 +000073 unsigned int lev = -1;
Harald Welte3e44c502001-10-22 08:16:24 +000074 unsigned int set = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000075
Harald Welteb4719762001-07-23 02:14:22 +000076 if (string_to_number(level, 0, 7, &lev) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +000077 unsigned int i = 0;
78
79 for (i = 0;
80 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
81 i++) {
82 if (strncasecmp(level, ipt_log_names[i].name,
83 strlen(level)) == 0) {
Harald Welte3e44c502001-10-22 08:16:24 +000084 if (set++)
Marc Bouchere6869a82000-03-20 06:03:29 +000085 exit_error(PARAMETER_PROBLEM,
86 "log-level `%s' ambiguous",
87 level);
88 lev = ipt_log_names[i].level;
89 }
90 }
91
Harald Welte3e44c502001-10-22 08:16:24 +000092 if (!set)
Marc Bouchere6869a82000-03-20 06:03:29 +000093 exit_error(PARAMETER_PROBLEM,
94 "log-level `%s' unknown", level);
95 }
96
97 return (u_int8_t)lev;
98}
99
100#define IPT_LOG_OPT_LEVEL 0x01
101#define IPT_LOG_OPT_PREFIX 0x02
102#define IPT_LOG_OPT_TCPSEQ 0x04
103#define IPT_LOG_OPT_TCPOPT 0x08
104#define IPT_LOG_OPT_IPOPT 0x10
John Langef46e1af2005-01-02 23:33:12 +0000105#define IPT_LOG_OPT_UID 0x20
Marc Bouchere6869a82000-03-20 06:03:29 +0000106
107/* Function which parses command options; returns true if it
108 ate an option */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000109static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
110 const void *entry, struct xt_entry_target **target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000111{
112 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
113
114 switch (c) {
115 case '!':
116 if (*flags & IPT_LOG_OPT_LEVEL)
117 exit_error(PARAMETER_PROBLEM,
118 "Can't specify --log-level twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000119
Harald Welteb77f1da2002-03-14 11:35:58 +0000120 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 exit_error(PARAMETER_PROBLEM,
122 "Unexpected `!' after --log-level");
123
124 loginfo->level = parse_level(optarg);
125 *flags |= IPT_LOG_OPT_LEVEL;
126 break;
127
128 case '#':
129 if (*flags & IPT_LOG_OPT_PREFIX)
130 exit_error(PARAMETER_PROBLEM,
131 "Can't specify --log-prefix twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000132
Harald Welteb77f1da2002-03-14 11:35:58 +0000133 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000134 exit_error(PARAMETER_PROBLEM,
135 "Unexpected `!' after --log-prefix");
136
137 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
138 exit_error(PARAMETER_PROBLEM,
139 "Maximum prefix length %u for --log-prefix",
Martin Josefssona28d4952004-05-26 16:04:48 +0000140 (unsigned int)sizeof(loginfo->prefix) - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000141
Mike Frysingered26b7b2006-10-10 06:18:40 +0000142 if (strlen(optarg) == 0)
143 exit_error(PARAMETER_PROBLEM,
144 "No prefix specified for --log-prefix");
145
Phil Oester182f3f62005-04-01 07:07:00 +0000146 if (strlen(optarg) != strlen(strtok(optarg, "\n")))
147 exit_error(PARAMETER_PROBLEM,
148 "Newlines not allowed in --log-prefix");
149
Marc Bouchere6869a82000-03-20 06:03:29 +0000150 strcpy(loginfo->prefix, optarg);
151 *flags |= IPT_LOG_OPT_PREFIX;
152 break;
153
154 case '1':
155 if (*flags & IPT_LOG_OPT_TCPSEQ)
156 exit_error(PARAMETER_PROBLEM,
157 "Can't specify --log-tcp-sequence "
158 "twice");
159
160 loginfo->logflags |= IPT_LOG_TCPSEQ;
161 *flags |= IPT_LOG_OPT_TCPSEQ;
162 break;
163
164 case '2':
165 if (*flags & IPT_LOG_OPT_TCPOPT)
166 exit_error(PARAMETER_PROBLEM,
167 "Can't specify --log-tcp-options twice");
168
169 loginfo->logflags |= IPT_LOG_TCPOPT;
170 *flags |= IPT_LOG_OPT_TCPOPT;
171 break;
172
173 case '3':
174 if (*flags & IPT_LOG_OPT_IPOPT)
175 exit_error(PARAMETER_PROBLEM,
176 "Can't specify --log-ip-options twice");
177
178 loginfo->logflags |= IPT_LOG_IPOPT;
179 *flags |= IPT_LOG_OPT_IPOPT;
180 break;
181
John Langef46e1af2005-01-02 23:33:12 +0000182 case '4':
183 if (*flags & IPT_LOG_OPT_UID)
184 exit_error(PARAMETER_PROBLEM,
185 "Can't specify --log-uid twice");
186
187 loginfo->logflags |= IPT_LOG_UID;
188 *flags |= IPT_LOG_OPT_UID;
189 break;
190
Marc Bouchere6869a82000-03-20 06:03:29 +0000191 default:
192 return 0;
193 }
194
195 return 1;
196}
197
Marc Bouchere6869a82000-03-20 06:03:29 +0000198/* Prints out the targinfo. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000199static void LOG_print(const void *ip, const struct xt_entry_target *target,
200 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000201{
202 const struct ipt_log_info *loginfo
203 = (const struct ipt_log_info *)target->data;
204 unsigned int i = 0;
205
206 printf("LOG ");
207 if (numeric)
208 printf("flags %u level %u ",
209 loginfo->logflags, loginfo->level);
210 else {
211 for (i = 0;
212 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
213 i++) {
214 if (loginfo->level == ipt_log_names[i].level) {
215 printf("level %s ", ipt_log_names[i].name);
216 break;
217 }
218 }
219 if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
220 printf("UNKNOWN level %u ", loginfo->level);
221 if (loginfo->logflags & IPT_LOG_TCPSEQ)
222 printf("tcp-sequence ");
223 if (loginfo->logflags & IPT_LOG_TCPOPT)
224 printf("tcp-options ");
225 if (loginfo->logflags & IPT_LOG_IPOPT)
226 printf("ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000227 if (loginfo->logflags & IPT_LOG_UID)
228 printf("uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000229 if (loginfo->logflags & ~(IPT_LOG_MASK))
230 printf("unknown-flags ");
231 }
232
233 if (strcmp(loginfo->prefix, "") != 0)
234 printf("prefix `%s' ", loginfo->prefix);
235}
236
237/* Saves the union ipt_targinfo in parsable form to stdout. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000238static void LOG_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000239{
240 const struct ipt_log_info *loginfo
241 = (const struct ipt_log_info *)target->data;
242
Max Kellermanna5d09942008-01-29 13:44:34 +0000243 if (strcmp(loginfo->prefix, "") != 0) {
244 printf("--log-prefix ");
245 save_string(loginfo->prefix);
246 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000247
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000248 if (loginfo->level != LOG_DEFAULT_LEVEL)
249 printf("--log-level %d ", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000250
251 if (loginfo->logflags & IPT_LOG_TCPSEQ)
252 printf("--log-tcp-sequence ");
253 if (loginfo->logflags & IPT_LOG_TCPOPT)
254 printf("--log-tcp-options ");
255 if (loginfo->logflags & IPT_LOG_IPOPT)
256 printf("--log-ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000257 if (loginfo->logflags & IPT_LOG_UID)
258 printf("--log-uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000259}
260
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000261static struct iptables_target log_target = {
Stephane Ouellette2be28ab2003-08-11 19:58:56 +0000262 .name = "LOG",
263 .version = IPTABLES_VERSION,
264 .size = IPT_ALIGN(sizeof(struct ipt_log_info)),
265 .userspacesize = IPT_ALIGN(sizeof(struct ipt_log_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000266 .help = LOG_help,
267 .init = LOG_init,
268 .parse = LOG_parse,
269 .print = LOG_print,
270 .save = LOG_save,
271 .extra_opts = LOG_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000272};
273
274void _init(void)
275{
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000276 register_target(&log_target);
Marc Bouchere6869a82000-03-20 06:03:29 +0000277}