1 /*
2 * linux/net/netfilter/xt_IDLETIMER.c
3 *
4 * Netfilter module to trigger a timer when packet matches.
5 * After timer expires a kevent will be sent.
6 *
7 * Copyright (C) 2004, 2010 Nokia Corporation
8 *
9 * Written by Timo Teras <ext-timo.teras@nokia.com>
10 *
11 * Converted to x_tables and reworked for upstream inclusion
12 * by Luciano Coelho <luciano.coelho@nokia.com>
13 *
14 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
15 *
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.
19 *
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.
24 *
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
28 * 02110-1301 USA
29 */
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);
51 };
53 struct idletimer_tg {
54 struct list_head entry;
55 struct timer_list timer;
56 struct work_struct work;
58 struct kobject *kobj;
59 struct idletimer_tg_attr attr;
61 unsigned int refcnt;
62 bool send_nl_msg;
63 bool active;
64 };
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 *label, struct idletimer_tg *timer)
72 {
73 char label_msg[NLMSG_MAX_SIZE];
74 char state_msg[NLMSG_MAX_SIZE];
75 char *envp[] = { label_msg, state_msg, NULL };
76 int res;
78 res = snprintf(label_msg, NLMSG_MAX_SIZE, "LABEL=%s",
79 label);
80 if (NLMSG_MAX_SIZE <= res) {
81 pr_err("message too long (%d)", res);
82 return;
83 }
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);
88 return;
89 }
90 pr_debug("putting nlmsg: <%s> <%s>\n", label_msg, state_msg);
91 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
92 return;
95 }
97 static
98 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
99 {
100 struct idletimer_tg *entry;
102 BUG_ON(!label);
104 list_for_each_entry(entry, &idletimer_tg_list, entry) {
105 if (!strcmp(label, entry->attr.attr.name))
106 return entry;
107 }
109 return NULL;
110 }
112 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
113 char *buf)
114 {
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);
122 if (timer)
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);
134 else
135 return sprintf(buf, "0\n");
136 }
138 static void idletimer_tg_work(struct work_struct *work)
139 {
140 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
141 work);
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);
147 }
149 static void idletimer_tg_expired(unsigned long data)
150 {
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);
157 }
159 static int idletimer_tg_create(struct idletimer_tg_info *info)
160 {
161 int ret;
163 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
164 if (!info->timer) {
165 ret = -ENOMEM;
166 goto out;
167 }
169 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
170 if (!info->timer->attr.attr.name) {
171 ret = -ENOMEM;
172 goto out_free_timer;
173 }
174 info->timer->attr.attr.mode = S_IRUGO;
175 info->timer->attr.show = idletimer_tg_show;
177 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
178 if (ret < 0) {
179 pr_debug("couldn't add file to sysfs");
180 goto out_free_attr;
181 }
183 list_add(&info->timer->entry, &idletimer_tg_list);
185 setup_timer(&info->timer->timer, idletimer_tg_expired,
186 (unsigned long) info->timer);
187 info->timer->refcnt = 1;
188 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
189 info->timer->active = true;
191 mod_timer(&info->timer->timer,
192 msecs_to_jiffies(info->timeout * 1000) + jiffies);
194 INIT_WORK(&info->timer->work, idletimer_tg_work);
196 return 0;
198 out_free_attr:
199 kfree(info->timer->attr.attr.name);
200 out_free_timer:
201 kfree(info->timer);
202 out:
203 return ret;
204 }
206 /*
207 * The actual xt_tables plugin.
208 */
209 static unsigned int idletimer_tg_target(struct sk_buff *skb,
210 const struct xt_action_param *par)
211 {
212 const struct idletimer_tg_info *info = par->targinfo;
213 unsigned long now = jiffies;
215 pr_debug("resetting timer %s, timeout period %u\n",
216 info->label, info->timeout);
218 BUG_ON(!info->timer);
220 info->timer->active = true;
222 if (time_before(info->timer->timer.expires, now)) {
223 schedule_work(&info->timer->work);
224 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
225 info->label, info->timer->timer.expires, now);
226 }
228 /* TODO: Avoid modifying timers on each packet */
229 mod_timer(&info->timer->timer,
230 msecs_to_jiffies(info->timeout * 1000) + now);
232 return XT_CONTINUE;
233 }
235 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
236 {
237 struct idletimer_tg_info *info = par->targinfo;
238 int ret;
239 unsigned long now = jiffies;
241 pr_debug("checkentry targinfo %s\n", info->label);
243 if (info->timeout == 0) {
244 pr_debug("timeout value is zero\n");
245 return -EINVAL;
246 }
248 if (info->label[0] == '\0' ||
249 strnlen(info->label,
250 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
251 pr_debug("label is empty or not nul-terminated\n");
252 return -EINVAL;
253 }
255 mutex_lock(&list_mutex);
257 info->timer = __idletimer_tg_find_by_label(info->label);
258 if (info->timer) {
259 info->timer->refcnt++;
260 info->timer->active = true;
262 if (time_before(info->timer->timer.expires, now)) {
263 schedule_work(&info->timer->work);
264 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
265 info->timer->timer.expires, now);
266 }
268 mod_timer(&info->timer->timer,
269 msecs_to_jiffies(info->timeout * 1000) + now);
271 pr_debug("increased refcnt of timer %s to %u\n",
272 info->label, info->timer->refcnt);
273 } else {
274 ret = idletimer_tg_create(info);
275 if (ret < 0) {
276 pr_debug("failed to create timer\n");
277 mutex_unlock(&list_mutex);
278 return ret;
279 }
280 }
282 mutex_unlock(&list_mutex);
284 return 0;
285 }
287 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
288 {
289 const struct idletimer_tg_info *info = par->targinfo;
291 pr_debug("destroy targinfo %s\n", info->label);
293 mutex_lock(&list_mutex);
295 if (--info->timer->refcnt == 0) {
296 pr_debug("deleting timer %s\n", info->label);
298 list_del(&info->timer->entry);
299 del_timer_sync(&info->timer->timer);
300 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
301 kfree(info->timer->attr.attr.name);
302 kfree(info->timer);
303 } else {
304 pr_debug("decreased refcnt of timer %s to %u\n",
305 info->label, info->timer->refcnt);
306 }
308 mutex_unlock(&list_mutex);
309 }
311 static struct xt_target idletimer_tg __read_mostly = {
312 .name = "IDLETIMER",
313 .revision = 1,
314 .family = NFPROTO_UNSPEC,
315 .target = idletimer_tg_target,
316 .targetsize = sizeof(struct idletimer_tg_info),
317 .checkentry = idletimer_tg_checkentry,
318 .destroy = idletimer_tg_destroy,
319 .me = THIS_MODULE,
320 };
322 static struct class *idletimer_tg_class;
324 static struct device *idletimer_tg_device;
326 static int __init idletimer_tg_init(void)
327 {
328 int err;
330 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
331 err = PTR_ERR(idletimer_tg_class);
332 if (IS_ERR(idletimer_tg_class)) {
333 pr_debug("couldn't register device class\n");
334 goto out;
335 }
337 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
338 MKDEV(0, 0), NULL, "timers");
339 err = PTR_ERR(idletimer_tg_device);
340 if (IS_ERR(idletimer_tg_device)) {
341 pr_debug("couldn't register system device\n");
342 goto out_class;
343 }
345 idletimer_tg_kobj = &idletimer_tg_device->kobj;
347 err = xt_register_target(&idletimer_tg);
348 if (err < 0) {
349 pr_debug("couldn't register xt target\n");
350 goto out_dev;
351 }
353 return 0;
354 out_dev:
355 device_destroy(idletimer_tg_class, MKDEV(0, 0));
356 out_class:
357 class_destroy(idletimer_tg_class);
358 out:
359 return err;
360 }
362 static void __exit idletimer_tg_exit(void)
363 {
364 xt_unregister_target(&idletimer_tg);
366 device_destroy(idletimer_tg_class, MKDEV(0, 0));
367 class_destroy(idletimer_tg_class);
368 }
370 module_init(idletimer_tg_init);
371 module_exit(idletimer_tg_exit);
373 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
374 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
375 MODULE_DESCRIPTION("Xtables: idle time monitor");
376 MODULE_LICENSE("GPL v2");
377 MODULE_ALIAS("ipt_IDLETIMER");
378 MODULE_ALIAS("ip6t_IDLETIMER");
379 MODULE_ALIAS("arpt_IDLETIMER");