xt_LED.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(unsigned long data)
  77. {
  78. struct xt_led_info_internal *ledinternal = (struct xt_led_info_internal *)data;
  79. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  80. }
  81. static struct xt_led_info_internal *led_trigger_lookup(const char *name)
  82. {
  83. struct xt_led_info_internal *ledinternal;
  84. list_for_each_entry(ledinternal, &xt_led_triggers, list) {
  85. if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
  86. return ledinternal;
  87. }
  88. }
  89. return NULL;
  90. }
  91. static int led_tg_check(const struct xt_tgchk_param *par)
  92. {
  93. struct xt_led_info *ledinfo = par->targinfo;
  94. struct xt_led_info_internal *ledinternal;
  95. int err;
  96. if (ledinfo->id[0] == '\0') {
  97. pr_info("No 'id' parameter given.\n");
  98. return -EINVAL;
  99. }
  100. mutex_lock(&xt_led_mutex);
  101. ledinternal = led_trigger_lookup(ledinfo->id);
  102. if (ledinternal) {
  103. ledinternal->refcnt++;
  104. goto out;
  105. }
  106. err = -ENOMEM;
  107. ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
  108. if (!ledinternal)
  109. goto exit_mutex_only;
  110. ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
  111. if (!ledinternal->trigger_id)
  112. goto exit_internal_alloc;
  113. ledinternal->refcnt = 1;
  114. ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
  115. err = led_trigger_register(&ledinternal->netfilter_led_trigger);
  116. if (err) {
  117. pr_err("Trigger name is already in use.\n");
  118. goto exit_alloc;
  119. }
  120. /* Since the letinternal timer can be shared between multiple targets,
  121. * always set it up, even if the current target does not need it
  122. */
  123. setup_timer(&ledinternal->timer, led_timeout_callback,
  124. (unsigned long)ledinternal);
  125. list_add_tail(&ledinternal->list, &xt_led_triggers);
  126. out:
  127. mutex_unlock(&xt_led_mutex);
  128. ledinfo->internal_data = ledinternal;
  129. return 0;
  130. exit_alloc:
  131. kfree(ledinternal->trigger_id);
  132. exit_internal_alloc:
  133. kfree(ledinternal);
  134. exit_mutex_only:
  135. mutex_unlock(&xt_led_mutex);
  136. return err;
  137. }
  138. static void led_tg_destroy(const struct xt_tgdtor_param *par)
  139. {
  140. const struct xt_led_info *ledinfo = par->targinfo;
  141. struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
  142. mutex_lock(&xt_led_mutex);
  143. if (--ledinternal->refcnt) {
  144. mutex_unlock(&xt_led_mutex);
  145. return;
  146. }
  147. list_del(&ledinternal->list);
  148. del_timer_sync(&ledinternal->timer);
  149. led_trigger_unregister(&ledinternal->netfilter_led_trigger);
  150. mutex_unlock(&xt_led_mutex);
  151. kfree(ledinternal->trigger_id);
  152. kfree(ledinternal);
  153. }
  154. static struct xt_target led_tg_reg __read_mostly = {
  155. .name = "LED",
  156. .revision = 0,
  157. .family = NFPROTO_UNSPEC,
  158. .target = led_tg,
  159. .targetsize = sizeof(struct xt_led_info),
  160. .checkentry = led_tg_check,
  161. .destroy = led_tg_destroy,
  162. .me = THIS_MODULE,
  163. };
  164. static int __init led_tg_init(void)
  165. {
  166. return xt_register_target(&led_tg_reg);
  167. }
  168. static void __exit led_tg_exit(void)
  169. {
  170. xt_unregister_target(&led_tg_reg);
  171. }
  172. module_init(led_tg_init);
  173. module_exit(led_tg_exit);