This source file includes following definitions.
- is_leap
- localtime_1
- localtime_2
- localtime_3
- time_mt
- time_mt_check
- time_mt_init
- time_mt_exit
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
22 struct 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
32 extern struct timezone sys_tz;
33
34 static const u_int16_t days_since_year[] = {
35 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
36 };
37
38 static 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
46 enum {
47 DSE_FIRST = 2039,
48 SECONDS_PER_DAY = 86400,
49 };
50 static 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
67 static 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
80 static inline unsigned int localtime_1(struct xtm *r, time_t time)
81 {
82 unsigned int v, w;
83
84
85 v = time % SECONDS_PER_DAY;
86 r->second = v % 60;
87 w = v / 60;
88 r->minute = w % 60;
89 r->hour = w / 60;
90 return v;
91 }
92
93 static inline void localtime_2(struct xtm *r, time_t time)
94 {
95
96
97
98
99 r->dse = time / 86400;
100
101
102
103
104
105 r->weekday = (4 + r->dse - 1) % 7 + 1;
106 }
107
108 static void localtime_3(struct xtm *r, time_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
157 static bool
158 time_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 s64 stamp;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 stamp = get_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 if (stamp < info->date_start || stamp > info->date_stop)
199 return false;
200
201 packet_time = localtime_1(¤t_time, stamp);
202
203 if (info->daytime_start < info->daytime_stop) {
204 if (packet_time < info->daytime_start ||
205 packet_time > info->daytime_stop)
206 return false;
207 } else {
208 if (packet_time < info->daytime_start &&
209 packet_time > info->daytime_stop)
210 return false;
211
212
213
214
215
216
217
218
219
220 if ((info->flags & XT_TIME_CONTIGUOUS) &&
221 packet_time <= info->daytime_stop)
222 stamp -= SECONDS_PER_DAY;
223 }
224
225 localtime_2(¤t_time, stamp);
226
227 if (!(info->weekdays_match & (1 << current_time.weekday)))
228 return false;
229
230
231 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
232 localtime_3(¤t_time, stamp);
233 if (!(info->monthdays_match & (1 << current_time.monthday)))
234 return false;
235 }
236
237 return true;
238 }
239
240 static int time_mt_check(const struct xt_mtchk_param *par)
241 {
242 const struct xt_time_info *info = par->matchinfo;
243
244 if (info->daytime_start > XT_TIME_MAX_DAYTIME ||
245 info->daytime_stop > XT_TIME_MAX_DAYTIME) {
246 pr_info_ratelimited("invalid argument - start or stop time greater than 23:59:59\n");
247 return -EDOM;
248 }
249
250 if (info->flags & ~XT_TIME_ALL_FLAGS) {
251 pr_info_ratelimited("unknown flags 0x%x\n",
252 info->flags & ~XT_TIME_ALL_FLAGS);
253 return -EINVAL;
254 }
255
256 if ((info->flags & XT_TIME_CONTIGUOUS) &&
257 info->daytime_start < info->daytime_stop)
258 return -EINVAL;
259
260 return 0;
261 }
262
263 static struct xt_match xt_time_mt_reg __read_mostly = {
264 .name = "time",
265 .family = NFPROTO_UNSPEC,
266 .match = time_mt,
267 .checkentry = time_mt_check,
268 .matchsize = sizeof(struct xt_time_info),
269 .me = THIS_MODULE,
270 };
271
272 static int __init time_mt_init(void)
273 {
274 int minutes = sys_tz.tz_minuteswest;
275
276 if (minutes < 0)
277 pr_info("kernel timezone is +%02d%02d\n",
278 -minutes / 60, -minutes % 60);
279 else
280 pr_info("kernel timezone is -%02d%02d\n",
281 minutes / 60, minutes % 60);
282
283 return xt_register_match(&xt_time_mt_reg);
284 }
285
286 static void __exit time_mt_exit(void)
287 {
288 xt_unregister_match(&xt_time_mt_reg);
289 }
290
291 module_init(time_mt_init);
292 module_exit(time_mt_exit);
293 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
294 MODULE_DESCRIPTION("Xtables: time-based matching");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS("ipt_time");
297 MODULE_ALIAS("ip6t_time");