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