Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables to add customized REJECT support. |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 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> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 12 | #include <xtables.h> |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 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 |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 41 | print_reject_types(void) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 54 | static void REJECT_help(void) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 55 | { |
| 56 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 57 | "REJECT target options:\n" |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 58 | "--reject-with type drop input packet and send back\n" |
| 59 | " a reply packet according to type:\n"); |
| 60 | |
| 61 | print_reject_types(); |
| 62 | } |
| 63 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 64 | static const struct option REJECT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 65 | { "reject-with", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 66 | { .name = NULL } |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 69 | static void REJECT_init(struct xt_entry_target *t) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 70 | { |
| 71 | struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data; |
| 72 | |
| 73 | /* default */ |
| 74 | reject->with = IP6T_ICMP6_PORT_UNREACH; |
| 75 | |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 78 | static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 79 | const void *entry, struct xt_entry_target **target) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 80 | { |
| 81 | struct ip6t_reject_info *reject = |
| 82 | (struct ip6t_reject_info *)(*target)->data; |
| 83 | unsigned int limit = sizeof(reject_table)/sizeof(struct reject_names); |
| 84 | unsigned int i; |
| 85 | |
| 86 | switch(c) { |
| 87 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 88 | if (check_inverse(optarg, &invert, NULL, 0)) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 89 | exit_error(PARAMETER_PROBLEM, |
| 90 | "Unexpected `!' after --reject-with"); |
| 91 | for (i = 0; i < limit; i++) { |
| 92 | if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0) |
| 93 | || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) { |
| 94 | reject->with = reject_table[i].with; |
| 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | exit_error(PARAMETER_PROBLEM, "unknown reject type `%s'",optarg); |
| 99 | default: |
| 100 | /* Fall through */ |
Fabrice MARIE | ae31bb6 | 2002-06-14 07:38:16 +0000 | [diff] [blame] | 101 | break; |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 106 | static void REJECT_print(const void *ip, const struct xt_entry_target *target, |
| 107 | int numeric) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 108 | { |
| 109 | const struct ip6t_reject_info *reject |
| 110 | = (const struct ip6t_reject_info *)target->data; |
| 111 | unsigned int i; |
| 112 | |
| 113 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) { |
| 114 | if (reject_table[i].with == reject->with) |
| 115 | break; |
| 116 | } |
| 117 | printf("reject-with %s ", reject_table[i].name); |
| 118 | } |
| 119 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 120 | static void REJECT_save(const void *ip, const struct xt_entry_target *target) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 121 | { |
| 122 | const struct ip6t_reject_info *reject |
| 123 | = (const struct ip6t_reject_info *)target->data; |
| 124 | unsigned int i; |
| 125 | |
| 126 | for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) |
| 127 | if (reject_table[i].with == reject->with) |
| 128 | break; |
| 129 | |
| 130 | printf("--reject-with %s ", reject_table[i].name); |
| 131 | } |
| 132 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 133 | static struct xtables_target reject_tg6_reg = { |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 134 | .name = "REJECT", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 135 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 136 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 137 | .size = XT_ALIGN(sizeof(struct ip6t_reject_info)), |
| 138 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)), |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 139 | .help = REJECT_help, |
| 140 | .init = REJECT_init, |
| 141 | .parse = REJECT_parse, |
| 142 | .print = REJECT_print, |
| 143 | .save = REJECT_save, |
| 144 | .extra_opts = REJECT_opts, |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | void _init(void) |
| 148 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 149 | xtables_register_target(&reject_tg6_reg); |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 150 | } |