Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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