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 | */ |
| 9 | #include <stdio.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 10 | #include <xtables.h> |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 11 | #include <linux/netfilter_ipv4/ipt_ECN.h> |
| 12 | |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 13 | enum { |
| 14 | O_ECN_TCP_REMOVE = 0, |
| 15 | O_ECN_TCP_CWR, |
| 16 | O_ECN_TCP_ECE, |
| 17 | O_ECN_IP_ECT, |
| 18 | F_ECN_TCP_REMOVE = 1 << O_ECN_TCP_REMOVE, |
| 19 | F_ECN_TCP_CWR = 1 << O_ECN_TCP_CWR, |
| 20 | F_ECN_TCP_ECE = 1 << O_ECN_TCP_ECE, |
| 21 | }; |
| 22 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 23 | static void ECN_help(void) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 24 | { |
| 25 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 26 | "ECN target options\n" |
| 27 | " --ecn-tcp-remove Remove all ECN bits from TCP header\n"); |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | #if 0 |
Harald Welte | 1c8fa73 | 2002-05-29 12:43:34 +0000 | [diff] [blame] | 31 | "ECN target v%s EXPERIMENTAL options (use with extreme care!)\n" |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 32 | " --ecn-ip-ect Set the IPv4 ECT codepoint (0 to 3)\n" |
| 33 | " --ecn-tcp-cwr Set the IPv4 CWR bit (0 or 1)\n" |
Harald Welte | 92cad5f | 2002-08-06 06:50:04 +0000 | [diff] [blame] | 34 | " --ecn-tcp-ece Set the IPv4 ECE bit (0 or 1)\n", |
Harald Welte | 31d12a5 | 2002-08-06 18:46:18 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 37 | static const struct xt_option_entry ECN_opts[] = { |
| 38 | {.name = "ecn-tcp-remove", .id = O_ECN_TCP_REMOVE, .type = XTTYPE_NONE, |
| 39 | .excl = F_ECN_TCP_CWR | F_ECN_TCP_ECE}, |
| 40 | {.name = "ecn-tcp-cwr", .id = O_ECN_TCP_CWR, .type = XTTYPE_UINT8, |
| 41 | .min = 0, .max = 1, .excl = F_ECN_TCP_REMOVE}, |
| 42 | {.name = "ecn-tcp-ece", .id = O_ECN_TCP_ECE, .type = XTTYPE_UINT8, |
| 43 | .min = 0, .max = 1, .excl = F_ECN_TCP_REMOVE}, |
| 44 | {.name = "ecn-ip-ect", .id = O_ECN_IP_ECT, .type = XTTYPE_UINT8, |
| 45 | .min = 0, .max = 3}, |
| 46 | XTOPT_TABLEEND, |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 49 | static void ECN_parse(struct xt_option_call *cb) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 50 | { |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 51 | struct ipt_ECN_info *einfo = cb->data; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 52 | |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 53 | xtables_option_parse(cb); |
| 54 | switch (cb->entry->id) { |
| 55 | case O_ECN_TCP_REMOVE: |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 56 | einfo->operation = IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR; |
| 57 | einfo->proto.tcp.ece = 0; |
| 58 | einfo->proto.tcp.cwr = 0; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 59 | break; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 60 | case O_ECN_TCP_CWR: |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 61 | einfo->operation |= IPT_ECN_OP_SET_CWR; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 62 | einfo->proto.tcp.cwr = cb->val.u8; |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 63 | break; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 64 | case O_ECN_TCP_ECE: |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 65 | einfo->operation |= IPT_ECN_OP_SET_ECE; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 66 | einfo->proto.tcp.ece = cb->val.u8; |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 67 | break; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 68 | case O_ECN_IP_ECT: |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 69 | einfo->operation |= IPT_ECN_OP_SET_IP; |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 70 | einfo->ip_ect = cb->val.u8; |
Harald Welte | c05c44f | 2002-08-05 19:36:15 +0000 | [diff] [blame] | 71 | break; |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 72 | } |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 75 | static void ECN_check(struct xt_fcheck_call *cb) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 76 | { |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 77 | if (cb->xflags == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 78 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 4e5d4bf | 2011-02-19 19:37:53 +0100 | [diff] [blame] | 79 | "ECN target: An operation is required"); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 82 | static void ECN_print(const void *ip, const struct xt_entry_target *target, |
| 83 | int numeric) |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 84 | { |
| 85 | const struct ipt_ECN_info *einfo = |
| 86 | (const struct ipt_ECN_info *)target->data; |
| 87 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 88 | printf(" ECN"); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 89 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 90 | if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR) |
| 91 | && einfo->proto.tcp.ece == 0 |
| 92 | && einfo->proto.tcp.cwr == 0) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 93 | printf(" TCP remove"); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 94 | else { |
| 95 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 96 | printf(" ECE=%u", einfo->proto.tcp.ece); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 97 | |
| 98 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 99 | printf(" CWR=%u", einfo->proto.tcp.cwr); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 100 | |
| 101 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 102 | printf(" ECT codepoint=%u", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 106 | 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] | 107 | { |
| 108 | const struct ipt_ECN_info *einfo = |
| 109 | (const struct ipt_ECN_info *)target->data; |
| 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(" --ecn-tcp-remove"); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 115 | else { |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 116 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 117 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 118 | printf(" --ecn-tcp-ece %d", einfo->proto.tcp.ece); |
Harald Welte | c980a24 | 2002-05-29 12:26:13 +0000 | [diff] [blame] | 119 | |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 120 | if (einfo->operation & IPT_ECN_OP_SET_CWR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 121 | printf(" --ecn-tcp-cwr %d", einfo->proto.tcp.cwr); |
Harald Welte | 7b49af4 | 2002-05-29 15:11:36 +0000 | [diff] [blame] | 122 | |
| 123 | if (einfo->operation & IPT_ECN_OP_SET_IP) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 124 | printf(" --ecn-ip-ect %d", einfo->ip_ect); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 128 | static struct xtables_target ecn_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 129 | .name = "ECN", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 130 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 131 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 132 | .size = XT_ALIGN(sizeof(struct ipt_ECN_info)), |
| 133 | .userspacesize = XT_ALIGN(sizeof(struct ipt_ECN_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 134 | .help = ECN_help, |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 135 | .print = ECN_print, |
| 136 | .save = ECN_save, |
Jan Engelhardt | e364632 | 2011-03-03 00:51:16 +0100 | [diff] [blame] | 137 | .x6_parse = ECN_parse, |
| 138 | .x6_fcheck = ECN_check, |
| 139 | .x6_options = ECN_opts, |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | void _init(void) |
| 143 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 144 | xtables_register_target(&ecn_tg_reg); |
Harald Welte | 385a1dd | 2002-02-17 21:32:32 +0000 | [diff] [blame] | 145 | } |