blob: 50b1130c331b1435d996dcfc7229b6bf0949c8ac [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(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020024"LOG target options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000025" --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"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020030" --log-uid Log UID owning the local socket.\n\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000031}
32
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000033static const struct option LOG_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000034 { .name = "log-level", .has_arg = 1, .val = '!' },
35 { .name = "log-prefix", .has_arg = 1, .val = '#' },
36 { .name = "log-tcp-sequence", .has_arg = 0, .val = '1' },
37 { .name = "log-tcp-options", .has_arg = 0, .val = '2' },
38 { .name = "log-ip-options", .has_arg = 0, .val = '3' },
39 { .name = "log-uid", .has_arg = 0, .val = '4' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000040 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000041};
42
43/* Initialize the target. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000044static void LOG_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000045{
46 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
47
48 loginfo->level = LOG_DEFAULT_LEVEL;
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050}
51
52struct ipt_log_names {
53 const char *name;
54 unsigned int level;
55};
56
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000057static const struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000058= { { .name = "alert", .level = LOG_ALERT },
59 { .name = "crit", .level = LOG_CRIT },
60 { .name = "debug", .level = LOG_DEBUG },
61 { .name = "emerg", .level = LOG_EMERG },
62 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
63 { .name = "info", .level = LOG_INFO },
64 { .name = "notice", .level = LOG_NOTICE },
65 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
66 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
69static u_int8_t
70parse_level(const char *level)
71{
Marc Boucher459357f2001-09-08 02:16:51 +000072 unsigned int lev = -1;
Harald Welte3e44c502001-10-22 08:16:24 +000073 unsigned int set = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000074
Harald Welteb4719762001-07-23 02:14:22 +000075 if (string_to_number(level, 0, 7, &lev) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +000076 unsigned int i = 0;
77
78 for (i = 0;
79 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
80 i++) {
81 if (strncasecmp(level, ipt_log_names[i].name,
82 strlen(level)) == 0) {
Harald Welte3e44c502001-10-22 08:16:24 +000083 if (set++)
Marc Bouchere6869a82000-03-20 06:03:29 +000084 exit_error(PARAMETER_PROBLEM,
85 "log-level `%s' ambiguous",
86 level);
87 lev = ipt_log_names[i].level;
88 }
89 }
90
Harald Welte3e44c502001-10-22 08:16:24 +000091 if (!set)
Marc Bouchere6869a82000-03-20 06:03:29 +000092 exit_error(PARAMETER_PROBLEM,
93 "log-level `%s' unknown", level);
94 }
95
96 return (u_int8_t)lev;
97}
98
99#define IPT_LOG_OPT_LEVEL 0x01
100#define IPT_LOG_OPT_PREFIX 0x02
101#define IPT_LOG_OPT_TCPSEQ 0x04
102#define IPT_LOG_OPT_TCPOPT 0x08
103#define IPT_LOG_OPT_IPOPT 0x10
John Langef46e1af2005-01-02 23:33:12 +0000104#define IPT_LOG_OPT_UID 0x20
Marc Bouchere6869a82000-03-20 06:03:29 +0000105
106/* Function which parses command options; returns true if it
107 ate an option */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000108static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
109 const void *entry, struct xt_entry_target **target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000110{
111 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
112
113 switch (c) {
114 case '!':
115 if (*flags & IPT_LOG_OPT_LEVEL)
116 exit_error(PARAMETER_PROBLEM,
117 "Can't specify --log-level twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000118
Harald Welteb77f1da2002-03-14 11:35:58 +0000119 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 exit_error(PARAMETER_PROBLEM,
121 "Unexpected `!' after --log-level");
122
123 loginfo->level = parse_level(optarg);
124 *flags |= IPT_LOG_OPT_LEVEL;
125 break;
126
127 case '#':
128 if (*flags & IPT_LOG_OPT_PREFIX)
129 exit_error(PARAMETER_PROBLEM,
130 "Can't specify --log-prefix twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000131
Harald Welteb77f1da2002-03-14 11:35:58 +0000132 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 exit_error(PARAMETER_PROBLEM,
134 "Unexpected `!' after --log-prefix");
135
136 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
137 exit_error(PARAMETER_PROBLEM,
138 "Maximum prefix length %u for --log-prefix",
Martin Josefssona28d4952004-05-26 16:04:48 +0000139 (unsigned int)sizeof(loginfo->prefix) - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000140
Mike Frysingered26b7b2006-10-10 06:18:40 +0000141 if (strlen(optarg) == 0)
142 exit_error(PARAMETER_PROBLEM,
143 "No prefix specified for --log-prefix");
144
Phil Oester182f3f62005-04-01 07:07:00 +0000145 if (strlen(optarg) != strlen(strtok(optarg, "\n")))
146 exit_error(PARAMETER_PROBLEM,
147 "Newlines not allowed in --log-prefix");
148
Marc Bouchere6869a82000-03-20 06:03:29 +0000149 strcpy(loginfo->prefix, optarg);
150 *flags |= IPT_LOG_OPT_PREFIX;
151 break;
152
153 case '1':
154 if (*flags & IPT_LOG_OPT_TCPSEQ)
155 exit_error(PARAMETER_PROBLEM,
156 "Can't specify --log-tcp-sequence "
157 "twice");
158
159 loginfo->logflags |= IPT_LOG_TCPSEQ;
160 *flags |= IPT_LOG_OPT_TCPSEQ;
161 break;
162
163 case '2':
164 if (*flags & IPT_LOG_OPT_TCPOPT)
165 exit_error(PARAMETER_PROBLEM,
166 "Can't specify --log-tcp-options twice");
167
168 loginfo->logflags |= IPT_LOG_TCPOPT;
169 *flags |= IPT_LOG_OPT_TCPOPT;
170 break;
171
172 case '3':
173 if (*flags & IPT_LOG_OPT_IPOPT)
174 exit_error(PARAMETER_PROBLEM,
175 "Can't specify --log-ip-options twice");
176
177 loginfo->logflags |= IPT_LOG_IPOPT;
178 *flags |= IPT_LOG_OPT_IPOPT;
179 break;
180
John Langef46e1af2005-01-02 23:33:12 +0000181 case '4':
182 if (*flags & IPT_LOG_OPT_UID)
183 exit_error(PARAMETER_PROBLEM,
184 "Can't specify --log-uid twice");
185
186 loginfo->logflags |= IPT_LOG_UID;
187 *flags |= IPT_LOG_OPT_UID;
188 break;
189
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 default:
191 return 0;
192 }
193
194 return 1;
195}
196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197/* Prints out the targinfo. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000198static void LOG_print(const void *ip, const struct xt_entry_target *target,
199 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000200{
201 const struct ipt_log_info *loginfo
202 = (const struct ipt_log_info *)target->data;
203 unsigned int i = 0;
204
205 printf("LOG ");
206 if (numeric)
207 printf("flags %u level %u ",
208 loginfo->logflags, loginfo->level);
209 else {
210 for (i = 0;
211 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
212 i++) {
213 if (loginfo->level == ipt_log_names[i].level) {
214 printf("level %s ", ipt_log_names[i].name);
215 break;
216 }
217 }
218 if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
219 printf("UNKNOWN level %u ", loginfo->level);
220 if (loginfo->logflags & IPT_LOG_TCPSEQ)
221 printf("tcp-sequence ");
222 if (loginfo->logflags & IPT_LOG_TCPOPT)
223 printf("tcp-options ");
224 if (loginfo->logflags & IPT_LOG_IPOPT)
225 printf("ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000226 if (loginfo->logflags & IPT_LOG_UID)
227 printf("uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 if (loginfo->logflags & ~(IPT_LOG_MASK))
229 printf("unknown-flags ");
230 }
231
232 if (strcmp(loginfo->prefix, "") != 0)
233 printf("prefix `%s' ", loginfo->prefix);
234}
235
236/* Saves the union ipt_targinfo in parsable form to stdout. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000237static void LOG_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000238{
239 const struct ipt_log_info *loginfo
240 = (const struct ipt_log_info *)target->data;
241
Max Kellermanna5d09942008-01-29 13:44:34 +0000242 if (strcmp(loginfo->prefix, "") != 0) {
243 printf("--log-prefix ");
244 save_string(loginfo->prefix);
245 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000246
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000247 if (loginfo->level != LOG_DEFAULT_LEVEL)
248 printf("--log-level %d ", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000249
250 if (loginfo->logflags & IPT_LOG_TCPSEQ)
251 printf("--log-tcp-sequence ");
252 if (loginfo->logflags & IPT_LOG_TCPOPT)
253 printf("--log-tcp-options ");
254 if (loginfo->logflags & IPT_LOG_IPOPT)
255 printf("--log-ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000256 if (loginfo->logflags & IPT_LOG_UID)
257 printf("--log-uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000258}
259
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200260static struct xtables_target log_tg_reg = {
Stephane Ouellette2be28ab2003-08-11 19:58:56 +0000261 .name = "LOG",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200262 .version = XTABLES_VERSION,
263 .family = PF_INET,
264 .size = XT_ALIGN(sizeof(struct ipt_log_info)),
265 .userspacesize = XT_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 Engelhardt8b7c64d2008-04-15 11:48:25 +0200276 xtables_register_target(&log_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000277}