Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 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 | */ |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 15 | #include <xtables.h> |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 16 | #include <linux/netfilter/xt_LED.h> |
| 17 | |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 18 | enum { |
| 19 | O_LED_TRIGGER_ID = 0, |
| 20 | O_LED_DELAY, |
| 21 | O_LED_ALWAYS_BLINK, |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 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 | |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 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 | |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 49 | static void LED_parse(struct xt_option_call *cb) |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 50 | { |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 51 | struct xt_led_info *led = cb->data; |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 52 | |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 53 | xtables_option_parse(cb); |
| 54 | switch (cb->entry->id) { |
| 55 | case O_LED_TRIGGER_ID: |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 56 | strcpy(led->id, "netfilter-"); |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 57 | strcat(led->id, cb->arg); |
| 58 | break; |
| 59 | case O_LED_DELAY: |
| 60 | if (strncasecmp(cb->arg, "inf", 3) == 0) |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 61 | led->delay = -1; |
| 62 | else |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 63 | led->delay = strtoul(cb->arg, NULL, 0); |
| 64 | break; |
| 65 | case O_LED_ALWAYS_BLINK: |
| 66 | led->always_blink = 1; |
| 67 | break; |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 68 | } |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void LED_print(const void *ip, const struct xt_entry_target *target, |
| 72 | int numeric) |
| 73 | { |
| 74 | const struct xt_led_info *led = (void *)target->data; |
| 75 | const char *id = led->id + strlen("netfilter-"); /* trim off prefix */ |
| 76 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 77 | printf(" led-trigger-id:\""); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 78 | /* Escape double quotes and backslashes in the ID */ |
| 79 | while (*id != '\0') { |
| 80 | if (*id == '"' || *id == '\\') |
| 81 | printf("\\"); |
| 82 | printf("%c", *id++); |
| 83 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 84 | printf("\""); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 85 | |
| 86 | if (led->delay == -1) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 87 | printf(" led-delay:inf"); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 88 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 89 | printf(" led-delay:%dms", led->delay); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 90 | |
| 91 | if (led->always_blink) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 92 | printf(" led-always-blink"); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void LED_save(const void *ip, const struct xt_entry_target *target) |
| 96 | { |
| 97 | const struct xt_led_info *led = (void *)target->data; |
| 98 | const char *id = led->id + strlen("netfilter-"); /* trim off prefix */ |
| 99 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 100 | printf(" --led-trigger-id \""); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 101 | /* Escape double quotes and backslashes in the ID */ |
| 102 | while (*id != '\0') { |
| 103 | if (*id == '"' || *id == '\\') |
| 104 | printf("\\"); |
| 105 | printf("%c", *id++); |
| 106 | } |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 107 | printf("\""); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 108 | |
| 109 | /* Only print the delay if it's not zero (the default) */ |
| 110 | if (led->delay > 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 111 | printf(" --led-delay %d", led->delay); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 112 | else if (led->delay == -1) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 113 | printf(" --led-delay inf"); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 114 | |
| 115 | /* Only print always_blink if it's not set to the default */ |
| 116 | if (led->always_blink) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 117 | printf(" --led-always-blink"); |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static struct xtables_target led_tg_reg = { |
| 121 | .version = XTABLES_VERSION, |
| 122 | .name = "LED", |
| 123 | .family = PF_UNSPEC, |
| 124 | .revision = 0, |
| 125 | .size = XT_ALIGN(sizeof(struct xt_led_info)), |
| 126 | .userspacesize = offsetof(struct xt_led_info, internal_data), |
| 127 | .help = LED_help, |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 128 | .print = LED_print, |
| 129 | .save = LED_save, |
Jan Engelhardt | 942f140 | 2011-03-06 18:21:42 +0100 | [diff] [blame] | 130 | .x6_parse = LED_parse, |
| 131 | .x6_options = LED_opts, |
Adam Nielsen | 7cd3c2e | 2010-04-04 12:59:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | void _init(void) |
| 135 | { |
| 136 | xtables_register_target(&led_tg_reg); |
| 137 | } |