inet_fragment.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <linux/rhashtable.h>
  23. #include <net/sock.h>
  24. #include <net/inet_frag.h>
  25. #include <net/inet_ecn.h>
  26. #include <net/ip.h>
  27. #include <net/ipv6.h>
  28. /* Use skb->cb to track consecutive/adjacent fragments coming at
  29. * the end of the queue. Nodes in the rb-tree queue will
  30. * contain "runs" of one or more adjacent fragments.
  31. *
  32. * Invariants:
  33. * - next_frag is NULL at the tail of a "run";
  34. * - the head of a "run" has the sum of all fragment lengths in frag_run_len.
  35. */
  36. struct ipfrag_skb_cb {
  37. union {
  38. struct inet_skb_parm h4;
  39. struct inet6_skb_parm h6;
  40. };
  41. struct sk_buff *next_frag;
  42. int frag_run_len;
  43. };
  44. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  45. static void fragcb_clear(struct sk_buff *skb)
  46. {
  47. RB_CLEAR_NODE(&skb->rbnode);
  48. FRAG_CB(skb)->next_frag = NULL;
  49. FRAG_CB(skb)->frag_run_len = skb->len;
  50. }
  51. /* Append skb to the last "run". */
  52. static void fragrun_append_to_last(struct inet_frag_queue *q,
  53. struct sk_buff *skb)
  54. {
  55. fragcb_clear(skb);
  56. FRAG_CB(q->last_run_head)->frag_run_len += skb->len;
  57. FRAG_CB(q->fragments_tail)->next_frag = skb;
  58. q->fragments_tail = skb;
  59. }
  60. /* Create a new "run" with the skb. */
  61. static void fragrun_create(struct inet_frag_queue *q, struct sk_buff *skb)
  62. {
  63. BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb));
  64. fragcb_clear(skb);
  65. if (q->last_run_head)
  66. rb_link_node(&skb->rbnode, &q->last_run_head->rbnode,
  67. &q->last_run_head->rbnode.rb_right);
  68. else
  69. rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node);
  70. rb_insert_color(&skb->rbnode, &q->rb_fragments);
  71. q->fragments_tail = skb;
  72. q->last_run_head = skb;
  73. }
  74. /* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
  75. * Value : 0xff if frame should be dropped.
  76. * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
  77. */
  78. const u8 ip_frag_ecn_table[16] = {
  79. /* at least one fragment had CE, and others ECT_0 or ECT_1 */
  80. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
  81. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  82. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  83. /* invalid combinations : drop frame */
  84. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
  85. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
  86. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
  87. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  88. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
  89. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
  90. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  91. };
  92. EXPORT_SYMBOL(ip_frag_ecn_table);
  93. int inet_frags_init(struct inet_frags *f)
  94. {
  95. f->frags_cachep = kmem_cache_create(f->frags_cache_name, f->qsize, 0, 0,
  96. NULL);
  97. if (!f->frags_cachep)
  98. return -ENOMEM;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(inet_frags_init);
  102. void inet_frags_fini(struct inet_frags *f)
  103. {
  104. /* We must wait that all inet_frag_destroy_rcu() have completed. */
  105. rcu_barrier();
  106. kmem_cache_destroy(f->frags_cachep);
  107. f->frags_cachep = NULL;
  108. }
  109. EXPORT_SYMBOL(inet_frags_fini);
  110. static void inet_frags_free_cb(void *ptr, void *arg)
  111. {
  112. struct inet_frag_queue *fq = ptr;
  113. /* If we can not cancel the timer, it means this frag_queue
  114. * is already disappearing, we have nothing to do.
  115. * Otherwise, we own a refcount until the end of this function.
  116. */
  117. if (!del_timer(&fq->timer))
  118. return;
  119. spin_lock_bh(&fq->lock);
  120. if (!(fq->flags & INET_FRAG_COMPLETE)) {
  121. fq->flags |= INET_FRAG_COMPLETE;
  122. refcount_dec(&fq->refcnt);
  123. }
  124. spin_unlock_bh(&fq->lock);
  125. inet_frag_put(fq);
  126. }
  127. void inet_frags_exit_net(struct netns_frags *nf)
  128. {
  129. nf->high_thresh = 0; /* prevent creation of new frags */
  130. rhashtable_free_and_destroy(&nf->rhashtable, inet_frags_free_cb, NULL);
  131. }
  132. EXPORT_SYMBOL(inet_frags_exit_net);
  133. void inet_frag_kill(struct inet_frag_queue *fq)
  134. {
  135. if (del_timer(&fq->timer))
  136. refcount_dec(&fq->refcnt);
  137. if (!(fq->flags & INET_FRAG_COMPLETE)) {
  138. struct netns_frags *nf = fq->net;
  139. fq->flags |= INET_FRAG_COMPLETE;
  140. rhashtable_remove_fast(&nf->rhashtable, &fq->node, nf->f->rhash_params);
  141. refcount_dec(&fq->refcnt);
  142. }
  143. }
  144. EXPORT_SYMBOL(inet_frag_kill);
  145. static void inet_frag_destroy_rcu(struct rcu_head *head)
  146. {
  147. struct inet_frag_queue *q = container_of(head, struct inet_frag_queue,
  148. rcu);
  149. struct inet_frags *f = q->net->f;
  150. if (f->destructor)
  151. f->destructor(q);
  152. kmem_cache_free(f->frags_cachep, q);
  153. }
  154. unsigned int inet_frag_rbtree_purge(struct rb_root *root)
  155. {
  156. struct rb_node *p = rb_first(root);
  157. unsigned int sum = 0;
  158. while (p) {
  159. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  160. p = rb_next(p);
  161. rb_erase(&skb->rbnode, root);
  162. while (skb) {
  163. struct sk_buff *next = FRAG_CB(skb)->next_frag;
  164. sum += skb->truesize;
  165. kfree_skb(skb);
  166. skb = next;
  167. }
  168. }
  169. return sum;
  170. }
  171. EXPORT_SYMBOL(inet_frag_rbtree_purge);
  172. void inet_frag_destroy(struct inet_frag_queue *q)
  173. {
  174. struct sk_buff *fp;
  175. struct netns_frags *nf;
  176. unsigned int sum, sum_truesize = 0;
  177. struct inet_frags *f;
  178. WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
  179. WARN_ON(del_timer(&q->timer) != 0);
  180. /* Release all fragment data. */
  181. fp = q->fragments;
  182. nf = q->net;
  183. f = nf->f;
  184. if (fp) {
  185. do {
  186. struct sk_buff *xp = fp->next;
  187. sum_truesize += fp->truesize;
  188. kfree_skb(fp);
  189. fp = xp;
  190. } while (fp);
  191. } else {
  192. sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments);
  193. }
  194. sum = sum_truesize + f->qsize;
  195. call_rcu(&q->rcu, inet_frag_destroy_rcu);
  196. sub_frag_mem_limit(nf, sum);
  197. }
  198. EXPORT_SYMBOL(inet_frag_destroy);
  199. static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
  200. struct inet_frags *f,
  201. void *arg)
  202. {
  203. struct inet_frag_queue *q;
  204. q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
  205. if (!q)
  206. return NULL;
  207. q->net = nf;
  208. f->constructor(q, arg);
  209. add_frag_mem_limit(nf, f->qsize);
  210. timer_setup(&q->timer, f->frag_expire, 0);
  211. spin_lock_init(&q->lock);
  212. refcount_set(&q->refcnt, 3);
  213. return q;
  214. }
  215. static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
  216. void *arg,
  217. struct inet_frag_queue **prev)
  218. {
  219. struct inet_frags *f = nf->f;
  220. struct inet_frag_queue *q;
  221. q = inet_frag_alloc(nf, f, arg);
  222. if (!q) {
  223. *prev = ERR_PTR(-ENOMEM);
  224. return NULL;
  225. }
  226. mod_timer(&q->timer, jiffies + nf->timeout);
  227. *prev = rhashtable_lookup_get_insert_key(&nf->rhashtable, &q->key,
  228. &q->node, f->rhash_params);
  229. if (*prev) {
  230. q->flags |= INET_FRAG_COMPLETE;
  231. inet_frag_kill(q);
  232. inet_frag_destroy(q);
  233. return NULL;
  234. }
  235. return q;
  236. }
  237. /* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */
  238. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key)
  239. {
  240. struct inet_frag_queue *fq = NULL, *prev;
  241. if (!nf->high_thresh || frag_mem_limit(nf) > nf->high_thresh)
  242. return NULL;
  243. rcu_read_lock();
  244. prev = rhashtable_lookup(&nf->rhashtable, key, nf->f->rhash_params);
  245. if (!prev)
  246. fq = inet_frag_create(nf, key, &prev);
  247. if (prev && !IS_ERR(prev)) {
  248. fq = prev;
  249. if (!refcount_inc_not_zero(&fq->refcnt))
  250. fq = NULL;
  251. }
  252. rcu_read_unlock();
  253. return fq;
  254. }
  255. EXPORT_SYMBOL(inet_frag_find);
  256. int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
  257. int offset, int end)
  258. {
  259. struct sk_buff *last = q->fragments_tail;
  260. /* RFC5722, Section 4, amended by Errata ID : 3089
  261. * When reassembling an IPv6 datagram, if
  262. * one or more its constituent fragments is determined to be an
  263. * overlapping fragment, the entire datagram (and any constituent
  264. * fragments) MUST be silently discarded.
  265. *
  266. * Duplicates, however, should be ignored (i.e. skb dropped, but the
  267. * queue/fragments kept for later reassembly).
  268. */
  269. if (!last)
  270. fragrun_create(q, skb); /* First fragment. */
  271. else if (last->ip_defrag_offset + last->len < end) {
  272. /* This is the common case: skb goes to the end. */
  273. /* Detect and discard overlaps. */
  274. if (offset < last->ip_defrag_offset + last->len)
  275. return IPFRAG_OVERLAP;
  276. if (offset == last->ip_defrag_offset + last->len)
  277. fragrun_append_to_last(q, skb);
  278. else
  279. fragrun_create(q, skb);
  280. } else {
  281. /* Binary search. Note that skb can become the first fragment,
  282. * but not the last (covered above).
  283. */
  284. struct rb_node **rbn, *parent;
  285. rbn = &q->rb_fragments.rb_node;
  286. do {
  287. struct sk_buff *curr;
  288. int curr_run_end;
  289. parent = *rbn;
  290. curr = rb_to_skb(parent);
  291. curr_run_end = curr->ip_defrag_offset +
  292. FRAG_CB(curr)->frag_run_len;
  293. if (end <= curr->ip_defrag_offset)
  294. rbn = &parent->rb_left;
  295. else if (offset >= curr_run_end)
  296. rbn = &parent->rb_right;
  297. else if (offset >= curr->ip_defrag_offset &&
  298. end <= curr_run_end)
  299. return IPFRAG_DUP;
  300. else
  301. return IPFRAG_OVERLAP;
  302. } while (*rbn);
  303. /* Here we have parent properly set, and rbn pointing to
  304. * one of its NULL left/right children. Insert skb.
  305. */
  306. fragcb_clear(skb);
  307. rb_link_node(&skb->rbnode, parent, rbn);
  308. rb_insert_color(&skb->rbnode, &q->rb_fragments);
  309. }
  310. skb->ip_defrag_offset = offset;
  311. return IPFRAG_OK;
  312. }
  313. EXPORT_SYMBOL(inet_frag_queue_insert);
  314. void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
  315. struct sk_buff *parent)
  316. {
  317. struct sk_buff *fp, *head = skb_rb_first(&q->rb_fragments);
  318. struct sk_buff **nextp;
  319. int delta;
  320. if (head != skb) {
  321. fp = skb_clone(skb, GFP_ATOMIC);
  322. if (!fp)
  323. return NULL;
  324. FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
  325. if (RB_EMPTY_NODE(&skb->rbnode))
  326. FRAG_CB(parent)->next_frag = fp;
  327. else
  328. rb_replace_node(&skb->rbnode, &fp->rbnode,
  329. &q->rb_fragments);
  330. if (q->fragments_tail == skb)
  331. q->fragments_tail = fp;
  332. skb_morph(skb, head);
  333. FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
  334. rb_replace_node(&head->rbnode, &skb->rbnode,
  335. &q->rb_fragments);
  336. consume_skb(head);
  337. head = skb;
  338. }
  339. WARN_ON(head->ip_defrag_offset != 0);
  340. delta = -head->truesize;
  341. /* Head of list must not be cloned. */
  342. if (skb_unclone(head, GFP_ATOMIC))
  343. return NULL;
  344. delta += head->truesize;
  345. if (delta)
  346. add_frag_mem_limit(q->net, delta);
  347. /* If the first fragment is fragmented itself, we split
  348. * it to two chunks: the first with data and paged part
  349. * and the second, holding only fragments.
  350. */
  351. if (skb_has_frag_list(head)) {
  352. struct sk_buff *clone;
  353. int i, plen = 0;
  354. clone = alloc_skb(0, GFP_ATOMIC);
  355. if (!clone)
  356. return NULL;
  357. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  358. skb_frag_list_init(head);
  359. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  360. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  361. clone->data_len = head->data_len - plen;
  362. clone->len = clone->data_len;
  363. head->truesize += clone->truesize;
  364. clone->csum = 0;
  365. clone->ip_summed = head->ip_summed;
  366. add_frag_mem_limit(q->net, clone->truesize);
  367. skb_shinfo(head)->frag_list = clone;
  368. nextp = &clone->next;
  369. } else {
  370. nextp = &skb_shinfo(head)->frag_list;
  371. }
  372. return nextp;
  373. }
  374. EXPORT_SYMBOL(inet_frag_reasm_prepare);
  375. void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
  376. void *reasm_data)
  377. {
  378. struct sk_buff **nextp = (struct sk_buff **)reasm_data;
  379. struct rb_node *rbn;
  380. struct sk_buff *fp;
  381. skb_push(head, head->data - skb_network_header(head));
  382. /* Traverse the tree in order, to build frag_list. */
  383. fp = FRAG_CB(head)->next_frag;
  384. rbn = rb_next(&head->rbnode);
  385. rb_erase(&head->rbnode, &q->rb_fragments);
  386. while (rbn || fp) {
  387. /* fp points to the next sk_buff in the current run;
  388. * rbn points to the next run.
  389. */
  390. /* Go through the current run. */
  391. while (fp) {
  392. *nextp = fp;
  393. nextp = &fp->next;
  394. fp->prev = NULL;
  395. memset(&fp->rbnode, 0, sizeof(fp->rbnode));
  396. fp->sk = NULL;
  397. head->data_len += fp->len;
  398. head->len += fp->len;
  399. if (head->ip_summed != fp->ip_summed)
  400. head->ip_summed = CHECKSUM_NONE;
  401. else if (head->ip_summed == CHECKSUM_COMPLETE)
  402. head->csum = csum_add(head->csum, fp->csum);
  403. head->truesize += fp->truesize;
  404. fp = FRAG_CB(fp)->next_frag;
  405. }
  406. /* Move to the next run. */
  407. if (rbn) {
  408. struct rb_node *rbnext = rb_next(rbn);
  409. fp = rb_to_skb(rbn);
  410. rb_erase(rbn, &q->rb_fragments);
  411. rbn = rbnext;
  412. }
  413. }
  414. sub_frag_mem_limit(q->net, head->truesize);
  415. *nextp = NULL;
  416. skb_mark_not_on_list(head);
  417. head->prev = NULL;
  418. head->tstamp = q->stamp;
  419. }
  420. EXPORT_SYMBOL(inet_frag_reasm_finish);
  421. struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q)
  422. {
  423. struct sk_buff *head;
  424. if (q->fragments) {
  425. head = q->fragments;
  426. q->fragments = head->next;
  427. } else {
  428. struct sk_buff *skb;
  429. head = skb_rb_first(&q->rb_fragments);
  430. if (!head)
  431. return NULL;
  432. skb = FRAG_CB(head)->next_frag;
  433. if (skb)
  434. rb_replace_node(&head->rbnode, &skb->rbnode,
  435. &q->rb_fragments);
  436. else
  437. rb_erase(&head->rbnode, &q->rb_fragments);
  438. memset(&head->rbnode, 0, sizeof(head->rbnode));
  439. barrier();
  440. }
  441. if (head == q->fragments_tail)
  442. q->fragments_tail = NULL;
  443. sub_frag_mem_limit(q->net, head->truesize);
  444. return head;
  445. }
  446. EXPORT_SYMBOL(inet_frag_pull_head);