inet_fragment.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 netns_frags *nf,
  143. struct inet_frag_queue *qp_in, struct inet_frags *f,
  144. 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 (qp->net == nf && 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 netns_frags *nf,
  177. struct inet_frags *f, void *arg)
  178. {
  179. struct inet_frag_queue *q;
  180. q = kzalloc(f->qsize, GFP_ATOMIC);
  181. if (q == NULL)
  182. return NULL;
  183. f->constructor(q, arg);
  184. atomic_add(f->qsize, &f->mem);
  185. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  186. spin_lock_init(&q->lock);
  187. atomic_set(&q->refcnt, 1);
  188. q->net = nf;
  189. return q;
  190. }
  191. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  192. struct inet_frags *f, void *arg, unsigned int hash)
  193. {
  194. struct inet_frag_queue *q;
  195. q = inet_frag_alloc(nf, f, arg);
  196. if (q == NULL)
  197. return NULL;
  198. return inet_frag_intern(nf, q, f, hash, arg);
  199. }
  200. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  201. struct inet_frags *f, void *key, unsigned int hash)
  202. {
  203. struct inet_frag_queue *q;
  204. struct hlist_node *n;
  205. read_lock(&f->lock);
  206. hlist_for_each_entry(q, n, &f->hash[hash], list) {
  207. if (q->net == nf && f->match(q, key)) {
  208. atomic_inc(&q->refcnt);
  209. read_unlock(&f->lock);
  210. return q;
  211. }
  212. }
  213. read_unlock(&f->lock);
  214. return inet_frag_create(nf, f, key, hash);
  215. }
  216. EXPORT_SYMBOL(inet_frag_find);