blob: 362c65ed88e9617e1c5e6b759e984fe38510613d [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01007#include <xtables.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <linux/netfilter_ipv4/ipt_REJECT.h>
Harald Welte5a52c512003-05-24 11:44:18 +00009#include <linux/version.h>
10
11/* If we are compiling against a kernel that does not support
12 * IPT_ICMP_ADMIN_PROHIBITED, we are emulating it.
13 * The result will be a plain DROP of the packet instead of
14 * reject. -- Maciej Soltysiak <solt@dns.toxicfilms.tv>
15 */
16#ifndef IPT_ICMP_ADMIN_PROHIBITED
17#define IPT_ICMP_ADMIN_PROHIBITED IPT_TCP_RESET + 1
18#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000019
20struct reject_names {
21 const char *name;
22 const char *alias;
23 enum ipt_reject_with with;
24 const char *desc;
25};
26
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010027enum {
28 O_REJECT_WITH = 0,
29};
30
Marc Bouchere6869a82000-03-20 06:03:29 +000031static const struct reject_names reject_table[] = {
Rusty Russell7e53bf92000-03-20 07:03:28 +000032 {"icmp-net-unreachable", "net-unreach",
Marc Bouchere6869a82000-03-20 06:03:29 +000033 IPT_ICMP_NET_UNREACHABLE, "ICMP network unreachable"},
34 {"icmp-host-unreachable", "host-unreach",
35 IPT_ICMP_HOST_UNREACHABLE, "ICMP host unreachable"},
Marc Bouchere6869a82000-03-20 06:03:29 +000036 {"icmp-proto-unreachable", "proto-unreach",
37 IPT_ICMP_PROT_UNREACHABLE, "ICMP protocol unreachable"},
Harald Welte30d920a2001-06-16 19:02:25 +000038 {"icmp-port-unreachable", "port-unreach",
39 IPT_ICMP_PORT_UNREACHABLE, "ICMP port unreachable (default)"},
Rusty Russellbd8382b2000-12-18 05:09:52 +000040#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +000041 {"echo-reply", "echoreply",
Rusty Russellf7e72d52000-06-20 19:07:29 +000042 IPT_ICMP_ECHOREPLY, "for ICMP echo only: faked ICMP echo reply"},
Rusty Russellbd8382b2000-12-18 05:09:52 +000043#endif
Rusty Russellf7e72d52000-06-20 19:07:29 +000044 {"icmp-net-prohibited", "net-prohib",
45 IPT_ICMP_NET_PROHIBITED, "ICMP network prohibited"},
46 {"icmp-host-prohibited", "host-prohib",
47 IPT_ICMP_HOST_PROHIBITED, "ICMP host prohibited"},
Harald Welte11b85912005-11-22 08:54:28 +000048 {"tcp-reset", "tcp-rst",
Harald Welte5a52c512003-05-24 11:44:18 +000049 IPT_TCP_RESET, "TCP RST packet"},
50 {"icmp-admin-prohibited", "admin-prohib",
51 IPT_ICMP_ADMIN_PROHIBITED, "ICMP administratively prohibited (*)"}
Marc Bouchere6869a82000-03-20 06:03:29 +000052};
53
Rusty Russell7e53bf92000-03-20 07:03:28 +000054static void
Patrick McHardy500f4832007-09-08 15:59:04 +000055print_reject_types(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000056{
57 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +000058
Marc Bouchere6869a82000-03-20 06:03:29 +000059 printf("Valid reject types:\n");
60
Jan Engelhardt2c69b552009-04-30 19:32:02 +020061 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
Marc Bouchere6869a82000-03-20 06:03:29 +000062 printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc);
63 printf(" %-25s\talias\n", reject_table[i].alias);
64 }
65 printf("\n");
66}
67
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000068static void REJECT_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000069{
70 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020071"REJECT target options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000072"--reject-with type drop input packet and send back\n"
Harald Welte7d774512005-04-15 09:39:55 +000073" a reply packet according to type:\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000074
75 print_reject_types();
Harald Welte5a52c512003-05-24 11:44:18 +000076
77 printf("(*) See man page or read the INCOMPATIBILITES file for compatibility issues.\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000078}
79
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010080static const struct xt_option_entry REJECT_opts[] = {
81 {.name = "reject-with", .id = O_REJECT_WITH, .type = XTTYPE_STRING},
82 XTOPT_TABLEEND,
Marc Bouchere6869a82000-03-20 06:03:29 +000083};
84
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000085static void REJECT_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000086{
87 struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data;
Rusty Russell7e53bf92000-03-20 07:03:28 +000088
Marc Bouchere6869a82000-03-20 06:03:29 +000089 /* default */
90 reject->with = IPT_ICMP_PORT_UNREACHABLE;
Rusty Russell7e53bf92000-03-20 07:03:28 +000091
Marc Bouchere6869a82000-03-20 06:03:29 +000092}
93
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010094static void REJECT_parse(struct xt_option_call *cb)
Rusty Russell7e53bf92000-03-20 07:03:28 +000095{
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010096 struct ipt_reject_info *reject = cb->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000097 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +000098
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010099 xtables_option_parse(cb);
100 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
101 if (strncasecmp(reject_table[i].name,
102 cb->arg, strlen(cb->arg)) == 0 ||
103 strncasecmp(reject_table[i].alias,
104 cb->arg, strlen(cb->arg)) == 0) {
105 reject->with = reject_table[i].with;
106 return;
Marc Bouchere6869a82000-03-20 06:03:29 +0000107 }
Jan Engelhardtb313d8f2011-02-16 01:16:39 +0100108 /* This due to be dropped late in 2.4 pre-release cycle --RR */
109 if (strncasecmp("echo-reply", cb->arg, strlen(cb->arg)) == 0 ||
110 strncasecmp("echoreply", cb->arg, strlen(cb->arg)) == 0)
111 fprintf(stderr, "--reject-with echo-reply no longer"
112 " supported\n");
113 xtables_error(PARAMETER_PROBLEM,
114 "unknown reject type \"%s\"", cb->arg);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000115}
Marc Bouchere6869a82000-03-20 06:03:29 +0000116
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000117static void REJECT_print(const void *ip, const struct xt_entry_target *target,
118 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000119{
Rusty Russell7e53bf92000-03-20 07:03:28 +0000120 const struct ipt_reject_info *reject
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 = (const struct ipt_reject_info *)target->data;
122 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000123
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200124 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Marc Bouchere6869a82000-03-20 06:03:29 +0000125 if (reject_table[i].with == reject->with)
126 break;
Jan Engelhardt73866352010-12-18 02:04:59 +0100127 printf(" reject-with %s", reject_table[i].name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000128}
129
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000130static void REJECT_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000131{
Rusty Russell7e53bf92000-03-20 07:03:28 +0000132 const struct ipt_reject_info *reject
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 = (const struct ipt_reject_info *)target->data;
Harald Welte97b3fde2001-05-12 05:22:18 +0000134 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000135
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200136 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Welte97b3fde2001-05-12 05:22:18 +0000137 if (reject_table[i].with == reject->with)
138 break;
139
Jan Engelhardt73866352010-12-18 02:04:59 +0100140 printf(" --reject-with %s", reject_table[i].name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000141}
142
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200143static struct xtables_target reject_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000144 .name = "REJECT",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200145 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100146 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200147 .size = XT_ALIGN(sizeof(struct ipt_reject_info)),
148 .userspacesize = XT_ALIGN(sizeof(struct ipt_reject_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000149 .help = REJECT_help,
150 .init = REJECT_init,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000151 .print = REJECT_print,
152 .save = REJECT_save,
Jan Engelhardtb313d8f2011-02-16 01:16:39 +0100153 .x6_parse = REJECT_parse,
154 .x6_options = REJECT_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000155};
156
157void _init(void)
158{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200159 xtables_register_target(&reject_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000160}