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 | |
| 11 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 12 | static void |
| 13 | help(void) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 14 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 15 | printf("condition match v%s options:\n" |
| 16 | "--condition [!] filename " |
| 17 | "Match on boolean value stored in /proc file\n", |
| 18 | IPTABLES_VERSION); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 22 | static struct option opts[] = { |
| 23 | { .name = "condition", .has_arg = 1, .flag = 0, .val = 'X' }, |
| 24 | { .name = 0 } |
| 25 | }; |
| 26 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 27 | static int |
| 28 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 29 | const struct ipt_entry *entry, unsigned int *nfcache, |
| 30 | struct ipt_entry_match **match) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 31 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 32 | struct condition_info *info = |
| 33 | (struct condition_info *) (*match)->data; |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 34 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 35 | if (c == 'X') { |
Stephane Ouellette | fbe3abe | 2003-03-26 14:42:35 +0000 | [diff] [blame] | 36 | if (*flags) |
| 37 | exit_error(PARAMETER_PROBLEM, |
| 38 | "Can't specify multiple conditions"); |
| 39 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 40 | check_inverse(optarg, &invert, &optind, 0); |
| 41 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 42 | if (strlen(argv[optind - 1]) < CONDITION_NAME_LEN) |
| 43 | strcpy(info->name, argv[optind - 1]); |
| 44 | else |
| 45 | exit_error(PARAMETER_PROBLEM, |
| 46 | "File name too long"); |
| 47 | |
| 48 | info->invert = invert; |
| 49 | *flags = 1; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 57 | static void |
| 58 | final_check(unsigned int flags) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 59 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 60 | if (!flags) |
| 61 | exit_error(PARAMETER_PROBLEM, |
| 62 | "Condition match: must specify --condition"); |
| 63 | } |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 64 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 65 | |
| 66 | static void |
| 67 | print(const struct ipt_ip *ip, |
| 68 | const struct ipt_entry_match *match, int numeric) |
| 69 | { |
| 70 | const struct condition_info *info = |
| 71 | (const struct condition_info *) match->data; |
| 72 | |
| 73 | printf("condition %s%s ", (info->invert) ? "!" : "", info->name); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static void |
| 78 | save(const struct ipt_ip *ip, |
| 79 | const struct ipt_entry_match *match) |
| 80 | { |
| 81 | const struct condition_info *info = |
| 82 | (const struct condition_info *) match->data; |
| 83 | |
Stephane Ouellette | ed30c6b | 2003-04-27 13:07:18 +0000 | [diff] [blame] | 84 | printf("--condition %s\"%s\" ", (info->invert) ? "! " : "", info->name); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | static struct iptables_match condition = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 89 | .name = "condition", |
| 90 | .version = IPTABLES_VERSION, |
| 91 | .size = IPT_ALIGN(sizeof(struct condition_info)), |
| 92 | .userspacesize = IPT_ALIGN(sizeof(struct condition_info)), |
| 93 | .help = &help, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 94 | .parse = &parse, |
| 95 | .final_check = &final_check, |
| 96 | .print = &print, |
| 97 | .save = &save, |
| 98 | .extra_opts = opts |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 102 | void |
| 103 | _init(void) |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 104 | { |
Stephane Ouellette | a2c7037 | 2003-02-25 11:54:56 +0000 | [diff] [blame] | 105 | register_match(&condition); |
Stephane Ouellette | d57b060 | 2002-11-02 15:00:15 +0000 | [diff] [blame] | 106 | } |