Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables for ECN, $Version$ |
| 2 | * |
| 3 | * (C) 2002 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * This program is distributed under the terms of GNU GPL v2, 1991 |
| 6 | * |
| 7 | * libipt_ECN.c borrowed heavily from libipt_DSCP.c |
| 8 | * |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 9 | * $Id$ |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 10 | */ |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <getopt.h> |
| 15 | |
| 16 | #include <iptables.h> |
| 17 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 18 | #include <linux/netfilter_ipv4/ipt_ECN.h> |
| 19 | |
| 20 | static void init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | static void help(void) |
| 25 | { |
| 26 | printf( |
Harald Welte | 1c8fa73 | 2002-05-29 12:43:34 +0000 | [diff] [blame] | 27 | "ECN target v%s options\n" |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 28 | " --ecn-tcp-remove Remove all ECN bits from TCP header\n", |
Harald Welte | e0f47ad | 2002-08-07 10:04:42 +0000 | [diff] [blame] | 29 | IPTABLES_VERSION); |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | #if 0 |
Harald Welte | 1c8fa73 | 2002-05-29 12:43:34 +0000 | [diff] [blame] | 33 | "ECN target v%s EXPERIMENTAL options (use with extreme care!)\n" |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 34 | " --ecn-ip-ect Set the IPv4 ECT codepoint (0 to 3)\n" |
| 35 | " --ecn-tcp-cwr Set the IPv4 CWR bit (0 or 1)\n" |
Harald Welte | 92cad5f | 2002-08-06 06:50:04 +0000 | [diff] [blame] | 36 | " --ecn-tcp-ece Set the IPv4 ECE bit (0 or 1)\n", |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 39 | |
| 40 | static struct option opts[] = { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 41 | { "ecn-tcp-remove", 0, 0, 'F' }, |
| 42 | { "ecn-tcp-cwr", 1, 0, 'G' }, |
| 43 | { "ecn-tcp-ece", 1, 0, 'H' }, |
| 44 | { "ecn-ip-ect", 1, 0, '9' }, |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 45 | { 0 } |
| 46 | }; |
| 47 | |
| 48 | static int |
| 49 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 50 | const struct ipt_entry *entry, |
| 51 | struct ipt_entry_target **target) |
| 52 | { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 53 | unsigned int result; |
Harald Welte | 5a15c9a | 2002-02-18 21:32:56 +0000 | [diff] [blame] | 54 | struct ipt_ECN_info *einfo |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 55 | = (struct ipt_ECN_info *)(*target)->data; |
| 56 | |
| 57 | switch (c) { |
| 58 | case 'F': |
| 59 | if (*flags) |
| 60 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 61 | "ECN target: Only use --ecn-tcp-remove ONCE!"); |
| 62 | einfo->operation = IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR; |
| 63 | einfo->proto.tcp.ece = 0; |
| 64 | einfo->proto.tcp.cwr = 0; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 65 | *flags = 1; |
| 66 | break; |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 67 | case 'G': |
| 68 | if (*flags & IPT_ECN_OP_SET_CWR) |
| 69 | exit_error(PARAMETER_PROBLEM, |
| 70 | "ECN target: Only use --ecn-tcp-cwr ONCE!"); |
| 71 | if (string_to_number(optarg, 0, 1, &result)) |
| 72 | exit_error(PARAMETER_PROBLEM, |
| 73 | "ECN target: Value out of range"); |
| 74 | einfo->operation |= IPT_ECN_OP_SET_CWR; |
| 75 | einfo->proto.tcp.cwr = result; |
| 76 | *flags |= IPT_ECN_OP_SET_CWR; |
| 77 | break; |
| 78 | case 'H': |
| 79 | if (*flags & IPT_ECN_OP_SET_ECE) |
| 80 | exit_error(PARAMETER_PROBLEM, |
| 81 | "ECN target: Only use --ecn-tcp-ece ONCE!"); |
| 82 | if (string_to_number(optarg, 0, 1, &result)) |
| 83 | exit_error(PARAMETER_PROBLEM, |
| 84 | "ECN target: Value out of range"); |
| 85 | einfo->operation |= IPT_ECN_OP_SET_ECE; |
| 86 | einfo->proto.tcp.ece = result; |
| 87 | *flags |= IPT_ECN_OP_SET_ECE; |
| 88 | break; |
| 89 | case '9': |
| 90 | if (*flags & IPT_ECN_OP_SET_IP) |
| 91 | exit_error(PARAMETER_PROBLEM, |
| 92 | "ECN target: Only use --ecn-ip-ect ONCE!"); |
| 93 | if (string_to_number(optarg, 0, 3, &result)) |
| 94 | exit_error(PARAMETER_PROBLEM, |
| 95 | "ECN target: Value out of range"); |
| 96 | einfo->operation |= IPT_ECN_OP_SET_IP; |
| 97 | einfo->ip_ect = result; |
Harald Welte | c05c44f | 2002-08-05 19:36:15 +0000 | [diff] [blame] | 98 | *flags |= IPT_ECN_OP_SET_IP; |
| 99 | break; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 100 | default: |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | final_check(unsigned int flags) |
| 109 | { |
| 110 | if (!flags) |
| 111 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 4ac9fdd | 2003-01-13 12:35:28 +0000 | [diff] [blame] | 112 | "ECN target: Parameter --ecn-tcp-remove is required"); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Prints out the targinfo. */ |
| 116 | static void |
| 117 | print(const struct ipt_ip *ip, |
| 118 | const struct ipt_entry_target *target, |
| 119 | int numeric) |
| 120 | { |
| 121 | const struct ipt_ECN_info *einfo = |
| 122 | (const struct ipt_ECN_info *)target->data; |
| 123 | |
| 124 | printf("ECN "); |
| 125 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 126 | if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR) |
| 127 | && einfo->proto.tcp.ece == 0 |
| 128 | && einfo->proto.tcp.cwr == 0) |
| 129 | printf("TCP remove "); |
| 130 | else { |
| 131 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 132 | printf("ECE=%u ", einfo->proto.tcp.ece); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 133 | |
| 134 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 135 | printf("CWR=%u ", einfo->proto.tcp.cwr); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 136 | |
| 137 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 138 | printf("ECT codepoint=%u ", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 143 | static void |
| 144 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 145 | { |
| 146 | const struct ipt_ECN_info *einfo = |
| 147 | (const struct ipt_ECN_info *)target->data; |
| 148 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 149 | if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR) |
| 150 | && einfo->proto.tcp.ece == 0 |
| 151 | && einfo->proto.tcp.cwr == 0) |
| 152 | printf("--ecn-tcp-remove "); |
| 153 | else { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 154 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 155 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
| 156 | printf("--ecn-tcp-ece %d ", einfo->proto.tcp.ece); |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 157 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 158 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
| 159 | printf("--ecn-tcp-cwr %d ", einfo->proto.tcp.cwr); |
| 160 | |
| 161 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
| 162 | printf("--ecn-ip-ect %d ", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | static |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 167 | struct iptables_target ecn = { |
| 168 | .next = NULL, |
| 169 | .name = "ECN", |
| 170 | .version = IPTABLES_VERSION, |
| 171 | .size = IPT_ALIGN(sizeof(struct ipt_ECN_info)), |
| 172 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_ECN_info)), |
| 173 | .help = &help, |
| 174 | .init = &init, |
| 175 | .parse = &parse, |
| 176 | .final_check = &final_check, |
| 177 | .print = &print, |
| 178 | .save = &save, |
| 179 | .extra_opts = opts |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | void _init(void) |
| 183 | { |
| 184 | register_target(&ecn); |
| 185 | } |