1 /* ebt_nflog
2 *
3 * Authors:
4 * Peter Warasin <[email protected]>
5 *
6 * February, 2008
7 *
8 * Based on:
9 * ebt_ulog.c, (C) 2004, Bart De Schuymer <[email protected]>
10 * libxt_NFLOG.c
11 *
12 * Adapted to libxtables for ebtables-compat in 2015 by
13 * Arturo Borrero Gonzalez <[email protected]>
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <getopt.h>
20 #include <xtables.h>
21 #include "iptables/nft.h"
22 #include "iptables/nft-bridge.h"
23 #include <linux/netfilter_bridge/ebt_nflog.h>
24
25 enum {
26 NFLOG_GROUP = 0x1,
27 NFLOG_PREFIX = 0x2,
28 NFLOG_RANGE = 0x4,
29 NFLOG_THRESHOLD = 0x8,
30 NFLOG_NFLOG = 0x16,
31 };
32
33 static const struct option brnflog_opts[] = {
34 { .name = "nflog-group", .has_arg = true, .val = NFLOG_GROUP},
35 { .name = "nflog-prefix", .has_arg = true, .val = NFLOG_PREFIX},
36 { .name = "nflog-range", .has_arg = true, .val = NFLOG_RANGE},
37 { .name = "nflog-threshold", .has_arg = true, .val = NFLOG_THRESHOLD},
38 { .name = "nflog", .has_arg = false, .val = NFLOG_NFLOG},
39 XT_GETOPT_TABLEEND,
40 };
41
brnflog_help(void)42 static void brnflog_help(void)
43 {
44 printf("nflog options:\n"
45 "--nflog : use the default nflog parameters\n"
46 "--nflog-prefix prefix : Prefix string for log message\n"
47 "--nflog-group group : NETLINK group used for logging\n"
48 "--nflog-range range : Number of byte to copy\n"
49 "--nflog-threshold : Message threshold of"
50 "in-kernel queue\n");
51 }
52
brnflog_init(struct xt_entry_target * t)53 static void brnflog_init(struct xt_entry_target *t)
54 {
55 struct ebt_nflog_info *info = (struct ebt_nflog_info *)t->data;
56
57 info->prefix[0] = '\0';
58 info->group = EBT_NFLOG_DEFAULT_GROUP;
59 info->threshold = EBT_NFLOG_DEFAULT_THRESHOLD;
60 }
61
brnflog_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_target ** target)62 static int brnflog_parse(int c, char **argv, int invert, unsigned int *flags,
63 const void *entry, struct xt_entry_target **target)
64 {
65 struct ebt_nflog_info *info = (struct ebt_nflog_info *)(*target)->data;
66 unsigned int i;
67
68 if (invert)
69 xtables_error(PARAMETER_PROBLEM,
70 "The use of '!' makes no sense for the"
71 " nflog watcher");
72
73 switch (c) {
74 case NFLOG_PREFIX:
75 EBT_CHECK_OPTION(flags, NFLOG_PREFIX);
76 if (strlen(optarg) > EBT_NFLOG_PREFIX_SIZE - 1)
77 xtables_error(PARAMETER_PROBLEM,
78 "Prefix too long for nflog-prefix");
79 strncpy(info->prefix, optarg, EBT_NFLOG_PREFIX_SIZE);
80 break;
81 case NFLOG_GROUP:
82 EBT_CHECK_OPTION(flags, NFLOG_GROUP);
83 if (!xtables_strtoui(optarg, NULL, &i, 1, UINT32_MAX))
84 xtables_error(PARAMETER_PROBLEM,
85 "--nflog-group must be a number!");
86 info->group = i;
87 break;
88 case NFLOG_RANGE:
89 EBT_CHECK_OPTION(flags, NFLOG_RANGE);
90 if (!xtables_strtoui(optarg, NULL, &i, 1, UINT32_MAX))
91 xtables_error(PARAMETER_PROBLEM,
92 "--nflog-range must be a number!");
93 info->len = i;
94 break;
95 case NFLOG_THRESHOLD:
96 EBT_CHECK_OPTION(flags, NFLOG_THRESHOLD);
97 if (!xtables_strtoui(optarg, NULL, &i, 1, UINT32_MAX))
98 xtables_error(PARAMETER_PROBLEM,
99 "--nflog-threshold must be a number!");
100 info->threshold = i;
101 break;
102 case NFLOG_NFLOG:
103 EBT_CHECK_OPTION(flags, NFLOG_NFLOG);
104 break;
105 default:
106 return 0;
107 }
108 return 1;
109 }
110
111 static void
brnflog_print(const void * ip,const struct xt_entry_target * target,int numeric)112 brnflog_print(const void *ip, const struct xt_entry_target *target,
113 int numeric)
114 {
115 struct ebt_nflog_info *info = (struct ebt_nflog_info *)target->data;
116
117 if (info->prefix[0] != '\0')
118 printf("--nflog-prefix \"%s\" ", info->prefix);
119 if (info->group)
120 printf("--nflog-group %d ", info->group);
121 if (info->len)
122 printf("--nflog-range %d ", info->len);
123 if (info->threshold != EBT_NFLOG_DEFAULT_THRESHOLD)
124 printf("--nflog-threshold %d ", info->threshold);
125 }
126
brnflog_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)127 static int brnflog_xlate(struct xt_xlate *xl,
128 const struct xt_xlate_tg_params *params)
129 {
130 const struct ebt_nflog_info *info = (void *)params->target->data;
131
132 xt_xlate_add(xl, "log ");
133 if (info->prefix[0] != '\0')
134 xt_xlate_add(xl, "prefix \"%s\" ", info->prefix);
135
136 xt_xlate_add(xl, "group %u ", info->group);
137
138 if (info->len)
139 xt_xlate_add(xl, "snaplen %u ", info->len);
140 if (info->threshold != EBT_NFLOG_DEFAULT_THRESHOLD)
141 xt_xlate_add(xl, "queue-threshold %u ", info->threshold);
142
143 return 1;
144 }
145
146 static struct xtables_target brnflog_watcher = {
147 .name = "nflog",
148 .revision = 0,
149 .ext_flags = XTABLES_EXT_WATCHER,
150 .version = XTABLES_VERSION,
151 .family = NFPROTO_BRIDGE,
152 .size = XT_ALIGN(sizeof(struct ebt_nflog_info)),
153 .userspacesize = XT_ALIGN(sizeof(struct ebt_nflog_info)),
154 .init = brnflog_init,
155 .help = brnflog_help,
156 .parse = brnflog_parse,
157 .print = brnflog_print,
158 .xlate = brnflog_xlate,
159 .extra_opts = brnflog_opts,
160 };
161
_init(void)162 void _init(void)
163 {
164 xtables_register_target(&brnflog_watcher);
165 }
166