inet_fragment.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #define INETFRAGS_EVICT_BUCKETS 128
  26. #define INETFRAGS_EVICT_MAX 512
  27. /* don't rebuild inetfrag table with new secret more often than this */
  28. #define INETFRAGS_MIN_REBUILD_INTERVAL (5 * HZ)
  29. /* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
  30. * Value : 0xff if frame should be dropped.
  31. * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
  32. */
  33. const u8 ip_frag_ecn_table[16] = {
  34. /* at least one fragment had CE, and others ECT_0 or ECT_1 */
  35. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
  36. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  37. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  38. /* invalid combinations : drop frame */
  39. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
  40. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
  41. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
  42. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  43. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
  44. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
  45. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  46. };
  47. EXPORT_SYMBOL(ip_frag_ecn_table);
  48. static unsigned int
  49. inet_frag_hashfn(const struct inet_frags *f, const struct inet_frag_queue *q)
  50. {
  51. return f->hashfn(q) & (INETFRAGS_HASHSZ - 1);
  52. }
  53. static bool inet_frag_may_rebuild(struct inet_frags *f)
  54. {
  55. return time_after(jiffies,
  56. f->last_rebuild_jiffies + INETFRAGS_MIN_REBUILD_INTERVAL);
  57. }
  58. static void inet_frag_secret_rebuild(struct inet_frags *f)
  59. {
  60. int i;
  61. write_seqlock_bh(&f->rnd_seqlock);
  62. if (!inet_frag_may_rebuild(f))
  63. goto out;
  64. get_random_bytes(&f->rnd, sizeof(u32));
  65. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  66. struct inet_frag_bucket *hb;
  67. struct inet_frag_queue *q;
  68. struct hlist_node *n;
  69. hb = &f->hash[i];
  70. spin_lock(&hb->chain_lock);
  71. hlist_for_each_entry_safe(q, n, &hb->chain, list) {
  72. unsigned int hval = inet_frag_hashfn(f, q);
  73. if (hval != i) {
  74. struct inet_frag_bucket *hb_dest;
  75. hlist_del(&q->list);
  76. /* Relink to new hash chain. */
  77. hb_dest = &f->hash[hval];
  78. /* This is the only place where we take
  79. * another chain_lock while already holding
  80. * one. As this will not run concurrently,
  81. * we cannot deadlock on hb_dest lock below, if its
  82. * already locked it will be released soon since
  83. * other caller cannot be waiting for hb lock
  84. * that we've taken above.
  85. */
  86. spin_lock_nested(&hb_dest->chain_lock,
  87. SINGLE_DEPTH_NESTING);
  88. hlist_add_head(&q->list, &hb_dest->chain);
  89. spin_unlock(&hb_dest->chain_lock);
  90. }
  91. }
  92. spin_unlock(&hb->chain_lock);
  93. }
  94. f->rebuild = false;
  95. f->last_rebuild_jiffies = jiffies;
  96. out:
  97. write_sequnlock_bh(&f->rnd_seqlock);
  98. }
  99. static bool inet_fragq_should_evict(const struct inet_frag_queue *q)
  100. {
  101. if (!hlist_unhashed(&q->list_evictor))
  102. return false;
  103. return q->net->low_thresh == 0 ||
  104. frag_mem_limit(q->net) >= q->net->low_thresh;
  105. }
  106. static unsigned int
  107. inet_evict_bucket(struct inet_frags *f, struct inet_frag_bucket *hb)
  108. {
  109. struct inet_frag_queue *fq;
  110. struct hlist_node *n;
  111. unsigned int evicted = 0;
  112. HLIST_HEAD(expired);
  113. spin_lock(&hb->chain_lock);
  114. hlist_for_each_entry_safe(fq, n, &hb->chain, list) {
  115. if (!inet_fragq_should_evict(fq))
  116. continue;
  117. if (!del_timer(&fq->timer))
  118. continue;
  119. hlist_add_head(&fq->list_evictor, &expired);
  120. ++evicted;
  121. }
  122. spin_unlock(&hb->chain_lock);
  123. hlist_for_each_entry_safe(fq, n, &expired, list_evictor)
  124. f->frag_expire((unsigned long) fq);
  125. return evicted;
  126. }
  127. static void inet_frag_worker(struct work_struct *work)
  128. {
  129. unsigned int budget = INETFRAGS_EVICT_BUCKETS;
  130. unsigned int i, evicted = 0;
  131. struct inet_frags *f;
  132. f = container_of(work, struct inet_frags, frags_work);
  133. BUILD_BUG_ON(INETFRAGS_EVICT_BUCKETS >= INETFRAGS_HASHSZ);
  134. local_bh_disable();
  135. for (i = ACCESS_ONCE(f->next_bucket); budget; --budget) {
  136. evicted += inet_evict_bucket(f, &f->hash[i]);
  137. i = (i + 1) & (INETFRAGS_HASHSZ - 1);
  138. if (evicted > INETFRAGS_EVICT_MAX)
  139. break;
  140. }
  141. f->next_bucket = i;
  142. local_bh_enable();
  143. if (f->rebuild && inet_frag_may_rebuild(f))
  144. inet_frag_secret_rebuild(f);
  145. }
  146. static void inet_frag_schedule_worker(struct inet_frags *f)
  147. {
  148. if (unlikely(!work_pending(&f->frags_work)))
  149. schedule_work(&f->frags_work);
  150. }
  151. int inet_frags_init(struct inet_frags *f)
  152. {
  153. int i;
  154. INIT_WORK(&f->frags_work, inet_frag_worker);
  155. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  156. struct inet_frag_bucket *hb = &f->hash[i];
  157. spin_lock_init(&hb->chain_lock);
  158. INIT_HLIST_HEAD(&hb->chain);
  159. }
  160. seqlock_init(&f->rnd_seqlock);
  161. f->last_rebuild_jiffies = 0;
  162. f->frags_cachep = kmem_cache_create(f->frags_cache_name, f->qsize, 0, 0,
  163. NULL);
  164. if (!f->frags_cachep)
  165. return -ENOMEM;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(inet_frags_init);
  169. void inet_frags_fini(struct inet_frags *f)
  170. {
  171. cancel_work_sync(&f->frags_work);
  172. kmem_cache_destroy(f->frags_cachep);
  173. }
  174. EXPORT_SYMBOL(inet_frags_fini);
  175. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f)
  176. {
  177. unsigned int seq;
  178. int i;
  179. nf->low_thresh = 0;
  180. evict_again:
  181. local_bh_disable();
  182. seq = read_seqbegin(&f->rnd_seqlock);
  183. for (i = 0; i < INETFRAGS_HASHSZ ; i++)
  184. inet_evict_bucket(f, &f->hash[i]);
  185. local_bh_enable();
  186. cond_resched();
  187. if (read_seqretry(&f->rnd_seqlock, seq) ||
  188. sum_frag_mem_limit(nf))
  189. goto evict_again;
  190. }
  191. EXPORT_SYMBOL(inet_frags_exit_net);
  192. static struct inet_frag_bucket *
  193. get_frag_bucket_locked(struct inet_frag_queue *fq, struct inet_frags *f)
  194. __acquires(hb->chain_lock)
  195. {
  196. struct inet_frag_bucket *hb;
  197. unsigned int seq, hash;
  198. restart:
  199. seq = read_seqbegin(&f->rnd_seqlock);
  200. hash = inet_frag_hashfn(f, fq);
  201. hb = &f->hash[hash];
  202. spin_lock(&hb->chain_lock);
  203. if (read_seqretry(&f->rnd_seqlock, seq)) {
  204. spin_unlock(&hb->chain_lock);
  205. goto restart;
  206. }
  207. return hb;
  208. }
  209. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  210. {
  211. struct inet_frag_bucket *hb;
  212. hb = get_frag_bucket_locked(fq, f);
  213. hlist_del(&fq->list);
  214. fq->flags |= INET_FRAG_COMPLETE;
  215. spin_unlock(&hb->chain_lock);
  216. }
  217. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  218. {
  219. if (del_timer(&fq->timer))
  220. atomic_dec(&fq->refcnt);
  221. if (!(fq->flags & INET_FRAG_COMPLETE)) {
  222. fq_unlink(fq, f);
  223. atomic_dec(&fq->refcnt);
  224. }
  225. }
  226. EXPORT_SYMBOL(inet_frag_kill);
  227. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f)
  228. {
  229. struct sk_buff *fp;
  230. struct netns_frags *nf;
  231. unsigned int sum, sum_truesize = 0;
  232. WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
  233. WARN_ON(del_timer(&q->timer) != 0);
  234. /* Release all fragment data. */
  235. fp = q->fragments;
  236. nf = q->net;
  237. while (fp) {
  238. struct sk_buff *xp = fp->next;
  239. sum_truesize += fp->truesize;
  240. kfree_skb(fp);
  241. fp = xp;
  242. }
  243. sum = sum_truesize + f->qsize;
  244. if (f->destructor)
  245. f->destructor(q);
  246. kmem_cache_free(f->frags_cachep, q);
  247. sub_frag_mem_limit(nf, sum);
  248. }
  249. EXPORT_SYMBOL(inet_frag_destroy);
  250. static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
  251. struct inet_frag_queue *qp_in,
  252. struct inet_frags *f,
  253. void *arg)
  254. {
  255. struct inet_frag_bucket *hb = get_frag_bucket_locked(qp_in, f);
  256. struct inet_frag_queue *qp;
  257. #ifdef CONFIG_SMP
  258. /* With SMP race we have to recheck hash table, because
  259. * such entry could have been created on other cpu before
  260. * we acquired hash bucket lock.
  261. */
  262. hlist_for_each_entry(qp, &hb->chain, list) {
  263. if (qp->net == nf && f->match(qp, arg)) {
  264. atomic_inc(&qp->refcnt);
  265. spin_unlock(&hb->chain_lock);
  266. qp_in->flags |= INET_FRAG_COMPLETE;
  267. inet_frag_put(qp_in, f);
  268. return qp;
  269. }
  270. }
  271. #endif
  272. qp = qp_in;
  273. if (!mod_timer(&qp->timer, jiffies + nf->timeout))
  274. atomic_inc(&qp->refcnt);
  275. atomic_inc(&qp->refcnt);
  276. hlist_add_head(&qp->list, &hb->chain);
  277. spin_unlock(&hb->chain_lock);
  278. return qp;
  279. }
  280. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  281. struct inet_frags *f,
  282. void *arg)
  283. {
  284. struct inet_frag_queue *q;
  285. if (!nf->high_thresh || frag_mem_limit(nf) > nf->high_thresh) {
  286. inet_frag_schedule_worker(f);
  287. return NULL;
  288. }
  289. q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
  290. if (!q)
  291. return NULL;
  292. q->net = nf;
  293. f->constructor(q, arg);
  294. add_frag_mem_limit(nf, f->qsize);
  295. setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
  296. spin_lock_init(&q->lock);
  297. atomic_set(&q->refcnt, 1);
  298. return q;
  299. }
  300. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  301. struct inet_frags *f,
  302. void *arg)
  303. {
  304. struct inet_frag_queue *q;
  305. q = inet_frag_alloc(nf, f, arg);
  306. if (!q)
  307. return NULL;
  308. return inet_frag_intern(nf, q, f, arg);
  309. }
  310. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  311. struct inet_frags *f, void *key,
  312. unsigned int hash)
  313. {
  314. struct inet_frag_bucket *hb;
  315. struct inet_frag_queue *q;
  316. int depth = 0;
  317. if (frag_mem_limit(nf) > nf->low_thresh)
  318. inet_frag_schedule_worker(f);
  319. hash &= (INETFRAGS_HASHSZ - 1);
  320. hb = &f->hash[hash];
  321. spin_lock(&hb->chain_lock);
  322. hlist_for_each_entry(q, &hb->chain, list) {
  323. if (q->net == nf && f->match(q, key)) {
  324. atomic_inc(&q->refcnt);
  325. spin_unlock(&hb->chain_lock);
  326. return q;
  327. }
  328. depth++;
  329. }
  330. spin_unlock(&hb->chain_lock);
  331. if (depth <= INETFRAGS_MAXDEPTH)
  332. return inet_frag_create(nf, f, key);
  333. if (inet_frag_may_rebuild(f)) {
  334. if (!f->rebuild)
  335. f->rebuild = true;
  336. inet_frag_schedule_worker(f);
  337. }
  338. return ERR_PTR(-ENOBUFS);
  339. }
  340. EXPORT_SYMBOL(inet_frag_find);
  341. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  342. const char *prefix)
  343. {
  344. static const char msg[] = "inet_frag_find: Fragment hash bucket"
  345. " list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
  346. ". Dropping fragment.\n";
  347. if (PTR_ERR(q) == -ENOBUFS)
  348. net_dbg_ratelimited("%s%s", prefix, msg);
  349. }
  350. EXPORT_SYMBOL(inet_frag_maybe_warn_overflow);