Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Shared library add-on to iptables to add tos match support |
| 3 | * |
| 4 | * Copyright © CC Computer Consultants GmbH, 2007 |
| 5 | * Contact: Jan Engelhardt <jengelh@computergmbh.de> |
| 6 | */ |
| 7 | #include <getopt.h> |
| 8 | #include <netdb.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <xtables.h> |
| 15 | #include <linux/netfilter/xt_dscp.h> |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 16 | #include "tos_values.c" |
| 17 | |
Jan Engelhardt | 350661a | 2010-01-31 22:42:52 +0100 | [diff] [blame] | 18 | struct ipt_tos_info { |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 19 | uint8_t tos; |
| 20 | uint8_t invert; |
Jan Engelhardt | 350661a | 2010-01-31 22:42:52 +0100 | [diff] [blame] | 21 | }; |
| 22 | |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 23 | enum { |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 24 | O_TOS = 1 << 0, |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 27 | static const struct xt_option_entry tos_mt_opts_v0[] = { |
| 28 | {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK, |
Jan Engelhardt | 06312da | 2011-05-01 19:58:56 +0200 | [diff] [blame] | 29 | .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0xFF}, |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 30 | XTOPT_TABLEEND, |
| 31 | }; |
| 32 | |
| 33 | static const struct xt_option_entry tos_mt_opts[] = { |
| 34 | {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK, |
Jan Engelhardt | 06312da | 2011-05-01 19:58:56 +0200 | [diff] [blame] | 35 | .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0x3F}, |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 36 | XTOPT_TABLEEND, |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static void tos_mt_help(void) |
| 40 | { |
| 41 | const struct tos_symbol_info *symbol; |
| 42 | |
| 43 | printf( |
| 44 | "tos match options:\n" |
| 45 | "[!] --tos value[/mask] Match Type of Service/Priority field value\n" |
| 46 | "[!] --tos symbol Match TOS field (IPv4 only) by symbol\n" |
| 47 | " Accepted symbolic names for value are:\n"); |
| 48 | |
| 49 | for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol) |
| 50 | printf(" (0x%02x) %2u %s\n", |
| 51 | symbol->value, symbol->value, symbol->name); |
| 52 | |
| 53 | printf("\n"); |
| 54 | } |
| 55 | |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 56 | static void tos_mt_parse_v0(struct xt_option_call *cb) |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 57 | { |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 58 | struct ipt_tos_info *info = cb->data; |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 59 | |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 60 | xtables_option_parse(cb); |
| 61 | if (cb->val.tos_mask != 0xFF) |
| 62 | xtables_error(PARAMETER_PROBLEM, "tos: Your kernel is " |
| 63 | "too old to support anything besides /0xFF " |
| 64 | "as a mask."); |
| 65 | info->tos = cb->val.tos_value; |
| 66 | if (cb->invert) |
| 67 | info->invert = true; |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 70 | static void tos_mt_parse(struct xt_option_call *cb) |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 71 | { |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 72 | struct xt_tos_match_info *info = cb->data; |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 73 | |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 74 | xtables_option_parse(cb); |
| 75 | info->tos_value = cb->val.tos_value; |
| 76 | info->tos_mask = cb->val.tos_mask; |
| 77 | if (cb->invert) |
| 78 | info->invert = true; |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void tos_mt_print_v0(const void *ip, const struct xt_entry_match *match, |
| 82 | int numeric) |
| 83 | { |
| 84 | const struct ipt_tos_info *info = (const void *)match->data; |
| 85 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 86 | printf(" tos match "); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 87 | if (info->invert) |
| 88 | printf("!"); |
| 89 | if (numeric || !tos_try_print_symbolic("", info->tos, 0x3F)) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 90 | printf("0x%02x", info->tos); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void tos_mt_print(const void *ip, const struct xt_entry_match *match, |
| 94 | int numeric) |
| 95 | { |
| 96 | const struct xt_tos_match_info *info = (const void *)match->data; |
| 97 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 98 | printf(" tos match"); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 99 | if (info->invert) |
| 100 | printf("!"); |
| 101 | if (numeric || |
| 102 | !tos_try_print_symbolic("", info->tos_value, info->tos_mask)) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 103 | printf("0x%02x/0x%02x", info->tos_value, info->tos_mask); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void tos_mt_save_v0(const void *ip, const struct xt_entry_match *match) |
| 107 | { |
| 108 | const struct ipt_tos_info *info = (const void *)match->data; |
| 109 | |
| 110 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 111 | printf(" !"); |
| 112 | printf(" --tos 0x%02x", info->tos); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void tos_mt_save(const void *ip, const struct xt_entry_match *match) |
| 116 | { |
| 117 | const struct xt_tos_match_info *info = (const void *)match->data; |
| 118 | |
| 119 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 120 | printf(" !"); |
| 121 | printf(" --tos 0x%02x/0x%02x", info->tos_value, info->tos_mask); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 124 | static struct xtables_match tos_mt_reg[] = { |
| 125 | { |
| 126 | .version = XTABLES_VERSION, |
| 127 | .name = "tos", |
| 128 | .family = NFPROTO_IPV4, |
| 129 | .revision = 0, |
| 130 | .size = XT_ALIGN(sizeof(struct ipt_tos_info)), |
| 131 | .userspacesize = XT_ALIGN(sizeof(struct ipt_tos_info)), |
| 132 | .help = tos_mt_help, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 133 | .print = tos_mt_print_v0, |
| 134 | .save = tos_mt_save_v0, |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 135 | .x6_parse = tos_mt_parse_v0, |
| 136 | .x6_options = tos_mt_opts_v0, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 137 | }, |
| 138 | { |
| 139 | .version = XTABLES_VERSION, |
| 140 | .name = "tos", |
| 141 | .family = NFPROTO_UNSPEC, |
| 142 | .revision = 1, |
| 143 | .size = XT_ALIGN(sizeof(struct xt_tos_match_info)), |
| 144 | .userspacesize = XT_ALIGN(sizeof(struct xt_tos_match_info)), |
| 145 | .help = tos_mt_help, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 146 | .print = tos_mt_print, |
| 147 | .save = tos_mt_save, |
Jan Engelhardt | d8f5919 | 2011-04-29 02:12:56 +0200 | [diff] [blame] | 148 | .x6_parse = tos_mt_parse, |
| 149 | .x6_options = tos_mt_opts, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 150 | }, |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | void _init(void) |
| 154 | { |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 155 | xtables_register_matches(tos_mt_reg, ARRAY_SIZE(tos_mt_reg)); |
Jan Engelhardt | 0720c12 | 2008-01-20 13:22:42 +0000 | [diff] [blame] | 156 | } |