libxt_statistic.c revision 23545c2a7a31c68c1e49c7c901b632c2f1c59968
1#include <stdio.h>
2#include <netdb.h>
3#include <string.h>
4#include <stdlib.h>
5#include <stddef.h>
6#include <getopt.h>
7
8#include <xtables.h>
9#include <linux/netfilter/xt_statistic.h>
10
11static void statistic_help(void)
12{
13	printf(
14"statistic match v%s options:\n"
15" --mode mode                    Match mode (random, nth)\n"
16" random mode:\n"
17" --probability p		 Probability\n"
18" nth mode:\n"
19" --every n			 Match every nth packet\n"
20" --packet p			 Initial counter value (0 <= p <= n-1, default 0)\n"
21"\n",
22IPTABLES_VERSION);
23}
24
25static const struct option statistic_opts[] = {
26	{ "mode", 1, NULL, '1' },
27	{ "probability", 1, NULL, '2' },
28	{ "every", 1, NULL, '3' },
29	{ "packet", 1, NULL, '4' },
30	{ .name = NULL }
31};
32
33static struct xt_statistic_info *global_info;
34
35static void statistic_mt_init(struct xt_entry_match *match)
36{
37	global_info = (void *)match->data;
38}
39
40static int
41statistic_parse(int c, char **argv, int invert, unsigned int *flags,
42                const void *entry, struct xt_entry_match **match)
43{
44	struct xt_statistic_info *info = (void *)(*match)->data;
45	double prob;
46
47	if (invert)
48		info->flags |= XT_STATISTIC_INVERT;
49
50	switch (c) {
51	case '1':
52		if (*flags & 0x1)
53			exit_error(PARAMETER_PROBLEM, "double --mode");
54		if (!strcmp(optarg, "random"))
55			info->mode = XT_STATISTIC_MODE_RANDOM;
56		else if (!strcmp(optarg, "nth"))
57			info->mode = XT_STATISTIC_MODE_NTH;
58		else
59			exit_error(PARAMETER_PROBLEM, "Bad mode `%s'", optarg);
60		*flags |= 0x1;
61		break;
62	case '2':
63		if (*flags & 0x2)
64			exit_error(PARAMETER_PROBLEM, "double --probability");
65		prob = atof(optarg);
66		if (prob < 0 || prob > 1)
67			exit_error(PARAMETER_PROBLEM,
68				   "--probability must be between 0 and 1");
69		info->u.random.probability = 0x80000000 * prob;
70		*flags |= 0x2;
71		break;
72	case '3':
73		if (*flags & 0x4)
74			exit_error(PARAMETER_PROBLEM, "double --every");
75		if (string_to_number(optarg, 0, 0xFFFFFFFF,
76				     &info->u.nth.every) == -1)
77			exit_error(PARAMETER_PROBLEM,
78				   "cannot parse --every `%s'", optarg);
79		if (info->u.nth.every == 0)
80			exit_error(PARAMETER_PROBLEM, "--every cannot be 0");
81		info->u.nth.every--;
82		*flags |= 0x4;
83		break;
84	case '4':
85		if (*flags & 0x8)
86			exit_error(PARAMETER_PROBLEM, "double --packet");
87		if (string_to_number(optarg, 0, 0xFFFFFFFF,
88				     &info->u.nth.packet) == -1)
89			exit_error(PARAMETER_PROBLEM,
90				   "cannot parse --packet `%s'", optarg);
91		*flags |= 0x8;
92		break;
93	default:
94		return 0;
95	}
96	return 1;
97}
98
99/* Final check; must have specified --mark. */
100static void statistic_check(unsigned int flags)
101{
102	if (!(flags & 0x1))
103		exit_error(PARAMETER_PROBLEM, "no mode specified");
104	if ((flags & 0x2) && (flags & (0x4 | 0x8)))
105		exit_error(PARAMETER_PROBLEM,
106			   "both nth and random parameters given");
107	if (flags & 0x2 && global_info->mode != XT_STATISTIC_MODE_RANDOM)
108		exit_error(PARAMETER_PROBLEM,
109			   "--probability can only be used in random mode");
110	if (flags & 0x4 && global_info->mode != XT_STATISTIC_MODE_NTH)
111		exit_error(PARAMETER_PROBLEM,
112			   "--every can only be used in nth mode");
113	if (flags & 0x8 && global_info->mode != XT_STATISTIC_MODE_NTH)
114		exit_error(PARAMETER_PROBLEM,
115			   "--packet can only be used in nth mode");
116	if ((flags & 0x8) && !(flags & 0x4))
117		exit_error(PARAMETER_PROBLEM,
118			   "--packet can only be used with --every");
119	/* at this point, info->u.nth.every have been decreased. */
120	if (global_info->u.nth.packet > global_info->u.nth.every)
121		exit_error(PARAMETER_PROBLEM,
122			  "the --packet p must be 0 <= p <= n-1");
123
124
125	global_info->u.nth.count = global_info->u.nth.every -
126	                           global_info->u.nth.packet;
127}
128
129/* Prints out the matchinfo. */
130static void print_match(const struct xt_statistic_info *info, char *prefix)
131{
132	if (info->flags & XT_STATISTIC_INVERT)
133		printf("! ");
134
135	switch (info->mode) {
136	case XT_STATISTIC_MODE_RANDOM:
137		printf("%smode random %sprobability %f ", prefix, prefix,
138		       1.0 * info->u.random.probability / 0x80000000);
139		break;
140	case XT_STATISTIC_MODE_NTH:
141		printf("%smode nth %severy %u ", prefix, prefix,
142		       info->u.nth.every + 1);
143		if (info->u.nth.packet)
144			printf("%spacket %u ", prefix, info->u.nth.packet);
145		break;
146	}
147}
148
149static void
150statistic_print(const void *ip, const struct xt_entry_match *match, int numeric)
151{
152	struct xt_statistic_info *info = (struct xt_statistic_info *)match->data;
153
154	printf("statistic ");
155	print_match(info, "");
156}
157
158/* Saves the union ipt_matchinfo in parsable form to stdout. */
159static void statistic_save(const void *ip, const struct xt_entry_match *match)
160{
161	struct xt_statistic_info *info = (struct xt_statistic_info *)match->data;
162
163	print_match(info, "--");
164}
165
166static struct xtables_match statistic_match = {
167	.family		= AF_UNSPEC,
168	.name		= "statistic",
169	.version	= IPTABLES_VERSION,
170	.size		= XT_ALIGN(sizeof(struct xt_statistic_info)),
171	.userspacesize	= offsetof(struct xt_statistic_info, u.nth.count),
172	.init		= statistic_mt_init,
173	.help		= statistic_help,
174	.parse		= statistic_parse,
175	.final_check	= statistic_check,
176	.print		= statistic_print,
177	.save		= statistic_save,
178	.extra_opts	= statistic_opts,
179};
180
181void _init(void)
182{
183	xtables_register_match(&statistic_match);
184}
185