iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libip6t_REJECT.c
1 /* Shared library add-on to iptables to add customized REJECT support.
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>
12 #include <ip6tables.h>
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
41 print_reject_types()
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
54 /* Saves the union ipt_targinfo in parsable form to stdout. */
55
56 /* Function which prints out usage message. */
57 static void
58 help(void)
59 {
60         printf(
61 "REJECT options:\n"
62 "--reject-with type              drop input packet and send back\n"
63 "                                a reply packet according to type:\n");
64
65         print_reject_types();
66 }
67
68 static struct option opts[] = {
69         { "reject-with", 1, 0, '1' },
70         { 0 }
71 };
72
73 /* Allocate and initialize the target. */
74 static void
75 init(struct ip6t_entry_target *t, unsigned int *nfcache)
76 {
77         struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data;
78
79         /* default */
80         reject->with = IP6T_ICMP6_PORT_UNREACH;
81
82         /* Can't cache this */
83         *nfcache |= NFC_UNKNOWN;
84 }
85
86 /* Function which parses command options; returns true if it
87    ate an option */
88 static int
89 parse(int c, char **argv, int invert, unsigned int *flags,
90       const struct ip6t_entry *entry,
91       struct ip6t_entry_target **target)
92 {
93         struct ip6t_reject_info *reject = 
94                 (struct ip6t_reject_info *)(*target)->data;
95         unsigned int limit = sizeof(reject_table)/sizeof(struct reject_names);
96         unsigned int i;
97
98         switch(c) {
99         case '1':
100                 if (check_inverse(optarg, &invert, NULL, 0))
101                         exit_error(PARAMETER_PROBLEM,
102                                    "Unexpected `!' after --reject-with");
103                 for (i = 0; i < limit; i++) {
104                         if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0)
105                             || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) {
106                                 reject->with = reject_table[i].with;
107                                 return 1;
108                         }
109                 }
110                 exit_error(PARAMETER_PROBLEM, "unknown reject type `%s'",optarg);
111         default:
112                 /* Fall through */
113                 break;
114         }
115         return 0;
116 }
117
118 /* Final check; nothing. */
119 static void final_check(unsigned int flags)
120 {
121 }
122
123 /* Prints out ipt_reject_info. */
124 static void
125 print(const struct ip6t_ip6 *ip,
126       const struct ip6t_entry_target *target,
127       int numeric)
128 {
129         const struct ip6t_reject_info *reject
130                 = (const struct ip6t_reject_info *)target->data;
131         unsigned int i;
132
133         for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++) {
134                 if (reject_table[i].with == reject->with)
135                         break;
136         }
137         printf("reject-with %s ", reject_table[i].name);
138 }
139
140 /* Saves ipt_reject in parsable form to stdout. */
141 static void save(const struct ip6t_ip6 *ip, 
142                  const struct ip6t_entry_target *target)
143 {
144         const struct ip6t_reject_info *reject
145                 = (const struct ip6t_reject_info *)target->data;
146         unsigned int i;
147
148         for (i = 0; i < sizeof(reject_table)/sizeof(struct reject_names); i++)
149                 if (reject_table[i].with == reject->with)
150                         break;
151
152         printf("--reject-with %s ", reject_table[i].name);
153 }
154
155 struct ip6tables_target reject
156 = { NULL,
157     "REJECT",
158     IPTABLES_VERSION,
159     IP6T_ALIGN(sizeof(struct ip6t_reject_info)),
160     IP6T_ALIGN(sizeof(struct ip6t_reject_info)),
161     &help,
162     &init,
163     &parse,
164     &final_check,
165     &print,
166     &save,
167     opts
168 };
169
170 void _init(void)
171 {
172         register_target6(&reject);
173 }