blob: 12a83dd801f799424e00898817d657fce55a4cd7 [file] [log] [blame]
Jan Engelhardt39d3aa32011-05-02 16:38:11 +02001#include <math.h>
Patrick McHardy10e0fbb2006-08-31 14:01:35 +00002#include <stdio.h>
Patrick McHardy10e0fbb2006-08-31 14:01:35 +00003#include <string.h>
Yasuyuki KOZAKAIdfd78a82007-08-04 08:28:23 +00004#include <xtables.h>
Patrick McHardy10e0fbb2006-08-31 14:01:35 +00005#include <linux/netfilter/xt_statistic.h>
6
Jan Engelhardt3c7f5012011-05-02 18:26:31 +02007enum {
8 O_MODE = 0,
9 O_PROBABILITY,
10 O_EVERY,
11 O_PACKET,
12 F_PROBABILITY = 1 << O_PROBABILITY,
13 F_EVERY = 1 << O_EVERY,
14 F_PACKET = 1 << O_PACKET,
15};
16
Jan Engelhardt181dead2007-10-04 16:27:07 +000017static void statistic_help(void)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"statistic match options:\n"
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000021" --mode mode Match mode (random, nth)\n"
22" random mode:\n"
Jan Engelhardtd118d212011-05-02 16:29:18 +020023"[!] --probability p Probability\n"
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000024" nth mode:\n"
Jan Engelhardtd118d212011-05-02 16:29:18 +020025"[!] --every n Match every nth packet\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020026" --packet p Initial counter value (0 <= p <= n-1, default 0)\n");
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000027}
28
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020029#define s struct xt_statistic_info
30static const struct xt_option_entry statistic_opts[] = {
31 {.name = "mode", .id = O_MODE, .type = XTTYPE_STRING,
32 .flags = XTOPT_MAND},
33 {.name = "probability", .id = O_PROBABILITY, .type = XTTYPE_DOUBLE,
34 .flags = XTOPT_INVERT, .min = 0, .max = 1,
35 .excl = F_EVERY | F_PACKET},
36 {.name = "every", .id = O_EVERY, .type = XTTYPE_UINT32, .min = 1,
37 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, u.nth.every),
38 .excl = F_PROBABILITY, .also = F_PACKET},
39 {.name = "packet", .id = O_PACKET, .type = XTTYPE_UINT32,
40 .flags = XTOPT_PUT, XTOPT_POINTER(s, u.nth.packet),
41 .excl = F_PROBABILITY, .also = F_EVERY},
42 XTOPT_TABLEEND,
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000043};
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020044#undef s
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000045
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020046static void statistic_parse(struct xt_option_call *cb)
Jan Engelhardtdbb77542008-02-11 00:33:30 +010047{
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020048 struct xt_statistic_info *info = cb->data;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000049
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020050 if (cb->invert)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000051 info->flags |= XT_STATISTIC_INVERT;
52
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020053 xtables_option_parse(cb);
54 switch (cb->entry->id) {
55 case O_MODE:
56 if (strcmp(cb->arg, "random") == 0)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000057 info->mode = XT_STATISTIC_MODE_RANDOM;
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020058 else if (strcmp(cb->arg, "nth") == 0)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000059 info->mode = XT_STATISTIC_MODE_NTH;
60 else
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020061 xtables_error(PARAMETER_PROBLEM, "Bad mode \"%s\"",
62 cb->arg);
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000063 break;
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020064 case O_PROBABILITY:
65 info->u.random.probability = lround(0x80000000 * cb->val.dbl);
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000066 break;
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020067 case O_EVERY:
68 --info->u.nth.every;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000069 break;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000070 }
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000071}
72
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020073static void statistic_check(struct xt_fcheck_call *cb)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000074{
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020075 struct xt_statistic_info *info = cb->data;
76
77 if (info->mode == XT_STATISTIC_MODE_RANDOM &&
78 !(cb->xflags & F_PROBABILITY))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010079 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020080 "--probability must be specified when using "
81 "random mode");
82 if (info->mode == XT_STATISTIC_MODE_NTH &&
83 !(cb->xflags & (F_EVERY | F_PACKET)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010084 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020085 "--every and --packet must be specified when "
86 "using nth mode");
87
Nicolas Bouliane0c6bf102007-07-03 12:52:55 +000088 /* at this point, info->u.nth.every have been decreased. */
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020089 if (info->u.nth.packet > info->u.nth.every)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010090 xtables_error(PARAMETER_PROBLEM,
Nicolas Bouliane0c6bf102007-07-03 12:52:55 +000091 "the --packet p must be 0 <= p <= n-1");
92
Jan Engelhardt3c7f5012011-05-02 18:26:31 +020093 info->u.nth.count = info->u.nth.every - info->u.nth.packet;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000094}
95
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000096static void print_match(const struct xt_statistic_info *info, char *prefix)
97{
Patrick McHardy10e0fbb2006-08-31 14:01:35 +000098 switch (info->mode) {
99 case XT_STATISTIC_MODE_RANDOM:
Jan Engelhardt39d3aa32011-05-02 16:38:11 +0200100 printf(" %smode random%s %sprobability %.11f", prefix,
Jan Engelhardtd118d212011-05-02 16:29:18 +0200101 (info->flags & XT_STATISTIC_INVERT) ? " !" : "",
102 prefix,
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000103 1.0 * info->u.random.probability / 0x80000000);
104 break;
105 case XT_STATISTIC_MODE_NTH:
Jan Engelhardtd118d212011-05-02 16:29:18 +0200106 printf(" %smode nth%s %severy %u", prefix,
107 (info->flags & XT_STATISTIC_INVERT) ? " !" : "",
108 prefix,
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000109 info->u.nth.every + 1);
110 if (info->u.nth.packet)
Jan Engelhardt73866352010-12-18 02:04:59 +0100111 printf(" %spacket %u", prefix, info->u.nth.packet);
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000112 break;
113 }
114}
115
116static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000117statistic_print(const void *ip, const struct xt_entry_match *match, int numeric)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000118{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200119 const struct xt_statistic_info *info = (const void *)match->data;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000120
Jan Engelhardt73866352010-12-18 02:04:59 +0100121 printf(" statistic");
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000122 print_match(info, "");
123}
124
Jan Engelhardt181dead2007-10-04 16:27:07 +0000125static void statistic_save(const void *ip, const struct xt_entry_match *match)
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000126{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200127 const struct xt_statistic_info *info = (const void *)match->data;
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000128
129 print_match(info, "--");
130}
131
Jan Engelhardt181dead2007-10-04 16:27:07 +0000132static struct xtables_match statistic_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +0200133 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAIdfd78a82007-08-04 08:28:23 +0000134 .name = "statistic",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200135 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIdfd78a82007-08-04 08:28:23 +0000136 .size = XT_ALIGN(sizeof(struct xt_statistic_info)),
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000137 .userspacesize = offsetof(struct xt_statistic_info, u.nth.count),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000138 .help = statistic_help,
Jan Engelhardt3c7f5012011-05-02 18:26:31 +0200139 .x6_parse = statistic_parse,
140 .x6_fcheck = statistic_check,
Jan Engelhardt181dead2007-10-04 16:27:07 +0000141 .print = statistic_print,
142 .save = statistic_save,
Jan Engelhardt3c7f5012011-05-02 18:26:31 +0200143 .x6_options = statistic_opts,
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000144};
145
146void _init(void)
147{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000148 xtables_register_match(&statistic_match);
Patrick McHardy10e0fbb2006-08-31 14:01:35 +0000149}