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