Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / xt_IDLETIMER.c
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 */
30
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
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>
51
52 struct idletimer_tg_attr {
53 struct attribute attr;
54 ssize_t (*show)(struct kobject *kobj,
55 struct attribute *attr, char *buf);
56 };
57
58 struct idletimer_tg {
59 struct list_head entry;
60 struct timer_list timer;
61 struct work_struct work;
62
63 struct kobject *kobj;
64 struct idletimer_tg_attr attr;
65
66 struct timespec delayed_timer_trigger;
67 struct timespec last_modified_timer;
68 struct timespec last_suspend_time;
69 struct notifier_block pm_nb;
70
71 int timeout;
72 unsigned int refcnt;
73 bool work_pending;
74 bool send_nl_msg;
75 bool active;
76 };
77
78 static LIST_HEAD(idletimer_tg_list);
79 static DEFINE_MUTEX(list_mutex);
80 static DEFINE_SPINLOCK(timestamp_lock);
81
82 static struct kobject *idletimer_tg_kobj;
83
84 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
85 struct timespec *ts)
86 {
87 bool state;
88 struct timespec temp;
89 spin_lock_bh(&timestamp_lock);
90 timer->work_pending = false;
91 if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
92 timer->delayed_timer_trigger.tv_sec != 0) {
93 state = false;
94 temp.tv_sec = timer->timeout;
95 temp.tv_nsec = 0;
96 if (timer->delayed_timer_trigger.tv_sec != 0) {
97 temp = timespec_add(timer->delayed_timer_trigger, temp);
98 ts->tv_sec = temp.tv_sec;
99 ts->tv_nsec = temp.tv_nsec;
100 timer->delayed_timer_trigger.tv_sec = 0;
101 timer->work_pending = true;
102 schedule_work(&timer->work);
103 } else {
104 temp = timespec_add(timer->last_modified_timer, temp);
105 ts->tv_sec = temp.tv_sec;
106 ts->tv_nsec = temp.tv_nsec;
107 }
108 } else {
109 state = timer->active;
110 }
111 spin_unlock_bh(&timestamp_lock);
112 return state;
113 }
114
115 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
116 {
117 char iface_msg[NLMSG_MAX_SIZE];
118 char state_msg[NLMSG_MAX_SIZE];
119 char timestamp_msg[NLMSG_MAX_SIZE];
120 char *envp[] = { iface_msg, state_msg, timestamp_msg, NULL };
121 int res;
122 struct timespec ts;
123 uint64_t time_ns;
124 bool state;
125
126 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
127 iface);
128 if (NLMSG_MAX_SIZE <= res) {
129 pr_err("message too long (%d)", res);
130 return;
131 }
132
133 get_monotonic_boottime(&ts);
134 state = check_for_delayed_trigger(timer, &ts);
135 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
136 state ? "active" : "inactive");
137
138 if (NLMSG_MAX_SIZE <= res) {
139 pr_err("message too long (%d)", res);
140 return;
141 }
142
143 time_ns = timespec_to_ns(&ts);
144 res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
145 if (NLMSG_MAX_SIZE <= res) {
146 timestamp_msg[0] = '\0';
147 pr_err("message too long (%d)", res);
148 }
149
150 pr_debug("putting nlmsg: <%s> <%s>\n", iface_msg, state_msg);
151 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
152 return;
153
154
155 }
156
157 static
158 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
159 {
160 struct idletimer_tg *entry;
161
162 BUG_ON(!label);
163
164 list_for_each_entry(entry, &idletimer_tg_list, entry) {
165 if (!strcmp(label, entry->attr.attr.name))
166 return entry;
167 }
168
169 return NULL;
170 }
171
172 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
173 char *buf)
174 {
175 struct idletimer_tg *timer;
176 unsigned long expires = 0;
177 unsigned long now = jiffies;
178
179 mutex_lock(&list_mutex);
180
181 timer = __idletimer_tg_find_by_label(attr->name);
182 if (timer)
183 expires = timer->timer.expires;
184
185 mutex_unlock(&list_mutex);
186
187 if (time_after(expires, now))
188 return sprintf(buf, "%u\n",
189 jiffies_to_msecs(expires - now) / 1000);
190
191 if (timer->send_nl_msg)
192 return sprintf(buf, "0 %d\n",
193 jiffies_to_msecs(now - expires) / 1000);
194 else
195 return sprintf(buf, "0\n");
196 }
197
198 static void idletimer_tg_work(struct work_struct *work)
199 {
200 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
201 work);
202
203 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
204
205 if (timer->send_nl_msg)
206 notify_netlink_uevent(timer->attr.attr.name, timer);
207 }
208
209 static void idletimer_tg_expired(unsigned long data)
210 {
211 struct idletimer_tg *timer = (struct idletimer_tg *) data;
212
213 pr_debug("timer %s expired\n", timer->attr.attr.name);
214 spin_lock_bh(&timestamp_lock);
215 timer->active = false;
216 timer->work_pending = true;
217 schedule_work(&timer->work);
218 spin_unlock_bh(&timestamp_lock);
219 }
220
221 static int idletimer_resume(struct notifier_block *notifier,
222 unsigned long pm_event, void *unused)
223 {
224 struct timespec ts;
225 unsigned long time_diff, now = jiffies;
226 struct idletimer_tg *timer = container_of(notifier,
227 struct idletimer_tg, pm_nb);
228 if (!timer)
229 return NOTIFY_DONE;
230 switch (pm_event) {
231 case PM_SUSPEND_PREPARE:
232 get_monotonic_boottime(&timer->last_suspend_time);
233 break;
234 case PM_POST_SUSPEND:
235 spin_lock_bh(&timestamp_lock);
236 if (!timer->active) {
237 spin_unlock_bh(&timestamp_lock);
238 break;
239 }
240 /* since jiffies are not updated when suspended now represents
241 * the time it would have suspended */
242 if (time_after(timer->timer.expires, now)) {
243 get_monotonic_boottime(&ts);
244 ts = timespec_sub(ts, timer->last_suspend_time);
245 time_diff = timespec_to_jiffies(&ts);
246 if (timer->timer.expires > (time_diff + now)) {
247 mod_timer_pending(&timer->timer,
248 (timer->timer.expires - time_diff));
249 } else {
250 del_timer(&timer->timer);
251 timer->timer.expires = 0;
252 timer->active = false;
253 timer->work_pending = true;
254 schedule_work(&timer->work);
255 }
256 }
257 spin_unlock_bh(&timestamp_lock);
258 break;
259 default:
260 break;
261 }
262 return NOTIFY_DONE;
263 }
264
265 static int idletimer_tg_create(struct idletimer_tg_info *info)
266 {
267 int ret;
268
269 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
270 if (!info->timer) {
271 ret = -ENOMEM;
272 goto out;
273 }
274
275 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
276 if (!info->timer->attr.attr.name) {
277 ret = -ENOMEM;
278 goto out_free_timer;
279 }
280 info->timer->attr.attr.mode = S_IRUGO;
281 info->timer->attr.show = idletimer_tg_show;
282
283 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
284 if (ret < 0) {
285 pr_debug("couldn't add file to sysfs");
286 goto out_free_attr;
287 }
288
289 list_add(&info->timer->entry, &idletimer_tg_list);
290
291 setup_timer(&info->timer->timer, idletimer_tg_expired,
292 (unsigned long) info->timer);
293 info->timer->refcnt = 1;
294 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
295 info->timer->active = true;
296 info->timer->timeout = info->timeout;
297
298 info->timer->delayed_timer_trigger.tv_sec = 0;
299 info->timer->delayed_timer_trigger.tv_nsec = 0;
300 info->timer->work_pending = false;
301 get_monotonic_boottime(&info->timer->last_modified_timer);
302
303 info->timer->pm_nb.notifier_call = idletimer_resume;
304 ret = register_pm_notifier(&info->timer->pm_nb);
305 if (ret)
306 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
307 __func__, ret);
308
309 mod_timer(&info->timer->timer,
310 msecs_to_jiffies(info->timeout * 1000) + jiffies);
311
312 INIT_WORK(&info->timer->work, idletimer_tg_work);
313
314 return 0;
315
316 out_free_attr:
317 kfree(info->timer->attr.attr.name);
318 out_free_timer:
319 kfree(info->timer);
320 out:
321 return ret;
322 }
323
324 static void reset_timer(const struct idletimer_tg_info *info)
325 {
326 unsigned long now = jiffies;
327 struct idletimer_tg *timer = info->timer;
328 bool timer_prev;
329
330 spin_lock_bh(&timestamp_lock);
331 timer_prev = timer->active;
332 timer->active = true;
333 /* timer_prev is used to guard overflow problem in time_before*/
334 if (!timer_prev || time_before(timer->timer.expires, now)) {
335 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
336 timer->timer.expires, now);
337 /* checks if there is a pending inactive notification*/
338 if (timer->work_pending)
339 timer->delayed_timer_trigger = timer->last_modified_timer;
340 else {
341 timer->work_pending = true;
342 schedule_work(&timer->work);
343 }
344 }
345
346 get_monotonic_boottime(&timer->last_modified_timer);
347 mod_timer(&timer->timer,
348 msecs_to_jiffies(info->timeout * 1000) + now);
349 spin_unlock_bh(&timestamp_lock);
350 }
351
352 /*
353 * The actual xt_tables plugin.
354 */
355 static unsigned int idletimer_tg_target(struct sk_buff *skb,
356 const struct xt_action_param *par)
357 {
358 const struct idletimer_tg_info *info = par->targinfo;
359 unsigned long now = jiffies;
360
361 pr_debug("resetting timer %s, timeout period %u\n",
362 info->label, info->timeout);
363
364 BUG_ON(!info->timer);
365
366 info->timer->active = true;
367
368 if (time_before(info->timer->timer.expires, now)) {
369 schedule_work(&info->timer->work);
370 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
371 info->label, info->timer->timer.expires, now);
372 }
373
374 /* TODO: Avoid modifying timers on each packet */
375 reset_timer(info);
376 return XT_CONTINUE;
377 }
378
379 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
380 {
381 struct idletimer_tg_info *info = par->targinfo;
382 int ret;
383
384 pr_debug("checkentry targinfo %s\n", info->label);
385
386 if (info->timeout == 0) {
387 pr_debug("timeout value is zero\n");
388 return -EINVAL;
389 }
390
391 if (info->label[0] == '\0' ||
392 strnlen(info->label,
393 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
394 pr_debug("label is empty or not nul-terminated\n");
395 return -EINVAL;
396 }
397
398 mutex_lock(&list_mutex);
399
400 info->timer = __idletimer_tg_find_by_label(info->label);
401 if (info->timer) {
402 info->timer->refcnt++;
403 reset_timer(info);
404 pr_debug("increased refcnt of timer %s to %u\n",
405 info->label, info->timer->refcnt);
406 } else {
407 ret = idletimer_tg_create(info);
408 if (ret < 0) {
409 pr_debug("failed to create timer\n");
410 mutex_unlock(&list_mutex);
411 return ret;
412 }
413 }
414
415 mutex_unlock(&list_mutex);
416
417 return 0;
418 }
419
420 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
421 {
422 const struct idletimer_tg_info *info = par->targinfo;
423
424 pr_debug("destroy targinfo %s\n", info->label);
425
426 mutex_lock(&list_mutex);
427
428 if (--info->timer->refcnt == 0) {
429 pr_debug("deleting timer %s\n", info->label);
430
431 list_del(&info->timer->entry);
432 del_timer_sync(&info->timer->timer);
433 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
434 unregister_pm_notifier(&info->timer->pm_nb);
435 kfree(info->timer->attr.attr.name);
436 kfree(info->timer);
437 } else {
438 pr_debug("decreased refcnt of timer %s to %u\n",
439 info->label, info->timer->refcnt);
440 }
441
442 mutex_unlock(&list_mutex);
443 }
444
445 static struct xt_target idletimer_tg __read_mostly = {
446 .name = "IDLETIMER",
447 .revision = 1,
448 .family = NFPROTO_UNSPEC,
449 .target = idletimer_tg_target,
450 .targetsize = sizeof(struct idletimer_tg_info),
451 .checkentry = idletimer_tg_checkentry,
452 .destroy = idletimer_tg_destroy,
453 .me = THIS_MODULE,
454 };
455
456 static struct class *idletimer_tg_class;
457
458 static struct device *idletimer_tg_device;
459
460 static int __init idletimer_tg_init(void)
461 {
462 int err;
463
464 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
465 err = PTR_ERR(idletimer_tg_class);
466 if (IS_ERR(idletimer_tg_class)) {
467 pr_debug("couldn't register device class\n");
468 goto out;
469 }
470
471 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
472 MKDEV(0, 0), NULL, "timers");
473 err = PTR_ERR(idletimer_tg_device);
474 if (IS_ERR(idletimer_tg_device)) {
475 pr_debug("couldn't register system device\n");
476 goto out_class;
477 }
478
479 idletimer_tg_kobj = &idletimer_tg_device->kobj;
480
481 err = xt_register_target(&idletimer_tg);
482 if (err < 0) {
483 pr_debug("couldn't register xt target\n");
484 goto out_dev;
485 }
486
487 return 0;
488 out_dev:
489 device_destroy(idletimer_tg_class, MKDEV(0, 0));
490 out_class:
491 class_destroy(idletimer_tg_class);
492 out:
493 return err;
494 }
495
496 static void __exit idletimer_tg_exit(void)
497 {
498 xt_unregister_target(&idletimer_tg);
499
500 device_destroy(idletimer_tg_class, MKDEV(0, 0));
501 class_destroy(idletimer_tg_class);
502 }
503
504 module_init(idletimer_tg_init);
505 module_exit(idletimer_tg_exit);
506
507 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
508 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
509 MODULE_DESCRIPTION("Xtables: idle time monitor");
510 MODULE_LICENSE("GPL v2");
511 MODULE_ALIAS("ipt_IDLETIMER");
512 MODULE_ALIAS("ip6t_IDLETIMER");
513 MODULE_ALIAS("arpt_IDLETIMER");