2 * linux/net/netfilter/xt_IDLETIMER.c
4 * Netfilter module to trigger a timer when packet matches.
5 * After timer expires a kevent will be sent.
7 * Copyright (C) 2004, 2010 Nokia Corporation
8 * Written by Timo Teras <ext-timo.teras@nokia.com>
10 * Converted to x_tables and reworked for upstream inclusion
11 * by Luciano Coelho <luciano.coelho@nokia.com>
13 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/module.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/mutex.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter/x_tables.h>
38 #include <linux/netfilter/xt_IDLETIMER.h>
39 #include <linux/kdev_t.h>
40 #include <linux/kobject.h>
41 #include <linux/workqueue.h>
42 #include <linux/sysfs.h>
44 struct idletimer_tg_attr
{
45 struct attribute attr
;
46 ssize_t (*show
)(struct kobject
*kobj
,
47 struct attribute
*attr
, char *buf
);
51 struct list_head entry
;
52 struct timer_list timer
;
53 struct work_struct work
;
56 struct idletimer_tg_attr attr
;
61 static LIST_HEAD(idletimer_tg_list
);
62 static DEFINE_MUTEX(list_mutex
);
64 static struct kobject
*idletimer_tg_kobj
;
67 struct idletimer_tg
*__idletimer_tg_find_by_label(const char *label
)
69 struct idletimer_tg
*entry
;
73 list_for_each_entry(entry
, &idletimer_tg_list
, entry
) {
74 if (!strcmp(label
, entry
->attr
.attr
.name
))
81 static ssize_t
idletimer_tg_show(struct kobject
*kobj
, struct attribute
*attr
,
84 struct idletimer_tg
*timer
;
85 unsigned long expires
= 0;
87 mutex_lock(&list_mutex
);
89 timer
= __idletimer_tg_find_by_label(attr
->name
);
91 expires
= timer
->timer
.expires
;
93 mutex_unlock(&list_mutex
);
95 if (time_after(expires
, jiffies
))
96 return sprintf(buf
, "%u\n",
97 jiffies_to_msecs(expires
- jiffies
) / 1000);
99 return sprintf(buf
, "0\n");
102 static void idletimer_tg_work(struct work_struct
*work
)
104 struct idletimer_tg
*timer
= container_of(work
, struct idletimer_tg
,
107 sysfs_notify(idletimer_tg_kobj
, NULL
, timer
->attr
.attr
.name
);
110 static void idletimer_tg_expired(struct timer_list
*t
)
112 struct idletimer_tg
*timer
= from_timer(timer
, t
, timer
);
114 pr_debug("timer %s expired\n", timer
->attr
.attr
.name
);
116 schedule_work(&timer
->work
);
119 static int idletimer_tg_create(struct idletimer_tg_info
*info
)
123 info
->timer
= kmalloc(sizeof(*info
->timer
), GFP_KERNEL
);
129 sysfs_attr_init(&info
->timer
->attr
.attr
);
130 info
->timer
->attr
.attr
.name
= kstrdup(info
->label
, GFP_KERNEL
);
131 if (!info
->timer
->attr
.attr
.name
) {
135 info
->timer
->attr
.attr
.mode
= S_IRUGO
;
136 info
->timer
->attr
.show
= idletimer_tg_show
;
138 ret
= sysfs_create_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
140 pr_debug("couldn't add file to sysfs");
144 list_add(&info
->timer
->entry
, &idletimer_tg_list
);
146 timer_setup(&info
->timer
->timer
, idletimer_tg_expired
, 0);
147 info
->timer
->refcnt
= 1;
149 INIT_WORK(&info
->timer
->work
, idletimer_tg_work
);
151 mod_timer(&info
->timer
->timer
,
152 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
157 kfree(info
->timer
->attr
.attr
.name
);
165 * The actual xt_tables plugin.
167 static unsigned int idletimer_tg_target(struct sk_buff
*skb
,
168 const struct xt_action_param
*par
)
170 const struct idletimer_tg_info
*info
= par
->targinfo
;
172 pr_debug("resetting timer %s, timeout period %u\n",
173 info
->label
, info
->timeout
);
175 BUG_ON(!info
->timer
);
177 mod_timer(&info
->timer
->timer
,
178 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
183 static int idletimer_tg_checkentry(const struct xt_tgchk_param
*par
)
185 struct idletimer_tg_info
*info
= par
->targinfo
;
188 pr_debug("checkentry targinfo%s\n", info
->label
);
190 if (info
->timeout
== 0) {
191 pr_debug("timeout value is zero\n");
194 if (info
->timeout
>= INT_MAX
/ 1000) {
195 pr_debug("timeout value is too big\n");
198 if (info
->label
[0] == '\0' ||
200 MAX_IDLETIMER_LABEL_SIZE
) == MAX_IDLETIMER_LABEL_SIZE
) {
201 pr_debug("label is empty or not nul-terminated\n");
205 mutex_lock(&list_mutex
);
207 info
->timer
= __idletimer_tg_find_by_label(info
->label
);
209 info
->timer
->refcnt
++;
210 mod_timer(&info
->timer
->timer
,
211 msecs_to_jiffies(info
->timeout
* 1000) + jiffies
);
213 pr_debug("increased refcnt of timer %s to %u\n",
214 info
->label
, info
->timer
->refcnt
);
216 ret
= idletimer_tg_create(info
);
218 pr_debug("failed to create timer\n");
219 mutex_unlock(&list_mutex
);
224 mutex_unlock(&list_mutex
);
228 static void idletimer_tg_destroy(const struct xt_tgdtor_param
*par
)
230 const struct idletimer_tg_info
*info
= par
->targinfo
;
232 pr_debug("destroy targinfo %s\n", info
->label
);
234 mutex_lock(&list_mutex
);
236 if (--info
->timer
->refcnt
== 0) {
237 pr_debug("deleting timer %s\n", info
->label
);
239 list_del(&info
->timer
->entry
);
240 del_timer_sync(&info
->timer
->timer
);
241 cancel_work_sync(&info
->timer
->work
);
242 sysfs_remove_file(idletimer_tg_kobj
, &info
->timer
->attr
.attr
);
243 kfree(info
->timer
->attr
.attr
.name
);
246 pr_debug("decreased refcnt of timer %s to %u\n",
247 info
->label
, info
->timer
->refcnt
);
250 mutex_unlock(&list_mutex
);
253 static struct xt_target idletimer_tg __read_mostly
= {
255 .family
= NFPROTO_UNSPEC
,
256 .target
= idletimer_tg_target
,
257 .targetsize
= sizeof(struct idletimer_tg_info
),
258 .usersize
= offsetof(struct idletimer_tg_info
, timer
),
259 .checkentry
= idletimer_tg_checkentry
,
260 .destroy
= idletimer_tg_destroy
,
264 static struct class *idletimer_tg_class
;
266 static struct device
*idletimer_tg_device
;
268 static int __init
idletimer_tg_init(void)
272 idletimer_tg_class
= class_create(THIS_MODULE
, "xt_idletimer");
273 err
= PTR_ERR(idletimer_tg_class
);
274 if (IS_ERR(idletimer_tg_class
)) {
275 pr_debug("couldn't register device class\n");
279 idletimer_tg_device
= device_create(idletimer_tg_class
, NULL
,
280 MKDEV(0, 0), NULL
, "timers");
281 err
= PTR_ERR(idletimer_tg_device
);
282 if (IS_ERR(idletimer_tg_device
)) {
283 pr_debug("couldn't register system device\n");
287 idletimer_tg_kobj
= &idletimer_tg_device
->kobj
;
289 err
= xt_register_target(&idletimer_tg
);
291 pr_debug("couldn't register xt target\n");
297 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
299 class_destroy(idletimer_tg_class
);
304 static void __exit
idletimer_tg_exit(void)
306 xt_unregister_target(&idletimer_tg
);
308 device_destroy(idletimer_tg_class
, MKDEV(0, 0));
309 class_destroy(idletimer_tg_class
);
312 module_init(idletimer_tg_init
);
313 module_exit(idletimer_tg_exit
);
315 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
316 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
317 MODULE_DESCRIPTION("Xtables: idle time monitor");
318 MODULE_LICENSE("GPL v2");
319 MODULE_ALIAS("ipt_IDLETIMER");
320 MODULE_ALIAS("ip6t_IDLETIMER");