History

netfilter: xt_recent: add an entry reaper

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.

Tim Gardner committed 16 years ago

Commit 0079c5a1 parent 5be4a4f2 files · +31 −1Browse files

Unified · Split

2 files
  1. include/linux/netfilter/xt_recent.h +4 −0
  2. net/netfilter/xt_recent.c +27 −1
include/linux/netfilter/xt_recent.h+4−0
@@ -9,6 +9,7 @@ enum { · 8 unmodified lines
99 XT_RECENT_UPDATE = 1 << 2,
1010 XT_RECENT_REMOVE = 1 << 3,
1111 XT_RECENT_TTL = 1 << 4,
12+ XT_RECENT_REAP = 1 << 5,
1213
1314 XT_RECENT_SOURCE = 0,
1415 XT_RECENT_DEST = 1,
@@ -16,6 +17,9 @@ enum { · 1 unmodified line
1617 XT_RECENT_NAME_LEN = 200,
1718 };
1819
20+/* Only allowed with --rcheck and --update */
21+#define XT_RECENT_MODIFIERS (XT_RECENT_TTL|XT_RECENT_REAP)
22+
1923 struct xt_recent_mtinfo {
2024 __u32 seconds;
2125 __u32 hit_count;
net/netfilter/xt_recent.c+27−1
@@ -143,6 +143,25 @@ static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) · 142 unmodified lines
143143 t->entries--;
144144 }
145145
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+
146165 static struct recent_entry *
147166 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
148167 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
269288 break;
270289 }
271290 }
291+
292+ /* info->seconds must be non-zero */
293+ if (info->check_set & XT_RECENT_REAP)
294+ recent_entry_reap(t, time);
272295 }
273296
274297 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
301324 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
302325 return false;
303326 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)
305331 return false;
306332 if (info->hit_count > ip_pkt_list_tot) {
307333 pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t