1 /* Kernel module to match connection tracking byte counter.
2 * GPL (C) 2002 Martin Devera (devik@cdi.cz).
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
9 * 2005-10-16 Harald Welte <laforge@netfilter.org>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter/xt_connbytes.h>
17 #include <net/netfilter/nf_conntrack.h>
19 #include <asm/div64.h>
20 #include <asm/bitops.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
24 MODULE_DESCRIPTION("iptables match for matching number of pkts/bytes per connection");
25 MODULE_ALIAS("ipt_connbytes");
28 match(const struct sk_buff *skb,
29 const struct net_device *in,
30 const struct net_device *out,
31 const struct xt_match *match,
32 const void *matchinfo,
37 const struct xt_connbytes_info *sinfo = matchinfo;
39 enum ip_conntrack_info ctinfo;
40 u_int64_t what = 0; /* initialize to make gcc happy */
43 const struct ip_conntrack_counter *counters;
45 ct = nf_ct_get(skb, &ctinfo);
48 counters = ct->counters;
50 switch (sinfo->what) {
51 case XT_CONNBYTES_PKTS:
52 switch (sinfo->direction) {
53 case XT_CONNBYTES_DIR_ORIGINAL:
54 what = counters[IP_CT_DIR_ORIGINAL].packets;
56 case XT_CONNBYTES_DIR_REPLY:
57 what = counters[IP_CT_DIR_REPLY].packets;
59 case XT_CONNBYTES_DIR_BOTH:
60 what = counters[IP_CT_DIR_ORIGINAL].packets;
61 what += counters[IP_CT_DIR_REPLY].packets;
65 case XT_CONNBYTES_BYTES:
66 switch (sinfo->direction) {
67 case XT_CONNBYTES_DIR_ORIGINAL:
68 what = counters[IP_CT_DIR_ORIGINAL].bytes;
70 case XT_CONNBYTES_DIR_REPLY:
71 what = counters[IP_CT_DIR_REPLY].bytes;
73 case XT_CONNBYTES_DIR_BOTH:
74 what = counters[IP_CT_DIR_ORIGINAL].bytes;
75 what += counters[IP_CT_DIR_REPLY].bytes;
79 case XT_CONNBYTES_AVGPKT:
80 switch (sinfo->direction) {
81 case XT_CONNBYTES_DIR_ORIGINAL:
82 bytes = counters[IP_CT_DIR_ORIGINAL].bytes;
83 pkts = counters[IP_CT_DIR_ORIGINAL].packets;
85 case XT_CONNBYTES_DIR_REPLY:
86 bytes = counters[IP_CT_DIR_REPLY].bytes;
87 pkts = counters[IP_CT_DIR_REPLY].packets;
89 case XT_CONNBYTES_DIR_BOTH:
90 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;
97 what = div64_64(bytes, pkts);
102 return (what <= sinfo->count.to && what >= sinfo->count.from);
104 return (what >= sinfo->count.from);
107 static int check(const char *tablename,
109 const struct xt_match *match,
111 unsigned int hook_mask)
113 const struct xt_connbytes_info *sinfo = matchinfo;
115 if (sinfo->what != XT_CONNBYTES_PKTS &&
116 sinfo->what != XT_CONNBYTES_BYTES &&
117 sinfo->what != XT_CONNBYTES_AVGPKT)
120 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
121 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
122 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
125 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);
135 destroy(const struct xt_match *match, void *matchinfo)
137 nf_ct_l3proto_module_put(match->family);
140 static struct xt_match xt_connbytes_match[] = {
147 .matchsize = sizeof(struct xt_connbytes_info),
156 .matchsize = sizeof(struct xt_connbytes_info),
161 static int __init xt_connbytes_init(void)
163 return xt_register_matches(xt_connbytes_match,
164 ARRAY_SIZE(xt_connbytes_match));
167 static void __exit xt_connbytes_fini(void)
169 xt_unregister_matches(xt_connbytes_match,
170 ARRAY_SIZE(xt_connbytes_match));
173 module_init(xt_connbytes_init);
174 module_exit(xt_connbytes_fini);