xt_LED.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * xt_LED.c - netfilter target to make LEDs blink upon packet matches
  3. *
  4. * Copyright (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netfilter/x_tables.h>
  25. #include <linux/slab.h>
  26. #include <linux/leds.h>
  27. #include <linux/mutex.h>
  28. #include <linux/netfilter/xt_LED.h>
  29. MODULE_LICENSE("GPL");
  30. MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>");
  31. MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match");
  32. MODULE_ALIAS("ipt_LED");
  33. MODULE_ALIAS("ip6t_LED");
  34. static LIST_HEAD(xt_led_triggers);
  35. static DEFINE_MUTEX(xt_led_mutex);
  36. /*
  37. * This is declared in here (the kernel module) only, to avoid having these
  38. * dependencies in userspace code. This is what xt_led_info.internal_data
  39. * points to.
  40. */
  41. struct xt_led_info_internal {
  42. struct list_head list;
  43. int refcnt;
  44. char *trigger_id;
  45. struct led_trigger netfilter_led_trigger;
  46. struct timer_list timer;
  47. };
  48. #define XT_LED_BLINK_DELAY 50 /* ms */
  49. static unsigned int
  50. led_tg(struct sk_buff *skb, const struct xt_action_param *par)
  51. {
  52. const struct xt_led_info *ledinfo = par->targinfo;
  53. struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
  54. unsigned long led_delay = XT_LED_BLINK_DELAY;
  55. /*
  56. * If "always blink" is enabled, and there's still some time until the
  57. * LED will switch off, briefly switch it off now.
  58. */
  59. if ((ledinfo->delay > 0) && ledinfo->always_blink &&
  60. timer_pending(&ledinternal->timer))
  61. led_trigger_blink_oneshot(&ledinternal->netfilter_led_trigger,
  62. &led_delay, &led_delay, 1);
  63. else
  64. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
  65. /* If there's a positive delay, start/update the timer */
  66. if (ledinfo->delay > 0) {
  67. mod_timer(&ledinternal->timer,
  68. jiffies + msecs_to_jiffies(ledinfo->delay));
  69. /* Otherwise if there was no delay given, blink as fast as possible */
  70. } else if (ledinfo->delay == 0) {
  71. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  72. }
  73. /* else the delay is negative, which means switch on and stay on */
  74. return XT_CONTINUE;
  75. }
  76. static void led_timeout_callback(struct timer_list *t)
  77. {
  78. struct xt_led_info_internal *ledinternal = from_timer(ledinternal, t,
  79. timer);
  80. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  81. }
  82. static struct xt_led_info_internal *led_trigger_lookup(const char *name)
  83. {
  84. struct xt_led_info_internal *ledinternal;
  85. list_for_each_entry(ledinternal, &xt_led_triggers, list) {
  86. if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
  87. return ledinternal;
  88. }
  89. }
  90. return NULL;
  91. }
  92. static int led_tg_check(const struct xt_tgchk_param *par)
  93. {
  94. struct xt_led_info *ledinfo = par->targinfo;
  95. struct xt_led_info_internal *ledinternal;
  96. int err;
  97. if (ledinfo->id[0] == '\0')
  98. return -EINVAL;
  99. mutex_lock(&xt_led_mutex);
  100. ledinternal = led_trigger_lookup(ledinfo->id);
  101. if (ledinternal) {
  102. ledinternal->refcnt++;
  103. goto out;
  104. }
  105. err = -ENOMEM;
  106. ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
  107. if (!ledinternal)
  108. goto exit_mutex_only;
  109. ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
  110. if (!ledinternal->trigger_id)
  111. goto exit_internal_alloc;
  112. ledinternal->refcnt = 1;
  113. ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
  114. err = led_trigger_register(&ledinternal->netfilter_led_trigger);
  115. if (err) {
  116. pr_info_ratelimited("Trigger name is already in use.\n");
  117. goto exit_alloc;
  118. }
  119. /* Since the letinternal timer can be shared between multiple targets,
  120. * always set it up, even if the current target does not need it
  121. */
  122. timer_setup(&ledinternal->timer, led_timeout_callback, 0);
  123. list_add_tail(&ledinternal->list, &xt_led_triggers);
  124. out:
  125. mutex_unlock(&xt_led_mutex);
  126. ledinfo->internal_data = ledinternal;
  127. return 0;
  128. exit_alloc:
  129. kfree(ledinternal->trigger_id);
  130. exit_internal_alloc:
  131. kfree(ledinternal);
  132. exit_mutex_only:
  133. mutex_unlock(&xt_led_mutex);
  134. return err;
  135. }
  136. static void led_tg_destroy(const struct xt_tgdtor_param *par)
  137. {
  138. const struct xt_led_info *ledinfo = par->targinfo;
  139. struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
  140. mutex_lock(&xt_led_mutex);
  141. if (--ledinternal->refcnt) {
  142. mutex_unlock(&xt_led_mutex);
  143. return;
  144. }
  145. list_del(&ledinternal->list);
  146. del_timer_sync(&ledinternal->timer);
  147. led_trigger_unregister(&ledinternal->netfilter_led_trigger);
  148. mutex_unlock(&xt_led_mutex);
  149. kfree(ledinternal->trigger_id);
  150. kfree(ledinternal);
  151. }
  152. static struct xt_target led_tg_reg __read_mostly = {
  153. .name = "LED",
  154. .revision = 0,
  155. .family = NFPROTO_UNSPEC,
  156. .target = led_tg,
  157. .targetsize = sizeof(struct xt_led_info),
  158. .usersize = offsetof(struct xt_led_info, internal_data),
  159. .checkentry = led_tg_check,
  160. .destroy = led_tg_destroy,
  161. .me = THIS_MODULE,
  162. };
  163. static int __init led_tg_init(void)
  164. {
  165. return xt_register_target(&led_tg_reg);
  166. }
  167. static void __exit led_tg_exit(void)
  168. {
  169. xt_unregister_target(&led_tg_reg);
  170. }
  171. module_init(led_tg_init);
  172. module_exit(led_tg_exit);