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