xt_LED.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static unsigned int
  49. led_tg(struct sk_buff *skb, const struct xt_action_param *par)
  50. {
  51. const struct xt_led_info *ledinfo = par->targinfo;
  52. struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
  53. /*
  54. * If "always blink" is enabled, and there's still some time until the
  55. * LED will switch off, briefly switch it off now.
  56. */
  57. if ((ledinfo->delay > 0) && ledinfo->always_blink &&
  58. timer_pending(&ledinternal->timer))
  59. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  60. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
  61. /* If there's a positive delay, start/update the timer */
  62. if (ledinfo->delay > 0) {
  63. mod_timer(&ledinternal->timer,
  64. jiffies + msecs_to_jiffies(ledinfo->delay));
  65. /* Otherwise if there was no delay given, blink as fast as possible */
  66. } else if (ledinfo->delay == 0) {
  67. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  68. }
  69. /* else the delay is negative, which means switch on and stay on */
  70. return XT_CONTINUE;
  71. }
  72. static void led_timeout_callback(unsigned long data)
  73. {
  74. struct xt_led_info_internal *ledinternal = (struct xt_led_info_internal *)data;
  75. led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
  76. }
  77. static struct xt_led_info_internal *led_trigger_lookup(const char *name)
  78. {
  79. struct xt_led_info_internal *ledinternal;
  80. list_for_each_entry(ledinternal, &xt_led_triggers, list) {
  81. if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
  82. return ledinternal;
  83. }
  84. }
  85. return NULL;
  86. }
  87. static int led_tg_check(const struct xt_tgchk_param *par)
  88. {
  89. struct xt_led_info *ledinfo = par->targinfo;
  90. struct xt_led_info_internal *ledinternal;
  91. int err;
  92. if (ledinfo->id[0] == '\0') {
  93. pr_info("No 'id' parameter given.\n");
  94. return -EINVAL;
  95. }
  96. mutex_lock(&xt_led_mutex);
  97. ledinternal = led_trigger_lookup(ledinfo->id);
  98. if (ledinternal) {
  99. ledinternal->refcnt++;
  100. goto out;
  101. }
  102. err = -ENOMEM;
  103. ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
  104. if (!ledinternal)
  105. goto exit_mutex_only;
  106. ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
  107. if (!ledinternal->trigger_id)
  108. goto exit_internal_alloc;
  109. ledinternal->refcnt = 1;
  110. ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
  111. err = led_trigger_register(&ledinternal->netfilter_led_trigger);
  112. if (err) {
  113. pr_warning("led_trigger_register() failed\n");
  114. if (err == -EEXIST)
  115. pr_warning("Trigger name is already in use.\n");
  116. goto exit_alloc;
  117. }
  118. /* See if we need to set up a timer */
  119. if (ledinfo->delay > 0)
  120. setup_timer(&ledinternal->timer, led_timeout_callback,
  121. (unsigned long)ledinternal);
  122. list_add_tail(&ledinternal->list, &xt_led_triggers);
  123. out:
  124. mutex_unlock(&xt_led_mutex);
  125. ledinfo->internal_data = ledinternal;
  126. return 0;
  127. exit_alloc:
  128. kfree(ledinternal->trigger_id);
  129. exit_internal_alloc:
  130. kfree(ledinternal);
  131. exit_mutex_only:
  132. mutex_unlock(&xt_led_mutex);
  133. return err;
  134. }
  135. static void led_tg_destroy(const struct xt_tgdtor_param *par)
  136. {
  137. const struct xt_led_info *ledinfo = par->targinfo;
  138. struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
  139. mutex_lock(&xt_led_mutex);
  140. if (--ledinternal->refcnt) {
  141. mutex_unlock(&xt_led_mutex);
  142. return;
  143. }
  144. list_del(&ledinternal->list);
  145. if (ledinfo->delay > 0)
  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. .checkentry = led_tg_check,
  159. .destroy = led_tg_destroy,
  160. .me = THIS_MODULE,
  161. };
  162. static int __init led_tg_init(void)
  163. {
  164. return xt_register_target(&led_tg_reg);
  165. }
  166. static void __exit led_tg_exit(void)
  167. {
  168. xt_unregister_target(&led_tg_reg);
  169. }
  170. module_init(led_tg_init);
  171. module_exit(led_tg_exit);