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 sysfs_emit(buf
, "%ld\n", time_diff
);
90 return sysfs_emit(buf
, "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 void idletimer_tg_alarmproc(struct alarm
*alarm
, ktime_t now
)
112 struct idletimer_tg
*timer
= alarm
->data
;
114 pr_debug("alarm %s expired\n", timer
->attr
.attr
.name
);
115 schedule_work(&timer
->work
);
118 static int idletimer_check_sysfs_name(const char *name
, unsigned int size
)
122 ret
= xt_check_proc_name(name
, size
);
126 if (!strcmp(name
, "power") ||
127 !strcmp(name
, "subsystem") ||
128 !strcmp(name
, "uevent"))
134 static int idletimer_tg_create(struct idletimer_tg_info
*info
)
138 info
->timer
= kzalloc(sizeof(*info
->timer
), GFP_KERNEL
);
144 ret
= idletimer_check_sysfs_name(info
->label
, sizeof(info
->label
));
148 sysfs_attr_init(&info
->timer
->attr
.attr
);
149 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
150 if (!info
->timer
->attr
.attr
.name
) {
154 info
->timer
->attr
.attr
.mode
= 0444;
155 info
->timer
->attr
.show
= idletimer_tg_show
;
157 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
159 pr_debug("couldn't add file to sysfs");
163 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
165 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
166 info
->timer
->refcnt
= 1;
168 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
170 mod_timer(&info
->timer
->timer
,
171 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
176 kfree(info
->timer
->attr
.attr
.name
);
183 static int idletimer_tg_create_v1(struct idletimer_tg_info_v1
*info
)
187 info
->timer
= kmalloc(sizeof(*info
->timer
), GFP_KERNEL
);
193 ret
= idletimer_check_sysfs_name(info
->label
, sizeof(info
->label
));
197 sysfs_attr_init(&info
->timer
->attr
.attr
);
198 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
199 if (!info
->timer
->attr
.attr
.name
) {
203 info
->timer
->attr
.attr
.mode
= 0444;
204 info
->timer
->attr
.show
= idletimer_tg_show
;
206 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
208 pr_debug("couldn't add file to sysfs");
212 /* notify userspace */
213 kobject_uevent(idletimer_tg_kobj
,KOBJ_ADD
);
215 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
216 pr_debug("timer type value is %u", info
->timer_type
);
217 info
->timer
->timer_type
= info
->timer_type
;
218 info
->timer
->refcnt
= 1;
220 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
222 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
224 alarm_init(&info
->timer
->alarm
, ALARM_BOOTTIME
,
225 idletimer_tg_alarmproc
);
226 info
->timer
->alarm
.data
= info
->timer
;
227 tout
= ktime_set(info
->timeout
, 0);
228 alarm_start_relative(&info
->timer
->alarm
, tout
);
230 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
231 mod_timer(&info
->timer
->timer
,
232 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
238 kfree(info
->timer
->attr
.attr
.name
);
246 * The actual xt_tables plugin.
248 static unsigned int idletimer_tg_target(struct sk_buff
*skb
,
249 const struct xt_action_param
*par
)
251 const struct idletimer_tg_info
*info
= par
->targinfo
;
253 pr_debug("resetting timer %s, timeout period %u\n",
254 info
->label
, info
->timeout
);
256 mod_timer(&info
->timer
->timer
,
257 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
263 * The actual xt_tables plugin.
265 static unsigned int idletimer_tg_target_v1(struct sk_buff
*skb
,
266 const struct xt_action_param
*par
)
268 const struct idletimer_tg_info_v1
*info
= par
->targinfo
;
270 pr_debug("resetting timer %s, timeout period %u\n",
271 info
->label
, info
->timeout
);
273 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
274 ktime_t tout
= ktime_set(info
->timeout
, 0);
275 alarm_start_relative(&info
->timer
->alarm
, tout
);
277 mod_timer(&info
->timer
->timer
,
278 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
284 static int idletimer_tg_helper(struct idletimer_tg_info
*info
)
286 if (info
->timeout
== 0) {
287 pr_debug("timeout value is zero\n");
290 if (info
->timeout
>= INT_MAX
/ 1000) {
291 pr_debug("timeout value is too big\n");
294 if (info
->label
[0] == '\0' ||
296 MAX_IDLETIMER_LABEL_SIZE
) == MAX_IDLETIMER_LABEL_SIZE
) {
297 pr_debug("label is empty or not nul-terminated\n");
304 static int idletimer_tg_checkentry(const struct xt_tgchk_param
*par
)
306 struct idletimer_tg_info
*info
= par
->targinfo
;
309 pr_debug("checkentry targinfo%s\n", info
->label
);
311 ret
= idletimer_tg_helper(info
);
314 pr_debug("checkentry helper return invalid\n");
317 mutex_lock(&list_mutex
);
319 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
321 info
->timer
->refcnt
++;
322 mod_timer(&info
->timer
->timer
,
323 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
325 pr_debug("increased refcnt of timer %s to %u\n",
326 info
->label
, info
->timer
->refcnt
);
328 ret
= idletimer_tg_create(info
);
330 pr_debug("failed to create timer\n");
331 mutex_unlock(&list_mutex
);
336 mutex_unlock(&list_mutex
);
340 static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param
*par
)
342 struct idletimer_tg_info_v1
*info
= par
->targinfo
;
345 pr_debug("checkentry targinfo%s\n", info
->label
);
347 if (info
->send_nl_msg
)
350 ret
= idletimer_tg_helper((struct idletimer_tg_info
*)info
);
353 pr_debug("checkentry helper return invalid\n");
357 if (info
->timer_type
> XT_IDLETIMER_ALARM
) {
358 pr_debug("invalid value for timer type\n");
362 mutex_lock(&list_mutex
);
364 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
366 if (info
->timer
->timer_type
!= info
->timer_type
) {
367 pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
368 mutex_unlock(&list_mutex
);
372 info
->timer
->refcnt
++;
373 if (info
->timer_type
& XT_IDLETIMER_ALARM
) {
374 /* calculate remaining expiry time */
375 ktime_t tout
= alarm_expires_remaining(&info
->timer
->alarm
);
376 struct timespec64 ktimespec
= ktime_to_timespec64(tout
);
378 if (ktimespec
.tv_sec
> 0) {
379 pr_debug("time_expiry_remaining %lld\n",
381 alarm_start_relative(&info
->timer
->alarm
, tout
);
384 mod_timer(&info
->timer
->timer
,
385 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
387 pr_debug("increased refcnt of timer %s to %u\n",
388 info
->label
, info
->timer
->refcnt
);
390 ret
= idletimer_tg_create_v1(info
);
392 pr_debug("failed to create timer\n");
393 mutex_unlock(&list_mutex
);
398 mutex_unlock(&list_mutex
);
402 static void idletimer_tg_destroy(const struct xt_tgdtor_param
*par
)
404 const struct idletimer_tg_info
*info
= par
->targinfo
;
406 pr_debug("destroy targinfo %s\n", info
->label
);
408 mutex_lock(&list_mutex
);
410 if (--info
->timer
->refcnt
> 0) {
411 pr_debug("decreased refcnt of timer %s to %u\n",
412 info
->label
, info
->timer
->refcnt
);
413 mutex_unlock(&list_mutex
);
417 pr_debug("deleting timer %s\n", info
->label
);
419 list_del(&info
->timer
->entry
);
420 mutex_unlock(&list_mutex
);
422 timer_shutdown_sync(&info
->timer
->timer
);
423 cancel_work_sync(&info
->timer
->work
);
424 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
425 kfree(info
->timer
->attr
.attr
.name
);
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("decreased refcnt of timer %s to %u\n",
439 info
->label
, info
->timer
->refcnt
);
440 mutex_unlock(&list_mutex
);
444 pr_debug("deleting timer %s\n", info
->label
);
446 list_del(&info
->timer
->entry
);
447 mutex_unlock(&list_mutex
);
449 if (info
->timer
->timer_type
& XT_IDLETIMER_ALARM
) {
450 alarm_cancel(&info
->timer
->alarm
);
452 timer_shutdown_sync(&info
->timer
->timer
);
454 cancel_work_sync(&info
->timer
->work
);
455 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
456 kfree(info
->timer
->attr
.attr
.name
);
461 static struct xt_target idletimer_tg
[] __read_mostly
= {
464 .family
= NFPROTO_IPV4
,
465 .target
= idletimer_tg_target
,
466 .targetsize
= sizeof(struct idletimer_tg_info
),
467 .usersize
= offsetof(struct idletimer_tg_info
, timer
),
468 .checkentry
= idletimer_tg_checkentry
,
469 .destroy
= idletimer_tg_destroy
,
474 .family
= NFPROTO_IPV4
,
476 .target
= idletimer_tg_target_v1
,
477 .targetsize
= sizeof(struct idletimer_tg_info_v1
),
478 .usersize
= offsetof(struct idletimer_tg_info_v1
, timer
),
479 .checkentry
= idletimer_tg_checkentry_v1
,
480 .destroy
= idletimer_tg_destroy_v1
,
483 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
486 .family
= NFPROTO_IPV6
,
487 .target
= idletimer_tg_target
,
488 .targetsize
= sizeof(struct idletimer_tg_info
),
489 .usersize
= offsetof(struct idletimer_tg_info
, timer
),
490 .checkentry
= idletimer_tg_checkentry
,
491 .destroy
= idletimer_tg_destroy
,
496 .family
= NFPROTO_IPV6
,
498 .target
= idletimer_tg_target_v1
,
499 .targetsize
= sizeof(struct idletimer_tg_info_v1
),
500 .usersize
= offsetof(struct idletimer_tg_info_v1
, timer
),
501 .checkentry
= idletimer_tg_checkentry_v1
,
502 .destroy
= idletimer_tg_destroy_v1
,
508 static struct class *idletimer_tg_class
;
510 static struct device
*idletimer_tg_device
;
512 static int __init
idletimer_tg_init(void)
516 idletimer_tg_class
= class_create("xt_idletimer");
517 err
= PTR_ERR(idletimer_tg_class
);
518 if (IS_ERR(idletimer_tg_class
)) {
519 pr_debug("couldn't register device class\n");
523 idletimer_tg_device
= device_create(idletimer_tg_class
, NULL
,
524 MKDEV(0, 0), NULL
, "timers");
525 err
= PTR_ERR(idletimer_tg_device
);
526 if (IS_ERR(idletimer_tg_device
)) {
527 pr_debug("couldn't register system device\n");
531 idletimer_tg_kobj
= &idletimer_tg_device
->kobj
;
533 err
= xt_register_targets(idletimer_tg
, ARRAY_SIZE(idletimer_tg
));
536 pr_debug("couldn't register xt target\n");
542 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
544 class_destroy(idletimer_tg_class
);
549 static void __exit
idletimer_tg_exit(void)
551 xt_unregister_targets(idletimer_tg
, ARRAY_SIZE(idletimer_tg
));
553 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
554 class_destroy(idletimer_tg_class
);
557 module_init(idletimer_tg_init
);
558 module_exit(idletimer_tg_exit
);
560 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
561 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
562 MODULE_DESCRIPTION("Xtables: idle time monitor");
563 MODULE_LICENSE("GPL v2");
564 MODULE_ALIAS("ipt_IDLETIMER");
565 MODULE_ALIAS("ip6t_IDLETIMER");