inet_fragment.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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, true);
  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, bool force)
  132. {
  133. struct inet_frag_queue *q;
  134. int work, evicted = 0;
  135. if (!force) {
  136. if (atomic_read(&nf->mem) <= nf->high_thresh)
  137. return 0;
  138. }
  139. work = atomic_read(&nf->mem) - nf->low_thresh;
  140. while (work > 0) {
  141. read_lock(&f->lock);
  142. if (list_empty(&nf->lru_list)) {
  143. read_unlock(&f->lock);
  144. break;
  145. }
  146. q = list_first_entry(&nf->lru_list,
  147. struct inet_frag_queue, lru_list);
  148. atomic_inc(&q->refcnt);
  149. read_unlock(&f->lock);
  150. spin_lock(&q->lock);
  151. if (!(q->last_in & INET_FRAG_COMPLETE))
  152. inet_frag_kill(q, f);
  153. spin_unlock(&q->lock);
  154. if (atomic_dec_and_test(&q->refcnt))
  155. inet_frag_destroy(q, f, &work);
  156. evicted++;
  157. }
  158. return evicted;
  159. }
  160. EXPORT_SYMBOL(inet_frag_evictor);
  161. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  162. struct inet_frag_queue *qp_in, struct inet_frags *f,
  163. void *arg)
  164. {
  165. struct inet_frag_queue *qp;
  166. #ifdef CONFIG_SMP
  167. struct hlist_node *n;
  168. #endif
  169. unsigned int hash;
  170. write_lock(&f->lock);
  171. /*
  172. * While we stayed w/o the lock other CPU could update
  173. * the rnd seed, so we need to re-calculate the hash
  174. * chain. Fortunatelly the qp_in can be used to get one.
  175. */
  176. hash = f->hashfn(qp_in);
  177. #ifdef CONFIG_SMP
  178. /* With SMP race we have to recheck hash table, because
  179. * such entry could be created on other cpu, while we
  180. * promoted read lock to write lock.
  181. */
  182. hlist_for_each_entry(qp, n, &f->hash[hash], list) {
  183. if (qp->net == nf && f->match(qp, arg)) {
  184. atomic_inc(&qp->refcnt);
  185. write_unlock(&f->lock);
  186. qp_in->last_in |= INET_FRAG_COMPLETE;
  187. inet_frag_put(qp_in, f);
  188. return qp;
  189. }
  190. }
  191. #endif
  192. qp = qp_in;
  193. if (!mod_timer(&qp->timer, jiffies + nf->timeout))
  194. atomic_inc(&qp->refcnt);
  195. atomic_inc(&qp->refcnt);
  196. hlist_add_head(&qp->list, &f->hash[hash]);
  197. list_add_tail(&qp->lru_list, &nf->lru_list);
  198. nf->nqueues++;
  199. write_unlock(&f->lock);
  200. return qp;
  201. }
  202. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  203. struct inet_frags *f, void *arg)
  204. {
  205. struct inet_frag_queue *q;
  206. q = kzalloc(f->qsize, GFP_ATOMIC);
  207. if (q == NULL)
  208. return NULL;
  209. q->net = nf;
  210. f->constructor(q, arg);
  211. atomic_add(f->qsize, &nf->mem);
  212. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  213. spin_lock_init(&q->lock);
  214. atomic_set(&q->refcnt, 1);
  215. return q;
  216. }
  217. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  218. struct inet_frags *f, void *arg)
  219. {
  220. struct inet_frag_queue *q;
  221. q = inet_frag_alloc(nf, f, arg);
  222. if (q == NULL)
  223. return NULL;
  224. return inet_frag_intern(nf, q, f, arg);
  225. }
  226. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  227. struct inet_frags *f, void *key, unsigned int hash)
  228. __releases(&f->lock)
  229. {
  230. struct inet_frag_queue *q;
  231. struct hlist_node *n;
  232. hlist_for_each_entry(q, n, &f->hash[hash], list) {
  233. if (q->net == nf && f->match(q, key)) {
  234. atomic_inc(&q->refcnt);
  235. read_unlock(&f->lock);
  236. return q;
  237. }
  238. }
  239. read_unlock(&f->lock);
  240. return inet_frag_create(nf, f, key);
  241. }
  242. EXPORT_SYMBOL(inet_frag_find);