inet_fragment.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. init_frag_mem_limit(nf);
  63. INIT_LIST_HEAD(&nf->lru_list);
  64. spin_lock_init(&nf->lru_lock);
  65. }
  66. EXPORT_SYMBOL(inet_frags_init_net);
  67. void inet_frags_fini(struct inet_frags *f)
  68. {
  69. del_timer(&f->secret_timer);
  70. }
  71. EXPORT_SYMBOL(inet_frags_fini);
  72. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f)
  73. {
  74. nf->low_thresh = 0;
  75. local_bh_disable();
  76. inet_frag_evictor(nf, f, true);
  77. local_bh_enable();
  78. percpu_counter_destroy(&nf->mem);
  79. }
  80. EXPORT_SYMBOL(inet_frags_exit_net);
  81. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  82. {
  83. write_lock(&f->lock);
  84. hlist_del(&fq->list);
  85. fq->net->nqueues--;
  86. write_unlock(&f->lock);
  87. inet_frag_lru_del(fq);
  88. }
  89. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  90. {
  91. if (del_timer(&fq->timer))
  92. atomic_dec(&fq->refcnt);
  93. if (!(fq->last_in & INET_FRAG_COMPLETE)) {
  94. fq_unlink(fq, f);
  95. atomic_dec(&fq->refcnt);
  96. fq->last_in |= INET_FRAG_COMPLETE;
  97. }
  98. }
  99. EXPORT_SYMBOL(inet_frag_kill);
  100. static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
  101. struct sk_buff *skb)
  102. {
  103. if (f->skb_free)
  104. f->skb_free(skb);
  105. kfree_skb(skb);
  106. }
  107. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
  108. int *work)
  109. {
  110. struct sk_buff *fp;
  111. struct netns_frags *nf;
  112. unsigned int sum, sum_truesize = 0;
  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. sum_truesize += fp->truesize;
  121. frag_kfree_skb(nf, f, fp);
  122. fp = xp;
  123. }
  124. sum = sum_truesize + f->qsize;
  125. if (work)
  126. *work -= sum;
  127. sub_frag_mem_limit(q, sum);
  128. if (f->destructor)
  129. f->destructor(q);
  130. kfree(q);
  131. }
  132. EXPORT_SYMBOL(inet_frag_destroy);
  133. int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force)
  134. {
  135. struct inet_frag_queue *q;
  136. int work, evicted = 0;
  137. if (!force) {
  138. if (frag_mem_limit(nf) <= nf->high_thresh)
  139. return 0;
  140. }
  141. work = frag_mem_limit(nf) - nf->low_thresh;
  142. while (work > 0) {
  143. spin_lock(&nf->lru_lock);
  144. if (list_empty(&nf->lru_list)) {
  145. spin_unlock(&nf->lru_lock);
  146. break;
  147. }
  148. q = list_first_entry(&nf->lru_list,
  149. struct inet_frag_queue, lru_list);
  150. atomic_inc(&q->refcnt);
  151. spin_unlock(&nf->lru_lock);
  152. spin_lock(&q->lock);
  153. if (!(q->last_in & INET_FRAG_COMPLETE))
  154. inet_frag_kill(q, f);
  155. spin_unlock(&q->lock);
  156. if (atomic_dec_and_test(&q->refcnt))
  157. inet_frag_destroy(q, f, &work);
  158. evicted++;
  159. }
  160. return evicted;
  161. }
  162. EXPORT_SYMBOL(inet_frag_evictor);
  163. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  164. struct inet_frag_queue *qp_in, struct inet_frags *f,
  165. void *arg)
  166. {
  167. struct inet_frag_queue *qp;
  168. #ifdef CONFIG_SMP
  169. struct hlist_node *n;
  170. #endif
  171. unsigned int hash;
  172. write_lock(&f->lock);
  173. /*
  174. * While we stayed w/o the lock other CPU could update
  175. * the rnd seed, so we need to re-calculate the hash
  176. * chain. Fortunatelly the qp_in can be used to get one.
  177. */
  178. hash = f->hashfn(qp_in);
  179. #ifdef CONFIG_SMP
  180. /* With SMP race we have to recheck hash table, because
  181. * such entry could be created on other cpu, while we
  182. * promoted read lock to write lock.
  183. */
  184. hlist_for_each_entry(qp, n, &f->hash[hash], list) {
  185. if (qp->net == nf && f->match(qp, arg)) {
  186. atomic_inc(&qp->refcnt);
  187. write_unlock(&f->lock);
  188. qp_in->last_in |= INET_FRAG_COMPLETE;
  189. inet_frag_put(qp_in, f);
  190. return qp;
  191. }
  192. }
  193. #endif
  194. qp = qp_in;
  195. if (!mod_timer(&qp->timer, jiffies + nf->timeout))
  196. atomic_inc(&qp->refcnt);
  197. atomic_inc(&qp->refcnt);
  198. hlist_add_head(&qp->list, &f->hash[hash]);
  199. nf->nqueues++;
  200. write_unlock(&f->lock);
  201. inet_frag_lru_add(nf, qp);
  202. return qp;
  203. }
  204. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  205. struct inet_frags *f, void *arg)
  206. {
  207. struct inet_frag_queue *q;
  208. q = kzalloc(f->qsize, GFP_ATOMIC);
  209. if (q == NULL)
  210. return NULL;
  211. q->net = nf;
  212. f->constructor(q, arg);
  213. add_frag_mem_limit(q, f->qsize);
  214. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  215. spin_lock_init(&q->lock);
  216. atomic_set(&q->refcnt, 1);
  217. return q;
  218. }
  219. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  220. struct inet_frags *f, void *arg)
  221. {
  222. struct inet_frag_queue *q;
  223. q = inet_frag_alloc(nf, f, arg);
  224. if (q == NULL)
  225. return NULL;
  226. return inet_frag_intern(nf, q, f, arg);
  227. }
  228. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  229. struct inet_frags *f, void *key, unsigned int hash)
  230. __releases(&f->lock)
  231. {
  232. struct inet_frag_queue *q;
  233. struct hlist_node *n;
  234. hlist_for_each_entry(q, n, &f->hash[hash], list) {
  235. if (q->net == nf && f->match(q, key)) {
  236. atomic_inc(&q->refcnt);
  237. read_unlock(&f->lock);
  238. return q;
  239. }
  240. }
  241. read_unlock(&f->lock);
  242. return inet_frag_create(nf, f, key);
  243. }
  244. EXPORT_SYMBOL(inet_frag_find);