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/list.h>
22 #include <linux/mutex.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter/xt_IDLETIMER.h>
26 #include <linux/kdev_t.h>
27 #include <linux/kobject.h>
28 #include <linux/workqueue.h>
29 #include <linux/sysfs.h>
32 struct list_head entry
;
33 struct timer_list timer
;
34 struct work_struct work
;
37 struct device_attribute attr
;
42 static LIST_HEAD(idletimer_tg_list
);
43 static DEFINE_MUTEX(list_mutex
);
45 static struct kobject
*idletimer_tg_kobj
;
48 struct idletimer_tg
*__idletimer_tg_find_by_label(const char *label
)
50 struct idletimer_tg
*entry
;
52 list_for_each_entry(entry
, &idletimer_tg_list
, entry
) {
53 if (!strcmp(label
, entry
->attr
.attr
.name
))
60 static ssize_t
idletimer_tg_show(struct device
*dev
,
61 struct device_attribute
*attr
, char *buf
)
63 struct idletimer_tg
*timer
;
64 unsigned long expires
= 0;
66 mutex_lock(&list_mutex
);
68 timer
= __idletimer_tg_find_by_label(attr
->attr
.name
);
70 expires
= timer
->timer
.expires
;
72 mutex_unlock(&list_mutex
);
74 if (time_after(expires
, jiffies
))
75 return sprintf(buf
, "%u\n",
76 jiffies_to_msecs(expires
- jiffies
) / 1000);
78 return sprintf(buf
, "0\n");
81 static void idletimer_tg_work(struct work_struct
*work
)
83 struct idletimer_tg
*timer
= container_of(work
, struct idletimer_tg
,
86 sysfs_notify(idletimer_tg_kobj
, NULL
, timer
->attr
.attr
.name
);
89 static void idletimer_tg_expired(struct timer_list
*t
)
91 struct idletimer_tg
*timer
= from_timer(timer
, t
, timer
);
93 pr_debug("timer %s expired\n", timer
->attr
.attr
.name
);
95 schedule_work(&timer
->work
);
98 static int idletimer_check_sysfs_name(const char *name
, unsigned int size
)
102 ret
= xt_check_proc_name(name
, size
);
106 if (!strcmp(name
, "power") ||
107 !strcmp(name
, "subsystem") ||
108 !strcmp(name
, "uevent"))
114 static int idletimer_tg_create(struct idletimer_tg_info
*info
)
118 info
->timer
= kmalloc(sizeof(*info
->timer
), GFP_KERNEL
);
124 ret
= idletimer_check_sysfs_name(info
->label
, sizeof(info
->label
));
128 sysfs_attr_init(&info
->timer
->attr
.attr
);
129 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
130 if (!info
->timer
->attr
.attr
.name
) {
134 info
->timer
->attr
.attr
.mode
= 0444;
135 info
->timer
->attr
.show
= idletimer_tg_show
;
137 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
139 pr_debug("couldn't add file to sysfs");
143 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
145 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
146 info
->timer
->refcnt
= 1;
148 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
150 mod_timer(&info
->timer
->timer
,
151 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
156 kfree(info
->timer
->attr
.attr
.name
);
164 * The actual xt_tables plugin.
166 static unsigned int idletimer_tg_target(struct sk_buff
*skb
,
167 const struct xt_action_param
*par
)
169 const struct idletimer_tg_info
*info
= par
->targinfo
;
171 pr_debug("resetting timer %s, timeout period %u\n",
172 info
->label
, info
->timeout
);
174 mod_timer(&info
->timer
->timer
,
175 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
180 static int idletimer_tg_checkentry(const struct xt_tgchk_param
*par
)
182 struct idletimer_tg_info
*info
= par
->targinfo
;
185 pr_debug("checkentry targinfo%s\n", info
->label
);
187 if (info
->timeout
== 0) {
188 pr_debug("timeout value is zero\n");
191 if (info
->timeout
>= INT_MAX
/ 1000) {
192 pr_debug("timeout value is too big\n");
195 if (info
->label
[0] == '\0' ||
197 MAX_IDLETIMER_LABEL_SIZE
) == MAX_IDLETIMER_LABEL_SIZE
) {
198 pr_debug("label is empty or not nul-terminated\n");
202 mutex_lock(&list_mutex
);
204 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
206 info
->timer
->refcnt
++;
207 mod_timer(&info
->timer
->timer
,
208 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
210 pr_debug("increased refcnt of timer %s to %u\n",
211 info
->label
, info
->timer
->refcnt
);
213 ret
= idletimer_tg_create(info
);
215 pr_debug("failed to create timer\n");
216 mutex_unlock(&list_mutex
);
221 mutex_unlock(&list_mutex
);
225 static void idletimer_tg_destroy(const struct xt_tgdtor_param
*par
)
227 const struct idletimer_tg_info
*info
= par
->targinfo
;
229 pr_debug("destroy targinfo %s\n", info
->label
);
231 mutex_lock(&list_mutex
);
233 if (--info
->timer
->refcnt
== 0) {
234 pr_debug("deleting timer %s\n", info
->label
);
236 list_del(&info
->timer
->entry
);
237 del_timer_sync(&info
->timer
->timer
);
238 cancel_work_sync(&info
->timer
->work
);
239 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
240 kfree(info
->timer
->attr
.attr
.name
);
243 pr_debug("decreased refcnt of timer %s to %u\n",
244 info
->label
, info
->timer
->refcnt
);
247 mutex_unlock(&list_mutex
);
250 static struct xt_target idletimer_tg __read_mostly
= {
252 .family
= NFPROTO_UNSPEC
,
253 .target
= idletimer_tg_target
,
254 .targetsize
= sizeof(struct idletimer_tg_info
),
255 .usersize
= offsetof(struct idletimer_tg_info
, timer
),
256 .checkentry
= idletimer_tg_checkentry
,
257 .destroy
= idletimer_tg_destroy
,
261 static struct class *idletimer_tg_class
;
263 static struct device
*idletimer_tg_device
;
265 static int __init
idletimer_tg_init(void)
269 idletimer_tg_class
= class_create(THIS_MODULE
, "xt_idletimer");
270 err
= PTR_ERR(idletimer_tg_class
);
271 if (IS_ERR(idletimer_tg_class
)) {
272 pr_debug("couldn't register device class\n");
276 idletimer_tg_device
= device_create(idletimer_tg_class
, NULL
,
277 MKDEV(0, 0), NULL
, "timers");
278 err
= PTR_ERR(idletimer_tg_device
);
279 if (IS_ERR(idletimer_tg_device
)) {
280 pr_debug("couldn't register system device\n");
284 idletimer_tg_kobj
= &idletimer_tg_device
->kobj
;
286 err
= xt_register_target(&idletimer_tg
);
288 pr_debug("couldn't register xt target\n");
294 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
296 class_destroy(idletimer_tg_class
);
301 static void __exit
idletimer_tg_exit(void)
303 xt_unregister_target(&idletimer_tg
);
305 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
306 class_destroy(idletimer_tg_class
);
309 module_init(idletimer_tg_init
);
310 module_exit(idletimer_tg_exit
);
312 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
313 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
314 MODULE_DESCRIPTION("Xtables: idle time monitor");
315 MODULE_LICENSE("GPL v2");
316 MODULE_ALIAS("ipt_IDLETIMER");
317 MODULE_ALIAS("ip6t_IDLETIMER");