Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 Hop Limit Target module |
| 3 | * Maciej Soltysiak <solt@dns.toxicfilms.tv> |
| 4 | * Based on HW's ttl target |
| 5 | * This program is distributed under the terms of GNU GPL |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <getopt.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 12 | #include <xtables.h> |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 13 | |
| 14 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 15 | #include <linux/netfilter_ipv6/ip6t_HL.h> |
| 16 | |
| 17 | #define IP6T_HL_USED 1 |
| 18 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 19 | static void HL_help(void) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 20 | { |
| 21 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 22 | "HL target options\n" |
Maciej Soltysiak | 46e8538 | 2004-07-28 14:08:22 +0000 | [diff] [blame] | 23 | " --hl-set value Set HL to <value 0-255>\n" |
| 24 | " --hl-dec value Decrement HL by <value 1-255>\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 25 | " --hl-inc value Increment HL by <value 1-255>\n"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 28 | static int HL_parse(int c, char **argv, int invert, unsigned int *flags, |
| 29 | const void *entry, struct xt_entry_target **target) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 30 | { |
| 31 | struct ip6t_HL_info *info = (struct ip6t_HL_info *) (*target)->data; |
Maciej Soltysiak | 46e8538 | 2004-07-28 14:08:22 +0000 | [diff] [blame] | 32 | unsigned int value; |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 33 | |
| 34 | if (*flags & IP6T_HL_USED) { |
| 35 | exit_error(PARAMETER_PROBLEM, |
| 36 | "Can't specify HL option twice"); |
| 37 | } |
| 38 | |
| 39 | if (!optarg) |
| 40 | exit_error(PARAMETER_PROBLEM, |
| 41 | "HL: You must specify a value"); |
| 42 | |
| 43 | if (check_inverse(optarg, &invert, NULL, 0)) |
| 44 | exit_error(PARAMETER_PROBLEM, |
| 45 | "HL: unexpected `!'"); |
| 46 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 47 | if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX)) |
Maciej Soltysiak | 46e8538 | 2004-07-28 14:08:22 +0000 | [diff] [blame] | 48 | exit_error(PARAMETER_PROBLEM, |
| 49 | "HL: Expected value between 0 and 255"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 50 | |
| 51 | switch (c) { |
| 52 | |
| 53 | case '1': |
| 54 | info->mode = IP6T_HL_SET; |
| 55 | break; |
| 56 | |
| 57 | case '2': |
| 58 | if (value == 0) { |
| 59 | exit_error(PARAMETER_PROBLEM, |
| 60 | "HL: decreasing by 0?"); |
| 61 | } |
| 62 | |
| 63 | info->mode = IP6T_HL_DEC; |
| 64 | break; |
| 65 | |
| 66 | case '3': |
| 67 | if (value == 0) { |
| 68 | exit_error(PARAMETER_PROBLEM, |
| 69 | "HL: increasing by 0?"); |
| 70 | } |
| 71 | |
| 72 | info->mode = IP6T_HL_INC; |
| 73 | break; |
| 74 | |
| 75 | default: |
| 76 | return 0; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | info->hop_limit = value; |
| 81 | *flags |= IP6T_HL_USED; |
| 82 | |
| 83 | return 1; |
| 84 | } |
| 85 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 86 | static void HL_check(unsigned int flags) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 87 | { |
| 88 | if (!(flags & IP6T_HL_USED)) |
| 89 | exit_error(PARAMETER_PROBLEM, |
| 90 | "HL: You must specify an action"); |
| 91 | } |
| 92 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 93 | static void HL_save(const void *ip, const struct xt_entry_target *target) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 94 | { |
| 95 | const struct ip6t_HL_info *info = |
| 96 | (struct ip6t_HL_info *) target->data; |
| 97 | |
| 98 | switch (info->mode) { |
| 99 | case IP6T_HL_SET: |
| 100 | printf("--hl-set "); |
| 101 | break; |
| 102 | case IP6T_HL_DEC: |
| 103 | printf("--hl-dec "); |
| 104 | break; |
| 105 | |
| 106 | case IP6T_HL_INC: |
| 107 | printf("--hl-inc "); |
| 108 | break; |
| 109 | } |
| 110 | printf("%u ", info->hop_limit); |
| 111 | } |
| 112 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 113 | static void HL_print(const void *ip, const struct xt_entry_target *target, |
| 114 | int numeric) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 115 | { |
| 116 | const struct ip6t_HL_info *info = |
| 117 | (struct ip6t_HL_info *) target->data; |
| 118 | |
| 119 | printf("HL "); |
| 120 | switch (info->mode) { |
| 121 | case IP6T_HL_SET: |
| 122 | printf("set to "); |
| 123 | break; |
| 124 | case IP6T_HL_DEC: |
| 125 | printf("decrement by "); |
| 126 | break; |
| 127 | case IP6T_HL_INC: |
| 128 | printf("increment by "); |
| 129 | break; |
| 130 | } |
| 131 | printf("%u ", info->hop_limit); |
| 132 | } |
| 133 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 134 | static const struct option HL_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 135 | { "hl-set", 1, NULL, '1' }, |
| 136 | { "hl-dec", 1, NULL, '2' }, |
| 137 | { "hl-inc", 1, NULL, '3' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 138 | { .name = NULL } |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 141 | static struct xtables_target hl_tg6_reg = { |
Harald Welte | fcbab56 | 2005-02-01 13:36:46 +0000 | [diff] [blame] | 142 | .name = "HL", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 143 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 144 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 145 | .size = XT_ALIGN(sizeof(struct ip6t_HL_info)), |
| 146 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_HL_info)), |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 147 | .help = HL_help, |
| 148 | .parse = HL_parse, |
| 149 | .final_check = HL_check, |
| 150 | .print = HL_print, |
| 151 | .save = HL_save, |
| 152 | .extra_opts = HL_opts, |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | void _init(void) |
| 156 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 157 | xtables_register_target(&hl_tg6_reg); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 158 | } |