blob: 9ad3b685e2d46370c406693466c5827ba2aeda04 [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>
10#include <stdlib.h>
11#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010012#include <xtables.h>
Harald Weltec8af1fd2001-07-23 02:15:09 +000013#include <linux/netfilter_ipv6/ip6t_REJECT.h>
14
15struct reject_names {
16 const char *name;
17 const char *alias;
18 enum ip6t_reject_with with;
19 const char *desc;
20};
21
22static 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
39static void
Patrick McHardy500f4832007-09-08 15:59:04 +000040print_reject_types(void)
Harald Weltec8af1fd2001-07-23 02:15:09 +000041{
42 unsigned int i;
43
44 printf("Valid reject types:\n");
45
Jan Engelhardt2c69b552009-04-30 19:32:02 +020046 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) {
Harald Weltec8af1fd2001-07-23 02:15:09 +000047 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 Engelhardt4d150eb2007-10-04 16:29:39 +000053static void REJECT_help(void)
Harald Weltec8af1fd2001-07-23 02:15:09 +000054{
55 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020056"REJECT target options:\n"
Harald Weltec8af1fd2001-07-23 02:15:09 +000057"--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 Engelhardt4d150eb2007-10-04 16:29:39 +000063static const struct option REJECT_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000064 { "reject-with", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000065 { .name = NULL }
Harald Weltec8af1fd2001-07-23 02:15:09 +000066};
67
Jan Engelhardt4d150eb2007-10-04 16:29:39 +000068static void REJECT_init(struct xt_entry_target *t)
Harald Weltec8af1fd2001-07-23 02:15:09 +000069{
70 struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data;
71
72 /* default */
73 reject->with = IP6T_ICMP6_PORT_UNREACH;
74
Harald Weltec8af1fd2001-07-23 02:15:09 +000075}
76
Jan Engelhardt4d150eb2007-10-04 16:29:39 +000077static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags,
78 const void *entry, struct xt_entry_target **target)
Harald Weltec8af1fd2001-07-23 02:15:09 +000079{
80 struct ip6t_reject_info *reject =
81 (struct ip6t_reject_info *)(*target)->data;
Harald Weltec8af1fd2001-07-23 02:15:09 +000082 unsigned int i;
83
84 switch(c) {
85 case '1':
Jan Engelhardt0f16c722009-01-30 04:55:38 +010086 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010087 xtables_error(PARAMETER_PROBLEM,
Harald Weltec8af1fd2001-07-23 02:15:09 +000088 "Unexpected `!' after --reject-with");
Jan Engelhardt2c69b552009-04-30 19:32:02 +020089 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Weltec8af1fd2001-07-23 02:15:09 +000090 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 Engelhardt1829ed42009-02-21 03:29:44 +010095 xtables_error(PARAMETER_PROBLEM, "unknown reject type \"%s\"", optarg);
Harald Weltec8af1fd2001-07-23 02:15:09 +000096 default:
97 /* Fall through */
Fabrice MARIEae31bb62002-06-14 07:38:16 +000098 break;
Harald Weltec8af1fd2001-07-23 02:15:09 +000099 }
100 return 0;
101}
102
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000103static void REJECT_print(const void *ip, const struct xt_entry_target *target,
104 int numeric)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000105{
106 const struct ip6t_reject_info *reject
107 = (const struct ip6t_reject_info *)target->data;
108 unsigned int i;
109
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200110 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000111 if (reject_table[i].with == reject->with)
112 break;
Harald Weltec8af1fd2001-07-23 02:15:09 +0000113 printf("reject-with %s ", reject_table[i].name);
114}
115
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000116static void REJECT_save(const void *ip, const struct xt_entry_target *target)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000117{
118 const struct ip6t_reject_info *reject
119 = (const struct ip6t_reject_info *)target->data;
120 unsigned int i;
121
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200122 for (i = 0; i < ARRAY_SIZE(reject_table); ++i)
Harald Weltec8af1fd2001-07-23 02:15:09 +0000123 if (reject_table[i].with == reject->with)
124 break;
125
126 printf("--reject-with %s ", reject_table[i].name);
127}
128
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200129static struct xtables_target reject_tg6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000130 .name = "REJECT",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200131 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100132 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200133 .size = XT_ALIGN(sizeof(struct ip6t_reject_info)),
134 .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)),
Jan Engelhardt4d150eb2007-10-04 16:29:39 +0000135 .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 Weltec8af1fd2001-07-23 02:15:09 +0000141};
142
143void _init(void)
144{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200145 xtables_register_target(&reject_tg6_reg);
Harald Weltec8af1fd2001-07-23 02:15:09 +0000146}