ip_fragment.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. if (head)
  223. kfree_skb(head);
  224. ipq_put(qp);
  225. }
  226. /* Find the correct entry in the "incomplete datagrams" queue for
  227. * this IP datagram, and create new one, if nothing is found.
  228. */
  229. static struct ipq *ip_find(struct net *net, struct iphdr *iph,
  230. u32 user, int vif)
  231. {
  232. struct frag_v4_compare_key key = {
  233. .saddr = iph->saddr,
  234. .daddr = iph->daddr,
  235. .user = user,
  236. .vif = vif,
  237. .id = iph->id,
  238. .protocol = iph->protocol,
  239. };
  240. struct inet_frag_queue *q;
  241. q = inet_frag_find(&net->ipv4.frags, &key);
  242. if (!q)
  243. return NULL;
  244. return container_of(q, struct ipq, q);
  245. }
  246. /* Is the fragment too far ahead to be part of ipq? */
  247. static int ip_frag_too_far(struct ipq *qp)
  248. {
  249. struct inet_peer *peer = qp->peer;
  250. unsigned int max = qp->q.net->max_dist;
  251. unsigned int start, end;
  252. int rc;
  253. if (!peer || !max)
  254. return 0;
  255. start = qp->rid;
  256. end = atomic_inc_return(&peer->rid);
  257. qp->rid = end;
  258. rc = qp->q.fragments_tail && (end - start) > max;
  259. if (rc) {
  260. struct net *net;
  261. net = container_of(qp->q.net, struct net, ipv4.frags);
  262. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  263. }
  264. return rc;
  265. }
  266. static int ip_frag_reinit(struct ipq *qp)
  267. {
  268. unsigned int sum_truesize = 0;
  269. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  270. refcount_inc(&qp->q.refcnt);
  271. return -ETIMEDOUT;
  272. }
  273. sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments);
  274. sub_frag_mem_limit(qp->q.net, sum_truesize);
  275. qp->q.flags = 0;
  276. qp->q.len = 0;
  277. qp->q.meat = 0;
  278. qp->q.fragments = NULL;
  279. qp->q.rb_fragments = RB_ROOT;
  280. qp->q.fragments_tail = NULL;
  281. qp->q.last_run_head = NULL;
  282. qp->iif = 0;
  283. qp->ecn = 0;
  284. return 0;
  285. }
  286. /* Add new segment to existing queue. */
  287. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  288. {
  289. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  290. struct rb_node **rbn, *parent;
  291. struct sk_buff *skb1, *prev_tail;
  292. struct net_device *dev;
  293. unsigned int fragsize;
  294. int flags, offset;
  295. int ihl, end;
  296. int err = -ENOENT;
  297. u8 ecn;
  298. if (qp->q.flags & INET_FRAG_COMPLETE)
  299. goto err;
  300. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  301. unlikely(ip_frag_too_far(qp)) &&
  302. unlikely(err = ip_frag_reinit(qp))) {
  303. ipq_kill(qp);
  304. goto err;
  305. }
  306. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  307. offset = ntohs(ip_hdr(skb)->frag_off);
  308. flags = offset & ~IP_OFFSET;
  309. offset &= IP_OFFSET;
  310. offset <<= 3; /* offset is in 8-byte chunks */
  311. ihl = ip_hdrlen(skb);
  312. /* Determine the position of this fragment. */
  313. end = offset + skb->len - skb_network_offset(skb) - ihl;
  314. err = -EINVAL;
  315. /* Is this the final fragment? */
  316. if ((flags & IP_MF) == 0) {
  317. /* If we already have some bits beyond end
  318. * or have different end, the segment is corrupted.
  319. */
  320. if (end < qp->q.len ||
  321. ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
  322. goto err;
  323. qp->q.flags |= INET_FRAG_LAST_IN;
  324. qp->q.len = end;
  325. } else {
  326. if (end&7) {
  327. end &= ~7;
  328. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  329. skb->ip_summed = CHECKSUM_NONE;
  330. }
  331. if (end > qp->q.len) {
  332. /* Some bits beyond end -> corruption. */
  333. if (qp->q.flags & INET_FRAG_LAST_IN)
  334. goto err;
  335. qp->q.len = end;
  336. }
  337. }
  338. if (end == offset)
  339. goto err;
  340. err = -ENOMEM;
  341. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  342. goto err;
  343. err = pskb_trim_rcsum(skb, end - offset);
  344. if (err)
  345. goto err;
  346. /* Note : skb->rbnode and skb->dev share the same location. */
  347. dev = skb->dev;
  348. /* Makes sure compiler wont do silly aliasing games */
  349. barrier();
  350. /* RFC5722, Section 4, amended by Errata ID : 3089
  351. * When reassembling an IPv6 datagram, if
  352. * one or more its constituent fragments is determined to be an
  353. * overlapping fragment, the entire datagram (and any constituent
  354. * fragments) MUST be silently discarded.
  355. *
  356. * We do the same here for IPv4 (and increment an snmp counter).
  357. */
  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 discard_qp;
  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 discard_qp;
  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. return err;
  416. }
  417. skb_dst_drop(skb);
  418. return -EINPROGRESS;
  419. discard_qp:
  420. inet_frag_kill(&qp->q);
  421. err = -EINVAL;
  422. __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
  423. err:
  424. kfree_skb(skb);
  425. return err;
  426. }
  427. /* Build a new IP datagram from all its fragments. */
  428. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
  429. struct sk_buff *prev_tail, struct net_device *dev)
  430. {
  431. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  432. struct iphdr *iph;
  433. struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments);
  434. struct sk_buff **nextp; /* To build frag_list. */
  435. struct rb_node *rbn;
  436. int len;
  437. int ihlen;
  438. int err;
  439. u8 ecn;
  440. ipq_kill(qp);
  441. ecn = ip_frag_ecn_table[qp->ecn];
  442. if (unlikely(ecn == 0xff)) {
  443. err = -EINVAL;
  444. goto out_fail;
  445. }
  446. /* Make the one we just received the head. */
  447. if (head != skb) {
  448. fp = skb_clone(skb, GFP_ATOMIC);
  449. if (!fp)
  450. goto out_nomem;
  451. FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
  452. if (RB_EMPTY_NODE(&skb->rbnode))
  453. FRAG_CB(prev_tail)->next_frag = fp;
  454. else
  455. rb_replace_node(&skb->rbnode, &fp->rbnode,
  456. &qp->q.rb_fragments);
  457. if (qp->q.fragments_tail == skb)
  458. qp->q.fragments_tail = fp;
  459. skb_morph(skb, head);
  460. FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
  461. rb_replace_node(&head->rbnode, &skb->rbnode,
  462. &qp->q.rb_fragments);
  463. consume_skb(head);
  464. head = skb;
  465. }
  466. WARN_ON(head->ip_defrag_offset != 0);
  467. /* Allocate a new buffer for the datagram. */
  468. ihlen = ip_hdrlen(head);
  469. len = ihlen + qp->q.len;
  470. err = -E2BIG;
  471. if (len > 65535)
  472. goto out_oversize;
  473. /* Head of list must not be cloned. */
  474. if (skb_unclone(head, GFP_ATOMIC))
  475. goto out_nomem;
  476. /* If the first fragment is fragmented itself, we split
  477. * it to two chunks: the first with data and paged part
  478. * and the second, holding only fragments. */
  479. if (skb_has_frag_list(head)) {
  480. struct sk_buff *clone;
  481. int i, plen = 0;
  482. clone = alloc_skb(0, GFP_ATOMIC);
  483. if (!clone)
  484. goto out_nomem;
  485. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  486. skb_frag_list_init(head);
  487. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  488. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  489. clone->len = clone->data_len = head->data_len - plen;
  490. head->truesize += clone->truesize;
  491. clone->csum = 0;
  492. clone->ip_summed = head->ip_summed;
  493. add_frag_mem_limit(qp->q.net, clone->truesize);
  494. skb_shinfo(head)->frag_list = clone;
  495. nextp = &clone->next;
  496. } else {
  497. nextp = &skb_shinfo(head)->frag_list;
  498. }
  499. skb_push(head, head->data - skb_network_header(head));
  500. /* Traverse the tree in order, to build frag_list. */
  501. fp = FRAG_CB(head)->next_frag;
  502. rbn = rb_next(&head->rbnode);
  503. rb_erase(&head->rbnode, &qp->q.rb_fragments);
  504. while (rbn || fp) {
  505. /* fp points to the next sk_buff in the current run;
  506. * rbn points to the next run.
  507. */
  508. /* Go through the current run. */
  509. while (fp) {
  510. *nextp = fp;
  511. nextp = &fp->next;
  512. fp->prev = NULL;
  513. memset(&fp->rbnode, 0, sizeof(fp->rbnode));
  514. head->data_len += fp->len;
  515. head->len += fp->len;
  516. if (head->ip_summed != fp->ip_summed)
  517. head->ip_summed = CHECKSUM_NONE;
  518. else if (head->ip_summed == CHECKSUM_COMPLETE)
  519. head->csum = csum_add(head->csum, fp->csum);
  520. head->truesize += fp->truesize;
  521. fp = FRAG_CB(fp)->next_frag;
  522. }
  523. /* Move to the next run. */
  524. if (rbn) {
  525. struct rb_node *rbnext = rb_next(rbn);
  526. fp = rb_to_skb(rbn);
  527. rb_erase(rbn, &qp->q.rb_fragments);
  528. rbn = rbnext;
  529. }
  530. }
  531. sub_frag_mem_limit(qp->q.net, head->truesize);
  532. *nextp = NULL;
  533. head->next = NULL;
  534. head->prev = NULL;
  535. head->dev = dev;
  536. head->tstamp = qp->q.stamp;
  537. IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  538. iph = ip_hdr(head);
  539. iph->tot_len = htons(len);
  540. iph->tos |= ecn;
  541. /* When we set IP_DF on a refragmented skb we must also force a
  542. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  543. * original sender only sent fragments of size f (where f < s).
  544. *
  545. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  546. * frag seen to avoid sending tiny DF-fragments in case skb was built
  547. * from one very small df-fragment and one large non-df frag.
  548. */
  549. if (qp->max_df_size == qp->q.max_size) {
  550. IPCB(head)->flags |= IPSKB_FRAG_PMTU;
  551. iph->frag_off = htons(IP_DF);
  552. } else {
  553. iph->frag_off = 0;
  554. }
  555. ip_send_check(iph);
  556. __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
  557. qp->q.fragments = NULL;
  558. qp->q.rb_fragments = RB_ROOT;
  559. qp->q.fragments_tail = NULL;
  560. qp->q.last_run_head = NULL;
  561. return 0;
  562. out_nomem:
  563. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  564. err = -ENOMEM;
  565. goto out_fail;
  566. out_oversize:
  567. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
  568. out_fail:
  569. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  570. return err;
  571. }
  572. /* Process an incoming IP datagram fragment. */
  573. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  574. {
  575. struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
  576. int vif = l3mdev_master_ifindex_rcu(dev);
  577. struct ipq *qp;
  578. __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
  579. skb_orphan(skb);
  580. /* Lookup (or create) queue header */
  581. qp = ip_find(net, ip_hdr(skb), user, vif);
  582. if (qp) {
  583. int ret;
  584. spin_lock(&qp->q.lock);
  585. ret = ip_frag_queue(qp, skb);
  586. spin_unlock(&qp->q.lock);
  587. ipq_put(qp);
  588. return ret;
  589. }
  590. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  591. kfree_skb(skb);
  592. return -ENOMEM;
  593. }
  594. EXPORT_SYMBOL(ip_defrag);
  595. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  596. {
  597. struct iphdr iph;
  598. int netoff;
  599. u32 len;
  600. if (skb->protocol != htons(ETH_P_IP))
  601. return skb;
  602. netoff = skb_network_offset(skb);
  603. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  604. return skb;
  605. if (iph.ihl < 5 || iph.version != 4)
  606. return skb;
  607. len = ntohs(iph.tot_len);
  608. if (skb->len < netoff + len || len < (iph.ihl * 4))
  609. return skb;
  610. if (ip_is_fragment(&iph)) {
  611. skb = skb_share_check(skb, GFP_ATOMIC);
  612. if (skb) {
  613. if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
  614. return skb;
  615. if (pskb_trim_rcsum(skb, netoff + len))
  616. return skb;
  617. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  618. if (ip_defrag(net, skb, user))
  619. return NULL;
  620. skb_clear_hash(skb);
  621. }
  622. }
  623. return skb;
  624. }
  625. EXPORT_SYMBOL(ip_check_defrag);
  626. unsigned int inet_frag_rbtree_purge(struct rb_root *root)
  627. {
  628. struct rb_node *p = rb_first(root);
  629. unsigned int sum = 0;
  630. while (p) {
  631. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  632. p = rb_next(p);
  633. rb_erase(&skb->rbnode, root);
  634. while (skb) {
  635. struct sk_buff *next = FRAG_CB(skb)->next_frag;
  636. sum += skb->truesize;
  637. kfree_skb(skb);
  638. skb = next;
  639. }
  640. }
  641. return sum;
  642. }
  643. EXPORT_SYMBOL(inet_frag_rbtree_purge);
  644. #ifdef CONFIG_SYSCTL
  645. static int dist_min;
  646. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  647. {
  648. .procname = "ipfrag_high_thresh",
  649. .data = &init_net.ipv4.frags.high_thresh,
  650. .maxlen = sizeof(unsigned long),
  651. .mode = 0644,
  652. .proc_handler = proc_doulongvec_minmax,
  653. .extra1 = &init_net.ipv4.frags.low_thresh
  654. },
  655. {
  656. .procname = "ipfrag_low_thresh",
  657. .data = &init_net.ipv4.frags.low_thresh,
  658. .maxlen = sizeof(unsigned long),
  659. .mode = 0644,
  660. .proc_handler = proc_doulongvec_minmax,
  661. .extra2 = &init_net.ipv4.frags.high_thresh
  662. },
  663. {
  664. .procname = "ipfrag_time",
  665. .data = &init_net.ipv4.frags.timeout,
  666. .maxlen = sizeof(int),
  667. .mode = 0644,
  668. .proc_handler = proc_dointvec_jiffies,
  669. },
  670. {
  671. .procname = "ipfrag_max_dist",
  672. .data = &init_net.ipv4.frags.max_dist,
  673. .maxlen = sizeof(int),
  674. .mode = 0644,
  675. .proc_handler = proc_dointvec_minmax,
  676. .extra1 = &dist_min,
  677. },
  678. { }
  679. };
  680. /* secret interval has been deprecated */
  681. static int ip4_frags_secret_interval_unused;
  682. static struct ctl_table ip4_frags_ctl_table[] = {
  683. {
  684. .procname = "ipfrag_secret_interval",
  685. .data = &ip4_frags_secret_interval_unused,
  686. .maxlen = sizeof(int),
  687. .mode = 0644,
  688. .proc_handler = proc_dointvec_jiffies,
  689. },
  690. { }
  691. };
  692. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  693. {
  694. struct ctl_table *table;
  695. struct ctl_table_header *hdr;
  696. table = ip4_frags_ns_ctl_table;
  697. if (!net_eq(net, &init_net)) {
  698. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  699. if (!table)
  700. goto err_alloc;
  701. table[0].data = &net->ipv4.frags.high_thresh;
  702. table[0].extra1 = &net->ipv4.frags.low_thresh;
  703. table[0].extra2 = &init_net.ipv4.frags.high_thresh;
  704. table[1].data = &net->ipv4.frags.low_thresh;
  705. table[1].extra2 = &net->ipv4.frags.high_thresh;
  706. table[2].data = &net->ipv4.frags.timeout;
  707. table[3].data = &net->ipv4.frags.max_dist;
  708. }
  709. hdr = register_net_sysctl(net, "net/ipv4", table);
  710. if (!hdr)
  711. goto err_reg;
  712. net->ipv4.frags_hdr = hdr;
  713. return 0;
  714. err_reg:
  715. if (!net_eq(net, &init_net))
  716. kfree(table);
  717. err_alloc:
  718. return -ENOMEM;
  719. }
  720. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  721. {
  722. struct ctl_table *table;
  723. table = net->ipv4.frags_hdr->ctl_table_arg;
  724. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  725. kfree(table);
  726. }
  727. static void __init ip4_frags_ctl_register(void)
  728. {
  729. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  730. }
  731. #else
  732. static int ip4_frags_ns_ctl_register(struct net *net)
  733. {
  734. return 0;
  735. }
  736. static void ip4_frags_ns_ctl_unregister(struct net *net)
  737. {
  738. }
  739. static void __init ip4_frags_ctl_register(void)
  740. {
  741. }
  742. #endif
  743. static int __net_init ipv4_frags_init_net(struct net *net)
  744. {
  745. int res;
  746. /* Fragment cache limits.
  747. *
  748. * The fragment memory accounting code, (tries to) account for
  749. * the real memory usage, by measuring both the size of frag
  750. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  751. * and the SKB's truesize.
  752. *
  753. * A 64K fragment consumes 129736 bytes (44*2944)+200
  754. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  755. *
  756. * We will commit 4MB at one time. Should we cross that limit
  757. * we will prune down to 3MB, making room for approx 8 big 64K
  758. * fragments 8x128k.
  759. */
  760. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  761. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  762. /*
  763. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  764. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  765. * by TTL.
  766. */
  767. net->ipv4.frags.timeout = IP_FRAG_TIME;
  768. net->ipv4.frags.max_dist = 64;
  769. net->ipv4.frags.f = &ip4_frags;
  770. res = inet_frags_init_net(&net->ipv4.frags);
  771. if (res < 0)
  772. return res;
  773. res = ip4_frags_ns_ctl_register(net);
  774. if (res < 0)
  775. inet_frags_exit_net(&net->ipv4.frags);
  776. return res;
  777. }
  778. static void __net_exit ipv4_frags_exit_net(struct net *net)
  779. {
  780. ip4_frags_ns_ctl_unregister(net);
  781. inet_frags_exit_net(&net->ipv4.frags);
  782. }
  783. static struct pernet_operations ip4_frags_ops = {
  784. .init = ipv4_frags_init_net,
  785. .exit = ipv4_frags_exit_net,
  786. };
  787. static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
  788. {
  789. return jhash2(data,
  790. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  791. }
  792. static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
  793. {
  794. const struct inet_frag_queue *fq = data;
  795. return jhash2((const u32 *)&fq->key.v4,
  796. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  797. }
  798. static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  799. {
  800. const struct frag_v4_compare_key *key = arg->key;
  801. const struct inet_frag_queue *fq = ptr;
  802. return !!memcmp(&fq->key, key, sizeof(*key));
  803. }
  804. static const struct rhashtable_params ip4_rhash_params = {
  805. .head_offset = offsetof(struct inet_frag_queue, node),
  806. .key_offset = offsetof(struct inet_frag_queue, key),
  807. .key_len = sizeof(struct frag_v4_compare_key),
  808. .hashfn = ip4_key_hashfn,
  809. .obj_hashfn = ip4_obj_hashfn,
  810. .obj_cmpfn = ip4_obj_cmpfn,
  811. .automatic_shrinking = true,
  812. };
  813. void __init ipfrag_init(void)
  814. {
  815. ip4_frags.constructor = ip4_frag_init;
  816. ip4_frags.destructor = ip4_frag_free;
  817. ip4_frags.qsize = sizeof(struct ipq);
  818. ip4_frags.frag_expire = ip_expire;
  819. ip4_frags.frags_cache_name = ip_frag_cache_name;
  820. ip4_frags.rhash_params = ip4_rhash_params;
  821. if (inet_frags_init(&ip4_frags))
  822. panic("IP: failed to allocate ip4_frags cache\n");
  823. ip4_frags_ctl_register();
  824. register_pernet_subsys(&ip4_frags_ops);
  825. }