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 |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 43 | * can make use of standard arithmetic for the truth tables below : |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 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 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 64 | /* possible flags for patterns matching or parsing */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 65 | enum { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 66 | PAT_MF_IGNORE_CASE = 1 << 0, /* ignore case */ |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 67 | PAT_MF_NO_DNS = 1 << 1, /* don't perform any DNS requests */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 70 | /* possible flags for patterns storage */ |
| 71 | enum { |
| 72 | PAT_SF_TREE = 1 << 0, /* some patterns are arranged in a tree */ |
| 73 | }; |
| 74 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 75 | /* ACL match methods */ |
| 76 | enum { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 77 | PAT_MATCH_FOUND, /* just ensure that fetch found the sample */ |
| 78 | PAT_MATCH_BOOL, /* match fetch's integer value as boolean */ |
| 79 | PAT_MATCH_INT, /* unsigned integer (int) */ |
| 80 | PAT_MATCH_IP, /* IPv4/IPv6 address (IP) */ |
| 81 | PAT_MATCH_BIN, /* hex string (bin) */ |
| 82 | PAT_MATCH_LEN, /* string length (str -> int) */ |
| 83 | PAT_MATCH_STR, /* exact string match (str) */ |
| 84 | PAT_MATCH_BEG, /* beginning of string (str) */ |
| 85 | PAT_MATCH_SUB, /* substring (str) */ |
| 86 | PAT_MATCH_DIR, /* directory-like sub-string (str) */ |
| 87 | PAT_MATCH_DOM, /* domain-like sub-string (str) */ |
| 88 | PAT_MATCH_END, /* end of string (str) */ |
| 89 | PAT_MATCH_REG, /* regex (str -> reg) */ |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 90 | PAT_MATCH_REGM, /* regex (str -> reg) with match zones */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 91 | /* keep this one last */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 92 | PAT_MATCH_NUM |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 95 | #define PAT_REF_MAP 0x1 /* Set if the reference is used by at least one map. */ |
| 96 | #define PAT_REF_ACL 0x2 /* Set if the reference is used by at least one acl. */ |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 97 | #define PAT_REF_SMP 0x4 /* Flag used if the reference contains a sample. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 98 | |
| 99 | /* This struct contain a list of reference strings for dunamically |
| 100 | * updatable patterns. |
| 101 | */ |
| 102 | struct pat_ref { |
| 103 | struct list list; /* Used to chain refs. */ |
| 104 | unsigned int flags; /* flags PAT_REF_*. */ |
| 105 | char *reference; /* The reference name. */ |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 106 | int unique_id; /* Each pattern reference have unique id. */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 107 | char *display; /* String displayed to identify the pattern origin. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 108 | struct list head; /* The head of the list of struct pat_ref_elt. */ |
| 109 | struct list pat; /* The head of the list of struct pattern_expr. */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 110 | __decl_hathreads(HA_SPINLOCK_T lock); /* Lock used to protect pat ref elements */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* This is a part of struct pat_ref. Each entry contain one |
| 114 | * pattern and one associated value as original string. |
| 115 | */ |
| 116 | struct pat_ref_elt { |
| 117 | struct list list; /* Used to chain elements. */ |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 118 | struct list back_refs; /* list of users tracking this pat ref */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 119 | char *pattern; |
| 120 | char *sample; |
| 121 | int line; |
| 122 | }; |
| 123 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 124 | /* This contain each tree indexed entry. This struct permit to associate |
| 125 | * "sample" with a tree entry. It is used with maps. |
| 126 | */ |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 127 | struct pattern_tree { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 128 | struct sample_data *data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 129 | struct pat_ref_elt *ref; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 130 | struct ebmb_node node; |
| 131 | }; |
| 132 | |
| 133 | /* This describes one ACL pattern, which might be a single value or a tree of |
| 134 | * values. All patterns for a single ACL expression are linked together. Some |
| 135 | * of them might have a type (eg: IP). Right now, the types are shared with |
| 136 | * the samples, though it is possible that in the future this will change to |
| 137 | * accommodate for other types (eg: meth, regex). Unsigned and constant types |
| 138 | * are preferred when there is a doubt. |
| 139 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 140 | struct pattern { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 141 | int type; /* type of the ACL pattern (SMP_T_*) */ |
| 142 | union { |
| 143 | int i; /* integer value */ |
| 144 | struct { |
| 145 | signed long long min, max; |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 146 | unsigned int min_set:1; |
| 147 | unsigned int max_set:1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 148 | } range; /* integer range */ |
| 149 | struct { |
| 150 | struct in_addr addr; |
| 151 | struct in_addr mask; |
| 152 | } ipv4; /* IPv4 address */ |
| 153 | struct { |
| 154 | struct in6_addr addr; |
| 155 | unsigned char mask; /* number of bits */ |
| 156 | } ipv6; /* IPv6 address/mask */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 157 | struct eb_root *tree; /* tree storing all values if any */ |
| 158 | } val; /* direct value */ |
| 159 | union { |
| 160 | void *ptr; /* any data */ |
| 161 | char *str; /* any string */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 162 | struct my_regex *reg; /* a compiled regex */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 163 | } ptr; /* indirect values, allocated */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 164 | int len; /* data length when required */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 165 | int sflags; /* flags relative to the storage method. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 166 | struct sample_data *data; /* used to store a pointer to sample value associated |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 167 | with the match. It is used with maps */ |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 168 | struct pat_ref_elt *ref; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 169 | }; |
| 170 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 171 | /* This struct is just used for chaining patterns */ |
| 172 | struct pattern_list { |
| 173 | struct list list; |
| 174 | struct pattern pat; |
| 175 | }; |
| 176 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 177 | /* Description of a pattern expression. |
| 178 | * It contains pointers to the parse and match functions, and a list or tree of |
| 179 | * patterns to test against. The structure is organized so that the hot parts |
| 180 | * are grouped together in order to optimize caching. |
| 181 | */ |
| 182 | struct pattern_expr { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 183 | struct list list; /* Used for chaining pattern_expr in pat_ref. */ |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 184 | unsigned long long revision; /* updated for each update */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 185 | struct pat_ref *ref; /* The pattern reference if exists. */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 186 | struct pattern_head *pat_head; /* Point to the pattern_head that contain manipulation functions. |
| 187 | * Note that this link point on compatible head but not on the real |
| 188 | * head. You can use only the function, and you must not use the |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 189 | * "head". Don't write "(struct pattern_expr *)any->pat_head->expr". |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 190 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 191 | struct list patterns; /* list of acl_patterns */ |
| 192 | struct eb_root pattern_tree; /* may be used for lookup in large datasets */ |
| 193 | struct eb_root pattern_tree_2; /* may be used for different types */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 194 | int mflags; /* flags relative to the parsing or matching method. */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 195 | __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect patterns */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 198 | /* This is a list of expression. A struct pattern_expr can be used by |
| 199 | * more than one "struct pattern_head". this intermediate struct |
| 200 | * permit more than one list. |
| 201 | */ |
| 202 | struct pattern_expr_list { |
| 203 | struct list list; /* Used for chaining pattern_expr in pattern_head. */ |
| 204 | int do_free; |
| 205 | struct pattern_expr *expr; /* The used expr. */ |
| 206 | }; |
| 207 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 208 | /* This struct contain a list of pattern expr */ |
| 209 | struct pattern_head { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 210 | int (*parse)(const char *text, struct pattern *pattern, int flags, char **err); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 211 | int (*parse_smp)(const char *text, struct sample_data *data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 212 | int (*index)(struct pattern_expr *, struct pattern *, char **); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 213 | void (*delete)(struct pattern_expr *, struct pat_ref_elt *); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 214 | void (*prune)(struct pattern_expr *); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 215 | struct pattern *(*match)(struct sample *, struct pattern_expr *, int); |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 216 | int expect_type; /* type of the expected sample (SMP_T_*) */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 217 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 218 | struct list head; /* This is a list of struct pattern_expr_list. */ |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 219 | }; |
| 220 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 221 | /* This is the root of the list of all pattern_ref avalaibles. */ |
| 222 | extern struct list pattern_reference; |
| 223 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 224 | #endif /* _TYPES_PATTERN_H */ |