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 <linux/rtc.h>
46 #include <linux/time.h>
47 #include <linux/math64.h>
48 #include <linux/suspend.h>
49 #include <linux/notifier.h>
50 #include <net/net_namespace.h>
53 struct idletimer_tg_attr {
54 struct attribute attr;
55 ssize_t (*show)(struct kobject *kobj,
56 struct attribute *attr, char *buf);
60 struct list_head entry;
61 struct timer_list timer;
62 struct work_struct work;
65 struct idletimer_tg_attr attr;
67 struct timespec delayed_timer_trigger;
68 struct timespec last_modified_timer;
69 struct timespec last_suspend_time;
70 struct notifier_block pm_nb;
80 static LIST_HEAD(idletimer_tg_list);
81 static DEFINE_MUTEX(list_mutex);
82 static DEFINE_SPINLOCK(timestamp_lock);
84 static struct kobject *idletimer_tg_kobj;
86 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
91 spin_lock_bh(×tamp_lock);
92 timer->work_pending = false;
93 if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
94 timer->delayed_timer_trigger.tv_sec != 0) {
96 temp.tv_sec = timer->timeout;
98 if (timer->delayed_timer_trigger.tv_sec != 0) {
99 temp = timespec_add(timer->delayed_timer_trigger, temp);
100 ts->tv_sec = temp.tv_sec;
101 ts->tv_nsec = temp.tv_nsec;
102 timer->delayed_timer_trigger.tv_sec = 0;
103 timer->work_pending = true;
104 schedule_work(&timer->work);
106 temp = timespec_add(timer->last_modified_timer, temp);
107 ts->tv_sec = temp.tv_sec;
108 ts->tv_nsec = temp.tv_nsec;
111 state = timer->active;
113 spin_unlock_bh(×tamp_lock);
117 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
119 char iface_msg[NLMSG_MAX_SIZE];
120 char state_msg[NLMSG_MAX_SIZE];
121 char timestamp_msg[NLMSG_MAX_SIZE];
122 char uid_msg[NLMSG_MAX_SIZE];
123 char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL };
129 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
131 if (NLMSG_MAX_SIZE <= res) {
132 pr_err("message too long (%d)", res);
136 get_monotonic_boottime(&ts);
137 state = check_for_delayed_trigger(timer, &ts);
138 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
139 state ? "active" : "inactive");
141 if (NLMSG_MAX_SIZE <= res) {
142 pr_err("message too long (%d)", res);
147 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid);
148 if (NLMSG_MAX_SIZE <= res)
149 pr_err("message too long (%d)", res);
151 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
152 if (NLMSG_MAX_SIZE <= res)
153 pr_err("message too long (%d)", res);
156 time_ns = timespec_to_ns(&ts);
157 res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
158 if (NLMSG_MAX_SIZE <= res) {
159 timestamp_msg[0] = '\0';
160 pr_err("message too long (%d)", res);
163 pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg,
164 timestamp_msg, uid_msg);
165 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
172 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
174 struct idletimer_tg *entry;
178 list_for_each_entry(entry, &idletimer_tg_list, entry) {
179 if (!strcmp(label, entry->attr.attr.name))
186 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
189 struct idletimer_tg *timer;
190 unsigned long expires = 0;
191 unsigned long now = jiffies;
193 mutex_lock(&list_mutex);
195 timer = __idletimer_tg_find_by_label(attr->name);
197 expires = timer->timer.expires;
199 mutex_unlock(&list_mutex);
201 if (time_after(expires, now))
202 return sprintf(buf, "%u\n",
203 jiffies_to_msecs(expires - now) / 1000);
205 if (timer->send_nl_msg)
206 return sprintf(buf, "0 %d\n",
207 jiffies_to_msecs(now - expires) / 1000);
209 return sprintf(buf, "0\n");
212 static void idletimer_tg_work(struct work_struct *work)
214 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
217 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
219 if (timer->send_nl_msg)
220 notify_netlink_uevent(timer->attr.attr.name, timer);
223 static void idletimer_tg_expired(unsigned long data)
225 struct idletimer_tg *timer = (struct idletimer_tg *) data;
227 pr_debug("timer %s expired\n", timer->attr.attr.name);
228 spin_lock_bh(×tamp_lock);
229 timer->active = false;
230 timer->work_pending = true;
231 schedule_work(&timer->work);
232 spin_unlock_bh(×tamp_lock);
235 static int idletimer_resume(struct notifier_block *notifier,
236 unsigned long pm_event, void *unused)
239 unsigned long time_diff, now = jiffies;
240 struct idletimer_tg *timer = container_of(notifier,
241 struct idletimer_tg, pm_nb);
245 case PM_SUSPEND_PREPARE:
246 get_monotonic_boottime(&timer->last_suspend_time);
248 case PM_POST_SUSPEND:
249 spin_lock_bh(×tamp_lock);
250 if (!timer->active) {
251 spin_unlock_bh(×tamp_lock);
254 /* since jiffies are not updated when suspended now represents
255 * the time it would have suspended */
256 if (time_after(timer->timer.expires, now)) {
257 get_monotonic_boottime(&ts);
258 ts = timespec_sub(ts, timer->last_suspend_time);
259 time_diff = timespec_to_jiffies(&ts);
260 if (timer->timer.expires > (time_diff + now)) {
261 mod_timer_pending(&timer->timer,
262 (timer->timer.expires - time_diff));
264 del_timer(&timer->timer);
265 timer->timer.expires = 0;
266 timer->active = false;
267 timer->work_pending = true;
268 schedule_work(&timer->work);
271 spin_unlock_bh(×tamp_lock);
279 static int idletimer_tg_create(struct idletimer_tg_info *info)
283 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
289 sysfs_attr_init(&info->timer->attr.attr);
290 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
291 if (!info->timer->attr.attr.name) {
295 info->timer->attr.attr.mode = S_IRUGO;
296 info->timer->attr.show = idletimer_tg_show;
298 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
300 pr_debug("couldn't add file to sysfs");
304 list_add(&info->timer->entry, &idletimer_tg_list);
306 setup_timer(&info->timer->timer, idletimer_tg_expired,
307 (unsigned long) info->timer);
308 info->timer->refcnt = 1;
309 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
310 info->timer->active = true;
311 info->timer->timeout = info->timeout;
313 info->timer->delayed_timer_trigger.tv_sec = 0;
314 info->timer->delayed_timer_trigger.tv_nsec = 0;
315 info->timer->work_pending = false;
316 info->timer->uid = 0;
317 get_monotonic_boottime(&info->timer->last_modified_timer);
319 info->timer->pm_nb.notifier_call = idletimer_resume;
320 ret = register_pm_notifier(&info->timer->pm_nb);
322 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
325 mod_timer(&info->timer->timer,
326 msecs_to_jiffies(info->timeout * 1000) + jiffies);
328 INIT_WORK(&info->timer->work, idletimer_tg_work);
333 kfree(info->timer->attr.attr.name);
340 static void reset_timer(const struct idletimer_tg_info *info,
343 unsigned long now = jiffies;
344 struct idletimer_tg *timer = info->timer;
347 spin_lock_bh(×tamp_lock);
348 timer_prev = timer->active;
349 timer->active = true;
350 /* timer_prev is used to guard overflow problem in time_before*/
351 if (!timer_prev || time_before(timer->timer.expires, now)) {
352 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
353 timer->timer.expires, now);
355 /* Stores the uid resposible for waking up the radio */
356 if (skb && (skb->sk)) {
357 timer->uid = from_kuid_munged(current_user_ns(),
358 sock_i_uid(skb->sk));
361 /* checks if there is a pending inactive notification*/
362 if (timer->work_pending)
363 timer->delayed_timer_trigger = timer->last_modified_timer;
365 timer->work_pending = true;
366 schedule_work(&timer->work);
370 get_monotonic_boottime(&timer->last_modified_timer);
371 mod_timer(&timer->timer,
372 msecs_to_jiffies(info->timeout * 1000) + now);
373 spin_unlock_bh(×tamp_lock);
377 * The actual xt_tables plugin.
379 static unsigned int idletimer_tg_target(struct sk_buff *skb,
380 const struct xt_action_param *par)
382 const struct idletimer_tg_info *info = par->targinfo;
383 unsigned long now = jiffies;
385 pr_debug("resetting timer %s, timeout period %u\n",
386 info->label, info->timeout);
388 BUG_ON(!info->timer);
390 info->timer->active = true;
392 if (time_before(info->timer->timer.expires, now)) {
393 schedule_work(&info->timer->work);
394 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
395 info->label, info->timer->timer.expires, now);
398 /* TODO: Avoid modifying timers on each packet */
399 reset_timer(info, skb);
403 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
405 struct idletimer_tg_info *info = par->targinfo;
408 pr_debug("checkentry targinfo %s\n", info->label);
410 if (info->timeout == 0) {
411 pr_debug("timeout value is zero\n");
415 if (info->label[0] == '\0' ||
417 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
418 pr_debug("label is empty or not nul-terminated\n");
422 mutex_lock(&list_mutex);
424 info->timer = __idletimer_tg_find_by_label(info->label);
426 info->timer->refcnt++;
427 reset_timer(info, NULL);
428 pr_debug("increased refcnt of timer %s to %u\n",
429 info->label, info->timer->refcnt);
431 ret = idletimer_tg_create(info);
433 pr_debug("failed to create timer\n");
434 mutex_unlock(&list_mutex);
439 mutex_unlock(&list_mutex);
444 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
446 const struct idletimer_tg_info *info = par->targinfo;
448 pr_debug("destroy targinfo %s\n", info->label);
450 mutex_lock(&list_mutex);
452 if (--info->timer->refcnt == 0) {
453 pr_debug("deleting timer %s\n", info->label);
455 list_del(&info->timer->entry);
456 del_timer_sync(&info->timer->timer);
457 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
458 unregister_pm_notifier(&info->timer->pm_nb);
459 kfree(info->timer->attr.attr.name);
462 pr_debug("decreased refcnt of timer %s to %u\n",
463 info->label, info->timer->refcnt);
466 mutex_unlock(&list_mutex);
469 static struct xt_target idletimer_tg __read_mostly = {
472 .family = NFPROTO_UNSPEC,
473 .target = idletimer_tg_target,
474 .targetsize = sizeof(struct idletimer_tg_info),
475 .checkentry = idletimer_tg_checkentry,
476 .destroy = idletimer_tg_destroy,
480 static struct class *idletimer_tg_class;
482 static struct device *idletimer_tg_device;
484 static int __init idletimer_tg_init(void)
488 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
489 err = PTR_ERR(idletimer_tg_class);
490 if (IS_ERR(idletimer_tg_class)) {
491 pr_debug("couldn't register device class\n");
495 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
496 MKDEV(0, 0), NULL, "timers");
497 err = PTR_ERR(idletimer_tg_device);
498 if (IS_ERR(idletimer_tg_device)) {
499 pr_debug("couldn't register system device\n");
503 idletimer_tg_kobj = &idletimer_tg_device->kobj;
505 err = xt_register_target(&idletimer_tg);
507 pr_debug("couldn't register xt target\n");
513 device_destroy(idletimer_tg_class, MKDEV(0, 0));
515 class_destroy(idletimer_tg_class);
520 static void __exit idletimer_tg_exit(void)
522 xt_unregister_target(&idletimer_tg);
524 device_destroy(idletimer_tg_class, MKDEV(0, 0));
525 class_destroy(idletimer_tg_class);
528 module_init(idletimer_tg_init);
529 module_exit(idletimer_tg_exit);
531 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
532 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
533 MODULE_DESCRIPTION("Xtables: idle time monitor");
534 MODULE_LICENSE("GPL v2");
535 MODULE_ALIAS("ipt_IDLETIMER");
536 MODULE_ALIAS("ip6t_IDLETIMER");
537 MODULE_ALIAS("arpt_IDLETIMER");