inet_fragment.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * inet fragments management
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Pavel Emelyanov <xemul@openvz.org>
  10. * Started as consolidation of ipv4/ip_fragment.c,
  11. * ipv6/reassembly. and ipv6 nf conntrack reassembly
  12. */
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/timer.h>
  17. #include <linux/mm.h>
  18. #include <linux/random.h>
  19. #include <net/inet_frag.h>
  20. static void inet_frag_secret_rebuild(unsigned long dummy)
  21. {
  22. struct inet_frags *f = (struct inet_frags *)dummy;
  23. unsigned long now = jiffies;
  24. int i;
  25. write_lock(&f->lock);
  26. get_random_bytes(&f->rnd, sizeof(u32));
  27. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  28. struct inet_frag_queue *q;
  29. struct hlist_node *p, *n;
  30. hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
  31. unsigned int hval = f->hashfn(q);
  32. if (hval != i) {
  33. hlist_del(&q->list);
  34. /* Relink to new hash chain. */
  35. hlist_add_head(&q->list, &f->hash[hval]);
  36. }
  37. }
  38. }
  39. write_unlock(&f->lock);
  40. mod_timer(&f->secret_timer, now + f->ctl->secret_interval);
  41. }
  42. void inet_frags_init(struct inet_frags *f)
  43. {
  44. int i;
  45. for (i = 0; i < INETFRAGS_HASHSZ; i++)
  46. INIT_HLIST_HEAD(&f->hash[i]);
  47. INIT_LIST_HEAD(&f->lru_list);
  48. rwlock_init(&f->lock);
  49. f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  50. (jiffies ^ (jiffies >> 6)));
  51. f->nqueues = 0;
  52. atomic_set(&f->mem, 0);
  53. init_timer(&f->secret_timer);
  54. f->secret_timer.function = inet_frag_secret_rebuild;
  55. f->secret_timer.data = (unsigned long)f;
  56. f->secret_timer.expires = jiffies + f->ctl->secret_interval;
  57. add_timer(&f->secret_timer);
  58. }
  59. EXPORT_SYMBOL(inet_frags_init);
  60. void inet_frags_fini(struct inet_frags *f)
  61. {
  62. del_timer(&f->secret_timer);
  63. }
  64. EXPORT_SYMBOL(inet_frags_fini);
  65. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  66. {
  67. write_lock(&f->lock);
  68. hlist_del(&fq->list);
  69. list_del(&fq->lru_list);
  70. f->nqueues--;
  71. write_unlock(&f->lock);
  72. }
  73. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  74. {
  75. if (del_timer(&fq->timer))
  76. atomic_dec(&fq->refcnt);
  77. if (!(fq->last_in & COMPLETE)) {
  78. fq_unlink(fq, f);
  79. atomic_dec(&fq->refcnt);
  80. fq->last_in |= COMPLETE;
  81. }
  82. }
  83. EXPORT_SYMBOL(inet_frag_kill);