ip_fragment.c 18 KB

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