ip_fragment.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 *skb,
  71. struct sk_buff *prev_tail, struct net_device *dev,
  72. int *refs);
  73. static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
  74. {
  75. struct ipq *qp = container_of(q, struct ipq, q);
  76. const struct frag_v4_compare_key *key = a;
  77. struct net *net = q->fqdir->net;
  78. struct inet_peer *p = NULL;
  79. q->key.v4 = *key;
  80. qp->ecn = 0;
  81. if (q->fqdir->max_dist) {
  82. rcu_read_lock();
  83. p = inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif);
  84. if (p && !refcount_inc_not_zero(&p->refcnt))
  85. p = NULL;
  86. rcu_read_unlock();
  87. }
  88. qp->peer = p;
  89. }
  90. static void ip4_frag_free(struct inet_frag_queue *q)
  91. {
  92. struct ipq *qp;
  93. qp = container_of(q, struct ipq, q);
  94. if (qp->peer)
  95. inet_putpeer(qp->peer);
  96. }
  97. static bool frag_expire_skip_icmp(u32 user)
  98. {
  99. return user == IP_DEFRAG_AF_PACKET ||
  100. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
  101. __IP_DEFRAG_CONNTRACK_IN_END) ||
  102. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  103. __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
  104. }
  105. /*
  106. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  107. */
  108. static void ip_expire(struct timer_list *t)
  109. {
  110. enum skb_drop_reason reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT;
  111. struct inet_frag_queue *frag = timer_container_of(frag, t, timer);
  112. const struct iphdr *iph;
  113. struct sk_buff *head = NULL;
  114. struct net *net;
  115. struct ipq *qp;
  116. int refs = 1;
  117. qp = container_of(frag, struct ipq, q);
  118. net = qp->q.fqdir->net;
  119. rcu_read_lock();
  120. spin_lock(&qp->q.lock);
  121. if (qp->q.flags & INET_FRAG_COMPLETE)
  122. goto out;
  123. qp->q.flags |= INET_FRAG_DROP;
  124. inet_frag_kill(&qp->q, &refs);
  125. /* Paired with WRITE_ONCE() in fqdir_pre_exit(). */
  126. if (READ_ONCE(qp->q.fqdir->dead)) {
  127. inet_frag_queue_flush(&qp->q, 0);
  128. goto out;
  129. }
  130. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  131. __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
  132. if (!(qp->q.flags & INET_FRAG_FIRST_IN))
  133. goto out;
  134. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  135. * pull the head out of the tree in order to be able to
  136. * deal with head->dev.
  137. */
  138. head = inet_frag_pull_head(&qp->q);
  139. if (!head)
  140. goto out;
  141. head->dev = dev_get_by_index_rcu(net, qp->iif);
  142. if (!head->dev)
  143. goto out;
  144. /* skb has no dst, perform route lookup again */
  145. iph = ip_hdr(head);
  146. reason = ip_route_input_noref(head, iph->daddr, iph->saddr,
  147. ip4h_dscp(iph), head->dev);
  148. if (reason)
  149. goto out;
  150. /* Only an end host needs to send an ICMP
  151. * "Fragment Reassembly Timeout" message, per RFC792.
  152. */
  153. reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT;
  154. if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
  155. (skb_rtable(head)->rt_type != RTN_LOCAL))
  156. goto out;
  157. spin_unlock(&qp->q.lock);
  158. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  159. goto out_rcu_unlock;
  160. out:
  161. spin_unlock(&qp->q.lock);
  162. out_rcu_unlock:
  163. rcu_read_unlock();
  164. kfree_skb_reason(head, reason);
  165. inet_frag_putn(&qp->q, refs);
  166. }
  167. /* Find the correct entry in the "incomplete datagrams" queue for
  168. * this IP datagram, and create new one, if nothing is found.
  169. */
  170. static struct ipq *ip_find(struct net *net, struct iphdr *iph,
  171. u32 user, int vif)
  172. {
  173. struct frag_v4_compare_key key = {
  174. .saddr = iph->saddr,
  175. .daddr = iph->daddr,
  176. .user = user,
  177. .vif = vif,
  178. .id = iph->id,
  179. .protocol = iph->protocol,
  180. };
  181. struct inet_frag_queue *q;
  182. q = inet_frag_find(net->ipv4.fqdir, &key);
  183. if (!q)
  184. return NULL;
  185. return container_of(q, struct ipq, q);
  186. }
  187. /* Is the fragment too far ahead to be part of ipq? */
  188. static int ip_frag_too_far(struct ipq *qp)
  189. {
  190. struct inet_peer *peer = qp->peer;
  191. unsigned int max = qp->q.fqdir->max_dist;
  192. unsigned int start, end;
  193. int rc;
  194. if (!peer || !max)
  195. return 0;
  196. start = qp->rid;
  197. end = atomic_inc_return(&peer->rid);
  198. qp->rid = end;
  199. rc = qp->q.fragments_tail && (end - start) > max;
  200. if (rc)
  201. __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS);
  202. return rc;
  203. }
  204. static int ip_frag_reinit(struct ipq *qp)
  205. {
  206. if (!mod_timer_pending(&qp->q.timer, jiffies + qp->q.fqdir->timeout))
  207. return -ETIMEDOUT;
  208. inet_frag_queue_flush(&qp->q, SKB_DROP_REASON_FRAG_TOO_FAR);
  209. qp->q.flags = 0;
  210. qp->q.len = 0;
  211. qp->q.meat = 0;
  212. qp->q.rb_fragments = RB_ROOT;
  213. qp->q.fragments_tail = NULL;
  214. qp->q.last_run_head = NULL;
  215. qp->iif = 0;
  216. qp->ecn = 0;
  217. return 0;
  218. }
  219. /* Add new segment to existing queue. */
  220. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb, int *refs)
  221. {
  222. struct net *net = qp->q.fqdir->net;
  223. int ihl, end, flags, offset;
  224. struct sk_buff *prev_tail;
  225. struct net_device *dev;
  226. unsigned int fragsize;
  227. int err = -ENOENT;
  228. SKB_DR(reason);
  229. u8 ecn;
  230. /* If reassembly is already done, @skb must be a duplicate frag. */
  231. if (qp->q.flags & INET_FRAG_COMPLETE) {
  232. SKB_DR_SET(reason, DUP_FRAG);
  233. goto err;
  234. }
  235. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  236. unlikely(ip_frag_too_far(qp)) &&
  237. unlikely(err = ip_frag_reinit(qp))) {
  238. inet_frag_kill(&qp->q, refs);
  239. goto err;
  240. }
  241. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  242. offset = ntohs(ip_hdr(skb)->frag_off);
  243. flags = offset & ~IP_OFFSET;
  244. offset &= IP_OFFSET;
  245. offset <<= 3; /* offset is in 8-byte chunks */
  246. ihl = ip_hdrlen(skb);
  247. /* Determine the position of this fragment. */
  248. end = offset + skb->len - skb_network_offset(skb) - ihl;
  249. err = -EINVAL;
  250. /* Is this the final fragment? */
  251. if ((flags & IP_MF) == 0) {
  252. /* If we already have some bits beyond end
  253. * or have different end, the segment is corrupted.
  254. */
  255. if (end < qp->q.len ||
  256. ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
  257. goto discard_qp;
  258. qp->q.flags |= INET_FRAG_LAST_IN;
  259. qp->q.len = end;
  260. } else {
  261. if (end&7) {
  262. end &= ~7;
  263. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  264. skb->ip_summed = CHECKSUM_NONE;
  265. }
  266. if (end > qp->q.len) {
  267. /* Some bits beyond end -> corruption. */
  268. if (qp->q.flags & INET_FRAG_LAST_IN)
  269. goto discard_qp;
  270. qp->q.len = end;
  271. }
  272. }
  273. if (end == offset)
  274. goto discard_qp;
  275. err = -ENOMEM;
  276. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  277. goto discard_qp;
  278. err = pskb_trim_rcsum(skb, end - offset);
  279. if (err)
  280. goto discard_qp;
  281. /* Note : skb->rbnode and skb->dev share the same location. */
  282. dev = skb->dev;
  283. /* Makes sure compiler wont do silly aliasing games */
  284. barrier();
  285. prev_tail = qp->q.fragments_tail;
  286. err = inet_frag_queue_insert(&qp->q, skb, offset, end);
  287. if (err)
  288. goto insert_error;
  289. if (dev)
  290. qp->iif = dev->ifindex;
  291. qp->q.stamp = skb->tstamp;
  292. qp->q.tstamp_type = skb->tstamp_type;
  293. qp->q.meat += skb->len;
  294. qp->ecn |= ecn;
  295. add_frag_mem_limit(qp->q.fqdir, skb->truesize);
  296. if (offset == 0)
  297. qp->q.flags |= INET_FRAG_FIRST_IN;
  298. fragsize = skb->len + ihl;
  299. if (fragsize > qp->q.max_size)
  300. qp->q.max_size = fragsize;
  301. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  302. fragsize > qp->max_df_size)
  303. qp->max_df_size = fragsize;
  304. if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  305. qp->q.meat == qp->q.len) {
  306. unsigned long orefdst = skb->_skb_refdst;
  307. skb->_skb_refdst = 0UL;
  308. err = ip_frag_reasm(qp, skb, prev_tail, dev, refs);
  309. skb->_skb_refdst = orefdst;
  310. if (err)
  311. inet_frag_kill(&qp->q, refs);
  312. return err;
  313. }
  314. skb_dst_drop(skb);
  315. skb_orphan(skb);
  316. return -EINPROGRESS;
  317. insert_error:
  318. if (err == IPFRAG_DUP) {
  319. SKB_DR_SET(reason, DUP_FRAG);
  320. err = -EINVAL;
  321. goto err;
  322. }
  323. err = -EINVAL;
  324. __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
  325. discard_qp:
  326. inet_frag_kill(&qp->q, refs);
  327. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  328. err:
  329. kfree_skb_reason(skb, reason);
  330. return err;
  331. }
  332. static bool ip_frag_coalesce_ok(const struct ipq *qp)
  333. {
  334. return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER;
  335. }
  336. /* Build a new IP datagram from all its fragments. */
  337. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
  338. struct sk_buff *prev_tail, struct net_device *dev,
  339. int *refs)
  340. {
  341. struct net *net = qp->q.fqdir->net;
  342. struct iphdr *iph;
  343. void *reasm_data;
  344. int len, err;
  345. u8 ecn;
  346. inet_frag_kill(&qp->q, refs);
  347. ecn = ip_frag_ecn_table[qp->ecn];
  348. if (unlikely(ecn == 0xff)) {
  349. err = -EINVAL;
  350. goto out_fail;
  351. }
  352. /* Make the one we just received the head. */
  353. reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail);
  354. if (!reasm_data)
  355. goto out_nomem;
  356. len = ip_hdrlen(skb) + qp->q.len;
  357. err = -E2BIG;
  358. if (len > 65535)
  359. goto out_oversize;
  360. inet_frag_reasm_finish(&qp->q, skb, reasm_data,
  361. ip_frag_coalesce_ok(qp));
  362. skb->dev = dev;
  363. IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  364. iph = ip_hdr(skb);
  365. iph->tot_len = htons(len);
  366. iph->tos |= ecn;
  367. /* When we set IP_DF on a refragmented skb we must also force a
  368. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  369. * original sender only sent fragments of size f (where f < s).
  370. *
  371. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  372. * frag seen to avoid sending tiny DF-fragments in case skb was built
  373. * from one very small df-fragment and one large non-df frag.
  374. */
  375. if (qp->max_df_size == qp->q.max_size) {
  376. IPCB(skb)->flags |= IPSKB_FRAG_PMTU;
  377. iph->frag_off = htons(IP_DF);
  378. } else {
  379. iph->frag_off = 0;
  380. }
  381. ip_send_check(iph);
  382. __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
  383. qp->q.rb_fragments = RB_ROOT;
  384. qp->q.fragments_tail = NULL;
  385. qp->q.last_run_head = NULL;
  386. return 0;
  387. out_nomem:
  388. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  389. err = -ENOMEM;
  390. goto out_fail;
  391. out_oversize:
  392. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
  393. out_fail:
  394. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  395. return err;
  396. }
  397. /* Process an incoming IP datagram fragment. */
  398. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  399. {
  400. struct net_device *dev;
  401. struct ipq *qp;
  402. int vif;
  403. __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
  404. /* Lookup (or create) queue header */
  405. rcu_read_lock();
  406. dev = skb->dev ? : skb_dst_dev_rcu(skb);
  407. vif = l3mdev_master_ifindex_rcu(dev);
  408. qp = ip_find(net, ip_hdr(skb), user, vif);
  409. if (qp) {
  410. int ret, refs = 0;
  411. spin_lock(&qp->q.lock);
  412. ret = ip_frag_queue(qp, skb, &refs);
  413. spin_unlock(&qp->q.lock);
  414. rcu_read_unlock();
  415. inet_frag_putn(&qp->q, refs);
  416. return ret;
  417. }
  418. rcu_read_unlock();
  419. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  420. kfree_skb(skb);
  421. return -ENOMEM;
  422. }
  423. EXPORT_SYMBOL(ip_defrag);
  424. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  425. {
  426. struct iphdr iph;
  427. int netoff;
  428. u32 len;
  429. if (skb->protocol != htons(ETH_P_IP))
  430. return skb;
  431. netoff = skb_network_offset(skb);
  432. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  433. return skb;
  434. if (iph.ihl < 5 || iph.version != 4)
  435. return skb;
  436. len = ntohs(iph.tot_len);
  437. if (skb->len < netoff + len || len < (iph.ihl * 4))
  438. return skb;
  439. if (ip_is_fragment(&iph)) {
  440. skb = skb_share_check(skb, GFP_ATOMIC);
  441. if (skb) {
  442. if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
  443. kfree_skb(skb);
  444. return NULL;
  445. }
  446. if (pskb_trim_rcsum(skb, netoff + len)) {
  447. kfree_skb(skb);
  448. return NULL;
  449. }
  450. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  451. if (ip_defrag(net, skb, user))
  452. return NULL;
  453. skb_clear_hash(skb);
  454. }
  455. }
  456. return skb;
  457. }
  458. EXPORT_SYMBOL(ip_check_defrag);
  459. #ifdef CONFIG_SYSCTL
  460. static int dist_min;
  461. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  462. {
  463. .procname = "ipfrag_high_thresh",
  464. .maxlen = sizeof(unsigned long),
  465. .mode = 0644,
  466. .proc_handler = proc_doulongvec_minmax,
  467. },
  468. {
  469. .procname = "ipfrag_low_thresh",
  470. .maxlen = sizeof(unsigned long),
  471. .mode = 0644,
  472. .proc_handler = proc_doulongvec_minmax,
  473. },
  474. {
  475. .procname = "ipfrag_time",
  476. .maxlen = sizeof(int),
  477. .mode = 0644,
  478. .proc_handler = proc_dointvec_jiffies,
  479. },
  480. {
  481. .procname = "ipfrag_max_dist",
  482. .maxlen = sizeof(int),
  483. .mode = 0644,
  484. .proc_handler = proc_dointvec_minmax,
  485. .extra1 = &dist_min,
  486. },
  487. };
  488. /* secret interval has been deprecated */
  489. static int ip4_frags_secret_interval_unused;
  490. static struct ctl_table ip4_frags_ctl_table[] = {
  491. {
  492. .procname = "ipfrag_secret_interval",
  493. .data = &ip4_frags_secret_interval_unused,
  494. .maxlen = sizeof(int),
  495. .mode = 0644,
  496. .proc_handler = proc_dointvec_jiffies,
  497. },
  498. };
  499. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  500. {
  501. struct ctl_table *table;
  502. struct ctl_table_header *hdr;
  503. table = ip4_frags_ns_ctl_table;
  504. if (!net_eq(net, &init_net)) {
  505. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  506. if (!table)
  507. goto err_alloc;
  508. }
  509. table[0].data = &net->ipv4.fqdir->high_thresh;
  510. table[0].extra1 = &net->ipv4.fqdir->low_thresh;
  511. table[1].data = &net->ipv4.fqdir->low_thresh;
  512. table[1].extra2 = &net->ipv4.fqdir->high_thresh;
  513. table[2].data = &net->ipv4.fqdir->timeout;
  514. table[3].data = &net->ipv4.fqdir->max_dist;
  515. hdr = register_net_sysctl_sz(net, "net/ipv4", table,
  516. ARRAY_SIZE(ip4_frags_ns_ctl_table));
  517. if (!hdr)
  518. goto err_reg;
  519. net->ipv4.frags_hdr = hdr;
  520. return 0;
  521. err_reg:
  522. if (!net_eq(net, &init_net))
  523. kfree(table);
  524. err_alloc:
  525. return -ENOMEM;
  526. }
  527. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  528. {
  529. const struct ctl_table *table;
  530. table = net->ipv4.frags_hdr->ctl_table_arg;
  531. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  532. kfree(table);
  533. }
  534. static void __init ip4_frags_ctl_register(void)
  535. {
  536. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  537. }
  538. #else
  539. static int ip4_frags_ns_ctl_register(struct net *net)
  540. {
  541. return 0;
  542. }
  543. static void ip4_frags_ns_ctl_unregister(struct net *net)
  544. {
  545. }
  546. static void __init ip4_frags_ctl_register(void)
  547. {
  548. }
  549. #endif
  550. static int __net_init ipv4_frags_init_net(struct net *net)
  551. {
  552. int res;
  553. res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net);
  554. if (res < 0)
  555. return res;
  556. /* Fragment cache limits.
  557. *
  558. * The fragment memory accounting code, (tries to) account for
  559. * the real memory usage, by measuring both the size of frag
  560. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  561. * and the SKB's truesize.
  562. *
  563. * A 64K fragment consumes 129736 bytes (44*2944)+200
  564. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  565. *
  566. * We will commit 4MB at one time. Should we cross that limit
  567. * we will prune down to 3MB, making room for approx 8 big 64K
  568. * fragments 8x128k.
  569. */
  570. net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024;
  571. net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024;
  572. /*
  573. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  574. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  575. * by TTL.
  576. */
  577. net->ipv4.fqdir->timeout = IP_FRAG_TIME;
  578. net->ipv4.fqdir->max_dist = 64;
  579. res = ip4_frags_ns_ctl_register(net);
  580. if (res < 0)
  581. fqdir_exit(net->ipv4.fqdir);
  582. return res;
  583. }
  584. static void __net_exit ipv4_frags_pre_exit_net(struct net *net)
  585. {
  586. fqdir_pre_exit(net->ipv4.fqdir);
  587. }
  588. static void __net_exit ipv4_frags_exit_net(struct net *net)
  589. {
  590. ip4_frags_ns_ctl_unregister(net);
  591. fqdir_exit(net->ipv4.fqdir);
  592. }
  593. static struct pernet_operations ip4_frags_ops = {
  594. .init = ipv4_frags_init_net,
  595. .pre_exit = ipv4_frags_pre_exit_net,
  596. .exit = ipv4_frags_exit_net,
  597. };
  598. static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
  599. {
  600. return jhash2(data,
  601. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  602. }
  603. static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
  604. {
  605. const struct inet_frag_queue *fq = data;
  606. return jhash2((const u32 *)&fq->key.v4,
  607. sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
  608. }
  609. static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  610. {
  611. const struct frag_v4_compare_key *key = arg->key;
  612. const struct inet_frag_queue *fq = ptr;
  613. return !!memcmp(&fq->key, key, sizeof(*key));
  614. }
  615. static const struct rhashtable_params ip4_rhash_params = {
  616. .head_offset = offsetof(struct inet_frag_queue, node),
  617. .key_offset = offsetof(struct inet_frag_queue, key),
  618. .key_len = sizeof(struct frag_v4_compare_key),
  619. .hashfn = ip4_key_hashfn,
  620. .obj_hashfn = ip4_obj_hashfn,
  621. .obj_cmpfn = ip4_obj_cmpfn,
  622. .automatic_shrinking = true,
  623. };
  624. void __init ipfrag_init(void)
  625. {
  626. ip4_frags.constructor = ip4_frag_init;
  627. ip4_frags.destructor = ip4_frag_free;
  628. ip4_frags.qsize = sizeof(struct ipq);
  629. ip4_frags.frag_expire = ip_expire;
  630. ip4_frags.frags_cache_name = ip_frag_cache_name;
  631. ip4_frags.rhash_params = ip4_rhash_params;
  632. if (inet_frags_init(&ip4_frags))
  633. panic("IP: failed to allocate ip4_frags cache\n");
  634. ip4_frags_ctl_register();
  635. register_pernet_subsys(&ip4_frags_ops);
  636. }