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"}, |
Harald Welte | 11b8591 | 2005-11-22 08:54:28 +0000 | [diff] [blame] | 47 | {"tcp-reset", "tcp-rst", |
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 |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 54 | print_reject_types(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 70 | static void REJECT_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | { |
| 72 | printf( |
| 73 | "REJECT options:\n" |
| 74 | "--reject-with type drop input packet and send back\n" |
Harald Welte | 7d77451 | 2005-04-15 09:39:55 +0000 | [diff] [blame] | 75 | " a reply packet according to type:\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 76 | |
| 77 | print_reject_types(); |
Harald Welte | 5a52c51 | 2003-05-24 11:44:18 +0000 | [diff] [blame] | 78 | |
| 79 | 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] | 80 | } |
| 81 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 82 | static const struct option REJECT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 83 | { "reject-with", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 84 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* Allocate and initialize the target. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 88 | static void REJECT_init(struct xt_entry_target *t) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 89 | { |
| 90 | struct ipt_reject_info *reject = (struct ipt_reject_info *)t->data; |
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 | /* default */ |
| 93 | reject->with = IPT_ICMP_PORT_UNREACHABLE; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 94 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Function which parses command options; returns true if it |
| 98 | ate an option */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 99 | static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 100 | const void *entry, struct xt_entry_target **target) |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 101 | { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 102 | struct ipt_reject_info *reject = (struct ipt_reject_info *)(*target)->data; |
| 103 | unsigned int limit = sizeof(reject_table)/sizeof(struct reject_names); |
| 104 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 105 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 106 | switch(c) { |
| 107 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 108 | if (check_inverse(optarg, &invert, NULL, 0)) |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 109 | exit_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 110 | "Unexpected `!' after --reject-with"); |
| 111 | for (i = 0; i < limit; i++) { |
| 112 | if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0) |
| 113 | || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) { |
| 114 | reject->with = reject_table[i].with; |
| 115 | return 1; |
| 116 | } |
| 117 | } |
Rusty Russell | bd8382b | 2000-12-18 05:09:52 +0000 | [diff] [blame] | 118 | /* This due to be dropped late in 2.4 pre-release cycle --RR */ |
| 119 | if (strncasecmp("echo-reply", optarg, strlen(optarg)) == 0 |
| 120 | || strncasecmp("echoreply", optarg, strlen(optarg)) == 0) |
| 121 | fprintf(stderr, "--reject-with echo-reply no longer" |
| 122 | " supported\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 123 | exit_error(PARAMETER_PROBLEM, "unknown reject type `%s'",optarg); |
| 124 | default: |
| 125 | /* Fall through */ |
Marc Boucher | 6e9bfc7 | 2002-01-19 12:48:05 +0000 | [diff] [blame] | 126 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | } |
| 128 | return 0; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 129 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 130 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 131 | /* Prints out ipt_reject_info. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 132 | static void REJECT_print(const void *ip, const struct xt_entry_target *target, |
| 133 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 135 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 136 | = (const struct ipt_reject_info *)target->data; |
| 137 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 138 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 139 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 140 | if (reject_table[i].with == reject->with) |
| 141 | break; |
| 142 | } |
| 143 | printf("reject-with %s ", reject_table[i].name); |
| 144 | } |
| 145 | |
| 146 | /* Saves ipt_reject in parsable form to stdout. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 147 | 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] | 148 | { |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 149 | const struct ipt_reject_info *reject |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | = (const struct ipt_reject_info *)target->data; |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 151 | unsigned int i; |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 152 | |
Harald Welte | 97b3fde | 2001-05-12 05:22:18 +0000 | [diff] [blame] | 153 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) |
| 154 | if (reject_table[i].with == reject->with) |
| 155 | break; |
| 156 | |
| 157 | printf("--reject-with %s ", reject_table[i].name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 160 | static struct iptables_target reject_target = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 161 | .name = "REJECT", |
| 162 | .version = IPTABLES_VERSION, |
| 163 | .size = IPT_ALIGN(sizeof(struct ipt_reject_info)), |
| 164 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_reject_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 165 | .help = REJECT_help, |
| 166 | .init = REJECT_init, |
| 167 | .parse = REJECT_parse, |
| 168 | .print = REJECT_print, |
| 169 | .save = REJECT_save, |
| 170 | .extra_opts = REJECT_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | void _init(void) |
| 174 | { |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 175 | register_target(&reject_target); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 176 | } |