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, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 47 | const struct xt_match *match, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 48 | const void *matchinfo, |
| 49 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 50 | unsigned int protoff, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 51 | int *hotdrop) |
| 52 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 53 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 54 | u_int64_t what = 0; /* initialize to make gcc happy */ |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame^] | 55 | u_int64_t bytes = 0; |
| 56 | u_int64_t pkts = 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 57 | const struct ip_conntrack_counter *counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 58 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 59 | if (!(counters = nf_ct_get_counters(skb))) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 60 | return 0; /* no match */ |
| 61 | |
| 62 | switch (sinfo->what) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 63 | case XT_CONNBYTES_PKTS: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 64 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 65 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 66 | what = counters[IP_CT_DIR_ORIGINAL].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_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 69 | what = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 70 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 72 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
| 73 | what += counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 77 | case XT_CONNBYTES_BYTES: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 78 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 80 | what = counters[IP_CT_DIR_ORIGINAL].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_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 83 | what = counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 84 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 85 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 86 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 87 | what += counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 91 | case XT_CONNBYTES_AVGPKT: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 92 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 93 | case XT_CONNBYTES_DIR_ORIGINAL: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame^] | 94 | bytes = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 95 | pkts = counters[IP_CT_DIR_ORIGINAL].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 96 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | case XT_CONNBYTES_DIR_REPLY: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame^] | 98 | bytes = counters[IP_CT_DIR_REPLY].bytes; |
| 99 | pkts = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 100 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 101 | case XT_CONNBYTES_DIR_BOTH: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -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 | break; |
| 107 | } |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame^] | 108 | if (pkts != 0) |
| 109 | what = div64_64(bytes, pkts); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | |
| 113 | if (sinfo->count.to) |
| 114 | return (what <= sinfo->count.to && what >= sinfo->count.from); |
| 115 | else |
| 116 | return (what >= sinfo->count.from); |
| 117 | } |
| 118 | |
| 119 | static int check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 120 | const void *ip, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 121 | const struct xt_match *match, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 122 | void *matchinfo, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 123 | unsigned int hook_mask) |
| 124 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 126 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | if (sinfo->what != XT_CONNBYTES_PKTS && |
| 128 | sinfo->what != XT_CONNBYTES_BYTES && |
| 129 | sinfo->what != XT_CONNBYTES_AVGPKT) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 132 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
| 133 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && |
| 134 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 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); |
| 140 | return 0; |
| 141 | } |
| 142 | |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 146 | static void |
| 147 | destroy(const struct xt_match *match, void *matchinfo) |
| 148 | { |
| 149 | nf_ct_l3proto_module_put(match->family); |
| 150 | } |
| 151 | |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 152 | static struct xt_match xt_connbytes_match[] = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 153 | { |
| 154 | .name = "connbytes", |
| 155 | .family = AF_INET, |
| 156 | .checkentry = check, |
| 157 | .match = match, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 158 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 159 | .matchsize = sizeof(struct xt_connbytes_info), |
| 160 | .me = THIS_MODULE |
| 161 | }, |
| 162 | { |
| 163 | .name = "connbytes", |
| 164 | .family = AF_INET6, |
| 165 | .checkentry = check, |
| 166 | .match = match, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 167 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 168 | .matchsize = sizeof(struct xt_connbytes_info), |
| 169 | .me = THIS_MODULE |
| 170 | }, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 173 | static int __init xt_connbytes_init(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 174 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 175 | return xt_register_matches(xt_connbytes_match, |
| 176 | ARRAY_SIZE(xt_connbytes_match)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 179 | static void __exit xt_connbytes_fini(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 180 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 181 | xt_unregister_matches(xt_connbytes_match, |
| 182 | ARRAY_SIZE(xt_connbytes_match)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 185 | module_init(xt_connbytes_init); |
| 186 | module_exit(xt_connbytes_fini); |