blob: d725e8b845031ce761f36cc9e787dcd2aa43c5d5 [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,
Patrick McHardyc4986732006-03-20 18:02:56 -080047 const struct xt_match *match,
Harald Welte9d810fd2005-08-13 13:56:26 -070048 const void *matchinfo,
49 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 unsigned int protoff,
Harald Welte9d810fd2005-08-13 13:56:26 -070051 int *hotdrop)
52{
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 const struct xt_connbytes_info *sinfo = matchinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -070054 u_int64_t what = 0; /* initialize to make gcc happy */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055 const struct ip_conntrack_counter *counters;
Harald Welte9d810fd2005-08-13 13:56:26 -070056
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 if (!(counters = nf_ct_get_counters(skb)))
Harald Welte9d810fd2005-08-13 13:56:26 -070058 return 0; /* no match */
59
60 switch (sinfo->what) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080061 case XT_CONNBYTES_PKTS:
Harald Welte9d810fd2005-08-13 13:56:26 -070062 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080063 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064 what = counters[IP_CT_DIR_ORIGINAL].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070065 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080066 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 what = counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070068 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080069 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070 what = counters[IP_CT_DIR_ORIGINAL].packets;
71 what += counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070072 break;
73 }
74 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075 case XT_CONNBYTES_BYTES:
Harald Welte9d810fd2005-08-13 13:56:26 -070076 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078 what = counters[IP_CT_DIR_ORIGINAL].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070079 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080080 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080081 what = counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070082 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080083 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080084 what = counters[IP_CT_DIR_ORIGINAL].bytes;
85 what += counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070086 break;
87 }
88 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080089 case XT_CONNBYTES_AVGPKT:
Harald Welte9d810fd2005-08-13 13:56:26 -070090 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080091 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092 what = div64_64(counters[IP_CT_DIR_ORIGINAL].bytes,
93 counters[IP_CT_DIR_ORIGINAL].packets);
Harald Welte9d810fd2005-08-13 13:56:26 -070094 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080095 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080096 what = div64_64(counters[IP_CT_DIR_REPLY].bytes,
97 counters[IP_CT_DIR_REPLY].packets);
Harald Welte9d810fd2005-08-13 13:56:26 -070098 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 case XT_CONNBYTES_DIR_BOTH:
Harald Welte9d810fd2005-08-13 13:56:26 -0700100 {
101 u_int64_t bytes;
102 u_int64_t pkts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800103 bytes = counters[IP_CT_DIR_ORIGINAL].bytes +
104 counters[IP_CT_DIR_REPLY].bytes;
105 pkts = counters[IP_CT_DIR_ORIGINAL].packets+
106 counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -0700107
108 /* FIXME_THEORETICAL: what to do if sum
109 * overflows ? */
110
111 what = div64_64(bytes, pkts);
112 }
113 break;
114 }
115 break;
116 }
117
118 if (sinfo->count.to)
119 return (what <= sinfo->count.to && what >= sinfo->count.from);
120 else
121 return (what >= sinfo->count.from);
122}
123
124static int check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -0800126 const struct xt_match *match,
Harald Welte9d810fd2005-08-13 13:56:26 -0700127 void *matchinfo,
128 unsigned int matchsize,
129 unsigned int hook_mask)
130{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131 const struct xt_connbytes_info *sinfo = matchinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -0700132
Harald Welte2e4e6a12006-01-12 13:30:04 -0800133 if (sinfo->what != XT_CONNBYTES_PKTS &&
134 sinfo->what != XT_CONNBYTES_BYTES &&
135 sinfo->what != XT_CONNBYTES_AVGPKT)
Harald Welte9d810fd2005-08-13 13:56:26 -0700136 return 0;
137
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
139 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
140 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
Harald Welte9d810fd2005-08-13 13:56:26 -0700141 return 0;
142
143 return 1;
144}
145
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700146static struct xt_match xt_connbytes_match[] = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700147 {
148 .name = "connbytes",
149 .family = AF_INET,
150 .checkentry = check,
151 .match = match,
152 .matchsize = sizeof(struct xt_connbytes_info),
153 .me = THIS_MODULE
154 },
155 {
156 .name = "connbytes",
157 .family = AF_INET6,
158 .checkentry = check,
159 .match = match,
160 .matchsize = sizeof(struct xt_connbytes_info),
161 .me = THIS_MODULE
162 },
Harald Welte9d810fd2005-08-13 13:56:26 -0700163};
164
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800165static int __init xt_connbytes_init(void)
Harald Welte9d810fd2005-08-13 13:56:26 -0700166{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700167 return xt_register_matches(xt_connbytes_match,
168 ARRAY_SIZE(xt_connbytes_match));
Harald Welte9d810fd2005-08-13 13:56:26 -0700169}
170
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800171static void __exit xt_connbytes_fini(void)
Harald Welte9d810fd2005-08-13 13:56:26 -0700172{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700173 xt_unregister_matches(xt_connbytes_match,
174 ARRAY_SIZE(xt_connbytes_match));
Harald Welte9d810fd2005-08-13 13:56:26 -0700175}
176
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800177module_init(xt_connbytes_init);
178module_exit(xt_connbytes_fini);