blob: 150d2a4b0f71059edaad4a8995a794f7fe456030 [file] [log] [blame]
Harald Welte9d810fd2005-08-13 13:56:26 -07001/* Kernel module to match connection tracking byte counter.
2 * GPL (C) 2002 Martin Devera (devik@cdi.cz).
3 *
4 * 2004-07-20 Harald Welte <laforge@netfilter.org>
5 * - reimplemented to use per-connection accounting counters
6 * - add functionality to match number of packets
7 * - add functionality to match average packet size
8 * - add support to match directions seperately
Harald Welte2e4e6a12006-01-12 13:30:04 -08009 * 2005-10-16 Harald Welte <laforge@netfilter.org>
10 * - Port to x_tables
Harald Welte9d810fd2005-08-13 13:56:26 -070011 *
12 */
13#include <linux/module.h>
14#include <linux/skbuff.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080015#include <net/netfilter/nf_conntrack_compat.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080016#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter/xt_connbytes.h>
Harald Welte9d810fd2005-08-13 13:56:26 -070018
19#include <asm/div64.h>
20#include <asm/bitops.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
24MODULE_DESCRIPTION("iptables match for matching number of pkts/bytes per connection");
Harald Welte2e4e6a12006-01-12 13:30:04 -080025MODULE_ALIAS("ipt_connbytes");
Harald Welte9d810fd2005-08-13 13:56:26 -070026
27/* 64bit divisor, dividend and result. dynamic precision */
Patrick McHardy8ffde672005-08-13 13:57:58 -070028static u_int64_t div64_64(u_int64_t dividend, u_int64_t divisor)
Harald Welte9d810fd2005-08-13 13:56:26 -070029{
Patrick McHardy8ffde672005-08-13 13:57:58 -070030 u_int32_t d = divisor;
Harald Welte9d810fd2005-08-13 13:56:26 -070031
Patrick McHardy8ffde672005-08-13 13:57:58 -070032 if (divisor > 0xffffffffULL) {
33 unsigned int shift = fls(divisor >> 32);
34
35 d = divisor >> shift;
36 dividend >>= shift;
Harald Welte9d810fd2005-08-13 13:56:26 -070037 }
38
Patrick McHardy8ffde672005-08-13 13:57:58 -070039 do_div(dividend, d);
40 return dividend;
Harald Welte9d810fd2005-08-13 13:56:26 -070041}
42
43static int
44match(const struct sk_buff *skb,
45 const struct net_device *in,
46 const struct net_device *out,
47 const void *matchinfo,
48 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080049 unsigned int protoff,
Harald Welte9d810fd2005-08-13 13:56:26 -070050 int *hotdrop)
51{
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 const struct xt_connbytes_info *sinfo = matchinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -070053 u_int64_t what = 0; /* initialize to make gcc happy */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054 const struct ip_conntrack_counter *counters;
Harald Welte9d810fd2005-08-13 13:56:26 -070055
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 if (!(counters = nf_ct_get_counters(skb)))
Harald Welte9d810fd2005-08-13 13:56:26 -070057 return 0; /* no match */
58
59 switch (sinfo->what) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 case XT_CONNBYTES_PKTS:
Harald Welte9d810fd2005-08-13 13:56:26 -070061 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080062 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063 what = counters[IP_CT_DIR_ORIGINAL].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070064 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080065 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066 what = counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070067 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080068 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069 what = counters[IP_CT_DIR_ORIGINAL].packets;
70 what += counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070071 break;
72 }
73 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080074 case XT_CONNBYTES_BYTES:
Harald Welte9d810fd2005-08-13 13:56:26 -070075 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080076 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077 what = counters[IP_CT_DIR_ORIGINAL].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070078 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080 what = counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070081 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080082 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083 what = counters[IP_CT_DIR_ORIGINAL].bytes;
84 what += counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070085 break;
86 }
87 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080088 case XT_CONNBYTES_AVGPKT:
Harald Welte9d810fd2005-08-13 13:56:26 -070089 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080090 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091 what = div64_64(counters[IP_CT_DIR_ORIGINAL].bytes,
92 counters[IP_CT_DIR_ORIGINAL].packets);
Harald Welte9d810fd2005-08-13 13:56:26 -070093 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080094 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 what = div64_64(counters[IP_CT_DIR_REPLY].bytes,
96 counters[IP_CT_DIR_REPLY].packets);
Harald Welte9d810fd2005-08-13 13:56:26 -070097 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080098 case XT_CONNBYTES_DIR_BOTH:
Harald Welte9d810fd2005-08-13 13:56:26 -070099 {
100 u_int64_t bytes;
101 u_int64_t pkts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102 bytes = counters[IP_CT_DIR_ORIGINAL].bytes +
103 counters[IP_CT_DIR_REPLY].bytes;
104 pkts = counters[IP_CT_DIR_ORIGINAL].packets+
105 counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -0700106
107 /* FIXME_THEORETICAL: what to do if sum
108 * overflows ? */
109
110 what = div64_64(bytes, pkts);
111 }
112 break;
113 }
114 break;
115 }
116
117 if (sinfo->count.to)
118 return (what <= sinfo->count.to && what >= sinfo->count.from);
119 else
120 return (what >= sinfo->count.from);
121}
122
123static int check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124 const void *ip,
Harald Welte9d810fd2005-08-13 13:56:26 -0700125 void *matchinfo,
126 unsigned int matchsize,
127 unsigned int hook_mask)
128{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129 const struct xt_connbytes_info *sinfo = matchinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -0700130
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131 if (matchsize != XT_ALIGN(sizeof(struct xt_connbytes_info)))
Harald Welte9d810fd2005-08-13 13:56:26 -0700132 return 0;
133
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134 if (sinfo->what != XT_CONNBYTES_PKTS &&
135 sinfo->what != XT_CONNBYTES_BYTES &&
136 sinfo->what != XT_CONNBYTES_AVGPKT)
Harald Welte9d810fd2005-08-13 13:56:26 -0700137 return 0;
138
Harald Welte2e4e6a12006-01-12 13:30:04 -0800139 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
140 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
141 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
Harald Welte9d810fd2005-08-13 13:56:26 -0700142 return 0;
143
144 return 1;
145}
146
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147static struct xt_match connbytes_match = {
148 .name = "connbytes",
149 .match = &match,
150 .checkentry = &check,
151 .me = THIS_MODULE
152};
153static struct xt_match connbytes6_match = {
Harald Welte9d810fd2005-08-13 13:56:26 -0700154 .name = "connbytes",
155 .match = &match,
156 .checkentry = &check,
157 .me = THIS_MODULE
158};
159
160static int __init init(void)
161{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800162 int ret;
163 ret = xt_register_match(AF_INET, &connbytes_match);
164 if (ret)
165 return ret;
166
167 ret = xt_register_match(AF_INET6, &connbytes6_match);
168 if (ret)
169 xt_unregister_match(AF_INET, &connbytes_match);
170 return ret;
Harald Welte9d810fd2005-08-13 13:56:26 -0700171}
172
173static void __exit fini(void)
174{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800175 xt_unregister_match(AF_INET, &connbytes_match);
176 xt_unregister_match(AF_INET6, &connbytes6_match);
Harald Welte9d810fd2005-08-13 13:56:26 -0700177}
178
179module_init(init);
180module_exit(fini);