libip6t_LOG.c revision 5d9678ad3eabc34ac40dfe055d7f6a8e44445a5a
1/* Shared library add-on to ip6tables 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_ipv6/ip6_tables.h>
10#include <linux/netfilter_ipv6/ip6t_LOG.h>
11
12#ifndef IP6T_LOG_UID	/* Old kernel */
13#define IP6T_LOG_UID	0x08
14#undef  IP6T_LOG_MASK
15#define IP6T_LOG_MASK	0x0f
16#endif
17
18#define LOG_DEFAULT_LEVEL LOG_WARNING
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"
26" --log-tcp-sequence		Log TCP sequence numbers.\n"
27" --log-tcp-options		Log TCP options.\n"
28" --log-ip-options		Log IP options.\n"
29" --log-uid			Log UID owning the local socket.\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 ip6t_log_info *loginfo = (struct ip6t_log_info *)t->data;
45
46	loginfo->level = LOG_DEFAULT_LEVEL;
47
48}
49
50struct ip6t_log_names {
51	const char *name;
52	unsigned int level;
53};
54
55static const struct ip6t_log_names ip6t_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 (string_to_number(level, 0, 7, &lev) == -1) {
74		unsigned int i = 0;
75
76		for (i = 0;
77		     i < sizeof(ip6t_log_names) / sizeof(struct ip6t_log_names);
78		     i++) {
79			if (strncasecmp(level, ip6t_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 = ip6t_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 (u_int8_t)lev;
95}
96
97#define IP6T_LOG_OPT_LEVEL 0x01
98#define IP6T_LOG_OPT_PREFIX 0x02
99#define IP6T_LOG_OPT_TCPSEQ 0x04
100#define IP6T_LOG_OPT_TCPOPT 0x08
101#define IP6T_LOG_OPT_IPOPT 0x10
102#define IP6T_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 ip6t_log_info *loginfo = (struct ip6t_log_info *)(*target)->data;
108
109	switch (c) {
110	case '!':
111		if (*flags & IP6T_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 |= IP6T_LOG_OPT_LEVEL;
121		break;
122
123	case '#':
124		if (*flags & IP6T_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 |= IP6T_LOG_OPT_PREFIX;
147		break;
148
149	case '1':
150		if (*flags & IP6T_LOG_OPT_TCPSEQ)
151			exit_error(PARAMETER_PROBLEM,
152				   "Can't specify --log-tcp-sequence "
153				   "twice");
154
155		loginfo->logflags |= IP6T_LOG_TCPSEQ;
156		*flags |= IP6T_LOG_OPT_TCPSEQ;
157		break;
158
159	case '2':
160		if (*flags & IP6T_LOG_OPT_TCPOPT)
161			exit_error(PARAMETER_PROBLEM,
162				   "Can't specify --log-tcp-options twice");
163
164		loginfo->logflags |= IP6T_LOG_TCPOPT;
165		*flags |= IP6T_LOG_OPT_TCPOPT;
166		break;
167
168	case '3':
169		if (*flags & IP6T_LOG_OPT_IPOPT)
170			exit_error(PARAMETER_PROBLEM,
171				   "Can't specify --log-ip-options twice");
172
173		loginfo->logflags |= IP6T_LOG_IPOPT;
174		*flags |= IP6T_LOG_OPT_IPOPT;
175		break;
176
177	case '4':
178		if (*flags & IP6T_LOG_OPT_UID)
179			exit_error(PARAMETER_PROBLEM,
180				   "Can't specify --log-uid twice");
181
182		loginfo->logflags |= IP6T_LOG_UID;
183		*flags |= IP6T_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 ip6t_log_info *loginfo
197		= (const struct ip6t_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(ip6t_log_names) / sizeof(struct ip6t_log_names);
207		     i++) {
208			if (loginfo->level == ip6t_log_names[i].level) {
209				printf("level %s ", ip6t_log_names[i].name);
210				break;
211			}
212		}
213		if (i == sizeof(ip6t_log_names) / sizeof(struct ip6t_log_names))
214			printf("UNKNOWN level %u ", loginfo->level);
215		if (loginfo->logflags & IP6T_LOG_TCPSEQ)
216			printf("tcp-sequence ");
217		if (loginfo->logflags & IP6T_LOG_TCPOPT)
218			printf("tcp-options ");
219		if (loginfo->logflags & IP6T_LOG_IPOPT)
220			printf("ip-options ");
221		if (loginfo->logflags & IP6T_LOG_UID)
222			printf("uid ");
223		if (loginfo->logflags & ~(IP6T_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 ip6t_log_info *loginfo
234		= (const struct ip6t_log_info *)target->data;
235
236	if (strcmp(loginfo->prefix, "") != 0)
237		printf("--log-prefix \"%s\" ", loginfo->prefix);
238
239	if (loginfo->level != LOG_DEFAULT_LEVEL)
240		printf("--log-level %d ", loginfo->level);
241
242	if (loginfo->logflags & IP6T_LOG_TCPSEQ)
243		printf("--log-tcp-sequence ");
244	if (loginfo->logflags & IP6T_LOG_TCPOPT)
245		printf("--log-tcp-options ");
246	if (loginfo->logflags & IP6T_LOG_IPOPT)
247		printf("--log-ip-options ");
248	if (loginfo->logflags & IP6T_LOG_UID)
249		printf("--log-uid ");
250}
251
252static struct xtables_target log_tg6_reg = {
253    .name          = "LOG",
254    .version       = XTABLES_VERSION,
255    .family        = NFPROTO_IPV6,
256    .size          = XT_ALIGN(sizeof(struct ip6t_log_info)),
257    .userspacesize = XT_ALIGN(sizeof(struct ip6t_log_info)),
258    .help          = LOG_help,
259    .init          = LOG_init,
260    .parse         = LOG_parse,
261    .print         = LOG_print,
262    .save          = LOG_save,
263    .extra_opts    = LOG_opts,
264};
265
266void _init(void)
267{
268	xtables_register_target(&log_tg6_reg);
269}
270