xt_hashlimit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * xt_hashlimit - Netfilter module to limit the number of packets per time
  3. * separately for each hashbucket (sourceip/sourceport/dstip/dstport)
  4. *
  5. * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
  6. * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  7. *
  8. * Development of this code was funded by Astaro AG, http://www.astaro.com/
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/random.h>
  14. #include <linux/jhash.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/list.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/mm.h>
  22. #include <linux/in.h>
  23. #include <linux/ip.h>
  24. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  25. #include <linux/ipv6.h>
  26. #include <net/ipv6.h>
  27. #endif
  28. #include <net/net_namespace.h>
  29. #include <net/netns/generic.h>
  30. #include <linux/netfilter/x_tables.h>
  31. #include <linux/netfilter_ipv4/ip_tables.h>
  32. #include <linux/netfilter_ipv6/ip6_tables.h>
  33. #include <linux/netfilter/xt_hashlimit.h>
  34. #include <linux/mutex.h>
  35. MODULE_LICENSE("GPL");
  36. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  37. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  38. MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match");
  39. MODULE_ALIAS("ipt_hashlimit");
  40. MODULE_ALIAS("ip6t_hashlimit");
  41. struct hashlimit_net {
  42. struct hlist_head htables;
  43. struct proc_dir_entry *ipt_hashlimit;
  44. struct proc_dir_entry *ip6t_hashlimit;
  45. };
  46. static int hashlimit_net_id;
  47. static inline struct hashlimit_net *hashlimit_pernet(struct net *net)
  48. {
  49. return net_generic(net, hashlimit_net_id);
  50. }
  51. /* need to declare this at the top */
  52. static const struct file_operations dl_file_ops;
  53. /* hash table crap */
  54. struct dsthash_dst {
  55. union {
  56. struct {
  57. __be32 src;
  58. __be32 dst;
  59. } ip;
  60. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  61. struct {
  62. __be32 src[4];
  63. __be32 dst[4];
  64. } ip6;
  65. #endif
  66. };
  67. __be16 src_port;
  68. __be16 dst_port;
  69. };
  70. struct dsthash_ent {
  71. /* static / read-only parts in the beginning */
  72. struct hlist_node node;
  73. struct dsthash_dst dst;
  74. /* modified structure members in the end */
  75. spinlock_t lock;
  76. unsigned long expires; /* precalculated expiry time */
  77. struct {
  78. unsigned long prev; /* last modification */
  79. u_int32_t credit;
  80. u_int32_t credit_cap, cost;
  81. } rateinfo;
  82. struct rcu_head rcu;
  83. };
  84. struct xt_hashlimit_htable {
  85. struct hlist_node node; /* global list of all htables */
  86. int use;
  87. u_int8_t family;
  88. bool rnd_initialized;
  89. struct hashlimit_cfg1 cfg; /* config */
  90. /* used internally */
  91. spinlock_t lock; /* lock for list_head */
  92. u_int32_t rnd; /* random seed for hash */
  93. unsigned int count; /* number entries in table */
  94. struct timer_list timer; /* timer for gc */
  95. /* seq_file stuff */
  96. struct proc_dir_entry *pde;
  97. struct net *net;
  98. struct hlist_head hash[0]; /* hashtable itself */
  99. };
  100. static DEFINE_MUTEX(hashlimit_mutex); /* protects htables list */
  101. static struct kmem_cache *hashlimit_cachep __read_mostly;
  102. static inline bool dst_cmp(const struct dsthash_ent *ent,
  103. const struct dsthash_dst *b)
  104. {
  105. return !memcmp(&ent->dst, b, sizeof(ent->dst));
  106. }
  107. static u_int32_t
  108. hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
  109. {
  110. u_int32_t hash = jhash2((const u32 *)dst,
  111. sizeof(*dst)/sizeof(u32),
  112. ht->rnd);
  113. /*
  114. * Instead of returning hash % ht->cfg.size (implying a divide)
  115. * we return the high 32 bits of the (hash * ht->cfg.size) that will
  116. * give results between [0 and cfg.size-1] and same hash distribution,
  117. * but using a multiply, less expensive than a divide
  118. */
  119. return ((u64)hash * ht->cfg.size) >> 32;
  120. }
  121. static struct dsthash_ent *
  122. dsthash_find(const struct xt_hashlimit_htable *ht,
  123. const struct dsthash_dst *dst)
  124. {
  125. struct dsthash_ent *ent;
  126. struct hlist_node *pos;
  127. u_int32_t hash = hash_dst(ht, dst);
  128. if (!hlist_empty(&ht->hash[hash])) {
  129. hlist_for_each_entry_rcu(ent, pos, &ht->hash[hash], node)
  130. if (dst_cmp(ent, dst)) {
  131. spin_lock(&ent->lock);
  132. return ent;
  133. }
  134. }
  135. return NULL;
  136. }
  137. /* allocate dsthash_ent, initialize dst, put in htable and lock it */
  138. static struct dsthash_ent *
  139. dsthash_alloc_init(struct xt_hashlimit_htable *ht,
  140. const struct dsthash_dst *dst)
  141. {
  142. struct dsthash_ent *ent;
  143. spin_lock(&ht->lock);
  144. /* initialize hash with random val at the time we allocate
  145. * the first hashtable entry */
  146. if (unlikely(!ht->rnd_initialized)) {
  147. get_random_bytes(&ht->rnd, sizeof(ht->rnd));
  148. ht->rnd_initialized = true;
  149. }
  150. if (ht->cfg.max && ht->count >= ht->cfg.max) {
  151. /* FIXME: do something. question is what.. */
  152. if (net_ratelimit())
  153. pr_err("max count of %u reached\n", ht->cfg.max);
  154. ent = NULL;
  155. } else
  156. ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
  157. if (!ent) {
  158. if (net_ratelimit())
  159. pr_err("cannot allocate dsthash_ent\n");
  160. } else {
  161. memcpy(&ent->dst, dst, sizeof(ent->dst));
  162. spin_lock_init(&ent->lock);
  163. spin_lock(&ent->lock);
  164. hlist_add_head_rcu(&ent->node, &ht->hash[hash_dst(ht, dst)]);
  165. ht->count++;
  166. }
  167. spin_unlock(&ht->lock);
  168. return ent;
  169. }
  170. static void dsthash_free_rcu(struct rcu_head *head)
  171. {
  172. struct dsthash_ent *ent = container_of(head, struct dsthash_ent, rcu);
  173. kmem_cache_free(hashlimit_cachep, ent);
  174. }
  175. static inline void
  176. dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
  177. {
  178. hlist_del_rcu(&ent->node);
  179. call_rcu_bh(&ent->rcu, dsthash_free_rcu);
  180. ht->count--;
  181. }
  182. static void htable_gc(unsigned long htlong);
  183. static int htable_create(struct net *net, struct xt_hashlimit_mtinfo1 *minfo,
  184. u_int8_t family)
  185. {
  186. struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
  187. struct xt_hashlimit_htable *hinfo;
  188. unsigned int size;
  189. unsigned int i;
  190. if (minfo->cfg.size) {
  191. size = minfo->cfg.size;
  192. } else {
  193. size = (totalram_pages << PAGE_SHIFT) / 16384 /
  194. sizeof(struct list_head);
  195. if (totalram_pages > 1024 * 1024 * 1024 / PAGE_SIZE)
  196. size = 8192;
  197. if (size < 16)
  198. size = 16;
  199. }
  200. /* FIXME: don't use vmalloc() here or anywhere else -HW */
  201. hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
  202. sizeof(struct list_head) * size);
  203. if (hinfo == NULL)
  204. return -ENOMEM;
  205. minfo->hinfo = hinfo;
  206. /* copy match config into hashtable config */
  207. memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
  208. hinfo->cfg.size = size;
  209. if (hinfo->cfg.max == 0)
  210. hinfo->cfg.max = 8 * hinfo->cfg.size;
  211. else if (hinfo->cfg.max < hinfo->cfg.size)
  212. hinfo->cfg.max = hinfo->cfg.size;
  213. for (i = 0; i < hinfo->cfg.size; i++)
  214. INIT_HLIST_HEAD(&hinfo->hash[i]);
  215. hinfo->use = 1;
  216. hinfo->count = 0;
  217. hinfo->family = family;
  218. hinfo->rnd_initialized = false;
  219. spin_lock_init(&hinfo->lock);
  220. hinfo->pde = proc_create_data(minfo->name, 0,
  221. (family == NFPROTO_IPV4) ?
  222. hashlimit_net->ipt_hashlimit : hashlimit_net->ip6t_hashlimit,
  223. &dl_file_ops, hinfo);
  224. if (hinfo->pde == NULL) {
  225. vfree(hinfo);
  226. return -ENOMEM;
  227. }
  228. hinfo->net = net;
  229. setup_timer(&hinfo->timer, htable_gc, (unsigned long)hinfo);
  230. hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
  231. add_timer(&hinfo->timer);
  232. hlist_add_head(&hinfo->node, &hashlimit_net->htables);
  233. return 0;
  234. }
  235. static bool select_all(const struct xt_hashlimit_htable *ht,
  236. const struct dsthash_ent *he)
  237. {
  238. return 1;
  239. }
  240. static bool select_gc(const struct xt_hashlimit_htable *ht,
  241. const struct dsthash_ent *he)
  242. {
  243. return time_after_eq(jiffies, he->expires);
  244. }
  245. static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
  246. bool (*select)(const struct xt_hashlimit_htable *ht,
  247. const struct dsthash_ent *he))
  248. {
  249. unsigned int i;
  250. /* lock hash table and iterate over it */
  251. spin_lock_bh(&ht->lock);
  252. for (i = 0; i < ht->cfg.size; i++) {
  253. struct dsthash_ent *dh;
  254. struct hlist_node *pos, *n;
  255. hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
  256. if ((*select)(ht, dh))
  257. dsthash_free(ht, dh);
  258. }
  259. }
  260. spin_unlock_bh(&ht->lock);
  261. }
  262. /* hash table garbage collector, run by timer */
  263. static void htable_gc(unsigned long htlong)
  264. {
  265. struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
  266. htable_selective_cleanup(ht, select_gc);
  267. /* re-add the timer accordingly */
  268. ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
  269. add_timer(&ht->timer);
  270. }
  271. static void htable_destroy(struct xt_hashlimit_htable *hinfo)
  272. {
  273. struct hashlimit_net *hashlimit_net = hashlimit_pernet(hinfo->net);
  274. struct proc_dir_entry *parent;
  275. del_timer_sync(&hinfo->timer);
  276. if (hinfo->family == NFPROTO_IPV4)
  277. parent = hashlimit_net->ipt_hashlimit;
  278. else
  279. parent = hashlimit_net->ip6t_hashlimit;
  280. remove_proc_entry(hinfo->pde->name, parent);
  281. htable_selective_cleanup(hinfo, select_all);
  282. vfree(hinfo);
  283. }
  284. static struct xt_hashlimit_htable *htable_find_get(struct net *net,
  285. const char *name,
  286. u_int8_t family)
  287. {
  288. struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
  289. struct xt_hashlimit_htable *hinfo;
  290. struct hlist_node *pos;
  291. hlist_for_each_entry(hinfo, pos, &hashlimit_net->htables, node) {
  292. if (!strcmp(name, hinfo->pde->name) &&
  293. hinfo->family == family) {
  294. hinfo->use++;
  295. return hinfo;
  296. }
  297. }
  298. return NULL;
  299. }
  300. static void htable_put(struct xt_hashlimit_htable *hinfo)
  301. {
  302. mutex_lock(&hashlimit_mutex);
  303. if (--hinfo->use == 0) {
  304. hlist_del(&hinfo->node);
  305. htable_destroy(hinfo);
  306. }
  307. mutex_unlock(&hashlimit_mutex);
  308. }
  309. /* The algorithm used is the Simple Token Bucket Filter (TBF)
  310. * see net/sched/sch_tbf.c in the linux source tree
  311. */
  312. /* Rusty: This is my (non-mathematically-inclined) understanding of
  313. this algorithm. The `average rate' in jiffies becomes your initial
  314. amount of credit `credit' and the most credit you can ever have
  315. `credit_cap'. The `peak rate' becomes the cost of passing the
  316. test, `cost'.
  317. `prev' tracks the last packet hit: you gain one credit per jiffy.
  318. If you get credit balance more than this, the extra credit is
  319. discarded. Every time the match passes, you lose `cost' credits;
  320. if you don't have that many, the test fails.
  321. See Alexey's formal explanation in net/sched/sch_tbf.c.
  322. To get the maximum range, we multiply by this factor (ie. you get N
  323. credits per jiffy). We want to allow a rate as low as 1 per day
  324. (slowest userspace tool allows), which means
  325. CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
  326. */
  327. #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
  328. /* Repeated shift and or gives us all 1s, final shift and add 1 gives
  329. * us the power of 2 below the theoretical max, so GCC simply does a
  330. * shift. */
  331. #define _POW2_BELOW2(x) ((x)|((x)>>1))
  332. #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
  333. #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
  334. #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
  335. #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
  336. #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
  337. #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
  338. /* Precision saver. */
  339. static inline u_int32_t
  340. user2credits(u_int32_t user)
  341. {
  342. /* If multiplying would overflow... */
  343. if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
  344. /* Divide first. */
  345. return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
  346. return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
  347. }
  348. static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
  349. {
  350. dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
  351. if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
  352. dh->rateinfo.credit = dh->rateinfo.credit_cap;
  353. dh->rateinfo.prev = now;
  354. }
  355. static inline __be32 maskl(__be32 a, unsigned int l)
  356. {
  357. return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0;
  358. }
  359. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  360. static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)
  361. {
  362. switch (p) {
  363. case 0 ... 31:
  364. i[0] = maskl(i[0], p);
  365. i[1] = i[2] = i[3] = 0;
  366. break;
  367. case 32 ... 63:
  368. i[1] = maskl(i[1], p - 32);
  369. i[2] = i[3] = 0;
  370. break;
  371. case 64 ... 95:
  372. i[2] = maskl(i[2], p - 64);
  373. i[3] = 0;
  374. break;
  375. case 96 ... 127:
  376. i[3] = maskl(i[3], p - 96);
  377. break;
  378. case 128:
  379. break;
  380. }
  381. }
  382. #endif
  383. static int
  384. hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo,
  385. struct dsthash_dst *dst,
  386. const struct sk_buff *skb, unsigned int protoff)
  387. {
  388. __be16 _ports[2], *ports;
  389. u8 nexthdr;
  390. int poff;
  391. memset(dst, 0, sizeof(*dst));
  392. switch (hinfo->family) {
  393. case NFPROTO_IPV4:
  394. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
  395. dst->ip.dst = maskl(ip_hdr(skb)->daddr,
  396. hinfo->cfg.dstmask);
  397. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
  398. dst->ip.src = maskl(ip_hdr(skb)->saddr,
  399. hinfo->cfg.srcmask);
  400. if (!(hinfo->cfg.mode &
  401. (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
  402. return 0;
  403. nexthdr = ip_hdr(skb)->protocol;
  404. break;
  405. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  406. case NFPROTO_IPV6:
  407. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) {
  408. memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr,
  409. sizeof(dst->ip6.dst));
  410. hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask);
  411. }
  412. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) {
  413. memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr,
  414. sizeof(dst->ip6.src));
  415. hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask);
  416. }
  417. if (!(hinfo->cfg.mode &
  418. (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
  419. return 0;
  420. nexthdr = ipv6_hdr(skb)->nexthdr;
  421. protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr);
  422. if ((int)protoff < 0)
  423. return -1;
  424. break;
  425. #endif
  426. default:
  427. BUG();
  428. return 0;
  429. }
  430. poff = proto_ports_offset(nexthdr);
  431. if (poff >= 0) {
  432. ports = skb_header_pointer(skb, protoff + poff, sizeof(_ports),
  433. &_ports);
  434. } else {
  435. _ports[0] = _ports[1] = 0;
  436. ports = _ports;
  437. }
  438. if (!ports)
  439. return -1;
  440. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
  441. dst->src_port = ports[0];
  442. if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
  443. dst->dst_port = ports[1];
  444. return 0;
  445. }
  446. static bool
  447. hashlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  448. {
  449. const struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
  450. struct xt_hashlimit_htable *hinfo = info->hinfo;
  451. unsigned long now = jiffies;
  452. struct dsthash_ent *dh;
  453. struct dsthash_dst dst;
  454. if (hashlimit_init_dst(hinfo, &dst, skb, par->thoff) < 0)
  455. goto hotdrop;
  456. rcu_read_lock_bh();
  457. dh = dsthash_find(hinfo, &dst);
  458. if (dh == NULL) {
  459. dh = dsthash_alloc_init(hinfo, &dst);
  460. if (dh == NULL) {
  461. rcu_read_unlock_bh();
  462. goto hotdrop;
  463. }
  464. dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
  465. dh->rateinfo.prev = jiffies;
  466. dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
  467. hinfo->cfg.burst);
  468. dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
  469. hinfo->cfg.burst);
  470. dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
  471. } else {
  472. /* update expiration timeout */
  473. dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
  474. rateinfo_recalc(dh, now);
  475. }
  476. if (dh->rateinfo.credit >= dh->rateinfo.cost) {
  477. /* below the limit */
  478. dh->rateinfo.credit -= dh->rateinfo.cost;
  479. spin_unlock(&dh->lock);
  480. rcu_read_unlock_bh();
  481. return !(info->cfg.mode & XT_HASHLIMIT_INVERT);
  482. }
  483. spin_unlock(&dh->lock);
  484. rcu_read_unlock_bh();
  485. /* default match is underlimit - so over the limit, we need to invert */
  486. return info->cfg.mode & XT_HASHLIMIT_INVERT;
  487. hotdrop:
  488. par->hotdrop = true;
  489. return false;
  490. }
  491. static int hashlimit_mt_check(const struct xt_mtchk_param *par)
  492. {
  493. struct net *net = par->net;
  494. struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
  495. int ret;
  496. /* Check for overflow. */
  497. if (info->cfg.burst == 0 ||
  498. user2credits(info->cfg.avg * info->cfg.burst) <
  499. user2credits(info->cfg.avg)) {
  500. pr_info("overflow, try lower: %u/%u\n",
  501. info->cfg.avg, info->cfg.burst);
  502. return -ERANGE;
  503. }
  504. if (info->cfg.gc_interval == 0 || info->cfg.expire == 0)
  505. return -EINVAL;
  506. if (info->name[sizeof(info->name)-1] != '\0')
  507. return -EINVAL;
  508. if (par->family == NFPROTO_IPV4) {
  509. if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32)
  510. return -EINVAL;
  511. } else {
  512. if (info->cfg.srcmask > 128 || info->cfg.dstmask > 128)
  513. return -EINVAL;
  514. }
  515. mutex_lock(&hashlimit_mutex);
  516. info->hinfo = htable_find_get(net, info->name, par->family);
  517. if (info->hinfo == NULL) {
  518. ret = htable_create(net, info, par->family);
  519. if (ret < 0) {
  520. mutex_unlock(&hashlimit_mutex);
  521. return ret;
  522. }
  523. }
  524. mutex_unlock(&hashlimit_mutex);
  525. return 0;
  526. }
  527. static void hashlimit_mt_destroy(const struct xt_mtdtor_param *par)
  528. {
  529. const struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
  530. htable_put(info->hinfo);
  531. }
  532. static struct xt_match hashlimit_mt_reg[] __read_mostly = {
  533. {
  534. .name = "hashlimit",
  535. .revision = 1,
  536. .family = NFPROTO_IPV4,
  537. .match = hashlimit_mt,
  538. .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
  539. .checkentry = hashlimit_mt_check,
  540. .destroy = hashlimit_mt_destroy,
  541. .me = THIS_MODULE,
  542. },
  543. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  544. {
  545. .name = "hashlimit",
  546. .revision = 1,
  547. .family = NFPROTO_IPV6,
  548. .match = hashlimit_mt,
  549. .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
  550. .checkentry = hashlimit_mt_check,
  551. .destroy = hashlimit_mt_destroy,
  552. .me = THIS_MODULE,
  553. },
  554. #endif
  555. };
  556. /* PROC stuff */
  557. static void *dl_seq_start(struct seq_file *s, loff_t *pos)
  558. __acquires(htable->lock)
  559. {
  560. struct xt_hashlimit_htable *htable = s->private;
  561. unsigned int *bucket;
  562. spin_lock_bh(&htable->lock);
  563. if (*pos >= htable->cfg.size)
  564. return NULL;
  565. bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
  566. if (!bucket)
  567. return ERR_PTR(-ENOMEM);
  568. *bucket = *pos;
  569. return bucket;
  570. }
  571. static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
  572. {
  573. struct xt_hashlimit_htable *htable = s->private;
  574. unsigned int *bucket = (unsigned int *)v;
  575. *pos = ++(*bucket);
  576. if (*pos >= htable->cfg.size) {
  577. kfree(v);
  578. return NULL;
  579. }
  580. return bucket;
  581. }
  582. static void dl_seq_stop(struct seq_file *s, void *v)
  583. __releases(htable->lock)
  584. {
  585. struct xt_hashlimit_htable *htable = s->private;
  586. unsigned int *bucket = (unsigned int *)v;
  587. if (!IS_ERR(bucket))
  588. kfree(bucket);
  589. spin_unlock_bh(&htable->lock);
  590. }
  591. static int dl_seq_real_show(struct dsthash_ent *ent, u_int8_t family,
  592. struct seq_file *s)
  593. {
  594. int res;
  595. spin_lock(&ent->lock);
  596. /* recalculate to show accurate numbers */
  597. rateinfo_recalc(ent, jiffies);
  598. switch (family) {
  599. case NFPROTO_IPV4:
  600. res = seq_printf(s, "%ld %pI4:%u->%pI4:%u %u %u %u\n",
  601. (long)(ent->expires - jiffies)/HZ,
  602. &ent->dst.ip.src,
  603. ntohs(ent->dst.src_port),
  604. &ent->dst.ip.dst,
  605. ntohs(ent->dst.dst_port),
  606. ent->rateinfo.credit, ent->rateinfo.credit_cap,
  607. ent->rateinfo.cost);
  608. break;
  609. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  610. case NFPROTO_IPV6:
  611. res = seq_printf(s, "%ld %pI6:%u->%pI6:%u %u %u %u\n",
  612. (long)(ent->expires - jiffies)/HZ,
  613. &ent->dst.ip6.src,
  614. ntohs(ent->dst.src_port),
  615. &ent->dst.ip6.dst,
  616. ntohs(ent->dst.dst_port),
  617. ent->rateinfo.credit, ent->rateinfo.credit_cap,
  618. ent->rateinfo.cost);
  619. break;
  620. #endif
  621. default:
  622. BUG();
  623. res = 0;
  624. }
  625. spin_unlock(&ent->lock);
  626. return res;
  627. }
  628. static int dl_seq_show(struct seq_file *s, void *v)
  629. {
  630. struct xt_hashlimit_htable *htable = s->private;
  631. unsigned int *bucket = (unsigned int *)v;
  632. struct dsthash_ent *ent;
  633. struct hlist_node *pos;
  634. if (!hlist_empty(&htable->hash[*bucket])) {
  635. hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
  636. if (dl_seq_real_show(ent, htable->family, s))
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. static const struct seq_operations dl_seq_ops = {
  642. .start = dl_seq_start,
  643. .next = dl_seq_next,
  644. .stop = dl_seq_stop,
  645. .show = dl_seq_show
  646. };
  647. static int dl_proc_open(struct inode *inode, struct file *file)
  648. {
  649. int ret = seq_open(file, &dl_seq_ops);
  650. if (!ret) {
  651. struct seq_file *sf = file->private_data;
  652. sf->private = PDE(inode)->data;
  653. }
  654. return ret;
  655. }
  656. static const struct file_operations dl_file_ops = {
  657. .owner = THIS_MODULE,
  658. .open = dl_proc_open,
  659. .read = seq_read,
  660. .llseek = seq_lseek,
  661. .release = seq_release
  662. };
  663. static int __net_init hashlimit_proc_net_init(struct net *net)
  664. {
  665. struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
  666. hashlimit_net->ipt_hashlimit = proc_mkdir("ipt_hashlimit", net->proc_net);
  667. if (!hashlimit_net->ipt_hashlimit)
  668. return -ENOMEM;
  669. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  670. hashlimit_net->ip6t_hashlimit = proc_mkdir("ip6t_hashlimit", net->proc_net);
  671. if (!hashlimit_net->ip6t_hashlimit) {
  672. proc_net_remove(net, "ipt_hashlimit");
  673. return -ENOMEM;
  674. }
  675. #endif
  676. return 0;
  677. }
  678. static void __net_exit hashlimit_proc_net_exit(struct net *net)
  679. {
  680. proc_net_remove(net, "ipt_hashlimit");
  681. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  682. proc_net_remove(net, "ip6t_hashlimit");
  683. #endif
  684. }
  685. static int __net_init hashlimit_net_init(struct net *net)
  686. {
  687. struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
  688. INIT_HLIST_HEAD(&hashlimit_net->htables);
  689. return hashlimit_proc_net_init(net);
  690. }
  691. static void __net_exit hashlimit_net_exit(struct net *net)
  692. {
  693. struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
  694. BUG_ON(!hlist_empty(&hashlimit_net->htables));
  695. hashlimit_proc_net_exit(net);
  696. }
  697. static struct pernet_operations hashlimit_net_ops = {
  698. .init = hashlimit_net_init,
  699. .exit = hashlimit_net_exit,
  700. .id = &hashlimit_net_id,
  701. .size = sizeof(struct hashlimit_net),
  702. };
  703. static int __init hashlimit_mt_init(void)
  704. {
  705. int err;
  706. err = register_pernet_subsys(&hashlimit_net_ops);
  707. if (err < 0)
  708. return err;
  709. err = xt_register_matches(hashlimit_mt_reg,
  710. ARRAY_SIZE(hashlimit_mt_reg));
  711. if (err < 0)
  712. goto err1;
  713. err = -ENOMEM;
  714. hashlimit_cachep = kmem_cache_create("xt_hashlimit",
  715. sizeof(struct dsthash_ent), 0, 0,
  716. NULL);
  717. if (!hashlimit_cachep) {
  718. pr_warning("unable to create slab cache\n");
  719. goto err2;
  720. }
  721. return 0;
  722. err2:
  723. xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
  724. err1:
  725. unregister_pernet_subsys(&hashlimit_net_ops);
  726. return err;
  727. }
  728. static void __exit hashlimit_mt_exit(void)
  729. {
  730. xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
  731. unregister_pernet_subsys(&hashlimit_net_ops);
  732. rcu_barrier_bh();
  733. kmem_cache_destroy(hashlimit_cachep);
  734. }
  735. module_init(hashlimit_mt_init);
  736. module_exit(hashlimit_mt_exit);