Skip to content

Commit 54eb3df

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: xt_time: add support to ignore day transition
Currently, if you want to do something like: "match Monday, starting 23:00, for two hours" You need two rules, one for Mon 23:00 to 0:00 and one for Tue 0:00-1:00. The rule: --weekdays Mo --timestart 23:00 --timestop 01:00 looks correct, but it will first match on monday from midnight to 1 a.m. and then again for another hour from 23:00 onwards. This permits userspace to explicitly ignore the day transition and match for a single, continuous time period instead. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 3e0304a commit 54eb3df

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎include/linux/netfilter/xt_time.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ enum {
1717
/* Match against local time (instead of UTC) */
1818
XT_TIME_LOCAL_TZ = 1 << 0,
1919

20+
/* treat timestart > timestop (e.g. 23:00-01:00) as single period */
21+
XT_TIME_CONTIGUOUS = 1 << 1,
22+
2023
/* Shortcuts */
2124
XT_TIME_ALL_MONTHDAYS = 0xFFFFFFFE,
2225
XT_TIME_ALL_WEEKDAYS = 0xFE,
2326
XT_TIME_MIN_DAYTIME = 0,
2427
XT_TIME_MAX_DAYTIME = 24 * 60 * 60 - 1,
2528
};
2629

30+
#define XT_TIME_ALL_FLAGS (XT_TIME_LOCAL_TZ|XT_TIME_CONTIGUOUS)
31+
2732
#endif /* _XT_TIME_H */

‎net/netfilter/xt_time.c‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static const u_int16_t days_since_leapyear[] = {
4242
*/
4343
enum {
4444
DSE_FIRST = 2039,
45+
SECONDS_PER_DAY = 86400,
4546
};
4647
static const u_int16_t days_since_epoch[] = {
4748
/* 2039 - 2030 */
@@ -78,7 +79,7 @@ static inline unsigned int localtime_1(struct xtm *r, time_t time)
7879
unsigned int v, w;
7980

8081
/* Each day has 86400s, so finding the hour/minute is actually easy. */
81-
v = time % 86400;
82+
v = time % SECONDS_PER_DAY;
8283
r->second = v % 60;
8384
w = v / 60;
8485
r->minute = w % 60;
@@ -199,6 +200,18 @@ time_mt(const struct sk_buff *skb, struct xt_action_param *par)
199200
if (packet_time < info->daytime_start &&
200201
packet_time > info->daytime_stop)
201202
return false;
203+
204+
/** if user asked to ignore 'next day', then e.g.
205+
* '1 PM Wed, August 1st' should be treated
206+
* like 'Tue 1 PM July 31st'.
207+
*
208+
* This also causes
209+
* 'Monday, "23:00 to 01:00", to match for 2 hours, starting
210+
* Monday 23:00 to Tuesday 01:00.
211+
*/
212+
if ((info->flags & XT_TIME_CONTIGUOUS) &&
213+
packet_time <= info->daytime_stop)
214+
stamp -= SECONDS_PER_DAY;
202215
}
203216

204217
localtime_2(&current_time, stamp);
@@ -227,6 +240,15 @@ static int time_mt_check(const struct xt_mtchk_param *par)
227240
return -EDOM;
228241
}
229242

243+
if (info->flags & ~XT_TIME_ALL_FLAGS) {
244+
pr_info("unknown flags 0x%x\n", info->flags & ~XT_TIME_ALL_FLAGS);
245+
return -EINVAL;
246+
}
247+
248+
if ((info->flags & XT_TIME_CONTIGUOUS) &&
249+
info->daytime_start < info->daytime_stop)
250+
return -EINVAL;
251+
230252
return 0;
231253
}
232254

0 commit comments

Comments
 (0)