xt_connlimit.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/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>
  32. #include <net/netfilter/nf_conntrack_zones.h>
  33. /* we will save the tuples of all connections we care about */
  34. struct xt_connlimit_conn {
  35. struct hlist_node node;
  36. struct nf_conntrack_tuple tuple;
  37. union nf_inet_addr addr;
  38. };
  39. struct xt_connlimit_data {
  40. struct hlist_head iphash[256];
  41. spinlock_t lock;
  42. };
  43. static u_int32_t connlimit_rnd __read_mostly;
  44. static inline unsigned int connlimit_iphash(__be32 addr)
  45. {
  46. return jhash_1word((__force __u32)addr, connlimit_rnd) & 0xFF;
  47. }
  48. static inline unsigned int
  49. connlimit_iphash6(const union nf_inet_addr *addr,
  50. const union nf_inet_addr *mask)
  51. {
  52. union nf_inet_addr res;
  53. unsigned int i;
  54. for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
  55. res.ip6[i] = addr->ip6[i] & mask->ip6[i];
  56. return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6), connlimit_rnd) & 0xFF;
  57. }
  58. static inline bool already_closed(const struct nf_conn *conn)
  59. {
  60. if (nf_ct_protonum(conn) == IPPROTO_TCP)
  61. return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
  62. conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
  63. else
  64. return 0;
  65. }
  66. static inline unsigned int
  67. same_source_net(const union nf_inet_addr *addr,
  68. const union nf_inet_addr *mask,
  69. const union nf_inet_addr *u3, u_int8_t family)
  70. {
  71. if (family == NFPROTO_IPV4) {
  72. return (addr->ip & mask->ip) == (u3->ip & mask->ip);
  73. } else {
  74. union nf_inet_addr lh, rh;
  75. unsigned int i;
  76. for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
  77. lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
  78. rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
  79. }
  80. return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6)) == 0;
  81. }
  82. }
  83. static int count_them(struct net *net,
  84. struct xt_connlimit_data *data,
  85. const struct nf_conntrack_tuple *tuple,
  86. const union nf_inet_addr *addr,
  87. const union nf_inet_addr *mask,
  88. u_int8_t family)
  89. {
  90. const struct nf_conntrack_tuple_hash *found;
  91. struct xt_connlimit_conn *conn;
  92. struct hlist_node *pos, *n;
  93. struct nf_conn *found_ct;
  94. struct hlist_head *hash;
  95. bool addit = true;
  96. int matches = 0;
  97. if (family == NFPROTO_IPV6)
  98. hash = &data->iphash[connlimit_iphash6(addr, mask)];
  99. else
  100. hash = &data->iphash[connlimit_iphash(addr->ip & mask->ip)];
  101. rcu_read_lock();
  102. /* check the saved connections */
  103. hlist_for_each_entry_safe(conn, pos, n, hash, node) {
  104. found = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE,
  105. &conn->tuple);
  106. found_ct = NULL;
  107. if (found != NULL)
  108. found_ct = nf_ct_tuplehash_to_ctrack(found);
  109. if (found_ct != NULL &&
  110. nf_ct_tuple_equal(&conn->tuple, tuple) &&
  111. !already_closed(found_ct))
  112. /*
  113. * Just to be sure we have it only once in the list.
  114. * We should not see tuples twice unless someone hooks
  115. * this into a table without "-p tcp --syn".
  116. */
  117. addit = false;
  118. if (found == NULL) {
  119. /* this one is gone */
  120. hlist_del(&conn->node);
  121. kfree(conn);
  122. continue;
  123. }
  124. if (already_closed(found_ct)) {
  125. /*
  126. * we do not care about connections which are
  127. * closed already -> ditch it
  128. */
  129. nf_ct_put(found_ct);
  130. hlist_del(&conn->node);
  131. kfree(conn);
  132. continue;
  133. }
  134. if (same_source_net(addr, mask, &conn->addr, family))
  135. /* same source network -> be counted! */
  136. ++matches;
  137. nf_ct_put(found_ct);
  138. }
  139. rcu_read_unlock();
  140. if (addit) {
  141. /* save the new connection in our list */
  142. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  143. if (conn == NULL)
  144. return -ENOMEM;
  145. conn->tuple = *tuple;
  146. conn->addr = *addr;
  147. hlist_add_head(&conn->node, hash);
  148. ++matches;
  149. }
  150. return matches;
  151. }
  152. static bool
  153. connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  154. {
  155. struct net *net = dev_net(par->in ? par->in : par->out);
  156. const struct xt_connlimit_info *info = par->matchinfo;
  157. union nf_inet_addr addr;
  158. struct nf_conntrack_tuple tuple;
  159. const struct nf_conntrack_tuple *tuple_ptr = &tuple;
  160. enum ip_conntrack_info ctinfo;
  161. const struct nf_conn *ct;
  162. int connections;
  163. ct = nf_ct_get(skb, &ctinfo);
  164. if (ct != NULL)
  165. tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  166. else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  167. par->family, &tuple))
  168. goto hotdrop;
  169. if (par->family == NFPROTO_IPV6) {
  170. const struct ipv6hdr *iph = ipv6_hdr(skb);
  171. memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
  172. &iph->daddr : &iph->saddr, sizeof(addr.ip6));
  173. } else {
  174. const struct iphdr *iph = ip_hdr(skb);
  175. addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
  176. iph->daddr : iph->saddr;
  177. }
  178. spin_lock_bh(&info->data->lock);
  179. connections = count_them(net, info->data, tuple_ptr, &addr,
  180. &info->mask, par->family);
  181. spin_unlock_bh(&info->data->lock);
  182. if (connections < 0)
  183. /* kmalloc failed, drop it entirely */
  184. goto hotdrop;
  185. return (connections > info->limit) ^
  186. !!(info->flags & XT_CONNLIMIT_INVERT);
  187. hotdrop:
  188. par->hotdrop = true;
  189. return false;
  190. }
  191. static int connlimit_mt_check(const struct xt_mtchk_param *par)
  192. {
  193. struct xt_connlimit_info *info = par->matchinfo;
  194. unsigned int i;
  195. int ret;
  196. if (unlikely(!connlimit_rnd)) {
  197. u_int32_t rand;
  198. do {
  199. get_random_bytes(&rand, sizeof(rand));
  200. } while (!rand);
  201. cmpxchg(&connlimit_rnd, 0, rand);
  202. }
  203. ret = nf_ct_l3proto_try_module_get(par->family);
  204. if (ret < 0) {
  205. pr_info("cannot load conntrack support for "
  206. "address family %u\n", par->family);
  207. return ret;
  208. }
  209. /* init private data */
  210. info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
  211. if (info->data == NULL) {
  212. nf_ct_l3proto_module_put(par->family);
  213. return -ENOMEM;
  214. }
  215. spin_lock_init(&info->data->lock);
  216. for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i)
  217. INIT_HLIST_HEAD(&info->data->iphash[i]);
  218. return 0;
  219. }
  220. static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
  221. {
  222. const struct xt_connlimit_info *info = par->matchinfo;
  223. struct xt_connlimit_conn *conn;
  224. struct hlist_node *pos, *n;
  225. struct hlist_head *hash = info->data->iphash;
  226. unsigned int i;
  227. nf_ct_l3proto_module_put(par->family);
  228. for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) {
  229. hlist_for_each_entry_safe(conn, pos, n, &hash[i], node) {
  230. hlist_del(&conn->node);
  231. kfree(conn);
  232. }
  233. }
  234. kfree(info->data);
  235. }
  236. static struct xt_match connlimit_mt_reg[] __read_mostly = {
  237. {
  238. .name = "connlimit",
  239. .revision = 0,
  240. .family = NFPROTO_UNSPEC,
  241. .checkentry = connlimit_mt_check,
  242. .match = connlimit_mt,
  243. .matchsize = sizeof(struct xt_connlimit_info),
  244. .destroy = connlimit_mt_destroy,
  245. .me = THIS_MODULE,
  246. },
  247. {
  248. .name = "connlimit",
  249. .revision = 1,
  250. .family = NFPROTO_UNSPEC,
  251. .checkentry = connlimit_mt_check,
  252. .match = connlimit_mt,
  253. .matchsize = sizeof(struct xt_connlimit_info),
  254. .destroy = connlimit_mt_destroy,
  255. .me = THIS_MODULE,
  256. },
  257. };
  258. static int __init connlimit_mt_init(void)
  259. {
  260. return xt_register_matches(connlimit_mt_reg,
  261. ARRAY_SIZE(connlimit_mt_reg));
  262. }
  263. static void __exit connlimit_mt_exit(void)
  264. {
  265. xt_unregister_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg));
  266. }
  267. module_init(connlimit_mt_init);
  268. module_exit(connlimit_mt_exit);
  269. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  270. MODULE_DESCRIPTION("Xtables: Number of connections matching");
  271. MODULE_LICENSE("GPL");
  272. MODULE_ALIAS("ipt_connlimit");
  273. MODULE_ALIAS("ip6t_connlimit");