nf: Remove compilation error caused by e8430cbed3ef15fdb1ac26cfd020e010aa5f1c35
[firefly-linux-kernel-4.4.55.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         sysfs_attr_init(&info->timer->attr.attr);
276         info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
277         if (!info->timer->attr.attr.name) {
278                 ret = -ENOMEM;
279                 goto out_free_timer;
280         }
281         info->timer->attr.attr.mode = S_IRUGO;
282         info->timer->attr.show = idletimer_tg_show;
283
284         ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
285         if (ret < 0) {
286                 pr_debug("couldn't add file to sysfs");
287                 goto out_free_attr;
288         }
289
290         list_add(&info->timer->entry, &idletimer_tg_list);
291
292         setup_timer(&info->timer->timer, idletimer_tg_expired,
293                     (unsigned long) info->timer);
294         info->timer->refcnt = 1;
295         info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
296         info->timer->active = true;
297         info->timer->timeout = info->timeout;
298
299         info->timer->delayed_timer_trigger.tv_sec = 0;
300         info->timer->delayed_timer_trigger.tv_nsec = 0;
301         info->timer->work_pending = false;
302         get_monotonic_boottime(&info->timer->last_modified_timer);
303
304         info->timer->pm_nb.notifier_call = idletimer_resume;
305         ret = register_pm_notifier(&info->timer->pm_nb);
306         if (ret)
307                 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
308                                 __func__, ret);
309
310         mod_timer(&info->timer->timer,
311                   msecs_to_jiffies(info->timeout * 1000) + jiffies);
312
313         INIT_WORK(&info->timer->work, idletimer_tg_work);
314
315         return 0;
316
317 out_free_attr:
318         kfree(info->timer->attr.attr.name);
319 out_free_timer:
320         kfree(info->timer);
321 out:
322         return ret;
323 }
324
325 static void reset_timer(const struct idletimer_tg_info *info)
326 {
327         unsigned long now = jiffies;
328         struct idletimer_tg *timer = info->timer;
329         bool timer_prev;
330
331         spin_lock_bh(&timestamp_lock);
332         timer_prev = timer->active;
333         timer->active = true;
334         /* timer_prev is used to guard overflow problem in time_before*/
335         if (!timer_prev || time_before(timer->timer.expires, now)) {
336                 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
337                                 timer->timer.expires, now);
338                 /* checks if there is a pending inactive notification*/
339                 if (timer->work_pending)
340                         timer->delayed_timer_trigger = timer->last_modified_timer;
341                 else {
342                         timer->work_pending = true;
343                         schedule_work(&timer->work);
344                 }
345         }
346
347         get_monotonic_boottime(&timer->last_modified_timer);
348         mod_timer(&timer->timer,
349                         msecs_to_jiffies(info->timeout * 1000) + now);
350         spin_unlock_bh(&timestamp_lock);
351 }
352
353 /*
354  * The actual xt_tables plugin.
355  */
356 static unsigned int idletimer_tg_target(struct sk_buff *skb,
357                                          const struct xt_action_param *par)
358 {
359         const struct idletimer_tg_info *info = par->targinfo;
360         unsigned long now = jiffies;
361
362         pr_debug("resetting timer %s, timeout period %u\n",
363                  info->label, info->timeout);
364
365         BUG_ON(!info->timer);
366
367         info->timer->active = true;
368
369         if (time_before(info->timer->timer.expires, now)) {
370                 schedule_work(&info->timer->work);
371                 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
372                          info->label, info->timer->timer.expires, now);
373         }
374
375         /* TODO: Avoid modifying timers on each packet */
376         reset_timer(info);
377         return XT_CONTINUE;
378 }
379
380 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
381 {
382         struct idletimer_tg_info *info = par->targinfo;
383         int ret;
384
385         pr_debug("checkentry targinfo %s\n", info->label);
386
387         if (info->timeout == 0) {
388                 pr_debug("timeout value is zero\n");
389                 return -EINVAL;
390         }
391
392         if (info->label[0] == '\0' ||
393             strnlen(info->label,
394                     MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
395                 pr_debug("label is empty or not nul-terminated\n");
396                 return -EINVAL;
397         }
398
399         mutex_lock(&list_mutex);
400
401         info->timer = __idletimer_tg_find_by_label(info->label);
402         if (info->timer) {
403                 info->timer->refcnt++;
404                 reset_timer(info);
405                 pr_debug("increased refcnt of timer %s to %u\n",
406                          info->label, info->timer->refcnt);
407         } else {
408                 ret = idletimer_tg_create(info);
409                 if (ret < 0) {
410                         pr_debug("failed to create timer\n");
411                         mutex_unlock(&list_mutex);
412                         return ret;
413                 }
414         }
415
416         mutex_unlock(&list_mutex);
417
418         return 0;
419 }
420
421 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
422 {
423         const struct idletimer_tg_info *info = par->targinfo;
424
425         pr_debug("destroy targinfo %s\n", info->label);
426
427         mutex_lock(&list_mutex);
428
429         if (--info->timer->refcnt == 0) {
430                 pr_debug("deleting timer %s\n", info->label);
431
432                 list_del(&info->timer->entry);
433                 del_timer_sync(&info->timer->timer);
434                 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
435                 unregister_pm_notifier(&info->timer->pm_nb);
436                 kfree(info->timer->attr.attr.name);
437                 kfree(info->timer);
438         } else {
439                 pr_debug("decreased refcnt of timer %s to %u\n",
440                 info->label, info->timer->refcnt);
441         }
442
443         mutex_unlock(&list_mutex);
444 }
445
446 static struct xt_target idletimer_tg __read_mostly = {
447         .name           = "IDLETIMER",
448         .revision       = 1,
449         .family         = NFPROTO_UNSPEC,
450         .target         = idletimer_tg_target,
451         .targetsize     = sizeof(struct idletimer_tg_info),
452         .checkentry     = idletimer_tg_checkentry,
453         .destroy        = idletimer_tg_destroy,
454         .me             = THIS_MODULE,
455 };
456
457 static struct class *idletimer_tg_class;
458
459 static struct device *idletimer_tg_device;
460
461 static int __init idletimer_tg_init(void)
462 {
463         int err;
464
465         idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
466         err = PTR_ERR(idletimer_tg_class);
467         if (IS_ERR(idletimer_tg_class)) {
468                 pr_debug("couldn't register device class\n");
469                 goto out;
470         }
471
472         idletimer_tg_device = device_create(idletimer_tg_class, NULL,
473                                             MKDEV(0, 0), NULL, "timers");
474         err = PTR_ERR(idletimer_tg_device);
475         if (IS_ERR(idletimer_tg_device)) {
476                 pr_debug("couldn't register system device\n");
477                 goto out_class;
478         }
479
480         idletimer_tg_kobj = &idletimer_tg_device->kobj;
481
482         err =  xt_register_target(&idletimer_tg);
483         if (err < 0) {
484                 pr_debug("couldn't register xt target\n");
485                 goto out_dev;
486         }
487
488         return 0;
489 out_dev:
490         device_destroy(idletimer_tg_class, MKDEV(0, 0));
491 out_class:
492         class_destroy(idletimer_tg_class);
493 out:
494         return err;
495 }
496
497 static void __exit idletimer_tg_exit(void)
498 {
499         xt_unregister_target(&idletimer_tg);
500
501         device_destroy(idletimer_tg_class, MKDEV(0, 0));
502         class_destroy(idletimer_tg_class);
503 }
504
505 module_init(idletimer_tg_init);
506 module_exit(idletimer_tg_exit);
507
508 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
509 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
510 MODULE_DESCRIPTION("Xtables: idle time monitor");
511 MODULE_LICENSE("GPL v2");
512 MODULE_ALIAS("ipt_IDLETIMER");
513 MODULE_ALIAS("ip6t_IDLETIMER");
514 MODULE_ALIAS("arpt_IDLETIMER");