1 /* Shared library add-on to iptables to add customized REJECT support.
3 * (C) 2000 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
10 #include <linux/netfilter_ipv4/ipt_REJECT.h>
11 #include <linux/version.h>
13 /* If we are compiling against a kernel that does not support
14 * IPT_ICMP_ADMIN_PROHIBITED, we are emulating it.
15 * The result will be a plain DROP of the packet instead of
16 * reject. -- Maciej Soltysiak <solt@dns.toxicfilms.tv>
18 #ifndef IPT_ICMP_ADMIN_PROHIBITED
19 #define IPT_ICMP_ADMIN_PROHIBITED IPT_TCP_RESET + 1
25 enum ipt_reject_with with;
29 static const struct reject_names reject_table[] = {
30 {"icmp-net-unreachable", "net-unreach",
31 IPT_ICMP_NET_UNREACHABLE, "ICMP network unreachable"},
32 {"icmp-host-unreachable", "host-unreach",
33 IPT_ICMP_HOST_UNREACHABLE, "ICMP host unreachable"},
34 {"icmp-proto-unreachable", "proto-unreach",
35 IPT_ICMP_PROT_UNREACHABLE, "ICMP protocol unreachable"},
36 {"icmp-port-unreachable", "port-unreach",
37 IPT_ICMP_PORT_UNREACHABLE, "ICMP port unreachable (default)"},
39 {"echo-reply", "echoreply",
40 IPT_ICMP_ECHOREPLY, "for ICMP echo only: faked ICMP echo reply"},
42 {"icmp-net-prohibited", "net-prohib",
43 IPT_ICMP_NET_PROHIBITED, "ICMP network prohibited"},
44 {"icmp-host-prohibited", "host-prohib",
45 IPT_ICMP_HOST_PROHIBITED, "ICMP host prohibited"},
46 {"tcp-reset", "tcp-rst",
47 IPT_TCP_RESET, "TCP RST packet"},
48 {"icmp-admin-prohibited", "admin-prohib",
49 IPT_ICMP_ADMIN_PROHIBITED, "ICMP administratively prohibited (*)"}
53 print_reject_types(void)
57 printf("Valid reject types:\n");
59 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
60 printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc);
61 printf(" %-25s\talias\n", reject_table[i].alias);
66 static void REJECT_help(void)
69 "REJECT target options:\n"
70 "--reject-with type drop input packet and send back\n"
71 " a reply packet according to type:\n");
75 printf("(*) See man page or read the INCOMPATIBILITES file for compatibility issues.\n");
78 static const struct option REJECT_opts[] = {
79 { "reject-with", 1, NULL, '1' },
83 static void REJECT_init(struct xt_entry_target *t)
85 struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data;
88 reject->with = IPT_ICMP_PORT_UNREACHABLE;
92 static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags,
93 const void *entry, struct xt_entry_target **target)
95 struct ipt_reject_info *reject = (struct ipt_reject_info *)(*target)->data;
96 static const unsigned int limit = ARRAY_SIZE(reject_table);
101 if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
102 xtables_error(PARAMETER_PROBLEM,
103 "Unexpected `!' after --reject-with");
104 for (i = 0; i < limit; i++) {
105 if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0)
106 || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) {
107 reject->with = reject_table[i].with;
111 /* This due to be dropped late in 2.4 pre-release cycle --RR */
112 if (strncasecmp("echo-reply", optarg, strlen(optarg)) == 0
113 || strncasecmp("echoreply", optarg, strlen(optarg)) == 0)
114 fprintf(stderr, "--reject-with echo-reply no longer"
116 xtables_error(PARAMETER_PROBLEM, "unknown reject type \"%s\"", optarg);
124 static void REJECT_print(const void *ip, const struct xt_entry_target *target,
127 const struct ipt_reject_info *reject
128 = (const struct ipt_reject_info *)target->data;
131 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
132 if (reject_table[i].with == reject->with)
134 printf("reject-with %s ", reject_table[i].name);
137 static void REJECT_save(const void *ip, const struct xt_entry_target *target)
139 const struct ipt_reject_info *reject
140 = (const struct ipt_reject_info *)target->data;
143 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
144 if (reject_table[i].with == reject->with)
147 printf("--reject-with %s ", reject_table[i].name);
150 static struct xtables_target reject_tg_reg = {
152 .version = XTABLES_VERSION,
153 .family = NFPROTO_IPV4,
154 .size = XT_ALIGN(sizeof(struct ipt_reject_info)),
155 .userspacesize = XT_ALIGN(sizeof(struct ipt_reject_info)),
158 .parse = REJECT_parse,
159 .print = REJECT_print,
161 .extra_opts = REJECT_opts,
166 xtables_register_target(&reject_tg_reg);