blob: 1a3cf6e42ce51f7f1e41b766257162802dfd6465 [file] [log] [blame]
Martin Devera766113a2003-06-19 12:23:37 +00001/* Shared library add-on to iptables to add byte tracking support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +00007#include <xtables.h>
Patrick McHardy40d54752007-04-18 07:00:36 +00008#include <linux/netfilter/nf_conntrack_common.h>
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +00009#include <linux/netfilter/xt_connbytes.h>
Martin Devera766113a2003-06-19 12:23:37 +000010
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"connbytes v%s options:\n"
17" [!] --connbytes from:[to]\n"
Harald Welte93f4a3d2004-11-18 22:50:01 +000018" --connbytes-dir [original, reply, both]\n"
19" --connbytes-mode [packets, bytes, avgpkt]\n"
Martin Devera766113a2003-06-19 12:23:37 +000020"\n", IPTABLES_VERSION);
21}
22
Jan Engelhardt661f1122007-07-30 14:46:51 +000023static const struct option opts[] = {
Martin Devera766113a2003-06-19 12:23:37 +000024 { "connbytes", 1, 0, '1' },
Harald Welte93f4a3d2004-11-18 22:50:01 +000025 { "connbytes-dir", 1, 0, '2' },
26 { "connbytes-mode", 1, 0, '3' },
Martin Devera766113a2003-06-19 12:23:37 +000027 {0}
28};
29
Martin Devera766113a2003-06-19 12:23:37 +000030static void
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000031parse_range(const char *arg, struct xt_connbytes_info *si)
Martin Devera766113a2003-06-19 12:23:37 +000032{
33 char *colon,*p;
34
Harald Welte7dc57e22004-11-18 22:59:36 +000035 si->count.from = strtoul(arg,&colon,10);
Martin Devera766113a2003-06-19 12:23:37 +000036 if (*colon != ':')
37 exit_error(PARAMETER_PROBLEM, "Bad range `%s'", arg);
Harald Welte7dc57e22004-11-18 22:59:36 +000038 si->count.to = strtoul(colon+1,&p,10);
Martin Devera766113a2003-06-19 12:23:37 +000039 if (p == colon+1) {
40 /* second number omited */
Harald Welte7dc57e22004-11-18 22:59:36 +000041 si->count.to = 0xffffffff;
Martin Devera766113a2003-06-19 12:23:37 +000042 }
Harald Welte7dc57e22004-11-18 22:59:36 +000043 if (si->count.from > si->count.to)
44 exit_error(PARAMETER_PROBLEM, "%llu should be less than %llu",
45 si->count.from, si->count.to);
Martin Devera766113a2003-06-19 12:23:37 +000046}
47
48/* Function which parses command options; returns true if it
49 ate an option */
50static int
51parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000052 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000053 struct xt_entry_match **match)
Martin Devera766113a2003-06-19 12:23:37 +000054{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000055 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)(*match)->data;
Martin Josefsson11460882004-05-08 14:02:36 +000056 unsigned long i;
Martin Devera766113a2003-06-19 12:23:37 +000057
58 switch (c) {
59 case '1':
Harald Welte7dc57e22004-11-18 22:59:36 +000060 if (check_inverse(optarg, &invert, &optind, 0))
Martin Devera766113a2003-06-19 12:23:37 +000061 optind++;
62
63 parse_range(argv[optind-1], sinfo);
64 if (invert) {
Harald Welte93f4a3d2004-11-18 22:50:01 +000065 i = sinfo->count.from;
Harald Welte7dc57e22004-11-18 22:59:36 +000066 sinfo->count.from = sinfo->count.to;
Harald Welte93f4a3d2004-11-18 22:50:01 +000067 sinfo->count.to = i;
Martin Devera766113a2003-06-19 12:23:37 +000068 }
Harald Welte93f4a3d2004-11-18 22:50:01 +000069 *flags |= 1;
Martin Devera766113a2003-06-19 12:23:37 +000070 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +000071 case '2':
72 if (!strcmp(optarg, "original"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000073 sinfo->direction = XT_CONNBYTES_DIR_ORIGINAL;
Harald Welte93f4a3d2004-11-18 22:50:01 +000074 else if (!strcmp(optarg, "reply"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000075 sinfo->direction = XT_CONNBYTES_DIR_REPLY;
Harald Welte93f4a3d2004-11-18 22:50:01 +000076 else if (!strcmp(optarg, "both"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000077 sinfo->direction = XT_CONNBYTES_DIR_BOTH;
Harald Welte93f4a3d2004-11-18 22:50:01 +000078 else
79 exit_error(PARAMETER_PROBLEM,
80 "Unknown --connbytes-dir `%s'", optarg);
Martin Devera766113a2003-06-19 12:23:37 +000081
Harald Welte93f4a3d2004-11-18 22:50:01 +000082 *flags |= 2;
83 break;
84 case '3':
Harald Welte7dc57e22004-11-18 22:59:36 +000085 if (!strcmp(optarg, "packets"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000086 sinfo->what = XT_CONNBYTES_PKTS;
Harald Welte93f4a3d2004-11-18 22:50:01 +000087 else if (!strcmp(optarg, "bytes"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000088 sinfo->what = XT_CONNBYTES_BYTES;
Harald Welte93f4a3d2004-11-18 22:50:01 +000089 else if (!strcmp(optarg, "avgpkt"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000090 sinfo->what = XT_CONNBYTES_AVGPKT;
Harald Welte93f4a3d2004-11-18 22:50:01 +000091 else
92 exit_error(PARAMETER_PROBLEM,
93 "Unknown --connbytes-mode `%s'", optarg);
94 *flags |= 4;
Piotrek Kaczmarek1c0f2362005-04-24 16:19:51 +000095 break;
Martin Devera766113a2003-06-19 12:23:37 +000096 default:
97 return 0;
98 }
99
100 return 1;
101}
102
103static void final_check(unsigned int flags)
104{
Harald Welte93f4a3d2004-11-18 22:50:01 +0000105 if (flags != 7)
106 exit_error(PARAMETER_PROBLEM, "You must specify `--connbytes'"
Harald Welte402c3112005-12-05 12:08:03 +0000107 "`--connbytes-dir' and `--connbytes-mode'");
Harald Welte93f4a3d2004-11-18 22:50:01 +0000108}
109
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000110static void print_mode(struct xt_connbytes_info *sinfo)
Harald Welte93f4a3d2004-11-18 22:50:01 +0000111{
112 switch (sinfo->what) {
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000113 case XT_CONNBYTES_PKTS:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000114 fputs("packets ", stdout);
115 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000116 case XT_CONNBYTES_BYTES:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000117 fputs("bytes ", stdout);
118 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000119 case XT_CONNBYTES_AVGPKT:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000120 fputs("avgpkt ", stdout);
121 break;
Harald Welte7dc57e22004-11-18 22:59:36 +0000122 default:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000123 fputs("unknown ", stdout);
Harald Welte7dc57e22004-11-18 22:59:36 +0000124 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +0000125 }
126}
127
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000128static void print_direction(struct xt_connbytes_info *sinfo)
Harald Welte93f4a3d2004-11-18 22:50:01 +0000129{
130 switch (sinfo->direction) {
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000131 case XT_CONNBYTES_DIR_ORIGINAL:
Harald Welte7dc57e22004-11-18 22:59:36 +0000132 fputs("original ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000133 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000134 case XT_CONNBYTES_DIR_REPLY:
Harald Welte7dc57e22004-11-18 22:59:36 +0000135 fputs("reply ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000136 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000137 case XT_CONNBYTES_DIR_BOTH:
Harald Welte7dc57e22004-11-18 22:59:36 +0000138 fputs("both ", stdout);
139 break;
140 default:
141 fputs("unknown ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000142 break;
143 }
Martin Devera766113a2003-06-19 12:23:37 +0000144}
145
146/* Prints out the matchinfo. */
147static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000148print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000149 const struct xt_entry_match *match,
Martin Devera766113a2003-06-19 12:23:37 +0000150 int numeric)
151{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000152 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)match->data;
Martin Devera766113a2003-06-19 12:23:37 +0000153
Harald Welte7dc57e22004-11-18 22:59:36 +0000154 if (sinfo->count.from > sinfo->count.to)
155 printf("connbytes ! %llu:%llu ", sinfo->count.to,
156 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000157 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000158 printf("connbytes %llu:%llu ",sinfo->count.from,
159 sinfo->count.to);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000160
161 fputs("connbytes mode ", stdout);
162 print_mode(sinfo);
163
164 fputs("connbytes direction ", stdout);
165 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000166}
167
168/* Saves the matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000169static void save(const void *ip, const struct xt_entry_match *match)
Martin Devera766113a2003-06-19 12:23:37 +0000170{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000171 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)match->data;
Martin Devera766113a2003-06-19 12:23:37 +0000172
Harald Welte93f4a3d2004-11-18 22:50:01 +0000173 if (sinfo->count.from > sinfo->count.to)
Harald Welte7dc57e22004-11-18 22:59:36 +0000174 printf("! --connbytes %llu:%llu ", sinfo->count.to,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000175 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000176 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000177 printf("--connbytes %llu:%llu ", sinfo->count.from,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000178 sinfo->count.to);
179
180 fputs("--connbytes-mode ", stdout);
181 print_mode(sinfo);
182
Harald Welte402c3112005-12-05 12:08:03 +0000183 fputs("--connbytes-dir ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000184 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000185}
186
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000187static struct xtables_match state = {
188 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000189 .name = "connbytes",
190 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000191 .size = XT_ALIGN(sizeof(struct xt_connbytes_info)),
192 .userspacesize = XT_ALIGN(sizeof(struct xt_connbytes_info)),
193 .help = &help,
194 .parse = &parse,
195 .final_check = &final_check,
196 .print = &print,
197 .save = &save,
198 .extra_opts = opts
199};
200
201static struct xtables_match state6 = {
202 .family = AF_INET6,
203 .name = "connbytes",
204 .version = IPTABLES_VERSION,
205 .size = XT_ALIGN(sizeof(struct xt_connbytes_info)),
206 .userspacesize = XT_ALIGN(sizeof(struct xt_connbytes_info)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000207 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000208 .parse = &parse,
209 .final_check = &final_check,
210 .print = &print,
211 .save = &save,
212 .extra_opts = opts
Martin Devera766113a2003-06-19 12:23:37 +0000213};
214
215void _init(void)
216{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000217 xtables_register_match(&state);
218 xtables_register_match(&state6);
Martin Devera766113a2003-06-19 12:23:37 +0000219}