xt_IDLETIMER.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <linux/rtc.h>
  44. #include <linux/time.h>
  45. #include <linux/math64.h>
  46. #include <linux/suspend.h>
  47. #include <linux/notifier.h>
  48. #include <net/net_namespace.h>
  49. #include <net/sock.h>
  50. struct idletimer_tg_attr {
  51. struct attribute attr;
  52. ssize_t (*show)(struct kobject *kobj,
  53. struct attribute *attr, char *buf);
  54. };
  55. struct idletimer_tg {
  56. struct list_head entry;
  57. struct timer_list timer;
  58. struct work_struct work;
  59. struct kobject *kobj;
  60. struct idletimer_tg_attr attr;
  61. struct timespec delayed_timer_trigger;
  62. struct timespec last_modified_timer;
  63. struct timespec last_suspend_time;
  64. struct notifier_block pm_nb;
  65. int timeout;
  66. unsigned int refcnt;
  67. bool work_pending;
  68. bool send_nl_msg;
  69. bool active;
  70. uid_t uid;
  71. };
  72. static LIST_HEAD(idletimer_tg_list);
  73. static DEFINE_MUTEX(list_mutex);
  74. static DEFINE_SPINLOCK(timestamp_lock);
  75. static struct kobject *idletimer_tg_kobj;
  76. static bool check_for_delayed_trigger(struct idletimer_tg *timer,
  77. struct timespec *ts)
  78. {
  79. bool state;
  80. struct timespec temp;
  81. spin_lock_bh(&timestamp_lock);
  82. timer->work_pending = false;
  83. if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
  84. timer->delayed_timer_trigger.tv_sec != 0) {
  85. state = false;
  86. temp.tv_sec = timer->timeout;
  87. temp.tv_nsec = 0;
  88. if (timer->delayed_timer_trigger.tv_sec != 0) {
  89. temp = timespec_add(timer->delayed_timer_trigger, temp);
  90. ts->tv_sec = temp.tv_sec;
  91. ts->tv_nsec = temp.tv_nsec;
  92. timer->delayed_timer_trigger.tv_sec = 0;
  93. timer->work_pending = true;
  94. schedule_work(&timer->work);
  95. } else {
  96. temp = timespec_add(timer->last_modified_timer, temp);
  97. ts->tv_sec = temp.tv_sec;
  98. ts->tv_nsec = temp.tv_nsec;
  99. }
  100. } else {
  101. state = timer->active;
  102. }
  103. spin_unlock_bh(&timestamp_lock);
  104. return state;
  105. }
  106. static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
  107. {
  108. char iface_msg[NLMSG_MAX_SIZE];
  109. char state_msg[NLMSG_MAX_SIZE];
  110. char timestamp_msg[NLMSG_MAX_SIZE];
  111. char uid_msg[NLMSG_MAX_SIZE];
  112. char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL };
  113. int res;
  114. struct timespec ts;
  115. uint64_t time_ns;
  116. bool state;
  117. res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
  118. iface);
  119. if (NLMSG_MAX_SIZE <= res) {
  120. pr_err("message too long (%d)", res);
  121. return;
  122. }
  123. get_monotonic_boottime(&ts);
  124. state = check_for_delayed_trigger(timer, &ts);
  125. res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
  126. state ? "active" : "inactive");
  127. if (NLMSG_MAX_SIZE <= res) {
  128. pr_err("message too long (%d)", res);
  129. return;
  130. }
  131. if (state) {
  132. res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid);
  133. if (NLMSG_MAX_SIZE <= res)
  134. pr_err("message too long (%d)", res);
  135. } else {
  136. res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
  137. if (NLMSG_MAX_SIZE <= res)
  138. pr_err("message too long (%d)", res);
  139. }
  140. time_ns = timespec_to_ns(&ts);
  141. res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
  142. if (NLMSG_MAX_SIZE <= res) {
  143. timestamp_msg[0] = '\0';
  144. pr_err("message too long (%d)", res);
  145. }
  146. pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg,
  147. timestamp_msg, uid_msg);
  148. kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
  149. return;
  150. }
  151. static
  152. struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
  153. {
  154. struct idletimer_tg *entry;
  155. BUG_ON(!label);
  156. list_for_each_entry(entry, &idletimer_tg_list, entry) {
  157. if (!strcmp(label, entry->attr.attr.name))
  158. return entry;
  159. }
  160. return NULL;
  161. }
  162. static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
  163. char *buf)
  164. {
  165. struct idletimer_tg *timer;
  166. unsigned long expires = 0;
  167. unsigned long now = jiffies;
  168. mutex_lock(&list_mutex);
  169. timer = __idletimer_tg_find_by_label(attr->name);
  170. if (timer)
  171. expires = timer->timer.expires;
  172. mutex_unlock(&list_mutex);
  173. if (time_after(expires, now))
  174. return sprintf(buf, "%u\n",
  175. jiffies_to_msecs(expires - now) / 1000);
  176. if (timer->send_nl_msg)
  177. return sprintf(buf, "0 %d\n",
  178. jiffies_to_msecs(now - expires) / 1000);
  179. else
  180. return sprintf(buf, "0\n");
  181. }
  182. static void idletimer_tg_work(struct work_struct *work)
  183. {
  184. struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
  185. work);
  186. sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
  187. if (timer->send_nl_msg)
  188. notify_netlink_uevent(timer->attr.attr.name, timer);
  189. }
  190. static void idletimer_tg_expired(unsigned long data)
  191. {
  192. struct idletimer_tg *timer = (struct idletimer_tg *) data;
  193. pr_debug("timer %s expired\n", timer->attr.attr.name);
  194. spin_lock_bh(&timestamp_lock);
  195. timer->active = false;
  196. timer->work_pending = true;
  197. schedule_work(&timer->work);
  198. spin_unlock_bh(&timestamp_lock);
  199. }
  200. static int idletimer_resume(struct notifier_block *notifier,
  201. unsigned long pm_event, void *unused)
  202. {
  203. struct timespec ts;
  204. unsigned long time_diff, now = jiffies;
  205. struct idletimer_tg *timer = container_of(notifier,
  206. struct idletimer_tg, pm_nb);
  207. if (!timer)
  208. return NOTIFY_DONE;
  209. switch (pm_event) {
  210. case PM_SUSPEND_PREPARE:
  211. get_monotonic_boottime(&timer->last_suspend_time);
  212. break;
  213. case PM_POST_SUSPEND:
  214. spin_lock_bh(&timestamp_lock);
  215. if (!timer->active) {
  216. spin_unlock_bh(&timestamp_lock);
  217. break;
  218. }
  219. /* since jiffies are not updated when suspended now represents
  220. * the time it would have suspended */
  221. if (time_after(timer->timer.expires, now)) {
  222. get_monotonic_boottime(&ts);
  223. ts = timespec_sub(ts, timer->last_suspend_time);
  224. time_diff = timespec_to_jiffies(&ts);
  225. if (timer->timer.expires > (time_diff + now)) {
  226. mod_timer_pending(&timer->timer,
  227. (timer->timer.expires - time_diff));
  228. } else {
  229. del_timer(&timer->timer);
  230. timer->timer.expires = 0;
  231. timer->active = false;
  232. timer->work_pending = true;
  233. schedule_work(&timer->work);
  234. }
  235. }
  236. spin_unlock_bh(&timestamp_lock);
  237. break;
  238. default:
  239. break;
  240. }
  241. return NOTIFY_DONE;
  242. }
  243. static int idletimer_tg_create(struct idletimer_tg_info *info)
  244. {
  245. int ret;
  246. info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
  247. if (!info->timer) {
  248. ret = -ENOMEM;
  249. goto out;
  250. }
  251. info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
  252. if (!info->timer->attr.attr.name) {
  253. ret = -ENOMEM;
  254. goto out_free_timer;
  255. }
  256. info->timer->attr.attr.mode = S_IRUGO;
  257. info->timer->attr.show = idletimer_tg_show;
  258. ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
  259. if (ret < 0) {
  260. pr_debug("couldn't add file to sysfs");
  261. goto out_free_attr;
  262. }
  263. list_add(&info->timer->entry, &idletimer_tg_list);
  264. setup_timer(&info->timer->timer, idletimer_tg_expired,
  265. (unsigned long) info->timer);
  266. info->timer->refcnt = 1;
  267. info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
  268. info->timer->active = true;
  269. info->timer->timeout = info->timeout;
  270. info->timer->delayed_timer_trigger.tv_sec = 0;
  271. info->timer->delayed_timer_trigger.tv_nsec = 0;
  272. info->timer->work_pending = false;
  273. info->timer->uid = 0;
  274. get_monotonic_boottime(&info->timer->last_modified_timer);
  275. info->timer->pm_nb.notifier_call = idletimer_resume;
  276. ret = register_pm_notifier(&info->timer->pm_nb);
  277. if (ret)
  278. printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
  279. __func__, ret);
  280. mod_timer(&info->timer->timer,
  281. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  282. INIT_WORK(&info->timer->work, idletimer_tg_work);
  283. return 0;
  284. out_free_attr:
  285. kfree(info->timer->attr.attr.name);
  286. out_free_timer:
  287. kfree(info->timer);
  288. out:
  289. return ret;
  290. }
  291. static void reset_timer(const struct idletimer_tg_info *info,
  292. struct sk_buff *skb)
  293. {
  294. unsigned long now = jiffies;
  295. struct idletimer_tg *timer = info->timer;
  296. bool timer_prev;
  297. spin_lock_bh(&timestamp_lock);
  298. timer_prev = timer->active;
  299. timer->active = true;
  300. /* timer_prev is used to guard overflow problem in time_before*/
  301. if (!timer_prev || time_before(timer->timer.expires, now)) {
  302. pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
  303. timer->timer.expires, now);
  304. /* Stores the uid resposible for waking up the radio */
  305. if (skb && (skb->sk)) {
  306. timer->uid = from_kuid_munged(current_user_ns(),
  307. sock_i_uid(skb->sk));
  308. }
  309. /* checks if there is a pending inactive notification*/
  310. if (timer->work_pending)
  311. timer->delayed_timer_trigger = timer->last_modified_timer;
  312. else {
  313. timer->work_pending = true;
  314. schedule_work(&timer->work);
  315. }
  316. }
  317. get_monotonic_boottime(&timer->last_modified_timer);
  318. mod_timer(&timer->timer,
  319. msecs_to_jiffies(info->timeout * 1000) + now);
  320. spin_unlock_bh(&timestamp_lock);
  321. }
  322. /*
  323. * The actual xt_tables plugin.
  324. */
  325. static unsigned int idletimer_tg_target(struct sk_buff *skb,
  326. const struct xt_action_param *par)
  327. {
  328. const struct idletimer_tg_info *info = par->targinfo;
  329. unsigned long now = jiffies;
  330. pr_debug("resetting timer %s, timeout period %u\n",
  331. info->label, info->timeout);
  332. BUG_ON(!info->timer);
  333. info->timer->active = true;
  334. if (time_before(info->timer->timer.expires, now)) {
  335. schedule_work(&info->timer->work);
  336. pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
  337. info->label, info->timer->timer.expires, now);
  338. }
  339. /* TODO: Avoid modifying timers on each packet */
  340. reset_timer(info, skb);
  341. return XT_CONTINUE;
  342. }
  343. static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
  344. {
  345. struct idletimer_tg_info *info = par->targinfo;
  346. int ret;
  347. pr_debug("checkentry targinfo %s\n", info->label);
  348. if (info->timeout == 0) {
  349. pr_debug("timeout value is zero\n");
  350. return -EINVAL;
  351. }
  352. if (info->label[0] == '\0' ||
  353. strnlen(info->label,
  354. MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
  355. pr_debug("label is empty or not nul-terminated\n");
  356. return -EINVAL;
  357. }
  358. mutex_lock(&list_mutex);
  359. info->timer = __idletimer_tg_find_by_label(info->label);
  360. if (info->timer) {
  361. info->timer->refcnt++;
  362. reset_timer(info, NULL);
  363. pr_debug("increased refcnt of timer %s to %u\n",
  364. info->label, info->timer->refcnt);
  365. } else {
  366. ret = idletimer_tg_create(info);
  367. if (ret < 0) {
  368. pr_debug("failed to create timer\n");
  369. mutex_unlock(&list_mutex);
  370. return ret;
  371. }
  372. }
  373. mutex_unlock(&list_mutex);
  374. return 0;
  375. }
  376. static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
  377. {
  378. const struct idletimer_tg_info *info = par->targinfo;
  379. pr_debug("destroy targinfo %s\n", info->label);
  380. mutex_lock(&list_mutex);
  381. if (--info->timer->refcnt == 0) {
  382. pr_debug("deleting timer %s\n", info->label);
  383. list_del(&info->timer->entry);
  384. del_timer_sync(&info->timer->timer);
  385. sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
  386. unregister_pm_notifier(&info->timer->pm_nb);
  387. kfree(info->timer->attr.attr.name);
  388. kfree(info->timer);
  389. } else {
  390. pr_debug("decreased refcnt of timer %s to %u\n",
  391. info->label, info->timer->refcnt);
  392. }
  393. mutex_unlock(&list_mutex);
  394. }
  395. static struct xt_target idletimer_tg __read_mostly = {
  396. .name = "IDLETIMER",
  397. .revision = 1,
  398. .family = NFPROTO_UNSPEC,
  399. .target = idletimer_tg_target,
  400. .targetsize = sizeof(struct idletimer_tg_info),
  401. .checkentry = idletimer_tg_checkentry,
  402. .destroy = idletimer_tg_destroy,
  403. .me = THIS_MODULE,
  404. };
  405. static struct class *idletimer_tg_class;
  406. static struct device *idletimer_tg_device;
  407. static int __init idletimer_tg_init(void)
  408. {
  409. int err;
  410. idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
  411. err = PTR_ERR(idletimer_tg_class);
  412. if (IS_ERR(idletimer_tg_class)) {
  413. pr_debug("couldn't register device class\n");
  414. goto out;
  415. }
  416. idletimer_tg_device = device_create(idletimer_tg_class, NULL,
  417. MKDEV(0, 0), NULL, "timers");
  418. err = PTR_ERR(idletimer_tg_device);
  419. if (IS_ERR(idletimer_tg_device)) {
  420. pr_debug("couldn't register system device\n");
  421. goto out_class;
  422. }
  423. idletimer_tg_kobj = &idletimer_tg_device->kobj;
  424. err = xt_register_target(&idletimer_tg);
  425. if (err < 0) {
  426. pr_debug("couldn't register xt target\n");
  427. goto out_dev;
  428. }
  429. return 0;
  430. out_dev:
  431. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  432. out_class:
  433. class_destroy(idletimer_tg_class);
  434. out:
  435. return err;
  436. }
  437. static void __exit idletimer_tg_exit(void)
  438. {
  439. xt_unregister_target(&idletimer_tg);
  440. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  441. class_destroy(idletimer_tg_class);
  442. }
  443. module_init(idletimer_tg_init);
  444. module_exit(idletimer_tg_exit);
  445. MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
  446. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  447. MODULE_DESCRIPTION("Xtables: idle time monitor");
  448. MODULE_LICENSE("GPL v2");
  449. MODULE_ALIAS("ipt_IDLETIMER");
  450. MODULE_ALIAS("ip6t_IDLETIMER");
  451. MODULE_ALIAS("arpt_IDLETIMER");