370-netfilter-nf_flow_table-fix-offloaded-connection-tim.patch 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Date: Wed, 13 Jun 2018 12:33:39 +0200
  3. Subject: [PATCH] netfilter: nf_flow_table: fix offloaded connection timeout
  4. corner case
  5. The full teardown of offloaded flows is deferred to a gc work item,
  6. however processing of packets by netfilter needs to happen immediately
  7. after a teardown is requested, because the conntrack state needs to be
  8. fixed up.
  9. Since the IPS_OFFLOAD_BIT is still kept until the teardown is complete,
  10. the netfilter conntrack gc can accidentally bump the timeout of a
  11. connection where offload was just stopped, causing a conntrack entry
  12. leak.
  13. Fix this by moving the conntrack timeout bumping from conntrack core to
  14. the nf_flow_offload and add a check to prevent bogus timeout bumps.
  15. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  16. ---
  17. --- a/net/netfilter/nf_conntrack_core.c
  18. +++ b/net/netfilter/nf_conntrack_core.c
  19. @@ -1054,18 +1054,6 @@ static bool gc_worker_can_early_drop(con
  20. return false;
  21. }
  22. -#define DAY (86400 * HZ)
  23. -
  24. -/* Set an arbitrary timeout large enough not to ever expire, this save
  25. - * us a check for the IPS_OFFLOAD_BIT from the packet path via
  26. - * nf_ct_is_expired().
  27. - */
  28. -static void nf_ct_offload_timeout(struct nf_conn *ct)
  29. -{
  30. - if (nf_ct_expires(ct) < DAY / 2)
  31. - ct->timeout = nfct_time_stamp + DAY;
  32. -}
  33. -
  34. static void gc_worker(struct work_struct *work)
  35. {
  36. unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
  37. @@ -1102,10 +1090,8 @@ static void gc_worker(struct work_struct
  38. tmp = nf_ct_tuplehash_to_ctrack(h);
  39. scanned++;
  40. - if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
  41. - nf_ct_offload_timeout(tmp);
  42. + if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
  43. continue;
  44. - }
  45. if (nf_ct_is_expired(tmp)) {
  46. nf_ct_gc_expired(tmp);
  47. --- a/net/netfilter/nf_flow_table_core.c
  48. +++ b/net/netfilter/nf_flow_table_core.c
  49. @@ -185,8 +185,27 @@ static const struct rhashtable_params nf
  50. .automatic_shrinking = true,
  51. };
  52. +#define DAY (86400 * HZ)
  53. +
  54. +/* Set an arbitrary timeout large enough not to ever expire, this save
  55. + * us a check for the IPS_OFFLOAD_BIT from the packet path via
  56. + * nf_ct_is_expired().
  57. + */
  58. +static void nf_ct_offload_timeout(struct flow_offload *flow)
  59. +{
  60. + struct flow_offload_entry *entry;
  61. + struct nf_conn *ct;
  62. +
  63. + entry = container_of(flow, struct flow_offload_entry, flow);
  64. + ct = entry->ct;
  65. +
  66. + if (nf_ct_expires(ct) < DAY / 2)
  67. + ct->timeout = nfct_time_stamp + DAY;
  68. +}
  69. +
  70. int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
  71. {
  72. + nf_ct_offload_timeout(flow);
  73. flow->timeout = (u32)jiffies;
  74. rhashtable_insert_fast(&flow_table->rhashtable,
  75. @@ -307,6 +326,8 @@ static int nf_flow_offload_gc_step(struc
  76. rhashtable_walk_start(&hti);
  77. while ((tuplehash = rhashtable_walk_next(&hti))) {
  78. + bool teardown;
  79. +
  80. if (IS_ERR(tuplehash)) {
  81. err = PTR_ERR(tuplehash);
  82. if (err != -EAGAIN)
  83. @@ -319,9 +340,13 @@ static int nf_flow_offload_gc_step(struc
  84. flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
  85. - if (nf_flow_has_expired(flow) ||
  86. - (flow->flags & (FLOW_OFFLOAD_DYING |
  87. - FLOW_OFFLOAD_TEARDOWN)))
  88. + teardown = flow->flags & (FLOW_OFFLOAD_DYING |
  89. + FLOW_OFFLOAD_TEARDOWN);
  90. +
  91. + if (!teardown)
  92. + nf_ct_offload_timeout(flow);
  93. +
  94. + if (nf_flow_has_expired(flow) || teardown)
  95. flow_offload_del(flow_table, flow);
  96. }
  97. out: