1 /* Shared library add-on to iptables to add LOG support. */
9 #include <linux/netfilter_ipv4/ipt_LOG.h>
11 #define LOG_DEFAULT_LEVEL LOG_WARNING
13 #ifndef IPT_LOG_UID /* Old kernel */
14 #define IPT_LOG_UID 0x08 /* Log UID owning local socket */
16 #define IPT_LOG_MASK 0x0f
19 static void LOG_help(void)
22 "LOG target options:\n"
23 " --log-level level Level of logging (numeric or see syslog.conf)\n"
24 " --log-prefix prefix Prefix log messages with this prefix.\n\n"
25 " --log-tcp-sequence Log TCP sequence numbers.\n\n"
26 " --log-tcp-options Log TCP options.\n\n"
27 " --log-ip-options Log IP options.\n\n"
28 " --log-uid Log UID owning the local socket.\n\n");
31 static const struct option LOG_opts
[] = {
32 { .name
= "log-level", .has_arg
= 1, .val
= '!' },
33 { .name
= "log-prefix", .has_arg
= 1, .val
= '#' },
34 { .name
= "log-tcp-sequence", .has_arg
= 0, .val
= '1' },
35 { .name
= "log-tcp-options", .has_arg
= 0, .val
= '2' },
36 { .name
= "log-ip-options", .has_arg
= 0, .val
= '3' },
37 { .name
= "log-uid", .has_arg
= 0, .val
= '4' },
41 static void LOG_init(struct xt_entry_target
*t
)
43 struct ipt_log_info
*loginfo
= (struct ipt_log_info
*)t
->data
;
45 loginfo
->level
= LOG_DEFAULT_LEVEL
;
49 struct ipt_log_names
{
54 static const 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 (!xtables_strtoui(level
, NULL
, &lev
, 0, 7)) {
75 for (i
= 0; i
< ARRAY_SIZE(ipt_log_names
); ++i
)
76 if (strncasecmp(level
, ipt_log_names
[i
].name
,
77 strlen(level
)) == 0) {
79 xtables_error(PARAMETER_PROBLEM
,
80 "log-level `%s' ambiguous",
82 lev
= ipt_log_names
[i
].level
;
86 xtables_error(PARAMETER_PROBLEM
,
87 "log-level `%s' unknown", level
);
93 #define IPT_LOG_OPT_LEVEL 0x01
94 #define IPT_LOG_OPT_PREFIX 0x02
95 #define IPT_LOG_OPT_TCPSEQ 0x04
96 #define IPT_LOG_OPT_TCPOPT 0x08
97 #define IPT_LOG_OPT_IPOPT 0x10
98 #define IPT_LOG_OPT_UID 0x20
100 static int LOG_parse(int c
, char **argv
, int invert
, unsigned int *flags
,
101 const void *entry
, struct xt_entry_target
**target
)
103 struct ipt_log_info
*loginfo
= (struct ipt_log_info
*)(*target
)->data
;
107 if (*flags
& IPT_LOG_OPT_LEVEL
)
108 xtables_error(PARAMETER_PROBLEM
,
109 "Can't specify --log-level twice");
111 if (xtables_check_inverse(optarg
, &invert
, NULL
, 0))
112 xtables_error(PARAMETER_PROBLEM
,
113 "Unexpected `!' after --log-level");
115 loginfo
->level
= parse_level(optarg
);
116 *flags
|= IPT_LOG_OPT_LEVEL
;
120 if (*flags
& IPT_LOG_OPT_PREFIX
)
121 xtables_error(PARAMETER_PROBLEM
,
122 "Can't specify --log-prefix twice");
124 if (xtables_check_inverse(optarg
, &invert
, NULL
, 0))
125 xtables_error(PARAMETER_PROBLEM
,
126 "Unexpected `!' after --log-prefix");
128 if (strlen(optarg
) > sizeof(loginfo
->prefix
) - 1)
129 xtables_error(PARAMETER_PROBLEM
,
130 "Maximum prefix length %u for --log-prefix",
131 (unsigned int)sizeof(loginfo
->prefix
) - 1);
133 if (strlen(optarg
) == 0)
134 xtables_error(PARAMETER_PROBLEM
,
135 "No prefix specified for --log-prefix");
137 if (strlen(optarg
) != strlen(strtok(optarg
, "\n")))
138 xtables_error(PARAMETER_PROBLEM
,
139 "Newlines not allowed in --log-prefix");
141 strcpy(loginfo
->prefix
, optarg
);
142 *flags
|= IPT_LOG_OPT_PREFIX
;
146 if (*flags
& IPT_LOG_OPT_TCPSEQ
)
147 xtables_error(PARAMETER_PROBLEM
,
148 "Can't specify --log-tcp-sequence "
151 loginfo
->logflags
|= IPT_LOG_TCPSEQ
;
152 *flags
|= IPT_LOG_OPT_TCPSEQ
;
156 if (*flags
& IPT_LOG_OPT_TCPOPT
)
157 xtables_error(PARAMETER_PROBLEM
,
158 "Can't specify --log-tcp-options twice");
160 loginfo
->logflags
|= IPT_LOG_TCPOPT
;
161 *flags
|= IPT_LOG_OPT_TCPOPT
;
165 if (*flags
& IPT_LOG_OPT_IPOPT
)
166 xtables_error(PARAMETER_PROBLEM
,
167 "Can't specify --log-ip-options twice");
169 loginfo
->logflags
|= IPT_LOG_IPOPT
;
170 *flags
|= IPT_LOG_OPT_IPOPT
;
174 if (*flags
& IPT_LOG_OPT_UID
)
175 xtables_error(PARAMETER_PROBLEM
,
176 "Can't specify --log-uid twice");
178 loginfo
->logflags
|= IPT_LOG_UID
;
179 *flags
|= IPT_LOG_OPT_UID
;
189 static void LOG_print(const void *ip
, const struct xt_entry_target
*target
,
192 const struct ipt_log_info
*loginfo
193 = (const struct ipt_log_info
*)target
->data
;
198 printf("flags %u level %u ",
199 loginfo
->logflags
, loginfo
->level
);
201 for (i
= 0; i
< ARRAY_SIZE(ipt_log_names
); ++i
)
202 if (loginfo
->level
== ipt_log_names
[i
].level
) {
203 printf("level %s ", ipt_log_names
[i
].name
);
206 if (i
== ARRAY_SIZE(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_UID
)
216 if (loginfo
->logflags
& ~(IPT_LOG_MASK
))
217 printf("unknown-flags ");
220 if (strcmp(loginfo
->prefix
, "") != 0)
221 printf("prefix `%s' ", loginfo
->prefix
);
224 static void LOG_save(const void *ip
, const struct xt_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 ");
231 xtables_save_string(loginfo
->prefix
);
234 if (loginfo
->level
!= LOG_DEFAULT_LEVEL
)
235 printf("--log-level %d ", loginfo
->level
);
237 if (loginfo
->logflags
& IPT_LOG_TCPSEQ
)
238 printf("--log-tcp-sequence ");
239 if (loginfo
->logflags
& IPT_LOG_TCPOPT
)
240 printf("--log-tcp-options ");
241 if (loginfo
->logflags
& IPT_LOG_IPOPT
)
242 printf("--log-ip-options ");
243 if (loginfo
->logflags
& IPT_LOG_UID
)
244 printf("--log-uid ");
247 static struct xtables_target log_tg_reg
= {
249 .version
= XTABLES_VERSION
,
250 .family
= NFPROTO_IPV4
,
251 .size
= XT_ALIGN(sizeof(struct ipt_log_info
)),
252 .userspacesize
= XT_ALIGN(sizeof(struct ipt_log_info
)),
258 .extra_opts
= LOG_opts
,
263 xtables_register_target(&log_tg_reg
);