blob: 357cbea181f35413f1ba3d63a6dd9320a762c5e7 [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add policy support. */
Patrick McHardy524bb802005-11-19 09:00:03 +00002#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <syslog.h>
7#include <getopt.h>
8#include <netdb.h>
9#include <errno.h>
10#include <sys/socket.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010013#include <xtables.h>
Patrick McHardy524bb802005-11-19 09:00:03 +000014#include <ip6tables.h>
Patrick McHardy524bb802005-11-19 09:00:03 +000015#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardta2a7f2b2008-09-01 14:20:13 +020016#include <linux/netfilter_ipv6/ip6t_policy.h>
Patrick McHardy524bb802005-11-19 09:00:03 +000017
18/*
19 * HACK: global pointer to current matchinfo for making
20 * final checks and adjustments in final_check.
21 */
22static struct ip6t_policy_info *policy_info;
23
Jan Engelhardt997045f2007-10-04 16:29:21 +000024static void policy_help(void)
Patrick McHardy524bb802005-11-19 09:00:03 +000025{
26 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020027"policy match options:\n"
Patrick McHardy524bb802005-11-19 09:00:03 +000028" --dir in|out match policy applied during decapsulation/\n"
29" policy to be applied during encapsulation\n"
30" --pol none|ipsec match policy\n"
31" --strict match entire policy instead of single element\n"
32" at any position\n"
33"[!] --reqid reqid match reqid\n"
34"[!] --spi spi match SPI\n"
35"[!] --proto proto match protocol (ah/esp/ipcomp)\n"
36"[!] --mode mode match mode (transport/tunnel)\n"
37"[!] --tunnel-src addr/masklen match tunnel source\n"
38"[!] --tunnel-dst addr/masklen match tunnel destination\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020039" --next begin next element in policy\n");
Patrick McHardy524bb802005-11-19 09:00:03 +000040}
41
Jan Engelhardt997045f2007-10-04 16:29:21 +000042static const struct option policy_opts[] =
Patrick McHardy524bb802005-11-19 09:00:03 +000043{
44 {
45 .name = "dir",
46 .has_arg = 1,
47 .val = '1',
48 },
49 {
50 .name = "pol",
51 .has_arg = 1,
52 .val = '2',
53 },
54 {
55 .name = "strict",
56 .val = '3'
57 },
58 {
59 .name = "reqid",
60 .has_arg = 1,
61 .val = '4',
62 },
63 {
64 .name = "spi",
65 .has_arg = 1,
66 .val = '5'
67 },
68 {
69 .name = "tunnel-src",
70 .has_arg = 1,
71 .val = '6'
72 },
73 {
74 .name = "tunnel-dst",
75 .has_arg = 1,
76 .val = '7'
77 },
78 {
79 .name = "proto",
80 .has_arg = 1,
81 .val = '8'
82 },
83 {
84 .name = "mode",
85 .has_arg = 1,
86 .val = '9'
87 },
88 {
89 .name = "next",
90 .val = 'a'
91 },
Max Kellermann9ee386a2008-01-29 13:48:05 +000092 { .name = NULL }
Patrick McHardy524bb802005-11-19 09:00:03 +000093};
94
95/* FIXME - Duplicated code from ip6tables.c */
96/* Duplicated to stop too many changes in other files .... */
97static void
98in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
99{
100 memcpy(dst, src, sizeof(struct in6_addr));
101 /* dst->s6_addr = src->s6_addr; */
102}
103
104static char *
105addr_to_numeric(const struct in6_addr *addrp)
106{
107 /* 0000:0000:0000:0000:0000:000.000.000.000
108 * 0000:0000:0000:0000:0000:0000:0000:0000 */
109 static char buf[50+1];
110 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
111}
112
113static char *
114mask_to_numeric(const struct in6_addr *addrp)
115{
116 static char buf[50+2];
117 int l = ipv6_prefix_length(addrp);
118 if (l == -1) {
119 strcpy(buf, "/");
120 strcat(buf, addr_to_numeric(addrp));
121 return buf;
122 }
123 sprintf(buf, "/%d", l);
124 return buf;
125}
126
Patrick McHardy524bb802005-11-19 09:00:03 +0000127static int parse_direction(char *s)
128{
129 if (strcmp(s, "in") == 0)
130 return IP6T_POLICY_MATCH_IN;
131 if (strcmp(s, "out") == 0)
132 return IP6T_POLICY_MATCH_OUT;
133 exit_error(PARAMETER_PROBLEM, "policy_match: invalid dir `%s'", s);
134}
135
136static int parse_policy(char *s)
137{
138 if (strcmp(s, "none") == 0)
139 return IP6T_POLICY_MATCH_NONE;
140 if (strcmp(s, "ipsec") == 0)
141 return 0;
142 exit_error(PARAMETER_PROBLEM, "policy match: invalid policy `%s'", s);
143}
144
145static int parse_mode(char *s)
146{
147 if (strcmp(s, "transport") == 0)
148 return IP6T_POLICY_MODE_TRANSPORT;
149 if (strcmp(s, "tunnel") == 0)
150 return IP6T_POLICY_MODE_TUNNEL;
151 exit_error(PARAMETER_PROBLEM, "policy match: invalid mode `%s'", s);
152}
153
Jan Engelhardt997045f2007-10-04 16:29:21 +0000154static int policy_parse(int c, char **argv, int invert, unsigned int *flags,
155 const void *entry, struct xt_entry_match **match)
Patrick McHardy524bb802005-11-19 09:00:03 +0000156{
157 struct ip6t_policy_info *info = (void *)(*match)->data;
158 struct ip6t_policy_elem *e = &info->pol[info->len];
159 struct in6_addr *addr = NULL, mask;
160 unsigned int naddr = 0;
161 int mode;
162
163 check_inverse(optarg, &invert, &optind, 0);
164
165 switch (c) {
166 case '1':
167 if (info->flags & (IP6T_POLICY_MATCH_IN|IP6T_POLICY_MATCH_OUT))
168 exit_error(PARAMETER_PROBLEM,
169 "policy match: double --dir option");
170 if (invert)
171 exit_error(PARAMETER_PROBLEM,
172 "policy match: can't invert --dir option");
173
174 info->flags |= parse_direction(argv[optind-1]);
175 break;
176 case '2':
177 if (invert)
178 exit_error(PARAMETER_PROBLEM,
179 "policy match: can't invert --policy option");
180
181 info->flags |= parse_policy(argv[optind-1]);
182 break;
183 case '3':
184 if (info->flags & IP6T_POLICY_MATCH_STRICT)
185 exit_error(PARAMETER_PROBLEM,
186 "policy match: double --strict option");
187
188 if (invert)
189 exit_error(PARAMETER_PROBLEM,
190 "policy match: can't invert --strict option");
191
192 info->flags |= IP6T_POLICY_MATCH_STRICT;
193 break;
194 case '4':
195 if (e->match.reqid)
196 exit_error(PARAMETER_PROBLEM,
197 "policy match: double --reqid option");
198
199 e->match.reqid = 1;
200 e->invert.reqid = invert;
201 e->reqid = strtol(argv[optind-1], NULL, 10);
202 break;
203 case '5':
204 if (e->match.spi)
205 exit_error(PARAMETER_PROBLEM,
206 "policy match: double --spi option");
Patrick McHardy1d0f57c2006-01-12 09:12:47 +0000207
Patrick McHardy524bb802005-11-19 09:00:03 +0000208 e->match.spi = 1;
209 e->invert.spi = invert;
210 e->spi = strtol(argv[optind-1], NULL, 0x10);
211 break;
212 case '6':
213 if (e->match.saddr)
214 exit_error(PARAMETER_PROBLEM,
215 "policy match: double --tunnel-src option");
216
Jan Engelhardtbd943842008-01-20 13:38:08 +0000217 ip6parse_hostnetworkmask(argv[optind-1], &addr, &mask, &naddr);
Patrick McHardy524bb802005-11-19 09:00:03 +0000218 if (naddr > 1)
219 exit_error(PARAMETER_PROBLEM,
220 "policy match: name resolves to multiple IPs");
221
222 e->match.saddr = 1;
223 e->invert.saddr = invert;
Patrick McHardy02e88f22006-01-31 18:24:14 +0000224 in6addrcpy(&e->saddr.a6, addr);
225 in6addrcpy(&e->smask.a6, &mask);
Patrick McHardy524bb802005-11-19 09:00:03 +0000226 break;
227 case '7':
228 if (e->match.daddr)
229 exit_error(PARAMETER_PROBLEM,
230 "policy match: double --tunnel-dst option");
231
Jan Engelhardtbd943842008-01-20 13:38:08 +0000232 ip6parse_hostnetworkmask(argv[optind-1], &addr, &mask, &naddr);
Patrick McHardy524bb802005-11-19 09:00:03 +0000233 if (naddr > 1)
234 exit_error(PARAMETER_PROBLEM,
235 "policy match: name resolves to multiple IPs");
236
237 e->match.daddr = 1;
238 e->invert.daddr = invert;
Patrick McHardy02e88f22006-01-31 18:24:14 +0000239 in6addrcpy(&e->daddr.a6, addr);
240 in6addrcpy(&e->dmask.a6, &mask);
Patrick McHardy524bb802005-11-19 09:00:03 +0000241 break;
242 case '8':
243 if (e->match.proto)
244 exit_error(PARAMETER_PROBLEM,
245 "policy match: double --proto option");
246
247 e->proto = parse_protocol(argv[optind-1]);
248 if (e->proto != IPPROTO_AH && e->proto != IPPROTO_ESP &&
249 e->proto != IPPROTO_COMP)
250 exit_error(PARAMETER_PROBLEM,
251 "policy match: protocol must ah/esp/ipcomp");
252 e->match.proto = 1;
253 e->invert.proto = invert;
254 break;
255 case '9':
256 if (e->match.mode)
257 exit_error(PARAMETER_PROBLEM,
258 "policy match: double --mode option");
Patrick McHardy1d0f57c2006-01-12 09:12:47 +0000259
Patrick McHardy524bb802005-11-19 09:00:03 +0000260 mode = parse_mode(argv[optind-1]);
261 e->match.mode = 1;
262 e->invert.mode = invert;
263 e->mode = mode;
264 break;
265 case 'a':
266 if (invert)
267 exit_error(PARAMETER_PROBLEM,
268 "policy match: can't invert --next option");
269
Patrick McHardy524bb802005-11-19 09:00:03 +0000270 if (++info->len == IP6T_POLICY_MAX_ELEM)
271 exit_error(PARAMETER_PROBLEM,
272 "policy match: maximum policy depth reached");
273 break;
274 default:
275 return 0;
276 }
277
278 policy_info = info;
279 return 1;
280}
281
Jan Engelhardt997045f2007-10-04 16:29:21 +0000282static void policy_check(unsigned int flags)
Patrick McHardy524bb802005-11-19 09:00:03 +0000283{
284 struct ip6t_policy_info *info = policy_info;
285 struct ip6t_policy_elem *e;
286 int i;
287
288 if (info == NULL)
289 exit_error(PARAMETER_PROBLEM,
290 "policy match: no parameters given");
291
292 if (!(info->flags & (IP6T_POLICY_MATCH_IN|IP6T_POLICY_MATCH_OUT)))
293 exit_error(PARAMETER_PROBLEM,
294 "policy match: neither --in nor --out specified");
295
296 if (info->flags & IP6T_POLICY_MATCH_NONE) {
297 if (info->flags & IP6T_POLICY_MATCH_STRICT)
298 exit_error(PARAMETER_PROBLEM,
299 "policy match: policy none but --strict given");
300
301 if (info->len != 0)
302 exit_error(PARAMETER_PROBLEM,
303 "policy match: policy none but policy given");
304 } else
305 info->len++; /* increase len by 1, no --next after last element */
306
307 if (!(info->flags & IP6T_POLICY_MATCH_STRICT) && info->len > 1)
308 exit_error(PARAMETER_PROBLEM,
309 "policy match: multiple elements but no --strict");
310
311 for (i = 0; i < info->len; i++) {
312 e = &info->pol[i];
Patrick McHardya46d88d2006-01-12 09:43:18 +0000313
Noticed by Tom Eastep3f347562006-01-22 13:47:07 +0000314 if (info->flags & IP6T_POLICY_MATCH_STRICT &&
315 !(e->match.reqid || e->match.spi || e->match.saddr ||
Patrick McHardya46d88d2006-01-12 09:43:18 +0000316 e->match.daddr || e->match.proto || e->match.mode))
317 exit_error(PARAMETER_PROBLEM,
318 "policy match: empty policy element");
319
Patrick McHardy524bb802005-11-19 09:00:03 +0000320 if ((e->match.saddr || e->match.daddr)
321 && ((e->mode == IP6T_POLICY_MODE_TUNNEL && e->invert.mode) ||
322 (e->mode == IP6T_POLICY_MODE_TRANSPORT && !e->invert.mode)))
323 exit_error(PARAMETER_PROBLEM,
324 "policy match: --tunnel-src/--tunnel-dst "
325 "is only valid in tunnel mode");
326 }
327}
328
329static void print_mode(char *prefix, u_int8_t mode, int numeric)
330{
331 printf("%smode ", prefix);
332
333 switch (mode) {
334 case IP6T_POLICY_MODE_TRANSPORT:
335 printf("transport ");
336 break;
337 case IP6T_POLICY_MODE_TUNNEL:
338 printf("tunnel ");
339 break;
340 default:
341 printf("??? ");
342 break;
343 }
344}
345
346static void print_proto(char *prefix, u_int8_t proto, int numeric)
347{
348 struct protoent *p = NULL;
349
350 printf("%sproto ", prefix);
351 if (!numeric)
352 p = getprotobynumber(proto);
353 if (p != NULL)
354 printf("%s ", p->p_name);
355 else
356 printf("%u ", proto);
357}
358
359#define PRINT_INVERT(x) \
360do { \
361 if (x) \
362 printf("! "); \
363} while(0)
364
365static void print_entry(char *prefix, const struct ip6t_policy_elem *e,
366 int numeric)
367{
368 if (e->match.reqid) {
369 PRINT_INVERT(e->invert.reqid);
370 printf("%sreqid %u ", prefix, e->reqid);
371 }
372 if (e->match.spi) {
373 PRINT_INVERT(e->invert.spi);
374 printf("%sspi 0x%x ", prefix, e->spi);
375 }
376 if (e->match.proto) {
377 PRINT_INVERT(e->invert.proto);
378 print_proto(prefix, e->proto, numeric);
379 }
380 if (e->match.mode) {
381 PRINT_INVERT(e->invert.mode);
382 print_mode(prefix, e->mode, numeric);
383 }
384 if (e->match.daddr) {
385 PRINT_INVERT(e->invert.daddr);
386 printf("%stunnel-dst %s%s ", prefix,
387 addr_to_numeric((struct in6_addr *)&e->daddr),
388 mask_to_numeric((struct in6_addr *)&e->dmask));
389 }
390 if (e->match.saddr) {
391 PRINT_INVERT(e->invert.saddr);
392 printf("%stunnel-src %s%s ", prefix,
393 addr_to_numeric((struct in6_addr *)&e->saddr),
394 mask_to_numeric((struct in6_addr *)&e->smask));
395 }
396}
397
398static void print_flags(char *prefix, const struct ip6t_policy_info *info)
399{
400 if (info->flags & IP6T_POLICY_MATCH_IN)
401 printf("%sdir in ", prefix);
402 else
403 printf("%sdir out ", prefix);
404
405 if (info->flags & IP6T_POLICY_MATCH_NONE)
406 printf("%spol none ", prefix);
407 else
408 printf("%spol ipsec ", prefix);
409
410 if (info->flags & IP6T_POLICY_MATCH_STRICT)
411 printf("%sstrict ", prefix);
412}
413
Jan Engelhardt997045f2007-10-04 16:29:21 +0000414static void policy_print(const void *ip, const struct xt_entry_match *match,
415 int numeric)
Patrick McHardy524bb802005-11-19 09:00:03 +0000416{
417 const struct ip6t_policy_info *info = (void *)match->data;
418 unsigned int i;
419
420 printf("policy match ");
421 print_flags("", info);
422 for (i = 0; i < info->len; i++) {
423 if (info->len > 1)
424 printf("[%u] ", i);
425 print_entry("", &info->pol[i], numeric);
426 }
427
428 printf("\n");
429}
430
Jan Engelhardt997045f2007-10-04 16:29:21 +0000431static void policy_save(const void *ip, const struct xt_entry_match *match)
Patrick McHardy524bb802005-11-19 09:00:03 +0000432{
433 const struct ip6t_policy_info *info = (void *)match->data;
434 unsigned int i;
435
436 print_flags("--", info);
437 for (i = 0; i < info->len; i++) {
438 print_entry("--", &info->pol[i], 0);
439 if (i + 1 < info->len)
440 printf("--next ");
441 }
442}
443
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200444static struct xtables_match policy_mt6_reg = {
Patrick McHardy524bb802005-11-19 09:00:03 +0000445 .name = "policy",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200446 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100447 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200448 .size = XT_ALIGN(sizeof(struct ip6t_policy_info)),
449 .userspacesize = XT_ALIGN(sizeof(struct ip6t_policy_info)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000450 .help = policy_help,
451 .parse = policy_parse,
452 .final_check = policy_check,
453 .print = policy_print,
454 .save = policy_save,
455 .extra_opts = policy_opts,
Patrick McHardy524bb802005-11-19 09:00:03 +0000456};
457
458void _init(void)
459{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200460 xtables_register_match(&policy_mt6_reg);
Patrick McHardy524bb802005-11-19 09:00:03 +0000461}