inet_fragment.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include <net/inet_ecn.h>
  25. /* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
  26. * Value : 0xff if frame should be dropped.
  27. * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
  28. */
  29. const u8 ip_frag_ecn_table[16] = {
  30. /* at least one fragment had CE, and others ECT_0 or ECT_1 */
  31. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
  32. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  33. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  34. /* invalid combinations : drop frame */
  35. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
  36. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
  37. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
  38. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  39. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
  40. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
  41. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  42. };
  43. EXPORT_SYMBOL(ip_frag_ecn_table);
  44. static void inet_frag_secret_rebuild(unsigned long dummy)
  45. {
  46. struct inet_frags *f = (struct inet_frags *)dummy;
  47. unsigned long now = jiffies;
  48. int i;
  49. write_lock(&f->lock);
  50. get_random_bytes(&f->rnd, sizeof(u32));
  51. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  52. struct inet_frag_queue *q;
  53. struct hlist_node *n;
  54. hlist_for_each_entry_safe(q, n, &f->hash[i], list) {
  55. unsigned int hval = f->hashfn(q);
  56. if (hval != i) {
  57. hlist_del(&q->list);
  58. /* Relink to new hash chain. */
  59. hlist_add_head(&q->list, &f->hash[hval]);
  60. }
  61. }
  62. }
  63. write_unlock(&f->lock);
  64. mod_timer(&f->secret_timer, now + f->secret_interval);
  65. }
  66. void inet_frags_init(struct inet_frags *f)
  67. {
  68. int i;
  69. for (i = 0; i < INETFRAGS_HASHSZ; i++)
  70. INIT_HLIST_HEAD(&f->hash[i]);
  71. rwlock_init(&f->lock);
  72. f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  73. (jiffies ^ (jiffies >> 6)));
  74. setup_timer(&f->secret_timer, inet_frag_secret_rebuild,
  75. (unsigned long)f);
  76. f->secret_timer.expires = jiffies + f->secret_interval;
  77. add_timer(&f->secret_timer);
  78. }
  79. EXPORT_SYMBOL(inet_frags_init);
  80. void inet_frags_init_net(struct netns_frags *nf)
  81. {
  82. nf->nqueues = 0;
  83. init_frag_mem_limit(nf);
  84. INIT_LIST_HEAD(&nf->lru_list);
  85. spin_lock_init(&nf->lru_lock);
  86. }
  87. EXPORT_SYMBOL(inet_frags_init_net);
  88. void inet_frags_fini(struct inet_frags *f)
  89. {
  90. del_timer(&f->secret_timer);
  91. }
  92. EXPORT_SYMBOL(inet_frags_fini);
  93. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f)
  94. {
  95. nf->low_thresh = 0;
  96. local_bh_disable();
  97. inet_frag_evictor(nf, f, true);
  98. local_bh_enable();
  99. percpu_counter_destroy(&nf->mem);
  100. }
  101. EXPORT_SYMBOL(inet_frags_exit_net);
  102. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  103. {
  104. write_lock(&f->lock);
  105. hlist_del(&fq->list);
  106. write_unlock(&f->lock);
  107. inet_frag_lru_del(fq);
  108. }
  109. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  110. {
  111. if (del_timer(&fq->timer))
  112. atomic_dec(&fq->refcnt);
  113. if (!(fq->last_in & INET_FRAG_COMPLETE)) {
  114. fq_unlink(fq, f);
  115. atomic_dec(&fq->refcnt);
  116. fq->last_in |= INET_FRAG_COMPLETE;
  117. }
  118. }
  119. EXPORT_SYMBOL(inet_frag_kill);
  120. static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
  121. struct sk_buff *skb)
  122. {
  123. if (f->skb_free)
  124. f->skb_free(skb);
  125. kfree_skb(skb);
  126. }
  127. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
  128. int *work)
  129. {
  130. struct sk_buff *fp;
  131. struct netns_frags *nf;
  132. unsigned int sum, sum_truesize = 0;
  133. WARN_ON(!(q->last_in & INET_FRAG_COMPLETE));
  134. WARN_ON(del_timer(&q->timer) != 0);
  135. /* Release all fragment data. */
  136. fp = q->fragments;
  137. nf = q->net;
  138. while (fp) {
  139. struct sk_buff *xp = fp->next;
  140. sum_truesize += fp->truesize;
  141. frag_kfree_skb(nf, f, fp);
  142. fp = xp;
  143. }
  144. sum = sum_truesize + f->qsize;
  145. if (work)
  146. *work -= sum;
  147. sub_frag_mem_limit(q, sum);
  148. if (f->destructor)
  149. f->destructor(q);
  150. kfree(q);
  151. }
  152. EXPORT_SYMBOL(inet_frag_destroy);
  153. int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force)
  154. {
  155. struct inet_frag_queue *q;
  156. int work, evicted = 0;
  157. if (!force) {
  158. if (frag_mem_limit(nf) <= nf->high_thresh)
  159. return 0;
  160. }
  161. work = frag_mem_limit(nf) - nf->low_thresh;
  162. while (work > 0) {
  163. spin_lock(&nf->lru_lock);
  164. if (list_empty(&nf->lru_list)) {
  165. spin_unlock(&nf->lru_lock);
  166. break;
  167. }
  168. q = list_first_entry(&nf->lru_list,
  169. struct inet_frag_queue, lru_list);
  170. atomic_inc(&q->refcnt);
  171. /* Remove q from list to avoid several CPUs grabbing it */
  172. list_del_init(&q->lru_list);
  173. spin_unlock(&nf->lru_lock);
  174. spin_lock(&q->lock);
  175. if (!(q->last_in & INET_FRAG_COMPLETE))
  176. inet_frag_kill(q, f);
  177. spin_unlock(&q->lock);
  178. if (atomic_dec_and_test(&q->refcnt))
  179. inet_frag_destroy(q, f, &work);
  180. evicted++;
  181. }
  182. return evicted;
  183. }
  184. EXPORT_SYMBOL(inet_frag_evictor);
  185. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  186. struct inet_frag_queue *qp_in, struct inet_frags *f,
  187. void *arg)
  188. {
  189. struct inet_frag_queue *qp;
  190. #ifdef CONFIG_SMP
  191. #endif
  192. unsigned int hash;
  193. write_lock(&f->lock);
  194. /*
  195. * While we stayed w/o the lock other CPU could update
  196. * the rnd seed, so we need to re-calculate the hash
  197. * chain. Fortunatelly the qp_in can be used to get one.
  198. */
  199. hash = f->hashfn(qp_in);
  200. #ifdef CONFIG_SMP
  201. /* With SMP race we have to recheck hash table, because
  202. * such entry could be created on other cpu, while we
  203. * promoted read lock to write lock.
  204. */
  205. hlist_for_each_entry(qp, &f->hash[hash], list) {
  206. if (qp->net == nf && f->match(qp, arg)) {
  207. atomic_inc(&qp->refcnt);
  208. write_unlock(&f->lock);
  209. qp_in->last_in |= INET_FRAG_COMPLETE;
  210. inet_frag_put(qp_in, f);
  211. return qp;
  212. }
  213. }
  214. #endif
  215. qp = qp_in;
  216. if (!mod_timer(&qp->timer, jiffies + nf->timeout))
  217. atomic_inc(&qp->refcnt);
  218. atomic_inc(&qp->refcnt);
  219. hlist_add_head(&qp->list, &f->hash[hash]);
  220. write_unlock(&f->lock);
  221. inet_frag_lru_add(nf, qp);
  222. return qp;
  223. }
  224. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  225. struct inet_frags *f, void *arg)
  226. {
  227. struct inet_frag_queue *q;
  228. q = kzalloc(f->qsize, GFP_ATOMIC);
  229. if (q == NULL)
  230. return NULL;
  231. q->net = nf;
  232. f->constructor(q, arg);
  233. add_frag_mem_limit(q, f->qsize);
  234. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  235. spin_lock_init(&q->lock);
  236. atomic_set(&q->refcnt, 1);
  237. return q;
  238. }
  239. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  240. struct inet_frags *f, void *arg)
  241. {
  242. struct inet_frag_queue *q;
  243. q = inet_frag_alloc(nf, f, arg);
  244. if (q == NULL)
  245. return NULL;
  246. return inet_frag_intern(nf, q, f, arg);
  247. }
  248. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  249. struct inet_frags *f, void *key, unsigned int hash)
  250. __releases(&f->lock)
  251. {
  252. struct inet_frag_queue *q;
  253. int depth = 0;
  254. hlist_for_each_entry(q, &f->hash[hash], list) {
  255. if (q->net == nf && f->match(q, key)) {
  256. atomic_inc(&q->refcnt);
  257. read_unlock(&f->lock);
  258. return q;
  259. }
  260. depth++;
  261. }
  262. read_unlock(&f->lock);
  263. if (depth <= INETFRAGS_MAXDEPTH)
  264. return inet_frag_create(nf, f, key);
  265. else
  266. return ERR_PTR(-ENOBUFS);
  267. }
  268. EXPORT_SYMBOL(inet_frag_find);
  269. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  270. const char *prefix)
  271. {
  272. static const char msg[] = "inet_frag_find: Fragment hash bucket"
  273. " list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
  274. ". Dropping fragment.\n";
  275. if (PTR_ERR(q) == -ENOBUFS)
  276. LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg);
  277. }
  278. EXPORT_SYMBOL(inet_frag_maybe_warn_overflow);