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_tg_create(struct idletimer_tg_info *info) 118 { 119 int ret; 120 121 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL); 122 if (!info->timer) { 123 ret = -ENOMEM; 124 goto out; 125 } 126 127 sysfs_attr_init(&info->timer->attr.attr); 128 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL); 129 if (!info->timer->attr.attr.name) { 130 ret = -ENOMEM; 131 goto out_free_timer; 132 } 133 info->timer->attr.attr.mode = 0444; 134 info->timer->attr.show = idletimer_tg_show; 135 136 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr); 137 if (ret < 0) { 138 pr_debug("couldn't add file to sysfs"); 139 goto out_free_attr; 140 } 141 142 list_add(&info->timer->entry, &idletimer_tg_list); 143 144 timer_setup(&info->timer->timer, idletimer_tg_expired, 0); 145 info->timer->refcnt = 1; 146 147 INIT_WORK(&info->timer->work, idletimer_tg_work); 148 149 mod_timer(&info->timer->timer, 150 msecs_to_jiffies(info->timeout * 1000) + jiffies); 151 152 return 0; 153 154 out_free_attr: 155 kfree(info->timer->attr.attr.name); 156 out_free_timer: 157 kfree(info->timer); 158 out: 159 return ret; 160 } 161 162 /* 163 * The actual xt_tables plugin. 164 */ 165 static unsigned int idletimer_tg_target(struct sk_buff *skb, 166 const struct xt_action_param *par) 167 { 168 const struct idletimer_tg_info *info = par->targinfo; 169 170 pr_debug("resetting timer %s, timeout period %u\n", 171 info->label, info->timeout); 172 173 mod_timer(&info->timer->timer, 174 msecs_to_jiffies(info->timeout * 1000) + jiffies); 175 176 return XT_CONTINUE; 177 } 178 179 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par) 180 { 181 struct idletimer_tg_info *info = par->targinfo; 182 int ret; 183 184 pr_debug("checkentry targinfo%s\n", info->label); 185 186 if (info->timeout == 0) { 187 pr_debug("timeout value is zero\n"); 188 return -EINVAL; 189 } 190 if (info->timeout >= INT_MAX / 1000) { 191 pr_debug("timeout value is too big\n"); 192 return -EINVAL; 193 } 194 if (info->label[0] == '\0' || 195 strnlen(info->label, 196 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) { 197 pr_debug("label is empty or not nul-terminated\n"); 198 return -EINVAL; 199 } 200 201 mutex_lock(&list_mutex); 202 203 info->timer = __idletimer_tg_find_by_label(info->label); 204 if (info->timer) { 205 info->timer->refcnt++; 206 mod_timer(&info->timer->timer, 207 msecs_to_jiffies(info->timeout * 1000) + jiffies); 208 209 pr_debug("increased refcnt of timer %s to %u\n", 210 info->label, info->timer->refcnt); 211 } else { 212 ret = idletimer_tg_create(info); 213 if (ret < 0) { 214 pr_debug("failed to create timer\n"); 215 mutex_unlock(&list_mutex); 216 return ret; 217 } 218 } 219 220 mutex_unlock(&list_mutex); 221 return 0; 222 } 223 224 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par) 225 { 226 const struct idletimer_tg_info *info = par->targinfo; 227 228 pr_debug("destroy targinfo %s\n", info->label); 229 230 mutex_lock(&list_mutex); 231 232 if (--info->timer->refcnt == 0) { 233 pr_debug("deleting timer %s\n", info->label); 234 235 list_del(&info->timer->entry); 236 del_timer_sync(&info->timer->timer); 237 cancel_work_sync(&info->timer->work); 238 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr); 239 kfree(info->timer->attr.attr.name); 240 kfree(info->timer); 241 } else { 242 pr_debug("decreased refcnt of timer %s to %u\n", 243 info->label, info->timer->refcnt); 244 } 245 246 mutex_unlock(&list_mutex); 247 } 248 249 static struct xt_target idletimer_tg __read_mostly = { 250 .name = "IDLETIMER", 251 .family = NFPROTO_UNSPEC, 252 .target = idletimer_tg_target, 253 .targetsize = sizeof(struct idletimer_tg_info), 254 .usersize = offsetof(struct idletimer_tg_info, timer), 255 .checkentry = idletimer_tg_checkentry, 256 .destroy = idletimer_tg_destroy, 257 .me = THIS_MODULE, 258 }; 259 260 static struct class *idletimer_tg_class; 261 262 static struct device *idletimer_tg_device; 263 264 static int __init idletimer_tg_init(void) 265 { 266 int err; 267 268 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer"); 269 err = PTR_ERR(idletimer_tg_class); 270 if (IS_ERR(idletimer_tg_class)) { 271 pr_debug("couldn't register device class\n"); 272 goto out; 273 } 274 275 idletimer_tg_device = device_create(idletimer_tg_class, NULL, 276 MKDEV(0, 0), NULL, "timers"); 277 err = PTR_ERR(idletimer_tg_device); 278 if (IS_ERR(idletimer_tg_device)) { 279 pr_debug("couldn't register system device\n"); 280 goto out_class; 281 } 282 283 idletimer_tg_kobj = &idletimer_tg_device->kobj; 284 285 err = xt_register_target(&idletimer_tg); 286 if (err < 0) { 287 pr_debug("couldn't register xt target\n"); 288 goto out_dev; 289 } 290 291 return 0; 292 out_dev: 293 device_destroy(idletimer_tg_class, MKDEV(0, 0)); 294 out_class: 295 class_destroy(idletimer_tg_class); 296 out: 297 return err; 298 } 299 300 static void __exit idletimer_tg_exit(void) 301 { 302 xt_unregister_target(&idletimer_tg); 303 304 device_destroy(idletimer_tg_class, MKDEV(0, 0)); 305 class_destroy(idletimer_tg_class); 306 } 307 308 module_init(idletimer_tg_init); 309 module_exit(idletimer_tg_exit); 310 311 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>"); 312 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>"); 313 MODULE_DESCRIPTION("Xtables: idle time monitor"); 314 MODULE_LICENSE("GPL v2"); 315 MODULE_ALIAS("ipt_IDLETIMER"); 316 MODULE_ALIAS("ip6t_IDLETIMER"); 317