]>
Commit | Line | Data |
---|---|---|
9d810fd2 HW |
1 | /* Kernel module to match connection tracking byte counter. |
2 | * GPL (C) 2002 Martin Devera (devik@cdi.cz). | |
9d810fd2 | 3 | */ |
8bee4bad | 4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9d810fd2 | 5 | #include <linux/module.h> |
1977f032 | 6 | #include <linux/bitops.h> |
9d810fd2 | 7 | #include <linux/skbuff.h> |
6f6d6a1a | 8 | #include <linux/math64.h> |
2e4e6a17 HW |
9 | #include <linux/netfilter/x_tables.h> |
10 | #include <linux/netfilter/xt_connbytes.h> | |
587aa641 | 11 | #include <net/netfilter/nf_conntrack.h> |
58401572 | 12 | #include <net/netfilter/nf_conntrack_acct.h> |
9d810fd2 | 13 | |
9d810fd2 HW |
14 | MODULE_LICENSE("GPL"); |
15 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
2ae15b64 | 16 | MODULE_DESCRIPTION("Xtables: Number of packets/bytes per connection matching"); |
2e4e6a17 | 17 | MODULE_ALIAS("ipt_connbytes"); |
73aaf935 | 18 | MODULE_ALIAS("ip6t_connbytes"); |
9d810fd2 | 19 | |
1d93a9cb | 20 | static bool |
62fc8051 | 21 | connbytes_mt(const struct sk_buff *skb, struct xt_action_param *par) |
9d810fd2 | 22 | { |
f7108a20 | 23 | const struct xt_connbytes_info *sinfo = par->matchinfo; |
a47362a2 | 24 | const struct nf_conn *ct; |
587aa641 | 25 | enum ip_conntrack_info ctinfo; |
9d810fd2 | 26 | u_int64_t what = 0; /* initialize to make gcc happy */ |
fb74a841 PM |
27 | u_int64_t bytes = 0; |
28 | u_int64_t pkts = 0; | |
58401572 | 29 | const struct nf_conn_counter *counters; |
9d810fd2 | 30 | |
587aa641 PM |
31 | ct = nf_ct_get(skb, &ctinfo); |
32 | if (!ct) | |
1d93a9cb | 33 | return false; |
58401572 KPO |
34 | |
35 | counters = nf_conn_acct_find(ct); | |
36 | if (!counters) | |
37 | return false; | |
9d810fd2 HW |
38 | |
39 | switch (sinfo->what) { | |
2e4e6a17 | 40 | case XT_CONNBYTES_PKTS: |
9d810fd2 | 41 | switch (sinfo->direction) { |
2e4e6a17 | 42 | case XT_CONNBYTES_DIR_ORIGINAL: |
b3e0bfa7 | 43 | what = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].packets); |
9d810fd2 | 44 | break; |
2e4e6a17 | 45 | case XT_CONNBYTES_DIR_REPLY: |
b3e0bfa7 | 46 | what = atomic64_read(&counters[IP_CT_DIR_REPLY].packets); |
9d810fd2 | 47 | break; |
2e4e6a17 | 48 | case XT_CONNBYTES_DIR_BOTH: |
b3e0bfa7 ED |
49 | what = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].packets); |
50 | what += atomic64_read(&counters[IP_CT_DIR_REPLY].packets); | |
9d810fd2 HW |
51 | break; |
52 | } | |
53 | break; | |
2e4e6a17 | 54 | case XT_CONNBYTES_BYTES: |
9d810fd2 | 55 | switch (sinfo->direction) { |
2e4e6a17 | 56 | case XT_CONNBYTES_DIR_ORIGINAL: |
b3e0bfa7 | 57 | what = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].bytes); |
9d810fd2 | 58 | break; |
2e4e6a17 | 59 | case XT_CONNBYTES_DIR_REPLY: |
b3e0bfa7 | 60 | what = atomic64_read(&counters[IP_CT_DIR_REPLY].bytes); |
9d810fd2 | 61 | break; |
2e4e6a17 | 62 | case XT_CONNBYTES_DIR_BOTH: |
b3e0bfa7 ED |
63 | what = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].bytes); |
64 | what += atomic64_read(&counters[IP_CT_DIR_REPLY].bytes); | |
9d810fd2 HW |
65 | break; |
66 | } | |
67 | break; | |
2e4e6a17 | 68 | case XT_CONNBYTES_AVGPKT: |
9d810fd2 | 69 | switch (sinfo->direction) { |
2e4e6a17 | 70 | case XT_CONNBYTES_DIR_ORIGINAL: |
b3e0bfa7 ED |
71 | bytes = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].bytes); |
72 | pkts = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].packets); | |
9d810fd2 | 73 | break; |
2e4e6a17 | 74 | case XT_CONNBYTES_DIR_REPLY: |
b3e0bfa7 ED |
75 | bytes = atomic64_read(&counters[IP_CT_DIR_REPLY].bytes); |
76 | pkts = atomic64_read(&counters[IP_CT_DIR_REPLY].packets); | |
9d810fd2 | 77 | break; |
2e4e6a17 | 78 | case XT_CONNBYTES_DIR_BOTH: |
b3e0bfa7 ED |
79 | bytes = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].bytes) + |
80 | atomic64_read(&counters[IP_CT_DIR_REPLY].bytes); | |
81 | pkts = atomic64_read(&counters[IP_CT_DIR_ORIGINAL].packets) + | |
82 | atomic64_read(&counters[IP_CT_DIR_REPLY].packets); | |
9d810fd2 HW |
83 | break; |
84 | } | |
fb74a841 | 85 | if (pkts != 0) |
6f6d6a1a | 86 | what = div64_u64(bytes, pkts); |
9d810fd2 HW |
87 | break; |
88 | } | |
89 | ||
0354b48f | 90 | if (sinfo->count.to >= sinfo->count.from) |
7c4e36bc | 91 | return what <= sinfo->count.to && what >= sinfo->count.from; |
0354b48f FW |
92 | else /* inverted */ |
93 | return what < sinfo->count.to || what > sinfo->count.from; | |
9d810fd2 HW |
94 | } |
95 | ||
b0f38452 | 96 | static int connbytes_mt_check(const struct xt_mtchk_param *par) |
9d810fd2 | 97 | { |
9b4fce7a | 98 | const struct xt_connbytes_info *sinfo = par->matchinfo; |
4a5a5c73 | 99 | int ret; |
9d810fd2 | 100 | |
2e4e6a17 HW |
101 | if (sinfo->what != XT_CONNBYTES_PKTS && |
102 | sinfo->what != XT_CONNBYTES_BYTES && | |
103 | sinfo->what != XT_CONNBYTES_AVGPKT) | |
bd414ee6 | 104 | return -EINVAL; |
9d810fd2 | 105 | |
2e4e6a17 HW |
106 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
107 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && | |
108 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) | |
bd414ee6 | 109 | return -EINVAL; |
9d810fd2 | 110 | |
4a5a5c73 | 111 | ret = nf_ct_l3proto_try_module_get(par->family); |
f95c74e3 | 112 | if (ret < 0) |
8bee4bad JE |
113 | pr_info("cannot load conntrack support for proto=%u\n", |
114 | par->family); | |
a8756201 TG |
115 | |
116 | /* | |
117 | * This filter cannot function correctly unless connection tracking | |
118 | * accounting is enabled, so complain in the hope that someone notices. | |
119 | */ | |
120 | if (!nf_ct_acct_enabled(par->net)) { | |
121 | pr_warning("Forcing CT accounting to be enabled\n"); | |
122 | nf_ct_set_acct(par->net, true); | |
123 | } | |
124 | ||
f95c74e3 | 125 | return ret; |
9d810fd2 HW |
126 | } |
127 | ||
6be3d859 | 128 | static void connbytes_mt_destroy(const struct xt_mtdtor_param *par) |
11078c37 | 129 | { |
92f3b2b1 | 130 | nf_ct_l3proto_module_put(par->family); |
11078c37 YK |
131 | } |
132 | ||
92f3b2b1 JE |
133 | static struct xt_match connbytes_mt_reg __read_mostly = { |
134 | .name = "connbytes", | |
135 | .revision = 0, | |
136 | .family = NFPROTO_UNSPEC, | |
137 | .checkentry = connbytes_mt_check, | |
138 | .match = connbytes_mt, | |
139 | .destroy = connbytes_mt_destroy, | |
140 | .matchsize = sizeof(struct xt_connbytes_info), | |
141 | .me = THIS_MODULE, | |
9d810fd2 HW |
142 | }; |
143 | ||
d3c5ee6d | 144 | static int __init connbytes_mt_init(void) |
9d810fd2 | 145 | { |
92f3b2b1 | 146 | return xt_register_match(&connbytes_mt_reg); |
9d810fd2 HW |
147 | } |
148 | ||
d3c5ee6d | 149 | static void __exit connbytes_mt_exit(void) |
9d810fd2 | 150 | { |
92f3b2b1 | 151 | xt_unregister_match(&connbytes_mt_reg); |
9d810fd2 HW |
152 | } |
153 | ||
d3c5ee6d JE |
154 | module_init(connbytes_mt_init); |
155 | module_exit(connbytes_mt_exit); |