ip_fragment.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * The IP fragmentation functionality.
  8. *
  9. * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
  10. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  11. *
  12. * Fixes:
  13. * Alan Cox : Split from ip.c , see ip_input.c for history.
  14. * David S. Miller : Begin massive cleanup...
  15. * Andi Kleen : Add sysctls.
  16. * xxxx : Overlapfrag bug.
  17. * Ultima : ip_expire() kernel panic.
  18. * Bill Hawes : Frag accounting and evictor fixes.
  19. * John McDonald : 0 length frag bug.
  20. * Alexey Kuznetsov: SMP races, threading, cleanup.
  21. * Patrick McHardy : LRU queue of frag heads for evictor.
  22. */
  23. #define pr_fmt(fmt) "IPv4: " fmt
  24. #include <linux/compiler.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/mm.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/list.h>
  31. #include <linux/ip.h>
  32. #include <linux/icmp.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/jhash.h>
  35. #include <linux/random.h>
  36. #include <linux/slab.h>
  37. #include <net/route.h>
  38. #include <net/dst.h>
  39. #include <net/sock.h>
  40. #include <net/ip.h>
  41. #include <net/icmp.h>
  42. #include <net/checksum.h>
  43. #include <net/inetpeer.h>
  44. #include <net/inet_frag.h>
  45. #include <linux/tcp.h>
  46. #include <linux/udp.h>
  47. #include <linux/inet.h>
  48. #include <linux/netfilter_ipv4.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/l3mdev.h>
  51. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  52. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  53. * as well. Or notify me, at least. --ANK
  54. */
  55. static const char ip_frag_cache_name[] = "ip4-frags";
  56. /* Use skb->cb to track consecutive/adjacent fragments coming at
  57. * the end of the queue. Nodes in the rb-tree queue will
  58. * contain "runs" of one or more adjacent fragments.
  59. *
  60. * Invariants:
  61. * - next_frag is NULL at the tail of a "run";
  62. * - the head of a "run" has the sum of all fragment lengths in frag_run_len.
  63. */
  64. struct ipfrag_skb_cb {
  65. struct inet_skb_parm h;
  66. struct sk_buff *next_frag;
  67. int frag_run_len;
  68. };
  69. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  70. static void ip4_frag_init_run(struct sk_buff *skb)
  71. {
  72. BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb));
  73. FRAG_CB(skb)->next_frag = NULL;
  74. FRAG_CB(skb)->frag_run_len = skb->len;
  75. }
  76. /* Append skb to the last "run". */
  77. static void ip4_frag_append_to_last_run(struct inet_frag_queue *q,
  78. struct sk_buff *skb)
  79. {
  80. RB_CLEAR_NODE(&skb->rbnode);
  81. FRAG_CB(skb)->next_frag = NULL;
  82. FRAG_CB(q->last_run_head)->frag_run_len += skb->len;
  83. FRAG_CB(q->fragments_tail)->next_frag = skb;
  84. q->fragments_tail = skb;
  85. }
  86. /* Create a new "run" with the skb. */
  87. static void ip4_frag_create_run(struct inet_frag_queue *q, struct sk_buff *skb)
  88. {
  89. if (q->last_run_head)
  90. rb_link_node(&skb->rbnode, &q->last_run_head->rbnode,
  91. &q->last_run_head->rbnode.rb_right);
  92. else
  93. rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node);
  94. rb_insert_color(&skb->rbnode, &q->rb_fragments);
  95. ip4_frag_init_run(skb);
  96. q->fragments_tail = skb;
  97. q->last_run_head = skb;
  98. }
  99. /* Describe an entry in the "incomplete datagrams" queue. */
  100. struct ipq {
  101. struct inet_frag_queue q;
  102. u8 ecn; /* RFC3168 support */
  103. u16 max_df_size; /* largest frag with DF set seen */
  104. int iif;
  105. unsigned int rid;
  106. struct inet_peer *peer;
  107. };
  108. static u8 ip4_frag_ecn(u8 tos)
  109. {
  110. return 1 << (tos & INET_ECN_MASK);
  111. }
  112. static struct inet_frags ip4_frags;
  113. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
  114. struct sk_buff *prev_tail, struct net_device *dev);
  115. static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
  116. {
  117. struct ipq *qp = container_of(q, struct ipq, q);
  118. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  119. frags);
  120. struct net *net = container_of(ipv4, struct net, ipv4);
  121. const struct frag_v4_compare_key *key = a;
  122. q->key.v4 = *key;
  123. qp->ecn = 0;
  124. qp->peer = q->net->max_dist ?
  125. inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
  126. NULL;
  127. }
  128. static void ip4_frag_free(struct inet_frag_queue *q)
  129. {
  130. struct ipq *qp;
  131. qp = container_of(q, struct ipq, q);
  132. if (qp->peer)
  133. inet_putpeer(qp->peer);
  134. }
  135. /* Destruction primitives. */
  136. static void ipq_put(struct ipq *ipq)
  137. {
  138. inet_frag_put(&ipq->q);
  139. }
  140. /* Kill ipq entry. It is not destroyed immediately,
  141. * because caller (and someone more) holds reference count.
  142. */
  143. static void ipq_kill(struct ipq *ipq)
  144. {
  145. inet_frag_kill(&ipq->q);
  146. }
  147. static bool frag_expire_skip_icmp(u32 user)
  148. {
  149. return user == IP_DEFRAG_AF_PACKET ||
  150. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
  151. __IP_DEFRAG_CONNTRACK_IN_END) ||
  152. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  153. __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
  154. }
  155. /*
  156. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  157. */
  158. static void ip_expire(struct timer_list *t)
  159. {
  160. struct inet_frag_queue *frag = from_timer(frag, t, timer);
  161. const struct iphdr *iph;
  162. struct sk_buff *head = NULL;
  163. struct net *net;
  164. struct ipq *qp;
  165. int err;
  166. qp = container_of(frag, struct ipq, q);
  167. net = container_of(qp->q.net, struct net, ipv4.frags);
  168. rcu_read_lock();
  169. spin_lock(&qp->q.lock);
  170. if (qp->q.flags & INET_FRAG_COMPLETE)
  171. goto out;
  172. ipq_kill(qp);
  173. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  174. __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
  175. if (!(qp->q.flags & INET_FRAG_FIRST_IN))
  176. goto out;
  177. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  178. * pull the head out of the tree in order to be able to
  179. * deal with head->dev.
  180. */
  181. if (qp->q.fragments) {
  182. head = qp->q.fragments;
  183. qp->q.fragments = head->next;
  184. } else {
  185. head = skb_rb_first(&qp->q.rb_fragments);
  186. if (!head)
  187. goto out;
  188. if (FRAG_CB(head)->next_frag)
  189. rb_replace_node(&head->rbnode,
  190. &FRAG_CB(head)->next_frag->rbnode,
  191. &qp->q.rb_fragments);
  192. else
  193. rb_erase(&head->rbnode, &qp->q.rb_fragments);
  194. memset(&head->rbnode, 0, sizeof(head->rbnode));
  195. barrier();
  196. }
  197. if (head == qp->q.fragments_tail)
  198. qp->q.fragments_tail = NULL;
  199. sub_frag_mem_limit(qp->q.net, head->truesize);
  200. head->dev = dev_get_by_index_rcu(net, qp->iif);
  201. if (!head->dev)
  202. goto out;
  203. /* skb has no dst, perform route lookup again */
  204. iph = ip_hdr(head);
  205. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  206. iph->tos, head->dev);
  207. if (err)
  208. goto out;
  209. /* Only an end host needs to send an ICMP
  210. * "Fragment Reassembly Timeout" message, per RFC792.
  211. */
  212. if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
  213. (skb_rtable(head)->rt_type != RTN_LOCAL))
  214. goto out;
  215. spin_unlock(&qp->q.lock);
  216. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  217. goto out_rcu_unlock;
  218. out:
  219. spin_unlock(&qp->q.lock);
  220. out_rcu_unlock:
  221. rcu_read_unlock();
  222. kfree_skb(head);
  223. ipq_put(qp);
  224. }
  225. /* Find the correct entry in the "incomplete datagrams" queue for
  226. * this IP datagram, and create new one, if nothing is found.
  227. */
  228. static struct ipq *ip_find(struct net *net, struct iphdr *iph,
  229. u32 user, int vif)
  230. {
  231. struct frag_v4_compare_key key = {
  232. .saddr = iph->saddr,
  233. .daddr = iph->daddr,
  234. .user = user,
  235. .vif = vif,
  236. .id = iph->id,
  237. .protocol = iph->protocol,
  238. };
  239. struct inet_frag_queue *q;
  240. q = inet_frag_find(&net->ipv4.frags, &key);
  241. if (!q)
  242. return NULL;
  243. return container_of(q, struct ipq, q);
  244. }
  245. /* Is the fragment too far ahead to be part of ipq? */
  246. static int ip_frag_too_far(struct ipq *qp)
  247. {
  248. struct inet_peer *peer = qp->peer;
  249. unsigned int max = qp->q.net->max_dist;
  250. unsigned int start, end;
  251. int rc;
  252. if (!peer || !max)
  253. return 0;
  254. start = qp->rid;
  255. end = atomic_inc_return(&peer->rid);
  256. qp->rid = end;
  257. rc = qp->q.fragments_tail && (end - start) > max;
  258. if (rc) {
  259. struct net *net;
  260. net = container_of(qp->q.net, struct net, ipv4.frags);
  261. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  262. }
  263. return rc;
  264. }
  265. static int ip_frag_reinit(struct ipq *qp)
  266. {
  267. unsigned int sum_truesize = 0;
  268. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  269. refcount_inc(&qp->q.refcnt);
  270. return -ETIMEDOUT;
  271. }
  272. sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments);
  273. sub_frag_mem_limit(qp->q.net, sum_truesize);
  274. qp->q.flags = 0;
  275. qp->q.len = 0;
  276. qp->q.meat = 0;
  277. qp->q.fragments = NULL;
  278. qp->q.rb_fragments = RB_ROOT;
  279. qp->q.fragments_tail = NULL;
  280. qp->q.last_run_head = NULL;
  281. qp->iif = 0;
  282. qp->ecn = 0;
  283. return 0;
  284. }
  285. /* Add new segment to existing queue. */
  286. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  287. {
  288. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  289. struct rb_node **rbn, *parent;
  290. struct sk_buff *skb1, *prev_tail;
  291. struct net_device *dev;
  292. unsigned int fragsize;
  293. int flags, offset;
  294. int ihl, end;
  295. int err = -ENOENT;
  296. u8 ecn;
  297. if (qp->q.flags & INET_FRAG_COMPLETE)
  298. goto err;
  299. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  300. unlikely(ip_frag_too_far(qp)) &&
  301. unlikely(err = ip_frag_reinit(qp))) {
  302. ipq_kill(qp);
  303. goto err;
  304. }
  305. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  306. offset = ntohs(ip_hdr(skb)->frag_off);
  307. flags = offset & ~IP_OFFSET;
  308. offset &= IP_OFFSET;
  309. offset <<= 3; /* offset is in 8-byte chunks */
  310. ihl = ip_hdrlen(skb);
  311. /* Determine the position of this fragment. */
  312. end = offset + skb->len - skb_network_offset(skb) - ihl;
  313. err = -EINVAL;
  314. /* Is this the final fragment? */
  315. if ((flags & IP_MF) == 0) {
  316. /* If we already have some bits beyond end
  317. * or have different end, the segment is corrupted.
  318. */
  319. if (end < qp->q.len ||
  320. ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
  321. goto discard_qp;
  322. qp->q.flags |= INET_FRAG_LAST_IN;
  323. qp->q.len = end;
  324. } else {
  325. if (end&7) {
  326. end &= ~7;
  327. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  328. skb->ip_summed = CHECKSUM_NONE;
  329. }
  330. if (end > qp->q.len) {
  331. /* Some bits beyond end -> corruption. */
  332. if (qp->q.flags & INET_FRAG_LAST_IN)
  333. goto discard_qp;
  334. qp->q.len = end;
  335. }
  336. }
  337. if (end == offset)
  338. goto discard_qp;
  339. err = -ENOMEM;
  340. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  341. goto discard_qp;
  342. err = pskb_trim_rcsum(skb, end - offset);
  343. if (err)
  344. goto discard_qp;
  345. /* Note : skb->rbnode and skb->dev share the same location. */
  346. dev = skb->dev;
  347. /* Makes sure compiler wont do silly aliasing games */
  348. barrier();
  349. /* RFC5722, Section 4, amended by Errata ID : 3089
  350. * When reassembling an IPv6 datagram, if
  351. * one or more its constituent fragments is determined to be an
  352. * overlapping fragment, the entire datagram (and any constituent
  353. * fragments) MUST be silently discarded.
  354. *
  355. * We do the same here for IPv4 (and increment an snmp counter).
  356. */
  357. err = -EINVAL;
  358. /* Find out where to put this fragment. */
  359. prev_tail = qp->q.fragments_tail;
  360. if (!prev_tail)
  361. ip4_frag_create_run(&qp->q, skb); /* First fragment. */
  362. else if (prev_tail->ip_defrag_offset + prev_tail->len < end) {
  363. /* This is the common case: skb goes to the end. */
  364. /* Detect and discard overlaps. */
  365. if (offset < prev_tail->ip_defrag_offset + prev_tail->len)
  366. goto overlap;
  367. if (offset == prev_tail->ip_defrag_offset + prev_tail->len)
  368. ip4_frag_append_to_last_run(&qp->q, skb);
  369. else
  370. ip4_frag_create_run(&qp->q, skb);
  371. } else {
  372. /* Binary search. Note that skb can become the first fragment,
  373. * but not the last (covered above).
  374. */
  375. rbn = &qp->q.rb_fragments.rb_node;
  376. do {
  377. parent = *rbn;
  378. skb1 = rb_to_skb(parent);
  379. if (end <= skb1->ip_defrag_offset)
  380. rbn = &parent->rb_left;
  381. else if (offset >= skb1->ip_defrag_offset +
  382. FRAG_CB(skb1)->frag_run_len)
  383. rbn = &parent->rb_right;
  384. else /* Found an overlap with skb1. */
  385. goto overlap;
  386. } while (*rbn);
  387. /* Here we have parent properly set, and rbn pointing to
  388. * one of its NULL left/right children. Insert skb.
  389. */
  390. ip4_frag_init_run(skb);
  391. rb_link_node(&skb->rbnode, parent, rbn);
  392. rb_insert_color(&skb->rbnode, &qp->q.rb_fragments);
  393. }
  394. if (dev)
  395. qp->iif = dev->ifindex;
  396. skb->ip_defrag_offset = offset;
  397. qp->q.stamp = skb->tstamp;
  398. qp->q.meat += skb->len;
  399. qp->ecn |= ecn;
  400. add_frag_mem_limit(qp->q.net, skb->truesize);
  401. if (offset == 0)
  402. qp->q.flags |= INET_FRAG_FIRST_IN;
  403. fragsize = skb->len + ihl;
  404. if (fragsize > qp->q.max_size)
  405. qp->q.max_size = fragsize;
  406. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  407. fragsize > qp->max_df_size)
  408. qp->max_df_size = fragsize;
  409. if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  410. qp->q.meat == qp->q.len) {
  411. unsigned long orefdst = skb->_skb_refdst;
  412. skb->_skb_refdst = 0UL;
  413. err = ip_frag_reasm(qp, skb, prev_tail, dev);
  414. skb->_skb_refdst = orefdst;
  415. if (err)
  416. inet_frag_kill(&qp->q);
  417. return err;
  418. }
  419. skb_dst_drop(skb);
  420. return -EINPROGRESS;
  421. overlap:
  422. __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
  423. discard_qp:
  424. inet_frag_kill(&qp->q);
  425. err:
  426. kfree_skb(skb);
  427. return err;
  428. }
  429. /* Build a new IP datagram from all its fragments. */
  430. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
  431. struct sk_buff *prev_tail, struct net_device *dev)
  432. {
  433. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  434. struct iphdr *iph;
  435. struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments);
  436. struct sk_buff **nextp; /* To build frag_list. */
  437. struct rb_node *rbn;
  438. int len;
  439. int ihlen;
  440. int delta;
  441. int err;
  442. u8 ecn;
  443. ipq_kill(qp);
  444. ecn = ip_frag_ecn_table[qp->ecn];
  445. if (unlikely(ecn == 0xff)) {
  446. err = -EINVAL;
  447. goto out_fail;
  448. }
  449. /* Make the one we just received the head. */
  450. if (head != skb) {
  451. fp = skb_clone(skb, GFP_ATOMIC);
  452. if (!fp)
  453. goto out_nomem;
  454. FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
  455. if (RB_EMPTY_NODE(&skb->rbnode))
  456. FRAG_CB(prev_tail)->next_frag = fp;
  457. else
  458. rb_replace_node(&skb->rbnode, &fp->rbnode,
  459. &qp->q.rb_fragments);
  460. if (qp->q.fragments_tail == skb)
  461. qp->q.fragments_tail = fp;
  462. skb_morph(skb, head);
  463. FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
  464. rb_replace_node(&head->rbnode, &skb->rbnode,
  465. &qp->q.rb_fragments);
  466. consume_skb(head);
  467. head = skb;
  468. }
  469. WARN_ON(head->ip_defrag_offset != 0);
  470. /* Allocate a new buffer for the datagram. */
  471. ihlen = ip_hdrlen(head);
  472. len = ihlen + qp->q.len;
  473. err = -E2BIG;
  474. if (len > 65535)
  475. goto out_oversize;
  476. delta = - head->truesize;
  477. /* Head of list must not be cloned. */
  478. if (skb_unclone(head, GFP_ATOMIC))
  479. goto out_nomem;
  480. delta += head->truesize;
  481. if (delta)
  482. add_frag_mem_limit(qp->q.net, delta);
  483. /* If the first fragment is fragmented itself, we split
  484. * it to two chunks: the first with data and paged part
  485. * and the second, holding only fragments. */
  486. if (skb_has_frag_list(head)) {
  487. struct sk_buff *clone;
  488. int i, plen = 0;
  489. clone = alloc_skb(0, GFP_ATOMIC);
  490. if (!clone)
  491. goto out_nomem;
  492. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  493. skb_frag_list_init(head);
  494. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  495. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  496. clone->len = clone->data_len = head->data_len - plen;
  497. head->truesize += clone->truesize;
  498. clone->csum = 0;
  499. clone->ip_summed = head->ip_summed;
  500. add_frag_mem_limit(qp->q.net, clone->truesize);
  501. skb_shinfo(head)->frag_list = clone;
  502. nextp = &clone->next;
  503. } else {
  504. nextp = &skb_shinfo(head)->frag_list;
  505. }
  506. skb_push(head, head->data - skb_network_header(head));
  507. /* Traverse the tree in order, to build frag_list. */
  508. fp = FRAG_CB(head)->next_frag;
  509. rbn = rb_next(&head->rbnode);
  510. rb_erase(&head->rbnode, &qp->q.rb_fragments);
  511. while (rbn || fp) {
  512. /* fp points to the next sk_buff in the current run;
  513. * rbn points to the next run.
  514. */
  515. /* Go through the current run. */
  516. while (fp) {
  517. *nextp = fp;
  518. nextp = &fp->next;
  519. fp->prev = NULL;
  520. memset(&fp->rbnode, 0, sizeof(fp->rbnode));
  521. fp->sk = NULL;
  522. head->data_len += fp->len;
  523. head->len += fp->len;
  524. if (head->ip_summed != fp->ip_summed)
  525. head->ip_summed = CHECKSUM_NONE;
  526. else if (head->ip_summed == CHECKSUM_COMPLETE)
  527. head->csum = csum_add(head->csum, fp->csum);
  528. head->truesize += fp->truesize;
  529. fp = FRAG_CB(fp)->next_frag;
  530. }
  531. /* Move to the next run. */
  532. if (rbn) {
  533. struct rb_node *rbnext = rb_next(rbn);
  534. fp = rb_to_skb(rbn);
  535. rb_erase(rbn, &qp->q.rb_fragments);
  536. rbn = rbnext;
  537. }
  538. }
  539. sub_frag_mem_limit(qp->q.net, head->truesize);
  540. *nextp = NULL;
  541. skb_mark_not_on_list(head);
  542. head->prev = NULL;
  543. head->dev = dev;
  544. head->tstamp = qp->q.stamp;
  545. IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  546. iph = ip_hdr(head);
  547. iph->tot_len = htons(len);
  548. iph->tos |= ecn;
  549. /* When we set IP_DF on a refragmented skb we must also force a
  550. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  551. * original sender only sent fragments of size f (where f < s).
  552. *
  553. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  554. * frag seen to avoid sending tiny DF-fragments in case skb was built
  555. * from one very small df-fragment and one large non-df frag.
  556. */
  557. if (qp->max_df_size == qp->q.max_size) {
  558. IPCB(head)->flags |= IPSKB_FRAG_PMTU;
  559. iph->frag_off = htons(IP_DF);
  560. } else {
  561. iph->frag_off = 0;
  562. }
  563. ip_send_check(iph);
  564. __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
  565. qp->q.fragments = NULL;
  566. qp->q.rb_fragments = RB_ROOT;
  567. qp->q.fragments_tail = NULL;
  568. qp->q.last_run_head = NULL;
  569. return 0;
  570. out_nomem:
  571. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  572. err = -ENOMEM;
  573. goto out_fail;
  574. out_oversize:
  575. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
  576. out_fail:
  577. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  578. return err;
  579. }
  580. /* Process an incoming IP datagram fragment. */
  581. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  582. {
  583. struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
  584. int vif = l3mdev_master_ifindex_rcu(dev);
  585. struct ipq *qp;
  586. __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
  587. skb_orphan(skb);
  588. /* Lookup (or create) queue header */
  589. qp = ip_find(net, ip_hdr(skb), user, vif);
  590. if (qp) {
  591. int ret;
  592. spin_lock(&qp->q.lock);
  593. ret = ip_frag_queue(qp, skb);
  594. spin_unlock(&qp->q.lock);
  595. ipq_put(qp);
  596. return ret;
  597. }
  598. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  599. kfree_skb(skb);
  600. return -ENOMEM;
  601. }
  602. EXPORT_SYMBOL(ip_defrag);
  603. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  604. {
  605. struct iphdr iph;
  606. int netoff;
  607. u32 len;
  608. if (skb->protocol != htons(ETH_P_IP))
  609. return skb;
  610. netoff = skb_network_offset(skb);
  611. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  612. return skb;
  613. if (iph.ihl < 5 || iph.version != 4)
  614. return skb;
  615. len = ntohs(iph.tot_len);
  616. if (skb->len < netoff + len || len < (iph.ihl * 4))
  617. return skb;
  618. if (ip_is_fragment(&iph)) {
  619. skb = skb_share_check(skb, GFP_ATOMIC);
  620. if (skb) {
  621. if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
  622. kfree_skb(skb);
  623. return NULL;
  624. }
  625. if (pskb_trim_rcsum(skb, netoff + len)) {
  626. kfree_skb(skb);
  627. return NULL;
  628. }
  629. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  630. if (ip_defrag(net, skb, user))
  631. return NULL;
  632. skb_clear_hash(skb);
  633. }
  634. }
  635. return skb;
  636. }
  637. EXPORT_SYMBOL(ip_check_defrag);
  638. unsigned int inet_frag_rbtree_purge(struct rb_root *root)
  639. {
  640. struct rb_node *p = rb_first(root);
  641. unsigned int sum = 0;
  642. while (p) {
  643. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  644. p = rb_next(p);
  645. rb_erase(&skb->rbnode, root);
  646. while (skb) {
  647. struct sk_buff *next = FRAG_CB(skb)->next_frag;
  648. sum += skb->truesize;
  649. kfree_skb(skb);
  650. skb = next;
  651. }
  652. }
  653. return sum;
  654. }
  655. EXPORT_SYMBOL(inet_frag_rbtree_purge);
  656. #ifdef CONFIG_SYSCTL
  657. static int dist_min;
  658. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  659. {
  660. .procname = "ipfrag_high_thresh",
  661. .data = &init_net.ipv4.frags.high_thresh,
  662. .maxlen = sizeof(unsigned long),
  663. .mode = 0644,
  664. .proc_handler = proc_doulongvec_minmax,
  665. .extra1 = &init_net.ipv4.frags.low_thresh
  666. },
  667. {
  668. .procname = "ipfrag_low_thresh",
  669. .data = &init_net.ipv4.frags.low_thresh,
  670. .maxlen = sizeof(unsigned long),
  671. .mode = 0644,
  672. .proc_handler = proc_doulongvec_minmax,
  673. .extra2 = &init_net.ipv4.frags.high_thresh
  674. },
  675. {
  676. .procname = "ipfrag_time",
  677. .data = &init_net.ipv4.frags.timeout,
  678. .maxlen = sizeof(int),
  679. .mode = 0644,
  680. .proc_handler = proc_dointvec_jiffies,
  681. },
  682. {
  683. .procname = "ipfrag_max_dist",
  684. .data = &init_net.ipv4.frags.max_dist,
  685. .maxlen = sizeof(int),
  686. .mode = 0644,
  687. .proc_handler = proc_dointvec_minmax,
  688. .extra1 = &dist_min,
  689. },
  690. { }
  691. };
  692. /* secret interval has been deprecated */
  693. static int ip4_frags_secret_interval_unused;
  694. static struct ctl_table ip4_frags_ctl_table[] = {
  695. {
  696. .procname = "ipfrag_secret_interval",
  697. .data = &ip4_frags_secret_interval_unused,
  698. .maxlen = sizeof(int),
  699. .mode = 0644,
  700. .proc_handler = proc_dointvec_jiffies,
  701. },
  702. { }
  703. };
  704. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  705. {
  706. struct ctl_table *table;
  707. struct ctl_table_header *hdr;
  708. table = ip4_frags_ns_ctl_table;
  709. if (!net_eq(net, &init_net)) {
  710. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  711. if (!table)
  712. goto err_alloc;
  713. table[0].data = &net->ipv4.frags.high_thresh;
  714. table[0].extra1 = &net->ipv4.frags.low_thresh;
  715. table[1].data = &net->ipv4.frags.low_thresh;
  716. table[1].extra2 = &net->ipv4.frags.high_thresh;
  717. table[2].data = &net->ipv4.frags.timeout;
  718. table[3].data = &net->ipv4.frags.max_dist;
  719. }
  720. hdr = register_net_sysctl(net, "net/ipv4", table);
  721. if (!hdr)
  722. goto err_reg;
  723. net->ipv4.frags_hdr = hdr;
  724. return 0;
  725. err_reg:
  726. if (!net_eq(net, &init_net))
  727. kfree(table);
  728. err_alloc:
  729. return -ENOMEM;
  730. }
  731. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  732. {
  733. struct ctl_table *table;
  734. table = net->ipv4.frags_hdr->ctl_table_arg;
  735. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  736. kfree(table);
  737. }
  738. static void __init ip4_frags_ctl_register(void)
  739. {
  740. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  741. }
  742. #else
  743. static int ip4_frags_ns_ctl_register(struct net *net)
  744. {
  745. return 0;
  746. }
  747. static void ip4_frags_ns_ctl_unregister(struct net *net)
  748. {
  749. }
  750. static void __init ip4_frags_ctl_register(void)
  751. {
  752. }
  753. #endif
  754. static int __net_init ipv4_frags_init_net(struct net *net)
  755. {
  756. int res;
  757. /* Fragment cache limits.
  758. *
  759. * The fragment memory accounting code, (tries to) account for
  760. * the real memory usage, by measuring both the size of frag
  761. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  762. * and the SKB's truesize.
  763. *
  764. * A 64K fragment consumes 129736 bytes (44*2944)+200
  765. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  766. *
  767. * We will commit 4MB at one time. Should we cross that limit
  768. * we will prune down to 3MB, making room for approx 8 big 64K
  769. * fragments 8x128k.
  770. */
  771. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  772. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  773. /*
  774. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  775. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  776. * by TTL.
  777. */
  778. net->ipv4.frags.timeout = IP_FRAG_TIME;
  779. net->ipv4.frags.max_dist = 64;
  780. net->ipv4.frags.f = &ip4_frags;
  781. res = inet_frags_init_net(&net->ipv4.frags);
  782. if (res < 0)
  783. return res;
  784. res = ip4_frags_ns_ctl_register(net);
  785. if (res < 0)
  786. inet_frags_exit_net(&net->ipv4.frags);
  787. return res;
  788. }
  789. static void __net_exit ipv4_frags_exit_net(struct net *net)
  790. {
  791. ip4_frags_ns_ctl_unregister(net);
  792. inet_frags_exit_net(&net->ipv4.frags);
  793. }
  794. static struct pernet_operations ip4_frags_ops = {
  795. .init = ipv4_frags_init_net,
  796. .exit = ipv4_frags_exit_net,
  797. };
  798. static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
  799. {
  800. return jhash2(data,
  801. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  802. }
  803. static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
  804. {
  805. const struct inet_frag_queue *fq = data;
  806. return jhash2((const u32 *)&fq->key.v4,
  807. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  808. }
  809. static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  810. {
  811. const struct frag_v4_compare_key *key = arg->key;
  812. const struct inet_frag_queue *fq = ptr;
  813. return !!memcmp(&fq->key, key, sizeof(*key));
  814. }
  815. static const struct rhashtable_params ip4_rhash_params = {
  816. .head_offset = offsetof(struct inet_frag_queue, node),
  817. .key_offset = offsetof(struct inet_frag_queue, key),
  818. .key_len = sizeof(struct frag_v4_compare_key),
  819. .hashfn = ip4_key_hashfn,
  820. .obj_hashfn = ip4_obj_hashfn,
  821. .obj_cmpfn = ip4_obj_cmpfn,
  822. .automatic_shrinking = true,
  823. };
  824. void __init ipfrag_init(void)
  825. {
  826. ip4_frags.constructor = ip4_frag_init;
  827. ip4_frags.destructor = ip4_frag_free;
  828. ip4_frags.qsize = sizeof(struct ipq);
  829. ip4_frags.frag_expire = ip_expire;
  830. ip4_frags.frags_cache_name = ip_frag_cache_name;
  831. ip4_frags.rhash_params = ip4_rhash_params;
  832. if (inet_frags_init(&ip4_frags))
  833. panic("IP: failed to allocate ip4_frags cache\n");
  834. ip4_frags_ctl_register();
  835. register_pernet_subsys(&ip4_frags_ops);
  836. }