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
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>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * version 2 as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/module.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/mutex.h>
37 #include <linux/netfilter.h>
38 #include <linux/netfilter/x_tables.h>
39 #include <linux/netfilter/xt_IDLETIMER.h>
40 #include <linux/kdev_t.h>
41 #include <linux/kobject.h>
42 #include <linux/skbuff.h>
43 #include <linux/workqueue.h>
44 #include <linux/sysfs.h>
45 #include <net/net_namespace.h>
47 struct idletimer_tg_attr {
48 struct attribute attr;
49 ssize_t (*show)(struct kobject *kobj,
50 struct attribute *attr, char *buf);
54 struct list_head entry;
55 struct timer_list timer;
56 struct work_struct work;
59 struct idletimer_tg_attr attr;
66 static LIST_HEAD(idletimer_tg_list);
67 static DEFINE_MUTEX(list_mutex);
69 static struct kobject *idletimer_tg_kobj;
71 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
73 char iface_msg[NLMSG_MAX_SIZE];
74 char state_msg[NLMSG_MAX_SIZE];
75 char *envp[] = { iface_msg, state_msg, NULL };
78 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
80 if (NLMSG_MAX_SIZE <= res) {
81 pr_err("message too long (%d)", res);
84 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
85 timer->active ? "active" : "inactive");
86 if (NLMSG_MAX_SIZE <= res) {
87 pr_err("message too long (%d)", res);
90 pr_debug("putting nlmsg: <%s> <%s>\n", iface_msg, state_msg);
91 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
98 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
100 struct idletimer_tg *entry;
104 list_for_each_entry(entry, &idletimer_tg_list, entry) {
105 if (!strcmp(label, entry->attr.attr.name))
112 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
115 struct idletimer_tg *timer;
116 unsigned long expires = 0;
117 unsigned long now = jiffies;
119 mutex_lock(&list_mutex);
121 timer = __idletimer_tg_find_by_label(attr->name);
123 expires = timer->timer.expires;
125 mutex_unlock(&list_mutex);
127 if (time_after(expires, now))
128 return sprintf(buf, "%u\n",
129 jiffies_to_msecs(expires - now) / 1000);
131 if (timer->send_nl_msg)
132 return sprintf(buf, "0 %d\n",
133 jiffies_to_msecs(now - expires) / 1000);
135 return sprintf(buf, "0\n");
138 static void idletimer_tg_work(struct work_struct *work)
140 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
143 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
145 if (timer->send_nl_msg)
146 notify_netlink_uevent(timer->attr.attr.name, timer);
149 static void idletimer_tg_expired(unsigned long data)
151 struct idletimer_tg *timer = (struct idletimer_tg *) data;
153 pr_debug("timer %s expired\n", timer->attr.attr.name);
155 timer->active = false;
156 schedule_work(&timer->work);
159 static int idletimer_tg_create(struct idletimer_tg_info *info)
163 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
169 sysfs_attr_init(&info->timer->attr.attr);
170 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
171 if (!info->timer->attr.attr.name) {
175 info->timer->attr.attr.mode = S_IRUGO;
176 info->timer->attr.show = idletimer_tg_show;
178 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
180 pr_debug("couldn't add file to sysfs");
184 list_add(&info->timer->entry, &idletimer_tg_list);
186 setup_timer(&info->timer->timer, idletimer_tg_expired,
187 (unsigned long) info->timer);
188 info->timer->refcnt = 1;
189 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
190 info->timer->active = true;
192 mod_timer(&info->timer->timer,
193 msecs_to_jiffies(info->timeout * 1000) + jiffies);
195 INIT_WORK(&info->timer->work, idletimer_tg_work);
200 kfree(info->timer->attr.attr.name);
208 * The actual xt_tables plugin.
210 static unsigned int idletimer_tg_target(struct sk_buff *skb,
211 const struct xt_action_param *par)
213 const struct idletimer_tg_info *info = par->targinfo;
214 unsigned long now = jiffies;
216 pr_debug("resetting timer %s, timeout period %u\n",
217 info->label, info->timeout);
219 BUG_ON(!info->timer);
221 info->timer->active = true;
223 if (time_before(info->timer->timer.expires, now)) {
224 schedule_work(&info->timer->work);
225 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
226 info->label, info->timer->timer.expires, now);
229 /* TODO: Avoid modifying timers on each packet */
230 mod_timer(&info->timer->timer,
231 msecs_to_jiffies(info->timeout * 1000) + now);
236 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
238 struct idletimer_tg_info *info = par->targinfo;
240 unsigned long now = jiffies;
242 pr_debug("checkentry targinfo %s\n", info->label);
244 if (info->timeout == 0) {
245 pr_debug("timeout value is zero\n");
249 if (info->label[0] == '\0' ||
251 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
252 pr_debug("label is empty or not nul-terminated\n");
256 mutex_lock(&list_mutex);
258 info->timer = __idletimer_tg_find_by_label(info->label);
260 info->timer->refcnt++;
261 info->timer->active = true;
263 if (time_before(info->timer->timer.expires, now)) {
264 schedule_work(&info->timer->work);
265 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
266 info->timer->timer.expires, now);
269 mod_timer(&info->timer->timer,
270 msecs_to_jiffies(info->timeout * 1000) + now);
272 pr_debug("increased refcnt of timer %s to %u\n",
273 info->label, info->timer->refcnt);
275 ret = idletimer_tg_create(info);
277 pr_debug("failed to create timer\n");
278 mutex_unlock(&list_mutex);
283 mutex_unlock(&list_mutex);
288 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
290 const struct idletimer_tg_info *info = par->targinfo;
292 pr_debug("destroy targinfo %s\n", info->label);
294 mutex_lock(&list_mutex);
296 if (--info->timer->refcnt == 0) {
297 pr_debug("deleting timer %s\n", info->label);
299 list_del(&info->timer->entry);
300 del_timer_sync(&info->timer->timer);
301 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
302 kfree(info->timer->attr.attr.name);
305 pr_debug("decreased refcnt of timer %s to %u\n",
306 info->label, info->timer->refcnt);
309 mutex_unlock(&list_mutex);
312 static struct xt_target idletimer_tg __read_mostly = {
315 .family = NFPROTO_UNSPEC,
316 .target = idletimer_tg_target,
317 .targetsize = sizeof(struct idletimer_tg_info),
318 .checkentry = idletimer_tg_checkentry,
319 .destroy = idletimer_tg_destroy,
323 static struct class *idletimer_tg_class;
325 static struct device *idletimer_tg_device;
327 static int __init idletimer_tg_init(void)
331 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
332 err = PTR_ERR(idletimer_tg_class);
333 if (IS_ERR(idletimer_tg_class)) {
334 pr_debug("couldn't register device class\n");
338 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
339 MKDEV(0, 0), NULL, "timers");
340 err = PTR_ERR(idletimer_tg_device);
341 if (IS_ERR(idletimer_tg_device)) {
342 pr_debug("couldn't register system device\n");
346 idletimer_tg_kobj = &idletimer_tg_device->kobj;
348 err = xt_register_target(&idletimer_tg);
350 pr_debug("couldn't register xt target\n");
356 device_destroy(idletimer_tg_class, MKDEV(0, 0));
358 class_destroy(idletimer_tg_class);
363 static void __exit idletimer_tg_exit(void)
365 xt_unregister_target(&idletimer_tg);
367 device_destroy(idletimer_tg_class, MKDEV(0, 0));
368 class_destroy(idletimer_tg_class);
371 module_init(idletimer_tg_init);
372 module_exit(idletimer_tg_exit);
374 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
375 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
376 MODULE_DESCRIPTION("Xtables: idle time monitor");
377 MODULE_LICENSE("GPL v2");
378 MODULE_ALIAS("ipt_IDLETIMER");
379 MODULE_ALIAS("ip6t_IDLETIMER");
380 MODULE_ALIAS("arpt_IDLETIMER");