inet_fragment.c 6.1 KB

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