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 struct xt_match
*match
,
48 const void *matchinfo
,
53 const struct xt_connbytes_info
*sinfo
= matchinfo
;
54 u_int64_t what
= 0; /* initialize to make gcc happy */
57 const struct ip_conntrack_counter
*counters
;
59 if (!(counters
= nf_ct_get_counters(skb
)))
60 return 0; /* no match */
62 switch (sinfo
->what
) {
63 case XT_CONNBYTES_PKTS
:
64 switch (sinfo
->direction
) {
65 case XT_CONNBYTES_DIR_ORIGINAL
:
66 what
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
68 case XT_CONNBYTES_DIR_REPLY
:
69 what
= counters
[IP_CT_DIR_REPLY
].packets
;
71 case XT_CONNBYTES_DIR_BOTH
:
72 what
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
73 what
+= counters
[IP_CT_DIR_REPLY
].packets
;
77 case XT_CONNBYTES_BYTES
:
78 switch (sinfo
->direction
) {
79 case XT_CONNBYTES_DIR_ORIGINAL
:
80 what
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
82 case XT_CONNBYTES_DIR_REPLY
:
83 what
= counters
[IP_CT_DIR_REPLY
].bytes
;
85 case XT_CONNBYTES_DIR_BOTH
:
86 what
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
87 what
+= counters
[IP_CT_DIR_REPLY
].bytes
;
91 case XT_CONNBYTES_AVGPKT
:
92 switch (sinfo
->direction
) {
93 case XT_CONNBYTES_DIR_ORIGINAL
:
94 bytes
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
95 pkts
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
97 case XT_CONNBYTES_DIR_REPLY
:
98 bytes
= counters
[IP_CT_DIR_REPLY
].bytes
;
99 pkts
= counters
[IP_CT_DIR_REPLY
].packets
;
101 case XT_CONNBYTES_DIR_BOTH
:
102 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
;
109 what
= div64_64(bytes
, pkts
);
114 return (what
<= sinfo
->count
.to
&& what
>= sinfo
->count
.from
);
116 return (what
>= sinfo
->count
.from
);
119 static int check(const char *tablename
,
121 const struct xt_match
*match
,
123 unsigned int hook_mask
)
125 const struct xt_connbytes_info
*sinfo
= matchinfo
;
127 if (sinfo
->what
!= XT_CONNBYTES_PKTS
&&
128 sinfo
->what
!= XT_CONNBYTES_BYTES
&&
129 sinfo
->what
!= XT_CONNBYTES_AVGPKT
)
132 if (sinfo
->direction
!= XT_CONNBYTES_DIR_ORIGINAL
&&
133 sinfo
->direction
!= XT_CONNBYTES_DIR_REPLY
&&
134 sinfo
->direction
!= XT_CONNBYTES_DIR_BOTH
)
137 if (nf_ct_l3proto_try_module_get(match
->family
) < 0) {
138 printk(KERN_WARNING
"can't load conntrack support for "
139 "proto=%d\n", match
->family
);
147 destroy(const struct xt_match
*match
, void *matchinfo
)
149 nf_ct_l3proto_module_put(match
->family
);
152 static struct xt_match xt_connbytes_match
[] = {
159 .matchsize
= sizeof(struct xt_connbytes_info
),
168 .matchsize
= sizeof(struct xt_connbytes_info
),
173 static int __init
xt_connbytes_init(void)
175 return xt_register_matches(xt_connbytes_match
,
176 ARRAY_SIZE(xt_connbytes_match
));
179 static void __exit
xt_connbytes_fini(void)
181 xt_unregister_matches(xt_connbytes_match
,
182 ARRAY_SIZE(xt_connbytes_match
));
185 module_init(xt_connbytes_init
);
186 module_exit(xt_connbytes_fini
);