Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables for condition match */ |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 2 | #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 Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 11 | static void condition_help(void) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 12 | { |
| 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 Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 19 | static const struct option condition_opts[] = { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 20 | { .name = "condition", .has_arg = 1, .flag = 0, .val = 'X' }, |
| 21 | { .name = 0 } |
| 22 | }; |
| 23 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 24 | static int |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 25 | condition_parse(int c, char **argv, int invert, unsigned int *flags, |
| 26 | const void *entry, struct xt_entry_match **match) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 27 | { |
| 28 | struct condition6_info *info = |
| 29 | (struct condition6_info *) (*match)->data; |
| 30 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 31 | if (c == 'X') { |
Stephane Ouellette | fbe3abe | 2003-03-26 14:42:35 +0000 | [diff] [blame] | 32 | if (*flags) |
| 33 | exit_error(PARAMETER_PROBLEM, |
| 34 | "Can't specify multiple conditions"); |
| 35 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 36 | check_inverse(optarg, &invert, &optind, 0); |
| 37 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 38 | 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 Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 52 | static void condition_check(unsigned int flags) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 53 | { |
| 54 | if (!flags) |
| 55 | exit_error(PARAMETER_PROBLEM, |
| 56 | "Condition match: must specify --condition"); |
| 57 | } |
| 58 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 59 | static void condition_print(const void *ip, const struct xt_entry_match *match, |
| 60 | int numeric) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 61 | { |
| 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 Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 69 | static void condition_save(const void *ip, const struct xt_entry_match *match) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 70 | { |
| 71 | const struct condition6_info *info = |
| 72 | (const struct condition6_info *) match->data; |
| 73 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 74 | printf("--condition %s\"%s\" ", (info->invert) ? "! " : "", info->name); |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 77 | static struct ip6tables_match condition_match6 = { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 78 | .name = "condition", |
| 79 | .version = IPTABLES_VERSION, |
| 80 | .size = IP6T_ALIGN(sizeof(struct condition6_info)), |
| 81 | .userspacesize = IP6T_ALIGN(sizeof(struct condition6_info)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 82 | .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 Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | void |
| 92 | _init(void) |
| 93 | { |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 94 | register_match6(&condition_match6); |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 95 | } |