inet_fragment.c 8.6 KB

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