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