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/ip6t_REJECT.h> |
| 14 | |
| 15 | struct reject_names { |
| 16 | const char *name; |
| 17 | const char *alias; |
| 18 | enum ip6t_reject_with with; |
| 19 | const char *desc; |
| 20 | }; |
| 21 | |
| 22 | static const struct reject_names reject_table[] = { |
| 23 | {"icmp6-no-route", "no-route", |
| 24 | IP6T_ICMP6_NO_ROUTE, "ICMPv6 no route"}, |
| 25 | {"icmp6-adm-prohibited", "adm-prohibited", |
| 26 | IP6T_ICMP6_ADM_PROHIBITED, "ICMPv6 administratively prohibited"}, |
| 27 | #if 0 |
| 28 | {"icmp6-not-neighbor", "not-neighbor"}, |
| 29 | IP6T_ICMP6_NOT_NEIGHBOR, "ICMPv6 not a neighbor"}, |
| 30 | #endif |
| 31 | {"icmp6-addr-unreachable", "addr-unreach", |
| 32 | IP6T_ICMP6_ADDR_UNREACH, "ICMPv6 address unreachable"}, |
| 33 | {"icmp6-port-unreachable", "port-unreach", |
| 34 | IP6T_ICMP6_PORT_UNREACH, "ICMPv6 port unreachable"}, |
| 35 | {"tcp-reset", "tcp-reset", |
| 36 | IP6T_TCP_RESET, "TCP RST packet"} |
| 37 | }; |
| 38 | |
| 39 | static void |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 40 | print_reject_types(void) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 41 | { |
| 42 | unsigned int i; |
| 43 | |
| 44 | printf("Valid reject types:\n"); |
| 45 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame^] | 46 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) { |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 47 | printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc); |
| 48 | printf(" %-25s\talias\n", reject_table[i].alias); |
| 49 | } |
| 50 | printf("\n"); |
| 51 | } |
| 52 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 53 | static void REJECT_help(void) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 54 | { |
| 55 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 56 | "REJECT target options:\n" |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 57 | "--reject-with type drop input packet and send back\n" |
| 58 | " a reply packet according to type:\n"); |
| 59 | |
| 60 | print_reject_types(); |
| 61 | } |
| 62 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 63 | static const struct option REJECT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 64 | { "reject-with", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 65 | { .name = NULL } |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 68 | static void REJECT_init(struct xt_entry_target *t) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 69 | { |
| 70 | struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data; |
| 71 | |
| 72 | /* default */ |
| 73 | reject->with = IP6T_ICMP6_PORT_UNREACH; |
| 74 | |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 77 | static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 78 | const void *entry, struct xt_entry_target **target) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 79 | { |
| 80 | struct ip6t_reject_info *reject = |
| 81 | (struct ip6t_reject_info *)(*target)->data; |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 82 | unsigned int i; |
| 83 | |
| 84 | switch(c) { |
| 85 | case '1': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 86 | if (xtables_check_inverse(optarg, &invert, NULL, 0)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 87 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 88 | "Unexpected `!' after --reject-with"); |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame^] | 89 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 90 | if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0) |
| 91 | || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) { |
| 92 | reject->with = reject_table[i].with; |
| 93 | return 1; |
| 94 | } |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 95 | xtables_error(PARAMETER_PROBLEM, "unknown reject type \"%s\"", optarg); |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 96 | default: |
| 97 | /* Fall through */ |
Fabrice MARIE | ae31bb6 | 2002-06-14 07:38:16 +0000 | [diff] [blame] | 98 | break; |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 103 | static void REJECT_print(const void *ip, const struct xt_entry_target *target, |
| 104 | int numeric) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 105 | { |
| 106 | const struct ip6t_reject_info *reject |
| 107 | = (const struct ip6t_reject_info *)target->data; |
| 108 | unsigned int i; |
| 109 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame^] | 110 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 111 | if (reject_table[i].with == reject->with) |
| 112 | break; |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 113 | printf("reject-with %s ", reject_table[i].name); |
| 114 | } |
| 115 | |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 116 | 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] | 117 | { |
| 118 | const struct ip6t_reject_info *reject |
| 119 | = (const struct ip6t_reject_info *)target->data; |
| 120 | unsigned int i; |
| 121 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame^] | 122 | for (i = 0; i < ARRAY_SIZE(reject_table); ++i) |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 123 | if (reject_table[i].with == reject->with) |
| 124 | break; |
| 125 | |
| 126 | printf("--reject-with %s ", reject_table[i].name); |
| 127 | } |
| 128 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 129 | static struct xtables_target reject_tg6_reg = { |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 130 | .name = "REJECT", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 131 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 132 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 133 | .size = XT_ALIGN(sizeof(struct ip6t_reject_info)), |
| 134 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)), |
Jan Engelhardt | 4d150eb | 2007-10-04 16:29:39 +0000 | [diff] [blame] | 135 | .help = REJECT_help, |
| 136 | .init = REJECT_init, |
| 137 | .parse = REJECT_parse, |
| 138 | .print = REJECT_print, |
| 139 | .save = REJECT_save, |
| 140 | .extra_opts = REJECT_opts, |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | void _init(void) |
| 144 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 145 | xtables_register_target(&reject_tg6_reg); |
Harald Welte | c8af1fd | 2001-07-23 02:15:09 +0000 | [diff] [blame] | 146 | } |