1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/netfilter/xt_IDLETIMER.c
5 * Netfilter module to trigger a timer when packet matches.
6 * After timer expires a kevent will be sent.
8 * Copyright (C) 2004, 2010 Nokia Corporation
9 * Written by Timo Teras <ext-timo.teras@nokia.com>
11 * Converted to x_tables and reworked for upstream inclusion
12 * by Luciano Coelho <luciano.coelho@nokia.com>
14 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/timer.h>
21 #include <linux/alarmtimer.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/workqueue.h>
30 #include <linux/sysfs.h>
33 struct list_head entry
;
35 struct timer_list timer
;
36 struct work_struct work
;
39 struct device_attribute attr
;
45 static LIST_HEAD(idletimer_tg_list
);
46 static DEFINE_MUTEX(list_mutex
);
48 static struct kobject
*idletimer_tg_kobj
;
51 struct idletimer_tg
*__idletimer_tg_find_by_label(const char *label
)
53 struct idletimer_tg
*entry
;
55 list_for_each_entry(entry
, &idletimer_tg_list
, entry
) {
56 if (!strcmp(label
, entry
->attr
.attr
.name
))
63 static ssize_t
idletimer_tg_show(struct device
*dev
,
64 struct device_attribute
*attr
, char *buf
)
66 struct idletimer_tg
*timer
;
67 unsigned long expires
= 0;
68 struct timespec64 ktimespec
= {};
71 mutex_lock(&list_mutex
);
73 timer
= __idletimer_tg_find_by_label(attr
->attr
.name
);
75 if (timer
->timer_type
& XT_IDLETIMER_ALARM
) {
76 ktime_t expires_alarm
= alarm_expires_remaining(&timer
->alarm
);
77 ktimespec
= ktime_to_timespec64(expires_alarm
);
78 time_diff
= ktimespec
.tv_sec
;
80 expires
= timer
->timer
.expires
;
81 time_diff
= jiffies_to_msecs(expires
- jiffies
) / 1000;
85 mutex_unlock(&list_mutex
);
87 if (time_after(expires
, jiffies
) || ktimespec
.tv_sec
> 0)
88 return snprintf(buf
, PAGE_SIZE
, "%ld\n", time_diff
);
90 return snprintf(buf
, PAGE_SIZE
, "0\n");
93 static void idletimer_tg_work(struct work_struct
*work
)
95 struct idletimer_tg
*timer
= container_of(work
, struct idletimer_tg
,
98 sysfs_notify(idletimer_tg_kobj
, NULL
, timer
->attr
.attr
.name
);
101 static void idletimer_tg_expired(struct timer_list
*t
)
103 struct idletimer_tg
*timer
= from_timer(timer
, t
, timer
);
105 pr_debug("timer %s expired\n", timer
->attr
.attr
.name
);
107 schedule_work(&timer
->work
);
110 static enum alarmtimer_restart
idletimer_tg_alarmproc(struct alarm
*alarm
,
113 struct idletimer_tg
*timer
= alarm
->data
;
115 pr_debug("alarm %s expired\n", timer
->attr
.attr
.name
);
116 schedule_work(&timer
->work
);
117 return ALARMTIMER_NORESTART
;
120 static int idletimer_check_sysfs_name(const char *name
, unsigned int size
)
124 ret
= xt_check_proc_name(name
, size
);
128 if (!strcmp(name
, "power") ||
129 !strcmp(name
, "subsystem") ||
130 !strcmp(name
, "uevent"))
136 static int idletimer_tg_create(struct idletimer_tg_info
*info
)
140 info
->timer
= kmalloc(sizeof(*info
->timer
), GFP_KERNEL
);
146 ret
= idletimer_check_sysfs_name(info
->label
, sizeof(info
->label
));
150 sysfs_attr_init(&info
->timer
->attr
.attr
);
151 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
152 if (!info
->timer
->attr
.attr
.name
) {
156 info
->timer
->attr
.attr
.mode
= 0444;
157 info
->timer
->attr
.show
= idletimer_tg_show
;
159 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
161 pr_debug("couldn't add file to sysfs");
165 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
167 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
168 info
->timer
->refcnt
= 1;
170 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
172 mod_timer(&info
->timer
->timer
,
173 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
178 kfree(info
->timer
->attr
.attr
.name
);
185 static int idletimer_tg_create_v1(struct idletimer_tg_info_v1
*info
)
189 info
->timer
= kmalloc(sizeof(*info
->timer
), GFP_KERNEL
);
195 ret
= idletimer_check_sysfs_name(info
->label
, sizeof(info
->label
));
199 sysfs_attr_init(&info
->timer
->attr
.attr
);
200 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
201 if (!info
->timer
->attr
.attr
.name
) {
205 info
->timer
->attr
.attr
.mode
= 0444;
206 info
->timer
->attr
.show
= idletimer_tg_show
;
208 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
210 pr_debug("couldn't add file to sysfs");
214 /* notify userspace */
215 kobject_uevent(idletimer_tg_kobj
,KOBJ_ADD
);
217 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
218 pr_debug("timer type value is %u", info
->timer_type
);
219 info
->timer
->timer_type
= info
->timer_type
;
220 info
->timer
->refcnt
= 1;
222 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
224 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
226 alarm_init(&info
->timer
->alarm
, ALARM_BOOTTIME
,
227 idletimer_tg_alarmproc
);
228 info
->timer
->alarm
.data
= info
->timer
;
229 tout
= ktime_set(info
->timeout
, 0);
230 alarm_start_relative(&info
->timer
->alarm
, tout
);
232 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
233 mod_timer(&info
->timer
->timer
,
234 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
240 kfree(info
->timer
->attr
.attr
.name
);
248 * The actual xt_tables plugin.
250 static unsigned int idletimer_tg_target(struct sk_buff
*skb
,
251 const struct xt_action_param
*par
)
253 const struct idletimer_tg_info
*info
= par
->targinfo
;
255 pr_debug("resetting timer %s, timeout period %u\n",
256 info
->label
, info
->timeout
);
258 mod_timer(&info
->timer
->timer
,
259 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
265 * The actual xt_tables plugin.
267 static unsigned int idletimer_tg_target_v1(struct sk_buff
*skb
,
268 const struct xt_action_param
*par
)
270 const struct idletimer_tg_info_v1
*info
= par
->targinfo
;
272 pr_debug("resetting timer %s, timeout period %u\n",
273 info
->label
, info
->timeout
);
275 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
276 ktime_t tout
= ktime_set(info
->timeout
, 0);
277 alarm_start_relative(&info
->timer
->alarm
, tout
);
279 mod_timer(&info
->timer
->timer
,
280 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
286 static int idletimer_tg_helper(struct idletimer_tg_info
*info
)
288 if (info
->timeout
== 0) {
289 pr_debug("timeout value is zero\n");
292 if (info
->timeout
>= INT_MAX
/ 1000) {
293 pr_debug("timeout value is too big\n");
296 if (info
->label
[0] == '\0' ||
298 MAX_IDLETIMER_LABEL_SIZE
) == MAX_IDLETIMER_LABEL_SIZE
) {
299 pr_debug("label is empty or not nul-terminated\n");
306 static int idletimer_tg_checkentry(const struct xt_tgchk_param
*par
)
308 struct idletimer_tg_info
*info
= par
->targinfo
;
311 pr_debug("checkentry targinfo%s\n", info
->label
);
313 ret
= idletimer_tg_helper(info
);
316 pr_debug("checkentry helper return invalid\n");
319 mutex_lock(&list_mutex
);
321 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
323 info
->timer
->refcnt
++;
324 mod_timer(&info
->timer
->timer
,
325 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
327 pr_debug("increased refcnt of timer %s to %u\n",
328 info
->label
, info
->timer
->refcnt
);
330 ret
= idletimer_tg_create(info
);
332 pr_debug("failed to create timer\n");
333 mutex_unlock(&list_mutex
);
338 mutex_unlock(&list_mutex
);
342 static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param
*par
)
344 struct idletimer_tg_info_v1
*info
= par
->targinfo
;
347 pr_debug("checkentry targinfo%s\n", info
->label
);
349 if (info
->send_nl_msg
)
352 ret
= idletimer_tg_helper((struct idletimer_tg_info
*)info
);
355 pr_debug("checkentry helper return invalid\n");
359 if (info
->timer_type
> XT_IDLETIMER_ALARM
) {
360 pr_debug("invalid value for timer type\n");
364 mutex_lock(&list_mutex
);
366 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
368 if (info
->timer
->timer_type
!= info
->timer_type
) {
369 pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
370 mutex_unlock(&list_mutex
);
374 info
->timer
->refcnt
++;
375 if (info
->timer_type
& XT_IDLETIMER_ALARM
) {
376 /* calculate remaining expiry time */
377 ktime_t tout
= alarm_expires_remaining(&info
->timer
->alarm
);
378 struct timespec64 ktimespec
= ktime_to_timespec64(tout
);
380 if (ktimespec
.tv_sec
> 0) {
381 pr_debug("time_expiry_remaining %lld\n",
383 alarm_start_relative(&info
->timer
->alarm
, tout
);
386 mod_timer(&info
->timer
->timer
,
387 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
389 pr_debug("increased refcnt of timer %s to %u\n",
390 info
->label
, info
->timer
->refcnt
);
392 ret
= idletimer_tg_create_v1(info
);
394 pr_debug("failed to create timer\n");
395 mutex_unlock(&list_mutex
);
400 mutex_unlock(&list_mutex
);
404 static void idletimer_tg_destroy(const struct xt_tgdtor_param
*par
)
406 const struct idletimer_tg_info
*info
= par
->targinfo
;
408 pr_debug("destroy targinfo %s\n", info
->label
);
410 mutex_lock(&list_mutex
);
412 if (--info
->timer
->refcnt
== 0) {
413 pr_debug("deleting timer %s\n", info
->label
);
415 list_del(&info
->timer
->entry
);
416 del_timer_sync(&info
->timer
->timer
);
417 cancel_work_sync(&info
->timer
->work
);
418 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
419 kfree(info
->timer
->attr
.attr
.name
);
422 pr_debug("decreased refcnt of timer %s to %u\n",
423 info
->label
, info
->timer
->refcnt
);
426 mutex_unlock(&list_mutex
);
429 static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param
*par
)
431 const struct idletimer_tg_info_v1
*info
= par
->targinfo
;
433 pr_debug("destroy targinfo %s\n", info
->label
);
435 mutex_lock(&list_mutex
);
437 if (--info
->timer
->refcnt
== 0) {
438 pr_debug("deleting timer %s\n", info
->label
);
440 list_del(&info
->timer
->entry
);
441 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
442 alarm_cancel(&info
->timer
->alarm
);
444 del_timer_sync(&info
->timer
->timer
);
446 cancel_work_sync(&info
->timer
->work
);
447 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
448 kfree(info
->timer
->attr
.attr
.name
);
451 pr_debug("decreased refcnt of timer %s to %u\n",
452 info
->label
, info
->timer
->refcnt
);
455 mutex_unlock(&list_mutex
);
459 static struct xt_target idletimer_tg
[] __read_mostly
= {
462 .family
= NFPROTO_UNSPEC
,
463 .target
= idletimer_tg_target
,
464 .targetsize
= sizeof(struct idletimer_tg_info
),
465 .usersize
= offsetof(struct idletimer_tg_info
, timer
),
466 .checkentry
= idletimer_tg_checkentry
,
467 .destroy
= idletimer_tg_destroy
,
472 .family
= NFPROTO_UNSPEC
,
474 .target
= idletimer_tg_target_v1
,
475 .targetsize
= sizeof(struct idletimer_tg_info_v1
),
476 .usersize
= offsetof(struct idletimer_tg_info_v1
, timer
),
477 .checkentry
= idletimer_tg_checkentry_v1
,
478 .destroy
= idletimer_tg_destroy_v1
,
485 static struct class *idletimer_tg_class
;
487 static struct device
*idletimer_tg_device
;
489 static int __init
idletimer_tg_init(void)
493 idletimer_tg_class
= class_create(THIS_MODULE
, "xt_idletimer");
494 err
= PTR_ERR(idletimer_tg_class
);
495 if (IS_ERR(idletimer_tg_class
)) {
496 pr_debug("couldn't register device class\n");
500 idletimer_tg_device
= device_create(idletimer_tg_class
, NULL
,
501 MKDEV(0, 0), NULL
, "timers");
502 err
= PTR_ERR(idletimer_tg_device
);
503 if (IS_ERR(idletimer_tg_device
)) {
504 pr_debug("couldn't register system device\n");
508 idletimer_tg_kobj
= &idletimer_tg_device
->kobj
;
510 err
= xt_register_targets(idletimer_tg
, ARRAY_SIZE(idletimer_tg
));
513 pr_debug("couldn't register xt target\n");
519 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
521 class_destroy(idletimer_tg_class
);
526 static void __exit
idletimer_tg_exit(void)
528 xt_unregister_targets(idletimer_tg
, ARRAY_SIZE(idletimer_tg
));
530 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
531 class_destroy(idletimer_tg_class
);
534 module_init(idletimer_tg_init
);
535 module_exit(idletimer_tg_exit
);
537 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
538 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
539 MODULE_DESCRIPTION("Xtables: idle time monitor");
540 MODULE_LICENSE("GPL v2");
541 MODULE_ALIAS("ipt_IDLETIMER");
542 MODULE_ALIAS("ip6t_IDLETIMER");