inet_fragment.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 *p, *n;
  34. hlist_for_each_entry_safe(q, p, 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. atomic_set(&nf->mem, 0);
  64. INIT_LIST_HEAD(&nf->lru_list);
  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);
  77. local_bh_enable();
  78. }
  79. EXPORT_SYMBOL(inet_frags_exit_net);
  80. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  81. {
  82. write_lock(&f->lock);
  83. hlist_del(&fq->list);
  84. list_del(&fq->lru_list);
  85. fq->net->nqueues--;
  86. write_unlock(&f->lock);
  87. }
  88. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  89. {
  90. if (del_timer(&fq->timer))
  91. atomic_dec(&fq->refcnt);
  92. if (!(fq->last_in & INET_FRAG_COMPLETE)) {
  93. fq_unlink(fq, f);
  94. atomic_dec(&fq->refcnt);
  95. fq->last_in |= INET_FRAG_COMPLETE;
  96. }
  97. }
  98. EXPORT_SYMBOL(inet_frag_kill);
  99. static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
  100. struct sk_buff *skb, int *work)
  101. {
  102. if (work)
  103. *work -= skb->truesize;
  104. atomic_sub(skb->truesize, &nf->mem);
  105. if (f->skb_free)
  106. f->skb_free(skb);
  107. kfree_skb(skb);
  108. }
  109. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
  110. int *work)
  111. {
  112. struct sk_buff *fp;
  113. struct netns_frags *nf;
  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. frag_kfree_skb(nf, f, fp, work);
  122. fp = xp;
  123. }
  124. if (work)
  125. *work -= f->qsize;
  126. atomic_sub(f->qsize, &nf->mem);
  127. if (f->destructor)
  128. f->destructor(q);
  129. kfree(q);
  130. }
  131. EXPORT_SYMBOL(inet_frag_destroy);
  132. int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f)
  133. {
  134. struct inet_frag_queue *q;
  135. int work, evicted = 0;
  136. work = atomic_read(&nf->mem) - nf->low_thresh;
  137. while (work > 0) {
  138. read_lock(&f->lock);
  139. if (list_empty(&nf->lru_list)) {
  140. read_unlock(&f->lock);
  141. break;
  142. }
  143. q = list_first_entry(&nf->lru_list,
  144. struct inet_frag_queue, lru_list);
  145. atomic_inc(&q->refcnt);
  146. read_unlock(&f->lock);
  147. spin_lock(&q->lock);
  148. if (!(q->last_in & INET_FRAG_COMPLETE))
  149. inet_frag_kill(q, f);
  150. spin_unlock(&q->lock);
  151. if (atomic_dec_and_test(&q->refcnt))
  152. inet_frag_destroy(q, f, &work);
  153. evicted++;
  154. }
  155. return evicted;
  156. }
  157. EXPORT_SYMBOL(inet_frag_evictor);
  158. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  159. struct inet_frag_queue *qp_in, struct inet_frags *f,
  160. void *arg)
  161. {
  162. struct inet_frag_queue *qp;
  163. #ifdef CONFIG_SMP
  164. struct hlist_node *n;
  165. #endif
  166. unsigned int hash;
  167. write_lock(&f->lock);
  168. /*
  169. * While we stayed w/o the lock other CPU could update
  170. * the rnd seed, so we need to re-calculate the hash
  171. * chain. Fortunatelly the qp_in can be used to get one.
  172. */
  173. hash = f->hashfn(qp_in);
  174. #ifdef CONFIG_SMP
  175. /* With SMP race we have to recheck hash table, because
  176. * such entry could be created on other cpu, while we
  177. * promoted read lock to write lock.
  178. */
  179. hlist_for_each_entry(qp, n, &f->hash[hash], list) {
  180. if (qp->net == nf && f->match(qp, arg)) {
  181. atomic_inc(&qp->refcnt);
  182. write_unlock(&f->lock);
  183. qp_in->last_in |= INET_FRAG_COMPLETE;
  184. inet_frag_put(qp_in, f);
  185. return qp;
  186. }
  187. }
  188. #endif
  189. qp = qp_in;
  190. if (!mod_timer(&qp->timer, jiffies + nf->timeout))
  191. atomic_inc(&qp->refcnt);
  192. atomic_inc(&qp->refcnt);
  193. hlist_add_head(&qp->list, &f->hash[hash]);
  194. list_add_tail(&qp->lru_list, &nf->lru_list);
  195. nf->nqueues++;
  196. write_unlock(&f->lock);
  197. return qp;
  198. }
  199. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  200. struct inet_frags *f, void *arg)
  201. {
  202. struct inet_frag_queue *q;
  203. q = kzalloc(f->qsize, GFP_ATOMIC);
  204. if (q == NULL)
  205. return NULL;
  206. f->constructor(q, arg);
  207. atomic_add(f->qsize, &nf->mem);
  208. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  209. spin_lock_init(&q->lock);
  210. atomic_set(&q->refcnt, 1);
  211. q->net = nf;
  212. return q;
  213. }
  214. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  215. struct inet_frags *f, void *arg)
  216. {
  217. struct inet_frag_queue *q;
  218. q = inet_frag_alloc(nf, f, arg);
  219. if (q == NULL)
  220. return NULL;
  221. return inet_frag_intern(nf, q, f, arg);
  222. }
  223. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  224. struct inet_frags *f, void *key, unsigned int hash)
  225. __releases(&f->lock)
  226. {
  227. struct inet_frag_queue *q;
  228. struct hlist_node *n;
  229. int depth = 0;
  230. hlist_for_each_entry(q, n, &f->hash[hash], list) {
  231. if (q->net == nf && f->match(q, key)) {
  232. atomic_inc(&q->refcnt);
  233. read_unlock(&f->lock);
  234. return q;
  235. }
  236. depth++;
  237. }
  238. read_unlock(&f->lock);
  239. if (depth <= INETFRAGS_MAXDEPTH)
  240. return inet_frag_create(nf, f, key);
  241. else
  242. return ERR_PTR(-ENOBUFS);
  243. }
  244. EXPORT_SYMBOL(inet_frag_find);
  245. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  246. const char *prefix)
  247. {
  248. static const char msg[] = "inet_frag_find: Fragment hash bucket"
  249. " list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
  250. ". Dropping fragment.\n";
  251. if (PTR_ERR(q) == -ENOBUFS)
  252. LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg);
  253. }
  254. EXPORT_SYMBOL(inet_frag_maybe_warn_overflow);