libipt_ECN.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. */
  9. #include <stdio.h>
  10. #include <xtables.h>
  11. #include <linux/netfilter_ipv4/ipt_ECN.h>
  12. enum {
  13. O_ECN_TCP_REMOVE = 0,
  14. O_ECN_TCP_CWR,
  15. O_ECN_TCP_ECE,
  16. O_ECN_IP_ECT,
  17. F_ECN_TCP_REMOVE = 1 << O_ECN_TCP_REMOVE,
  18. F_ECN_TCP_CWR = 1 << O_ECN_TCP_CWR,
  19. F_ECN_TCP_ECE = 1 << O_ECN_TCP_ECE,
  20. };
  21. static void ECN_help(void)
  22. {
  23. printf(
  24. "ECN target options\n"
  25. " --ecn-tcp-remove Remove all ECN bits from TCP header\n");
  26. }
  27. #if 0
  28. "ECN target v%s EXPERIMENTAL options (use with extreme care!)\n"
  29. " --ecn-ip-ect Set the IPv4 ECT codepoint (0 to 3)\n"
  30. " --ecn-tcp-cwr Set the IPv4 CWR bit (0 or 1)\n"
  31. " --ecn-tcp-ece Set the IPv4 ECE bit (0 or 1)\n",
  32. #endif
  33. static const struct xt_option_entry ECN_opts[] = {
  34. {.name = "ecn-tcp-remove", .id = O_ECN_TCP_REMOVE, .type = XTTYPE_NONE,
  35. .excl = F_ECN_TCP_CWR | F_ECN_TCP_ECE},
  36. {.name = "ecn-tcp-cwr", .id = O_ECN_TCP_CWR, .type = XTTYPE_UINT8,
  37. .min = 0, .max = 1, .excl = F_ECN_TCP_REMOVE},
  38. {.name = "ecn-tcp-ece", .id = O_ECN_TCP_ECE, .type = XTTYPE_UINT8,
  39. .min = 0, .max = 1, .excl = F_ECN_TCP_REMOVE},
  40. {.name = "ecn-ip-ect", .id = O_ECN_IP_ECT, .type = XTTYPE_UINT8,
  41. .min = 0, .max = 3},
  42. XTOPT_TABLEEND,
  43. };
  44. static void ECN_parse(struct xt_option_call *cb)
  45. {
  46. struct ipt_ECN_info *einfo = cb->data;
  47. xtables_option_parse(cb);
  48. switch (cb->entry->id) {
  49. case O_ECN_TCP_REMOVE:
  50. einfo->operation = IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR;
  51. einfo->proto.tcp.ece = 0;
  52. einfo->proto.tcp.cwr = 0;
  53. break;
  54. case O_ECN_TCP_CWR:
  55. einfo->operation |= IPT_ECN_OP_SET_CWR;
  56. einfo->proto.tcp.cwr = cb->val.u8;
  57. break;
  58. case O_ECN_TCP_ECE:
  59. einfo->operation |= IPT_ECN_OP_SET_ECE;
  60. einfo->proto.tcp.ece = cb->val.u8;
  61. break;
  62. case O_ECN_IP_ECT:
  63. einfo->operation |= IPT_ECN_OP_SET_IP;
  64. einfo->ip_ect = cb->val.u8;
  65. break;
  66. }
  67. }
  68. static void ECN_check(struct xt_fcheck_call *cb)
  69. {
  70. if (cb->xflags == 0)
  71. xtables_error(PARAMETER_PROBLEM,
  72. "ECN target: An operation is required");
  73. }
  74. static void ECN_print(const void *ip, const struct xt_entry_target *target,
  75. int numeric)
  76. {
  77. const struct ipt_ECN_info *einfo =
  78. (const struct ipt_ECN_info *)target->data;
  79. printf(" ECN");
  80. if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)
  81. && einfo->proto.tcp.ece == 0
  82. && einfo->proto.tcp.cwr == 0)
  83. printf(" TCP remove");
  84. else {
  85. if (einfo->operation & IPT_ECN_OP_SET_ECE)
  86. printf(" ECE=%u", einfo->proto.tcp.ece);
  87. if (einfo->operation & IPT_ECN_OP_SET_CWR)
  88. printf(" CWR=%u", einfo->proto.tcp.cwr);
  89. if (einfo->operation & IPT_ECN_OP_SET_IP)
  90. printf(" ECT codepoint=%u", einfo->ip_ect);
  91. }
  92. }
  93. static void ECN_save(const void *ip, const struct xt_entry_target *target)
  94. {
  95. const struct ipt_ECN_info *einfo =
  96. (const struct ipt_ECN_info *)target->data;
  97. if (einfo->operation == (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)
  98. && einfo->proto.tcp.ece == 0
  99. && einfo->proto.tcp.cwr == 0)
  100. printf(" --ecn-tcp-remove");
  101. else {
  102. if (einfo->operation & IPT_ECN_OP_SET_ECE)
  103. printf(" --ecn-tcp-ece %d", einfo->proto.tcp.ece);
  104. if (einfo->operation & IPT_ECN_OP_SET_CWR)
  105. printf(" --ecn-tcp-cwr %d", einfo->proto.tcp.cwr);
  106. if (einfo->operation & IPT_ECN_OP_SET_IP)
  107. printf(" --ecn-ip-ect %d", einfo->ip_ect);
  108. }
  109. }
  110. static struct xtables_target ecn_tg_reg = {
  111. .name = "ECN",
  112. .version = XTABLES_VERSION,
  113. .family = NFPROTO_IPV4,
  114. .size = XT_ALIGN(sizeof(struct ipt_ECN_info)),
  115. .userspacesize = XT_ALIGN(sizeof(struct ipt_ECN_info)),
  116. .help = ECN_help,
  117. .print = ECN_print,
  118. .save = ECN_save,
  119. .x6_parse = ECN_parse,
  120. .x6_fcheck = ECN_check,
  121. .x6_options = ECN_opts,
  122. };
  123. void _init(void)
  124. {
  125. xtables_register_target(&ecn_tg_reg);
  126. }