xt_IDLETIMER.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/netfilter/xt_IDLETIMER.c
  4. *
  5. * Netfilter module to trigger a timer when packet matches.
  6. * After timer expires a kevent will be sent.
  7. *
  8. * Copyright (C) 2004, 2010 Nokia Corporation
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/timer.h>
  19. #include <linux/alarmtimer.h>
  20. #include <linux/list.h>
  21. #include <linux/mutex.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/netfilter/x_tables.h>
  24. #include <linux/netfilter/xt_IDLETIMER.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/kobject.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/suspend.h>
  30. #include <net/sock.h>
  31. #include <net/inet_sock.h>
  32. #define NLMSG_MAX_SIZE 64
  33. struct idletimer_tg {
  34. struct list_head entry;
  35. struct alarm alarm;
  36. struct timer_list timer;
  37. struct work_struct work;
  38. struct kobject *kobj;
  39. struct device_attribute attr;
  40. struct timespec64 delayed_timer_trigger;
  41. struct timespec64 last_modified_timer;
  42. struct timespec64 last_suspend_time;
  43. struct notifier_block pm_nb;
  44. int timeout;
  45. unsigned int refcnt;
  46. u8 timer_type;
  47. bool work_pending;
  48. bool send_nl_msg;
  49. bool active;
  50. uid_t uid;
  51. bool suspend_time_valid;
  52. };
  53. static LIST_HEAD(idletimer_tg_list);
  54. static DEFINE_MUTEX(list_mutex);
  55. static DEFINE_SPINLOCK(timestamp_lock);
  56. static struct kobject *idletimer_tg_kobj;
  57. static bool check_for_delayed_trigger(struct idletimer_tg *timer,
  58. struct timespec64 *ts)
  59. {
  60. bool state;
  61. struct timespec64 temp;
  62. spin_lock_bh(&timestamp_lock);
  63. timer->work_pending = false;
  64. if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
  65. timer->delayed_timer_trigger.tv_sec != 0) {
  66. state = false;
  67. temp.tv_sec = timer->timeout;
  68. temp.tv_nsec = 0;
  69. if (timer->delayed_timer_trigger.tv_sec != 0) {
  70. temp = timespec64_add(timer->delayed_timer_trigger,
  71. temp);
  72. ts->tv_sec = temp.tv_sec;
  73. ts->tv_nsec = temp.tv_nsec;
  74. timer->delayed_timer_trigger.tv_sec = 0;
  75. timer->work_pending = true;
  76. schedule_work(&timer->work);
  77. } else {
  78. temp = timespec64_add(timer->last_modified_timer, temp);
  79. ts->tv_sec = temp.tv_sec;
  80. ts->tv_nsec = temp.tv_nsec;
  81. }
  82. } else {
  83. state = timer->active;
  84. }
  85. spin_unlock_bh(&timestamp_lock);
  86. return state;
  87. }
  88. static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
  89. {
  90. char iface_msg[NLMSG_MAX_SIZE];
  91. char state_msg[NLMSG_MAX_SIZE];
  92. char timestamp_msg[NLMSG_MAX_SIZE];
  93. char uid_msg[NLMSG_MAX_SIZE];
  94. char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL };
  95. int res;
  96. struct timespec64 ts;
  97. u64 time_ns;
  98. bool state;
  99. res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
  100. iface);
  101. if (NLMSG_MAX_SIZE <= res) {
  102. pr_err("message too long (%d)\n", res);
  103. return;
  104. }
  105. ts = ktime_to_timespec64(ktime_get_boottime());
  106. state = check_for_delayed_trigger(timer, &ts);
  107. res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
  108. state ? "active" : "inactive");
  109. if (NLMSG_MAX_SIZE <= res) {
  110. pr_err("message too long (%d)\n", res);
  111. return;
  112. }
  113. if (state) {
  114. res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid);
  115. if (NLMSG_MAX_SIZE <= res)
  116. pr_err("message too long (%d)\n", res);
  117. } else {
  118. res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
  119. if (NLMSG_MAX_SIZE <= res)
  120. pr_err("message too long (%d)\n", res);
  121. }
  122. time_ns = timespec64_to_ns(&ts);
  123. res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
  124. if (NLMSG_MAX_SIZE <= res) {
  125. timestamp_msg[0] = '\0';
  126. pr_err("message too long (%d)\n", res);
  127. }
  128. pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg,
  129. timestamp_msg, uid_msg);
  130. kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
  131. return;
  132. }
  133. static
  134. struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
  135. {
  136. struct idletimer_tg *entry;
  137. list_for_each_entry(entry, &idletimer_tg_list, entry) {
  138. if (!strcmp(label, entry->attr.attr.name))
  139. return entry;
  140. }
  141. return NULL;
  142. }
  143. static ssize_t idletimer_tg_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct idletimer_tg *timer;
  147. unsigned long expires = 0;
  148. struct timespec64 ktimespec = {};
  149. long time_diff = 0;
  150. unsigned long now = jiffies;
  151. mutex_lock(&list_mutex);
  152. timer = __idletimer_tg_find_by_label(attr->attr.name);
  153. if (timer) {
  154. if (timer->timer_type & XT_IDLETIMER_ALARM) {
  155. ktime_t expires_alarm = alarm_expires_remaining(&timer->alarm);
  156. ktimespec = ktime_to_timespec64(expires_alarm);
  157. time_diff = ktimespec.tv_sec;
  158. } else {
  159. expires = timer->timer.expires;
  160. time_diff = jiffies_to_msecs(expires - now) / 1000;
  161. }
  162. }
  163. mutex_unlock(&list_mutex);
  164. if (time_after(expires, now) || ktimespec.tv_sec > 0)
  165. return scnprintf(buf, PAGE_SIZE, "%ld\n", time_diff);
  166. if (timer->send_nl_msg)
  167. return scnprintf(buf, PAGE_SIZE, "0 %d\n",
  168. jiffies_to_msecs(now - expires) / 1000);
  169. return scnprintf(buf, PAGE_SIZE, "0\n");
  170. }
  171. static void idletimer_tg_work(struct work_struct *work)
  172. {
  173. struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
  174. work);
  175. sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
  176. if (timer->send_nl_msg)
  177. notify_netlink_uevent(timer->attr.attr.name, timer);
  178. }
  179. static void idletimer_tg_expired(struct timer_list *t)
  180. {
  181. struct idletimer_tg *timer = from_timer(timer, t, timer);
  182. pr_debug("timer %s expired\n", timer->attr.attr.name);
  183. spin_lock_bh(&timestamp_lock);
  184. timer->active = false;
  185. timer->work_pending = true;
  186. schedule_work(&timer->work);
  187. spin_unlock_bh(&timestamp_lock);
  188. }
  189. static int idletimer_resume(struct notifier_block *notifier,
  190. unsigned long pm_event, void *unused)
  191. {
  192. struct timespec64 ts;
  193. unsigned long time_diff, now = jiffies;
  194. struct idletimer_tg *timer = container_of(notifier,
  195. struct idletimer_tg, pm_nb);
  196. if (!timer)
  197. return NOTIFY_DONE;
  198. switch (pm_event) {
  199. case PM_SUSPEND_PREPARE:
  200. timer->last_suspend_time =
  201. ktime_to_timespec64(ktime_get_boottime());
  202. timer->suspend_time_valid = true;
  203. break;
  204. case PM_POST_SUSPEND:
  205. if (!timer->suspend_time_valid)
  206. break;
  207. timer->suspend_time_valid = false;
  208. spin_lock_bh(&timestamp_lock);
  209. if (!timer->active) {
  210. spin_unlock_bh(&timestamp_lock);
  211. break;
  212. }
  213. /* since jiffies are not updated when suspended now represents
  214. * the time it would have suspended */
  215. if (time_after(timer->timer.expires, now)) {
  216. ts = ktime_to_timespec64(ktime_get_boottime());
  217. ts = timespec64_sub(ts, timer->last_suspend_time);
  218. time_diff = timespec64_to_jiffies(&ts);
  219. if (timer->timer.expires > (time_diff + now)) {
  220. mod_timer_pending(&timer->timer,
  221. (timer->timer.expires - time_diff));
  222. } else {
  223. del_timer(&timer->timer);
  224. timer->timer.expires = 0;
  225. timer->active = false;
  226. timer->work_pending = true;
  227. schedule_work(&timer->work);
  228. }
  229. }
  230. spin_unlock_bh(&timestamp_lock);
  231. break;
  232. default:
  233. break;
  234. }
  235. return NOTIFY_DONE;
  236. }
  237. static enum alarmtimer_restart idletimer_tg_alarmproc(struct alarm *alarm,
  238. ktime_t now)
  239. {
  240. struct idletimer_tg *timer = alarm->data;
  241. pr_debug("alarm %s expired\n", timer->attr.attr.name);
  242. schedule_work(&timer->work);
  243. return ALARMTIMER_NORESTART;
  244. }
  245. static int idletimer_check_sysfs_name(const char *name, unsigned int size)
  246. {
  247. int ret;
  248. ret = xt_check_proc_name(name, size);
  249. if (ret < 0)
  250. return ret;
  251. if (!strcmp(name, "power") ||
  252. !strcmp(name, "subsystem") ||
  253. !strcmp(name, "uevent"))
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static int idletimer_tg_create(struct idletimer_tg_info *info)
  258. {
  259. int ret;
  260. info->timer = kzalloc(sizeof(*info->timer), GFP_KERNEL);
  261. if (!info->timer) {
  262. ret = -ENOMEM;
  263. goto out;
  264. }
  265. ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
  266. if (ret < 0)
  267. goto out_free_timer;
  268. sysfs_attr_init(&info->timer->attr.attr);
  269. info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
  270. if (!info->timer->attr.attr.name) {
  271. ret = -ENOMEM;
  272. goto out_free_timer;
  273. }
  274. info->timer->attr.attr.mode = 0444;
  275. info->timer->attr.show = idletimer_tg_show;
  276. ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
  277. if (ret < 0) {
  278. pr_debug("couldn't add file to sysfs\n");
  279. goto out_free_attr;
  280. }
  281. list_add(&info->timer->entry, &idletimer_tg_list);
  282. pr_debug("timer type value is 0.\n");
  283. info->timer->timer_type = 0;
  284. info->timer->refcnt = 1;
  285. info->timer->send_nl_msg = false;
  286. info->timer->active = true;
  287. info->timer->timeout = info->timeout;
  288. info->timer->delayed_timer_trigger.tv_sec = 0;
  289. info->timer->delayed_timer_trigger.tv_nsec = 0;
  290. info->timer->work_pending = false;
  291. info->timer->uid = 0;
  292. info->timer->last_modified_timer =
  293. ktime_to_timespec64(ktime_get_boottime());
  294. info->timer->pm_nb.notifier_call = idletimer_resume;
  295. ret = register_pm_notifier(&info->timer->pm_nb);
  296. if (ret)
  297. printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
  298. __func__, ret);
  299. INIT_WORK(&info->timer->work, idletimer_tg_work);
  300. timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
  301. mod_timer(&info->timer->timer,
  302. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  303. return 0;
  304. out_free_attr:
  305. kfree(info->timer->attr.attr.name);
  306. out_free_timer:
  307. kfree(info->timer);
  308. out:
  309. return ret;
  310. }
  311. static int idletimer_tg_create_v1(struct idletimer_tg_info_v1 *info)
  312. {
  313. int ret;
  314. info->timer = kzalloc(sizeof(*info->timer), GFP_KERNEL);
  315. if (!info->timer) {
  316. ret = -ENOMEM;
  317. goto out;
  318. }
  319. ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
  320. if (ret < 0)
  321. goto out_free_timer;
  322. sysfs_attr_init(&info->timer->attr.attr);
  323. info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
  324. if (!info->timer->attr.attr.name) {
  325. ret = -ENOMEM;
  326. goto out_free_timer;
  327. }
  328. info->timer->attr.attr.mode = 0444;
  329. info->timer->attr.show = idletimer_tg_show;
  330. ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
  331. if (ret < 0) {
  332. pr_debug("couldn't add file to sysfs\n");
  333. goto out_free_attr;
  334. }
  335. /* notify userspace */
  336. kobject_uevent(idletimer_tg_kobj,KOBJ_ADD);
  337. list_add(&info->timer->entry, &idletimer_tg_list);
  338. pr_debug("timer type value is %u\n", info->timer_type);
  339. info->timer->timer_type = info->timer_type;
  340. info->timer->refcnt = 1;
  341. info->timer->send_nl_msg = (info->send_nl_msg != 0);
  342. info->timer->active = true;
  343. info->timer->timeout = info->timeout;
  344. info->timer->delayed_timer_trigger.tv_sec = 0;
  345. info->timer->delayed_timer_trigger.tv_nsec = 0;
  346. info->timer->work_pending = false;
  347. info->timer->uid = 0;
  348. info->timer->last_modified_timer =
  349. ktime_to_timespec64(ktime_get_boottime());
  350. info->timer->pm_nb.notifier_call = idletimer_resume;
  351. ret = register_pm_notifier(&info->timer->pm_nb);
  352. if (ret)
  353. printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
  354. __func__, ret);
  355. INIT_WORK(&info->timer->work, idletimer_tg_work);
  356. if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
  357. ktime_t tout;
  358. alarm_init(&info->timer->alarm, ALARM_BOOTTIME,
  359. idletimer_tg_alarmproc);
  360. info->timer->alarm.data = info->timer;
  361. tout = ktime_set(info->timeout, 0);
  362. alarm_start_relative(&info->timer->alarm, tout);
  363. } else {
  364. timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
  365. mod_timer(&info->timer->timer,
  366. msecs_to_jiffies(info->timeout * 1000) + jiffies);
  367. }
  368. return 0;
  369. out_free_attr:
  370. kfree(info->timer->attr.attr.name);
  371. out_free_timer:
  372. kfree(info->timer);
  373. out:
  374. return ret;
  375. }
  376. static void reset_timer(struct idletimer_tg * const info_timer,
  377. const __u32 info_timeout,
  378. struct sk_buff *skb)
  379. {
  380. unsigned long now = jiffies;
  381. bool timer_prev;
  382. spin_lock_bh(&timestamp_lock);
  383. timer_prev = info_timer->active;
  384. info_timer->active = true;
  385. /* timer_prev is used to guard overflow problem in time_before*/
  386. if (!timer_prev || time_before(info_timer->timer.expires, now)) {
  387. pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
  388. info_timer->timer.expires, now);
  389. /* Stores the uid resposible for waking up the radio */
  390. if (skb && (skb->sk)) {
  391. info_timer->uid = from_kuid_munged(current_user_ns(),
  392. sock_i_uid(skb_to_full_sk(skb)));
  393. }
  394. /* checks if there is a pending inactive notification*/
  395. if (info_timer->work_pending)
  396. info_timer->delayed_timer_trigger = info_timer->last_modified_timer;
  397. else {
  398. info_timer->work_pending = true;
  399. schedule_work(&info_timer->work);
  400. }
  401. }
  402. info_timer->last_modified_timer = ktime_to_timespec64(ktime_get_boottime());
  403. mod_timer(&info_timer->timer, msecs_to_jiffies(info_timeout * 1000) + now);
  404. spin_unlock_bh(&timestamp_lock);
  405. }
  406. /*
  407. * The actual xt_tables plugin.
  408. */
  409. static unsigned int idletimer_tg_target(struct sk_buff *skb,
  410. const struct xt_action_param *par)
  411. {
  412. const struct idletimer_tg_info *info = par->targinfo;
  413. unsigned long now = jiffies;
  414. pr_debug("resetting timer %s, timeout period %u\n",
  415. info->label, info->timeout);
  416. info->timer->active = true;
  417. if (time_before(info->timer->timer.expires, now)) {
  418. schedule_work(&info->timer->work);
  419. pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
  420. info->label, info->timer->timer.expires, now);
  421. }
  422. /* TODO: Avoid modifying timers on each packet */
  423. reset_timer(info->timer, info->timeout, skb);
  424. return XT_CONTINUE;
  425. }
  426. /*
  427. * The actual xt_tables plugin.
  428. */
  429. static unsigned int idletimer_tg_target_v1(struct sk_buff *skb,
  430. const struct xt_action_param *par)
  431. {
  432. const struct idletimer_tg_info_v1 *info = par->targinfo;
  433. unsigned long now = jiffies;
  434. pr_debug("resetting timer %s, timeout period %u\n",
  435. info->label, info->timeout);
  436. if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
  437. ktime_t tout = ktime_set(info->timeout, 0);
  438. alarm_start_relative(&info->timer->alarm, tout);
  439. } else {
  440. info->timer->active = true;
  441. if (time_before(info->timer->timer.expires, now)) {
  442. schedule_work(&info->timer->work);
  443. pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
  444. info->label, info->timer->timer.expires, now);
  445. }
  446. /* TODO: Avoid modifying timers on each packet */
  447. reset_timer(info->timer, info->timeout, skb);
  448. }
  449. return XT_CONTINUE;
  450. }
  451. static int idletimer_tg_helper(struct idletimer_tg_info *info)
  452. {
  453. if (info->timeout == 0) {
  454. pr_debug("timeout value is zero\n");
  455. return -EINVAL;
  456. }
  457. if (info->timeout >= INT_MAX / 1000) {
  458. pr_debug("timeout value is too big\n");
  459. return -EINVAL;
  460. }
  461. if (info->label[0] == '\0' ||
  462. strnlen(info->label,
  463. MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
  464. pr_debug("label is empty or not nul-terminated\n");
  465. return -EINVAL;
  466. }
  467. return 0;
  468. }
  469. static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
  470. {
  471. struct idletimer_tg_info *info = par->targinfo;
  472. int ret;
  473. pr_debug("checkentry targinfo%s\n", info->label);
  474. ret = idletimer_tg_helper(info);
  475. if(ret < 0)
  476. {
  477. pr_debug("checkentry helper return invalid\n");
  478. return -EINVAL;
  479. }
  480. mutex_lock(&list_mutex);
  481. info->timer = __idletimer_tg_find_by_label(info->label);
  482. if (info->timer) {
  483. info->timer->refcnt++;
  484. reset_timer(info->timer, info->timeout, NULL);
  485. pr_debug("increased refcnt of timer %s to %u\n",
  486. info->label, info->timer->refcnt);
  487. } else {
  488. ret = idletimer_tg_create(info);
  489. if (ret < 0) {
  490. pr_debug("failed to create timer\n");
  491. mutex_unlock(&list_mutex);
  492. return ret;
  493. }
  494. }
  495. mutex_unlock(&list_mutex);
  496. return 0;
  497. }
  498. static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
  499. {
  500. struct idletimer_tg_info_v1 *info = par->targinfo;
  501. int ret;
  502. pr_debug("checkentry targinfo%s\n", info->label);
  503. ret = idletimer_tg_helper((struct idletimer_tg_info *)info);
  504. if(ret < 0)
  505. {
  506. pr_debug("checkentry helper return invalid\n");
  507. return -EINVAL;
  508. }
  509. if (info->timer_type > XT_IDLETIMER_ALARM) {
  510. pr_debug("invalid value for timer type\n");
  511. return -EINVAL;
  512. }
  513. if (info->send_nl_msg > 1) {
  514. pr_debug("invalid value for send_nl_msg\n");
  515. return -EINVAL;
  516. }
  517. mutex_lock(&list_mutex);
  518. info->timer = __idletimer_tg_find_by_label(info->label);
  519. if (info->timer) {
  520. if (info->timer->timer_type != info->timer_type) {
  521. pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
  522. mutex_unlock(&list_mutex);
  523. return -EINVAL;
  524. }
  525. info->timer->refcnt++;
  526. if (info->timer_type & XT_IDLETIMER_ALARM) {
  527. /* calculate remaining expiry time */
  528. ktime_t tout = alarm_expires_remaining(&info->timer->alarm);
  529. struct timespec64 ktimespec = ktime_to_timespec64(tout);
  530. if (ktimespec.tv_sec > 0) {
  531. pr_debug("time_expiry_remaining %lld\n",
  532. ktimespec.tv_sec);
  533. alarm_start_relative(&info->timer->alarm, tout);
  534. }
  535. } else {
  536. reset_timer(info->timer, info->timeout, NULL);
  537. }
  538. pr_debug("increased refcnt of timer %s to %u\n",
  539. info->label, info->timer->refcnt);
  540. } else {
  541. ret = idletimer_tg_create_v1(info);
  542. if (ret < 0) {
  543. pr_debug("failed to create timer\n");
  544. mutex_unlock(&list_mutex);
  545. return ret;
  546. }
  547. }
  548. mutex_unlock(&list_mutex);
  549. return 0;
  550. }
  551. static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
  552. {
  553. const struct idletimer_tg_info *info = par->targinfo;
  554. pr_debug("destroy targinfo %s\n", info->label);
  555. mutex_lock(&list_mutex);
  556. if (--info->timer->refcnt == 0) {
  557. pr_debug("deleting timer %s\n", info->label);
  558. list_del(&info->timer->entry);
  559. del_timer_sync(&info->timer->timer);
  560. sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
  561. unregister_pm_notifier(&info->timer->pm_nb);
  562. cancel_work_sync(&info->timer->work);
  563. kfree(info->timer->attr.attr.name);
  564. kfree(info->timer);
  565. } else {
  566. pr_debug("decreased refcnt of timer %s to %u\n",
  567. info->label, info->timer->refcnt);
  568. }
  569. mutex_unlock(&list_mutex);
  570. }
  571. static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
  572. {
  573. const struct idletimer_tg_info_v1 *info = par->targinfo;
  574. pr_debug("destroy targinfo %s\n", info->label);
  575. mutex_lock(&list_mutex);
  576. if (--info->timer->refcnt == 0) {
  577. pr_debug("deleting timer %s\n", info->label);
  578. list_del(&info->timer->entry);
  579. if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
  580. alarm_cancel(&info->timer->alarm);
  581. } else {
  582. del_timer_sync(&info->timer->timer);
  583. }
  584. sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
  585. unregister_pm_notifier(&info->timer->pm_nb);
  586. cancel_work_sync(&info->timer->work);
  587. kfree(info->timer->attr.attr.name);
  588. kfree(info->timer);
  589. } else {
  590. pr_debug("decreased refcnt of timer %s to %u\n",
  591. info->label, info->timer->refcnt);
  592. }
  593. mutex_unlock(&list_mutex);
  594. }
  595. static struct xt_target idletimer_tg[] __read_mostly = {
  596. {
  597. .name = "IDLETIMER",
  598. .family = NFPROTO_UNSPEC,
  599. .target = idletimer_tg_target,
  600. .targetsize = sizeof(struct idletimer_tg_info),
  601. .usersize = offsetof(struct idletimer_tg_info, timer),
  602. .checkentry = idletimer_tg_checkentry,
  603. .destroy = idletimer_tg_destroy,
  604. .me = THIS_MODULE,
  605. },
  606. {
  607. .name = "IDLETIMER",
  608. .family = NFPROTO_UNSPEC,
  609. .revision = 1,
  610. .target = idletimer_tg_target_v1,
  611. .targetsize = sizeof(struct idletimer_tg_info_v1),
  612. .usersize = offsetof(struct idletimer_tg_info_v1, timer),
  613. .checkentry = idletimer_tg_checkentry_v1,
  614. .destroy = idletimer_tg_destroy_v1,
  615. .me = THIS_MODULE,
  616. },
  617. };
  618. static struct class *idletimer_tg_class;
  619. static struct device *idletimer_tg_device;
  620. static int __init idletimer_tg_init(void)
  621. {
  622. int err;
  623. idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
  624. err = PTR_ERR(idletimer_tg_class);
  625. if (IS_ERR(idletimer_tg_class)) {
  626. pr_debug("couldn't register device class\n");
  627. goto out;
  628. }
  629. idletimer_tg_device = device_create(idletimer_tg_class, NULL,
  630. MKDEV(0, 0), NULL, "timers");
  631. err = PTR_ERR(idletimer_tg_device);
  632. if (IS_ERR(idletimer_tg_device)) {
  633. pr_debug("couldn't register system device\n");
  634. goto out_class;
  635. }
  636. idletimer_tg_kobj = &idletimer_tg_device->kobj;
  637. err = xt_register_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
  638. if (err < 0) {
  639. pr_debug("couldn't register xt target\n");
  640. goto out_dev;
  641. }
  642. return 0;
  643. out_dev:
  644. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  645. out_class:
  646. class_destroy(idletimer_tg_class);
  647. out:
  648. return err;
  649. }
  650. static void __exit idletimer_tg_exit(void)
  651. {
  652. xt_unregister_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
  653. device_destroy(idletimer_tg_class, MKDEV(0, 0));
  654. class_destroy(idletimer_tg_class);
  655. }
  656. module_init(idletimer_tg_init);
  657. module_exit(idletimer_tg_exit);
  658. MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
  659. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  660. MODULE_DESCRIPTION("Xtables: idle time monitor");
  661. MODULE_LICENSE("GPL v2");
  662. MODULE_ALIAS("ipt_IDLETIMER");
  663. MODULE_ALIAS("ip6t_IDLETIMER");
  664. MODULE_ALIAS("arpt_IDLETIMER");