ip_fragment.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The IP fragmentation functionality.
  7. *
  8. * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
  9. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  10. *
  11. * Fixes:
  12. * Alan Cox : Split from ip.c , see ip_input.c for history.
  13. * David S. Miller : Begin massive cleanup...
  14. * Andi Kleen : Add sysctls.
  15. * xxxx : Overlapfrag bug.
  16. * Ultima : ip_expire() kernel panic.
  17. * Bill Hawes : Frag accounting and evictor fixes.
  18. * John McDonald : 0 length frag bug.
  19. * Alexey Kuznetsov: SMP races, threading, cleanup.
  20. * Patrick McHardy : LRU queue of frag heads for evictor.
  21. */
  22. #define pr_fmt(fmt) "IPv4: " fmt
  23. #include <linux/compiler.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/list.h>
  30. #include <linux/ip.h>
  31. #include <linux/icmp.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/jhash.h>
  34. #include <linux/random.h>
  35. #include <linux/slab.h>
  36. #include <net/route.h>
  37. #include <net/dst.h>
  38. #include <net/sock.h>
  39. #include <net/ip.h>
  40. #include <net/icmp.h>
  41. #include <net/checksum.h>
  42. #include <net/inetpeer.h>
  43. #include <net/inet_frag.h>
  44. #include <linux/tcp.h>
  45. #include <linux/udp.h>
  46. #include <linux/inet.h>
  47. #include <linux/netfilter_ipv4.h>
  48. #include <net/inet_ecn.h>
  49. #include <net/l3mdev.h>
  50. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  51. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  52. * as well. Or notify me, at least. --ANK
  53. */
  54. static int sysctl_ipfrag_max_dist __read_mostly = 64;
  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 = sysctl_ipfrag_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(unsigned long arg)
  159. {
  160. const struct iphdr *iph;
  161. struct sk_buff *head = NULL;
  162. struct net *net;
  163. struct ipq *qp;
  164. int err;
  165. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  166. net = container_of(qp->q.net, struct net, ipv4.frags);
  167. rcu_read_lock();
  168. spin_lock(&qp->q.lock);
  169. if (qp->q.flags & INET_FRAG_COMPLETE)
  170. goto out;
  171. ipq_kill(qp);
  172. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  173. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
  174. if (!(qp->q.flags & INET_FRAG_FIRST_IN))
  175. goto out;
  176. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  177. * pull the head out of the tree in order to be able to
  178. * deal with head->dev.
  179. */
  180. if (qp->q.fragments) {
  181. head = qp->q.fragments;
  182. qp->q.fragments = head->next;
  183. } else {
  184. head = skb_rb_first(&qp->q.rb_fragments);
  185. if (!head)
  186. goto out;
  187. if (FRAG_CB(head)->next_frag)
  188. rb_replace_node(&head->rbnode,
  189. &FRAG_CB(head)->next_frag->rbnode,
  190. &qp->q.rb_fragments);
  191. else
  192. rb_erase(&head->rbnode, &qp->q.rb_fragments);
  193. memset(&head->rbnode, 0, sizeof(head->rbnode));
  194. barrier();
  195. }
  196. if (head == qp->q.fragments_tail)
  197. qp->q.fragments_tail = NULL;
  198. sub_frag_mem_limit(qp->q.net, head->truesize);
  199. head->dev = dev_get_by_index_rcu(net, qp->iif);
  200. if (!head->dev)
  201. goto out;
  202. /* skb has no dst, perform route lookup again */
  203. iph = ip_hdr(head);
  204. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  205. iph->tos, head->dev);
  206. if (err)
  207. goto out;
  208. /* Only an end host needs to send an ICMP
  209. * "Fragment Reassembly Timeout" message, per RFC792.
  210. */
  211. if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
  212. (skb_rtable(head)->rt_type != RTN_LOCAL))
  213. goto out;
  214. spin_unlock(&qp->q.lock);
  215. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  216. goto out_rcu_unlock;
  217. out:
  218. spin_unlock(&qp->q.lock);
  219. out_rcu_unlock:
  220. rcu_read_unlock();
  221. if (head)
  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 = sysctl_ipfrag_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_BH(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. atomic_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. int ihl, end, skb1_run_end;
  292. struct net_device *dev;
  293. unsigned int fragsize;
  294. int flags, offset;
  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 err;
  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 err;
  334. qp->q.len = end;
  335. }
  336. }
  337. if (end == offset)
  338. goto err;
  339. err = -ENOMEM;
  340. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  341. goto err;
  342. err = pskb_trim_rcsum(skb, end - offset);
  343. if (err)
  344. goto err;
  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) but
  356. * we do not want to drop the whole queue in response to a duplicate
  357. * fragment.
  358. */
  359. err = -EINVAL;
  360. /* Find out where to put this fragment. */
  361. prev_tail = qp->q.fragments_tail;
  362. if (!prev_tail)
  363. ip4_frag_create_run(&qp->q, skb); /* First fragment. */
  364. else if (prev_tail->ip_defrag_offset + prev_tail->len < end) {
  365. /* This is the common case: skb goes to the end. */
  366. /* Detect and discard overlaps. */
  367. if (offset < prev_tail->ip_defrag_offset + prev_tail->len)
  368. goto discard_qp;
  369. if (offset == prev_tail->ip_defrag_offset + prev_tail->len)
  370. ip4_frag_append_to_last_run(&qp->q, skb);
  371. else
  372. ip4_frag_create_run(&qp->q, skb);
  373. } else {
  374. /* Binary search. Note that skb can become the first fragment,
  375. * but not the last (covered above).
  376. */
  377. rbn = &qp->q.rb_fragments.rb_node;
  378. do {
  379. parent = *rbn;
  380. skb1 = rb_to_skb(parent);
  381. skb1_run_end = skb1->ip_defrag_offset +
  382. FRAG_CB(skb1)->frag_run_len;
  383. if (end <= skb1->ip_defrag_offset)
  384. rbn = &parent->rb_left;
  385. else if (offset >= skb1_run_end)
  386. rbn = &parent->rb_right;
  387. else if (offset >= skb1->ip_defrag_offset &&
  388. end <= skb1_run_end)
  389. goto err; /* No new data, potential duplicate */
  390. else
  391. goto discard_qp; /* Found an overlap */
  392. } while (*rbn);
  393. /* Here we have parent properly set, and rbn pointing to
  394. * one of its NULL left/right children. Insert skb.
  395. */
  396. ip4_frag_init_run(skb);
  397. rb_link_node(&skb->rbnode, parent, rbn);
  398. rb_insert_color(&skb->rbnode, &qp->q.rb_fragments);
  399. }
  400. if (dev)
  401. qp->iif = dev->ifindex;
  402. skb->ip_defrag_offset = offset;
  403. qp->q.stamp = skb->tstamp;
  404. qp->q.meat += skb->len;
  405. qp->ecn |= ecn;
  406. add_frag_mem_limit(qp->q.net, skb->truesize);
  407. if (offset == 0)
  408. qp->q.flags |= INET_FRAG_FIRST_IN;
  409. fragsize = skb->len + ihl;
  410. if (fragsize > qp->q.max_size)
  411. qp->q.max_size = fragsize;
  412. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  413. fragsize > qp->max_df_size)
  414. qp->max_df_size = fragsize;
  415. if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  416. qp->q.meat == qp->q.len) {
  417. unsigned long orefdst = skb->_skb_refdst;
  418. skb->_skb_refdst = 0UL;
  419. err = ip_frag_reasm(qp, skb, prev_tail, dev);
  420. skb->_skb_refdst = orefdst;
  421. return err;
  422. }
  423. skb_dst_drop(skb);
  424. return -EINPROGRESS;
  425. discard_qp:
  426. inet_frag_kill(&qp->q);
  427. IP_INC_STATS_BH(net, IPSTATS_MIB_REASM_OVERLAPS);
  428. err:
  429. kfree_skb(skb);
  430. return err;
  431. }
  432. /* Build a new IP datagram from all its fragments. */
  433. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
  434. struct sk_buff *prev_tail, struct net_device *dev)
  435. {
  436. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  437. struct iphdr *iph;
  438. struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments);
  439. struct sk_buff **nextp; /* To build frag_list. */
  440. struct rb_node *rbn;
  441. int len;
  442. int ihlen;
  443. int err;
  444. u8 ecn;
  445. ipq_kill(qp);
  446. ecn = ip_frag_ecn_table[qp->ecn];
  447. if (unlikely(ecn == 0xff)) {
  448. err = -EINVAL;
  449. goto out_fail;
  450. }
  451. /* Make the one we just received the head. */
  452. if (head != skb) {
  453. fp = skb_clone(skb, GFP_ATOMIC);
  454. if (!fp)
  455. goto out_nomem;
  456. FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
  457. if (RB_EMPTY_NODE(&skb->rbnode))
  458. FRAG_CB(prev_tail)->next_frag = fp;
  459. else
  460. rb_replace_node(&skb->rbnode, &fp->rbnode,
  461. &qp->q.rb_fragments);
  462. if (qp->q.fragments_tail == skb)
  463. qp->q.fragments_tail = fp;
  464. skb_morph(skb, head);
  465. FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
  466. rb_replace_node(&head->rbnode, &skb->rbnode,
  467. &qp->q.rb_fragments);
  468. consume_skb(head);
  469. head = skb;
  470. }
  471. WARN_ON(head->ip_defrag_offset != 0);
  472. /* Allocate a new buffer for the datagram. */
  473. ihlen = ip_hdrlen(head);
  474. len = ihlen + qp->q.len;
  475. err = -E2BIG;
  476. if (len > 65535)
  477. goto out_oversize;
  478. /* Head of list must not be cloned. */
  479. if (skb_unclone(head, GFP_ATOMIC))
  480. goto out_nomem;
  481. /* If the first fragment is fragmented itself, we split
  482. * it to two chunks: the first with data and paged part
  483. * and the second, holding only fragments. */
  484. if (skb_has_frag_list(head)) {
  485. struct sk_buff *clone;
  486. int i, plen = 0;
  487. clone = alloc_skb(0, GFP_ATOMIC);
  488. if (!clone)
  489. goto out_nomem;
  490. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  491. skb_frag_list_init(head);
  492. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  493. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  494. clone->len = clone->data_len = head->data_len - plen;
  495. head->truesize += clone->truesize;
  496. clone->csum = 0;
  497. clone->ip_summed = head->ip_summed;
  498. add_frag_mem_limit(qp->q.net, clone->truesize);
  499. skb_shinfo(head)->frag_list = clone;
  500. nextp = &clone->next;
  501. } else {
  502. nextp = &skb_shinfo(head)->frag_list;
  503. }
  504. skb_push(head, head->data - skb_network_header(head));
  505. /* Traverse the tree in order, to build frag_list. */
  506. fp = FRAG_CB(head)->next_frag;
  507. rbn = rb_next(&head->rbnode);
  508. rb_erase(&head->rbnode, &qp->q.rb_fragments);
  509. while (rbn || fp) {
  510. /* fp points to the next sk_buff in the current run;
  511. * rbn points to the next run.
  512. */
  513. /* Go through the current run. */
  514. while (fp) {
  515. *nextp = fp;
  516. nextp = &fp->next;
  517. fp->prev = NULL;
  518. memset(&fp->rbnode, 0, sizeof(fp->rbnode));
  519. fp->sk = NULL;
  520. head->data_len += fp->len;
  521. head->len += fp->len;
  522. if (head->ip_summed != fp->ip_summed)
  523. head->ip_summed = CHECKSUM_NONE;
  524. else if (head->ip_summed == CHECKSUM_COMPLETE)
  525. head->csum = csum_add(head->csum, fp->csum);
  526. head->truesize += fp->truesize;
  527. fp = FRAG_CB(fp)->next_frag;
  528. }
  529. /* Move to the next run. */
  530. if (rbn) {
  531. struct rb_node *rbnext = rb_next(rbn);
  532. fp = rb_to_skb(rbn);
  533. rb_erase(rbn, &qp->q.rb_fragments);
  534. rbn = rbnext;
  535. }
  536. }
  537. sub_frag_mem_limit(qp->q.net, head->truesize);
  538. *nextp = NULL;
  539. head->next = NULL;
  540. head->prev = NULL;
  541. head->dev = dev;
  542. head->tstamp = qp->q.stamp;
  543. IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  544. iph = ip_hdr(head);
  545. iph->tot_len = htons(len);
  546. iph->tos |= ecn;
  547. /* When we set IP_DF on a refragmented skb we must also force a
  548. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  549. * original sender only sent fragments of size f (where f < s).
  550. *
  551. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  552. * frag seen to avoid sending tiny DF-fragments in case skb was built
  553. * from one very small df-fragment and one large non-df frag.
  554. */
  555. if (qp->max_df_size == qp->q.max_size) {
  556. IPCB(head)->flags |= IPSKB_FRAG_PMTU;
  557. iph->frag_off = htons(IP_DF);
  558. } else {
  559. iph->frag_off = 0;
  560. }
  561. ip_send_check(iph);
  562. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
  563. qp->q.fragments = NULL;
  564. qp->q.rb_fragments = RB_ROOT;
  565. qp->q.fragments_tail = NULL;
  566. qp->q.last_run_head = NULL;
  567. return 0;
  568. out_nomem:
  569. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  570. err = -ENOMEM;
  571. goto out_fail;
  572. out_oversize:
  573. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
  574. out_fail:
  575. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  576. return err;
  577. }
  578. /* Process an incoming IP datagram fragment. */
  579. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  580. {
  581. struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
  582. int vif = l3mdev_master_ifindex_rcu(dev);
  583. struct ipq *qp;
  584. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
  585. skb_orphan(skb);
  586. /* Lookup (or create) queue header */
  587. qp = ip_find(net, ip_hdr(skb), user, vif);
  588. if (qp) {
  589. int ret;
  590. spin_lock(&qp->q.lock);
  591. ret = ip_frag_queue(qp, skb);
  592. spin_unlock(&qp->q.lock);
  593. ipq_put(qp);
  594. return ret;
  595. }
  596. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  597. kfree_skb(skb);
  598. return -ENOMEM;
  599. }
  600. EXPORT_SYMBOL(ip_defrag);
  601. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  602. {
  603. struct iphdr iph;
  604. int netoff;
  605. u32 len;
  606. if (skb->protocol != htons(ETH_P_IP))
  607. return skb;
  608. netoff = skb_network_offset(skb);
  609. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  610. return skb;
  611. if (iph.ihl < 5 || iph.version != 4)
  612. return skb;
  613. len = ntohs(iph.tot_len);
  614. if (skb->len < netoff + len || len < (iph.ihl * 4))
  615. return skb;
  616. if (ip_is_fragment(&iph)) {
  617. skb = skb_share_check(skb, GFP_ATOMIC);
  618. if (skb) {
  619. if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
  620. kfree_skb(skb);
  621. return NULL;
  622. }
  623. if (pskb_trim_rcsum(skb, netoff + len)) {
  624. kfree_skb(skb);
  625. return NULL;
  626. }
  627. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  628. if (ip_defrag(net, skb, user))
  629. return NULL;
  630. skb_clear_hash(skb);
  631. }
  632. }
  633. return skb;
  634. }
  635. EXPORT_SYMBOL(ip_check_defrag);
  636. unsigned int inet_frag_rbtree_purge(struct rb_root *root)
  637. {
  638. struct rb_node *p = rb_first(root);
  639. unsigned int sum = 0;
  640. while (p) {
  641. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  642. p = rb_next(p);
  643. rb_erase(&skb->rbnode, root);
  644. while (skb) {
  645. struct sk_buff *next = FRAG_CB(skb)->next_frag;
  646. sum += skb->truesize;
  647. kfree_skb(skb);
  648. skb = next;
  649. }
  650. }
  651. return sum;
  652. }
  653. EXPORT_SYMBOL(inet_frag_rbtree_purge);
  654. #ifdef CONFIG_SYSCTL
  655. static int dist_min;
  656. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  657. {
  658. .procname = "ipfrag_high_thresh",
  659. .data = &init_net.ipv4.frags.high_thresh,
  660. .maxlen = sizeof(unsigned long),
  661. .mode = 0644,
  662. .proc_handler = proc_doulongvec_minmax,
  663. .extra1 = &init_net.ipv4.frags.low_thresh
  664. },
  665. {
  666. .procname = "ipfrag_low_thresh",
  667. .data = &init_net.ipv4.frags.low_thresh,
  668. .maxlen = sizeof(unsigned long),
  669. .mode = 0644,
  670. .proc_handler = proc_doulongvec_minmax,
  671. .extra2 = &init_net.ipv4.frags.high_thresh
  672. },
  673. {
  674. .procname = "ipfrag_time",
  675. .data = &init_net.ipv4.frags.timeout,
  676. .maxlen = sizeof(int),
  677. .mode = 0644,
  678. .proc_handler = proc_dointvec_jiffies,
  679. },
  680. { }
  681. };
  682. /* secret interval has been deprecated */
  683. static int ip4_frags_secret_interval_unused;
  684. static struct ctl_table ip4_frags_ctl_table[] = {
  685. {
  686. .procname = "ipfrag_secret_interval",
  687. .data = &ip4_frags_secret_interval_unused,
  688. .maxlen = sizeof(int),
  689. .mode = 0644,
  690. .proc_handler = proc_dointvec_jiffies,
  691. },
  692. {
  693. .procname = "ipfrag_max_dist",
  694. .data = &sysctl_ipfrag_max_dist,
  695. .maxlen = sizeof(int),
  696. .mode = 0644,
  697. .proc_handler = proc_dointvec_minmax,
  698. .extra1 = &dist_min,
  699. },
  700. { }
  701. };
  702. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  703. {
  704. struct ctl_table *table;
  705. struct ctl_table_header *hdr;
  706. table = ip4_frags_ns_ctl_table;
  707. if (!net_eq(net, &init_net)) {
  708. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  709. if (!table)
  710. goto err_alloc;
  711. table[0].data = &net->ipv4.frags.high_thresh;
  712. table[0].extra1 = &net->ipv4.frags.low_thresh;
  713. table[0].extra2 = &init_net.ipv4.frags.high_thresh;
  714. table[1].data = &net->ipv4.frags.low_thresh;
  715. table[1].extra2 = &net->ipv4.frags.high_thresh;
  716. table[2].data = &net->ipv4.frags.timeout;
  717. /* Don't export sysctls to unprivileged users */
  718. if (net->user_ns != &init_user_ns)
  719. table[0].procname = NULL;
  720. }
  721. hdr = register_net_sysctl(net, "net/ipv4", table);
  722. if (!hdr)
  723. goto err_reg;
  724. net->ipv4.frags_hdr = hdr;
  725. return 0;
  726. err_reg:
  727. if (!net_eq(net, &init_net))
  728. kfree(table);
  729. err_alloc:
  730. return -ENOMEM;
  731. }
  732. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  733. {
  734. struct ctl_table *table;
  735. table = net->ipv4.frags_hdr->ctl_table_arg;
  736. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  737. kfree(table);
  738. }
  739. static void __init ip4_frags_ctl_register(void)
  740. {
  741. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  742. }
  743. #else
  744. static int ip4_frags_ns_ctl_register(struct net *net)
  745. {
  746. return 0;
  747. }
  748. static void ip4_frags_ns_ctl_unregister(struct net *net)
  749. {
  750. }
  751. static void __init ip4_frags_ctl_register(void)
  752. {
  753. }
  754. #endif
  755. static int __net_init ipv4_frags_init_net(struct net *net)
  756. {
  757. int res;
  758. /* Fragment cache limits.
  759. *
  760. * The fragment memory accounting code, (tries to) account for
  761. * the real memory usage, by measuring both the size of frag
  762. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  763. * and the SKB's truesize.
  764. *
  765. * A 64K fragment consumes 129736 bytes (44*2944)+200
  766. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  767. *
  768. * We will commit 4MB at one time. Should we cross that limit
  769. * we will prune down to 3MB, making room for approx 8 big 64K
  770. * fragments 8x128k.
  771. */
  772. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  773. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  774. /*
  775. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  776. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  777. * by TTL.
  778. */
  779. net->ipv4.frags.timeout = IP_FRAG_TIME;
  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.skb_free = NULL;
  829. ip4_frags.qsize = sizeof(struct ipq);
  830. ip4_frags.frag_expire = ip_expire;
  831. ip4_frags.frags_cache_name = ip_frag_cache_name;
  832. ip4_frags.rhash_params = ip4_rhash_params;
  833. if (inet_frags_init(&ip4_frags))
  834. panic("IP: failed to allocate ip4_frags cache\n");
  835. ip4_frags_ctl_register();
  836. register_pernet_subsys(&ip4_frags_ops);
  837. }