| Harald Welte | c8af1fd | 2001-07-23 02:15:09 +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 | * ported to IPv6 by Harald Welte <laforge@gnumonks.org> |
| 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <getopt.h> |
| 12 | #include <ip6tables.h> |
| 13 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 14 | #include <linux/netfilter_ipv6/ip6t_REJECT.h> |
| 15 | |
| 16 | struct reject_names { |
| 17 | const char *name; |
| 18 | const char *alias; |
| 19 | enum ip6t_reject_with with; |
| 20 | const char *desc; |
| 21 | }; |
| 22 | |
| 23 | static const struct reject_names reject_table[] = { |
| 24 | {"icmp6-no-route", "no-route", |
| 25 | IP6T_ICMP6_NO_ROUTE, "ICMPv6 no route"}, |
| 26 | {"icmp6-adm-prohibited", "adm-prohibited", |
| 27 | IP6T_ICMP6_ADM_PROHIBITED, "ICMPv6 administratively prohibited"}, |
| 28 | #if 0 |
| 29 | {"icmp6-not-neighbor", "not-neighbor"}, |
| 30 | IP6T_ICMP6_NOT_NEIGHBOR, "ICMPv6 not a neighbor"}, |
| 31 | #endif |
| 32 | {"icmp6-addr-unreachable", "addr-unreach", |
| 33 | IP6T_ICMP6_ADDR_UNREACH, "ICMPv6 address unreachable"}, |
| 34 | {"icmp6-port-unreachable", "port-unreach", |
| 35 | IP6T_ICMP6_PORT_UNREACH, "ICMPv6 port unreachable"}, |
| 36 | {"tcp-reset", "tcp-reset", |
| 37 | IP6T_TCP_RESET, "TCP RST packet"} |
| 38 | }; |
| 39 | |
| 40 | static void |
| 41 | print_reject_types() |
| 42 | { |
| 43 | unsigned int i; |
| 44 | |
| 45 | printf("Valid reject types:\n"); |
| 46 | |
| 47 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 48 | printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc); |
| 49 | printf(" %-25s\talias\n", reject_table[i].alias); |
| 50 | } |
| 51 | printf("\n"); |
| 52 | } |
| 53 | |
| 54 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 55 | |
| 56 | /* Function which prints out usage message. */ |
| 57 | static void |
| 58 | help(void) |
| 59 | { |
| 60 | printf( |
| 61 | "REJECT options:\n" |
| 62 | "--reject-with type drop input packet and send back\n" |
| 63 | " a reply packet according to type:\n"); |
| 64 | |
| 65 | print_reject_types(); |
| 66 | } |
| 67 | |
| 68 | static struct option opts[] = { |
| 69 | { "reject-with", 1, 0, '1' }, |
| 70 | { 0 } |
| 71 | }; |
| 72 | |
| 73 | /* Allocate and initialize the target. */ |
| 74 | static void |
| 75 | init(struct ip6t_entry_target *t, unsigned int *nfcache) |
| 76 | { |
| 77 | struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data; |
| 78 | |
| 79 | /* default */ |
| 80 | reject->with = IP6T_ICMP6_PORT_UNREACH; |
| 81 | |
| 82 | /* Can't cache this */ |
| 83 | *nfcache |= NFC_UNKNOWN; |
| 84 | } |
| 85 | |
| 86 | /* Function which parses command options; returns true if it |
| 87 | ate an option */ |
| 88 | static int |
| 89 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 90 | const struct ip6t_entry *entry, |
| 91 | struct ip6t_entry_target **target) |
| 92 | { |
| 93 | struct ip6t_reject_info *reject = |
| 94 | (struct ip6t_reject_info *)(*target)->data; |
| 95 | unsigned int limit = sizeof(reject_table)/sizeof(struct reject_names); |
| 96 | unsigned int i; |
| 97 | |
| 98 | switch(c) { |
| 99 | case '1': |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame^] | 100 | if (check_inverse(optarg, &invert, NULL, 0)) |
| Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 101 | exit_error(PARAMETER_PROBLEM, |
| 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 | } |
| 110 | exit_error(PARAMETER_PROBLEM, "unknown reject type `%s'",optarg); |
| 111 | default: |
| 112 | /* Fall through */ |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* Final check; nothing. */ |
| 118 | static void final_check(unsigned int flags) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /* Prints out ipt_reject_info. */ |
| 123 | static void |
| 124 | print(const struct ip6t_ip6 *ip, |
| 125 | const struct ip6t_entry_target *target, |
| 126 | int numeric) |
| 127 | { |
| 128 | const struct ip6t_reject_info *reject |
| 129 | = (const struct ip6t_reject_info *)target->data; |
| 130 | unsigned int i; |
| 131 | |
| 132 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 133 | if (reject_table[i].with == reject->with) |
| 134 | break; |
| 135 | } |
| 136 | printf("reject-with %s ", reject_table[i].name); |
| 137 | } |
| 138 | |
| 139 | /* Saves ipt_reject in parsable form to stdout. */ |
| 140 | static void save(const struct ip6t_ip6 *ip, |
| 141 | const struct ip6t_entry_target *target) |
| 142 | { |
| 143 | const struct ip6t_reject_info *reject |
| 144 | = (const struct ip6t_reject_info *)target->data; |
| 145 | unsigned int i; |
| 146 | |
| 147 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) |
| 148 | if (reject_table[i].with == reject->with) |
| 149 | break; |
| 150 | |
| 151 | printf("--reject-with %s ", reject_table[i].name); |
| 152 | } |
| 153 | |
| 154 | struct ip6tables_target reject |
| 155 | = { NULL, |
| 156 | "REJECT", |
| 157 | NETFILTER_VERSION, |
| 158 | IP6T_ALIGN(sizeof(struct ip6t_reject_info)), |
| 159 | IP6T_ALIGN(sizeof(struct ip6t_reject_info)), |
| 160 | &help, |
| 161 | &init, |
| 162 | &parse, |
| 163 | &final_check, |
| 164 | &print, |
| 165 | &save, |
| 166 | opts |
| 167 | }; |
| 168 | |
| 169 | void _init(void) |
| 170 | { |
| 171 | register_target6(&reject); |
| 172 | } |