Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Shared library add-on to iptables to add TOS target support |
| 3 | * |
| 4 | * Copyright © CC Computer Consultants GmbH, 2007 |
| 5 | * Contact: Jan Engelhardt <jengelh@computergmbh.de> |
| 6 | */ |
| 7 | #include <getopt.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Jirí Moravec | 56156cd | 2008-10-22 08:57:38 +0200 | [diff] [blame] | 11 | #include <netinet/in.h> |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 12 | |
| 13 | #include <xtables.h> |
| 14 | #include <linux/netfilter/xt_DSCP.h> |
| 15 | #include <linux/netfilter_ipv4/ipt_TOS.h> |
| 16 | #include "tos_values.c" |
| 17 | |
| 18 | enum { |
| 19 | FLAG_TOS = 1 << 0, |
| 20 | }; |
| 21 | |
| 22 | static const struct option tos_tg_opts_v0[] = { |
| 23 | {.name = "set-tos", .has_arg = true, .val = '='}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 24 | { .name = NULL } |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static const struct option tos_tg_opts[] = { |
| 28 | {.name = "set-tos", .has_arg = true, .val = '='}, |
| 29 | {.name = "and-tos", .has_arg = true, .val = '&'}, |
| 30 | {.name = "or-tos", .has_arg = true, .val = '|'}, |
| 31 | {.name = "xor-tos", .has_arg = true, .val = '^'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 32 | { .name = NULL } |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static void tos_tg_help_v0(void) |
| 36 | { |
| 37 | const struct tos_symbol_info *symbol; |
| 38 | |
| 39 | printf( |
| 40 | "TOS target options:\n" |
| 41 | " --set-tos value Set Type of Service/Priority field to value\n" |
| 42 | " --set-tos symbol Set TOS field (IPv4 only) by symbol\n" |
| 43 | " Accepted symbolic names for value are:\n"); |
| 44 | |
| 45 | for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol) |
| 46 | printf(" (0x%02x) %2u %s\n", |
| 47 | symbol->value, symbol->value, symbol->name); |
| 48 | |
| 49 | printf("\n"); |
| 50 | } |
| 51 | |
| 52 | static void tos_tg_help(void) |
| 53 | { |
| 54 | const struct tos_symbol_info *symbol; |
| 55 | |
| 56 | printf( |
| 57 | "TOS target v%s options:\n" |
| 58 | " --set-tos value[/mask] Set Type of Service/Priority field to value\n" |
| 59 | " (Zero out bits in mask and XOR value into TOS)\n" |
| 60 | " --set-tos symbol Set TOS field (IPv4 only) by symbol\n" |
| 61 | " (this zeroes the 4-bit Precedence part!)\n" |
| 62 | " Accepted symbolic names for value are:\n", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 63 | XTABLES_VERSION); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 64 | |
| 65 | for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol) |
| 66 | printf(" (0x%02x) %2u %s\n", |
| 67 | symbol->value, symbol->value, symbol->name); |
| 68 | |
| 69 | printf( |
| 70 | "\n" |
| 71 | " --and-tos bits Binary AND the TOS value with bits\n" |
| 72 | " --or-tos bits Binary OR the TOS value with bits\n" |
| 73 | " --xor-tos bits Binary XOR the TOS value with bits\n" |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | static int tos_tg_parse_v0(int c, char **argv, int invert, unsigned int *flags, |
| 78 | const void *entry, struct xt_entry_target **target) |
| 79 | { |
| 80 | struct ipt_tos_target_info *info = (void *)(*target)->data; |
| 81 | struct tos_value_mask tvm; |
| 82 | |
| 83 | switch (c) { |
| 84 | case '=': |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 85 | xtables_param_act(XTF_ONLY_ONCE, "TOS", "--set-tos", *flags & FLAG_TOS); |
| 86 | xtables_param_act(XTF_NO_INVERT, "TOS", "--set-tos", invert); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 87 | if (!tos_parse_symbolic(optarg, &tvm, 0xFF)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 88 | xtables_param_act(XTF_BAD_VALUE, "TOS", "--set-tos", optarg); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 89 | if (tvm.mask != 0xFF) |
| 90 | exit_error(PARAMETER_PROBLEM, "tos match: Your kernel " |
| 91 | "is too old to support anything besides " |
| 92 | "/0xFF as a mask."); |
| 93 | info->tos = tvm.value; |
| 94 | *flags |= FLAG_TOS; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | static int tos_tg_parse(int c, char **argv, int invert, unsigned int *flags, |
| 102 | const void *entry, struct xt_entry_target **target) |
| 103 | { |
| 104 | struct xt_tos_target_info *info = (void *)(*target)->data; |
| 105 | struct tos_value_mask tvm; |
| 106 | unsigned int bits; |
| 107 | |
| 108 | switch (c) { |
| 109 | case '=': /* --set-tos */ |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 110 | xtables_param_act(XTF_ONLY_ONCE, "TOS", "--set-tos", *flags & FLAG_TOS); |
| 111 | xtables_param_act(XTF_NO_INVERT, "TOS", "--set-tos", invert); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 112 | if (!tos_parse_symbolic(optarg, &tvm, 0x3F)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 113 | xtables_param_act(XTF_BAD_VALUE, "TOS", "--set-tos", optarg); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 114 | info->tos_value = tvm.value; |
| 115 | info->tos_mask = tvm.mask; |
| 116 | break; |
| 117 | |
| 118 | case '&': /* --and-tos */ |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 119 | xtables_param_act(XTF_ONLY_ONCE, "TOS", "--and-tos", *flags & FLAG_TOS); |
| 120 | xtables_param_act(XTF_NO_INVERT, "TOS", "--and-tos", invert); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 121 | if (!xtables_strtoui(optarg, NULL, &bits, 0, UINT8_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 122 | xtables_param_act(XTF_BAD_VALUE, "TOS", "--and-tos", optarg); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 123 | info->tos_value = 0; |
| 124 | info->tos_mask = ~bits; |
| 125 | break; |
| 126 | |
| 127 | case '|': /* --or-tos */ |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 128 | xtables_param_act(XTF_ONLY_ONCE, "TOS", "--or-tos", *flags & FLAG_TOS); |
| 129 | xtables_param_act(XTF_NO_INVERT, "TOS", "--or-tos", invert); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 130 | if (!xtables_strtoui(optarg, NULL, &bits, 0, UINT8_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 131 | xtables_param_act(XTF_BAD_VALUE, "TOS", "--or-tos", optarg); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 132 | info->tos_value = bits; |
| 133 | info->tos_mask = bits; |
| 134 | break; |
| 135 | |
| 136 | case '^': /* --xor-tos */ |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 137 | xtables_param_act(XTF_ONLY_ONCE, "TOS", "--xor-tos", *flags & FLAG_TOS); |
| 138 | xtables_param_act(XTF_NO_INVERT, "TOS", "--xor-tos", invert); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 139 | if (!xtables_strtoui(optarg, NULL, &bits, 0, UINT8_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 140 | xtables_param_act(XTF_BAD_VALUE, "TOS", "--xor-tos", optarg); |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 141 | info->tos_value = bits; |
| 142 | info->tos_mask = 0; |
| 143 | break; |
| 144 | |
| 145 | default: |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | *flags |= FLAG_TOS; |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | static void tos_tg_check(unsigned int flags) |
| 154 | { |
| 155 | if (flags == 0) |
| 156 | exit_error(PARAMETER_PROBLEM, |
| 157 | "TOS: The --set-tos parameter is required"); |
| 158 | } |
| 159 | |
| 160 | static void tos_tg_print_v0(const void *ip, |
| 161 | const struct xt_entry_target *target, int numeric) |
| 162 | { |
| 163 | const struct ipt_tos_target_info *info = (const void *)target->data; |
| 164 | |
| 165 | printf("TOS set "); |
| 166 | if (numeric || !tos_try_print_symbolic("", info->tos, 0xFF)) |
| 167 | printf("0x%02x ", info->tos); |
| 168 | } |
| 169 | |
| 170 | static void tos_tg_print(const void *ip, const struct xt_entry_target *target, |
| 171 | int numeric) |
| 172 | { |
| 173 | const struct xt_tos_target_info *info = (const void *)target->data; |
| 174 | |
| 175 | if (numeric) |
| 176 | printf("TOS set 0x%02x/0x%02x ", |
| 177 | info->tos_value, info->tos_mask); |
| 178 | else if (tos_try_print_symbolic("TOS set ", |
| 179 | info->tos_value, info->tos_mask)) |
| 180 | /* already printed by call */ |
| 181 | return; |
| 182 | else if (info->tos_value == 0) |
| 183 | printf("TOS and 0x%02x ", |
| 184 | (unsigned int)(u_int8_t)~info->tos_mask); |
| 185 | else if (info->tos_value == info->tos_mask) |
| 186 | printf("TOS or 0x%02x ", info->tos_value); |
| 187 | else if (info->tos_mask == 0) |
| 188 | printf("TOS xor 0x%02x ", info->tos_value); |
| 189 | else |
| 190 | printf("TOS set 0x%02x/0x%02x ", |
| 191 | info->tos_value, info->tos_mask); |
| 192 | } |
| 193 | |
| 194 | static void tos_tg_save_v0(const void *ip, const struct xt_entry_target *target) |
| 195 | { |
| 196 | const struct ipt_tos_target_info *info = (const void *)target->data; |
| 197 | |
| 198 | printf("--set-tos 0x%02x ", info->tos); |
| 199 | } |
| 200 | |
| 201 | static void tos_tg_save(const void *ip, const struct xt_entry_target *target) |
| 202 | { |
| 203 | const struct xt_tos_target_info *info = (const void *)target->data; |
| 204 | |
| 205 | printf("--set-tos 0x%02x/0x%02x ", info->tos_value, info->tos_mask); |
| 206 | } |
| 207 | |
| 208 | static struct xtables_target tos_tg_reg_v0 = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 209 | .version = XTABLES_VERSION, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 210 | .name = "TOS", |
| 211 | .revision = 0, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 212 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 213 | .size = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 214 | .userspacesize = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 215 | .help = tos_tg_help_v0, |
| 216 | .parse = tos_tg_parse_v0, |
| 217 | .final_check = tos_tg_check, |
| 218 | .print = tos_tg_print_v0, |
| 219 | .save = tos_tg_save_v0, |
| 220 | .extra_opts = tos_tg_opts_v0, |
| 221 | }; |
| 222 | |
| 223 | static struct xtables_target tos_tg_reg = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 224 | .version = XTABLES_VERSION, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 225 | .name = "TOS", |
| 226 | .revision = 1, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 227 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 228 | .size = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 229 | .userspacesize = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 230 | .help = tos_tg_help, |
| 231 | .parse = tos_tg_parse, |
| 232 | .final_check = tos_tg_check, |
| 233 | .print = tos_tg_print, |
| 234 | .save = tos_tg_save, |
| 235 | .extra_opts = tos_tg_opts, |
| 236 | }; |
| 237 | |
| 238 | static struct xtables_target tos_tg6_reg = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 239 | .version = XTABLES_VERSION, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 240 | .name = "TOS", |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 241 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 36f2ead | 2008-01-20 13:25:08 +0000 | [diff] [blame] | 242 | .revision = 1, |
| 243 | .size = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 244 | .userspacesize = XT_ALIGN(sizeof(struct xt_tos_target_info)), |
| 245 | .help = tos_tg_help, |
| 246 | .parse = tos_tg_parse, |
| 247 | .final_check = tos_tg_check, |
| 248 | .print = tos_tg_print, |
| 249 | .save = tos_tg_save, |
| 250 | .extra_opts = tos_tg_opts, |
| 251 | }; |
| 252 | |
| 253 | void _init(void) |
| 254 | { |
| 255 | xtables_register_target(&tos_tg_reg_v0); |
| 256 | xtables_register_target(&tos_tg_reg); |
| 257 | xtables_register_target(&tos_tg6_reg); |
| 258 | } |