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> |
| 12 | #include <ip6tables.h> |
| 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( |
| 22 | "HL target v%s 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" |
| 25 | " --hl-inc value Increment HL by <value 1-255>\n" |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 26 | , IPTABLES_VERSION); |
| 27 | } |
| 28 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 29 | static int HL_parse(int c, char **argv, int invert, unsigned int *flags, |
| 30 | const void *entry, struct xt_entry_target **target) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 31 | { |
| 32 | struct ip6t_HL_info *info = (struct ip6t_HL_info *) (*target)->data; |
Maciej Soltysiak | 46e8538 | 2004-07-28 14:08:22 +0000 | [diff] [blame] | 33 | unsigned int value; |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 34 | |
| 35 | if (*flags & IP6T_HL_USED) { |
| 36 | exit_error(PARAMETER_PROBLEM, |
| 37 | "Can't specify HL option twice"); |
| 38 | } |
| 39 | |
| 40 | if (!optarg) |
| 41 | exit_error(PARAMETER_PROBLEM, |
| 42 | "HL: You must specify a value"); |
| 43 | |
| 44 | if (check_inverse(optarg, &invert, NULL, 0)) |
| 45 | exit_error(PARAMETER_PROBLEM, |
| 46 | "HL: unexpected `!'"); |
| 47 | |
Maciej Soltysiak | 46e8538 | 2004-07-28 14:08:22 +0000 | [diff] [blame] | 48 | if (string_to_number(optarg, 0, 255, &value) == -1) |
| 49 | exit_error(PARAMETER_PROBLEM, |
| 50 | "HL: Expected value between 0 and 255"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 51 | |
| 52 | switch (c) { |
| 53 | |
| 54 | case '1': |
| 55 | info->mode = IP6T_HL_SET; |
| 56 | break; |
| 57 | |
| 58 | case '2': |
| 59 | if (value == 0) { |
| 60 | exit_error(PARAMETER_PROBLEM, |
| 61 | "HL: decreasing by 0?"); |
| 62 | } |
| 63 | |
| 64 | info->mode = IP6T_HL_DEC; |
| 65 | break; |
| 66 | |
| 67 | case '3': |
| 68 | if (value == 0) { |
| 69 | exit_error(PARAMETER_PROBLEM, |
| 70 | "HL: increasing by 0?"); |
| 71 | } |
| 72 | |
| 73 | info->mode = IP6T_HL_INC; |
| 74 | break; |
| 75 | |
| 76 | default: |
| 77 | return 0; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | info->hop_limit = value; |
| 82 | *flags |= IP6T_HL_USED; |
| 83 | |
| 84 | return 1; |
| 85 | } |
| 86 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 87 | static void HL_check(unsigned int flags) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 88 | { |
| 89 | if (!(flags & IP6T_HL_USED)) |
| 90 | exit_error(PARAMETER_PROBLEM, |
| 91 | "HL: You must specify an action"); |
| 92 | } |
| 93 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 94 | 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] | 95 | { |
| 96 | const struct ip6t_HL_info *info = |
| 97 | (struct ip6t_HL_info *) target->data; |
| 98 | |
| 99 | switch (info->mode) { |
| 100 | case IP6T_HL_SET: |
| 101 | printf("--hl-set "); |
| 102 | break; |
| 103 | case IP6T_HL_DEC: |
| 104 | printf("--hl-dec "); |
| 105 | break; |
| 106 | |
| 107 | case IP6T_HL_INC: |
| 108 | printf("--hl-inc "); |
| 109 | break; |
| 110 | } |
| 111 | printf("%u ", info->hop_limit); |
| 112 | } |
| 113 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 114 | static void HL_print(const void *ip, const struct xt_entry_target *target, |
| 115 | int numeric) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 116 | { |
| 117 | const struct ip6t_HL_info *info = |
| 118 | (struct ip6t_HL_info *) target->data; |
| 119 | |
| 120 | printf("HL "); |
| 121 | switch (info->mode) { |
| 122 | case IP6T_HL_SET: |
| 123 | printf("set to "); |
| 124 | break; |
| 125 | case IP6T_HL_DEC: |
| 126 | printf("decrement by "); |
| 127 | break; |
| 128 | case IP6T_HL_INC: |
| 129 | printf("increment by "); |
| 130 | break; |
| 131 | } |
| 132 | printf("%u ", info->hop_limit); |
| 133 | } |
| 134 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 135 | static const struct option HL_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 136 | { "hl-set", 1, NULL, '1' }, |
| 137 | { "hl-dec", 1, NULL, '2' }, |
| 138 | { "hl-inc", 1, NULL, '3' }, |
| 139 | { } |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 142 | static struct ip6tables_target hl_target6 = { |
Harald Welte | fcbab56 | 2005-02-01 13:36:46 +0000 | [diff] [blame] | 143 | .name = "HL", |
| 144 | .version = IPTABLES_VERSION, |
| 145 | .size = IP6T_ALIGN(sizeof(struct ip6t_HL_info)), |
| 146 | .userspacesize = IP6T_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 | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 157 | register_target6(&hl_target6); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 158 | } |