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 /* Function which prints out usage message. */
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"
24 " --log-ip-options Log IP options.\n\n",
28 static struct option opts[] = {
29 { .name = "log-level", .has_arg = 1, .flag = 0, .val = '!' },
30 { .name = "log-prefix", .has_arg = 1, .flag = 0, .val = '#' },
31 { .name = "log-tcp-sequence", .has_arg = 0, .flag = 0, .val = '1' },
32 { .name = "log-tcp-options", .has_arg = 0, .flag = 0, .val = '2' },
33 { .name = "log-ip-options", .has_arg = 0, .flag = 0, .val = '3' },
37 /* Initialize the target. */
39 init(struct ipt_entry_target *t, unsigned int *nfcache)
41 struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data;
43 loginfo->level = LOG_DEFAULT_LEVEL;
45 /* Can't cache this */
46 *nfcache |= NFC_UNKNOWN;
49 struct ipt_log_names {
54 static struct ipt_log_names ipt_log_names[]
55 = { { .name = "alert", .level = LOG_ALERT },
56 { .name = "crit", .level = LOG_CRIT },
57 { .name = "debug", .level = LOG_DEBUG },
58 { .name = "emerg", .level = LOG_EMERG },
59 { .name = "error", .level = LOG_ERR }, /* DEPRECATED */
60 { .name = "info", .level = LOG_INFO },
61 { .name = "notice", .level = LOG_NOTICE },
62 { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */
63 { .name = "warning", .level = LOG_WARNING }
67 parse_level(const char *level)
69 unsigned int lev = -1;
72 if (string_to_number(level, 0, 7, &lev) == -1) {
76 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
78 if (strncasecmp(level, ipt_log_names[i].name,
79 strlen(level)) == 0) {
81 exit_error(PARAMETER_PROBLEM,
82 "log-level `%s' ambiguous",
84 lev = ipt_log_names[i].level;
89 exit_error(PARAMETER_PROBLEM,
90 "log-level `%s' unknown", level);
96 #define IPT_LOG_OPT_LEVEL 0x01
97 #define IPT_LOG_OPT_PREFIX 0x02
98 #define IPT_LOG_OPT_TCPSEQ 0x04
99 #define IPT_LOG_OPT_TCPOPT 0x08
100 #define IPT_LOG_OPT_IPOPT 0x10
102 /* Function which parses command options; returns true if it
105 parse(int c, char **argv, int invert, unsigned int *flags,
106 const struct ipt_entry *entry,
107 struct ipt_entry_target **target)
109 struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data;
113 if (*flags & IPT_LOG_OPT_LEVEL)
114 exit_error(PARAMETER_PROBLEM,
115 "Can't specify --log-level twice");
117 if (check_inverse(optarg, &invert, NULL, 0))
118 exit_error(PARAMETER_PROBLEM,
119 "Unexpected `!' after --log-level");
121 loginfo->level = parse_level(optarg);
122 *flags |= IPT_LOG_OPT_LEVEL;
126 if (*flags & IPT_LOG_OPT_PREFIX)
127 exit_error(PARAMETER_PROBLEM,
128 "Can't specify --log-prefix twice");
130 if (check_inverse(optarg, &invert, NULL, 0))
131 exit_error(PARAMETER_PROBLEM,
132 "Unexpected `!' after --log-prefix");
134 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
135 exit_error(PARAMETER_PROBLEM,
136 "Maximum prefix length %u for --log-prefix",
137 sizeof(loginfo->prefix) - 1);
139 strcpy(loginfo->prefix, optarg);
140 *flags |= IPT_LOG_OPT_PREFIX;
144 if (*flags & IPT_LOG_OPT_TCPSEQ)
145 exit_error(PARAMETER_PROBLEM,
146 "Can't specify --log-tcp-sequence "
149 loginfo->logflags |= IPT_LOG_TCPSEQ;
150 *flags |= IPT_LOG_OPT_TCPSEQ;
154 if (*flags & IPT_LOG_OPT_TCPOPT)
155 exit_error(PARAMETER_PROBLEM,
156 "Can't specify --log-tcp-options twice");
158 loginfo->logflags |= IPT_LOG_TCPOPT;
159 *flags |= IPT_LOG_OPT_TCPOPT;
163 if (*flags & IPT_LOG_OPT_IPOPT)
164 exit_error(PARAMETER_PROBLEM,
165 "Can't specify --log-ip-options twice");
167 loginfo->logflags |= IPT_LOG_IPOPT;
168 *flags |= IPT_LOG_OPT_IPOPT;
178 /* Final check; nothing. */
179 static void final_check(unsigned int flags)
183 /* Prints out the targinfo. */
185 print(const struct ipt_ip *ip,
186 const struct ipt_entry_target *target,
189 const struct ipt_log_info *loginfo
190 = (const struct ipt_log_info *)target->data;
195 printf("flags %u level %u ",
196 loginfo->logflags, loginfo->level);
199 i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names);
201 if (loginfo->level == ipt_log_names[i].level) {
202 printf("level %s ", ipt_log_names[i].name);
206 if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names))
207 printf("UNKNOWN level %u ", loginfo->level);
208 if (loginfo->logflags & IPT_LOG_TCPSEQ)
209 printf("tcp-sequence ");
210 if (loginfo->logflags & IPT_LOG_TCPOPT)
211 printf("tcp-options ");
212 if (loginfo->logflags & IPT_LOG_IPOPT)
213 printf("ip-options ");
214 if (loginfo->logflags & ~(IPT_LOG_MASK))
215 printf("unknown-flags ");
218 if (strcmp(loginfo->prefix, "") != 0)
219 printf("prefix `%s' ", loginfo->prefix);
222 /* Saves the union ipt_targinfo in parsable form to stdout. */
224 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
226 const struct ipt_log_info *loginfo
227 = (const struct ipt_log_info *)target->data;
229 if (strcmp(loginfo->prefix, "") != 0)
230 printf("--log-prefix \"%s\" ", loginfo->prefix);
232 if (loginfo->level != LOG_DEFAULT_LEVEL)
233 printf("--log-level %d ", loginfo->level);
235 if (loginfo->logflags & IPT_LOG_TCPSEQ)
236 printf("--log-tcp-sequence ");
237 if (loginfo->logflags & IPT_LOG_TCPOPT)
238 printf("--log-tcp-options ");
239 if (loginfo->logflags & IPT_LOG_IPOPT)
240 printf("--log-ip-options ");
244 struct iptables_target log
247 .version = IPTABLES_VERSION,
248 .size = IPT_ALIGN(sizeof(struct ipt_log_info)),
249 .userspacesize = IPT_ALIGN(sizeof(struct ipt_log_info)),
253 .final_check = &final_check,
261 register_target(&log);