inet_fragment.c 6.4 KB

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