Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add TOS matching support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_tos.h> |
| 10 | |
| 11 | /* TOS names and values. */ |
| 12 | struct TOS_value |
| 13 | { |
| 14 | unsigned char TOS; |
| 15 | const char *name; |
| 16 | } TOS_values[] = { |
| 17 | { IPTOS_LOWDELAY, "Minimize-Delay" }, |
| 18 | { IPTOS_THROUGHPUT, "Maximize-Throughput" }, |
| 19 | { IPTOS_RELIABILITY, "Maximize-Reliability" }, |
| 20 | { IPTOS_MINCOST, "Minimize-Cost" }, |
| 21 | { IPTOS_NORMALSVC, "Normal-Service" }, |
| 22 | }; |
| 23 | |
| 24 | /* Function which prints out usage message. */ |
| 25 | static void |
| 26 | help(void) |
| 27 | { |
| 28 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 29 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 30 | printf( |
| 31 | "TOS match v%s options:\n" |
| 32 | "[!] --tos value Match Type of Service field from one of the\n" |
| 33 | " following numeric or descriptive values:\n", |
| 34 | NETFILTER_VERSION); |
| 35 | |
| 36 | for (i = 0; i < sizeof(TOS_values)/sizeof(struct TOS_value);i++) |
| 37 | printf(" %s %u (0x%02x)\n", |
| 38 | TOS_values[i].name, |
| 39 | TOS_values[i].TOS, |
| 40 | TOS_values[i].TOS); |
| 41 | fputc('\n', stdout); |
| 42 | } |
| 43 | |
| 44 | static struct option opts[] = { |
| 45 | { "tos", 1, 0, '1' }, |
| 46 | {0} |
| 47 | }; |
| 48 | |
| 49 | /* Initialize the match. */ |
| 50 | static void |
| 51 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 52 | { |
| 53 | *nfcache |= NFC_IP_TOS; |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | parse_tos(const unsigned char *s, struct ipt_tos_info *info) |
| 58 | { |
| 59 | unsigned int i; |
| 60 | int tos = string_to_number(s, 0, 255); |
| 61 | |
| 62 | if (tos != -1) { |
| 63 | if (tos == IPTOS_LOWDELAY |
| 64 | || tos == IPTOS_THROUGHPUT |
| 65 | || tos == IPTOS_RELIABILITY |
| 66 | || tos == IPTOS_MINCOST |
| 67 | || tos == IPTOS_NORMALSVC) { |
| 68 | info->tos = (u_int8_t )tos; |
| 69 | return; |
| 70 | } |
| 71 | } else { |
| 72 | for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++) |
| 73 | if (strcasecmp(s,TOS_values[i].name) == 0) { |
| 74 | info->tos = TOS_values[i].TOS; |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | exit_error(PARAMETER_PROBLEM, "Bad TOS value `%s'", s); |
| 79 | } |
| 80 | |
| 81 | /* Function which parses command options; returns true if it |
| 82 | ate an option */ |
| 83 | static int |
| 84 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 85 | const struct ipt_entry *entry, |
| 86 | unsigned int *nfcache, |
| 87 | struct ipt_entry_match **match) |
| 88 | { |
| 89 | struct ipt_tos_info *tosinfo = (struct ipt_tos_info *)(*match)->data; |
| 90 | |
| 91 | switch (c) { |
| 92 | case '1': |
| 93 | if (check_inverse(optarg, &invert)) |
| 94 | optind++; |
| 95 | parse_tos(argv[optind-1], tosinfo); |
| 96 | if (invert) |
| 97 | tosinfo->invert = 1; |
| 98 | *flags = 1; |
| 99 | break; |
| 100 | |
| 101 | default: |
| 102 | return 0; |
| 103 | } |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | print_tos(u_int8_t tos, int invert, int numeric) |
| 109 | { |
| 110 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 111 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 112 | if (invert) |
| 113 | fputc('!', stdout); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 114 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 115 | if (!numeric) { |
| 116 | for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++) |
| 117 | if (TOS_values[i].TOS == tos) { |
| 118 | printf("%s ", TOS_values[i].name); |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | printf("0x%02x ", tos); |
| 123 | } |
| 124 | |
| 125 | /* Final check; must have specified --tos. */ |
| 126 | static void |
| 127 | final_check(unsigned int flags) |
| 128 | { |
| 129 | if (!flags) |
| 130 | exit_error(PARAMETER_PROBLEM, |
| 131 | "TOS match: You must specify `--tos'"); |
| 132 | } |
| 133 | |
| 134 | /* Prints out the matchinfo. */ |
| 135 | static void |
| 136 | print(const struct ipt_ip *ip, |
| 137 | const struct ipt_entry_match *match, |
| 138 | int numeric) |
| 139 | { |
| 140 | printf("TOS match "); |
| 141 | print_tos(((struct ipt_tos_info *)match->data)->tos, |
| 142 | ((struct ipt_tos_info *)match->data)->invert, numeric); |
| 143 | } |
| 144 | |
| 145 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 146 | static void |
| 147 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 148 | { |
| 149 | printf("--tos "); |
| 150 | print_tos(((struct ipt_tos_info *)match->data)->tos, |
| 151 | ((struct ipt_tos_info *)match->data)->invert, 0); |
| 152 | } |
| 153 | |
| 154 | struct iptables_match tos |
| 155 | = { NULL, |
| 156 | "tos", |
| 157 | NETFILTER_VERSION, |
| 158 | sizeof(struct ipt_tos_info), |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 159 | sizeof(struct ipt_tos_info), |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 160 | &help, |
| 161 | &init, |
| 162 | &parse, |
| 163 | &final_check, |
| 164 | &print, |
| 165 | &save, |
| 166 | opts |
| 167 | }; |
| 168 | |
| 169 | void _init(void) |
| 170 | { |
| 171 | register_match(&tos); |
| 172 | } |