xt_IDLETIMER.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_tg_create(struct idletimer_tg_info *info)
  96. {
  97. int ret;
  98. info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
  99. if (!info->timer) {
  100. ret = -ENOMEM;
  101. goto out;
  102. }
  103. sysfs_attr_init(&info->timer->attr.attr);
  104. info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
  105. if (!info->timer->attr.attr.name) {
  106. ret = -ENOMEM;
  107. goto out_free_timer;
  108. }
  109. info->timer->attr.attr.mode = S_IRUGO;
  110. info->timer->attr.show = idletimer_tg_show;
  111. ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
  112. if (ret < 0) {
  113. pr_debug("couldn't add file to sysfs");
  114. goto out_free_attr;
  115. }
  116. list_add(&info->timer->entry, &idletimer_tg_list);
  117. setup_timer(&info->timer->timer, idletimer_tg_expired,
  118. (unsigned long) info->timer);
  119. info->timer->refcnt = 1;
  120. INIT_WORK(&info->timer->work, idletimer_tg_work);
  121. mod_timer(&info->timer->timer,
  122. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  123. return 0;
  124. out_free_attr:
  125. kfree(info->timer->attr.attr.name);
  126. out_free_timer:
  127. kfree(info->timer);
  128. out:
  129. return ret;
  130. }
  131. /*
  132. * The actual xt_tables plugin.
  133. */
  134. static unsigned int idletimer_tg_target(struct sk_buff *skb,
  135. const struct xt_action_param *par)
  136. {
  137. const struct idletimer_tg_info *info = par->targinfo;
  138. pr_debug("resetting timer %s, timeout period %u\n",
  139. info->label, info->timeout);
  140. BUG_ON(!info->timer);
  141. mod_timer(&info->timer->timer,
  142. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  143. return XT_CONTINUE;
  144. }
  145. static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
  146. {
  147. struct idletimer_tg_info *info = par->targinfo;
  148. int ret;
  149. pr_debug("checkentry targinfo%s\n", info->label);
  150. if (info->timeout == 0) {
  151. pr_debug("timeout value is zero\n");
  152. return -EINVAL;
  153. }
  154. if (info->timeout >= INT_MAX / 1000) {
  155. pr_debug("timeout value is too big\n");
  156. return -EINVAL;
  157. }
  158. if (info->label[0] == '\0' ||
  159. strnlen(info->label,
  160. MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
  161. pr_debug("label is empty or not nul-terminated\n");
  162. return -EINVAL;
  163. }
  164. mutex_lock(&list_mutex);
  165. info->timer = __idletimer_tg_find_by_label(info->label);
  166. if (info->timer) {
  167. info->timer->refcnt++;
  168. mod_timer(&info->timer->timer,
  169. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  170. pr_debug("increased refcnt of timer %s to %u\n",
  171. info->label, info->timer->refcnt);
  172. } else {
  173. ret = idletimer_tg_create(info);
  174. if (ret < 0) {
  175. pr_debug("failed to create timer\n");
  176. mutex_unlock(&list_mutex);
  177. return ret;
  178. }
  179. }
  180. mutex_unlock(&list_mutex);
  181. return 0;
  182. }
  183. static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
  184. {
  185. const struct idletimer_tg_info *info = par->targinfo;
  186. pr_debug("destroy targinfo %s\n", info->label);
  187. mutex_lock(&list_mutex);
  188. if (--info->timer->refcnt == 0) {
  189. pr_debug("deleting timer %s\n", info->label);
  190. list_del(&info->timer->entry);
  191. del_timer_sync(&info->timer->timer);
  192. cancel_work_sync(&info->timer->work);
  193. sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
  194. kfree(info->timer->attr.attr.name);
  195. kfree(info->timer);
  196. } else {
  197. pr_debug("decreased refcnt of timer %s to %u\n",
  198. info->label, info->timer->refcnt);
  199. }
  200. mutex_unlock(&list_mutex);
  201. }
  202. static struct xt_target idletimer_tg __read_mostly = {
  203. .name = "IDLETIMER",
  204. .family = NFPROTO_UNSPEC,
  205. .target = idletimer_tg_target,
  206. .targetsize = sizeof(struct idletimer_tg_info),
  207. .checkentry = idletimer_tg_checkentry,
  208. .destroy = idletimer_tg_destroy,
  209. .me = THIS_MODULE,
  210. };
  211. static struct class *idletimer_tg_class;
  212. static struct device *idletimer_tg_device;
  213. static int __init idletimer_tg_init(void)
  214. {
  215. int err;
  216. idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
  217. err = PTR_ERR(idletimer_tg_class);
  218. if (IS_ERR(idletimer_tg_class)) {
  219. pr_debug("couldn't register device class\n");
  220. goto out;
  221. }
  222. idletimer_tg_device = device_create(idletimer_tg_class, NULL,
  223. MKDEV(0, 0), NULL, "timers");
  224. err = PTR_ERR(idletimer_tg_device);
  225. if (IS_ERR(idletimer_tg_device)) {
  226. pr_debug("couldn't register system device\n");
  227. goto out_class;
  228. }
  229. idletimer_tg_kobj = &idletimer_tg_device->kobj;
  230. err = xt_register_target(&idletimer_tg);
  231. if (err < 0) {
  232. pr_debug("couldn't register xt target\n");
  233. goto out_dev;
  234. }
  235. return 0;
  236. out_dev:
  237. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  238. out_class:
  239. class_destroy(idletimer_tg_class);
  240. out:
  241. return err;
  242. }
  243. static void __exit idletimer_tg_exit(void)
  244. {
  245. xt_unregister_target(&idletimer_tg);
  246. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  247. class_destroy(idletimer_tg_class);
  248. }
  249. module_init(idletimer_tg_init);
  250. module_exit(idletimer_tg_exit);
  251. MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
  252. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  253. MODULE_DESCRIPTION("Xtables: idle time monitor");
  254. MODULE_LICENSE("GPL v2");
  255. MODULE_ALIAS("ipt_IDLETIMER");
  256. MODULE_ALIAS("ip6t_IDLETIMER");