inet_fragment.c 6.3 KB

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