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> |
| 5 | #include <linux/skbuff.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 6 | #include <linux/netfilter/x_tables.h> |
| 7 | #include <linux/netfilter/xt_connbytes.h> |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 8 | #include <net/netfilter/nf_conntrack.h> |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 9 | |
| 10 | #include <asm/div64.h> |
| 11 | #include <asm/bitops.h> |
| 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"); |
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 |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 19 | match(const struct sk_buff *skb, |
| 20 | const struct net_device *in, |
| 21 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 22 | const struct xt_match *match, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 23 | const void *matchinfo, |
| 24 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 26 | bool *hotdrop) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 27 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 28 | const struct xt_connbytes_info *sinfo = matchinfo; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 29 | const struct nf_conn *ct; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 30 | enum ip_conntrack_info ctinfo; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 31 | u_int64_t what = 0; /* initialize to make gcc happy */ |
Patrick McHardy | fb74a841 | 2007-01-30 14:24:29 -0800 | [diff] [blame] | 32 | u_int64_t bytes = 0; |
| 33 | u_int64_t pkts = 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 34 | const struct ip_conntrack_counter *counters; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 35 | |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 36 | ct = nf_ct_get(skb, &ctinfo); |
| 37 | if (!ct) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 38 | return false; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 39 | counters = ct->counters; |
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) |
| 88 | what = div64_64(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 | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 98 | static bool check(const char *tablename, |
| 99 | const void *ip, |
| 100 | const struct xt_match *match, |
| 101 | void *matchinfo, |
| 102 | unsigned int hook_mask) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 103 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 104 | const struct xt_connbytes_info *sinfo = matchinfo; |
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->what != XT_CONNBYTES_PKTS && |
| 107 | sinfo->what != XT_CONNBYTES_BYTES && |
| 108 | sinfo->what != XT_CONNBYTES_AVGPKT) |
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 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 111 | if (sinfo->direction != XT_CONNBYTES_DIR_ORIGINAL && |
| 112 | sinfo->direction != XT_CONNBYTES_DIR_REPLY && |
| 113 | sinfo->direction != XT_CONNBYTES_DIR_BOTH) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 114 | return false; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 115 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 116 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
| 117 | printk(KERN_WARNING "can't load conntrack support for " |
| 118 | "proto=%d\n", match->family); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 119 | return false; |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 122 | return true; |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 125 | static void |
| 126 | destroy(const struct xt_match *match, void *matchinfo) |
| 127 | { |
| 128 | nf_ct_l3proto_module_put(match->family); |
| 129 | } |
| 130 | |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 131 | static struct xt_match xt_connbytes_match[] = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 132 | { |
| 133 | .name = "connbytes", |
| 134 | .family = AF_INET, |
| 135 | .checkentry = check, |
| 136 | .match = match, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 137 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 138 | .matchsize = sizeof(struct xt_connbytes_info), |
| 139 | .me = THIS_MODULE |
| 140 | }, |
| 141 | { |
| 142 | .name = "connbytes", |
| 143 | .family = AF_INET6, |
| 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 | }, |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 152 | static int __init xt_connbytes_init(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 153 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 154 | return xt_register_matches(xt_connbytes_match, |
| 155 | ARRAY_SIZE(xt_connbytes_match)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 158 | static void __exit xt_connbytes_fini(void) |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 159 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 160 | xt_unregister_matches(xt_connbytes_match, |
| 161 | ARRAY_SIZE(xt_connbytes_match)); |
Harald Welte | 9d810fd | 2005-08-13 13:56:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 164 | module_init(xt_connbytes_init); |
| 165 | module_exit(xt_connbytes_fini); |