Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * netfilter module to limit the number of parallel tcp |
| 3 | * connections per IP address. |
| 4 | * (c) 2000 Gerd Knorr <kraxel@bytesex.org> |
| 5 | * Nov 2002: Martin Bene <martin.bene@icomedias.com>: |
| 6 | * only ignore TIME_WAIT or gone connections |
Jan Engelhardt | ba5dc27 | 2007-11-05 20:35:56 -0800 | [diff] [blame] | 7 | * (C) CC Computer Consultants GmbH, 2007 |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 8 | * |
| 9 | * based on ... |
| 10 | * |
| 11 | * Kernel module to match connection tracking information. |
| 12 | * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au). |
| 13 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 15 | #include <linux/in.h> |
| 16 | #include <linux/in6.h> |
| 17 | #include <linux/ip.h> |
| 18 | #include <linux/ipv6.h> |
| 19 | #include <linux/jhash.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 21 | #include <linux/list.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/random.h> |
| 24 | #include <linux/skbuff.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/netfilter/nf_conntrack_tcp.h> |
| 27 | #include <linux/netfilter/x_tables.h> |
| 28 | #include <linux/netfilter/xt_connlimit.h> |
| 29 | #include <net/netfilter/nf_conntrack.h> |
| 30 | #include <net/netfilter/nf_conntrack_core.h> |
| 31 | #include <net/netfilter/nf_conntrack_tuple.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 32 | #include <net/netfilter/nf_conntrack_zones.h> |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 33 | |
| 34 | /* we will save the tuples of all connections we care about */ |
| 35 | struct xt_connlimit_conn { |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 36 | struct hlist_node node; |
Changli Gao | 8183e3a | 2011-03-15 13:23:28 +0100 | [diff] [blame] | 37 | struct nf_conntrack_tuple tuple; |
| 38 | union nf_inet_addr addr; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct xt_connlimit_data { |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 42 | struct hlist_head iphash[256]; |
| 43 | spinlock_t lock; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Jan Engelhardt | 294188a | 2010-01-04 16:28:38 +0100 | [diff] [blame] | 46 | static u_int32_t connlimit_rnd __read_mostly; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 47 | |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 48 | static inline unsigned int connlimit_iphash(__be32 addr) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 49 | { |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 50 | return jhash_1word((__force __u32)addr, connlimit_rnd) & 0xFF; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static inline unsigned int |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 54 | connlimit_iphash6(const union nf_inet_addr *addr, |
| 55 | const union nf_inet_addr *mask) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 56 | { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 57 | union nf_inet_addr res; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 58 | unsigned int i; |
| 59 | |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 60 | for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) |
| 61 | res.ip6[i] = addr->ip6[i] & mask->ip6[i]; |
| 62 | |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 63 | return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6), connlimit_rnd) & 0xFF; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static inline bool already_closed(const struct nf_conn *conn) |
| 67 | { |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 68 | if (nf_ct_protonum(conn) == IPPROTO_TCP) |
Dong Wei | d2ee3f2 | 2008-06-04 09:57:51 -0700 | [diff] [blame] | 69 | return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT || |
| 70 | conn->proto.tcp.state == TCP_CONNTRACK_CLOSE; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 71 | else |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline unsigned int |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 76 | same_source_net(const union nf_inet_addr *addr, |
| 77 | const union nf_inet_addr *mask, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 78 | const union nf_inet_addr *u3, u_int8_t family) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 79 | { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 80 | if (family == NFPROTO_IPV4) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 81 | return (addr->ip & mask->ip) == (u3->ip & mask->ip); |
| 82 | } else { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 83 | union nf_inet_addr lh, rh; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 84 | unsigned int i; |
| 85 | |
| 86 | for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) { |
| 87 | lh.ip6[i] = addr->ip6[i] & mask->ip6[i]; |
| 88 | rh.ip6[i] = u3->ip6[i] & mask->ip6[i]; |
| 89 | } |
| 90 | |
| 91 | return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6)) == 0; |
| 92 | } |
| 93 | } |
| 94 | |
Florian Westphal | 15cfd52 | 2014-03-07 14:37:09 +0100 | [diff] [blame] | 95 | static int count_hlist(struct net *net, |
| 96 | struct hlist_head *head, |
| 97 | const struct nf_conntrack_tuple *tuple, |
| 98 | const union nf_inet_addr *addr, |
| 99 | const union nf_inet_addr *mask, |
| 100 | u_int8_t family) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 101 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 102 | const struct nf_conntrack_tuple_hash *found; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 103 | struct xt_connlimit_conn *conn; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 104 | struct hlist_node *n; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 105 | struct nf_conn *found_ct; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 106 | bool addit = true; |
| 107 | int matches = 0; |
| 108 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 109 | rcu_read_lock(); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 110 | |
| 111 | /* check the saved connections */ |
Florian Westphal | 15cfd52 | 2014-03-07 14:37:09 +0100 | [diff] [blame] | 112 | hlist_for_each_entry_safe(conn, n, head, node) { |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 113 | found = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE, |
| 114 | &conn->tuple); |
Florian Westphal | d9ec4f1 | 2014-03-07 14:37:10 +0100 | [diff] [blame^] | 115 | if (found == NULL) { |
| 116 | hlist_del(&conn->node); |
| 117 | kfree(conn); |
| 118 | continue; |
| 119 | } |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 120 | |
Florian Westphal | d9ec4f1 | 2014-03-07 14:37:10 +0100 | [diff] [blame^] | 121 | found_ct = nf_ct_tuplehash_to_ctrack(found); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 122 | |
Florian Westphal | d9ec4f1 | 2014-03-07 14:37:10 +0100 | [diff] [blame^] | 123 | if (nf_ct_tuple_equal(&conn->tuple, tuple)) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Just to be sure we have it only once in the list. |
| 126 | * We should not see tuples twice unless someone hooks |
| 127 | * this into a table without "-p tcp --syn". |
| 128 | */ |
| 129 | addit = false; |
Florian Westphal | d9ec4f1 | 2014-03-07 14:37:10 +0100 | [diff] [blame^] | 130 | } else if (already_closed(found_ct)) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 131 | /* |
| 132 | * we do not care about connections which are |
| 133 | * closed already -> ditch it |
| 134 | */ |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 135 | nf_ct_put(found_ct); |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 136 | hlist_del(&conn->node); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 137 | kfree(conn); |
| 138 | continue; |
| 139 | } |
| 140 | |
Changli Gao | 8183e3a | 2011-03-15 13:23:28 +0100 | [diff] [blame] | 141 | if (same_source_net(addr, mask, &conn->addr, family)) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 142 | /* same source network -> be counted! */ |
| 143 | ++matches; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 144 | nf_ct_put(found_ct); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 147 | rcu_read_unlock(); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 148 | |
| 149 | if (addit) { |
| 150 | /* save the new connection in our list */ |
Changli Gao | 0e23ca1 | 2011-03-15 13:24:56 +0100 | [diff] [blame] | 151 | conn = kmalloc(sizeof(*conn), GFP_ATOMIC); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 152 | if (conn == NULL) |
| 153 | return -ENOMEM; |
| 154 | conn->tuple = *tuple; |
Changli Gao | 8183e3a | 2011-03-15 13:23:28 +0100 | [diff] [blame] | 155 | conn->addr = *addr; |
Florian Westphal | 15cfd52 | 2014-03-07 14:37:09 +0100 | [diff] [blame] | 156 | hlist_add_head(&conn->node, head); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 157 | ++matches; |
| 158 | } |
| 159 | |
| 160 | return matches; |
| 161 | } |
| 162 | |
Florian Westphal | 15cfd52 | 2014-03-07 14:37:09 +0100 | [diff] [blame] | 163 | static int count_them(struct net *net, |
| 164 | struct xt_connlimit_data *data, |
| 165 | const struct nf_conntrack_tuple *tuple, |
| 166 | const union nf_inet_addr *addr, |
| 167 | const union nf_inet_addr *mask, |
| 168 | u_int8_t family) |
| 169 | { |
| 170 | struct hlist_head *hhead; |
| 171 | int count; |
| 172 | u32 hash; |
| 173 | |
| 174 | if (family == NFPROTO_IPV6) |
| 175 | hash = connlimit_iphash6(addr, mask); |
| 176 | else |
| 177 | hash = connlimit_iphash(addr->ip & mask->ip); |
| 178 | |
| 179 | hhead = &data->iphash[hash]; |
| 180 | |
| 181 | spin_lock_bh(&data->lock); |
| 182 | count = count_hlist(net, hhead, tuple, addr, mask, family); |
| 183 | spin_unlock_bh(&data->lock); |
| 184 | |
| 185 | return count; |
| 186 | } |
| 187 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 188 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 189 | connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 190 | { |
Alexey Dobriyan | 83fc810 | 2010-01-18 08:07:50 +0100 | [diff] [blame] | 191 | struct net *net = dev_net(par->in ? par->in : par->out); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 192 | const struct xt_connlimit_info *info = par->matchinfo; |
Jan Engelhardt | 22c2d8b | 2007-12-17 22:44:47 -0800 | [diff] [blame] | 193 | union nf_inet_addr addr; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 194 | struct nf_conntrack_tuple tuple; |
| 195 | const struct nf_conntrack_tuple *tuple_ptr = &tuple; |
| 196 | enum ip_conntrack_info ctinfo; |
| 197 | const struct nf_conn *ct; |
| 198 | int connections; |
| 199 | |
| 200 | ct = nf_ct_get(skb, &ctinfo); |
Changli Gao | 8183e3a | 2011-03-15 13:23:28 +0100 | [diff] [blame] | 201 | if (ct != NULL) |
| 202 | tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 203 | else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), |
| 204 | par->family, &tuple)) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 205 | goto hotdrop; |
| 206 | |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 207 | if (par->family == NFPROTO_IPV6) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 208 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
Jan Engelhardt | cc4fc02 | 2011-01-18 17:32:40 +0100 | [diff] [blame] | 209 | memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ? |
| 210 | &iph->daddr : &iph->saddr, sizeof(addr.ip6)); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 211 | } else { |
| 212 | const struct iphdr *iph = ip_hdr(skb); |
Jan Engelhardt | cc4fc02 | 2011-01-18 17:32:40 +0100 | [diff] [blame] | 213 | addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ? |
| 214 | iph->daddr : iph->saddr; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Alexey Dobriyan | 83fc810 | 2010-01-18 08:07:50 +0100 | [diff] [blame] | 217 | connections = count_them(net, info->data, tuple_ptr, &addr, |
Stefan Berger | 20b7975 | 2011-02-14 16:54:33 +0100 | [diff] [blame] | 218 | &info->mask, par->family); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 219 | |
Richard Weinberger | 1cc34c3 | 2011-01-18 01:36:57 +0100 | [diff] [blame] | 220 | if (connections < 0) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 221 | /* kmalloc failed, drop it entirely */ |
Richard Weinberger | 1cc34c3 | 2011-01-18 01:36:57 +0100 | [diff] [blame] | 222 | goto hotdrop; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 223 | |
Jan Engelhardt | cc4fc02 | 2011-01-18 17:32:40 +0100 | [diff] [blame] | 224 | return (connections > info->limit) ^ |
| 225 | !!(info->flags & XT_CONNLIMIT_INVERT); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 226 | |
| 227 | hotdrop: |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 228 | par->hotdrop = true; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 232 | static int connlimit_mt_check(const struct xt_mtchk_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 233 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 234 | struct xt_connlimit_info *info = par->matchinfo; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 235 | unsigned int i; |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 236 | int ret; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 237 | |
Changli Gao | 4656c4d | 2011-03-15 13:26:32 +0100 | [diff] [blame] | 238 | if (unlikely(!connlimit_rnd)) { |
| 239 | u_int32_t rand; |
| 240 | |
| 241 | do { |
| 242 | get_random_bytes(&rand, sizeof(rand)); |
| 243 | } while (!rand); |
| 244 | cmpxchg(&connlimit_rnd, 0, rand); |
Jan Engelhardt | 294188a | 2010-01-04 16:28:38 +0100 | [diff] [blame] | 245 | } |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 246 | ret = nf_ct_l3proto_try_module_get(par->family); |
| 247 | if (ret < 0) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 248 | pr_info("cannot load conntrack support for " |
| 249 | "address family %u\n", par->family); |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 250 | return ret; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* init private data */ |
| 254 | info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL); |
| 255 | if (info->data == NULL) { |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 256 | nf_ct_l3proto_module_put(par->family); |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 257 | return -ENOMEM; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | spin_lock_init(&info->data->lock); |
| 261 | for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 262 | INIT_HLIST_HEAD(&info->data->iphash[i]); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 263 | |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 264 | return 0; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 267 | static void connlimit_mt_destroy(const struct xt_mtdtor_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 268 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 269 | const struct xt_connlimit_info *info = par->matchinfo; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 270 | struct xt_connlimit_conn *conn; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 271 | struct hlist_node *n; |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 272 | struct hlist_head *hash = info->data->iphash; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 273 | unsigned int i; |
| 274 | |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 275 | nf_ct_l3proto_module_put(par->family); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 276 | |
| 277 | for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 278 | hlist_for_each_entry_safe(conn, n, &hash[i], node) { |
Changli Gao | 3e0d514 | 2011-03-15 13:25:42 +0100 | [diff] [blame] | 279 | hlist_del(&conn->node); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 280 | kfree(conn); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | kfree(info->data); |
| 285 | } |
| 286 | |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 287 | static struct xt_match connlimit_mt_reg __read_mostly = { |
| 288 | .name = "connlimit", |
| 289 | .revision = 1, |
| 290 | .family = NFPROTO_UNSPEC, |
| 291 | .checkentry = connlimit_mt_check, |
| 292 | .match = connlimit_mt, |
| 293 | .matchsize = sizeof(struct xt_connlimit_info), |
| 294 | .destroy = connlimit_mt_destroy, |
| 295 | .me = THIS_MODULE, |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 298 | static int __init connlimit_mt_init(void) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 299 | { |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 300 | return xt_register_match(&connlimit_mt_reg); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 303 | static void __exit connlimit_mt_exit(void) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 304 | { |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 305 | xt_unregister_match(&connlimit_mt_reg); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 308 | module_init(connlimit_mt_init); |
| 309 | module_exit(connlimit_mt_exit); |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 310 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 311 | MODULE_DESCRIPTION("Xtables: Number of connections matching"); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 312 | MODULE_LICENSE("GPL"); |
| 313 | MODULE_ALIAS("ipt_connlimit"); |
| 314 | MODULE_ALIAS("ip6t_connlimit"); |