blob: 6f97bcb0b4b280d997ed199d5fe62204d24848e3 [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
Martin Josefsson11460882004-05-08 14:02:36 +000043 si->from = strtoul(arg,&colon,10);
Martin Devera766113a2003-06-19 12:23:37 +000044 if (*colon != ':')
45 exit_error(PARAMETER_PROBLEM, "Bad range `%s'", arg);
Martin Josefsson11460882004-05-08 14:02:36 +000046 si->to = strtoul(colon+1,&p,10);
Martin Devera766113a2003-06-19 12:23:37 +000047 if (p == colon+1) {
48 /* second number omited */
49 si->to = 0xffffffff;
50 }
51 if (si->from > si->to)
52 exit_error(PARAMETER_PROBLEM, "%lu should be less than %lu", si->from,si->to);
53}
54
55/* Function which parses command options; returns true if it
56 ate an option */
57static int
58parse(int c, char **argv, int invert, unsigned int *flags,
59 const struct ipt_entry *entry,
60 unsigned int *nfcache,
61 struct ipt_entry_match **match)
62{
63 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)(*match)->data;
Martin Josefsson11460882004-05-08 14:02:36 +000064 unsigned long i;
Martin Devera766113a2003-06-19 12:23:37 +000065
66 switch (c) {
67 case '1':
68 if (check_inverse(optarg, &invert, optind, 0))
69 optind++;
70
71 parse_range(argv[optind-1], sinfo);
72 if (invert) {
Harald Welte93f4a3d2004-11-18 22:50:01 +000073 i = sinfo->count.from;
74 sinfo->count.from = sinfo->to;
75 sinfo->count.to = i;
Martin Devera766113a2003-06-19 12:23:37 +000076 }
Harald Welte93f4a3d2004-11-18 22:50:01 +000077 *flags |= 1;
Martin Devera766113a2003-06-19 12:23:37 +000078 break;
Harald Welte93f4a3d2004-11-18 22:50:01 +000079 case '2':
80 if (!strcmp(optarg, "original"))
81 sinfo->direction = IPT_CONNBYTES_DIR_ORIGINAL;
82 else if (!strcmp(optarg, "reply"))
83 sinfo->direction = IPT_CONNBYTES_DIR_REPLY;
84 else if (!strcmp(optarg, "both"))
85 sinfo->direction = IPT_CONNBYTES_DIR_BOTH;
86 else
87 exit_error(PARAMETER_PROBLEM,
88 "Unknown --connbytes-dir `%s'", optarg);
Martin Devera766113a2003-06-19 12:23:37 +000089
Harald Welte93f4a3d2004-11-18 22:50:01 +000090 *flags |= 2;
91 break;
92 case '3':
93 if (!stcmp(optarg, "packets"))
94 sinfo->what = IPT_CONNBYTES_WHAT_PKTS;
95 else if (!strcmp(optarg, "bytes"))
96 sinfo->what = IPT_CONNBYTES_WHAT_BYTES;
97 else if (!strcmp(optarg, "avgpkt"))
98 sinfo->what = IPT_CONNBYTES_WHAT_AVGPKT;
99 else
100 exit_error(PARAMETER_PROBLEM,
101 "Unknown --connbytes-mode `%s'", optarg);
102 *flags |= 4;
Martin Devera766113a2003-06-19 12:23:37 +0000103 default:
104 return 0;
105 }
106
107 return 1;
108}
109
110static void final_check(unsigned int flags)
111{
Harald Welte93f4a3d2004-11-18 22:50:01 +0000112 if (flags != 7)
113 exit_error(PARAMETER_PROBLEM, "You must specify `--connbytes'"
114 "`--connbytes-direction' and `--connbytes-mode'");
115}
116
117static void print_mode(struct ipt_connbytes_info *sinfo)
118{
119 switch (sinfo->what) {
120 case IPT_CONNBYTES_WHAT_PKTS:
121 fputs("packets ", stdout);
122 break;
123 case IPT_CONNBYTES_WHAT_BYTES:
124 fputs("bytes ", stdout);
125 break;
126 case IPT_CONNBYTES_WHAT_AVGPKT:
127 fputs("avgpkt ", stdout);
128 break;
129 case default:
130 fputs("unknown ", stdout);
131 }
132}
133
134static void print_direction(struct ipt_connbytes_info *sinfo)
135{
136 switch (sinfo->direction) {
137 case IPT_CONNBYTES_DIR_ORIGINAL:
138 fputs("original ");
139 break;
140 case IPT_CONNBYTES_DIR_REPLY:
141 fputs("reply ");
142 break;
143 case IPT_CONNBYTES_DIR_BOTH:
144 fputs("both ");
145 break;
146 }
Martin Devera766113a2003-06-19 12:23:37 +0000147}
148
149/* Prints out the matchinfo. */
150static void
151print(const struct ipt_ip *ip,
152 const struct ipt_entry_match *match,
153 int numeric)
154{
155 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data;
156
157 if (sinfo->from > sinfo->to)
Youza Youzovic161b35f2004-08-24 18:59:05 +0000158 printf("connbytes ! %lu:%lu ",sinfo->to,sinfo->from);
Martin Devera766113a2003-06-19 12:23:37 +0000159 else
Youza Youzovic161b35f2004-08-24 18:59:05 +0000160 printf("connbytes %lu:%lu ",sinfo->from,sinfo->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. */
170static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
171{
172 struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data;
173
Harald Welte93f4a3d2004-11-18 22:50:01 +0000174 if (sinfo->count.from > sinfo->count.to)
175 printf("! --connbytes %lu:%lu ", sinfo->count.to,
176 sinfo->count.from);
Martin Devera766113a2003-06-19 12:23:37 +0000177 else
Harald Welte93f4a3d2004-11-18 22:50:01 +0000178 printf("--connbytes %lu:%lu ", sinfo->count.from,
179 sinfo->count.to);
180
181 fputs("--connbytes-mode ", stdout);
182 print_mode(sinfo);
183
184 fputs("--connbytes-direction ", stdout);
185 print_direction(sinfo);
Martin Devera766113a2003-06-19 12:23:37 +0000186}
187
188static
189struct iptables_match state
190= { NULL,
191 "connbytes",
192 IPTABLES_VERSION,
193 IPT_ALIGN(sizeof(struct ipt_connbytes_info)),
194 IPT_ALIGN(sizeof(struct ipt_connbytes_info)),
195 &help,
196 &init,
197 &parse,
198 &final_check,
199 &print,
200 &save,
201 opts
202};
203
204void _init(void)
205{
206 register_match(&state);
207}