inet_fragment.c 5.8 KB

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