Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 1 | /* Kernel module to match connection tracking byte counter. |
| 2 | * GPL (C) 2002 Martin Devera (devik@cdi.cz). |
| 3 | * |
| 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 |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 9 | * 2005-10-16 Harald Welte <laforge@netfilter.org> |
| 10 | * - Port to x_tables |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 15 | #include <net/netfilter/nf_conntrack_compat.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 16 | #include <linux/netfilter/x_tables.h> |
| 17 | #include <linux/netfilter/xt_connbytes.h> |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/div64.h> |
| 20 | #include <asm/bitops.h> |
| 21 | |
| 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"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 25 | MODULE_ALIAS("ipt_connbytes"); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 26 | |
| 27 | /* 64bit divisor, dividend and result. dynamic precision */ |
Patrick McHardy | 8ffde67 | 2005-08-13 13:57:58 -0700 | [diff] [blame] | 28 | static u_int64_t div64_64(u_int64_t dividend, u_int64_t divisor) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 29 | { |
Patrick McHardy | 8ffde67 | 2005-08-13 13:57:58 -0700 | [diff] [blame] | 30 | u_int32_t d = divisor; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 31 | |
Patrick McHardy | 8ffde67 | 2005-08-13 13:57:58 -0700 | [diff] [blame] | 32 | if (divisor > 0xffffffffULL) { |
| 33 | unsigned int shift = fls(divisor >> 32); |
| 34 | |
| 35 | d = divisor >> shift; |
| 36 | dividend >>= shift; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Patrick McHardy | 8ffde67 | 2005-08-13 13:57:58 -0700 | [diff] [blame] | 39 | do_div(dividend, d); |
| 40 | return dividend; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int |
| 44 | match(const struct sk_buff *skb, |
| 45 | const struct net_device *in, |
| 46 | const struct net_device *out, |
| 47 | const void *matchinfo, |
| 48 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 49 | unsigned int protoff, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 50 | int *hotdrop) |
| 51 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 52 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 53 | u_int64_t what = 0; /* initialize to make gcc happy */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 54 | const struct ip_conntrack_counter *counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 55 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 56 | if (!(counters = nf_ct_get_counters(skb))) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 57 | return 0; /* no match */ |
| 58 | |
| 59 | switch (sinfo->what) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 60 | case XT_CONNBYTES_PKTS: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 61 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 62 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 63 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 64 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 65 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 66 | what = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 67 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 68 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 69 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
| 70 | what += counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 74 | case XT_CONNBYTES_BYTES: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 75 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 76 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 77 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 78 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 79 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 80 | what = counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 81 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 82 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 83 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 84 | what += counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 88 | case XT_CONNBYTES_AVGPKT: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 89 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 90 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 91 | what = div64_64(counters[IP_CT_DIR_ORIGINAL].bytes, |
| 92 | counters[IP_CT_DIR_ORIGINAL].packets); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 93 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 94 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 95 | what = div64_64(counters[IP_CT_DIR_REPLY].bytes, |
| 96 | counters[IP_CT_DIR_REPLY].packets); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 97 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 98 | case XT_CONNBYTES_DIR_BOTH: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 99 | { |
| 100 | u_int64_t bytes; |
| 101 | u_int64_t pkts; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 106 | |
| 107 | /* FIXME_THEORETICAL: what to do if sum |
| 108 | * overflows ? */ |
| 109 | |
| 110 | what = div64_64(bytes, pkts); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | if (sinfo->count.to) |
| 118 | return (what <= sinfo->count.to && what >= sinfo->count.from); |
| 119 | else |
| 120 | return (what >= sinfo->count.from); |
| 121 | } |
| 122 | |
| 123 | static int check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 124 | const void *ip, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 125 | void *matchinfo, |
| 126 | unsigned int matchsize, |
| 127 | unsigned int hook_mask) |
| 128 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 129 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 130 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 131 | if (matchsize != XT_ALIGN(sizeof(struct xt_connbytes_info))) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 134 | if (sinfo->what != XT_CONNBYTES_PKTS && |
| 135 | sinfo->what != XT_CONNBYTES_BYTES && |
| 136 | sinfo->what != XT_CONNBYTES_AVGPKT) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 139 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
| 140 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && |
| 141 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 147 | static struct xt_match connbytes_match = { |
| 148 | .name = "connbytes", |
| 149 | .match = &match, |
| 150 | .checkentry = &check, |
| 151 | .me = THIS_MODULE |
| 152 | }; |
| 153 | static struct xt_match connbytes6_match = { |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 154 | .name = "connbytes", |
| 155 | .match = &match, |
| 156 | .checkentry = &check, |
| 157 | .me = THIS_MODULE |
| 158 | }; |
| 159 | |
| 160 | static int __init init(void) |
| 161 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 162 | int ret; |
| 163 | ret = xt_register_match(AF_INET, &connbytes_match); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | ret = xt_register_match(AF_INET6, &connbytes6_match); |
| 168 | if (ret) |
| 169 | xt_unregister_match(AF_INET, &connbytes_match); |
| 170 | return ret; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void __exit fini(void) |
| 174 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 175 | xt_unregister_match(AF_INET, &connbytes_match); |
| 176 | xt_unregister_match(AF_INET6, &connbytes6_match); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | module_init(init); |
| 180 | module_exit(fini); |