xt_connlimit.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/module.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/netfilter/x_tables.h>
  20. #include <linux/netfilter/xt_connlimit.h>
  21. #include <net/netfilter/nf_conntrack.h>
  22. #include <net/netfilter/nf_conntrack_core.h>
  23. #include <net/netfilter/nf_conntrack_tuple.h>
  24. #include <net/netfilter/nf_conntrack_zones.h>
  25. #include <net/netfilter/nf_conntrack_count.h>
  26. static bool
  27. connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  28. {
  29. struct net *net = xt_net(par);
  30. const struct xt_connlimit_info *info = par->matchinfo;
  31. struct nf_conntrack_tuple tuple;
  32. const struct nf_conntrack_tuple *tuple_ptr = &tuple;
  33. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  34. enum ip_conntrack_info ctinfo;
  35. const struct nf_conn *ct;
  36. unsigned int connections;
  37. u32 key[5];
  38. ct = nf_ct_get(skb, &ctinfo);
  39. if (ct != NULL) {
  40. tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  41. zone = nf_ct_zone(ct);
  42. } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  43. xt_family(par), net, &tuple)) {
  44. goto hotdrop;
  45. }
  46. if (xt_family(par) == NFPROTO_IPV6) {
  47. const struct ipv6hdr *iph = ipv6_hdr(skb);
  48. union nf_inet_addr addr;
  49. unsigned int i;
  50. memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
  51. &iph->daddr : &iph->saddr, sizeof(addr.ip6));
  52. for (i = 0; i < ARRAY_SIZE(addr.ip6); ++i)
  53. addr.ip6[i] &= info->mask.ip6[i];
  54. memcpy(key, &addr, sizeof(addr.ip6));
  55. key[4] = zone->id;
  56. } else {
  57. const struct iphdr *iph = ip_hdr(skb);
  58. key[0] = (info->flags & XT_CONNLIMIT_DADDR) ?
  59. iph->daddr : iph->saddr;
  60. key[0] &= info->mask.ip;
  61. key[1] = zone->id;
  62. }
  63. connections = nf_conncount_count(net, info->data, key, tuple_ptr,
  64. zone);
  65. if (connections == 0)
  66. /* kmalloc failed, drop it entirely */
  67. goto hotdrop;
  68. return (connections > info->limit) ^ !!(info->flags & XT_CONNLIMIT_INVERT);
  69. hotdrop:
  70. par->hotdrop = true;
  71. return false;
  72. }
  73. static int connlimit_mt_check(const struct xt_mtchk_param *par)
  74. {
  75. struct xt_connlimit_info *info = par->matchinfo;
  76. unsigned int keylen;
  77. keylen = sizeof(u32);
  78. if (par->family == NFPROTO_IPV6)
  79. keylen += sizeof(struct in6_addr);
  80. else
  81. keylen += sizeof(struct in_addr);
  82. /* init private data */
  83. info->data = nf_conncount_init(par->net, par->family, keylen);
  84. return PTR_ERR_OR_ZERO(info->data);
  85. }
  86. static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
  87. {
  88. const struct xt_connlimit_info *info = par->matchinfo;
  89. nf_conncount_destroy(par->net, par->family, info->data);
  90. }
  91. static struct xt_match connlimit_mt_reg __read_mostly = {
  92. .name = "connlimit",
  93. .revision = 1,
  94. .family = NFPROTO_UNSPEC,
  95. .checkentry = connlimit_mt_check,
  96. .match = connlimit_mt,
  97. .matchsize = sizeof(struct xt_connlimit_info),
  98. .usersize = offsetof(struct xt_connlimit_info, data),
  99. .destroy = connlimit_mt_destroy,
  100. .me = THIS_MODULE,
  101. };
  102. static int __init connlimit_mt_init(void)
  103. {
  104. return xt_register_match(&connlimit_mt_reg);
  105. }
  106. static void __exit connlimit_mt_exit(void)
  107. {
  108. xt_unregister_match(&connlimit_mt_reg);
  109. }
  110. module_init(connlimit_mt_init);
  111. module_exit(connlimit_mt_exit);
  112. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  113. MODULE_DESCRIPTION("Xtables: Number of connections matching");
  114. MODULE_LICENSE("GPL");
  115. MODULE_ALIAS("ipt_connlimit");
  116. MODULE_ALIAS("ip6t_connlimit");