xt_IDLETIMER.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. *
  9. * Written by Timo Teras <ext-timo.teras@nokia.com>
  10. *
  11. * Converted to x_tables and reworked for upstream inclusion
  12. * by Luciano Coelho <luciano.coelho@nokia.com>
  13. *
  14. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * version 2 as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  28. * 02110-1301 USA
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/module.h>
  32. #include <linux/timer.h>
  33. #include <linux/list.h>
  34. #include <linux/mutex.h>
  35. #include <linux/netfilter.h>
  36. #include <linux/netfilter/x_tables.h>
  37. #include <linux/netfilter/xt_IDLETIMER.h>
  38. #include <linux/kdev_t.h>
  39. #include <linux/kobject.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/sysfs.h>
  43. #include <net/net_namespace.h>
  44. struct idletimer_tg_attr {
  45. struct attribute attr;
  46. ssize_t (*show)(struct kobject *kobj,
  47. struct attribute *attr, char *buf);
  48. };
  49. struct idletimer_tg {
  50. struct list_head entry;
  51. struct timer_list timer;
  52. struct work_struct work;
  53. struct kobject *kobj;
  54. struct idletimer_tg_attr attr;
  55. unsigned int refcnt;
  56. bool send_nl_msg;
  57. bool active;
  58. };
  59. static LIST_HEAD(idletimer_tg_list);
  60. static DEFINE_MUTEX(list_mutex);
  61. static struct kobject *idletimer_tg_kobj;
  62. static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
  63. {
  64. char iface_msg[NLMSG_MAX_SIZE];
  65. char state_msg[NLMSG_MAX_SIZE];
  66. char *envp[] = { iface_msg, state_msg, NULL };
  67. int res;
  68. res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
  69. iface);
  70. if (NLMSG_MAX_SIZE <= res) {
  71. pr_err("message too long (%d)", res);
  72. return;
  73. }
  74. res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
  75. timer->active ? "active" : "inactive");
  76. if (NLMSG_MAX_SIZE <= res) {
  77. pr_err("message too long (%d)", res);
  78. return;
  79. }
  80. pr_debug("putting nlmsg: <%s> <%s>\n", iface_msg, state_msg);
  81. kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
  82. return;
  83. }
  84. static
  85. struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
  86. {
  87. struct idletimer_tg *entry;
  88. BUG_ON(!label);
  89. list_for_each_entry(entry, &idletimer_tg_list, entry) {
  90. if (!strcmp(label, entry->attr.attr.name))
  91. return entry;
  92. }
  93. return NULL;
  94. }
  95. static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
  96. char *buf)
  97. {
  98. struct idletimer_tg *timer;
  99. unsigned long expires = 0;
  100. unsigned long now = jiffies;
  101. mutex_lock(&list_mutex);
  102. timer = __idletimer_tg_find_by_label(attr->name);
  103. if (timer)
  104. expires = timer->timer.expires;
  105. mutex_unlock(&list_mutex);
  106. if (time_after(expires, now))
  107. return sprintf(buf, "%u\n",
  108. jiffies_to_msecs(expires - now) / 1000);
  109. if (timer->send_nl_msg)
  110. return sprintf(buf, "0 %d\n",
  111. jiffies_to_msecs(now - expires) / 1000);
  112. else
  113. return sprintf(buf, "0\n");
  114. }
  115. static void idletimer_tg_work(struct work_struct *work)
  116. {
  117. struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
  118. work);
  119. sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
  120. if (timer->send_nl_msg)
  121. notify_netlink_uevent(timer->attr.attr.name, timer);
  122. }
  123. static void idletimer_tg_expired(unsigned long data)
  124. {
  125. struct idletimer_tg *timer = (struct idletimer_tg *) data;
  126. pr_debug("timer %s expired\n", timer->attr.attr.name);
  127. timer->active = false;
  128. schedule_work(&timer->work);
  129. }
  130. static int idletimer_tg_create(struct idletimer_tg_info *info)
  131. {
  132. int ret;
  133. info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
  134. if (!info->timer) {
  135. pr_debug("couldn't alloc timer\n");
  136. ret = -ENOMEM;
  137. goto out;
  138. }
  139. info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
  140. if (!info->timer->attr.attr.name) {
  141. pr_debug("couldn't alloc attribute name\n");
  142. ret = -ENOMEM;
  143. goto out_free_timer;
  144. }
  145. info->timer->attr.attr.mode = S_IRUGO;
  146. info->timer->attr.show = idletimer_tg_show;
  147. ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
  148. if (ret < 0) {
  149. pr_debug("couldn't add file to sysfs");
  150. goto out_free_attr;
  151. }
  152. list_add(&info->timer->entry, &idletimer_tg_list);
  153. setup_timer(&info->timer->timer, idletimer_tg_expired,
  154. (unsigned long) info->timer);
  155. info->timer->refcnt = 1;
  156. info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
  157. info->timer->active = true;
  158. mod_timer(&info->timer->timer,
  159. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  160. INIT_WORK(&info->timer->work, idletimer_tg_work);
  161. return 0;
  162. out_free_attr:
  163. kfree(info->timer->attr.attr.name);
  164. out_free_timer:
  165. kfree(info->timer);
  166. out:
  167. return ret;
  168. }
  169. /*
  170. * The actual xt_tables plugin.
  171. */
  172. static unsigned int idletimer_tg_target(struct sk_buff *skb,
  173. const struct xt_action_param *par)
  174. {
  175. const struct idletimer_tg_info *info = par->targinfo;
  176. unsigned long now = jiffies;
  177. pr_debug("resetting timer %s, timeout period %u\n",
  178. info->label, info->timeout);
  179. BUG_ON(!info->timer);
  180. info->timer->active = true;
  181. if (time_before(info->timer->timer.expires, now)) {
  182. schedule_work(&info->timer->work);
  183. pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
  184. info->label, info->timer->timer.expires, now);
  185. }
  186. /* TODO: Avoid modifying timers on each packet */
  187. mod_timer(&info->timer->timer,
  188. msecs_to_jiffies(info->timeout * 1000) + now);
  189. return XT_CONTINUE;
  190. }
  191. static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
  192. {
  193. struct idletimer_tg_info *info = par->targinfo;
  194. int ret;
  195. unsigned long now = jiffies;
  196. pr_debug("checkentry targinfo %s\n", info->label);
  197. if (info->timeout == 0) {
  198. pr_debug("timeout value is zero\n");
  199. return -EINVAL;
  200. }
  201. if (info->label[0] == '\0' ||
  202. strnlen(info->label,
  203. MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
  204. pr_debug("label is empty or not nul-terminated\n");
  205. return -EINVAL;
  206. }
  207. mutex_lock(&list_mutex);
  208. info->timer = __idletimer_tg_find_by_label(info->label);
  209. if (info->timer) {
  210. info->timer->refcnt++;
  211. info->timer->active = true;
  212. if (time_before(info->timer->timer.expires, now)) {
  213. schedule_work(&info->timer->work);
  214. pr_debug("Starting Checkentry timer"
  215. "(Expired, Jiffies): %lu, %lu\n",
  216. info->timer->timer.expires, now);
  217. }
  218. mod_timer(&info->timer->timer,
  219. msecs_to_jiffies(info->timeout * 1000) + now);
  220. pr_debug("increased refcnt of timer %s to %u\n",
  221. info->label, info->timer->refcnt);
  222. } else {
  223. ret = idletimer_tg_create(info);
  224. if (ret < 0) {
  225. pr_debug("failed to create timer\n");
  226. mutex_unlock(&list_mutex);
  227. return ret;
  228. }
  229. }
  230. mutex_unlock(&list_mutex);
  231. return 0;
  232. }
  233. static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
  234. {
  235. const struct idletimer_tg_info *info = par->targinfo;
  236. pr_debug("destroy targinfo %s\n", info->label);
  237. mutex_lock(&list_mutex);
  238. if (--info->timer->refcnt == 0) {
  239. pr_debug("deleting timer %s\n", info->label);
  240. list_del(&info->timer->entry);
  241. del_timer_sync(&info->timer->timer);
  242. sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
  243. kfree(info->timer->attr.attr.name);
  244. kfree(info->timer);
  245. } else {
  246. pr_debug("decreased refcnt of timer %s to %u\n",
  247. info->label, info->timer->refcnt);
  248. }
  249. mutex_unlock(&list_mutex);
  250. }
  251. static struct xt_target idletimer_tg __read_mostly = {
  252. .name = "IDLETIMER",
  253. .revision = 1,
  254. .family = NFPROTO_UNSPEC,
  255. .target = idletimer_tg_target,
  256. .targetsize = sizeof(struct idletimer_tg_info),
  257. .checkentry = idletimer_tg_checkentry,
  258. .destroy = idletimer_tg_destroy,
  259. .me = THIS_MODULE,
  260. };
  261. static struct class *idletimer_tg_class;
  262. static struct device *idletimer_tg_device;
  263. static int __init idletimer_tg_init(void)
  264. {
  265. int err;
  266. idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
  267. err = PTR_ERR(idletimer_tg_class);
  268. if (IS_ERR(idletimer_tg_class)) {
  269. pr_debug("couldn't register device class\n");
  270. goto out;
  271. }
  272. idletimer_tg_device = device_create(idletimer_tg_class, NULL,
  273. MKDEV(0, 0), NULL, "timers");
  274. err = PTR_ERR(idletimer_tg_device);
  275. if (IS_ERR(idletimer_tg_device)) {
  276. pr_debug("couldn't register system device\n");
  277. goto out_class;
  278. }
  279. idletimer_tg_kobj = &idletimer_tg_device->kobj;
  280. err = xt_register_target(&idletimer_tg);
  281. if (err < 0) {
  282. pr_debug("couldn't register xt target\n");
  283. goto out_dev;
  284. }
  285. return 0;
  286. out_dev:
  287. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  288. out_class:
  289. class_destroy(idletimer_tg_class);
  290. out:
  291. return err;
  292. }
  293. static void __exit idletimer_tg_exit(void)
  294. {
  295. xt_unregister_target(&idletimer_tg);
  296. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  297. class_destroy(idletimer_tg_class);
  298. }
  299. module_init(idletimer_tg_init);
  300. module_exit(idletimer_tg_exit);
  301. MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
  302. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  303. MODULE_DESCRIPTION("Xtables: idle time monitor");
  304. MODULE_LICENSE("GPL v2");
  305. MODULE_ALIAS("ipt_IDLETIMER");
  306. MODULE_ALIAS("ip6t_IDLETIMER");
  307. MODULE_ALIAS("arpt_IDLETIMER");