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> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 15 | #include <linux/netfilter/x_tables.h> |
| 16 | #include <linux/netfilter/xt_connbytes.h> |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame^] | 17 | #include <net/netfilter/nf_conntrack.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 | |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 27 | static int |
| 28 | match(const struct sk_buff *skb, |
| 29 | const struct net_device *in, |
| 30 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 31 | const struct xt_match *match, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 32 | const void *matchinfo, |
| 33 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 34 | unsigned int protoff, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 35 | int *hotdrop) |
| 36 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | const struct xt_connbytes_info *sinfo = matchinfo; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame^] | 38 | struct nf_conn *ct; |
| 39 | enum ip_conntrack_info ctinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 40 | u_int64_t what = 0; /* initialize to make gcc happy */ |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 41 | u_int64_t bytes = 0; |
| 42 | u_int64_t pkts = 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 43 | const struct ip_conntrack_counter *counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 44 | |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame^] | 45 | ct = nf_ct_get(skb, &ctinfo); |
| 46 | if (!ct) |
| 47 | return 0; |
| 48 | counters = ct->counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 49 | |
| 50 | switch (sinfo->what) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 51 | case XT_CONNBYTES_PKTS: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 52 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 53 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 54 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 55 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 56 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 57 | what = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 58 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | case XT_CONNBYTES_DIR_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 60 | what = counters[IP_CT_DIR_ORIGINAL].packets; |
| 61 | what += counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 65 | case XT_CONNBYTES_BYTES: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 66 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | case XT_CONNBYTES_DIR_ORIGINAL: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 68 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 69 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 70 | case XT_CONNBYTES_DIR_REPLY: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 71 | what = counters[IP_CT_DIR_REPLY].bytes; |
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_BOTH: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 74 | what = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 75 | what += counters[IP_CT_DIR_REPLY].bytes; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | case XT_CONNBYTES_AVGPKT: |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 80 | switch (sinfo->direction) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 81 | case XT_CONNBYTES_DIR_ORIGINAL: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 82 | bytes = counters[IP_CT_DIR_ORIGINAL].bytes; |
| 83 | pkts = counters[IP_CT_DIR_ORIGINAL].packets; |
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_REPLY: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 86 | bytes = counters[IP_CT_DIR_REPLY].bytes; |
| 87 | pkts = counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 88 | break; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 89 | case XT_CONNBYTES_DIR_BOTH: |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 90 | bytes = counters[IP_CT_DIR_ORIGINAL].bytes + |
| 91 | counters[IP_CT_DIR_REPLY].bytes; |
| 92 | pkts = counters[IP_CT_DIR_ORIGINAL].packets + |
| 93 | counters[IP_CT_DIR_REPLY].packets; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 94 | break; |
| 95 | } |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 96 | if (pkts != 0) |
| 97 | what = div64_64(bytes, pkts); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 98 | break; |
| 99 | } |
| 100 | |
| 101 | if (sinfo->count.to) |
| 102 | return (what <= sinfo->count.to && what >= sinfo->count.from); |
| 103 | else |
| 104 | return (what >= sinfo->count.from); |
| 105 | } |
| 106 | |
| 107 | static int check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 108 | const void *ip, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 109 | const struct xt_match *match, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 110 | void *matchinfo, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 111 | unsigned int hook_mask) |
| 112 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 113 | const struct xt_connbytes_info *sinfo = matchinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 114 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 115 | if (sinfo->what != XT_CONNBYTES_PKTS && |
| 116 | sinfo->what != XT_CONNBYTES_BYTES && |
| 117 | sinfo->what != XT_CONNBYTES_AVGPKT) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 120 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
| 121 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && |
| 122 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 125 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
| 126 | printk(KERN_WARNING "can't load conntrack support for " |
| 127 | "proto=%d\n", match->family); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 134 | static void |
| 135 | destroy(const struct xt_match *match, void *matchinfo) |
| 136 | { |
| 137 | nf_ct_l3proto_module_put(match->family); |
| 138 | } |
| 139 | |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 140 | static struct xt_match xt_connbytes_match[] = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 141 | { |
| 142 | .name = "connbytes", |
| 143 | .family = AF_INET, |
| 144 | .checkentry = check, |
| 145 | .match = match, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 146 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 147 | .matchsize = sizeof(struct xt_connbytes_info), |
| 148 | .me = THIS_MODULE |
| 149 | }, |
| 150 | { |
| 151 | .name = "connbytes", |
| 152 | .family = AF_INET6, |
| 153 | .checkentry = check, |
| 154 | .match = match, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 155 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 156 | .matchsize = sizeof(struct xt_connbytes_info), |
| 157 | .me = THIS_MODULE |
| 158 | }, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 161 | static int __init xt_connbytes_init(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 162 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 163 | return xt_register_matches(xt_connbytes_match, |
| 164 | ARRAY_SIZE(xt_connbytes_match)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 167 | static void __exit xt_connbytes_fini(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 168 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 169 | xt_unregister_matches(xt_connbytes_match, |
| 170 | ARRAY_SIZE(xt_connbytes_match)); |
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 | module_init(xt_connbytes_init); |
| 174 | module_exit(xt_connbytes_fini); |