libip6t_REJECT.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Shared library add-on to ip6tables to add customized REJECT support.
  2. *
  3. * (C) 2000 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  4. *
  5. * ported to IPv6 by Harald Welte <laforge@gnumonks.org>
  6. *
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <xtables.h>
  11. #include <linux/netfilter_ipv6/ip6t_REJECT.h>
  12. struct reject_names {
  13. const char *name;
  14. const char *alias;
  15. enum ip6t_reject_with with;
  16. const char *desc;
  17. };
  18. enum {
  19. O_REJECT_WITH = 0,
  20. };
  21. static const struct reject_names reject_table[] = {
  22. {"icmp6-no-route", "no-route",
  23. IP6T_ICMP6_NO_ROUTE, "ICMPv6 no route"},
  24. {"icmp6-adm-prohibited", "adm-prohibited",
  25. IP6T_ICMP6_ADM_PROHIBITED, "ICMPv6 administratively prohibited"},
  26. #if 0
  27. {"icmp6-not-neighbor", "not-neighbor"},
  28. IP6T_ICMP6_NOT_NEIGHBOR, "ICMPv6 not a neighbor"},
  29. #endif
  30. {"icmp6-addr-unreachable", "addr-unreach",
  31. IP6T_ICMP6_ADDR_UNREACH, "ICMPv6 address unreachable"},
  32. {"icmp6-port-unreachable", "port-unreach",
  33. IP6T_ICMP6_PORT_UNREACH, "ICMPv6 port unreachable"},
  34. {"tcp-reset", "tcp-reset",
  35. IP6T_TCP_RESET, "TCP RST packet"}
  36. };
  37. static void
  38. print_reject_types(void)
  39. {
  40. unsigned int i;
  41. printf("Valid reject types:\n");
  42. for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
  43. printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc);
  44. printf(" %-25s\talias\n", reject_table[i].alias);
  45. }
  46. printf("\n");
  47. }
  48. static void REJECT_help(void)
  49. {
  50. printf(
  51. "REJECT target options:\n"
  52. "--reject-with type drop input packet and send back\n"
  53. " a reply packet according to type:\n");
  54. print_reject_types();
  55. }
  56. static const struct xt_option_entry REJECT_opts[] = {
  57. {.name = "reject-with", .id = O_REJECT_WITH, .type = XTTYPE_STRING},
  58. XTOPT_TABLEEND,
  59. };
  60. static void REJECT_init(struct xt_entry_target *t)
  61. {
  62. struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data;
  63. /* default */
  64. reject->with = IP6T_ICMP6_PORT_UNREACH;
  65. }
  66. static void REJECT_parse(struct xt_option_call *cb)
  67. {
  68. struct ip6t_reject_info *reject = cb->data;
  69. unsigned int i;
  70. xtables_option_parse(cb);
  71. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  72. if (strncasecmp(reject_table[i].name,
  73. cb->arg, strlen(cb->arg)) == 0 ||
  74. strncasecmp(reject_table[i].alias,
  75. cb->arg, strlen(cb->arg)) == 0) {
  76. reject->with = reject_table[i].with;
  77. return;
  78. }
  79. xtables_error(PARAMETER_PROBLEM,
  80. "unknown reject type \"%s\"", cb->arg);
  81. }
  82. static void REJECT_print(const void *ip, const struct xt_entry_target *target,
  83. int numeric)
  84. {
  85. const struct ip6t_reject_info *reject
  86. = (const struct ip6t_reject_info *)target->data;
  87. unsigned int i;
  88. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  89. if (reject_table[i].with == reject->with)
  90. break;
  91. printf(" reject-with %s", reject_table[i].name);
  92. }
  93. static void REJECT_save(const void *ip, const struct xt_entry_target *target)
  94. {
  95. const struct ip6t_reject_info *reject
  96. = (const struct ip6t_reject_info *)target->data;
  97. unsigned int i;
  98. for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
  99. if (reject_table[i].with == reject->with)
  100. break;
  101. printf(" --reject-with %s", reject_table[i].name);
  102. }
  103. static struct xtables_target reject_tg6_reg = {
  104. .name = "REJECT",
  105. .version = XTABLES_VERSION,
  106. .family = NFPROTO_IPV6,
  107. .size = XT_ALIGN(sizeof(struct ip6t_reject_info)),
  108. .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)),
  109. .help = REJECT_help,
  110. .init = REJECT_init,
  111. .print = REJECT_print,
  112. .save = REJECT_save,
  113. .x6_parse = REJECT_parse,
  114. .x6_options = REJECT_opts,
  115. };
  116. void _init(void)
  117. {
  118. xtables_register_target(&reject_tg6_reg);
  119. }