Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/types/pattern.h |
| 3 | * This file provides structures and types for ACLs. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _TYPES_PATTERN_H |
| 23 | #define _TYPES_PATTERN_H |
| 24 | |
| 25 | #include <common/compat.h> |
| 26 | #include <common/config.h> |
| 27 | #include <common/mini-clist.h> |
| 28 | #include <common/regex.h> |
| 29 | |
| 30 | #include <types/sample.h> |
| 31 | |
| 32 | #include <ebmbtree.h> |
| 33 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 34 | /* Pattern matching function result. |
| 35 | * |
| 36 | * We're using a 3-state matching system to match samples against patterns in |
| 37 | * ACLs : |
| 38 | * - PASS : at least one pattern already matches |
| 39 | * - MISS : some data is missing to decide if some rules may finally match. |
| 40 | * - FAIL : no mattern may ever match |
| 41 | * |
| 42 | * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we |
| 43 | * can make use of standard arithmetics for the truth tables below : |
| 44 | * |
| 45 | * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3) |
| 46 | * ------+----- -----+------+------+----- -----+------+------+----- |
| 47 | * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3) |
| 48 | * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3) |
| 49 | * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3) |
| 50 | * |
| 51 | * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y) |
| 52 | * |
| 53 | * For efficiency, the ACL return flags are directly mapped from the pattern |
| 54 | * match flags. A pattern can't return "MISS" since it's always presented an |
| 55 | * existing sample. So that leaves us with only two possible values : |
| 56 | * MATCH = 0 |
| 57 | * NOMATCH = 3 |
| 58 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 59 | enum pat_match_res { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 60 | PAT_NOMATCH = 0, /* sample didn't match any pattern */ |
| 61 | PAT_MATCH = 3, /* sample matched at least one pattern */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* possible flags for expressions or patterns */ |
| 65 | enum { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 66 | PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */ |
| 67 | PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 68 | PAT_F_TREE = 1 << 2, /* some patterns are arranged in a tree */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* ACL match methods */ |
| 72 | enum { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 73 | PAT_MATCH_FOUND, /* just ensure that fetch found the sample */ |
| 74 | PAT_MATCH_BOOL, /* match fetch's integer value as boolean */ |
| 75 | PAT_MATCH_INT, /* unsigned integer (int) */ |
| 76 | PAT_MATCH_IP, /* IPv4/IPv6 address (IP) */ |
| 77 | PAT_MATCH_BIN, /* hex string (bin) */ |
| 78 | PAT_MATCH_LEN, /* string length (str -> int) */ |
| 79 | PAT_MATCH_STR, /* exact string match (str) */ |
| 80 | PAT_MATCH_BEG, /* beginning of string (str) */ |
| 81 | PAT_MATCH_SUB, /* substring (str) */ |
| 82 | PAT_MATCH_DIR, /* directory-like sub-string (str) */ |
| 83 | PAT_MATCH_DOM, /* domain-like sub-string (str) */ |
| 84 | PAT_MATCH_END, /* end of string (str) */ |
| 85 | PAT_MATCH_REG, /* regex (str -> reg) */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 86 | /* keep this one last */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 87 | PAT_MATCH_NUM |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /* How to store a time range and the valid days in 29 bits */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 91 | struct pat_time { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 92 | int dow:7; /* 1 bit per day of week: 0-6 */ |
| 93 | int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */ |
| 94 | int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */ |
| 95 | }; |
| 96 | |
| 97 | /* This contain each tree indexed entry. This struct permit to associate |
| 98 | * "sample" with a tree entry. It is used with maps. |
| 99 | */ |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 100 | struct pattern_tree { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 101 | struct sample_storage *smp; |
| 102 | struct ebmb_node node; |
| 103 | }; |
| 104 | |
| 105 | /* This describes one ACL pattern, which might be a single value or a tree of |
| 106 | * values. All patterns for a single ACL expression are linked together. Some |
| 107 | * of them might have a type (eg: IP). Right now, the types are shared with |
| 108 | * the samples, though it is possible that in the future this will change to |
| 109 | * accommodate for other types (eg: meth, regex). Unsigned and constant types |
| 110 | * are preferred when there is a doubt. |
| 111 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 112 | struct pattern { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 113 | int type; /* type of the ACL pattern (SMP_T_*) */ |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 114 | int expect_type; /* type of the expected sample (SMP_T_*) */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 115 | union { |
| 116 | int i; /* integer value */ |
| 117 | struct { |
| 118 | signed long long min, max; |
| 119 | int min_set :1; |
| 120 | int max_set :1; |
| 121 | } range; /* integer range */ |
| 122 | struct { |
| 123 | struct in_addr addr; |
| 124 | struct in_addr mask; |
| 125 | } ipv4; /* IPv4 address */ |
| 126 | struct { |
| 127 | struct in6_addr addr; |
| 128 | unsigned char mask; /* number of bits */ |
| 129 | } ipv6; /* IPv6 address/mask */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 130 | struct pat_time time; /* valid hours and days */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 131 | struct eb_root *tree; /* tree storing all values if any */ |
| 132 | } val; /* direct value */ |
| 133 | union { |
| 134 | void *ptr; /* any data */ |
| 135 | char *str; /* any string */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 136 | struct my_regex *reg; /* a compiled regex */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 137 | } ptr; /* indirect values, allocated */ |
| 138 | void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */ |
| 139 | int len; /* data length when required */ |
| 140 | int flags; /* expr or pattern flags. */ |
| 141 | struct sample_storage *smp; /* used to store a pointer to sample value associated |
| 142 | with the match. It is used with maps */ |
| 143 | |
| 144 | }; |
| 145 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 146 | /* This struct is just used for chaining patterns */ |
| 147 | struct pattern_list { |
| 148 | struct list list; |
| 149 | struct pattern pat; |
| 150 | }; |
| 151 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 152 | /* Description of a pattern expression. |
| 153 | * It contains pointers to the parse and match functions, and a list or tree of |
| 154 | * patterns to test against. The structure is organized so that the hot parts |
| 155 | * are grouped together in order to optimize caching. |
| 156 | */ |
| 157 | struct pattern_expr { |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 158 | int (*parse)(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 159 | int (*index)(struct pattern_expr *, struct pattern *, char **); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame^] | 160 | void (*delete)(struct pattern_expr *, struct pattern *); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 161 | struct pattern *(*match)(struct sample *, struct pattern_expr *, int); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 162 | struct list patterns; /* list of acl_patterns */ |
| 163 | struct eb_root pattern_tree; /* may be used for lookup in large datasets */ |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 164 | struct eb_root pattern_tree_2; /* may be used for different types */ |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 167 | extern char *pat_match_names[PAT_MATCH_NUM]; |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 168 | extern int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 169 | extern int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame^] | 170 | extern void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *); |
| 171 | extern struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int); |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 172 | extern int pat_match_types[PAT_MATCH_NUM]; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 173 | |
| 174 | #endif /* _TYPES_PATTERN_H */ |