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 <net/netfilter/nf_conntrack_compat.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_connbytes.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");
27 /* 64bit divisor, dividend and result. dynamic precision */
28 static u_int64_t div64_64(u_int64_t dividend, u_int64_t divisor)
30 u_int32_t d = divisor;
32 if (divisor > 0xffffffffULL) {
33 unsigned int shift = fls(divisor >> 32);
44 match(const struct sk_buff *skb,
45 const struct net_device *in,
46 const struct net_device *out,
47 const void *matchinfo,
52 const struct xt_connbytes_info *sinfo = matchinfo;
53 u_int64_t what = 0; /* initialize to make gcc happy */
56 const struct ip_conntrack_counter *counters;
58 if (!(counters = nf_ct_get_counters(skb)))
59 return 0; /* no match */
61 switch (sinfo->what) {
62 case XT_CONNBYTES_PKTS:
63 switch (sinfo->direction) {
64 case XT_CONNBYTES_DIR_ORIGINAL:
65 what = counters[IP_CT_DIR_ORIGINAL].packets;
67 case XT_CONNBYTES_DIR_REPLY:
68 what = counters[IP_CT_DIR_REPLY].packets;
70 case XT_CONNBYTES_DIR_BOTH:
71 what = counters[IP_CT_DIR_ORIGINAL].packets;
72 what += counters[IP_CT_DIR_REPLY].packets;
76 case XT_CONNBYTES_BYTES:
77 switch (sinfo->direction) {
78 case XT_CONNBYTES_DIR_ORIGINAL:
79 what = counters[IP_CT_DIR_ORIGINAL].bytes;
81 case XT_CONNBYTES_DIR_REPLY:
82 what = counters[IP_CT_DIR_REPLY].bytes;
84 case XT_CONNBYTES_DIR_BOTH:
85 what = counters[IP_CT_DIR_ORIGINAL].bytes;
86 what += counters[IP_CT_DIR_REPLY].bytes;
90 case XT_CONNBYTES_AVGPKT:
91 switch (sinfo->direction) {
92 case XT_CONNBYTES_DIR_ORIGINAL:
93 bytes = counters[IP_CT_DIR_ORIGINAL].bytes;
94 pkts = counters[IP_CT_DIR_ORIGINAL].packets;
96 case XT_CONNBYTES_DIR_REPLY:
97 bytes = counters[IP_CT_DIR_REPLY].bytes;
98 pkts = counters[IP_CT_DIR_REPLY].packets;
100 case XT_CONNBYTES_DIR_BOTH:
101 bytes = counters[IP_CT_DIR_ORIGINAL].bytes +
102 counters[IP_CT_DIR_REPLY].bytes;
103 pkts = counters[IP_CT_DIR_ORIGINAL].packets +
104 counters[IP_CT_DIR_REPLY].packets;
108 what = div64_64(bytes, pkts);
113 return (what <= sinfo->count.to && what >= sinfo->count.from);
115 return (what >= sinfo->count.from);
118 static int check(const char *tablename,
121 unsigned int matchsize,
122 unsigned int hook_mask)
124 const struct xt_connbytes_info *sinfo = matchinfo;
126 if (matchsize != XT_ALIGN(sizeof(struct xt_connbytes_info)))
129 if (sinfo->what != XT_CONNBYTES_PKTS &&
130 sinfo->what != XT_CONNBYTES_BYTES &&
131 sinfo->what != XT_CONNBYTES_AVGPKT)
134 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
135 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
136 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
142 static struct xt_match connbytes_match = {
145 .checkentry = &check,
148 static struct xt_match connbytes6_match = {
151 .checkentry = &check,
155 static int __init init(void)
158 ret = xt_register_match(AF_INET, &connbytes_match);
162 ret = xt_register_match(AF_INET6, &connbytes6_match);
164 xt_unregister_match(AF_INET, &connbytes_match);
168 static void __exit fini(void)
170 xt_unregister_match(AF_INET, &connbytes_match);
171 xt_unregister_match(AF_INET6, &connbytes6_match);