Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables for condition match */ |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <getopt.h> |
| 6 | #include <iptables.h> |
| 7 | |
| 8 | #include<linux/netfilter_ipv4/ip_tables.h> |
| 9 | #include<linux/netfilter_ipv4/ipt_condition.h> |
| 10 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 11 | static void condition_help(void) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 12 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 13 | printf("condition match v%s options:\n" |
| 14 | "--condition [!] filename " |
| 15 | "Match on boolean value stored in /proc file\n", |
| 16 | IPTABLES_VERSION); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +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 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 24 | static int condition_parse(int c, char **argv, int invert, unsigned int *flags, |
| 25 | const void *entry, struct xt_entry_match **match) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 26 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 27 | struct condition_info *info = |
| 28 | (struct condition_info *) (*match)->data; |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 29 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 30 | if (c == 'X') { |
Stephane Ouellette | fbe3abe | 2003-03-26 14:42:35 +0000 | [diff] [blame] | 31 | if (*flags) |
| 32 | exit_error(PARAMETER_PROBLEM, |
| 33 | "Can't specify multiple conditions"); |
| 34 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 35 | check_inverse(optarg, &invert, &optind, 0); |
| 36 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 37 | if (strlen(argv[optind - 1]) < CONDITION_NAME_LEN) |
| 38 | strcpy(info->name, argv[optind - 1]); |
| 39 | else |
| 40 | exit_error(PARAMETER_PROBLEM, |
| 41 | "File name too long"); |
| 42 | |
| 43 | info->invert = invert; |
| 44 | *flags = 1; |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | return 0; |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 51 | static void condition_check(unsigned int flags) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 52 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 53 | if (!flags) |
| 54 | exit_error(PARAMETER_PROBLEM, |
| 55 | "Condition match: must specify --condition"); |
| 56 | } |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 57 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 58 | static void condition_print(const void *ip, const struct xt_entry_match *match, |
| 59 | int numeric) |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 60 | { |
| 61 | const struct condition_info *info = |
| 62 | (const struct condition_info *) match->data; |
| 63 | |
| 64 | printf("condition %s%s ", (info->invert) ? "!" : "", info->name); |
| 65 | } |
| 66 | |
| 67 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 68 | 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] | 69 | { |
| 70 | const struct condition_info *info = |
| 71 | (const struct condition_info *) match->data; |
| 72 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 73 | printf("--condition %s\"%s\" ", (info->invert) ? "! " : "", info->name); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 76 | static struct iptables_match condition_match = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 77 | .name = "condition", |
| 78 | .version = IPTABLES_VERSION, |
| 79 | .size = IPT_ALIGN(sizeof(struct condition_info)), |
| 80 | .userspacesize = IPT_ALIGN(sizeof(struct condition_info)), |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 81 | .help = condition_help, |
| 82 | .parse = condition_parse, |
| 83 | .final_check = condition_check, |
| 84 | .print = condition_print, |
| 85 | .save = condition_save, |
| 86 | .extra_opts = condition_opts, |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 90 | void |
| 91 | _init(void) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 92 | { |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 93 | register_match(&condition_match); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 94 | } |