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