libipt_LOG.c revision 5f2922cfc0bbfbeb878f5c12e9fb3eb602ae5507
1/* 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 <xtables.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#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
20static void LOG_help(void)
21{
22	printf(
23"LOG target options:\n"
24" --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"
28" --log-ip-options		Log IP options.\n\n"
29" --log-uid			Log UID owning the local socket.\n\n");
30}
31
32static const struct option LOG_opts[] = {
33	{ .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' },
39	{ .name = NULL }
40};
41
42static void LOG_init(struct xt_entry_target *t)
43{
44	struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
45
46	loginfo->level = LOG_DEFAULT_LEVEL;
47
48}
49
50struct ipt_log_names {
51	const char *name;
52	unsigned int level;
53};
54
55static const struct ipt_log_names ipt_log_names[]
56= { { .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 }
65};
66
67static u_int8_t
68parse_level(const char *level)
69{
70	unsigned int lev = -1;
71	unsigned int set = 0;
72
73	if (!xtables_strtoui(level, NULL, &lev, 0, 7)) {
74		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) {
81				if (set++)
82					exit_error(PARAMETER_PROBLEM,
83						   "log-level `%s' ambiguous",
84						   level);
85				lev = ipt_log_names[i].level;
86			}
87		}
88
89		if (!set)
90			exit_error(PARAMETER_PROBLEM,
91				   "log-level `%s' unknown", level);
92	}
93
94	return lev;
95}
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
102#define IPT_LOG_OPT_UID 0x20
103
104static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
105                     const void *entry, struct xt_entry_target **target)
106{
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)
112			exit_error(PARAMETER_PROBLEM,
113				   "Can't specify --log-level twice");
114
115		if (check_inverse(optarg, &invert, NULL, 0))
116			exit_error(PARAMETER_PROBLEM,
117				   "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)
125			exit_error(PARAMETER_PROBLEM,
126				   "Can't specify --log-prefix twice");
127
128		if (check_inverse(optarg, &invert, NULL, 0))
129			exit_error(PARAMETER_PROBLEM,
130				   "Unexpected `!' after --log-prefix");
131
132		if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
133			exit_error(PARAMETER_PROBLEM,
134				   "Maximum prefix length %u for --log-prefix",
135				   (unsigned int)sizeof(loginfo->prefix) - 1);
136
137		if (strlen(optarg) == 0)
138			exit_error(PARAMETER_PROBLEM,
139				   "No prefix specified for --log-prefix");
140
141		if (strlen(optarg) != strlen(strtok(optarg, "\n")))
142			exit_error(PARAMETER_PROBLEM,
143				   "Newlines not allowed in --log-prefix");
144
145		strcpy(loginfo->prefix, optarg);
146		*flags |= IPT_LOG_OPT_PREFIX;
147		break;
148
149	case '1':
150		if (*flags & IPT_LOG_OPT_TCPSEQ)
151			exit_error(PARAMETER_PROBLEM,
152				   "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)
161			exit_error(PARAMETER_PROBLEM,
162				   "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)
170			exit_error(PARAMETER_PROBLEM,
171				   "Can't specify --log-ip-options twice");
172
173		loginfo->logflags |= IPT_LOG_IPOPT;
174		*flags |= IPT_LOG_OPT_IPOPT;
175		break;
176
177	case '4':
178		if (*flags & IPT_LOG_OPT_UID)
179			exit_error(PARAMETER_PROBLEM,
180				   "Can't specify --log-uid twice");
181
182		loginfo->logflags |= IPT_LOG_UID;
183		*flags |= IPT_LOG_OPT_UID;
184		break;
185
186	default:
187		return 0;
188	}
189
190	return 1;
191}
192
193static void LOG_print(const void *ip, const struct xt_entry_target *target,
194                      int numeric)
195{
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 ");
221		if (loginfo->logflags & IPT_LOG_UID)
222			printf("uid ");
223		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
231static void LOG_save(const void *ip, const struct xt_entry_target *target)
232{
233	const struct ipt_log_info *loginfo
234		= (const struct ipt_log_info *)target->data;
235
236	if (strcmp(loginfo->prefix, "") != 0) {
237		printf("--log-prefix ");
238		save_string(loginfo->prefix);
239	}
240
241	if (loginfo->level != LOG_DEFAULT_LEVEL)
242		printf("--log-level %d ", loginfo->level);
243
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 ");
250	if (loginfo->logflags & IPT_LOG_UID)
251		printf("--log-uid ");
252}
253
254static struct xtables_target log_tg_reg = {
255    .name          = "LOG",
256    .version       = XTABLES_VERSION,
257    .family        = NFPROTO_IPV4,
258    .size          = XT_ALIGN(sizeof(struct ipt_log_info)),
259    .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
260    .help          = LOG_help,
261    .init          = LOG_init,
262    .parse         = LOG_parse,
263    .print         = LOG_print,
264    .save          = LOG_save,
265    .extra_opts    = LOG_opts,
266};
267
268void _init(void)
269{
270	xtables_register_target(&log_tg_reg);
271}
272