inet_fragment.c 8.7 KB

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