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> |
| 7 | #include <stdlib.h> |
| 8 | #include <getopt.h> |
| 9 | #include <iptables.h> |
| 10 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 11 | #include <linux/netfilter_ipv4/ipt_REJECT.h> |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame^] | 12 | #include <linux/version.h> |
| 13 | |
| 14 | /* If we are compiling against a kernel that does not support |
| 15 | * IPT_ICMP_ADMIN_PROHIBITED, we are emulating it. |
| 16 | * The result will be a plain DROP of the packet instead of |
| 17 | * reject. -- Maciej Soltysiak <solt@dns.toxicfilms.tv> |
| 18 | */ |
| 19 | #ifndef IPT_ICMP_ADMIN_PROHIBITED |
| 20 | #define IPT_ICMP_ADMIN_PROHIBITED IPT_TCP_RESET + 1 |
| 21 | #endif |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | |
| 23 | struct reject_names { |
| 24 | const char *name; |
| 25 | const char *alias; |
| 26 | enum ipt_reject_with with; |
| 27 | const char *desc; |
| 28 | }; |
| 29 | |
| 30 | static const struct reject_names reject_table[] = { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 31 | {"icmp-net-unreachable", "net-unreach", |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 32 | IPT_ICMP_NET_UNREACHABLE, "ICMP network unreachable"}, |
| 33 | {"icmp-host-unreachable", "host-unreach", |
| 34 | IPT_ICMP_HOST_UNREACHABLE, "ICMP host unreachable"}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 35 | {"icmp-proto-unreachable", "proto-unreach", |
| 36 | IPT_ICMP_PROT_UNREACHABLE, "ICMP protocol unreachable"}, |
Harald Welte | 30d920a | 2001-06-16 19:02:25 +0000 | [diff] [blame] | 37 | {"icmp-port-unreachable", "port-unreach", |
| 38 | IPT_ICMP_PORT_UNREACHABLE, "ICMP port unreachable (default)"}, |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 39 | #if 0 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 40 | {"echo-reply", "echoreply", |
Rusty Russell | f7e72d5 | 2000-06-20 19:07:29 +0000 | [diff] [blame] | 41 | IPT_ICMP_ECHOREPLY, "for ICMP echo only: faked ICMP echo reply"}, |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 42 | #endif |
Rusty Russell | f7e72d5 | 2000-06-20 19:07:29 +0000 | [diff] [blame] | 43 | {"icmp-net-prohibited", "net-prohib", |
| 44 | IPT_ICMP_NET_PROHIBITED, "ICMP network prohibited"}, |
| 45 | {"icmp-host-prohibited", "host-prohib", |
| 46 | IPT_ICMP_HOST_PROHIBITED, "ICMP host prohibited"}, |
Rusty Russell | 52e440e | 2000-07-12 07:30:55 +0000 | [diff] [blame] | 47 | {"tcp-reset", "tcp-reset", |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame^] | 48 | IPT_TCP_RESET, "TCP RST packet"}, |
| 49 | {"icmp-admin-prohibited", "admin-prohib", |
| 50 | IPT_ICMP_ADMIN_PROHIBITED, "ICMP administratively prohibited (*)"} |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 53 | static void |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 54 | print_reject_types() |
| 55 | { |
| 56 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 57 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 58 | printf("Valid reject types:\n"); |
| 59 | |
| 60 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 61 | printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc); |
| 62 | printf(" %-25s\talias\n", reject_table[i].alias); |
| 63 | } |
| 64 | printf("\n"); |
| 65 | } |
| 66 | |
| 67 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 68 | |
| 69 | /* Function which prints out usage message. */ |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 70 | static void |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | help(void) |
| 72 | { |
| 73 | printf( |
| 74 | "REJECT options:\n" |
| 75 | "--reject-with type drop input packet and send back\n" |
| 76 | " a reply packet according to type:\n"); |
| 77 | |
| 78 | print_reject_types(); |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame^] | 79 | |
| 80 | 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] | 81 | } |
| 82 | |
| 83 | static struct option opts[] = { |
| 84 | { "reject-with", 1, 0, '1' }, |
| 85 | { 0 } |
| 86 | }; |
| 87 | |
| 88 | /* Allocate and initialize the target. */ |
| 89 | static void |
| 90 | init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 91 | { |
| 92 | struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 93 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 94 | /* default */ |
| 95 | reject->with = IPT_ICMP_PORT_UNREACHABLE; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 96 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 97 | /* Can't cache this */ |
| 98 | *nfcache |= NFC_UNKNOWN; |
| 99 | } |
| 100 | |
| 101 | /* Function which parses command options; returns true if it |
| 102 | ate an option */ |
| 103 | static int |
| 104 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 105 | const struct ipt_entry *entry, |
| 106 | struct ipt_entry_target **target) |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 107 | { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 108 | struct ipt_reject_info *reject = (struct ipt_reject_info *)(*target)->data; |
| 109 | unsigned int limit = sizeof(reject_table)/sizeof(struct reject_names); |
| 110 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 111 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 112 | switch(c) { |
| 113 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 114 | if (check_inverse(optarg, &invert, NULL, 0)) |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 115 | exit_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 116 | "Unexpected `!' after --reject-with"); |
| 117 | for (i = 0; i < limit; i++) { |
| 118 | if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0) |
| 119 | || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) { |
| 120 | reject->with = reject_table[i].with; |
| 121 | return 1; |
| 122 | } |
| 123 | } |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 124 | /* This due to be dropped late in 2.4 pre-release cycle --RR */ |
| 125 | if (strncasecmp("echo-reply", optarg, strlen(optarg)) == 0 |
| 126 | || strncasecmp("echoreply", optarg, strlen(optarg)) == 0) |
| 127 | fprintf(stderr, "--reject-with echo-reply no longer" |
| 128 | " supported\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 129 | exit_error(PARAMETER_PROBLEM, "unknown reject type `%s'",optarg); |
| 130 | default: |
| 131 | /* Fall through */ |
Marc Boucher | 6e9bfc7 | 2002-01-19 12:48:05 +0000 | [diff] [blame] | 132 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | } |
| 134 | return 0; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 135 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 136 | |
| 137 | /* Final check; nothing. */ |
| 138 | static void final_check(unsigned int flags) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | /* Prints out ipt_reject_info. */ |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 143 | static void |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 144 | print(const struct ipt_ip *ip, |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 145 | const struct ipt_entry_target *target, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 146 | int numeric) |
| 147 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 148 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 149 | = (const struct ipt_reject_info *)target->data; |
| 150 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 151 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 152 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 153 | if (reject_table[i].with == reject->with) |
| 154 | break; |
| 155 | } |
| 156 | printf("reject-with %s ", reject_table[i].name); |
| 157 | } |
| 158 | |
| 159 | /* Saves ipt_reject in parsable form to stdout. */ |
| 160 | static void save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 161 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 162 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 163 | = (const struct ipt_reject_info *)target->data; |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 164 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 165 | |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 166 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) |
| 167 | if (reject_table[i].with == reject->with) |
| 168 | break; |
| 169 | |
| 170 | printf("--reject-with %s ", reject_table[i].name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 173 | static |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 174 | struct iptables_target reject |
| 175 | = { NULL, |
| 176 | "REJECT", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 177 | IPTABLES_VERSION, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 178 | IPT_ALIGN(sizeof(struct ipt_reject_info)), |
| 179 | IPT_ALIGN(sizeof(struct ipt_reject_info)), |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 180 | &help, |
| 181 | &init, |
| 182 | &parse, |
| 183 | &final_check, |
| 184 | &print, |
| 185 | &save, |
| 186 | opts |
| 187 | }; |
| 188 | |
| 189 | void _init(void) |
| 190 | { |
| 191 | register_target(&reject); |
| 192 | } |