libipt_REJECT.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Shared library add-on to iptables to add customized REJECT support.
  2. *
  3. * (C) 2000 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <xtables.h>
  8. #include <linux/netfilter_ipv4/ipt_REJECT.h>
  9. #include <linux/version.h>
  10. /* If we are compiling against a kernel that does not support
  11. * IPT_ICMP_ADMIN_PROHIBITED, we are emulating it.
  12. * The result will be a plain DROP of the packet instead of
  13. * reject. -- Maciej Soltysiak <solt@dns.toxicfilms.tv>
  14. */
  15. #ifndef IPT_ICMP_ADMIN_PROHIBITED
  16. #define IPT_ICMP_ADMIN_PROHIBITED IPT_TCP_RESET + 1
  17. #endif
  18. struct reject_names {
  19. const char *name;
  20. const char *alias;
  21. enum ipt_reject_with with;
  22. const char *desc;
  23. };
  24. enum {
  25. O_REJECT_WITH = 0,
  26. };
  27. static const struct reject_names reject_table[] = {
  28. {"icmp-net-unreachable", "net-unreach",
  29. IPT_ICMP_NET_UNREACHABLE, "ICMP network unreachable"},
  30. {"icmp-host-unreachable", "host-unreach",
  31. IPT_ICMP_HOST_UNREACHABLE, "ICMP host unreachable"},
  32. {"icmp-proto-unreachable", "proto-unreach",
  33. IPT_ICMP_PROT_UNREACHABLE, "ICMP protocol unreachable"},
  34. {"icmp-port-unreachable", "port-unreach",
  35. IPT_ICMP_PORT_UNREACHABLE, "ICMP port unreachable (default)"},
  36. #if 0
  37. {"echo-reply", "echoreply",
  38. IPT_ICMP_ECHOREPLY, "for ICMP echo only: faked ICMP echo reply"},
  39. #endif
  40. {"icmp-net-prohibited", "net-prohib",
  41. IPT_ICMP_NET_PROHIBITED, "ICMP network prohibited"},
  42. {"icmp-host-prohibited", "host-prohib",
  43. IPT_ICMP_HOST_PROHIBITED, "ICMP host prohibited"},
  44. {"tcp-reset", "tcp-rst",
  45. IPT_TCP_RESET, "TCP RST packet"},
  46. {"icmp-admin-prohibited", "admin-prohib",
  47. IPT_ICMP_ADMIN_PROHIBITED, "ICMP administratively prohibited (*)"}
  48. };
  49. static void
  50. print_reject_types(void)
  51. {
  52. unsigned int i;
  53. printf("Valid reject types:\n");
  54. for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
  55. printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc);
  56. printf(" %-25s\talias\n", reject_table[i].alias);
  57. }
  58. printf("\n");
  59. }
  60. static void REJECT_help(void)
  61. {
  62. printf(
  63. "REJECT target options:\n"
  64. "--reject-with type drop input packet and send back\n"
  65. " a reply packet according to type:\n");
  66. print_reject_types();
  67. printf("(*) See man page or read the INCOMPATIBILITES file for compatibility issues.\n");
  68. }
  69. static const struct xt_option_entry REJECT_opts[] = {
  70. {.name = "reject-with", .id = O_REJECT_WITH, .type = XTTYPE_STRING},
  71. XTOPT_TABLEEND,
  72. };
  73. static void REJECT_init(struct xt_entry_target *t)
  74. {
  75. struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data;
  76. /* default */
  77. reject->with = IPT_ICMP_PORT_UNREACHABLE;
  78. }
  79. static void REJECT_parse(struct xt_option_call *cb)
  80. {
  81. struct ipt_reject_info *reject = cb->data;
  82. unsigned int i;
  83. xtables_option_parse(cb);
  84. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  85. if (strncasecmp(reject_table[i].name,
  86. cb->arg, strlen(cb->arg)) == 0 ||
  87. strncasecmp(reject_table[i].alias,
  88. cb->arg, strlen(cb->arg)) == 0) {
  89. reject->with = reject_table[i].with;
  90. return;
  91. }
  92. /* This due to be dropped late in 2.4 pre-release cycle --RR */
  93. if (strncasecmp("echo-reply", cb->arg, strlen(cb->arg)) == 0 ||
  94. strncasecmp("echoreply", cb->arg, strlen(cb->arg)) == 0)
  95. fprintf(stderr, "--reject-with echo-reply no longer"
  96. " supported\n");
  97. xtables_error(PARAMETER_PROBLEM,
  98. "unknown reject type \"%s\"", cb->arg);
  99. }
  100. static void REJECT_print(const void *ip, const struct xt_entry_target *target,
  101. int numeric)
  102. {
  103. const struct ipt_reject_info *reject
  104. = (const struct ipt_reject_info *)target->data;
  105. unsigned int i;
  106. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  107. if (reject_table[i].with == reject->with)
  108. break;
  109. printf(" reject-with %s", reject_table[i].name);
  110. }
  111. static void REJECT_save(const void *ip, const struct xt_entry_target *target)
  112. {
  113. const struct ipt_reject_info *reject
  114. = (const struct ipt_reject_info *)target->data;
  115. unsigned int i;
  116. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  117. if (reject_table[i].with == reject->with)
  118. break;
  119. printf(" --reject-with %s", reject_table[i].name);
  120. }
  121. static struct xtables_target reject_tg_reg = {
  122. .name = "REJECT",
  123. .version = XTABLES_VERSION,
  124. .family = NFPROTO_IPV4,
  125. .size = XT_ALIGN(sizeof(struct ipt_reject_info)),
  126. .userspacesize = XT_ALIGN(sizeof(struct ipt_reject_info)),
  127. .help = REJECT_help,
  128. .init = REJECT_init,
  129. .print = REJECT_print,
  130. .save = REJECT_save,
  131. .x6_parse = REJECT_parse,
  132. .x6_options = REJECT_opts,
  133. };
  134. void _init(void)
  135. {
  136. xtables_register_target(&reject_tg_reg);
  137. }