blob: 0f1cf9e97b46f915207e6bbe5cd37eeacf01b148 [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
14/* Function which prints out usage message. */
15static void
16help(void)
17{
18 printf(
19"LOG v%s options:\n"
20" --log-level level Level of logging (numeric or see syslog.conf)\n"
21" --log-prefix prefix Prefix log messages with this prefix.\n\n"
22" --log-tcp-sequence Log TCP sequence numbers.\n\n"
23" --log-tcp-options Log TCP options.\n\n"
John Langef46e1af2005-01-02 23:33:12 +000024" --log-ip-options Log IP options.\n\n"
25" --log-uid Log UID owning the local socket.\n\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000026IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000027}
28
29static struct option opts[] = {
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000030 { .name = "log-level", .has_arg = 1, .flag = 0, .val = '!' },
31 { .name = "log-prefix", .has_arg = 1, .flag = 0, .val = '#' },
32 { .name = "log-tcp-sequence", .has_arg = 0, .flag = 0, .val = '1' },
33 { .name = "log-tcp-options", .has_arg = 0, .flag = 0, .val = '2' },
34 { .name = "log-ip-options", .has_arg = 0, .flag = 0, .val = '3' },
John Langef46e1af2005-01-02 23:33:12 +000035 { .name = "log-uid", .has_arg = 0, .flag = 0, .val = '4' },
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000036 { .name = 0 }
Marc Bouchere6869a82000-03-20 06:03:29 +000037};
38
39/* Initialize the target. */
40static void
41init(struct ipt_entry_target *t, unsigned int *nfcache)
42{
43 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
44
45 loginfo->level = LOG_DEFAULT_LEVEL;
46
47 /* Can't cache this */
48 *nfcache |= NFC_UNKNOWN;
49}
50
51struct ipt_log_names {
52 const char *name;
53 unsigned int level;
54};
55
56static struct ipt_log_names ipt_log_names[]
Stephane Ouellette2be28ab2003-08-11 19:58:56 +000057= { { .name = "alert", .level = LOG_ALERT },
58 { .name = "crit", .level = LOG_CRIT },
59 { .name = "debug", .level = LOG_DEBUG },
60 { .name = "emerg", .level = LOG_EMERG },
61 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
62 { .name = "info", .level = LOG_INFO },
63 { .name = "notice", .level = LOG_NOTICE },
64 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
65 { .name = "warning", .level = LOG_WARNING }
Marc Bouchere6869a82000-03-20 06:03:29 +000066};
67
68static u_int8_t
69parse_level(const char *level)
70{
Marc Boucher459357f2001-09-08 02:16:51 +000071 unsigned int lev = -1;
Harald Welte3e44c502001-10-22 08:16:24 +000072 unsigned int set = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000073
Harald Welteb4719762001-07-23 02:14:22 +000074 if (string_to_number(level, 0, 7, &lev) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +000075 unsigned int i = 0;
76
77 for (i = 0;
78 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
79 i++) {
80 if (strncasecmp(level, ipt_log_names[i].name,
81 strlen(level)) == 0) {
Harald Welte3e44c502001-10-22 08:16:24 +000082 if (set++)
Marc Bouchere6869a82000-03-20 06:03:29 +000083 exit_error(PARAMETER_PROBLEM,
84 "log-level `%s' ambiguous",
85 level);
86 lev = ipt_log_names[i].level;
87 }
88 }
89
Harald Welte3e44c502001-10-22 08:16:24 +000090 if (!set)
Marc Bouchere6869a82000-03-20 06:03:29 +000091 exit_error(PARAMETER_PROBLEM,
92 "log-level `%s' unknown", level);
93 }
94
95 return (u_int8_t)lev;
96}
97
98#define IPT_LOG_OPT_LEVEL 0x01
99#define IPT_LOG_OPT_PREFIX 0x02
100#define IPT_LOG_OPT_TCPSEQ 0x04
101#define IPT_LOG_OPT_TCPOPT 0x08
102#define IPT_LOG_OPT_IPOPT 0x10
John Langef46e1af2005-01-02 23:33:12 +0000103#define IPT_LOG_OPT_UID 0x20
Marc Bouchere6869a82000-03-20 06:03:29 +0000104
105/* Function which parses command options; returns true if it
106 ate an option */
107static int
108parse(int c, char **argv, int invert, unsigned int *flags,
109 const struct ipt_entry *entry,
110 struct ipt_entry_target **target)
111{
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
142 strcpy(loginfo->prefix, optarg);
143 *flags |= IPT_LOG_OPT_PREFIX;
144 break;
145
146 case '1':
147 if (*flags & IPT_LOG_OPT_TCPSEQ)
148 exit_error(PARAMETER_PROBLEM,
149 "Can't specify --log-tcp-sequence "
150 "twice");
151
152 loginfo->logflags |= IPT_LOG_TCPSEQ;
153 *flags |= IPT_LOG_OPT_TCPSEQ;
154 break;
155
156 case '2':
157 if (*flags & IPT_LOG_OPT_TCPOPT)
158 exit_error(PARAMETER_PROBLEM,
159 "Can't specify --log-tcp-options twice");
160
161 loginfo->logflags |= IPT_LOG_TCPOPT;
162 *flags |= IPT_LOG_OPT_TCPOPT;
163 break;
164
165 case '3':
166 if (*flags & IPT_LOG_OPT_IPOPT)
167 exit_error(PARAMETER_PROBLEM,
168 "Can't specify --log-ip-options twice");
169
170 loginfo->logflags |= IPT_LOG_IPOPT;
171 *flags |= IPT_LOG_OPT_IPOPT;
172 break;
173
John Langef46e1af2005-01-02 23:33:12 +0000174 case '4':
175 if (*flags & IPT_LOG_OPT_UID)
176 exit_error(PARAMETER_PROBLEM,
177 "Can't specify --log-uid twice");
178
179 loginfo->logflags |= IPT_LOG_UID;
180 *flags |= IPT_LOG_OPT_UID;
181 break;
182
Marc Bouchere6869a82000-03-20 06:03:29 +0000183 default:
184 return 0;
185 }
186
187 return 1;
188}
189
190/* Final check; nothing. */
191static void final_check(unsigned int flags)
192{
193}
194
195/* Prints out the targinfo. */
196static void
197print(const struct ipt_ip *ip,
198 const struct ipt_entry_target *target,
199 int numeric)
200{
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. */
237static void
238save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
239{
240 const struct ipt_log_info *loginfo
241 = (const struct ipt_log_info *)target->data;
242
243 if (strcmp(loginfo->prefix, "") != 0)
Harald Welteeea8a932001-01-24 01:52:46 +0000244 printf("--log-prefix \"%s\" ", loginfo->prefix);
Marc Bouchere6869a82000-03-20 06:03:29 +0000245
Thomas Woerner01cbaa62003-07-14 20:01:29 +0000246 if (loginfo->level != LOG_DEFAULT_LEVEL)
247 printf("--log-level %d ", loginfo->level);
Marc Bouchere6869a82000-03-20 06:03:29 +0000248
249 if (loginfo->logflags & IPT_LOG_TCPSEQ)
250 printf("--log-tcp-sequence ");
251 if (loginfo->logflags & IPT_LOG_TCPOPT)
252 printf("--log-tcp-options ");
253 if (loginfo->logflags & IPT_LOG_IPOPT)
254 printf("--log-ip-options ");
John Langef46e1af2005-01-02 23:33:12 +0000255 if (loginfo->logflags & IPT_LOG_UID)
256 printf("--log-uid ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000257}
258
Harald Welte3efb6ea2001-08-06 18:50:21 +0000259static
Marc Bouchere6869a82000-03-20 06:03:29 +0000260struct iptables_target log
Stephane Ouellette2be28ab2003-08-11 19:58:56 +0000261= {
262 .name = "LOG",
263 .version = IPTABLES_VERSION,
264 .size = IPT_ALIGN(sizeof(struct ipt_log_info)),
265 .userspacesize = IPT_ALIGN(sizeof(struct ipt_log_info)),
266 .help = &help,
267 .init = &init,
268 .parse = &parse,
269 .final_check = &final_check,
270 .print = &print,
271 .save = &save,
272 .extra_opts = opts
Marc Bouchere6869a82000-03-20 06:03:29 +0000273};
274
275void _init(void)
276{
277 register_target(&log);
278}