1 /* Shared library add-on to iptables to add LOG support. */
9 #include <linux/netfilter_ipv4/ip_tables.h>
10 #include <linux/netfilter_ipv4/ipt_LOG.h>
12 #define LOG_DEFAULT_LEVEL LOG_WARNING
14 #ifndef IPT_LOG_UID /* Old kernel */
15 #define IPT_LOG_UID 0x08 /* Log UID owning local socket */
17 #define IPT_LOG_MASK 0x0f
20 /* Function which prints out usage message. */
21 static void LOG_help(void)
24 "LOG target options:\n"
25 " --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"
29 " --log-ip-options Log IP options.\n\n"
30 " --log-uid Log UID owning the local socket.\n\n");
33 static const struct option LOG_opts[] = {
34 { .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' },
43 /* Initialize the target. */
44 static void LOG_init(struct xt_entry_target *t)
46 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
48 loginfo->level = LOG_DEFAULT_LEVEL;
52 struct ipt_log_names {
57 static const struct ipt_log_names ipt_log_names[]
58 = { { .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 }
70 parse_level(const char *level)
72 unsigned int lev = -1;
75 if (string_to_number(level, 0, 7, &lev) == -1) {
79 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
81 if (strncasecmp(level, ipt_log_names[i].name,
82 strlen(level)) == 0) {
84 exit_error(PARAMETER_PROBLEM,
85 "log-level `%s' ambiguous",
87 lev = ipt_log_names[i].level;
92 exit_error(PARAMETER_PROBLEM,
93 "log-level `%s' unknown", level);
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
104 #define IPT_LOG_OPT_UID 0x20
106 /* Function which parses command options; returns true if it
108 static int LOG_parse(int c, char **argv, int invert, unsigned int *flags,
109 const void *entry, struct xt_entry_target **target)
111 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
115 if (*flags & IPT_LOG_OPT_LEVEL)
116 exit_error(PARAMETER_PROBLEM,
117 "Can't specify --log-level twice");
119 if (check_inverse(optarg, &invert, NULL, 0))
120 exit_error(PARAMETER_PROBLEM,
121 "Unexpected `!' after --log-level");
123 loginfo->level = parse_level(optarg);
124 *flags |= IPT_LOG_OPT_LEVEL;
128 if (*flags & IPT_LOG_OPT_PREFIX)
129 exit_error(PARAMETER_PROBLEM,
130 "Can't specify --log-prefix twice");
132 if (check_inverse(optarg, &invert, NULL, 0))
133 exit_error(PARAMETER_PROBLEM,
134 "Unexpected `!' after --log-prefix");
136 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
137 exit_error(PARAMETER_PROBLEM,
138 "Maximum prefix length %u for --log-prefix",
139 (unsigned int)sizeof(loginfo->prefix) - 1);
141 if (strlen(optarg) == 0)
142 exit_error(PARAMETER_PROBLEM,
143 "No prefix specified for --log-prefix");
145 if (strlen(optarg) != strlen(strtok(optarg, "\n")))
146 exit_error(PARAMETER_PROBLEM,
147 "Newlines not allowed in --log-prefix");
149 strcpy(loginfo->prefix, optarg);
150 *flags |= IPT_LOG_OPT_PREFIX;
154 if (*flags & IPT_LOG_OPT_TCPSEQ)
155 exit_error(PARAMETER_PROBLEM,
156 "Can't specify --log-tcp-sequence "
159 loginfo->logflags |= IPT_LOG_TCPSEQ;
160 *flags |= IPT_LOG_OPT_TCPSEQ;
164 if (*flags & IPT_LOG_OPT_TCPOPT)
165 exit_error(PARAMETER_PROBLEM,
166 "Can't specify --log-tcp-options twice");
168 loginfo->logflags |= IPT_LOG_TCPOPT;
169 *flags |= IPT_LOG_OPT_TCPOPT;
173 if (*flags & IPT_LOG_OPT_IPOPT)
174 exit_error(PARAMETER_PROBLEM,
175 "Can't specify --log-ip-options twice");
177 loginfo->logflags |= IPT_LOG_IPOPT;
178 *flags |= IPT_LOG_OPT_IPOPT;
182 if (*flags & IPT_LOG_OPT_UID)
183 exit_error(PARAMETER_PROBLEM,
184 "Can't specify --log-uid twice");
186 loginfo->logflags |= IPT_LOG_UID;
187 *flags |= IPT_LOG_OPT_UID;
197 /* Prints out the targinfo. */
198 static void LOG_print(const void *ip, const struct xt_entry_target *target,
201 const struct ipt_log_info *loginfo
202 = (const struct ipt_log_info *)target->data;
207 printf("flags %u level %u ",
208 loginfo->logflags, loginfo->level);
211 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
213 if (loginfo->level == ipt_log_names[i].level) {
214 printf("level %s ", ipt_log_names[i].name);
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 ");
226 if (loginfo->logflags & IPT_LOG_UID)
228 if (loginfo->logflags & ~(IPT_LOG_MASK))
229 printf("unknown-flags ");
232 if (strcmp(loginfo->prefix, "") != 0)
233 printf("prefix `%s' ", loginfo->prefix);
236 /* Saves the union ipt_targinfo in parsable form to stdout. */
237 static void LOG_save(const void *ip, const struct xt_entry_target *target)
239 const struct ipt_log_info *loginfo
240 = (const struct ipt_log_info *)target->data;
242 if (strcmp(loginfo->prefix, "") != 0) {
243 printf("--log-prefix ");
244 save_string(loginfo->prefix);
247 if (loginfo->level != LOG_DEFAULT_LEVEL)
248 printf("--log-level %d ", loginfo->level);
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 ");
256 if (loginfo->logflags & IPT_LOG_UID)
257 printf("--log-uid ");
260 static struct xtables_target log_tg_reg = {
262 .version = XTABLES_VERSION,
264 .size = XT_ALIGN(sizeof(struct ipt_log_info)),
265 .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)),
271 .extra_opts = LOG_opts,
276 xtables_register_target(&log_tg_reg);