linux/net/netfilter/xt_IDLETIMER.c
<<
>>
Prefs
   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
  44struct idletimer_tg_attr {
  45        struct attribute attr;
  46        ssize_t (*show)(struct kobject *kobj,
  47                        struct attribute *attr, char *buf);
  48};
  49
  50struct 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
  61static LIST_HEAD(idletimer_tg_list);
  62static DEFINE_MUTEX(list_mutex);
  63
  64static struct kobject *idletimer_tg_kobj;
  65
  66static
  67struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
  68{
  69        struct idletimer_tg *entry;
  70
  71        BUG_ON(!label);
  72
  73        list_for_each_entry(entry, &idletimer_tg_list, entry) {
  74                if (!strcmp(label, entry->attr.attr.name))
  75                        return entry;
  76        }
  77
  78        return NULL;
  79}
  80
  81static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
  82                                 char *buf)
  83{
  84        struct idletimer_tg *timer;
  85        unsigned long expires = 0;
  86
  87        mutex_lock(&list_mutex);
  88
  89        timer = __idletimer_tg_find_by_label(attr->name);
  90        if (timer)
  91                expires = timer->timer.expires;
  92
  93        mutex_unlock(&list_mutex);
  94
  95        if (time_after(expires, jiffies))
  96                return sprintf(buf, "%u\n",
  97                               jiffies_to_msecs(expires - jiffies) / 1000);
  98
  99        return sprintf(buf, "0\n");
 100}
 101
 102static void idletimer_tg_work(struct work_struct *work)
 103{
 104        struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
 105                                                  work);
 106
 107        sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
 108}
 109
 110static void idletimer_tg_expired(unsigned long data)
 111{
 112        struct idletimer_tg *timer = (struct idletimer_tg *) data;
 113
 114        pr_debug("timer %s expired\n", timer->attr.attr.name);
 115
 116        schedule_work(&timer->work);
 117}
 118
 119static int idletimer_tg_create(struct idletimer_tg_info *info)
 120{
 121        int ret;
 122
 123        info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
 124        if (!info->timer) {
 125                ret = -ENOMEM;
 126                goto out;
 127        }
 128
 129        sysfs_attr_init(&info->timer->attr.attr);
 130        info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
 131        if (!info->timer->attr.attr.name) {
 132                ret = -ENOMEM;
 133                goto out_free_timer;
 134        }
 135        info->timer->attr.attr.mode = S_IRUGO;
 136        info->timer->attr.show = idletimer_tg_show;
 137
 138        ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
 139        if (ret < 0) {
 140                pr_debug("couldn't add file to sysfs");
 141                goto out_free_attr;
 142        }
 143
 144        list_add(&info->timer->entry, &idletimer_tg_list);
 145
 146        setup_timer(&info->timer->timer, idletimer_tg_expired,
 147                    (unsigned long) info->timer);
 148        info->timer->refcnt = 1;
 149
 150        mod_timer(&info->timer->timer,
 151                  msecs_to_jiffies(info->timeout * 1000) + jiffies);
 152
 153        INIT_WORK(&info->timer->work, idletimer_tg_work);
 154
 155        return 0;
 156
 157out_free_attr:
 158        kfree(info->timer->attr.attr.name);
 159out_free_timer:
 160        kfree(info->timer);
 161out:
 162        return ret;
 163}
 164
 165/*
 166 * The actual xt_tables plugin.
 167 */
 168static unsigned int idletimer_tg_target(struct sk_buff *skb,
 169                                         const struct xt_action_param *par)
 170{
 171        const struct idletimer_tg_info *info = par->targinfo;
 172
 173        pr_debug("resetting timer %s, timeout period %u\n",
 174                 info->label, info->timeout);
 175
 176        BUG_ON(!info->timer);
 177
 178        mod_timer(&info->timer->timer,
 179                  msecs_to_jiffies(info->timeout * 1000) + jiffies);
 180
 181        return XT_CONTINUE;
 182}
 183
 184static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
 185{
 186        struct idletimer_tg_info *info = par->targinfo;
 187        int ret;
 188
 189        pr_debug("checkentry targinfo%s\n", info->label);
 190
 191        if (info->timeout == 0) {
 192                pr_debug("timeout value is zero\n");
 193                return -EINVAL;
 194        }
 195
 196        if (info->label[0] == '\0' ||
 197            strnlen(info->label,
 198                    MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
 199                pr_debug("label is empty or not nul-terminated\n");
 200                return -EINVAL;
 201        }
 202
 203        mutex_lock(&list_mutex);
 204
 205        info->timer = __idletimer_tg_find_by_label(info->label);
 206        if (info->timer) {
 207                info->timer->refcnt++;
 208                mod_timer(&info->timer->timer,
 209                          msecs_to_jiffies(info->timeout * 1000) + jiffies);
 210
 211                pr_debug("increased refcnt of timer %s to %u\n",
 212                         info->label, info->timer->refcnt);
 213        } else {
 214                ret = idletimer_tg_create(info);
 215                if (ret < 0) {
 216                        pr_debug("failed to create timer\n");
 217                        mutex_unlock(&list_mutex);
 218                        return ret;
 219                }
 220        }
 221
 222        mutex_unlock(&list_mutex);
 223        return 0;
 224}
 225
 226static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
 227{
 228        const struct idletimer_tg_info *info = par->targinfo;
 229
 230        pr_debug("destroy targinfo %s\n", info->label);
 231
 232        mutex_lock(&list_mutex);
 233
 234        if (--info->timer->refcnt == 0) {
 235                pr_debug("deleting timer %s\n", info->label);
 236
 237                list_del(&info->timer->entry);
 238                del_timer_sync(&info->timer->timer);
 239                cancel_work_sync(&info->timer->work);
 240                sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
 241                kfree(info->timer->attr.attr.name);
 242                kfree(info->timer);
 243        } else {
 244                pr_debug("decreased refcnt of timer %s to %u\n",
 245                         info->label, info->timer->refcnt);
 246        }
 247
 248        mutex_unlock(&list_mutex);
 249}
 250
 251static struct xt_target idletimer_tg __read_mostly = {
 252        .name           = "IDLETIMER",
 253        .family         = NFPROTO_UNSPEC,
 254        .target         = idletimer_tg_target,
 255        .targetsize     = sizeof(struct idletimer_tg_info),
 256        .checkentry     = idletimer_tg_checkentry,
 257        .destroy        = idletimer_tg_destroy,
 258        .me             = THIS_MODULE,
 259};
 260
 261static struct class *idletimer_tg_class;
 262
 263static struct device *idletimer_tg_device;
 264
 265static int __init idletimer_tg_init(void)
 266{
 267        int err;
 268
 269        idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
 270        err = PTR_ERR(idletimer_tg_class);
 271        if (IS_ERR(idletimer_tg_class)) {
 272                pr_debug("couldn't register device class\n");
 273                goto out;
 274        }
 275
 276        idletimer_tg_device = device_create(idletimer_tg_class, NULL,
 277                                            MKDEV(0, 0), NULL, "timers");
 278        err = PTR_ERR(idletimer_tg_device);
 279        if (IS_ERR(idletimer_tg_device)) {
 280                pr_debug("couldn't register system device\n");
 281                goto out_class;
 282        }
 283
 284        idletimer_tg_kobj = &idletimer_tg_device->kobj;
 285
 286        err =  xt_register_target(&idletimer_tg);
 287        if (err < 0) {
 288                pr_debug("couldn't register xt target\n");
 289                goto out_dev;
 290        }
 291
 292        return 0;
 293out_dev:
 294        device_destroy(idletimer_tg_class, MKDEV(0, 0));
 295out_class:
 296        class_destroy(idletimer_tg_class);
 297out:
 298        return err;
 299}
 300
 301static void __exit idletimer_tg_exit(void)
 302{
 303        xt_unregister_target(&idletimer_tg);
 304
 305        device_destroy(idletimer_tg_class, MKDEV(0, 0));
 306        class_destroy(idletimer_tg_class);
 307}
 308
 309module_init(idletimer_tg_init);
 310module_exit(idletimer_tg_exit);
 311
 312MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
 313MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
 314MODULE_DESCRIPTION("Xtables: idle time monitor");
 315MODULE_LICENSE("GPL v2");
 316MODULE_ALIAS("ipt_IDLETIMER");
 317MODULE_ALIAS("ip6t_IDLETIMER");
 318