inet_fragment.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <linux/skbuff.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/inet_frag.h>
  22. static void inet_frag_secret_rebuild(unsigned long dummy)
  23. {
  24. struct inet_frags *f = (struct inet_frags *)dummy;
  25. unsigned long now = jiffies;
  26. int i;
  27. write_lock(&f->lock);
  28. get_random_bytes(&f->rnd, sizeof(u32));
  29. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  30. struct inet_frag_queue *q;
  31. struct hlist_node *p, *n;
  32. hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
  33. unsigned int hval = f->hashfn(q);
  34. if (hval != i) {
  35. hlist_del(&q->list);
  36. /* Relink to new hash chain. */
  37. hlist_add_head(&q->list, &f->hash[hval]);
  38. }
  39. }
  40. }
  41. write_unlock(&f->lock);
  42. mod_timer(&f->secret_timer, now + f->ctl->secret_interval);
  43. }
  44. void inet_frags_init(struct inet_frags *f)
  45. {
  46. int i;
  47. for (i = 0; i < INETFRAGS_HASHSZ; i++)
  48. INIT_HLIST_HEAD(&f->hash[i]);
  49. INIT_LIST_HEAD(&f->lru_list);
  50. rwlock_init(&f->lock);
  51. f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  52. (jiffies ^ (jiffies >> 6)));
  53. f->nqueues = 0;
  54. atomic_set(&f->mem, 0);
  55. init_timer(&f->secret_timer);
  56. f->secret_timer.function = inet_frag_secret_rebuild;
  57. f->secret_timer.data = (unsigned long)f;
  58. f->secret_timer.expires = jiffies + f->ctl->secret_interval;
  59. add_timer(&f->secret_timer);
  60. }
  61. EXPORT_SYMBOL(inet_frags_init);
  62. void inet_frags_fini(struct inet_frags *f)
  63. {
  64. del_timer(&f->secret_timer);
  65. }
  66. EXPORT_SYMBOL(inet_frags_fini);
  67. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  68. {
  69. write_lock(&f->lock);
  70. hlist_del(&fq->list);
  71. list_del(&fq->lru_list);
  72. f->nqueues--;
  73. write_unlock(&f->lock);
  74. }
  75. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  76. {
  77. if (del_timer(&fq->timer))
  78. atomic_dec(&fq->refcnt);
  79. if (!(fq->last_in & COMPLETE)) {
  80. fq_unlink(fq, f);
  81. atomic_dec(&fq->refcnt);
  82. fq->last_in |= COMPLETE;
  83. }
  84. }
  85. EXPORT_SYMBOL(inet_frag_kill);
  86. static inline void frag_kfree_skb(struct inet_frags *f, struct sk_buff *skb,
  87. int *work)
  88. {
  89. if (work)
  90. *work -= skb->truesize;
  91. atomic_sub(skb->truesize, &f->mem);
  92. if (f->skb_free)
  93. f->skb_free(skb);
  94. kfree_skb(skb);
  95. }
  96. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
  97. int *work)
  98. {
  99. struct sk_buff *fp;
  100. BUG_TRAP(q->last_in & COMPLETE);
  101. BUG_TRAP(del_timer(&q->timer) == 0);
  102. /* Release all fragment data. */
  103. fp = q->fragments;
  104. while (fp) {
  105. struct sk_buff *xp = fp->next;
  106. frag_kfree_skb(f, fp, work);
  107. fp = xp;
  108. }
  109. if (work)
  110. *work -= f->qsize;
  111. atomic_sub(f->qsize, &f->mem);
  112. if (f->destructor)
  113. f->destructor(q);
  114. kfree(q);
  115. }
  116. EXPORT_SYMBOL(inet_frag_destroy);
  117. int inet_frag_evictor(struct inet_frags *f)
  118. {
  119. struct inet_frag_queue *q;
  120. int work, evicted = 0;
  121. work = atomic_read(&f->mem) - f->ctl->low_thresh;
  122. while (work > 0) {
  123. read_lock(&f->lock);
  124. if (list_empty(&f->lru_list)) {
  125. read_unlock(&f->lock);
  126. break;
  127. }
  128. q = list_first_entry(&f->lru_list,
  129. struct inet_frag_queue, lru_list);
  130. atomic_inc(&q->refcnt);
  131. read_unlock(&f->lock);
  132. spin_lock(&q->lock);
  133. if (!(q->last_in & COMPLETE))
  134. inet_frag_kill(q, f);
  135. spin_unlock(&q->lock);
  136. if (atomic_dec_and_test(&q->refcnt))
  137. inet_frag_destroy(q, f, &work);
  138. evicted++;
  139. }
  140. return evicted;
  141. }
  142. EXPORT_SYMBOL(inet_frag_evictor);
  143. static struct inet_frag_queue *inet_frag_intern(struct inet_frag_queue *qp_in,
  144. struct inet_frags *f, unsigned int hash, void *arg)
  145. {
  146. struct inet_frag_queue *qp;
  147. #ifdef CONFIG_SMP
  148. struct hlist_node *n;
  149. #endif
  150. write_lock(&f->lock);
  151. #ifdef CONFIG_SMP
  152. /* With SMP race we have to recheck hash table, because
  153. * such entry could be created on other cpu, while we
  154. * promoted read lock to write lock.
  155. */
  156. hlist_for_each_entry(qp, n, &f->hash[hash], list) {
  157. if (f->match(qp, arg)) {
  158. atomic_inc(&qp->refcnt);
  159. write_unlock(&f->lock);
  160. qp_in->last_in |= COMPLETE;
  161. inet_frag_put(qp_in, f);
  162. return qp;
  163. }
  164. }
  165. #endif
  166. qp = qp_in;
  167. if (!mod_timer(&qp->timer, jiffies + f->ctl->timeout))
  168. atomic_inc(&qp->refcnt);
  169. atomic_inc(&qp->refcnt);
  170. hlist_add_head(&qp->list, &f->hash[hash]);
  171. list_add_tail(&qp->lru_list, &f->lru_list);
  172. f->nqueues++;
  173. write_unlock(&f->lock);
  174. return qp;
  175. }
  176. static struct inet_frag_queue *inet_frag_alloc(struct inet_frags *f, void *arg)
  177. {
  178. struct inet_frag_queue *q;
  179. q = kzalloc(f->qsize, GFP_ATOMIC);
  180. if (q == NULL)
  181. return NULL;
  182. f->constructor(q, arg);
  183. atomic_add(f->qsize, &f->mem);
  184. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  185. spin_lock_init(&q->lock);
  186. atomic_set(&q->refcnt, 1);
  187. return q;
  188. }
  189. static struct inet_frag_queue *inet_frag_create(struct inet_frags *f,
  190. void *arg, unsigned int hash)
  191. {
  192. struct inet_frag_queue *q;
  193. q = inet_frag_alloc(f, arg);
  194. if (q == NULL)
  195. return NULL;
  196. return inet_frag_intern(q, f, hash, arg);
  197. }
  198. struct inet_frag_queue *inet_frag_find(struct inet_frags *f, void *key,
  199. unsigned int hash)
  200. {
  201. struct inet_frag_queue *q;
  202. struct hlist_node *n;
  203. read_lock(&f->lock);
  204. hlist_for_each_entry(q, n, &f->hash[hash], list) {
  205. if (f->match(q, key)) {
  206. atomic_inc(&q->refcnt);
  207. read_unlock(&f->lock);
  208. return q;
  209. }
  210. }
  211. read_unlock(&f->lock);
  212. return inet_frag_create(f, key, hash);
  213. }
  214. EXPORT_SYMBOL(inet_frag_find);