blob: 448576afaabe3bb7e39254acede9530d7ad70ad1 [file] [log] [blame]
Jan Engelhardt32b8e612010-07-23 21:16:14 +02001#include <stdbool.h>
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +00002#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <getopt.h>
6#include <xtables.h>
7
8#include <linux/netfilter/x_tables.h>
9#include <linux/netfilter/xt_NFLOG.h>
10
11enum {
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020012 O_GROUP = 0,
13 O_PREFIX,
14 O_RANGE,
15 O_THRESHOLD,
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000016};
17
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020018#define s struct xt_nflog_info
19static const struct xt_option_entry NFLOG_opts[] = {
20 {.name = "nflog-group", .id = O_GROUP, .type = XTTYPE_UINT16,
21 .flags = XTOPT_PUT, XTOPT_POINTER(s, group)},
22 {.name = "nflog-prefix", .id = O_PREFIX, .type = XTTYPE_STRING,
23 .min = 1, .flags = XTOPT_PUT, XTOPT_POINTER(s, prefix)},
24 {.name = "nflog-range", .id = O_RANGE, .type = XTTYPE_UINT32,
25 .flags = XTOPT_PUT, XTOPT_POINTER(s, len)},
26 {.name = "nflog-threshold", .id = O_THRESHOLD, .type = XTTYPE_UINT16,
27 .flags = XTOPT_PUT, XTOPT_POINTER(s, threshold)},
28 XTOPT_TABLEEND,
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000029};
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020030#undef s
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000031
Jan Engelhardt932e6482007-10-04 16:27:30 +000032static void NFLOG_help(void)
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000033{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020034 printf("NFLOG target options:\n"
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000035 " --nflog-group NUM NETLINK group used for logging\n"
36 " --nflog-range NUM Number of byte to copy\n"
37 " --nflog-threshold NUM Message threshold of in-kernel queue\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020038 " --nflog-prefix STRING Prefix string for log messages\n");
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000039}
40
Jan Engelhardt932e6482007-10-04 16:27:30 +000041static void NFLOG_init(struct xt_entry_target *t)
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000042{
43 struct xt_nflog_info *info = (struct xt_nflog_info *)t->data;
44
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000045 info->threshold = XT_NFLOG_DEFAULT_THRESHOLD;
46}
47
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020048static void NFLOG_parse(struct xt_option_call *cb)
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000049{
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020050 xtables_option_parse(cb);
51 switch (cb->entry->id) {
52 case O_PREFIX:
53 if (strchr(cb->arg, '\n') != NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010054 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020055 "Newlines not allowed in --log-prefix");
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000056 break;
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000057 }
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000058}
59
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000060static void nflog_print(const struct xt_nflog_info *info, char *prefix)
61{
Max Kellermanna5d09942008-01-29 13:44:34 +000062 if (info->prefix[0] != '\0') {
Jan Engelhardt73866352010-12-18 02:04:59 +010063 printf(" %snflog-prefix ", prefix);
Jan Engelhardta0baae82009-01-30 04:32:50 +010064 xtables_save_string(info->prefix);
Max Kellermanna5d09942008-01-29 13:44:34 +000065 }
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000066 if (info->group)
Jan Engelhardt73866352010-12-18 02:04:59 +010067 printf(" %snflog-group %u", prefix, info->group);
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000068 if (info->len)
Jan Engelhardt73866352010-12-18 02:04:59 +010069 printf(" %snflog-range %u", prefix, info->len);
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000070 if (info->threshold != XT_NFLOG_DEFAULT_THRESHOLD)
Jan Engelhardt73866352010-12-18 02:04:59 +010071 printf(" %snflog-threshold %u", prefix, info->threshold);
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000072}
73
Jan Engelhardt932e6482007-10-04 16:27:30 +000074static void NFLOG_print(const void *ip, const struct xt_entry_target *target,
75 int numeric)
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000076{
77 const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data;
78
79 nflog_print(info, "");
80}
81
Jan Engelhardt932e6482007-10-04 16:27:30 +000082static void NFLOG_save(const void *ip, const struct xt_entry_target *target)
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000083{
84 const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data;
85
86 nflog_print(info, "--");
87}
88
Jan Engelhardt932e6482007-10-04 16:27:30 +000089static struct xtables_target nflog_target = {
Jan Engelhardtc5e85732009-06-12 20:55:44 +020090 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000091 .name = "NFLOG",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020092 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +000093 .size = XT_ALIGN(sizeof(struct xt_nflog_info)),
94 .userspacesize = XT_ALIGN(sizeof(struct xt_nflog_info)),
Jan Engelhardt932e6482007-10-04 16:27:30 +000095 .help = NFLOG_help,
96 .init = NFLOG_init,
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +020097 .x6_parse = NFLOG_parse,
Jan Engelhardt932e6482007-10-04 16:27:30 +000098 .print = NFLOG_print,
99 .save = NFLOG_save,
Jan Engelhardt6cfb28b2011-05-01 16:27:46 +0200100 .x6_options = NFLOG_opts,
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +0000101};
102
103void _init(void)
104{
Jan Engelhardt932e6482007-10-04 16:27:30 +0000105 xtables_register_target(&nflog_target);
Yasuyuki KOZAKAIYasuyuki KOZAKAIa16e1142007-08-04 08:21:16 +0000106}