ip_fragment.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. /* Describe an entry in the "incomplete datagrams" queue. */
  57. struct ipq {
  58. struct inet_frag_queue q;
  59. u8 ecn; /* RFC3168 support */
  60. u16 max_df_size; /* largest frag with DF set seen */
  61. int iif;
  62. unsigned int rid;
  63. struct inet_peer *peer;
  64. };
  65. static u8 ip4_frag_ecn(u8 tos)
  66. {
  67. return 1 << (tos & INET_ECN_MASK);
  68. }
  69. static struct inet_frags ip4_frags;
  70. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  71. struct net_device *dev);
  72. static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
  73. {
  74. struct ipq *qp = container_of(q, struct ipq, q);
  75. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  76. frags);
  77. struct net *net = container_of(ipv4, struct net, ipv4);
  78. const struct frag_v4_compare_key *key = a;
  79. q->key.v4 = *key;
  80. qp->ecn = 0;
  81. qp->peer = q->net->max_dist ?
  82. inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
  83. NULL;
  84. }
  85. static void ip4_frag_free(struct inet_frag_queue *q)
  86. {
  87. struct ipq *qp;
  88. qp = container_of(q, struct ipq, q);
  89. if (qp->peer)
  90. inet_putpeer(qp->peer);
  91. }
  92. /* Destruction primitives. */
  93. static void ipq_put(struct ipq *ipq)
  94. {
  95. inet_frag_put(&ipq->q);
  96. }
  97. /* Kill ipq entry. It is not destroyed immediately,
  98. * because caller (and someone more) holds reference count.
  99. */
  100. static void ipq_kill(struct ipq *ipq)
  101. {
  102. inet_frag_kill(&ipq->q);
  103. }
  104. static bool frag_expire_skip_icmp(u32 user)
  105. {
  106. return user == IP_DEFRAG_AF_PACKET ||
  107. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
  108. __IP_DEFRAG_CONNTRACK_IN_END) ||
  109. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  110. __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
  111. }
  112. /*
  113. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  114. */
  115. static void ip_expire(struct timer_list *t)
  116. {
  117. struct inet_frag_queue *frag = from_timer(frag, t, timer);
  118. const struct iphdr *iph;
  119. struct sk_buff *head;
  120. struct net *net;
  121. struct ipq *qp;
  122. int err;
  123. qp = container_of(frag, struct ipq, q);
  124. net = container_of(qp->q.net, struct net, ipv4.frags);
  125. rcu_read_lock();
  126. spin_lock(&qp->q.lock);
  127. if (qp->q.flags & INET_FRAG_COMPLETE)
  128. goto out;
  129. ipq_kill(qp);
  130. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  131. head = qp->q.fragments;
  132. __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
  133. if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
  134. goto out;
  135. head->dev = dev_get_by_index_rcu(net, qp->iif);
  136. if (!head->dev)
  137. goto out;
  138. /* skb has no dst, perform route lookup again */
  139. iph = ip_hdr(head);
  140. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  141. iph->tos, head->dev);
  142. if (err)
  143. goto out;
  144. /* Only an end host needs to send an ICMP
  145. * "Fragment Reassembly Timeout" message, per RFC792.
  146. */
  147. if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
  148. (skb_rtable(head)->rt_type != RTN_LOCAL))
  149. goto out;
  150. skb_get(head);
  151. spin_unlock(&qp->q.lock);
  152. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  153. kfree_skb(head);
  154. goto out_rcu_unlock;
  155. out:
  156. spin_unlock(&qp->q.lock);
  157. out_rcu_unlock:
  158. rcu_read_unlock();
  159. ipq_put(qp);
  160. }
  161. /* Find the correct entry in the "incomplete datagrams" queue for
  162. * this IP datagram, and create new one, if nothing is found.
  163. */
  164. static struct ipq *ip_find(struct net *net, struct iphdr *iph,
  165. u32 user, int vif)
  166. {
  167. struct frag_v4_compare_key key = {
  168. .saddr = iph->saddr,
  169. .daddr = iph->daddr,
  170. .user = user,
  171. .vif = vif,
  172. .id = iph->id,
  173. .protocol = iph->protocol,
  174. };
  175. struct inet_frag_queue *q;
  176. q = inet_frag_find(&net->ipv4.frags, &key);
  177. if (!q)
  178. return NULL;
  179. return container_of(q, struct ipq, q);
  180. }
  181. /* Is the fragment too far ahead to be part of ipq? */
  182. static int ip_frag_too_far(struct ipq *qp)
  183. {
  184. struct inet_peer *peer = qp->peer;
  185. unsigned int max = qp->q.net->max_dist;
  186. unsigned int start, end;
  187. int rc;
  188. if (!peer || !max)
  189. return 0;
  190. start = qp->rid;
  191. end = atomic_inc_return(&peer->rid);
  192. qp->rid = end;
  193. rc = qp->q.fragments && (end - start) > max;
  194. if (rc) {
  195. struct net *net;
  196. net = container_of(qp->q.net, struct net, ipv4.frags);
  197. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  198. }
  199. return rc;
  200. }
  201. static int ip_frag_reinit(struct ipq *qp)
  202. {
  203. struct sk_buff *fp;
  204. unsigned int sum_truesize = 0;
  205. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  206. refcount_inc(&qp->q.refcnt);
  207. return -ETIMEDOUT;
  208. }
  209. fp = qp->q.fragments;
  210. do {
  211. struct sk_buff *xp = fp->next;
  212. sum_truesize += fp->truesize;
  213. kfree_skb(fp);
  214. fp = xp;
  215. } while (fp);
  216. sub_frag_mem_limit(qp->q.net, sum_truesize);
  217. qp->q.flags = 0;
  218. qp->q.len = 0;
  219. qp->q.meat = 0;
  220. qp->q.fragments = NULL;
  221. qp->q.fragments_tail = NULL;
  222. qp->iif = 0;
  223. qp->ecn = 0;
  224. return 0;
  225. }
  226. /* Add new segment to existing queue. */
  227. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  228. {
  229. struct sk_buff *prev, *next;
  230. struct net_device *dev;
  231. unsigned int fragsize;
  232. int flags, offset;
  233. int ihl, end;
  234. int err = -ENOENT;
  235. u8 ecn;
  236. if (qp->q.flags & INET_FRAG_COMPLETE)
  237. goto err;
  238. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  239. unlikely(ip_frag_too_far(qp)) &&
  240. unlikely(err = ip_frag_reinit(qp))) {
  241. ipq_kill(qp);
  242. goto err;
  243. }
  244. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  245. offset = ntohs(ip_hdr(skb)->frag_off);
  246. flags = offset & ~IP_OFFSET;
  247. offset &= IP_OFFSET;
  248. offset <<= 3; /* offset is in 8-byte chunks */
  249. ihl = ip_hdrlen(skb);
  250. /* Determine the position of this fragment. */
  251. end = offset + skb->len - skb_network_offset(skb) - ihl;
  252. err = -EINVAL;
  253. /* Is this the final fragment? */
  254. if ((flags & IP_MF) == 0) {
  255. /* If we already have some bits beyond end
  256. * or have different end, the segment is corrupted.
  257. */
  258. if (end < qp->q.len ||
  259. ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
  260. goto err;
  261. qp->q.flags |= INET_FRAG_LAST_IN;
  262. qp->q.len = end;
  263. } else {
  264. if (end&7) {
  265. end &= ~7;
  266. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  267. skb->ip_summed = CHECKSUM_NONE;
  268. }
  269. if (end > qp->q.len) {
  270. /* Some bits beyond end -> corruption. */
  271. if (qp->q.flags & INET_FRAG_LAST_IN)
  272. goto err;
  273. qp->q.len = end;
  274. }
  275. }
  276. if (end == offset)
  277. goto err;
  278. err = -ENOMEM;
  279. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  280. goto err;
  281. err = pskb_trim_rcsum(skb, end - offset);
  282. if (err)
  283. goto err;
  284. /* Find out which fragments are in front and at the back of us
  285. * in the chain of fragments so far. We must know where to put
  286. * this fragment, right?
  287. */
  288. prev = qp->q.fragments_tail;
  289. if (!prev || prev->ip_defrag_offset < offset) {
  290. next = NULL;
  291. goto found;
  292. }
  293. prev = NULL;
  294. for (next = qp->q.fragments; next != NULL; next = next->next) {
  295. if (next->ip_defrag_offset >= offset)
  296. break; /* bingo! */
  297. prev = next;
  298. }
  299. found:
  300. /* We found where to put this one. Check for overlap with
  301. * preceding fragment, and, if needed, align things so that
  302. * any overlaps are eliminated.
  303. */
  304. if (prev) {
  305. int i = (prev->ip_defrag_offset + prev->len) - offset;
  306. if (i > 0) {
  307. offset += i;
  308. err = -EINVAL;
  309. if (end <= offset)
  310. goto err;
  311. err = -ENOMEM;
  312. if (!pskb_pull(skb, i))
  313. goto err;
  314. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  315. skb->ip_summed = CHECKSUM_NONE;
  316. }
  317. }
  318. err = -ENOMEM;
  319. while (next && next->ip_defrag_offset < end) {
  320. int i = end - next->ip_defrag_offset; /* overlap is 'i' bytes */
  321. if (i < next->len) {
  322. /* Eat head of the next overlapped fragment
  323. * and leave the loop. The next ones cannot overlap.
  324. */
  325. if (!pskb_pull(next, i))
  326. goto err;
  327. next->ip_defrag_offset += i;
  328. qp->q.meat -= i;
  329. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  330. next->ip_summed = CHECKSUM_NONE;
  331. break;
  332. } else {
  333. struct sk_buff *free_it = next;
  334. /* Old fragment is completely overridden with
  335. * new one drop it.
  336. */
  337. next = next->next;
  338. if (prev)
  339. prev->next = next;
  340. else
  341. qp->q.fragments = next;
  342. qp->q.meat -= free_it->len;
  343. sub_frag_mem_limit(qp->q.net, free_it->truesize);
  344. kfree_skb(free_it);
  345. }
  346. }
  347. /* Note : skb->ip_defrag_offset and skb->dev share the same location */
  348. dev = skb->dev;
  349. if (dev)
  350. qp->iif = dev->ifindex;
  351. /* Makes sure compiler wont do silly aliasing games */
  352. barrier();
  353. skb->ip_defrag_offset = offset;
  354. /* Insert this fragment in the chain of fragments. */
  355. skb->next = next;
  356. if (!next)
  357. qp->q.fragments_tail = skb;
  358. if (prev)
  359. prev->next = skb;
  360. else
  361. qp->q.fragments = skb;
  362. qp->q.stamp = skb->tstamp;
  363. qp->q.meat += skb->len;
  364. qp->ecn |= ecn;
  365. add_frag_mem_limit(qp->q.net, skb->truesize);
  366. if (offset == 0)
  367. qp->q.flags |= INET_FRAG_FIRST_IN;
  368. fragsize = skb->len + ihl;
  369. if (fragsize > qp->q.max_size)
  370. qp->q.max_size = fragsize;
  371. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  372. fragsize > qp->max_df_size)
  373. qp->max_df_size = fragsize;
  374. if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  375. qp->q.meat == qp->q.len) {
  376. unsigned long orefdst = skb->_skb_refdst;
  377. skb->_skb_refdst = 0UL;
  378. err = ip_frag_reasm(qp, prev, dev);
  379. skb->_skb_refdst = orefdst;
  380. return err;
  381. }
  382. skb_dst_drop(skb);
  383. return -EINPROGRESS;
  384. err:
  385. kfree_skb(skb);
  386. return err;
  387. }
  388. /* Build a new IP datagram from all its fragments. */
  389. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  390. struct net_device *dev)
  391. {
  392. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  393. struct iphdr *iph;
  394. struct sk_buff *fp, *head = qp->q.fragments;
  395. int len;
  396. int ihlen;
  397. int err;
  398. u8 ecn;
  399. ipq_kill(qp);
  400. ecn = ip_frag_ecn_table[qp->ecn];
  401. if (unlikely(ecn == 0xff)) {
  402. err = -EINVAL;
  403. goto out_fail;
  404. }
  405. /* Make the one we just received the head. */
  406. if (prev) {
  407. head = prev->next;
  408. fp = skb_clone(head, GFP_ATOMIC);
  409. if (!fp)
  410. goto out_nomem;
  411. fp->next = head->next;
  412. if (!fp->next)
  413. qp->q.fragments_tail = fp;
  414. prev->next = fp;
  415. skb_morph(head, qp->q.fragments);
  416. head->next = qp->q.fragments->next;
  417. consume_skb(qp->q.fragments);
  418. qp->q.fragments = head;
  419. }
  420. WARN_ON(!head);
  421. WARN_ON(head->ip_defrag_offset != 0);
  422. /* Allocate a new buffer for the datagram. */
  423. ihlen = ip_hdrlen(head);
  424. len = ihlen + qp->q.len;
  425. err = -E2BIG;
  426. if (len > 65535)
  427. goto out_oversize;
  428. /* Head of list must not be cloned. */
  429. if (skb_unclone(head, GFP_ATOMIC))
  430. goto out_nomem;
  431. /* If the first fragment is fragmented itself, we split
  432. * it to two chunks: the first with data and paged part
  433. * and the second, holding only fragments. */
  434. if (skb_has_frag_list(head)) {
  435. struct sk_buff *clone;
  436. int i, plen = 0;
  437. clone = alloc_skb(0, GFP_ATOMIC);
  438. if (!clone)
  439. goto out_nomem;
  440. clone->next = head->next;
  441. head->next = clone;
  442. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  443. skb_frag_list_init(head);
  444. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  445. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  446. clone->len = clone->data_len = head->data_len - plen;
  447. head->data_len -= clone->len;
  448. head->len -= clone->len;
  449. clone->csum = 0;
  450. clone->ip_summed = head->ip_summed;
  451. add_frag_mem_limit(qp->q.net, clone->truesize);
  452. }
  453. skb_shinfo(head)->frag_list = head->next;
  454. skb_push(head, head->data - skb_network_header(head));
  455. for (fp=head->next; fp; fp = fp->next) {
  456. head->data_len += fp->len;
  457. head->len += fp->len;
  458. if (head->ip_summed != fp->ip_summed)
  459. head->ip_summed = CHECKSUM_NONE;
  460. else if (head->ip_summed == CHECKSUM_COMPLETE)
  461. head->csum = csum_add(head->csum, fp->csum);
  462. head->truesize += fp->truesize;
  463. }
  464. sub_frag_mem_limit(qp->q.net, head->truesize);
  465. head->next = NULL;
  466. head->dev = dev;
  467. head->tstamp = qp->q.stamp;
  468. IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  469. iph = ip_hdr(head);
  470. iph->tot_len = htons(len);
  471. iph->tos |= ecn;
  472. /* When we set IP_DF on a refragmented skb we must also force a
  473. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  474. * original sender only sent fragments of size f (where f < s).
  475. *
  476. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  477. * frag seen to avoid sending tiny DF-fragments in case skb was built
  478. * from one very small df-fragment and one large non-df frag.
  479. */
  480. if (qp->max_df_size == qp->q.max_size) {
  481. IPCB(head)->flags |= IPSKB_FRAG_PMTU;
  482. iph->frag_off = htons(IP_DF);
  483. } else {
  484. iph->frag_off = 0;
  485. }
  486. ip_send_check(iph);
  487. __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
  488. qp->q.fragments = NULL;
  489. qp->q.fragments_tail = NULL;
  490. return 0;
  491. out_nomem:
  492. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  493. err = -ENOMEM;
  494. goto out_fail;
  495. out_oversize:
  496. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
  497. out_fail:
  498. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  499. return err;
  500. }
  501. /* Process an incoming IP datagram fragment. */
  502. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  503. {
  504. struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
  505. int vif = l3mdev_master_ifindex_rcu(dev);
  506. struct ipq *qp;
  507. __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
  508. skb_orphan(skb);
  509. /* Lookup (or create) queue header */
  510. qp = ip_find(net, ip_hdr(skb), user, vif);
  511. if (qp) {
  512. int ret;
  513. spin_lock(&qp->q.lock);
  514. ret = ip_frag_queue(qp, skb);
  515. spin_unlock(&qp->q.lock);
  516. ipq_put(qp);
  517. return ret;
  518. }
  519. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  520. kfree_skb(skb);
  521. return -ENOMEM;
  522. }
  523. EXPORT_SYMBOL(ip_defrag);
  524. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  525. {
  526. struct iphdr iph;
  527. int netoff;
  528. u32 len;
  529. if (skb->protocol != htons(ETH_P_IP))
  530. return skb;
  531. netoff = skb_network_offset(skb);
  532. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  533. return skb;
  534. if (iph.ihl < 5 || iph.version != 4)
  535. return skb;
  536. len = ntohs(iph.tot_len);
  537. if (skb->len < netoff + len || len < (iph.ihl * 4))
  538. return skb;
  539. if (ip_is_fragment(&iph)) {
  540. skb = skb_share_check(skb, GFP_ATOMIC);
  541. if (skb) {
  542. if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
  543. return skb;
  544. if (pskb_trim_rcsum(skb, netoff + len))
  545. return skb;
  546. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  547. if (ip_defrag(net, skb, user))
  548. return NULL;
  549. skb_clear_hash(skb);
  550. }
  551. }
  552. return skb;
  553. }
  554. EXPORT_SYMBOL(ip_check_defrag);
  555. #ifdef CONFIG_SYSCTL
  556. static int dist_min;
  557. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  558. {
  559. .procname = "ipfrag_high_thresh",
  560. .data = &init_net.ipv4.frags.high_thresh,
  561. .maxlen = sizeof(unsigned long),
  562. .mode = 0644,
  563. .proc_handler = proc_doulongvec_minmax,
  564. .extra1 = &init_net.ipv4.frags.low_thresh
  565. },
  566. {
  567. .procname = "ipfrag_low_thresh",
  568. .data = &init_net.ipv4.frags.low_thresh,
  569. .maxlen = sizeof(unsigned long),
  570. .mode = 0644,
  571. .proc_handler = proc_doulongvec_minmax,
  572. .extra2 = &init_net.ipv4.frags.high_thresh
  573. },
  574. {
  575. .procname = "ipfrag_time",
  576. .data = &init_net.ipv4.frags.timeout,
  577. .maxlen = sizeof(int),
  578. .mode = 0644,
  579. .proc_handler = proc_dointvec_jiffies,
  580. },
  581. {
  582. .procname = "ipfrag_max_dist",
  583. .data = &init_net.ipv4.frags.max_dist,
  584. .maxlen = sizeof(int),
  585. .mode = 0644,
  586. .proc_handler = proc_dointvec_minmax,
  587. .extra1 = &dist_min,
  588. },
  589. { }
  590. };
  591. /* secret interval has been deprecated */
  592. static int ip4_frags_secret_interval_unused;
  593. static struct ctl_table ip4_frags_ctl_table[] = {
  594. {
  595. .procname = "ipfrag_secret_interval",
  596. .data = &ip4_frags_secret_interval_unused,
  597. .maxlen = sizeof(int),
  598. .mode = 0644,
  599. .proc_handler = proc_dointvec_jiffies,
  600. },
  601. { }
  602. };
  603. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  604. {
  605. struct ctl_table *table;
  606. struct ctl_table_header *hdr;
  607. table = ip4_frags_ns_ctl_table;
  608. if (!net_eq(net, &init_net)) {
  609. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  610. if (!table)
  611. goto err_alloc;
  612. table[0].data = &net->ipv4.frags.high_thresh;
  613. table[0].extra1 = &net->ipv4.frags.low_thresh;
  614. table[0].extra2 = &init_net.ipv4.frags.high_thresh;
  615. table[1].data = &net->ipv4.frags.low_thresh;
  616. table[1].extra2 = &net->ipv4.frags.high_thresh;
  617. table[2].data = &net->ipv4.frags.timeout;
  618. table[3].data = &net->ipv4.frags.max_dist;
  619. }
  620. hdr = register_net_sysctl(net, "net/ipv4", table);
  621. if (!hdr)
  622. goto err_reg;
  623. net->ipv4.frags_hdr = hdr;
  624. return 0;
  625. err_reg:
  626. if (!net_eq(net, &init_net))
  627. kfree(table);
  628. err_alloc:
  629. return -ENOMEM;
  630. }
  631. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  632. {
  633. struct ctl_table *table;
  634. table = net->ipv4.frags_hdr->ctl_table_arg;
  635. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  636. kfree(table);
  637. }
  638. static void __init ip4_frags_ctl_register(void)
  639. {
  640. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  641. }
  642. #else
  643. static int ip4_frags_ns_ctl_register(struct net *net)
  644. {
  645. return 0;
  646. }
  647. static void ip4_frags_ns_ctl_unregister(struct net *net)
  648. {
  649. }
  650. static void __init ip4_frags_ctl_register(void)
  651. {
  652. }
  653. #endif
  654. static int __net_init ipv4_frags_init_net(struct net *net)
  655. {
  656. int res;
  657. /* Fragment cache limits.
  658. *
  659. * The fragment memory accounting code, (tries to) account for
  660. * the real memory usage, by measuring both the size of frag
  661. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  662. * and the SKB's truesize.
  663. *
  664. * A 64K fragment consumes 129736 bytes (44*2944)+200
  665. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  666. *
  667. * We will commit 4MB at one time. Should we cross that limit
  668. * we will prune down to 3MB, making room for approx 8 big 64K
  669. * fragments 8x128k.
  670. */
  671. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  672. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  673. /*
  674. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  675. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  676. * by TTL.
  677. */
  678. net->ipv4.frags.timeout = IP_FRAG_TIME;
  679. net->ipv4.frags.max_dist = 64;
  680. net->ipv4.frags.f = &ip4_frags;
  681. res = inet_frags_init_net(&net->ipv4.frags);
  682. if (res < 0)
  683. return res;
  684. res = ip4_frags_ns_ctl_register(net);
  685. if (res < 0)
  686. inet_frags_exit_net(&net->ipv4.frags);
  687. return res;
  688. }
  689. static void __net_exit ipv4_frags_exit_net(struct net *net)
  690. {
  691. ip4_frags_ns_ctl_unregister(net);
  692. inet_frags_exit_net(&net->ipv4.frags);
  693. }
  694. static struct pernet_operations ip4_frags_ops = {
  695. .init = ipv4_frags_init_net,
  696. .exit = ipv4_frags_exit_net,
  697. };
  698. static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
  699. {
  700. return jhash2(data,
  701. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  702. }
  703. static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
  704. {
  705. const struct inet_frag_queue *fq = data;
  706. return jhash2((const u32 *)&fq->key.v4,
  707. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  708. }
  709. static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  710. {
  711. const struct frag_v4_compare_key *key = arg->key;
  712. const struct inet_frag_queue *fq = ptr;
  713. return !!memcmp(&fq->key, key, sizeof(*key));
  714. }
  715. static const struct rhashtable_params ip4_rhash_params = {
  716. .head_offset = offsetof(struct inet_frag_queue, node),
  717. .key_offset = offsetof(struct inet_frag_queue, key),
  718. .key_len = sizeof(struct frag_v4_compare_key),
  719. .hashfn = ip4_key_hashfn,
  720. .obj_hashfn = ip4_obj_hashfn,
  721. .obj_cmpfn = ip4_obj_cmpfn,
  722. .automatic_shrinking = true,
  723. };
  724. void __init ipfrag_init(void)
  725. {
  726. ip4_frags.constructor = ip4_frag_init;
  727. ip4_frags.destructor = ip4_frag_free;
  728. ip4_frags.qsize = sizeof(struct ipq);
  729. ip4_frags.frag_expire = ip_expire;
  730. ip4_frags.frags_cache_name = ip_frag_cache_name;
  731. ip4_frags.rhash_params = ip4_rhash_params;
  732. if (inet_frags_init(&ip4_frags))
  733. panic("IP: failed to allocate ip4_frags cache\n");
  734. ip4_frags_ctl_register();
  735. register_pernet_subsys(&ip4_frags_ops);
  736. }