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). |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/module.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 5 | #include <linux/bitops.h> |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 6 | #include <linux/skbuff.h> |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 7 | #include <linux/math64.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 8 | #include <linux/netfilter/x_tables.h> |
| 9 | #include <linux/netfilter/xt_connbytes.h> |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 10 | #include <net/netfilter/nf_conntrack.h> |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 11 | #include <net/netfilter/nf_conntrack_acct.h> |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 12 | |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 13 | MODULE_LICENSE("GPL"); |
| 14 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 15 | MODULE_DESCRIPTION("Xtables: Number of packets/bytes per connection matching"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | MODULE_ALIAS("ipt_connbytes"); |
Jan Engelhardt | 73aaf93 | 2007-10-11 14:36:40 -0700 | [diff] [blame] | 17 | MODULE_ALIAS("ip6t_connbytes"); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 18 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 19 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 20 | connbytes_mt(const struct sk_buff *skb, const struct net_device *in, |
| 21 | const struct net_device *out, const struct xt_match *match, |
| 22 | const void *matchinfo, int offset, unsigned int protoff, |
| 23 | bool *hotdrop) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 24 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | const struct xt_connbytes_info *sinfo = matchinfo; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 26 | const struct nf_conn *ct; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 27 | enum ip_conntrack_info ctinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 28 | u_int64_t what = 0; /* initialize to make gcc happy */ |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 29 | u_int64_t bytes = 0; |
| 30 | u_int64_t pkts = 0; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 31 | const struct nf_conn_counter *counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 32 | |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 33 | ct = nf_ct_get(skb, &ctinfo); |
| 34 | if (!ct) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 35 | return false; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 36 | |
| 37 | counters = nf_conn_acct_find(ct); |
| 38 | if (!counters) |
| 39 | return false; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 40 | |
| 41 | switch (sinfo->what) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 42 | case XT_CONNBYTES_PKTS: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 43 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 44 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 45 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 46 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 48 | what = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 49 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 50 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 51 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
| 52 | what += counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 56 | case XT_CONNBYTES_BYTES: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 57 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 58 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 59 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 60 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 61 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 62 | what = counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 63 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 64 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 65 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 66 | what += counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 70 | case XT_CONNBYTES_AVGPKT: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 71 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | case XT_CONNBYTES_DIR_ORIGINAL: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 73 | bytes = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 74 | pkts = counters[IP_CT_DIR_ORIGINAL].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 75 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 76 | case XT_CONNBYTES_DIR_REPLY: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 77 | bytes = counters[IP_CT_DIR_REPLY].bytes; |
| 78 | pkts = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 79 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | case XT_CONNBYTES_DIR_BOTH: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 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; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 87 | if (pkts != 0) |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 88 | what = div64_u64(bytes, pkts); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (sinfo->count.to) |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 93 | return what <= sinfo->count.to && what >= sinfo->count.from; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 94 | else |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 95 | return what >= sinfo->count.from; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 98 | static bool |
| 99 | connbytes_mt_check(const char *tablename, const void *ip, |
| 100 | const struct xt_match *match, void *matchinfo, |
| 101 | unsigned int hook_mask) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 102 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 103 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 104 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 105 | if (sinfo->what != XT_CONNBYTES_PKTS && |
| 106 | sinfo->what != XT_CONNBYTES_BYTES && |
| 107 | sinfo->what != XT_CONNBYTES_AVGPKT) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 108 | return false; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 109 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 110 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
| 111 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && |
| 112 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 113 | return false; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 114 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 115 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
| 116 | printk(KERN_WARNING "can't load conntrack support for " |
Jan Engelhardt | df54aae | 2007-12-17 22:43:15 -0800 | [diff] [blame] | 117 | "proto=%u\n", match->family); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 118 | return false; |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 121 | return true; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 124 | static void |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 125 | connbytes_mt_destroy(const struct xt_match *match, void *matchinfo) |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 126 | { |
| 127 | nf_ct_l3proto_module_put(match->family); |
| 128 | } |
| 129 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 130 | static struct xt_match connbytes_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 131 | { |
| 132 | .name = "connbytes", |
| 133 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 134 | .checkentry = connbytes_mt_check, |
| 135 | .match = connbytes_mt, |
| 136 | .destroy = connbytes_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 137 | .matchsize = sizeof(struct xt_connbytes_info), |
| 138 | .me = THIS_MODULE |
| 139 | }, |
| 140 | { |
| 141 | .name = "connbytes", |
| 142 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 143 | .checkentry = connbytes_mt_check, |
| 144 | .match = connbytes_mt, |
| 145 | .destroy = connbytes_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 146 | .matchsize = sizeof(struct xt_connbytes_info), |
| 147 | .me = THIS_MODULE |
| 148 | }, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 151 | static int __init connbytes_mt_init(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 152 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 153 | return xt_register_matches(connbytes_mt_reg, |
| 154 | ARRAY_SIZE(connbytes_mt_reg)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 157 | static void __exit connbytes_mt_exit(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 158 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 159 | xt_unregister_matches(connbytes_mt_reg, ARRAY_SIZE(connbytes_mt_reg)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 162 | module_init(connbytes_mt_init); |
| 163 | module_exit(connbytes_mt_exit); |