blob: 03e27229983468c981882d77d140bb41a8d00306 [file] [log] [blame]
Stephane Ouelletteed30c6b2003-04-27 13:07:18 +00001/* Shared library add-on to ip6tables for condition match */
Stephane Ouellettea2c70372003-02-25 11:54:56 +00002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <getopt.h>
6#include <ip6tables.h>
7
8#include<linux/netfilter_ipv6/ip6_tables.h>
9#include<linux/netfilter_ipv6/ip6t_condition.h>
10
Jan Engelhardt997045f2007-10-04 16:29:21 +000011static void condition_help(void)
Stephane Ouellettea2c70372003-02-25 11:54:56 +000012{
13 printf("condition match v%s options:\n"
14 "--condition [!] filename "
15 "Match on boolean value stored in /proc file\n",
16 IPTABLES_VERSION);
17}
18
Jan Engelhardt997045f2007-10-04 16:29:21 +000019static const struct option condition_opts[] = {
Stephane Ouellettea2c70372003-02-25 11:54:56 +000020 { .name = "condition", .has_arg = 1, .flag = 0, .val = 'X' },
21 { .name = 0 }
22};
23
Stephane Ouellettea2c70372003-02-25 11:54:56 +000024static int
Jan Engelhardt997045f2007-10-04 16:29:21 +000025condition_parse(int c, char **argv, int invert, unsigned int *flags,
26 const void *entry, struct xt_entry_match **match)
Stephane Ouellettea2c70372003-02-25 11:54:56 +000027{
28 struct condition6_info *info =
29 (struct condition6_info *) (*match)->data;
30
Stephane Ouellettea2c70372003-02-25 11:54:56 +000031 if (c == 'X') {
Stephane Ouellettefbe3abe2003-03-26 14:42:35 +000032 if (*flags)
33 exit_error(PARAMETER_PROBLEM,
34 "Can't specify multiple conditions");
35
Stephane Ouelletteed30c6b2003-04-27 13:07:18 +000036 check_inverse(optarg, &invert, &optind, 0);
37
Stephane Ouellettea2c70372003-02-25 11:54:56 +000038 if (strlen(argv[optind - 1]) < CONDITION6_NAME_LEN)
39 strcpy(info->name, argv[optind - 1]);
40 else
41 exit_error(PARAMETER_PROBLEM,
42 "File name too long");
43
44 info->invert = invert;
45 *flags = 1;
46 return 1;
47 }
48
49 return 0;
50}
51
Jan Engelhardt997045f2007-10-04 16:29:21 +000052static void condition_check(unsigned int flags)
Stephane Ouellettea2c70372003-02-25 11:54:56 +000053{
54 if (!flags)
55 exit_error(PARAMETER_PROBLEM,
56 "Condition match: must specify --condition");
57}
58
Jan Engelhardt997045f2007-10-04 16:29:21 +000059static void condition_print(const void *ip, const struct xt_entry_match *match,
60 int numeric)
Stephane Ouellettea2c70372003-02-25 11:54:56 +000061{
62 const struct condition6_info *info =
63 (const struct condition6_info *) match->data;
64
65 printf("condition %s%s ", (info->invert) ? "!" : "", info->name);
66}
67
68
Jan Engelhardt997045f2007-10-04 16:29:21 +000069static void condition_save(const void *ip, const struct xt_entry_match *match)
Stephane Ouellettea2c70372003-02-25 11:54:56 +000070{
71 const struct condition6_info *info =
72 (const struct condition6_info *) match->data;
73
Stephane Ouelletteed30c6b2003-04-27 13:07:18 +000074 printf("--condition %s\"%s\" ", (info->invert) ? "! " : "", info->name);
Stephane Ouellettea2c70372003-02-25 11:54:56 +000075}
76
Jan Engelhardt997045f2007-10-04 16:29:21 +000077static struct ip6tables_match condition_match6 = {
Stephane Ouellettea2c70372003-02-25 11:54:56 +000078 .name = "condition",
79 .version = IPTABLES_VERSION,
80 .size = IP6T_ALIGN(sizeof(struct condition6_info)),
81 .userspacesize = IP6T_ALIGN(sizeof(struct condition6_info)),
Jan Engelhardt997045f2007-10-04 16:29:21 +000082 .help = condition_help,
83 .parse = condition_parse,
84 .final_check = condition_check,
85 .print = condition_print,
86 .save = condition_save,
87 .extra_opts = condition_opts,
Stephane Ouellettea2c70372003-02-25 11:54:56 +000088};
89
90
91void
92_init(void)
93{
Jan Engelhardt997045f2007-10-04 16:29:21 +000094 register_match6(&condition_match6);
Stephane Ouellettea2c70372003-02-25 11:54:56 +000095}