iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libip6t_ipv6header.c
1 /* ipv6header match - matches IPv6 packets based
2 on whether they contain certain headers */
3
4 /* Original idea: Brad Chapman 
5  * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
6
7 #include <getopt.h>
8 #include <ip6tables.h>
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <netdb.h>
14 #include <sys/types.h>
15
16 #include <linux/netfilter_ipv6/ip6_tables.h>
17 #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
18
19 /* This maybe required 
20 #include <linux/in.h>
21 #include <linux/in6.h>
22 */
23
24
25 /* A few hardcoded protocols for 'all' and in case the user has no
26  *    /etc/protocols */
27 struct pprot {
28         char *name;
29         u_int8_t num;
30 };
31
32 struct numflag {
33         u_int8_t proto;
34         u_int8_t flag;
35 };
36
37 static const struct pprot chain_protos[] = {
38         { "hop-by-hop", IPPROTO_HOPOPTS },
39         { "protocol", IPPROTO_RAW },
40         { "hop", IPPROTO_HOPOPTS },
41         { "dst", IPPROTO_DSTOPTS },
42         { "route", IPPROTO_ROUTING },
43         { "frag", IPPROTO_FRAGMENT },
44         { "auth", IPPROTO_AH },
45         { "esp", IPPROTO_ESP },
46         { "none", IPPROTO_NONE },
47         { "prot", IPPROTO_RAW },
48         { "0", IPPROTO_HOPOPTS },
49         { "60", IPPROTO_DSTOPTS },
50         { "43", IPPROTO_ROUTING },
51         { "44", IPPROTO_FRAGMENT },
52         { "51", IPPROTO_AH },
53         { "50", IPPROTO_ESP },
54         { "59", IPPROTO_NONE },
55         { "255", IPPROTO_RAW },
56         /* { "all", 0 }, */
57 };
58
59 static const struct numflag chain_flags[] = {
60         { IPPROTO_HOPOPTS, MASK_HOPOPTS },
61         { IPPROTO_DSTOPTS, MASK_DSTOPTS },
62         { IPPROTO_ROUTING, MASK_ROUTING },
63         { IPPROTO_FRAGMENT, MASK_FRAGMENT },
64         { IPPROTO_AH, MASK_AH },
65         { IPPROTO_ESP, MASK_ESP },
66         { IPPROTO_NONE, MASK_NONE },
67         { IPPROTO_RAW, MASK_PROTO },
68 };
69
70 static char *
71 proto_to_name(u_int8_t proto, int nolookup)
72 {
73         unsigned int i;
74
75         if (proto && !nolookup) {
76                 struct protoent *pent = getprotobynumber(proto);
77                 if (pent)
78                         return pent->p_name;
79         }
80
81         for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
82                 if (chain_protos[i].num == proto)
83                         return chain_protos[i].name;
84
85         return NULL;
86 }
87
88 static u_int16_t
89 name_to_proto(const char *s)
90 {
91         unsigned int proto=0;
92         struct protoent *pent;
93
94         if ((pent = getprotobyname(s)))
95                 proto = pent->p_proto;
96         else {
97                 unsigned int i;
98                 for (i = 0;
99                         i < sizeof(chain_protos)/sizeof(struct pprot);
100                         i++) {
101                         if (strcmp(s, chain_protos[i].name) == 0) {
102                                 proto = chain_protos[i].num;
103                                 break;
104                         }
105                 }
106
107                 if (i == sizeof(chain_protos)/sizeof(struct pprot))
108                         exit_error(PARAMETER_PROBLEM,
109                                 "unknown header `%s' specified",
110                                 s);
111         }
112
113         return (u_int16_t)proto;
114 }
115
116 static unsigned int 
117 add_proto_to_mask(int proto){
118         unsigned int i=0, flag=0;
119
120         for (i = 0;
121                 i < sizeof(chain_flags)/sizeof(struct numflag);
122                 i++) {
123                         if (proto == chain_flags[i].proto){
124                                 flag = chain_flags[i].flag;
125                                 break;
126                         }
127         }
128
129         if (i == sizeof(chain_flags)/sizeof(struct numflag))
130                 exit_error(PARAMETER_PROBLEM,
131                 "unknown header `%d' specified",
132                 proto);
133         
134         return flag;
135 }       
136
137 static void
138 help(void)
139 {
140         printf(
141 "ipv6header v%s match options:\n"
142 "--header [!] headers     Type of header to match, by name\n"
143 "                         names: hop,dst,route,frag,auth,esp,none,proto\n"
144 "                    long names: hop-by-hop,ipv6-opts,ipv6-route,\n"
145 "                                ipv6-frag,ah,esp,ipv6-nonxt,protocol\n"
146 "                       numbers: 0,60,43,44,51,50,59\n"
147 "--soft                    The header CONTAINS the specified extensions\n",
148         IPTABLES_VERSION);
149 }
150
151 static struct option opts[] = {
152         { "header", 1, 0, '1' },
153         { "soft", 0, 0, '2' },
154         { 0 }
155 };
156
157 static void
158 init(struct ip6t_entry_match *m, unsigned int *nfcache)
159 {
160         struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)m->data;
161
162         info->matchflags = 0x00;
163         info->invflags = 0x00;
164         info->modeflag = 0x00;
165         /* No caching (yet) */
166         *nfcache |= NFC_UNKNOWN;
167 }
168
169 static unsigned int
170 parse_header(const char *flags) {
171         unsigned int ret = 0;
172         char *ptr;
173         char *buffer;
174
175         buffer = strdup(flags);
176
177         for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) 
178                 ret |= add_proto_to_mask(name_to_proto(ptr));
179                 
180         free(buffer);
181         return ret;
182 }
183
184 #define IPV6_HDR_HEADER 0x01
185 #define IPV6_HDR_SOFT   0x02
186
187 /* Parses command options; returns 0 if it ate an option */
188 static int
189 parse(int c, char **argv, int invert, unsigned int *flags,
190       const struct ip6t_entry *entry,
191       unsigned int *nfcache,
192       struct ip6t_entry_match **match)
193 {
194         struct ip6t_ipv6header_info *info = (struct ip6t_ipv6header_info *)(*match)->data;
195
196         switch (c) {
197                 case '1' : 
198                         /* Parse the provided header names */
199                         if (*flags & IPV6_HDR_HEADER)
200                                 exit_error(PARAMETER_PROBLEM,
201                                         "Only one `--header' allowed");
202
203                         check_inverse(optarg, &invert, &optind, 0);
204
205                         if (! (info->matchflags = parse_header(argv[optind-1])) )
206                                 exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names");
207
208                         if (invert) 
209                                 info->invflags |= 0xFF;
210                         *flags |= IPV6_HDR_HEADER;
211                         break;
212                 case '2' : 
213                         /* Soft-mode requested? */
214                         if (*flags & IPV6_HDR_SOFT)
215                                 exit_error(PARAMETER_PROBLEM,
216                                         "Only one `--soft' allowed");
217
218                         info->modeflag |= 0xFF;
219                         *flags |= IPV6_HDR_SOFT;
220                         break;
221                 default:
222                         return 0;
223         }
224
225         return 1;
226 }
227
228 /* Checks the flags variable */
229 static void
230 final_check(unsigned int flags)
231 {
232         if (!flags) exit_error(PARAMETER_PROBLEM, "ip6t_ipv6header: no options specified");
233 }
234
235 static void
236 print_header(u_int8_t flags){
237         int have_flag = 0;
238
239         while (flags) {
240                 unsigned int i;
241
242                 for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
243
244                 if (have_flag)
245                         printf(",");
246
247                 printf("%s", proto_to_name(chain_flags[i].proto,0));
248                 have_flag = 1;
249
250                 flags &= ~chain_flags[i].flag;
251         }
252
253         if (!have_flag)
254                 printf("NONE");
255 }
256
257 /* Prints out the match */
258 static void
259 print(const struct ip6t_ip6 *ip,
260       const struct ip6t_entry_match *match,
261       int numeric)
262 {
263         const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
264         printf("ipv6header ");
265
266         if (info->matchflags || info->invflags) {
267                 printf("flags:%s", info->invflags ? "!" : "");
268                 if (numeric)
269                         printf("0x%02X ", info->matchflags);
270                 else {
271                         print_header(info->matchflags);
272                         printf(" ");
273                 }
274         }
275
276         if (info->modeflag)
277                 printf("soft ");
278
279         return;
280 }
281
282 /* Saves the match */
283 static void
284 save(const struct ip6t_ip6 *ip,
285      const struct ip6t_entry_match *match)
286 {
287
288         const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
289
290         printf("--header ");
291         printf("%s", info->invflags ? "!" : "");
292         print_header(info->matchflags);
293         printf(" ");
294         if (info->modeflag)
295                 printf("--soft ");
296
297         return;
298 }
299
300 static
301 struct ip6tables_match ipv6header
302 = { NULL,
303     "ipv6header",
304     IPTABLES_VERSION,
305     IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)),
306     IP6T_ALIGN(sizeof(struct ip6t_ipv6header_info)),
307     &help,
308     &init,
309     &parse,
310     &final_check,
311     &print,
312     &save,
313     opts
314 };
315
316 void _init(void)
317 {
318         register_match6(&ipv6header);
319 }