1 /*
2 * libxt_LED.c - shared library add-on to iptables to add customized LED
3 * trigger support.
4 *
5 * (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <xtables.h>
16 #include <linux/netfilter/xt_LED.h>
17
18 enum {
19 O_LED_TRIGGER_ID = 0,
20 O_LED_DELAY,
21 O_LED_ALWAYS_BLINK,
22 };
23
24 #define s struct xt_led_info
25 static const struct xt_option_entry LED_opts[] = {
26 {.name = "led-trigger-id", .id = O_LED_TRIGGER_ID,
27 .flags = XTOPT_MAND, .type = XTTYPE_STRING, .min = 0,
28 .max = sizeof(((struct xt_led_info *)NULL)->id) -
29 sizeof("netfilter-")},
30 {.name = "led-delay", .id = O_LED_DELAY, .type = XTTYPE_STRING},
31 {.name = "led-always-blink", .id = O_LED_ALWAYS_BLINK,
32 .type = XTTYPE_NONE},
33 XTOPT_TABLEEND,
34 };
35 #undef s
36
LED_help(void)37 static void LED_help(void)
38 {
39 printf(
40 "LED target options:\n"
41 "--led-trigger-id name suffix for led trigger name\n"
42 "--led-delay ms leave the LED on for this number of\n"
43 " milliseconds after triggering.\n"
44 "--led-always-blink blink on arriving packets, even if\n"
45 " the LED is already on.\n"
46 );
47 }
48
LED_parse(struct xt_option_call * cb)49 static void LED_parse(struct xt_option_call *cb)
50 {
51 struct xt_led_info *led = cb->data;
52
53 xtables_option_parse(cb);
54 switch (cb->entry->id) {
55 case O_LED_TRIGGER_ID:
56 snprintf(led->id, sizeof(led->id), "netfilter-%s", cb->arg);
57 break;
58 case O_LED_DELAY:
59 if (strncasecmp(cb->arg, "inf", 3) == 0)
60 led->delay = -1;
61 else if (!xtables_strtoui(cb->arg, NULL, &led->delay, 0, UINT32_MAX))
62 xtables_error(PARAMETER_PROBLEM,
63 "Delay value must be within range 0..%u",
64 UINT32_MAX);
65 break;
66 case O_LED_ALWAYS_BLINK:
67 led->always_blink = 1;
68 break;
69 }
70 }
71
LED_print(const void * ip,const struct xt_entry_target * target,int numeric)72 static void LED_print(const void *ip, const struct xt_entry_target *target,
73 int numeric)
74 {
75 const struct xt_led_info *led = (void *)target->data;
76 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
77
78 printf(" led-trigger-id:\"");
79 /* Escape double quotes and backslashes in the ID */
80 while (*id != '\0') {
81 if (*id == '"' || *id == '\\')
82 printf("\\");
83 printf("%c", *id++);
84 }
85 printf("\"");
86
87 if (led->delay == -1)
88 printf(" led-delay:inf");
89 else
90 printf(" led-delay:%dms", led->delay);
91
92 if (led->always_blink)
93 printf(" led-always-blink");
94 }
95
LED_save(const void * ip,const struct xt_entry_target * target)96 static void LED_save(const void *ip, const struct xt_entry_target *target)
97 {
98 const struct xt_led_info *led = (void *)target->data;
99 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
100
101 printf(" --led-trigger-id \"");
102 /* Escape double quotes and backslashes in the ID */
103 while (*id != '\0') {
104 if (*id == '"' || *id == '\\')
105 printf("\\");
106 printf("%c", *id++);
107 }
108 printf("\"");
109
110 /* Only print the delay if it's not zero (the default) */
111 if (led->delay > 0)
112 printf(" --led-delay %d", led->delay);
113 else if (led->delay == -1)
114 printf(" --led-delay inf");
115
116 /* Only print always_blink if it's not set to the default */
117 if (led->always_blink)
118 printf(" --led-always-blink");
119 }
120
121 static struct xtables_target led_tg_reg = {
122 .version = XTABLES_VERSION,
123 .name = "LED",
124 .family = PF_UNSPEC,
125 .revision = 0,
126 .size = XT_ALIGN(sizeof(struct xt_led_info)),
127 .userspacesize = offsetof(struct xt_led_info, internal_data),
128 .help = LED_help,
129 .print = LED_print,
130 .save = LED_save,
131 .x6_parse = LED_parse,
132 .x6_options = LED_opts,
133 };
134
_init(void)135 void _init(void)
136 {
137 xtables_register_target(&led_tg_reg);
138 }
139