xt_connlimit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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
  7. * (C) CC Computer Consultants GmbH, 2007
  8. *
  9. * based on ...
  10. *
  11. * Kernel module to match connection tracking information.
  12. * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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>
  20. #include <linux/slab.h>
  21. #include <linux/list.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/module.h>
  24. #include <linux/random.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/netfilter/nf_conntrack_tcp.h>
  28. #include <linux/netfilter/x_tables.h>
  29. #include <linux/netfilter/xt_connlimit.h>
  30. #include <net/netfilter/nf_conntrack.h>
  31. #include <net/netfilter/nf_conntrack_core.h>
  32. #include <net/netfilter/nf_conntrack_tuple.h>
  33. #include <net/netfilter/nf_conntrack_zones.h>
  34. #define CONNLIMIT_SLOTS 256U
  35. #ifdef CONFIG_LOCKDEP
  36. #define CONNLIMIT_LOCK_SLOTS 8U
  37. #else
  38. #define CONNLIMIT_LOCK_SLOTS 256U
  39. #endif
  40. #define CONNLIMIT_GC_MAX_NODES 8
  41. /* we will save the tuples of all connections we care about */
  42. struct xt_connlimit_conn {
  43. struct hlist_node node;
  44. struct nf_conntrack_tuple tuple;
  45. union nf_inet_addr addr;
  46. };
  47. struct xt_connlimit_rb {
  48. struct rb_node node;
  49. struct hlist_head hhead; /* connections/hosts in same subnet */
  50. union nf_inet_addr addr; /* search key */
  51. };
  52. static spinlock_t xt_connlimit_locks[CONNLIMIT_LOCK_SLOTS] __cacheline_aligned_in_smp;
  53. struct xt_connlimit_data {
  54. struct rb_root climit_root4[CONNLIMIT_SLOTS];
  55. struct rb_root climit_root6[CONNLIMIT_SLOTS];
  56. };
  57. static u_int32_t connlimit_rnd __read_mostly;
  58. static struct kmem_cache *connlimit_rb_cachep __read_mostly;
  59. static struct kmem_cache *connlimit_conn_cachep __read_mostly;
  60. static inline unsigned int connlimit_iphash(__be32 addr)
  61. {
  62. return jhash_1word((__force __u32)addr,
  63. connlimit_rnd) % CONNLIMIT_SLOTS;
  64. }
  65. static inline unsigned int
  66. connlimit_iphash6(const union nf_inet_addr *addr,
  67. const union nf_inet_addr *mask)
  68. {
  69. union nf_inet_addr res;
  70. unsigned int i;
  71. for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
  72. res.ip6[i] = addr->ip6[i] & mask->ip6[i];
  73. return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6),
  74. connlimit_rnd) % CONNLIMIT_SLOTS;
  75. }
  76. static inline bool already_closed(const struct nf_conn *conn)
  77. {
  78. if (nf_ct_protonum(conn) == IPPROTO_TCP)
  79. return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
  80. conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
  81. else
  82. return 0;
  83. }
  84. static int
  85. same_source_net(const union nf_inet_addr *addr,
  86. const union nf_inet_addr *mask,
  87. const union nf_inet_addr *u3, u_int8_t family)
  88. {
  89. if (family == NFPROTO_IPV4) {
  90. return ntohl(addr->ip & mask->ip) -
  91. ntohl(u3->ip & mask->ip);
  92. } else {
  93. union nf_inet_addr lh, rh;
  94. unsigned int i;
  95. for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
  96. lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
  97. rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
  98. }
  99. return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6));
  100. }
  101. }
  102. static bool add_hlist(struct hlist_head *head,
  103. const struct nf_conntrack_tuple *tuple,
  104. const union nf_inet_addr *addr)
  105. {
  106. struct xt_connlimit_conn *conn;
  107. conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
  108. if (conn == NULL)
  109. return false;
  110. conn->tuple = *tuple;
  111. conn->addr = *addr;
  112. hlist_add_head(&conn->node, head);
  113. return true;
  114. }
  115. static unsigned int check_hlist(struct net *net,
  116. struct hlist_head *head,
  117. const struct nf_conntrack_tuple *tuple,
  118. const struct nf_conntrack_zone *zone,
  119. bool *addit)
  120. {
  121. const struct nf_conntrack_tuple_hash *found;
  122. struct xt_connlimit_conn *conn;
  123. struct hlist_node *n;
  124. struct nf_conn *found_ct;
  125. unsigned int length = 0;
  126. *addit = true;
  127. rcu_read_lock();
  128. /* check the saved connections */
  129. hlist_for_each_entry_safe(conn, n, head, node) {
  130. found = nf_conntrack_find_get(net, zone, &conn->tuple);
  131. if (found == NULL) {
  132. hlist_del(&conn->node);
  133. kmem_cache_free(connlimit_conn_cachep, conn);
  134. continue;
  135. }
  136. found_ct = nf_ct_tuplehash_to_ctrack(found);
  137. if (nf_ct_tuple_equal(&conn->tuple, tuple)) {
  138. /*
  139. * Just to be sure we have it only once in the list.
  140. * We should not see tuples twice unless someone hooks
  141. * this into a table without "-p tcp --syn".
  142. */
  143. *addit = false;
  144. } else if (already_closed(found_ct)) {
  145. /*
  146. * we do not care about connections which are
  147. * closed already -> ditch it
  148. */
  149. nf_ct_put(found_ct);
  150. hlist_del(&conn->node);
  151. kmem_cache_free(connlimit_conn_cachep, conn);
  152. continue;
  153. }
  154. nf_ct_put(found_ct);
  155. length++;
  156. }
  157. rcu_read_unlock();
  158. return length;
  159. }
  160. static void tree_nodes_free(struct rb_root *root,
  161. struct xt_connlimit_rb *gc_nodes[],
  162. unsigned int gc_count)
  163. {
  164. struct xt_connlimit_rb *rbconn;
  165. while (gc_count) {
  166. rbconn = gc_nodes[--gc_count];
  167. rb_erase(&rbconn->node, root);
  168. kmem_cache_free(connlimit_rb_cachep, rbconn);
  169. }
  170. }
  171. static unsigned int
  172. count_tree(struct net *net, struct rb_root *root,
  173. const struct nf_conntrack_tuple *tuple,
  174. const union nf_inet_addr *addr, const union nf_inet_addr *mask,
  175. u8 family, const struct nf_conntrack_zone *zone)
  176. {
  177. struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];
  178. struct rb_node **rbnode, *parent;
  179. struct xt_connlimit_rb *rbconn;
  180. struct xt_connlimit_conn *conn;
  181. unsigned int gc_count;
  182. bool no_gc = false;
  183. restart:
  184. gc_count = 0;
  185. parent = NULL;
  186. rbnode = &(root->rb_node);
  187. while (*rbnode) {
  188. int diff;
  189. bool addit;
  190. rbconn = container_of(*rbnode, struct xt_connlimit_rb, node);
  191. parent = *rbnode;
  192. diff = same_source_net(addr, mask, &rbconn->addr, family);
  193. if (diff < 0) {
  194. rbnode = &((*rbnode)->rb_left);
  195. } else if (diff > 0) {
  196. rbnode = &((*rbnode)->rb_right);
  197. } else {
  198. /* same source network -> be counted! */
  199. unsigned int count;
  200. count = check_hlist(net, &rbconn->hhead, tuple, zone, &addit);
  201. tree_nodes_free(root, gc_nodes, gc_count);
  202. if (!addit)
  203. return count;
  204. if (!add_hlist(&rbconn->hhead, tuple, addr))
  205. return 0; /* hotdrop */
  206. return count + 1;
  207. }
  208. if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
  209. continue;
  210. /* only used for GC on hhead, retval and 'addit' ignored */
  211. check_hlist(net, &rbconn->hhead, tuple, zone, &addit);
  212. if (hlist_empty(&rbconn->hhead))
  213. gc_nodes[gc_count++] = rbconn;
  214. }
  215. if (gc_count) {
  216. no_gc = true;
  217. tree_nodes_free(root, gc_nodes, gc_count);
  218. /* tree_node_free before new allocation permits
  219. * allocator to re-use newly free'd object.
  220. *
  221. * This is a rare event; in most cases we will find
  222. * existing node to re-use. (or gc_count is 0).
  223. */
  224. goto restart;
  225. }
  226. /* no match, need to insert new node */
  227. rbconn = kmem_cache_alloc(connlimit_rb_cachep, GFP_ATOMIC);
  228. if (rbconn == NULL)
  229. return 0;
  230. conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
  231. if (conn == NULL) {
  232. kmem_cache_free(connlimit_rb_cachep, rbconn);
  233. return 0;
  234. }
  235. conn->tuple = *tuple;
  236. conn->addr = *addr;
  237. rbconn->addr = *addr;
  238. INIT_HLIST_HEAD(&rbconn->hhead);
  239. hlist_add_head(&conn->node, &rbconn->hhead);
  240. rb_link_node(&rbconn->node, parent, rbnode);
  241. rb_insert_color(&rbconn->node, root);
  242. return 1;
  243. }
  244. static int count_them(struct net *net,
  245. struct xt_connlimit_data *data,
  246. const struct nf_conntrack_tuple *tuple,
  247. const union nf_inet_addr *addr,
  248. const union nf_inet_addr *mask,
  249. u_int8_t family,
  250. const struct nf_conntrack_zone *zone)
  251. {
  252. struct rb_root *root;
  253. int count;
  254. u32 hash;
  255. if (family == NFPROTO_IPV6) {
  256. hash = connlimit_iphash6(addr, mask);
  257. root = &data->climit_root6[hash];
  258. } else {
  259. hash = connlimit_iphash(addr->ip & mask->ip);
  260. root = &data->climit_root4[hash];
  261. }
  262. spin_lock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
  263. count = count_tree(net, root, tuple, addr, mask, family, zone);
  264. spin_unlock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
  265. return count;
  266. }
  267. static bool
  268. connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  269. {
  270. struct net *net = par->net;
  271. const struct xt_connlimit_info *info = par->matchinfo;
  272. union nf_inet_addr addr;
  273. struct nf_conntrack_tuple tuple;
  274. const struct nf_conntrack_tuple *tuple_ptr = &tuple;
  275. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  276. enum ip_conntrack_info ctinfo;
  277. const struct nf_conn *ct;
  278. unsigned int connections;
  279. ct = nf_ct_get(skb, &ctinfo);
  280. if (ct != NULL) {
  281. tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  282. zone = nf_ct_zone(ct);
  283. } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  284. par->family, net, &tuple)) {
  285. goto hotdrop;
  286. }
  287. if (par->family == NFPROTO_IPV6) {
  288. const struct ipv6hdr *iph = ipv6_hdr(skb);
  289. memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
  290. &iph->daddr : &iph->saddr, sizeof(addr.ip6));
  291. } else {
  292. const struct iphdr *iph = ip_hdr(skb);
  293. addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
  294. iph->daddr : iph->saddr;
  295. }
  296. connections = count_them(net, info->data, tuple_ptr, &addr,
  297. &info->mask, par->family, zone);
  298. if (connections == 0)
  299. /* kmalloc failed, drop it entirely */
  300. goto hotdrop;
  301. return (connections > info->limit) ^
  302. !!(info->flags & XT_CONNLIMIT_INVERT);
  303. hotdrop:
  304. par->hotdrop = true;
  305. return false;
  306. }
  307. static int connlimit_mt_check(const struct xt_mtchk_param *par)
  308. {
  309. struct xt_connlimit_info *info = par->matchinfo;
  310. unsigned int i;
  311. int ret;
  312. if (unlikely(!connlimit_rnd)) {
  313. u_int32_t rand;
  314. do {
  315. get_random_bytes(&rand, sizeof(rand));
  316. } while (!rand);
  317. cmpxchg(&connlimit_rnd, 0, rand);
  318. }
  319. ret = nf_ct_l3proto_try_module_get(par->family);
  320. if (ret < 0) {
  321. pr_info("cannot load conntrack support for "
  322. "address family %u\n", par->family);
  323. return ret;
  324. }
  325. /* init private data */
  326. info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
  327. if (info->data == NULL) {
  328. nf_ct_l3proto_module_put(par->family);
  329. return -ENOMEM;
  330. }
  331. for (i = 0; i < ARRAY_SIZE(info->data->climit_root4); ++i)
  332. info->data->climit_root4[i] = RB_ROOT;
  333. for (i = 0; i < ARRAY_SIZE(info->data->climit_root6); ++i)
  334. info->data->climit_root6[i] = RB_ROOT;
  335. return 0;
  336. }
  337. static void destroy_tree(struct rb_root *r)
  338. {
  339. struct xt_connlimit_conn *conn;
  340. struct xt_connlimit_rb *rbconn;
  341. struct hlist_node *n;
  342. struct rb_node *node;
  343. while ((node = rb_first(r)) != NULL) {
  344. rbconn = container_of(node, struct xt_connlimit_rb, node);
  345. rb_erase(node, r);
  346. hlist_for_each_entry_safe(conn, n, &rbconn->hhead, node)
  347. kmem_cache_free(connlimit_conn_cachep, conn);
  348. kmem_cache_free(connlimit_rb_cachep, rbconn);
  349. }
  350. }
  351. static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
  352. {
  353. const struct xt_connlimit_info *info = par->matchinfo;
  354. unsigned int i;
  355. nf_ct_l3proto_module_put(par->family);
  356. for (i = 0; i < ARRAY_SIZE(info->data->climit_root4); ++i)
  357. destroy_tree(&info->data->climit_root4[i]);
  358. for (i = 0; i < ARRAY_SIZE(info->data->climit_root6); ++i)
  359. destroy_tree(&info->data->climit_root6[i]);
  360. kfree(info->data);
  361. }
  362. static struct xt_match connlimit_mt_reg __read_mostly = {
  363. .name = "connlimit",
  364. .revision = 1,
  365. .family = NFPROTO_UNSPEC,
  366. .checkentry = connlimit_mt_check,
  367. .match = connlimit_mt,
  368. .matchsize = sizeof(struct xt_connlimit_info),
  369. .destroy = connlimit_mt_destroy,
  370. .me = THIS_MODULE,
  371. };
  372. static int __init connlimit_mt_init(void)
  373. {
  374. int ret, i;
  375. BUILD_BUG_ON(CONNLIMIT_LOCK_SLOTS > CONNLIMIT_SLOTS);
  376. BUILD_BUG_ON((CONNLIMIT_SLOTS % CONNLIMIT_LOCK_SLOTS) != 0);
  377. for (i = 0; i < CONNLIMIT_LOCK_SLOTS; ++i)
  378. spin_lock_init(&xt_connlimit_locks[i]);
  379. connlimit_conn_cachep = kmem_cache_create("xt_connlimit_conn",
  380. sizeof(struct xt_connlimit_conn),
  381. 0, 0, NULL);
  382. if (!connlimit_conn_cachep)
  383. return -ENOMEM;
  384. connlimit_rb_cachep = kmem_cache_create("xt_connlimit_rb",
  385. sizeof(struct xt_connlimit_rb),
  386. 0, 0, NULL);
  387. if (!connlimit_rb_cachep) {
  388. kmem_cache_destroy(connlimit_conn_cachep);
  389. return -ENOMEM;
  390. }
  391. ret = xt_register_match(&connlimit_mt_reg);
  392. if (ret != 0) {
  393. kmem_cache_destroy(connlimit_conn_cachep);
  394. kmem_cache_destroy(connlimit_rb_cachep);
  395. }
  396. return ret;
  397. }
  398. static void __exit connlimit_mt_exit(void)
  399. {
  400. xt_unregister_match(&connlimit_mt_reg);
  401. kmem_cache_destroy(connlimit_conn_cachep);
  402. kmem_cache_destroy(connlimit_rb_cachep);
  403. }
  404. module_init(connlimit_mt_init);
  405. module_exit(connlimit_mt_exit);
  406. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  407. MODULE_DESCRIPTION("Xtables: Number of connections matching");
  408. MODULE_LICENSE("GPL");
  409. MODULE_ALIAS("ipt_connlimit");
  410. MODULE_ALIAS("ip6t_connlimit");