blob: fec9316a1e10609381d783263fa21fed97bd6e09 [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>
Harald Welte2e4e6a12006-01-12 13:30:04 -080015#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter/xt_connbytes.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070017#include <net/netfilter/nf_conntrack.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
Harald Welte9d810fd2005-08-13 13:56:26 -070027static int
28match(const struct sk_buff *skb,
29 const struct net_device *in,
30 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080031 const struct xt_match *match,
Harald Welte9d810fd2005-08-13 13:56:26 -070032 const void *matchinfo,
33 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080034 unsigned int protoff,
Harald Welte9d810fd2005-08-13 13:56:26 -070035 int *hotdrop)
36{
Harald Welte2e4e6a12006-01-12 13:30:04 -080037 const struct xt_connbytes_info *sinfo = matchinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -070038 struct nf_conn *ct;
39 enum ip_conntrack_info ctinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -070040 u_int64_t what = 0; /* initialize to make gcc happy */
Patrick McHardyfb74a8412007-01-30 14:24:29 -080041 u_int64_t bytes = 0;
42 u_int64_t pkts = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043 const struct ip_conntrack_counter *counters;
Harald Welte9d810fd2005-08-13 13:56:26 -070044
Patrick McHardy587aa642007-03-14 16:37:25 -070045 ct = nf_ct_get(skb, &ctinfo);
46 if (!ct)
47 return 0;
48 counters = ct->counters;
Harald Welte9d810fd2005-08-13 13:56:26 -070049
50 switch (sinfo->what) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 case XT_CONNBYTES_PKTS:
Harald Welte9d810fd2005-08-13 13:56:26 -070052 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054 what = counters[IP_CT_DIR_ORIGINAL].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070055 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080056 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 what = counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070058 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080059 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060 what = counters[IP_CT_DIR_ORIGINAL].packets;
61 what += counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070062 break;
63 }
64 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080065 case XT_CONNBYTES_BYTES:
Harald Welte9d810fd2005-08-13 13:56:26 -070066 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080067 case XT_CONNBYTES_DIR_ORIGINAL:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068 what = counters[IP_CT_DIR_ORIGINAL].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070069 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080070 case XT_CONNBYTES_DIR_REPLY:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071 what = counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070072 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080073 case XT_CONNBYTES_DIR_BOTH:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074 what = counters[IP_CT_DIR_ORIGINAL].bytes;
75 what += counters[IP_CT_DIR_REPLY].bytes;
Harald Welte9d810fd2005-08-13 13:56:26 -070076 break;
77 }
78 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079 case XT_CONNBYTES_AVGPKT:
Harald Welte9d810fd2005-08-13 13:56:26 -070080 switch (sinfo->direction) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080081 case XT_CONNBYTES_DIR_ORIGINAL:
Patrick McHardyfb74a8412007-01-30 14:24:29 -080082 bytes = counters[IP_CT_DIR_ORIGINAL].bytes;
83 pkts = counters[IP_CT_DIR_ORIGINAL].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070084 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080085 case XT_CONNBYTES_DIR_REPLY:
Patrick McHardyfb74a8412007-01-30 14:24:29 -080086 bytes = counters[IP_CT_DIR_REPLY].bytes;
87 pkts = counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070088 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080089 case XT_CONNBYTES_DIR_BOTH:
Patrick McHardyfb74a8412007-01-30 14:24:29 -080090 bytes = counters[IP_CT_DIR_ORIGINAL].bytes +
91 counters[IP_CT_DIR_REPLY].bytes;
92 pkts = counters[IP_CT_DIR_ORIGINAL].packets +
93 counters[IP_CT_DIR_REPLY].packets;
Harald Welte9d810fd2005-08-13 13:56:26 -070094 break;
95 }
Patrick McHardyfb74a8412007-01-30 14:24:29 -080096 if (pkts != 0)
97 what = div64_64(bytes, pkts);
Harald Welte9d810fd2005-08-13 13:56:26 -070098 break;
99 }
100
101 if (sinfo->count.to)
102 return (what <= sinfo->count.to && what >= sinfo->count.from);
103 else
104 return (what >= sinfo->count.from);
105}
106
107static int check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -0800109 const struct xt_match *match,
Harald Welte9d810fd2005-08-13 13:56:26 -0700110 void *matchinfo,
Harald Welte9d810fd2005-08-13 13:56:26 -0700111 unsigned int hook_mask)
112{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800113 const struct xt_connbytes_info *sinfo = matchinfo;
Harald Welte9d810fd2005-08-13 13:56:26 -0700114
Harald Welte2e4e6a12006-01-12 13:30:04 -0800115 if (sinfo->what != XT_CONNBYTES_PKTS &&
116 sinfo->what != XT_CONNBYTES_BYTES &&
117 sinfo->what != XT_CONNBYTES_AVGPKT)
Harald Welte9d810fd2005-08-13 13:56:26 -0700118 return 0;
119
Harald Welte2e4e6a12006-01-12 13:30:04 -0800120 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
121 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
122 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
Harald Welte9d810fd2005-08-13 13:56:26 -0700123 return 0;
124
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800125 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
126 printk(KERN_WARNING "can't load conntrack support for "
127 "proto=%d\n", match->family);
128 return 0;
129 }
130
Harald Welte9d810fd2005-08-13 13:56:26 -0700131 return 1;
132}
133
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800134static void
135destroy(const struct xt_match *match, void *matchinfo)
136{
137 nf_ct_l3proto_module_put(match->family);
138}
139
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700140static struct xt_match xt_connbytes_match[] = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700141 {
142 .name = "connbytes",
143 .family = AF_INET,
144 .checkentry = check,
145 .match = match,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800146 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700147 .matchsize = sizeof(struct xt_connbytes_info),
148 .me = THIS_MODULE
149 },
150 {
151 .name = "connbytes",
152 .family = AF_INET6,
153 .checkentry = check,
154 .match = match,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800155 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700156 .matchsize = sizeof(struct xt_connbytes_info),
157 .me = THIS_MODULE
158 },
Harald Welte9d810fd2005-08-13 13:56:26 -0700159};
160
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800161static int __init xt_connbytes_init(void)
Harald Welte9d810fd2005-08-13 13:56:26 -0700162{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700163 return xt_register_matches(xt_connbytes_match,
164 ARRAY_SIZE(xt_connbytes_match));
Harald Welte9d810fd2005-08-13 13:56:26 -0700165}
166
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800167static void __exit xt_connbytes_fini(void)
Harald Welte9d810fd2005-08-13 13:56:26 -0700168{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700169 xt_unregister_matches(xt_connbytes_match,
170 ARRAY_SIZE(xt_connbytes_match));
Harald Welte9d810fd2005-08-13 13:56:26 -0700171}
172
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800173module_init(xt_connbytes_init);
174module_exit(xt_connbytes_fini);