1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/net/netfilter/xt_IDLETIMER.c
4 *
5 * Netfilter module to trigger a timer when packet matches.
6 * After timer expires a kevent will be sent.
7 *
8 * Copyright (C) 2004, 2010 Nokia Corporation
9 *
10 * Written by Timo Teras <ext-timo.teras@nokia.com>
11 *
12 * Converted to x_tables and reworked for upstream inclusion
13 * by Luciano Coelho <luciano.coelho@nokia.com>
14 *
15 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
16 */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/module.h>
21 #include <linux/timer.h>
22 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter/xt_IDLETIMER.h>
27 #include <linux/kdev_t.h>
28 #include <linux/kobject.h>
29 #include <linux/skbuff.h>
30 #include <linux/workqueue.h>
31 #include <linux/sysfs.h>
32 #include <linux/rtc.h>
33 #include <linux/time.h>
34 #include <linux/math64.h>
35 #include <linux/suspend.h>
36 #include <linux/notifier.h>
37 #include <net/net_namespace.h>
38 #include <net/sock.h>
39 #include <net/inet_sock.h>
40
41 struct idletimer_tg {
42 struct list_head entry;
43 struct timer_list timer;
44 struct work_struct work;
45
46 struct kobject *kobj;
47 struct device_attribute attr;
48
49 struct timespec64 delayed_timer_trigger;
50 struct timespec64 last_modified_timer;
51 struct timespec64 last_suspend_time;
52 struct notifier_block pm_nb;
53
54 int timeout;
55 unsigned int refcnt;
56 bool work_pending;
57 bool send_nl_msg;
58 bool active;
59 uid_t uid;
60 bool suspend_time_valid;
61 };
62
63 static LIST_HEAD(idletimer_tg_list);
64 static DEFINE_MUTEX(list_mutex);
65 static DEFINE_SPINLOCK(timestamp_lock);
66
67 static struct kobject *idletimer_tg_kobj;
68
check_for_delayed_trigger(struct idletimer_tg * timer,struct timespec64 * ts)69 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
70 struct timespec64 *ts)
71 {
72 bool state;
73 struct timespec64 temp;
74 spin_lock_bh(×tamp_lock);
75 timer->work_pending = false;
76 if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
77 timer->delayed_timer_trigger.tv_sec != 0) {
78 state = false;
79 temp.tv_sec = timer->timeout;
80 temp.tv_nsec = 0;
81 if (timer->delayed_timer_trigger.tv_sec != 0) {
82 temp = timespec64_add(timer->delayed_timer_trigger,
83 temp);
84 ts->tv_sec = temp.tv_sec;
85 ts->tv_nsec = temp.tv_nsec;
86 timer->delayed_timer_trigger.tv_sec = 0;
87 timer->work_pending = true;
88 schedule_work(&timer->work);
89 } else {
90 temp = timespec64_add(timer->last_modified_timer, temp);
91 ts->tv_sec = temp.tv_sec;
92 ts->tv_nsec = temp.tv_nsec;
93 }
94 } else {
95 state = timer->active;
96 }
97 spin_unlock_bh(×tamp_lock);
98 return state;
99 }
100
notify_netlink_uevent(const char * iface,struct idletimer_tg * timer)101 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
102 {
103 char iface_msg[NLMSG_MAX_SIZE];
104 char state_msg[NLMSG_MAX_SIZE];
105 char timestamp_msg[NLMSG_MAX_SIZE];
106 char uid_msg[NLMSG_MAX_SIZE];
107 char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL };
108 int res;
109 struct timespec64 ts;
110 uint64_t time_ns;
111 bool state;
112
113 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
114 iface);
115 if (NLMSG_MAX_SIZE <= res) {
116 pr_err("message too long (%d)", res);
117 return;
118 }
119
120 ts = ktime_to_timespec64(ktime_get_boottime());
121 state = check_for_delayed_trigger(timer, &ts);
122 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
123 state ? "active" : "inactive");
124
125 if (NLMSG_MAX_SIZE <= res) {
126 pr_err("message too long (%d)", res);
127 return;
128 }
129
130 if (state) {
131 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid);
132 if (NLMSG_MAX_SIZE <= res)
133 pr_err("message too long (%d)", res);
134 } else {
135 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
136 if (NLMSG_MAX_SIZE <= res)
137 pr_err("message too long (%d)", res);
138 }
139
140 time_ns = timespec64_to_ns(&ts);
141 res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
142 if (NLMSG_MAX_SIZE <= res) {
143 timestamp_msg[0] = '\0';
144 pr_err("message too long (%d)", res);
145 }
146
147 pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg,
148 timestamp_msg, uid_msg);
149 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
150 return;
151
152
153 }
154
155 static
__idletimer_tg_find_by_label(const char * label)156 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
157 {
158 struct idletimer_tg *entry;
159
160 list_for_each_entry(entry, &idletimer_tg_list, entry) {
161 if (!strcmp(label, entry->attr.attr.name))
162 return entry;
163 }
164
165 return NULL;
166 }
167
idletimer_tg_show(struct device * dev,struct device_attribute * attr,char * buf)168 static ssize_t idletimer_tg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170 {
171 struct idletimer_tg *timer;
172 unsigned long expires = 0;
173 unsigned long now = jiffies;
174
175 mutex_lock(&list_mutex);
176
177 timer = __idletimer_tg_find_by_label(attr->attr.name);
178 if (timer)
179 expires = timer->timer.expires;
180
181 mutex_unlock(&list_mutex);
182
183 if (time_after(expires, now))
184 return sprintf(buf, "%u\n",
185 jiffies_to_msecs(expires - now) / 1000);
186
187 if (timer->send_nl_msg)
188 return sprintf(buf, "0 %d\n",
189 jiffies_to_msecs(now - expires) / 1000);
190 else
191 return sprintf(buf, "0\n");
192 }
193
idletimer_tg_work(struct work_struct * work)194 static void idletimer_tg_work(struct work_struct *work)
195 {
196 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
197 work);
198
199 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
200
201 if (timer->send_nl_msg)
202 notify_netlink_uevent(timer->attr.attr.name, timer);
203 }
204
idletimer_tg_expired(struct timer_list * t)205 static void idletimer_tg_expired(struct timer_list *t)
206 {
207 struct idletimer_tg *timer = from_timer(timer, t, timer);
208
209 pr_debug("timer %s expired\n", timer->attr.attr.name);
210 spin_lock_bh(×tamp_lock);
211 timer->active = false;
212 timer->work_pending = true;
213 schedule_work(&timer->work);
214 spin_unlock_bh(×tamp_lock);
215 }
216
idletimer_resume(struct notifier_block * notifier,unsigned long pm_event,void * unused)217 static int idletimer_resume(struct notifier_block *notifier,
218 unsigned long pm_event, void *unused)
219 {
220 struct timespec64 ts;
221 unsigned long time_diff, now = jiffies;
222 struct idletimer_tg *timer = container_of(notifier,
223 struct idletimer_tg, pm_nb);
224 if (!timer)
225 return NOTIFY_DONE;
226 switch (pm_event) {
227 case PM_SUSPEND_PREPARE:
228 timer->last_suspend_time =
229 ktime_to_timespec64(ktime_get_boottime());
230 timer->suspend_time_valid = true;
231 break;
232 case PM_POST_SUSPEND:
233 if (!timer->suspend_time_valid)
234 break;
235 timer->suspend_time_valid = false;
236
237 spin_lock_bh(×tamp_lock);
238 if (!timer->active) {
239 spin_unlock_bh(×tamp_lock);
240 break;
241 }
242 /* since jiffies are not updated when suspended now represents
243 * the time it would have suspended */
244 if (time_after(timer->timer.expires, now)) {
245 ts = ktime_to_timespec64(ktime_get_boottime());
246 ts = timespec64_sub(ts, timer->last_suspend_time);
247 time_diff = timespec64_to_jiffies(&ts);
248 if (timer->timer.expires > (time_diff + now)) {
249 mod_timer_pending(&timer->timer,
250 (timer->timer.expires - time_diff));
251 } else {
252 del_timer(&timer->timer);
253 timer->timer.expires = 0;
254 timer->active = false;
255 timer->work_pending = true;
256 schedule_work(&timer->work);
257 }
258 }
259 spin_unlock_bh(×tamp_lock);
260 break;
261 default:
262 break;
263 }
264 return NOTIFY_DONE;
265 }
266
idletimer_check_sysfs_name(const char * name,unsigned int size)267 static int idletimer_check_sysfs_name(const char *name, unsigned int size)
268 {
269 int ret;
270
271 ret = xt_check_proc_name(name, size);
272 if (ret < 0)
273 return ret;
274
275 if (!strcmp(name, "power") ||
276 !strcmp(name, "subsystem") ||
277 !strcmp(name, "uevent"))
278 return -EINVAL;
279
280 return 0;
281 }
282
idletimer_tg_create(struct idletimer_tg_info * info)283 static int idletimer_tg_create(struct idletimer_tg_info *info)
284 {
285 int ret;
286
287 info->timer = kzalloc(sizeof(*info->timer), GFP_KERNEL);
288 if (!info->timer) {
289 ret = -ENOMEM;
290 goto out;
291 }
292
293 ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
294 if (ret < 0)
295 goto out_free_timer;
296
297 sysfs_attr_init(&info->timer->attr.attr);
298 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
299 if (!info->timer->attr.attr.name) {
300 ret = -ENOMEM;
301 goto out_free_timer;
302 }
303 info->timer->attr.attr.mode = 0444;
304 info->timer->attr.show = idletimer_tg_show;
305
306 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
307 if (ret < 0) {
308 pr_debug("couldn't add file to sysfs");
309 goto out_free_attr;
310 }
311
312 list_add(&info->timer->entry, &idletimer_tg_list);
313
314 timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
315 info->timer->refcnt = 1;
316 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
317 info->timer->active = true;
318 info->timer->timeout = info->timeout;
319
320 info->timer->delayed_timer_trigger.tv_sec = 0;
321 info->timer->delayed_timer_trigger.tv_nsec = 0;
322 info->timer->work_pending = false;
323 info->timer->uid = 0;
324 info->timer->last_modified_timer =
325 ktime_to_timespec64(ktime_get_boottime());
326
327 info->timer->pm_nb.notifier_call = idletimer_resume;
328 ret = register_pm_notifier(&info->timer->pm_nb);
329 if (ret)
330 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
331 __func__, ret);
332
333 INIT_WORK(&info->timer->work, idletimer_tg_work);
334
335 mod_timer(&info->timer->timer,
336 msecs_to_jiffies(info->timeout * 1000) + jiffies);
337
338 return 0;
339
340 out_free_attr:
341 kfree(info->timer->attr.attr.name);
342 out_free_timer:
343 kfree(info->timer);
344 out:
345 return ret;
346 }
347
reset_timer(const struct idletimer_tg_info * info,struct sk_buff * skb)348 static void reset_timer(const struct idletimer_tg_info *info,
349 struct sk_buff *skb)
350 {
351 unsigned long now = jiffies;
352 struct idletimer_tg *timer = info->timer;
353 bool timer_prev;
354
355 spin_lock_bh(×tamp_lock);
356 timer_prev = timer->active;
357 timer->active = true;
358 /* timer_prev is used to guard overflow problem in time_before*/
359 if (!timer_prev || time_before(timer->timer.expires, now)) {
360 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
361 timer->timer.expires, now);
362
363 /* Stores the uid resposible for waking up the radio */
364 if (skb && (skb->sk)) {
365 timer->uid = from_kuid_munged(current_user_ns(),
366 sock_i_uid(skb_to_full_sk(skb)));
367 }
368
369 /* checks if there is a pending inactive notification*/
370 if (timer->work_pending)
371 timer->delayed_timer_trigger = timer->last_modified_timer;
372 else {
373 timer->work_pending = true;
374 schedule_work(&timer->work);
375 }
376 }
377
378 timer->last_modified_timer = ktime_to_timespec64(ktime_get_boottime());
379 mod_timer(&timer->timer,
380 msecs_to_jiffies(info->timeout * 1000) + now);
381 spin_unlock_bh(×tamp_lock);
382 }
383
384 /*
385 * The actual xt_tables plugin.
386 */
idletimer_tg_target(struct sk_buff * skb,const struct xt_action_param * par)387 static unsigned int idletimer_tg_target(struct sk_buff *skb,
388 const struct xt_action_param *par)
389 {
390 const struct idletimer_tg_info *info = par->targinfo;
391 unsigned long now = jiffies;
392
393 pr_debug("resetting timer %s, timeout period %u\n",
394 info->label, info->timeout);
395
396 BUG_ON(!info->timer);
397
398 info->timer->active = true;
399
400 if (time_before(info->timer->timer.expires, now)) {
401 schedule_work(&info->timer->work);
402 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
403 info->label, info->timer->timer.expires, now);
404 }
405
406 /* TODO: Avoid modifying timers on each packet */
407 reset_timer(info, skb);
408 return XT_CONTINUE;
409 }
410
idletimer_tg_checkentry(const struct xt_tgchk_param * par)411 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
412 {
413 struct idletimer_tg_info *info = par->targinfo;
414 int ret;
415
416 pr_debug("checkentry targinfo %s\n", info->label);
417
418 if (info->timeout == 0) {
419 pr_debug("timeout value is zero\n");
420 return -EINVAL;
421 }
422 if (info->timeout >= INT_MAX / 1000) {
423 pr_debug("timeout value is too big\n");
424 return -EINVAL;
425 }
426 if (info->label[0] == '\0' ||
427 strnlen(info->label,
428 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
429 pr_debug("label is empty or not nul-terminated\n");
430 return -EINVAL;
431 }
432
433 mutex_lock(&list_mutex);
434
435 info->timer = __idletimer_tg_find_by_label(info->label);
436 if (info->timer) {
437 info->timer->refcnt++;
438 reset_timer(info, NULL);
439 pr_debug("increased refcnt of timer %s to %u\n",
440 info->label, info->timer->refcnt);
441 } else {
442 ret = idletimer_tg_create(info);
443 if (ret < 0) {
444 pr_debug("failed to create timer\n");
445 mutex_unlock(&list_mutex);
446 return ret;
447 }
448 }
449
450 mutex_unlock(&list_mutex);
451
452 return 0;
453 }
454
idletimer_tg_destroy(const struct xt_tgdtor_param * par)455 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
456 {
457 const struct idletimer_tg_info *info = par->targinfo;
458
459 pr_debug("destroy targinfo %s\n", info->label);
460
461 mutex_lock(&list_mutex);
462
463 if (--info->timer->refcnt == 0) {
464 pr_debug("deleting timer %s\n", info->label);
465
466 list_del(&info->timer->entry);
467 del_timer_sync(&info->timer->timer);
468 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
469 unregister_pm_notifier(&info->timer->pm_nb);
470 cancel_work_sync(&info->timer->work);
471 kfree(info->timer->attr.attr.name);
472 kfree(info->timer);
473 } else {
474 pr_debug("decreased refcnt of timer %s to %u\n",
475 info->label, info->timer->refcnt);
476 }
477
478 mutex_unlock(&list_mutex);
479 }
480
481 static struct xt_target idletimer_tg __read_mostly = {
482 .name = "IDLETIMER",
483 .revision = 1,
484 .family = NFPROTO_UNSPEC,
485 .target = idletimer_tg_target,
486 .targetsize = sizeof(struct idletimer_tg_info),
487 .usersize = offsetof(struct idletimer_tg_info, timer),
488 .checkentry = idletimer_tg_checkentry,
489 .destroy = idletimer_tg_destroy,
490 .me = THIS_MODULE,
491 };
492
493 static struct class *idletimer_tg_class;
494
495 static struct device *idletimer_tg_device;
496
idletimer_tg_init(void)497 static int __init idletimer_tg_init(void)
498 {
499 int err;
500
501 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
502 err = PTR_ERR(idletimer_tg_class);
503 if (IS_ERR(idletimer_tg_class)) {
504 pr_debug("couldn't register device class\n");
505 goto out;
506 }
507
508 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
509 MKDEV(0, 0), NULL, "timers");
510 err = PTR_ERR(idletimer_tg_device);
511 if (IS_ERR(idletimer_tg_device)) {
512 pr_debug("couldn't register system device\n");
513 goto out_class;
514 }
515
516 idletimer_tg_kobj = &idletimer_tg_device->kobj;
517
518 err = xt_register_target(&idletimer_tg);
519 if (err < 0) {
520 pr_debug("couldn't register xt target\n");
521 goto out_dev;
522 }
523
524 return 0;
525 out_dev:
526 device_destroy(idletimer_tg_class, MKDEV(0, 0));
527 out_class:
528 class_destroy(idletimer_tg_class);
529 out:
530 return err;
531 }
532
idletimer_tg_exit(void)533 static void __exit idletimer_tg_exit(void)
534 {
535 xt_unregister_target(&idletimer_tg);
536
537 device_destroy(idletimer_tg_class, MKDEV(0, 0));
538 class_destroy(idletimer_tg_class);
539 }
540
541 module_init(idletimer_tg_init);
542 module_exit(idletimer_tg_exit);
543
544 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
545 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
546 MODULE_DESCRIPTION("Xtables: idle time monitor");
547 MODULE_LICENSE("GPL v2");
548 MODULE_ALIAS("ipt_IDLETIMER");
549 MODULE_ALIAS("ip6t_IDLETIMER");
550 MODULE_ALIAS("arpt_IDLETIMER");
551