xt_IDLETIMER.c 8.1 KB

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