inet_fragment.c 5.6 KB

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