Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[sfrench/cifs-2.6.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  * Written by Timo Teras <ext-timo.teras@nokia.com>
9  *
10  * Converted to x_tables and reworked for upstream inclusion
11  * by Luciano Coelho <luciano.coelho@nokia.com>
12  *
13  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27  * 02110-1301 USA
28  */
29
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/module.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/mutex.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter/x_tables.h>
38 #include <linux/netfilter/xt_IDLETIMER.h>
39 #include <linux/kdev_t.h>
40 #include <linux/kobject.h>
41 #include <linux/workqueue.h>
42 #include <linux/sysfs.h>
43
44 struct idletimer_tg_attr {
45         struct attribute attr;
46         ssize_t (*show)(struct kobject *kobj,
47                         struct attribute *attr, char *buf);
48 };
49
50 struct idletimer_tg {
51         struct list_head entry;
52         struct timer_list timer;
53         struct work_struct work;
54
55         struct kobject *kobj;
56         struct idletimer_tg_attr attr;
57
58         unsigned int refcnt;
59 };
60
61 static LIST_HEAD(idletimer_tg_list);
62 static DEFINE_MUTEX(list_mutex);
63
64 static struct kobject *idletimer_tg_kobj;
65
66 static
67 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
68 {
69         struct idletimer_tg *entry;
70
71         list_for_each_entry(entry, &idletimer_tg_list, entry) {
72                 if (!strcmp(label, entry->attr.attr.name))
73                         return entry;
74         }
75
76         return NULL;
77 }
78
79 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
80                                  char *buf)
81 {
82         struct idletimer_tg *timer;
83         unsigned long expires = 0;
84
85         mutex_lock(&list_mutex);
86
87         timer = __idletimer_tg_find_by_label(attr->name);
88         if (timer)
89                 expires = timer->timer.expires;
90
91         mutex_unlock(&list_mutex);
92
93         if (time_after(expires, jiffies))
94                 return sprintf(buf, "%u\n",
95                                jiffies_to_msecs(expires - jiffies) / 1000);
96
97         return sprintf(buf, "0\n");
98 }
99
100 static void idletimer_tg_work(struct work_struct *work)
101 {
102         struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
103                                                   work);
104
105         sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
106 }
107
108 static void idletimer_tg_expired(struct timer_list *t)
109 {
110         struct idletimer_tg *timer = from_timer(timer, t, timer);
111
112         pr_debug("timer %s expired\n", timer->attr.attr.name);
113
114         schedule_work(&timer->work);
115 }
116
117 static int idletimer_check_sysfs_name(const char *name, unsigned int size)
118 {
119         int ret;
120
121         ret = xt_check_proc_name(name, size);
122         if (ret < 0)
123                 return ret;
124
125         if (!strcmp(name, "power") ||
126             !strcmp(name, "subsystem") ||
127             !strcmp(name, "uevent"))
128                 return -EINVAL;
129
130         return 0;
131 }
132
133 static int idletimer_tg_create(struct idletimer_tg_info *info)
134 {
135         int ret;
136
137         info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
138         if (!info->timer) {
139                 ret = -ENOMEM;
140                 goto out;
141         }
142
143         ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
144         if (ret < 0)
145                 goto out_free_timer;
146
147         sysfs_attr_init(&info->timer->attr.attr);
148         info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
149         if (!info->timer->attr.attr.name) {
150                 ret = -ENOMEM;
151                 goto out_free_timer;
152         }
153         info->timer->attr.attr.mode = 0444;
154         info->timer->attr.show = idletimer_tg_show;
155
156         ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
157         if (ret < 0) {
158                 pr_debug("couldn't add file to sysfs");
159                 goto out_free_attr;
160         }
161
162         list_add(&info->timer->entry, &idletimer_tg_list);
163
164         timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
165         info->timer->refcnt = 1;
166
167         INIT_WORK(&info->timer->work, idletimer_tg_work);
168
169         mod_timer(&info->timer->timer,
170                   msecs_to_jiffies(info->timeout * 1000) + jiffies);
171
172         return 0;
173
174 out_free_attr:
175         kfree(info->timer->attr.attr.name);
176 out_free_timer:
177         kfree(info->timer);
178 out:
179         return ret;
180 }
181
182 /*
183  * The actual xt_tables plugin.
184  */
185 static unsigned int idletimer_tg_target(struct sk_buff *skb,
186                                          const struct xt_action_param *par)
187 {
188         const struct idletimer_tg_info *info = par->targinfo;
189
190         pr_debug("resetting timer %s, timeout period %u\n",
191                  info->label, info->timeout);
192
193         mod_timer(&info->timer->timer,
194                   msecs_to_jiffies(info->timeout * 1000) + jiffies);
195
196         return XT_CONTINUE;
197 }
198
199 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
200 {
201         struct idletimer_tg_info *info = par->targinfo;
202         int ret;
203
204         pr_debug("checkentry targinfo%s\n", info->label);
205
206         if (info->timeout == 0) {
207                 pr_debug("timeout value is zero\n");
208                 return -EINVAL;
209         }
210         if (info->timeout >= INT_MAX / 1000) {
211                 pr_debug("timeout value is too big\n");
212                 return -EINVAL;
213         }
214         if (info->label[0] == '\0' ||
215             strnlen(info->label,
216                     MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
217                 pr_debug("label is empty or not nul-terminated\n");
218                 return -EINVAL;
219         }
220
221         mutex_lock(&list_mutex);
222
223         info->timer = __idletimer_tg_find_by_label(info->label);
224         if (info->timer) {
225                 info->timer->refcnt++;
226                 mod_timer(&info->timer->timer,
227                           msecs_to_jiffies(info->timeout * 1000) + jiffies);
228
229                 pr_debug("increased refcnt of timer %s to %u\n",
230                          info->label, info->timer->refcnt);
231         } else {
232                 ret = idletimer_tg_create(info);
233                 if (ret < 0) {
234                         pr_debug("failed to create timer\n");
235                         mutex_unlock(&list_mutex);
236                         return ret;
237                 }
238         }
239
240         mutex_unlock(&list_mutex);
241         return 0;
242 }
243
244 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
245 {
246         const struct idletimer_tg_info *info = par->targinfo;
247
248         pr_debug("destroy targinfo %s\n", info->label);
249
250         mutex_lock(&list_mutex);
251
252         if (--info->timer->refcnt == 0) {
253                 pr_debug("deleting timer %s\n", info->label);
254
255                 list_del(&info->timer->entry);
256                 del_timer_sync(&info->timer->timer);
257                 cancel_work_sync(&info->timer->work);
258                 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
259                 kfree(info->timer->attr.attr.name);
260                 kfree(info->timer);
261         } else {
262                 pr_debug("decreased refcnt of timer %s to %u\n",
263                          info->label, info->timer->refcnt);
264         }
265
266         mutex_unlock(&list_mutex);
267 }
268
269 static struct xt_target idletimer_tg __read_mostly = {
270         .name           = "IDLETIMER",
271         .family         = NFPROTO_UNSPEC,
272         .target         = idletimer_tg_target,
273         .targetsize     = sizeof(struct idletimer_tg_info),
274         .usersize       = offsetof(struct idletimer_tg_info, timer),
275         .checkentry     = idletimer_tg_checkentry,
276         .destroy        = idletimer_tg_destroy,
277         .me             = THIS_MODULE,
278 };
279
280 static struct class *idletimer_tg_class;
281
282 static struct device *idletimer_tg_device;
283
284 static int __init idletimer_tg_init(void)
285 {
286         int err;
287
288         idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
289         err = PTR_ERR(idletimer_tg_class);
290         if (IS_ERR(idletimer_tg_class)) {
291                 pr_debug("couldn't register device class\n");
292                 goto out;
293         }
294
295         idletimer_tg_device = device_create(idletimer_tg_class, NULL,
296                                             MKDEV(0, 0), NULL, "timers");
297         err = PTR_ERR(idletimer_tg_device);
298         if (IS_ERR(idletimer_tg_device)) {
299                 pr_debug("couldn't register system device\n");
300                 goto out_class;
301         }
302
303         idletimer_tg_kobj = &idletimer_tg_device->kobj;
304
305         err =  xt_register_target(&idletimer_tg);
306         if (err < 0) {
307                 pr_debug("couldn't register xt target\n");
308                 goto out_dev;
309         }
310
311         return 0;
312 out_dev:
313         device_destroy(idletimer_tg_class, MKDEV(0, 0));
314 out_class:
315         class_destroy(idletimer_tg_class);
316 out:
317         return err;
318 }
319
320 static void __exit idletimer_tg_exit(void)
321 {
322         xt_unregister_target(&idletimer_tg);
323
324         device_destroy(idletimer_tg_class, MKDEV(0, 0));
325         class_destroy(idletimer_tg_class);
326 }
327
328 module_init(idletimer_tg_init);
329 module_exit(idletimer_tg_exit);
330
331 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
332 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
333 MODULE_DESCRIPTION("Xtables: idle time monitor");
334 MODULE_LICENSE("GPL v2");
335 MODULE_ALIAS("ipt_IDLETIMER");
336 MODULE_ALIAS("ip6t_IDLETIMER");