Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 7 | #include <xtables.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 8 | #include <linux/netfilter_ipv4/ipt_REJECT.h> |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame] | 9 | #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 Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 19 | |
| 20 | struct reject_names { |
| 21 | const char *name; |
| 22 | const char *alias; |
| 23 | enum ipt_reject_with with; |
| 24 | const char *desc; |
| 25 | }; |
| 26 | |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 27 | enum { |
| 28 | O_REJECT_WITH = 0, |
| 29 | }; |
| 30 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | static const struct reject_names reject_table[] = { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 32 | {"icmp-net-unreachable", "net-unreach", |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 33 | IPT_ICMP_NET_UNREACHABLE, "ICMP network unreachable"}, |
| 34 | {"icmp-host-unreachable", "host-unreach", |
| 35 | IPT_ICMP_HOST_UNREACHABLE, "ICMP host unreachable"}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 36 | {"icmp-proto-unreachable", "proto-unreach", |
| 37 | IPT_ICMP_PROT_UNREACHABLE, "ICMP protocol unreachable"}, |
Harald Welte | 30d920a | 2001-06-16 19:02:25 +0000 | [diff] [blame] | 38 | {"icmp-port-unreachable", "port-unreach", |
| 39 | IPT_ICMP_PORT_UNREACHABLE, "ICMP port unreachable (default)"}, |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 40 | #if 0 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 41 | {"echo-reply", "echoreply", |
Rusty Russell | f7e72d5 | 2000-06-20 19:07:29 +0000 | [diff] [blame] | 42 | IPT_ICMP_ECHOREPLY, "for ICMP echo only: faked ICMP echo reply"}, |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 43 | #endif |
Rusty Russell | f7e72d5 | 2000-06-20 19:07:29 +0000 | [diff] [blame] | 44 | {"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 Welte | 11b8591 | 2005-11-22 08:54:28 +0000 | [diff] [blame] | 48 | {"tcp-reset", "tcp-rst", |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame] | 49 | IPT_TCP_RESET, "TCP RST packet"}, |
| 50 | {"icmp-admin-prohibited", "admin-prohib", |
| 51 | IPT_ICMP_ADMIN_PROHIBITED, "ICMP administratively prohibited (*)"} |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 54 | static void |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 55 | print_reject_types(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 56 | { |
| 57 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 58 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | printf("Valid reject types:\n"); |
| 60 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 61 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 62 | 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 Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 68 | static void REJECT_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 69 | { |
| 70 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 71 | "REJECT target options:\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 72 | "--reject-with type drop input packet and send back\n" |
Harald Welte | 7d77451 | 2005-04-15 09:39:55 +0000 | [diff] [blame] | 73 | " a reply packet according to type:\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | |
| 75 | print_reject_types(); |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame] | 76 | |
| 77 | printf("(*) See man page or read the INCOMPATIBILITES file for compatibility issues.\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 80 | static const struct xt_option_entry REJECT_opts[] = { |
| 81 | {.name = "reject-with", .id = O_REJECT_WITH, .type = XTTYPE_STRING}, |
| 82 | XTOPT_TABLEEND, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 85 | static void REJECT_init(struct xt_entry_target *t) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 86 | { |
| 87 | struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 88 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 89 | /* default */ |
| 90 | reject->with = IPT_ICMP_PORT_UNREACHABLE; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 91 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 94 | static void REJECT_parse(struct xt_option_call *cb) |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 95 | { |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 96 | struct ipt_reject_info *reject = cb->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 97 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 98 | |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 99 | 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 Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 107 | } |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 108 | /* 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 Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 115 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 116 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 117 | static void REJECT_print(const void *ip, const struct xt_entry_target *target, |
| 118 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 119 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 120 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 121 | = (const struct ipt_reject_info *)target->data; |
| 122 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 123 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 124 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 125 | if (reject_table[i].with == reject->with) |
| 126 | break; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 127 | printf(" reject-with %s", reject_table[i].name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 130 | static void REJECT_save(const void *ip, const struct xt_entry_target *target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 131 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 132 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | = (const struct ipt_reject_info *)target->data; |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 134 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 135 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 136 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 137 | if (reject_table[i].with == reject->with) |
| 138 | break; |
| 139 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 140 | printf(" --reject-with %s", reject_table[i].name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 143 | static struct xtables_target reject_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 144 | .name = "REJECT", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 145 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 146 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 147 | .size = XT_ALIGN(sizeof(struct ipt_reject_info)), |
| 148 | .userspacesize = XT_ALIGN(sizeof(struct ipt_reject_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 149 | .help = REJECT_help, |
| 150 | .init = REJECT_init, |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 151 | .print = REJECT_print, |
| 152 | .save = REJECT_save, |
Jan Engelhardt | b313d8f | 2011-02-16 01:16:39 +0100 | [diff] [blame] | 153 | .x6_parse = REJECT_parse, |
| 154 | .x6_options = REJECT_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | void _init(void) |
| 158 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 159 | xtables_register_target(&reject_tg_reg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 160 | } |