1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/ktime.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/types.h>
19#include <linux/netfilter/x_tables.h>
20#include <linux/netfilter/xt_time.h>
21
22struct xtm {
23 u_int8_t month;
24 u_int8_t monthday;
25 u_int8_t weekday;
26 u_int8_t hour;
27 u_int8_t minute;
28 u_int8_t second;
29 unsigned int dse;
30};
31
32extern struct timezone sys_tz;
33
34static const u_int16_t days_since_year[] = {
35 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
36};
37
38static const u_int16_t days_since_leapyear[] = {
39 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
40};
41
42
43
44
45
46enum {
47 DSE_FIRST = 2039,
48 SECONDS_PER_DAY = 86400,
49};
50static const u_int16_t days_since_epoch[] = {
51
52 25202, 24837, 24472, 24106, 23741, 23376, 23011, 22645, 22280, 21915,
53
54 21550, 21184, 20819, 20454, 20089, 19723, 19358, 18993, 18628, 18262,
55
56 17897, 17532, 17167, 16801, 16436, 16071, 15706, 15340, 14975, 14610,
57
58 14245, 13879, 13514, 13149, 12784, 12418, 12053, 11688, 11323, 10957,
59
60 10592, 10227, 9862, 9496, 9131, 8766, 8401, 8035, 7670, 7305,
61
62 6940, 6574, 6209, 5844, 5479, 5113, 4748, 4383, 4018, 3652,
63
64 3287, 2922, 2557, 2191, 1826, 1461, 1096, 730, 365, 0,
65};
66
67static inline bool is_leap(unsigned int y)
68{
69 return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
70}
71
72
73
74
75
76
77
78
79
80static inline unsigned int localtime_1(struct xtm *r, time64_t time)
81{
82 unsigned int v, w;
83
84
85 div_u64_rem(time, SECONDS_PER_DAY, &v);
86 r->second = v % 60;
87 w = v / 60;
88 r->minute = w % 60;
89 r->hour = w / 60;
90 return v;
91}
92
93static inline void localtime_2(struct xtm *r, time64_t time)
94{
95
96
97
98
99 r->dse = div_u64(time, SECONDS_PER_DAY);
100
101
102
103
104
105 r->weekday = (4 + r->dse - 1) % 7 + 1;
106}
107
108static void localtime_3(struct xtm *r, time64_t time)
109{
110 unsigned int year, i, w = r->dse;
111
112
113
114
115
116
117
118
119
120 for (i = 0, year = DSE_FIRST; days_since_epoch[i] > w;
121 ++i, --year)
122 ;
123
124 w -= days_since_epoch[i];
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 if (is_leap(year)) {
142
143 for (i = ARRAY_SIZE(days_since_leapyear) - 1;
144 i > 0 && days_since_leapyear[i] > w; --i)
145 ;
146 r->monthday = w - days_since_leapyear[i] + 1;
147 } else {
148 for (i = ARRAY_SIZE(days_since_year) - 1;
149 i > 0 && days_since_year[i] > w; --i)
150 ;
151 r->monthday = w - days_since_year[i] + 1;
152 }
153
154 r->month = i + 1;
155}
156
157static bool
158time_mt(const struct sk_buff *skb, struct xt_action_param *par)
159{
160 const struct xt_time_info *info = par->matchinfo;
161 unsigned int packet_time;
162 struct xtm current_time;
163 time64_t stamp;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 stamp = ktime_get_real_seconds();
184
185 if (info->flags & XT_TIME_LOCAL_TZ)
186
187 stamp -= 60 * sys_tz.tz_minuteswest;
188
189
190
191
192
193
194
195
196
197
198
199
200
201 if (stamp < info->date_start || stamp > info->date_stop)
202 return false;
203
204 packet_time = localtime_1(¤t_time, stamp);
205
206 if (info->daytime_start < info->daytime_stop) {
207 if (packet_time < info->daytime_start ||
208 packet_time > info->daytime_stop)
209 return false;
210 } else {
211 if (packet_time < info->daytime_start &&
212 packet_time > info->daytime_stop)
213 return false;
214
215
216
217
218
219
220
221
222
223 if ((info->flags & XT_TIME_CONTIGUOUS) &&
224 packet_time <= info->daytime_stop)
225 stamp -= SECONDS_PER_DAY;
226 }
227
228 localtime_2(¤t_time, stamp);
229
230 if (!(info->weekdays_match & (1 << current_time.weekday)))
231 return false;
232
233
234 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
235 localtime_3(¤t_time, stamp);
236 if (!(info->monthdays_match & (1 << current_time.monthday)))
237 return false;
238 }
239
240 return true;
241}
242
243static int time_mt_check(const struct xt_mtchk_param *par)
244{
245 const struct xt_time_info *info = par->matchinfo;
246
247 if (info->daytime_start > XT_TIME_MAX_DAYTIME ||
248 info->daytime_stop > XT_TIME_MAX_DAYTIME) {
249 pr_info_ratelimited("invalid argument - start or stop time greater than 23:59:59\n");
250 return -EDOM;
251 }
252
253 if (info->flags & ~XT_TIME_ALL_FLAGS) {
254 pr_info_ratelimited("unknown flags 0x%x\n",
255 info->flags & ~XT_TIME_ALL_FLAGS);
256 return -EINVAL;
257 }
258
259 if ((info->flags & XT_TIME_CONTIGUOUS) &&
260 info->daytime_start < info->daytime_stop)
261 return -EINVAL;
262
263 return 0;
264}
265
266static struct xt_match xt_time_mt_reg __read_mostly = {
267 .name = "time",
268 .family = NFPROTO_UNSPEC,
269 .match = time_mt,
270 .checkentry = time_mt_check,
271 .matchsize = sizeof(struct xt_time_info),
272 .me = THIS_MODULE,
273};
274
275static int __init time_mt_init(void)
276{
277 int minutes = sys_tz.tz_minuteswest;
278
279 if (minutes < 0)
280 pr_info("kernel timezone is +%02d%02d\n",
281 -minutes / 60, -minutes % 60);
282 else
283 pr_info("kernel timezone is -%02d%02d\n",
284 minutes / 60, minutes % 60);
285
286 return xt_register_match(&xt_time_mt_reg);
287}
288
289static void __exit time_mt_exit(void)
290{
291 xt_unregister_match(&xt_time_mt_reg);
292}
293
294module_init(time_mt_init);
295module_exit(time_mt_exit);
296MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
297MODULE_DESCRIPTION("Xtables: time-based matching");
298MODULE_LICENSE("GPL");
299MODULE_ALIAS("ipt_time");
300MODULE_ALIAS("ip6t_time");
301