inet_fragment.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/sock.h>
  23. #include <net/inet_frag.h>
  24. static void inet_frag_secret_rebuild(unsigned long dummy)
  25. {
  26. struct inet_frags *f = (struct inet_frags *)dummy;
  27. unsigned long now = jiffies;
  28. int i;
  29. write_lock(&f->lock);
  30. get_random_bytes(&f->rnd, sizeof(u32));
  31. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  32. struct inet_frag_queue *q;
  33. struct hlist_node *n;
  34. hlist_for_each_entry_safe(q, n, &f->hash[i], list) {
  35. unsigned int hval = f->hashfn(q);
  36. if (hval != i) {
  37. hlist_del(&q->list);
  38. /* Relink to new hash chain. */
  39. hlist_add_head(&q->list, &f->hash[hval]);
  40. }
  41. }
  42. }
  43. write_unlock(&f->lock);
  44. mod_timer(&f->secret_timer, now + f->secret_interval);
  45. }
  46. void inet_frags_init(struct inet_frags *f)
  47. {
  48. int i;
  49. for (i = 0; i < INETFRAGS_HASHSZ; i++)
  50. INIT_HLIST_HEAD(&f->hash[i]);
  51. rwlock_init(&f->lock);
  52. f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  53. (jiffies ^ (jiffies >> 6)));
  54. setup_timer(&f->secret_timer, inet_frag_secret_rebuild,
  55. (unsigned long)f);
  56. f->secret_timer.expires = jiffies + f->secret_interval;
  57. add_timer(&f->secret_timer);
  58. }
  59. EXPORT_SYMBOL(inet_frags_init);
  60. void inet_frags_init_net(struct netns_frags *nf)
  61. {
  62. nf->nqueues = 0;
  63. init_frag_mem_limit(nf);
  64. INIT_LIST_HEAD(&nf->lru_list);
  65. spin_lock_init(&nf->lru_lock);
  66. }
  67. EXPORT_SYMBOL(inet_frags_init_net);
  68. void inet_frags_fini(struct inet_frags *f)
  69. {
  70. del_timer(&f->secret_timer);
  71. }
  72. EXPORT_SYMBOL(inet_frags_fini);
  73. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f)
  74. {
  75. nf->low_thresh = 0;
  76. local_bh_disable();
  77. inet_frag_evictor(nf, f, true);
  78. local_bh_enable();
  79. percpu_counter_destroy(&nf->mem);
  80. }
  81. EXPORT_SYMBOL(inet_frags_exit_net);
  82. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  83. {
  84. write_lock(&f->lock);
  85. hlist_del(&fq->list);
  86. fq->net->nqueues--;
  87. write_unlock(&f->lock);
  88. inet_frag_lru_del(fq);
  89. }
  90. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  91. {
  92. if (del_timer(&fq->timer))
  93. atomic_dec(&fq->refcnt);
  94. if (!(fq->last_in & INET_FRAG_COMPLETE)) {
  95. fq_unlink(fq, f);
  96. atomic_dec(&fq->refcnt);
  97. fq->last_in |= INET_FRAG_COMPLETE;
  98. }
  99. }
  100. EXPORT_SYMBOL(inet_frag_kill);
  101. static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
  102. struct sk_buff *skb)
  103. {
  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. unsigned int sum, sum_truesize = 0;
  114. WARN_ON(!(q->last_in & INET_FRAG_COMPLETE));
  115. WARN_ON(del_timer(&q->timer) != 0);
  116. /* Release all fragment data. */
  117. fp = q->fragments;
  118. nf = q->net;
  119. while (fp) {
  120. struct sk_buff *xp = fp->next;
  121. sum_truesize += fp->truesize;
  122. frag_kfree_skb(nf, f, fp);
  123. fp = xp;
  124. }
  125. sum = sum_truesize + f->qsize;
  126. if (work)
  127. *work -= sum;
  128. sub_frag_mem_limit(q, sum);
  129. if (f->destructor)
  130. f->destructor(q);
  131. kfree(q);
  132. }
  133. EXPORT_SYMBOL(inet_frag_destroy);
  134. int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force)
  135. {
  136. struct inet_frag_queue *q;
  137. int work, evicted = 0;
  138. if (!force) {
  139. if (frag_mem_limit(nf) <= nf->high_thresh)
  140. return 0;
  141. }
  142. work = frag_mem_limit(nf) - nf->low_thresh;
  143. while (work > 0) {
  144. spin_lock(&nf->lru_lock);
  145. if (list_empty(&nf->lru_list)) {
  146. spin_unlock(&nf->lru_lock);
  147. break;
  148. }
  149. q = list_first_entry(&nf->lru_list,
  150. struct inet_frag_queue, lru_list);
  151. atomic_inc(&q->refcnt);
  152. spin_unlock(&nf->lru_lock);
  153. spin_lock(&q->lock);
  154. if (!(q->last_in & INET_FRAG_COMPLETE))
  155. inet_frag_kill(q, f);
  156. spin_unlock(&q->lock);
  157. if (atomic_dec_and_test(&q->refcnt))
  158. inet_frag_destroy(q, f, &work);
  159. evicted++;
  160. }
  161. return evicted;
  162. }
  163. EXPORT_SYMBOL(inet_frag_evictor);
  164. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  165. struct inet_frag_queue *qp_in, struct inet_frags *f,
  166. void *arg)
  167. {
  168. struct inet_frag_queue *qp;
  169. #ifdef CONFIG_SMP
  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, &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. int depth = 0;
  234. hlist_for_each_entry(q, &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. depth++;
  241. }
  242. read_unlock(&f->lock);
  243. if (depth <= INETFRAGS_MAXDEPTH)
  244. return inet_frag_create(nf, f, key);
  245. else
  246. return ERR_PTR(-ENOBUFS);
  247. }
  248. EXPORT_SYMBOL(inet_frag_find);
  249. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  250. const char *prefix)
  251. {
  252. static const char msg[] = "inet_frag_find: Fragment hash bucket"
  253. " list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
  254. ". Dropping fragment.\n";
  255. if (PTR_ERR(q) == -ENOBUFS)
  256. LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg);
  257. }
  258. EXPORT_SYMBOL(inet_frag_maybe_warn_overflow);