blob: 2d61a4af8694f41111bb075e0fc8b9073a807f32 [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>
7#include <iptables.h>
8#include <linux/netfilter_ipv4/ip_conntrack.h>
9#include <linux/netfilter_ipv4/ipt_connbytes.h>
10
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
23static struct option opts[] = {
24 { "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
30/* Initialize the match. */
31static void
32init(struct ipt_entry_match *m, unsigned int *nfcache)
33{
34 /* Can't cache this */
35 *nfcache |= NFC_UNKNOWN;
36}
37
38static void
39parse_range(const char *arg, struct ipt_connbytes_info *si)
40{
41 char *colon,*p;
42
Harald Welte7dc57e22004-11-18 22:59:36 +000043 si->count.from = strtoul(arg,&colon,10);
Martin Devera766113a2003-06-19 12:23:37 +000044 if (*colon != ':')
45 exit_error(PARAMETER_PROBLEM, "Bad range `%s'", arg);
Harald Welte7dc57e22004-11-18 22:59:36 +000046 si->count.to = strtoul(colon+1,&p,10);
Martin Devera766113a2003-06-19 12:23:37 +000047 if (p == colon+1) {
48 /* second number omited */
Harald Welte7dc57e22004-11-18 22:59:36 +000049 si->count.to = 0xffffffff;
Martin Devera766113a2003-06-19 12:23:37 +000050 }
Harald Welte7dc57e22004-11-18 22:59:36 +000051 if (si->count.from > si->count.to)
52 exit_error(PARAMETER_PROBLEM, "%llu should be less than %llu",
53 si->count.from, si->count.to);
Martin Devera766113a2003-06-19 12:23:37 +000054}
55
56/* Function which parses command options; returns true if it
57 ate an option */
58static int
59parse(int c, char **argv, int invert, unsigned int *flags,
60 const struct ipt_entry *entry,
61 unsigned int *nfcache,
62 struct ipt_entry_match **match)
63{
64 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)(*match)->data;
Martin Josefsson11460882004-05-08 14:02:36 +000065 unsigned long i;
Martin Devera766113a2003-06-19 12:23:37 +000066
67 switch (c) {
68 case '1':
Harald Welte7dc57e22004-11-18 22:59:36 +000069 if (check_inverse(optarg, &invert, &optind, 0))
Martin Devera766113a2003-06-19 12:23:37 +000070 optind++;
71
72 parse_range(argv[optind-1], sinfo);
73 if (invert) {
Harald Welte93f4a3d2004-11-18 22:50:01 +000074 i = sinfo->count.from;
Harald Welte7dc57e22004-11-18 22:59:36 +000075 sinfo->count.from = sinfo->count.to;
Harald Welte93f4a3d2004-11-18 22:50:01 +000076 sinfo->count.to = i;
Martin Devera766113a2003-06-19 12:23:37 +000077 }
Harald Welte93f4a3d2004-11-18 22:50:01 +000078 *flags |= 1;
Martin Devera766113a2003-06-19 12:23:37 +000079 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +000080 case '2':
81 if (!strcmp(optarg, "original"))
82 sinfo->direction = IPT_CONNBYTES_DIR_ORIGINAL;
83 else if (!strcmp(optarg, "reply"))
84 sinfo->direction = IPT_CONNBYTES_DIR_REPLY;
85 else if (!strcmp(optarg, "both"))
86 sinfo->direction = IPT_CONNBYTES_DIR_BOTH;
87 else
88 exit_error(PARAMETER_PROBLEM,
89 "Unknown --connbytes-dir `%s'", optarg);
Martin Devera766113a2003-06-19 12:23:37 +000090
Harald Welte93f4a3d2004-11-18 22:50:01 +000091 *flags |= 2;
92 break;
93 case '3':
Harald Welte7dc57e22004-11-18 22:59:36 +000094 if (!strcmp(optarg, "packets"))
Harald Welte93f4a3d2004-11-18 22:50:01 +000095 sinfo->what = IPT_CONNBYTES_WHAT_PKTS;
96 else if (!strcmp(optarg, "bytes"))
97 sinfo->what = IPT_CONNBYTES_WHAT_BYTES;
98 else if (!strcmp(optarg, "avgpkt"))
99 sinfo->what = IPT_CONNBYTES_WHAT_AVGPKT;
100 else
101 exit_error(PARAMETER_PROBLEM,
102 "Unknown --connbytes-mode `%s'", optarg);
103 *flags |= 4;
Martin Devera766113a2003-06-19 12:23:37 +0000104 default:
105 return 0;
106 }
107
108 return 1;
109}
110
111static void final_check(unsigned int flags)
112{
Harald Welte93f4a3d2004-11-18 22:50:01 +0000113 if (flags != 7)
114 exit_error(PARAMETER_PROBLEM, "You must specify `--connbytes'"
115 "`--connbytes-direction' and `--connbytes-mode'");
116}
117
118static void print_mode(struct ipt_connbytes_info *sinfo)
119{
120 switch (sinfo->what) {
121 case IPT_CONNBYTES_WHAT_PKTS:
122 fputs("packets ", stdout);
123 break;
124 case IPT_CONNBYTES_WHAT_BYTES:
125 fputs("bytes ", stdout);
126 break;
127 case IPT_CONNBYTES_WHAT_AVGPKT:
128 fputs("avgpkt ", stdout);
129 break;
Harald Welte7dc57e22004-11-18 22:59:36 +0000130 default:
Harald Welte93f4a3d2004-11-18 22:50:01 +0000131 fputs("unknown ", stdout);
Harald Welte7dc57e22004-11-18 22:59:36 +0000132 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +0000133 }
134}
135
136static void print_direction(struct ipt_connbytes_info *sinfo)
137{
138 switch (sinfo->direction) {
139 case IPT_CONNBYTES_DIR_ORIGINAL:
Harald Welte7dc57e22004-11-18 22:59:36 +0000140 fputs("original ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000141 break;
142 case IPT_CONNBYTES_DIR_REPLY:
Harald Welte7dc57e22004-11-18 22:59:36 +0000143 fputs("reply ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000144 break;
145 case IPT_CONNBYTES_DIR_BOTH:
Harald Welte7dc57e22004-11-18 22:59:36 +0000146 fputs("both ", stdout);
147 break;
148 default:
149 fputs("unknown ", stdout);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000150 break;
151 }
Martin Devera766113a2003-06-19 12:23:37 +0000152}
153
154/* Prints out the matchinfo. */
155static void
156print(const struct ipt_ip *ip,
157 const struct ipt_entry_match *match,
158 int numeric)
159{
160 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data;
161
Harald Welte7dc57e22004-11-18 22:59:36 +0000162 if (sinfo->count.from > sinfo->count.to)
163 printf("connbytes ! %llu:%llu ", sinfo->count.to,
164 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000165 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000166 printf("connbytes %llu:%llu ",sinfo->count.from,
167 sinfo->count.to);
Harald Welte93f4a3d2004-11-18 22:50:01 +0000168
169 fputs("connbytes mode ", stdout);
170 print_mode(sinfo);
171
172 fputs("connbytes direction ", stdout);
173 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000174}
175
176/* Saves the matchinfo in parsable form to stdout. */
177static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
178{
179 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data;
180
Harald Welte93f4a3d2004-11-18 22:50:01 +0000181 if (sinfo->count.from > sinfo->count.to)
Harald Welte7dc57e22004-11-18 22:59:36 +0000182 printf("! --connbytes %llu:%llu ", sinfo->count.to,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000183 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000184 else
Harald Welte7dc57e22004-11-18 22:59:36 +0000185 printf("--connbytes %llu:%llu ", sinfo->count.from,
Harald Welte93f4a3d2004-11-18 22:50:01 +0000186 sinfo->count.to);
187
188 fputs("--connbytes-mode ", stdout);
189 print_mode(sinfo);
190
191 fputs("--connbytes-direction ", stdout);
192 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000193}
194
Pablo Neira8caee8b2004-12-28 13:11:59 +0000195static struct iptables_match state = {
196 .next = NULL,
197 .name = "connbytes",
198 .version = IPTABLES_VERSION,
199 .size = IPT_ALIGN(sizeof(struct ipt_connbytes_info)),
200 .userspacesize = IPT_ALIGN(sizeof(struct ipt_connbytes_info)),
201 .help = &help,
202 .init = &init,
203 .parse = &parse,
204 .final_check = &final_check,
205 .print = &print,
206 .save = &save,
207 .extra_opts = opts
Martin Devera766113a2003-06-19 12:23:37 +0000208};
209
210void _init(void)
211{
212 register_match(&state);
213}