blob: 8085321a6d654e2b331db2b90ef060348d8d7717 [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add customized REJECT support.
Harald Weltec8af1fd2001-07-23 02:15:09 +00002 *
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 Engelhardt5d9678a2008-11-20 10:15:35 +010010#include <xtables.h>
Harald Weltec8af1fd2001-07-23 02:15:09 +000011#include <linux/netfilter_ipv6/ip6t_REJECT.h>
12
13struct reject_names {
14 const char *name;
15 const char *alias;
16 enum ip6t_reject_with with;
17 const char *desc;
18};
19
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010020enum {
21 O_REJECT_WITH = 0,
22};
23
Harald Weltec8af1fd2001-07-23 02:15:09 +000024static 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
41static void
Patrick McHardy500f4832007-09-08 15:59:04 +000042print_reject_types(void)
Harald Weltec8af1fd2001-07-23 02:15:09 +000043{
44 unsigned int i;
45
46 printf("Valid reject types:\n");
47
Jan Engelhardt2c69b552009-04-30 19:32:02 +020048 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
Harald Weltec8af1fd2001-07-23 02:15:09 +000049 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 Engelhardt4d150eb2007-10-04 16:29:39 +000055static void REJECT_help(void)
Harald Weltec8af1fd2001-07-23 02:15:09 +000056{
57 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020058"REJECT target options:\n"
Harald Weltec8af1fd2001-07-23 02:15:09 +000059"--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 Engelhardtb313d8f2011-02-16 01:16:39 +010065static const struct xt_option_entry REJECT_opts[] = {
66 {.name = "reject-with", .id = O_REJECT_WITH, .type = XTTYPE_STRING},
67 XTOPT_TABLEEND,
Harald Weltec8af1fd2001-07-23 02:15:09 +000068};
69
Jan Engelhardt4d150eb2007-10-04 16:29:39 +000070static void REJECT_init(struct xt_entry_target *t)
Harald Weltec8af1fd2001-07-23 02:15:09 +000071{
72 struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data;
73
74 /* default */
75 reject->with = IP6T_ICMP6_PORT_UNREACH;
76
Harald Weltec8af1fd2001-07-23 02:15:09 +000077}
78
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010079static void REJECT_parse(struct xt_option_call *cb)
Harald Weltec8af1fd2001-07-23 02:15:09 +000080{
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010081 struct ip6t_reject_info *reject = cb->data;
Harald Weltec8af1fd2001-07-23 02:15:09 +000082 unsigned int i;
83
Jan Engelhardtb313d8f2011-02-16 01:16:39 +010084 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 Weltec8af1fd2001-07-23 02:15:09 +000095}
96
Jan Engelhardt4d150eb2007-10-04 16:29:39 +000097static void REJECT_print(const void *ip, const struct xt_entry_target *target,
98 int numeric)
Harald Weltec8af1fd2001-07-23 02:15:09 +000099{
100 const struct ip6t_reject_info *reject
101 = (const struct ip6t_reject_info *)target->data;
102 unsigned int i;
103
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200104 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000105 if (reject_table[i].with == reject->with)
106 break;
Jan Engelhardt73866352010-12-18 02:04:59 +0100107 printf(" reject-with %s", reject_table[i].name);
Harald Weltec8af1fd2001-07-23 02:15:09 +0000108}
109
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000110static void REJECT_save(const void *ip, const struct xt_entry_target *target)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000111{
112 const struct ip6t_reject_info *reject
113 = (const struct ip6t_reject_info *)target->data;
114 unsigned int i;
115
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200116 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000117 if (reject_table[i].with == reject->with)
118 break;
119
Jan Engelhardt73866352010-12-18 02:04:59 +0100120 printf(" --reject-with %s", reject_table[i].name);
Harald Weltec8af1fd2001-07-23 02:15:09 +0000121}
122
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200123static struct xtables_target reject_tg6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000124 .name = "REJECT",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200125 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100126 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200127 .size = XT_ALIGN(sizeof(struct ip6t_reject_info)),
128 .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)),
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000129 .help = REJECT_help,
130 .init = REJECT_init,
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000131 .print = REJECT_print,
132 .save = REJECT_save,
Jan Engelhardtb313d8f2011-02-16 01:16:39 +0100133 .x6_parse = REJECT_parse,
134 .x6_options = REJECT_opts,
Harald Weltec8af1fd2001-07-23 02:15:09 +0000135};
136
137void _init(void)
138{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200139 xtables_register_target(&reject_tg6_reg);
Harald Weltec8af1fd2001-07-23 02:15:09 +0000140}