blob: 39d6fb0b95bceae3fffa2488180e157699edac28 [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. */
21static void
22help(void)
23{
24 printf(
25"LOG v%s options:\n"
26" --log-level level Level of logging (numeric or see syslog.conf)\n"
27" --log-prefix prefix Prefix log messages with this prefix.\n\n"
28" --log-tcp-sequence Log TCP sequence numbers.\n\n"
29" --log-tcp-options Log TCP options.\n\n"
John Langef46e1af2005-01-02 23:33:12 +000030" --log-ip-options Log IP options.\n\n"
31" --log-uid Log UID owning the local socket.\n\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000032IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000033}
34
35static struct option opts[] = {
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000036 { .name = "log-level", .has_arg = 1, .flag = 0, .val = '!' },
37 { .name = "log-prefix", .has_arg = 1, .flag = 0, .val = '#' },
38 { .name = "log-tcp-sequence", .has_arg = 0, .flag = 0, .val = '1' },
39 { .name = "log-tcp-options", .has_arg = 0, .flag = 0, .val = '2' },
40 { .name = "log-ip-options", .has_arg = 0, .flag = 0, .val = '3' },
John Langef46e1af2005-01-02 23:33:12 +000041 { .name = "log-uid", .has_arg = 0, .flag = 0, .val = '4' },
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000042 { .name = 0 }
Marc Bouchere6869a82000-03-20 06:03:29 +000043};
44
45/* Initialize the target. */
46static void
47init(struct ipt_entry_target *t, unsigned int *nfcache)
48{
49 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
50
51 loginfo->level = LOG_DEFAULT_LEVEL;
52
53 /* Can't cache this */
54 *nfcache |= NFC_UNKNOWN;
55}
56
57struct ipt_log_names {
58 const char *name;
59 unsigned int level;
60};
61
62static struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000063= { { .name = "alert", .level = LOG_ALERT },
64 { .name = "crit", .level = LOG_CRIT },
65 { .name = "debug", .level = LOG_DEBUG },
66 { .name = "emerg", .level = LOG_EMERG },
67 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
68 { .name = "info", .level = LOG_INFO },
69 { .name = "notice", .level = LOG_NOTICE },
70 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
71 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000072};
73
74static u_int8_t
75parse_level(const char *level)
76{
Marc Boucher459357f2001-09-08 02:16:51 +000077 unsigned int lev = -1;
Harald Welte3e44c502001-10-22 08:16:24 +000078 unsigned int set = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000079
Harald Welteb4719762001-07-23 02:14:22 +000080 if (string_to_number(level, 0, 7, &lev) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +000081 unsigned int i = 0;
82
83 for (i = 0;
84 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
85 i++) {
86 if (strncasecmp(level, ipt_log_names[i].name,
87 strlen(level)) == 0) {
Harald Welte3e44c502001-10-22 08:16:24 +000088 if (set++)
Marc Bouchere6869a82000-03-20 06:03:29 +000089 exit_error(PARAMETER_PROBLEM,
90 "log-level `%s' ambiguous",
91 level);
92 lev = ipt_log_names[i].level;
93 }
94 }
95
Harald Welte3e44c502001-10-22 08:16:24 +000096 if (!set)
Marc Bouchere6869a82000-03-20 06:03:29 +000097 exit_error(PARAMETER_PROBLEM,
98 "log-level `%s' unknown", level);
99 }
100
101 return (u_int8_t)lev;
102}
103
104#define IPT_LOG_OPT_LEVEL 0x01
105#define IPT_LOG_OPT_PREFIX 0x02
106#define IPT_LOG_OPT_TCPSEQ 0x04
107#define IPT_LOG_OPT_TCPOPT 0x08
108#define IPT_LOG_OPT_IPOPT 0x10
John Langef46e1af2005-01-02 23:33:12 +0000109#define IPT_LOG_OPT_UID 0x20
Marc Bouchere6869a82000-03-20 06:03:29 +0000110
111/* Function which parses command options; returns true if it
112 ate an option */
113static int
114parse(int c, char **argv, int invert, unsigned int *flags,
115 const struct ipt_entry *entry,
116 struct ipt_entry_target **target)
117{
118 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
119
120 switch (c) {
121 case '!':
122 if (*flags & IPT_LOG_OPT_LEVEL)
123 exit_error(PARAMETER_PROBLEM,
124 "Can't specify --log-level twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000125
Harald Welteb77f1da2002-03-14 11:35:58 +0000126 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 exit_error(PARAMETER_PROBLEM,
128 "Unexpected `!' after --log-level");
129
130 loginfo->level = parse_level(optarg);
131 *flags |= IPT_LOG_OPT_LEVEL;
132 break;
133
134 case '#':
135 if (*flags & IPT_LOG_OPT_PREFIX)
136 exit_error(PARAMETER_PROBLEM,
137 "Can't specify --log-prefix twice");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000138
Harald Welteb77f1da2002-03-14 11:35:58 +0000139 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000140 exit_error(PARAMETER_PROBLEM,
141 "Unexpected `!' after --log-prefix");
142
143 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
144 exit_error(PARAMETER_PROBLEM,
145 "Maximum prefix length %u for --log-prefix",
Martin Josefssona28d4952004-05-26 16:04:48 +0000146 (unsigned int)sizeof(loginfo->prefix) - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000147
148 strcpy(loginfo->prefix, optarg);
149 *flags |= IPT_LOG_OPT_PREFIX;
150 break;
151
152 case '1':
153 if (*flags & IPT_LOG_OPT_TCPSEQ)
154 exit_error(PARAMETER_PROBLEM,
155 "Can't specify --log-tcp-sequence "
156 "twice");
157
158 loginfo->logflags |= IPT_LOG_TCPSEQ;
159 *flags |= IPT_LOG_OPT_TCPSEQ;
160 break;
161
162 case '2':
163 if (*flags & IPT_LOG_OPT_TCPOPT)
164 exit_error(PARAMETER_PROBLEM,
165 "Can't specify --log-tcp-options twice");
166
167 loginfo->logflags |= IPT_LOG_TCPOPT;
168 *flags |= IPT_LOG_OPT_TCPOPT;
169 break;
170
171 case '3':
172 if (*flags & IPT_LOG_OPT_IPOPT)
173 exit_error(PARAMETER_PROBLEM,
174 "Can't specify --log-ip-options twice");
175
176 loginfo->logflags |= IPT_LOG_IPOPT;
177 *flags |= IPT_LOG_OPT_IPOPT;
178 break;
179
John Langef46e1af2005-01-02 23:33:12 +0000180 case '4':
181 if (*flags & IPT_LOG_OPT_UID)
182 exit_error(PARAMETER_PROBLEM,
183 "Can't specify --log-uid twice");
184
185 loginfo->logflags |= IPT_LOG_UID;
186 *flags |= IPT_LOG_OPT_UID;
187 break;
188
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 default:
190 return 0;
191 }
192
193 return 1;
194}
195
196/* Final check; nothing. */
197static void final_check(unsigned int flags)
198{
199}
200
201/* Prints out the targinfo. */
202static void
203print(const struct ipt_ip *ip,
204 const struct ipt_entry_target *target,
205 int numeric)
206{
207 const struct ipt_log_info *loginfo
208 = (const struct ipt_log_info *)target->data;
209 unsigned int i = 0;
210
211 printf("LOG ");
212 if (numeric)
213 printf("flags %u level %u ",
214 loginfo->logflags, loginfo->level);
215 else {
216 for (i = 0;
217 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
218 i++) {
219 if (loginfo->level == ipt_log_names[i].level) {
220 printf("level %s ", ipt_log_names[i].name);
221 break;
222 }
223 }
224 if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
225 printf("UNKNOWN level %u ", loginfo->level);
226 if (loginfo->logflags & IPT_LOG_TCPSEQ)
227 printf("tcp-sequence ");
228 if (loginfo->logflags & IPT_LOG_TCPOPT)
229 printf("tcp-options ");
230 if (loginfo->logflags & IPT_LOG_IPOPT)
231 printf("ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000232 if (loginfo->logflags & IPT_LOG_UID)
233 printf("uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000234 if (loginfo->logflags & ~(IPT_LOG_MASK))
235 printf("unknown-flags ");
236 }
237
238 if (strcmp(loginfo->prefix, "") != 0)
239 printf("prefix `%s' ", loginfo->prefix);
240}
241
242/* Saves the union ipt_targinfo in parsable form to stdout. */
243static void
244save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
245{
246 const struct ipt_log_info *loginfo
247 = (const struct ipt_log_info *)target->data;
248
249 if (strcmp(loginfo->prefix, "") != 0)
Harald Welteeea8a932001-01-24 01:52:46 +0000250 printf("--log-prefix \"%s\" ", loginfo->prefix);
Marc Bouchere6869a82000-03-20 06:03:29 +0000251
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000252 if (loginfo->level != LOG_DEFAULT_LEVEL)
253 printf("--log-level %d ", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000254
255 if (loginfo->logflags & IPT_LOG_TCPSEQ)
256 printf("--log-tcp-sequence ");
257 if (loginfo->logflags & IPT_LOG_TCPOPT)
258 printf("--log-tcp-options ");
259 if (loginfo->logflags & IPT_LOG_IPOPT)
260 printf("--log-ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000261 if (loginfo->logflags & IPT_LOG_UID)
262 printf("--log-uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000263}
264
Harald Welte3efb6ea2001-08-06 18:50:21 +0000265static
Marc Bouchere6869a82000-03-20 06:03:29 +0000266struct iptables_target log
Stephane Ouellette2be28ab2003-08-11 19:58:56 +0000267= {
268 .name = "LOG",
269 .version = IPTABLES_VERSION,
270 .size = IPT_ALIGN(sizeof(struct ipt_log_info)),
271 .userspacesize = IPT_ALIGN(sizeof(struct ipt_log_info)),
272 .help = &help,
273 .init = &init,
274 .parse = &parse,
275 .final_check = &final_check,
276 .print = &print,
277 .save = &save,
278 .extra_opts = opts
Marc Bouchere6869a82000-03-20 06:03:29 +0000279};
280
281void _init(void)
282{
283 register_target(&log);
284}