One of the problems with the way xt_recent is implemented is that there is no efficient way to remove expired entries. Of course, one can write a rule '-m recent --remove', but you have to know beforehand which entry to delete. This commit adds reaper logic which checks the head of the LRU list when a rule is invoked that has a '--seconds' value and XT_RECENT_REAP set. If an entry ceases to accumulate time stamps, then it will eventually bubble to the top of the LRU list where it is then reaped.
Unified · Split
| @@ -9,6 +9,7 @@ enum { · 8 unmodified lines | ||
| 9 | 9 | XT_RECENT_UPDATE = 1 << 2, |
| 10 | 10 | XT_RECENT_REMOVE = 1 << 3, |
| 11 | 11 | XT_RECENT_TTL = 1 << 4, |
| 12 | + XT_RECENT_REAP = 1 << 5, | |
| 12 | 13 | |
| 13 | 14 | XT_RECENT_SOURCE = 0, |
| 14 | 15 | XT_RECENT_DEST = 1, |
| @@ -16,6 +17,9 @@ enum { · 1 unmodified line | ||
| 16 | 17 | XT_RECENT_NAME_LEN = 200, |
| 17 | 18 | }; |
| 18 | 19 | |
| 20 | +/* Only allowed with --rcheck and --update */ | |
| 21 | +#define XT_RECENT_MODIFIERS (XT_RECENT_TTL|XT_RECENT_REAP) | |
| 22 | + | |
| 19 | 23 | struct xt_recent_mtinfo { |
| 20 | 24 | __u32 seconds; |
| 21 | 25 | __u32 hit_count; |
| @@ -143,6 +143,25 @@ static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) · 142 unmodified lines | ||
| 143 | 143 | t->entries--; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | +/* | |
| 147 | + * Drop entries with timestamps older then 'time'. | |
| 148 | + */ | |
| 149 | +static void recent_entry_reap(struct recent_table *t, unsigned long time) | |
| 150 | +{ | |
| 151 | + struct recent_entry *e; | |
| 152 | + | |
| 153 | + /* | |
| 154 | + * The head of the LRU list is always the oldest entry. | |
| 155 | + */ | |
| 156 | + e = list_entry(t->lru_list.next, struct recent_entry, lru_list); | |
| 157 | + | |
| 158 | + /* | |
| 159 | + * The last time stamp is the most recent. | |
| 160 | + */ | |
| 161 | + if (time_after(time, e->stamps[e->index-1])) | |
| 162 | + recent_entry_remove(t, e); | |
| 163 | +} | |
| 164 | + | |
| 146 | 165 | static struct recent_entry * |
| 147 | 166 | recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr, |
| 148 | 167 | u_int16_t family, u_int8_t ttl) |
| @@ -269,6 +288,10 @@ recent_mt(const struct sk_buff *skb, const struct xt_match_param *par) · 120 unmodified lines | ||
| 269 | 288 | break; |
| 270 | 289 | } |
| 271 | 290 | } |
| 291 | + | |
| 292 | + /* info->seconds must be non-zero */ | |
| 293 | + if (info->check_set & XT_RECENT_REAP) | |
| 294 | + recent_entry_reap(t, time); | |
| 272 | 295 | } |
| 273 | 296 | |
| 274 | 297 | if (info->check_set & XT_RECENT_SET || |
| @@ -301,7 +324,10 @@ static bool recent_mt_check(const struct xt_mtchk_param *par) · 26 unmodified lines | ||
| 301 | 324 | XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1) |
| 302 | 325 | return false; |
| 303 | 326 | if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) && |
| 304 | − (info->seconds || info->hit_count)) | |
| 327 | + (info->seconds || info->hit_count || | |
| 328 | + (info->check_set & XT_RECENT_MODIFIERS))) | |
| 329 | + return false; | |
| 330 | + if ((info->check_set & XT_RECENT_REAP) && !info->seconds) | |
| 305 | 331 | return false; |
| 306 | 332 | if (info->hit_count > ip_pkt_list_tot) { |
| 307 | 333 | pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than " |