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