1 /* Kernel module to match connection tracking byte counter.
2 * GPL (C) 2002 Martin Devera (devik@cdi.cz).
4 #include <linux/module.h>
5 #include <linux/skbuff.h>
6 #include <linux/netfilter/x_tables.h>
7 #include <linux/netfilter/xt_connbytes.h>
8 #include <net/netfilter/nf_conntrack.h>
10 #include <asm/div64.h>
11 #include <asm/bitops.h>
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
15 MODULE_DESCRIPTION("iptables match for matching number of pkts/bytes per connection");
16 MODULE_ALIAS("ipt_connbytes");
19 match(const struct sk_buff *skb,
20 const struct net_device *in,
21 const struct net_device *out,
22 const struct xt_match *match,
23 const void *matchinfo,
28 const struct xt_connbytes_info *sinfo = matchinfo;
29 const struct nf_conn *ct;
30 enum ip_conntrack_info ctinfo;
31 u_int64_t what = 0; /* initialize to make gcc happy */
34 const struct ip_conntrack_counter *counters;
36 ct = nf_ct_get(skb, &ctinfo);
39 counters = ct->counters;
41 switch (sinfo->what) {
42 case XT_CONNBYTES_PKTS:
43 switch (sinfo->direction) {
44 case XT_CONNBYTES_DIR_ORIGINAL:
45 what = counters[IP_CT_DIR_ORIGINAL].packets;
47 case XT_CONNBYTES_DIR_REPLY:
48 what = counters[IP_CT_DIR_REPLY].packets;
50 case XT_CONNBYTES_DIR_BOTH:
51 what = counters[IP_CT_DIR_ORIGINAL].packets;
52 what += counters[IP_CT_DIR_REPLY].packets;
56 case XT_CONNBYTES_BYTES:
57 switch (sinfo->direction) {
58 case XT_CONNBYTES_DIR_ORIGINAL:
59 what = counters[IP_CT_DIR_ORIGINAL].bytes;
61 case XT_CONNBYTES_DIR_REPLY:
62 what = counters[IP_CT_DIR_REPLY].bytes;
64 case XT_CONNBYTES_DIR_BOTH:
65 what = counters[IP_CT_DIR_ORIGINAL].bytes;
66 what += counters[IP_CT_DIR_REPLY].bytes;
70 case XT_CONNBYTES_AVGPKT:
71 switch (sinfo->direction) {
72 case XT_CONNBYTES_DIR_ORIGINAL:
73 bytes = counters[IP_CT_DIR_ORIGINAL].bytes;
74 pkts = counters[IP_CT_DIR_ORIGINAL].packets;
76 case XT_CONNBYTES_DIR_REPLY:
77 bytes = counters[IP_CT_DIR_REPLY].bytes;
78 pkts = counters[IP_CT_DIR_REPLY].packets;
80 case XT_CONNBYTES_DIR_BOTH:
81 bytes = counters[IP_CT_DIR_ORIGINAL].bytes +
82 counters[IP_CT_DIR_REPLY].bytes;
83 pkts = counters[IP_CT_DIR_ORIGINAL].packets +
84 counters[IP_CT_DIR_REPLY].packets;
88 what = div64_64(bytes, pkts);
93 return what <= sinfo->count.to && what >= sinfo->count.from;
95 return what >= sinfo->count.from;
98 static bool check(const char *tablename,
100 const struct xt_match *match,
102 unsigned int hook_mask)
104 const struct xt_connbytes_info *sinfo = matchinfo;
106 if (sinfo->what != XT_CONNBYTES_PKTS &&
107 sinfo->what != XT_CONNBYTES_BYTES &&
108 sinfo->what != XT_CONNBYTES_AVGPKT)
111 if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL &&
112 sinfo->direction != XT_CONNBYTES_DIR_REPLY &&
113 sinfo->direction != XT_CONNBYTES_DIR_BOTH)
116 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
117 printk(KERN_WARNING "can't load conntrack support for "
118 "proto=%d\n", match->family);
126 destroy(const struct xt_match *match, void *matchinfo)
128 nf_ct_l3proto_module_put(match->family);
131 static struct xt_match xt_connbytes_match[] __read_mostly = {
138 .matchsize = sizeof(struct xt_connbytes_info),
147 .matchsize = sizeof(struct xt_connbytes_info),
152 static int __init xt_connbytes_init(void)
154 return xt_register_matches(xt_connbytes_match,
155 ARRAY_SIZE(xt_connbytes_match));
158 static void __exit xt_connbytes_fini(void)
160 xt_unregister_matches(xt_connbytes_match,
161 ARRAY_SIZE(xt_connbytes_match));
164 module_init(xt_connbytes_init);
165 module_exit(xt_connbytes_fini);