blob: 48ef9a37180cd61c58434bd5444ad91efb17f5c8 [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,
Martin Devera766113a2003-06-19 12:23:37 +000053 unsigned int *nfcache,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000054 struct xt_entry_match **match)
Martin Devera766113a2003-06-19 12:23:37 +000055{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000056 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)(*match)->data;
Martin Josefsson11460882004-05-08 14:02:36 +000057 unsigned long i;
Martin Devera766113a2003-06-19 12:23:37 +000058
59 switch (c) {
60 case '1':
Harald Welte7dc57e22004-11-18 22:59:36 +000061 if (check_inverse(optarg, &invert, &optind, 0))
Martin Devera766113a2003-06-19 12:23:37 +000062 optind++;
63
64 parse_range(argv[optind-1], sinfo);
65 if (invert) {
Harald Welte93f4a3d2004-11-18 22:50:01 +000066 i = sinfo->count.from;
Harald Welte7dc57e22004-11-18 22:59:36 +000067 sinfo->count.from = sinfo->count.to;
Harald Welte93f4a3d2004-11-18 22:50:01 +000068 sinfo->count.to = i;
Martin Devera766113a2003-06-19 12:23:37 +000069 }
Harald Welte93f4a3d2004-11-18 22:50:01 +000070 *flags |= 1;
Martin Devera766113a2003-06-19 12:23:37 +000071 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +000072 case '2':
73 if (!strcmp(optarg, "original"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000074 sinfo->direction = XT_CONNBYTES_DIR_ORIGINAL;
Harald Welte93f4a3d2004-11-18 22:50:01 +000075 else if (!strcmp(optarg, "reply"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000076 sinfo->direction = XT_CONNBYTES_DIR_REPLY;
Harald Welte93f4a3d2004-11-18 22:50:01 +000077 else if (!strcmp(optarg, "both"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000078 sinfo->direction = XT_CONNBYTES_DIR_BOTH;
Harald Welte93f4a3d2004-11-18 22:50:01 +000079 else
80 exit_error(PARAMETER_PROBLEM,
81 "Unknown --connbytes-dir `%s'", optarg);
Martin Devera766113a2003-06-19 12:23:37 +000082
Harald Welte93f4a3d2004-11-18 22:50:01 +000083 *flags |= 2;
84 break;
85 case '3':
Harald Welte7dc57e22004-11-18 22:59:36 +000086 if (!strcmp(optarg, "packets"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000087 sinfo->what = XT_CONNBYTES_PKTS;
Harald Welte93f4a3d2004-11-18 22:50:01 +000088 else if (!strcmp(optarg, "bytes"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000089 sinfo->what = XT_CONNBYTES_BYTES;
Harald Welte93f4a3d2004-11-18 22:50:01 +000090 else if (!strcmp(optarg, "avgpkt"))
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +000091 sinfo->what = XT_CONNBYTES_AVGPKT;
Harald Welte93f4a3d2004-11-18 22:50:01 +000092 else
93 exit_error(PARAMETER_PROBLEM,
94 "Unknown --connbytes-mode `%s'", optarg);
95 *flags |= 4;
Piotrek Kaczmarek1c0f2362005-04-24 16:19:51 +000096 break;
Martin Devera766113a2003-06-19 12:23:37 +000097 default:
98 return 0;
99 }
100
101 return 1;
102}
103
104static void final_check(unsigned int flags)
105{
Harald Welte93f4a3d2004-11-18 22:50:01 +0000106 if (flags != 7)
107 exit_error(PARAMETER_PROBLEM, "You must specify `--connbytes'"
Harald Welte402c3112005-12-05 12:08:03 +0000108 "`--connbytes-dir' and `--connbytes-mode'");
Harald Welte93f4a3d2004-11-18 22:50:01 +0000109}
110
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000111static void print_mode(struct xt_connbytes_info *sinfo)
Harald Welte93f4a3d2004-11-18 22:50:01 +0000112{
113 switch (sinfo->what) {
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000114 case XT_CONNBYTES_PKTS:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000115 fputs("packets ", stdout);
116 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000117 case XT_CONNBYTES_BYTES:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000118 fputs("bytes ", stdout);
119 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000120 case XT_CONNBYTES_AVGPKT:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000121 fputs("avgpkt ", stdout);
122 break;
Harald Welte7dc57e22004-11-18 22:59:36 +0000123 default:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000124 fputs("unknown ", stdout);
Harald Welte7dc57e22004-11-18 22:59:36 +0000125 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +0000126 }
127}
128
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000129static void print_direction(struct xt_connbytes_info *sinfo)
Harald Welte93f4a3d2004-11-18 22:50:01 +0000130{
131 switch (sinfo->direction) {
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000132 case XT_CONNBYTES_DIR_ORIGINAL:
Harald Welte7dc57e22004-11-18 22:59:36 +0000133 fputs("original ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000134 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000135 case XT_CONNBYTES_DIR_REPLY:
Harald Welte7dc57e22004-11-18 22:59:36 +0000136 fputs("reply ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000137 break;
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000138 case XT_CONNBYTES_DIR_BOTH:
Harald Welte7dc57e22004-11-18 22:59:36 +0000139 fputs("both ", stdout);
140 break;
141 default:
142 fputs("unknown ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000143 break;
144 }
Martin Devera766113a2003-06-19 12:23:37 +0000145}
146
147/* Prints out the matchinfo. */
148static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000149print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000150 const struct xt_entry_match *match,
Martin Devera766113a2003-06-19 12:23:37 +0000151 int numeric)
152{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000153 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)match->data;
Martin Devera766113a2003-06-19 12:23:37 +0000154
Harald Welte7dc57e22004-11-18 22:59:36 +0000155 if (sinfo->count.from > sinfo->count.to)
156 printf("connbytes ! %llu:%llu ", sinfo->count.to,
157 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000158 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000159 printf("connbytes %llu:%llu ",sinfo->count.from,
160 sinfo->count.to);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000161
162 fputs("connbytes mode ", stdout);
163 print_mode(sinfo);
164
165 fputs("connbytes direction ", stdout);
166 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000167}
168
169/* Saves the matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000170static void save(const void *ip, const struct xt_entry_match *match)
Martin Devera766113a2003-06-19 12:23:37 +0000171{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000172 struct xt_connbytes_info *sinfo = (struct xt_connbytes_info *)match->data;
Martin Devera766113a2003-06-19 12:23:37 +0000173
Harald Welte93f4a3d2004-11-18 22:50:01 +0000174 if (sinfo->count.from > sinfo->count.to)
Harald Welte7dc57e22004-11-18 22:59:36 +0000175 printf("! --connbytes %llu:%llu ", sinfo->count.to,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000176 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000177 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000178 printf("--connbytes %llu:%llu ", sinfo->count.from,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000179 sinfo->count.to);
180
181 fputs("--connbytes-mode ", stdout);
182 print_mode(sinfo);
183
Harald Welte402c3112005-12-05 12:08:03 +0000184 fputs("--connbytes-dir ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000185 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000186}
187
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000188static struct xtables_match state = {
189 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000190 .name = "connbytes",
191 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000192 .size = XT_ALIGN(sizeof(struct xt_connbytes_info)),
193 .userspacesize = XT_ALIGN(sizeof(struct xt_connbytes_info)),
194 .help = &help,
195 .parse = &parse,
196 .final_check = &final_check,
197 .print = &print,
198 .save = &save,
199 .extra_opts = opts
200};
201
202static struct xtables_match state6 = {
203 .family = AF_INET6,
204 .name = "connbytes",
205 .version = IPTABLES_VERSION,
206 .size = XT_ALIGN(sizeof(struct xt_connbytes_info)),
207 .userspacesize = XT_ALIGN(sizeof(struct xt_connbytes_info)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000208 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000209 .parse = &parse,
210 .final_check = &final_check,
211 .print = &print,
212 .save = &save,
213 .extra_opts = opts
Martin Devera766113a2003-06-19 12:23:37 +0000214};
215
216void _init(void)
217{
Yasuyuki KOZAKAI6aac5002007-08-04 08:25:43 +0000218 xtables_register_match(&state);
219 xtables_register_match(&state6);
Martin Devera766113a2003-06-19 12:23:37 +0000220}