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/bitops.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter/x_tables.h>
8 #include <linux/netfilter/xt_connbytes.h>
9 #include <net/netfilter/nf_conntrack.h>
11 #include <asm/div64.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");
17 MODULE_ALIAS("ip6t_connbytes");
20 match(const struct sk_buff
*skb
,
21 const struct net_device
*in
,
22 const struct net_device
*out
,
23 const struct xt_match
*match
,
24 const void *matchinfo
,
29 const struct xt_connbytes_info
*sinfo
= matchinfo
;
30 const struct nf_conn
*ct
;
31 enum ip_conntrack_info ctinfo
;
32 u_int64_t what
= 0; /* initialize to make gcc happy */
35 const struct ip_conntrack_counter
*counters
;
37 ct
= nf_ct_get(skb
, &ctinfo
);
40 counters
= ct
->counters
;
42 switch (sinfo
->what
) {
43 case XT_CONNBYTES_PKTS
:
44 switch (sinfo
->direction
) {
45 case XT_CONNBYTES_DIR_ORIGINAL
:
46 what
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
48 case XT_CONNBYTES_DIR_REPLY
:
49 what
= counters
[IP_CT_DIR_REPLY
].packets
;
51 case XT_CONNBYTES_DIR_BOTH
:
52 what
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
53 what
+= counters
[IP_CT_DIR_REPLY
].packets
;
57 case XT_CONNBYTES_BYTES
:
58 switch (sinfo
->direction
) {
59 case XT_CONNBYTES_DIR_ORIGINAL
:
60 what
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
62 case XT_CONNBYTES_DIR_REPLY
:
63 what
= counters
[IP_CT_DIR_REPLY
].bytes
;
65 case XT_CONNBYTES_DIR_BOTH
:
66 what
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
67 what
+= counters
[IP_CT_DIR_REPLY
].bytes
;
71 case XT_CONNBYTES_AVGPKT
:
72 switch (sinfo
->direction
) {
73 case XT_CONNBYTES_DIR_ORIGINAL
:
74 bytes
= counters
[IP_CT_DIR_ORIGINAL
].bytes
;
75 pkts
= counters
[IP_CT_DIR_ORIGINAL
].packets
;
77 case XT_CONNBYTES_DIR_REPLY
:
78 bytes
= counters
[IP_CT_DIR_REPLY
].bytes
;
79 pkts
= counters
[IP_CT_DIR_REPLY
].packets
;
81 case XT_CONNBYTES_DIR_BOTH
:
82 bytes
= counters
[IP_CT_DIR_ORIGINAL
].bytes
+
83 counters
[IP_CT_DIR_REPLY
].bytes
;
84 pkts
= counters
[IP_CT_DIR_ORIGINAL
].packets
+
85 counters
[IP_CT_DIR_REPLY
].packets
;
89 what
= div64_64(bytes
, pkts
);
94 return what
<= sinfo
->count
.to
&& what
>= sinfo
->count
.from
;
96 return what
>= sinfo
->count
.from
;
99 static bool check(const char *tablename
,
101 const struct xt_match
*match
,
103 unsigned int hook_mask
)
105 const struct xt_connbytes_info
*sinfo
= matchinfo
;
107 if (sinfo
->what
!= XT_CONNBYTES_PKTS
&&
108 sinfo
->what
!= XT_CONNBYTES_BYTES
&&
109 sinfo
->what
!= XT_CONNBYTES_AVGPKT
)
112 if (sinfo
->direction
!= XT_CONNBYTES_DIR_ORIGINAL
&&
113 sinfo
->direction
!= XT_CONNBYTES_DIR_REPLY
&&
114 sinfo
->direction
!= XT_CONNBYTES_DIR_BOTH
)
117 if (nf_ct_l3proto_try_module_get(match
->family
) < 0) {
118 printk(KERN_WARNING
"can't load conntrack support for "
119 "proto=%d\n", match
->family
);
127 destroy(const struct xt_match
*match
, void *matchinfo
)
129 nf_ct_l3proto_module_put(match
->family
);
132 static struct xt_match xt_connbytes_match
[] __read_mostly
= {
139 .matchsize
= sizeof(struct xt_connbytes_info
),
148 .matchsize
= sizeof(struct xt_connbytes_info
),
153 static int __init
xt_connbytes_init(void)
155 return xt_register_matches(xt_connbytes_match
,
156 ARRAY_SIZE(xt_connbytes_match
));
159 static void __exit
xt_connbytes_fini(void)
161 xt_unregister_matches(xt_connbytes_match
,
162 ARRAY_SIZE(xt_connbytes_match
));
165 module_init(xt_connbytes_init
);
166 module_exit(xt_connbytes_fini
);