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 |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 8 | */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 9 | #include <stdbool.h> |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <getopt.h> |
| 14 | |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 15 | #include <xtables.h> |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 16 | #include <linux/netfilter_ipv4/ipt_ECN.h> |
| 17 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 18 | static void ECN_help(void) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 19 | { |
| 20 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | "ECN target options\n" |
| 22 | " --ecn-tcp-remove Remove all ECN bits from TCP header\n"); |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #if 0 |
Harald Welte | 1c8fa73 | 2002-05-29 12:43:34 +0000 | [diff] [blame] | 26 | "ECN target v%s EXPERIMENTAL options (use with extreme care!)\n" |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 27 | " --ecn-ip-ect Set the IPv4 ECT codepoint (0 to 3)\n" |
| 28 | " --ecn-tcp-cwr Set the IPv4 CWR bit (0 or 1)\n" |
Harald Welte | 92cad5f | 2002-08-06 06:50:04 +0000 | [diff] [blame] | 29 | " --ecn-tcp-ece Set the IPv4 ECE bit (0 or 1)\n", |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 32 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 33 | static const struct option ECN_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 34 | {.name = "ecn-tcp-remove", .has_arg = false, .val = 'F'}, |
| 35 | {.name = "ecn-tcp-cwr", .has_arg = true, .val = 'G'}, |
| 36 | {.name = "ecn-tcp-ece", .has_arg = true, .val = 'H'}, |
| 37 | {.name = "ecn-ip-ect", .has_arg = true, .val = '9'}, |
| 38 | XT_GETOPT_TABLEEND, |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 41 | static int ECN_parse(int c, char **argv, int invert, unsigned int *flags, |
| 42 | const void *entry, struct xt_entry_target **target) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 43 | { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 44 | unsigned int result; |
Harald Welte | 5a15c9a | 2002-02-18 21:32:56 +0000 | [diff] [blame] | 45 | struct ipt_ECN_info *einfo |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 46 | = (struct ipt_ECN_info *)(*target)->data; |
| 47 | |
| 48 | switch (c) { |
| 49 | case 'F': |
| 50 | if (*flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 51 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 52 | "ECN target: Only use --ecn-tcp-remove ONCE!"); |
| 53 | einfo->operation = IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR; |
| 54 | einfo->proto.tcp.ece = 0; |
| 55 | einfo->proto.tcp.cwr = 0; |
Jan Engelhardt | 094f104 | 2011-02-19 20:00:06 +0100 | [diff] [blame] | 56 | *flags |= IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 57 | break; |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 58 | case 'G': |
| 59 | if (*flags & IPT_ECN_OP_SET_CWR) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 60 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 61 | "ECN target: Only use --ecn-tcp-cwr ONCE!"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 62 | if (!xtables_strtoui(optarg, NULL, &result, 0, 1)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 63 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 64 | "ECN target: Value out of range"); |
| 65 | einfo->operation |= IPT_ECN_OP_SET_CWR; |
| 66 | einfo->proto.tcp.cwr = result; |
| 67 | *flags |= IPT_ECN_OP_SET_CWR; |
| 68 | break; |
| 69 | case 'H': |
| 70 | if (*flags & IPT_ECN_OP_SET_ECE) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 71 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 72 | "ECN target: Only use --ecn-tcp-ece ONCE!"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 73 | if (!xtables_strtoui(optarg, NULL, &result, 0, 1)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 74 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 75 | "ECN target: Value out of range"); |
| 76 | einfo->operation |= IPT_ECN_OP_SET_ECE; |
| 77 | einfo->proto.tcp.ece = result; |
| 78 | *flags |= IPT_ECN_OP_SET_ECE; |
| 79 | break; |
| 80 | case '9': |
| 81 | if (*flags & IPT_ECN_OP_SET_IP) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 82 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 83 | "ECN target: Only use --ecn-ip-ect ONCE!"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 84 | if (!xtables_strtoui(optarg, NULL, &result, 0, 3)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 85 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 86 | "ECN target: Value out of range"); |
| 87 | einfo->operation |= IPT_ECN_OP_SET_IP; |
| 88 | einfo->ip_ect = result; |
Harald Welte | c05c44f | 2002-08-05 19:36:15 +0000 | [diff] [blame] | 89 | *flags |= IPT_ECN_OP_SET_IP; |
| 90 | break; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return 1; |
| 94 | } |
| 95 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 96 | static void ECN_check(unsigned int flags) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 97 | { |
| 98 | if (!flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 99 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 4e5d4bf | 2011-02-19 19:37:53 +0100 | [diff] [blame] | 100 | "ECN target: An operation is required"); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 103 | static void ECN_print(const void *ip, const struct xt_entry_target *target, |
| 104 | int numeric) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 105 | { |
| 106 | const struct ipt_ECN_info *einfo = |
| 107 | (const struct ipt_ECN_info *)target->data; |
| 108 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 109 | printf(" ECN"); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 110 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 111 | if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR) |
| 112 | && einfo->proto.tcp.ece == 0 |
| 113 | && einfo->proto.tcp.cwr == 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 114 | printf(" TCP remove"); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 115 | else { |
| 116 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 117 | printf(" ECE=%u", einfo->proto.tcp.ece); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 118 | |
| 119 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 120 | printf(" CWR=%u", einfo->proto.tcp.cwr); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 121 | |
| 122 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 123 | printf(" ECT codepoint=%u", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 127 | static void ECN_save(const void *ip, const struct xt_entry_target *target) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 128 | { |
| 129 | const struct ipt_ECN_info *einfo = |
| 130 | (const struct ipt_ECN_info *)target->data; |
| 131 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 132 | if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR) |
| 133 | && einfo->proto.tcp.ece == 0 |
| 134 | && einfo->proto.tcp.cwr == 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 135 | printf(" --ecn-tcp-remove"); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 136 | else { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 137 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 138 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 139 | printf(" --ecn-tcp-ece %d", einfo->proto.tcp.ece); |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 140 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 141 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 142 | printf(" --ecn-tcp-cwr %d", einfo->proto.tcp.cwr); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 143 | |
| 144 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 145 | printf(" --ecn-ip-ect %d", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 149 | static struct xtables_target ecn_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 150 | .name = "ECN", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 151 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 152 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 153 | .size = XT_ALIGN(sizeof(struct ipt_ECN_info)), |
| 154 | .userspacesize = XT_ALIGN(sizeof(struct ipt_ECN_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 155 | .help = ECN_help, |
| 156 | .parse = ECN_parse, |
| 157 | .final_check = ECN_check, |
| 158 | .print = ECN_print, |
| 159 | .save = ECN_save, |
| 160 | .extra_opts = ECN_opts, |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | void _init(void) |
| 164 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 165 | xtables_register_target(&ecn_tg_reg); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 166 | } |