Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Pattern management functions. |
| 3 | * |
| 4 | * Copyright 2000-2013 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <stdio.h> |
Jerome Magnin | b8bd6d7 | 2020-01-17 18:01:20 +0100 | [diff] [blame] | 15 | #include <errno.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 16 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <import/ebsttree.h> |
| 18 | #include <import/lru.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 20 | #include <haproxy/api.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 21 | #include <haproxy/global.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 22 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 23 | #include <haproxy/net_helper.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 24 | #include <haproxy/pattern.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 25 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 26 | #include <haproxy/sample.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/tools.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 28 | #include <haproxy/xxhash.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 29 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 30 | |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 31 | const char *const pat_match_names[PAT_MATCH_NUM] = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 32 | [PAT_MATCH_FOUND] = "found", |
| 33 | [PAT_MATCH_BOOL] = "bool", |
| 34 | [PAT_MATCH_INT] = "int", |
| 35 | [PAT_MATCH_IP] = "ip", |
| 36 | [PAT_MATCH_BIN] = "bin", |
| 37 | [PAT_MATCH_LEN] = "len", |
| 38 | [PAT_MATCH_STR] = "str", |
| 39 | [PAT_MATCH_BEG] = "beg", |
| 40 | [PAT_MATCH_SUB] = "sub", |
| 41 | [PAT_MATCH_DIR] = "dir", |
| 42 | [PAT_MATCH_DOM] = "dom", |
| 43 | [PAT_MATCH_END] = "end", |
| 44 | [PAT_MATCH_REG] = "reg", |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 45 | [PAT_MATCH_REGM] = "regm", |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 48 | int (*const pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 49 | [PAT_MATCH_FOUND] = pat_parse_nothing, |
| 50 | [PAT_MATCH_BOOL] = pat_parse_nothing, |
| 51 | [PAT_MATCH_INT] = pat_parse_int, |
| 52 | [PAT_MATCH_IP] = pat_parse_ip, |
| 53 | [PAT_MATCH_BIN] = pat_parse_bin, |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 54 | [PAT_MATCH_LEN] = pat_parse_int, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 55 | [PAT_MATCH_STR] = pat_parse_str, |
| 56 | [PAT_MATCH_BEG] = pat_parse_str, |
| 57 | [PAT_MATCH_SUB] = pat_parse_str, |
| 58 | [PAT_MATCH_DIR] = pat_parse_str, |
| 59 | [PAT_MATCH_DOM] = pat_parse_str, |
| 60 | [PAT_MATCH_END] = pat_parse_str, |
| 61 | [PAT_MATCH_REG] = pat_parse_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 62 | [PAT_MATCH_REGM] = pat_parse_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 65 | int (*const pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 66 | [PAT_MATCH_FOUND] = pat_idx_list_val, |
| 67 | [PAT_MATCH_BOOL] = pat_idx_list_val, |
| 68 | [PAT_MATCH_INT] = pat_idx_list_val, |
| 69 | [PAT_MATCH_IP] = pat_idx_tree_ip, |
| 70 | [PAT_MATCH_BIN] = pat_idx_list_ptr, |
| 71 | [PAT_MATCH_LEN] = pat_idx_list_val, |
| 72 | [PAT_MATCH_STR] = pat_idx_tree_str, |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 73 | [PAT_MATCH_BEG] = pat_idx_tree_pfx, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 74 | [PAT_MATCH_SUB] = pat_idx_list_str, |
| 75 | [PAT_MATCH_DIR] = pat_idx_list_str, |
| 76 | [PAT_MATCH_DOM] = pat_idx_list_str, |
| 77 | [PAT_MATCH_END] = pat_idx_list_str, |
| 78 | [PAT_MATCH_REG] = pat_idx_list_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 79 | [PAT_MATCH_REGM] = pat_idx_list_regm, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 82 | void (*const pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = { |
Willy Tarreau | 6d8a689 | 2020-11-02 19:26:02 +0100 | [diff] [blame] | 83 | [PAT_MATCH_FOUND] = pat_prune_gen, |
| 84 | [PAT_MATCH_BOOL] = pat_prune_gen, |
| 85 | [PAT_MATCH_INT] = pat_prune_gen, |
| 86 | [PAT_MATCH_IP] = pat_prune_gen, |
| 87 | [PAT_MATCH_BIN] = pat_prune_gen, |
| 88 | [PAT_MATCH_LEN] = pat_prune_gen, |
| 89 | [PAT_MATCH_STR] = pat_prune_gen, |
| 90 | [PAT_MATCH_BEG] = pat_prune_gen, |
| 91 | [PAT_MATCH_SUB] = pat_prune_gen, |
| 92 | [PAT_MATCH_DIR] = pat_prune_gen, |
| 93 | [PAT_MATCH_DOM] = pat_prune_gen, |
| 94 | [PAT_MATCH_END] = pat_prune_gen, |
| 95 | [PAT_MATCH_REG] = pat_prune_gen, |
| 96 | [PAT_MATCH_REGM] = pat_prune_gen, |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 99 | struct pattern *(*const pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 100 | [PAT_MATCH_FOUND] = NULL, |
| 101 | [PAT_MATCH_BOOL] = pat_match_nothing, |
| 102 | [PAT_MATCH_INT] = pat_match_int, |
| 103 | [PAT_MATCH_IP] = pat_match_ip, |
| 104 | [PAT_MATCH_BIN] = pat_match_bin, |
| 105 | [PAT_MATCH_LEN] = pat_match_len, |
| 106 | [PAT_MATCH_STR] = pat_match_str, |
| 107 | [PAT_MATCH_BEG] = pat_match_beg, |
| 108 | [PAT_MATCH_SUB] = pat_match_sub, |
| 109 | [PAT_MATCH_DIR] = pat_match_dir, |
| 110 | [PAT_MATCH_DOM] = pat_match_dom, |
| 111 | [PAT_MATCH_END] = pat_match_end, |
| 112 | [PAT_MATCH_REG] = pat_match_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 113 | [PAT_MATCH_REGM] = pat_match_regm, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 116 | /* Just used for checking configuration compatibility */ |
Willy Tarreau | 9057a00 | 2021-04-10 17:44:27 +0200 | [diff] [blame] | 117 | int const pat_match_types[PAT_MATCH_NUM] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 118 | [PAT_MATCH_FOUND] = SMP_T_SINT, |
| 119 | [PAT_MATCH_BOOL] = SMP_T_SINT, |
| 120 | [PAT_MATCH_INT] = SMP_T_SINT, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 121 | [PAT_MATCH_IP] = SMP_T_ADDR, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 122 | [PAT_MATCH_BIN] = SMP_T_BIN, |
| 123 | [PAT_MATCH_LEN] = SMP_T_STR, |
| 124 | [PAT_MATCH_STR] = SMP_T_STR, |
| 125 | [PAT_MATCH_BEG] = SMP_T_STR, |
| 126 | [PAT_MATCH_SUB] = SMP_T_STR, |
| 127 | [PAT_MATCH_DIR] = SMP_T_STR, |
| 128 | [PAT_MATCH_DOM] = SMP_T_STR, |
| 129 | [PAT_MATCH_END] = SMP_T_STR, |
| 130 | [PAT_MATCH_REG] = SMP_T_STR, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 131 | [PAT_MATCH_REGM] = SMP_T_STR, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 134 | /* this struct is used to return information */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 135 | static THREAD_LOCAL struct pattern static_pattern; |
| 136 | static THREAD_LOCAL struct sample_data static_sample_data; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 137 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 138 | /* This is the root of the list of all pattern_ref avalaibles. */ |
| 139 | struct list pattern_reference = LIST_HEAD_INIT(pattern_reference); |
| 140 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 141 | static THREAD_LOCAL struct lru64_head *pat_lru_tree; |
Willy Tarreau | 295a89c | 2021-04-10 17:42:04 +0200 | [diff] [blame] | 142 | static unsigned long long pat_lru_seed __read_mostly; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 143 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 144 | /* |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 145 | * |
| 146 | * The following functions are not exported and are used by internals process |
| 147 | * of pattern matching |
| 148 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 149 | */ |
| 150 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 151 | /* Background: Fast way to find a zero byte in a word |
| 152 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 153 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 154 | * |
| 155 | * To look for 4 different byte values, xor the word with those bytes and |
| 156 | * then check for zero bytes: |
| 157 | * |
| 158 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 159 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 160 | * and <c> is the character that is being tested |
| 161 | */ |
| 162 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 163 | { |
| 164 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 165 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 166 | } |
| 167 | |
| 168 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 169 | { |
| 170 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 171 | } |
| 172 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 173 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 174 | /* |
| 175 | * |
| 176 | * These functions are exported and may be used by any other component. |
| 177 | * |
Willy Tarreau | 5def8ef | 2014-08-29 15:19:33 +0200 | [diff] [blame] | 178 | * The following functions are used for parsing pattern matching input value. |
| 179 | * The <text> contain the string to be parsed. <pattern> must be a preallocated |
| 180 | * pattern. The pat_parse_* functions fill this structure with the parsed value. |
| 181 | * <err> is filled with an error message built with memprintf() function. It is |
| 182 | * allowed to use a trash as a temporary storage for the returned pattern, as |
| 183 | * the next call after these functions will be pat_idx_*. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 184 | * |
Willy Tarreau | 5def8ef | 2014-08-29 15:19:33 +0200 | [diff] [blame] | 185 | * In success case, the pat_parse_* function returns 1. If the function |
| 186 | * fails, it returns 0 and <err> is filled. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 187 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 188 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 189 | /* ignore the current line */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 190 | int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 191 | { |
| 192 | return 1; |
| 193 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 194 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 195 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 196 | int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 197 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 198 | pattern->type = SMP_T_STR; |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 199 | pattern->ptr.str = (char *)text; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 200 | pattern->len = strlen(text); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 201 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 204 | /* Parse a binary written in hexa. It is allocated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 205 | int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 206 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 207 | struct buffer *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 208 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 209 | pattern->type = SMP_T_BIN; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 210 | trash = get_trash_chunk(); |
| 211 | pattern->len = trash->size; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 212 | pattern->ptr.str = trash->area; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 213 | return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 216 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 217 | int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 218 | { |
Thierry FOURNIER | 0b6d15f | 2014-01-29 19:35:16 +0100 | [diff] [blame] | 219 | pattern->ptr.str = (char *)text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 224 | * one integer is read, it is set as both min and max. An operator may be |
| 225 | * specified as the prefix, among this list of 5 : |
| 226 | * |
| 227 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 228 | * |
| 229 | * The default operator is "eq". It supports range matching. Ranges are |
| 230 | * rejected for other operators. The operator may be changed at any time. |
| 231 | * The operator is stored in the 'opaque' argument. |
| 232 | * |
| 233 | * If err is non-NULL, an error message will be returned there on errors and |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 234 | * the caller will have to free it. The function returns zero on error, and |
| 235 | * non-zero on success. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 236 | * |
| 237 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 238 | int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 239 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 240 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 241 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 242 | pattern->type = SMP_T_SINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 243 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 244 | /* Empty string is not valid */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 245 | if (!*text) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 246 | goto not_valid_range; |
| 247 | |
| 248 | /* Search ':' or '-' separator. */ |
| 249 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 250 | ptr++; |
| 251 | |
| 252 | /* If separator not found. */ |
| 253 | if (!*ptr) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 254 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) { |
| 255 | memprintf(err, "'%s' is not a number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 256 | return 0; |
| 257 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 258 | pattern->val.range.max = pattern->val.range.min; |
| 259 | pattern->val.range.min_set = 1; |
| 260 | pattern->val.range.max_set = 1; |
| 261 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 264 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 265 | if (ptr == text && *(ptr + 1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 266 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 267 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 268 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 269 | pattern->val.range.min_set = 0; |
| 270 | pattern->val.range.max_set = 1; |
| 271 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 274 | /* If separator is the last character. */ |
| 275 | if (*(ptr + 1) == '\0') { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 276 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 277 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 278 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 279 | pattern->val.range.min_set = 1; |
| 280 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 281 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 282 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 283 | |
| 284 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 285 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 286 | goto not_valid_range; |
| 287 | |
| 288 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 289 | goto not_valid_range; |
| 290 | |
| 291 | if (pattern->val.range.min > pattern->val.range.max) |
| 292 | goto not_valid_range; |
| 293 | |
| 294 | pattern->val.range.min_set = 1; |
| 295 | pattern->val.range.max_set = 1; |
| 296 | return 1; |
| 297 | |
| 298 | not_valid_range: |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 299 | memprintf(err, "'%s' is not a valid number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 300 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 304 | * '-'. The version consists in a major and a minor, both of which must be |
| 305 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 306 | * integer. |
| 307 | * If only one version is read, it is set as both min and max. Just like for |
| 308 | * pure integers, an operator may be specified as the prefix, among this list |
| 309 | * of 5 : |
| 310 | * |
| 311 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 312 | * |
| 313 | * The default operator is "eq". It supports range matching. Ranges are |
| 314 | * rejected for other operators. The operator may be changed at any time. |
| 315 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 316 | * such as the following one : |
| 317 | * |
| 318 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 319 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 320 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 321 | * |
| 322 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 323 | int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 324 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 325 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 326 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 327 | pattern->type = SMP_T_SINT; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 328 | |
| 329 | /* Search ':' or '-' separator. */ |
| 330 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 331 | ptr++; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 332 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 333 | /* If separator not found. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 334 | if (*ptr == '\0' && ptr > text) { |
| 335 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 336 | memprintf(err, "'%s' is not a dotted number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 339 | pattern->val.range.max = pattern->val.range.min; |
| 340 | pattern->val.range.min_set = 1; |
| 341 | pattern->val.range.max_set = 1; |
| 342 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 345 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 346 | if (ptr == text && *(ptr+1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 347 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 348 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 349 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 350 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 351 | pattern->val.range.min_set = 0; |
| 352 | pattern->val.range.max_set = 1; |
| 353 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 356 | /* If separator is the last character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 357 | if (ptr == &text[strlen(text)-1]) { |
| 358 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 359 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 360 | return 0; |
| 361 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 362 | pattern->val.range.min_set = 1; |
| 363 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 368 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 369 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 373 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | if (pattern->val.range.min > pattern->val.range.max) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 377 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 378 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 379 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 380 | pattern->val.range.min_set = 1; |
| 381 | pattern->val.range.max_set = 1; |
| 382 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 386 | * The addr may either be an IPv4 address or a hostname. The mask |
| 387 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 388 | * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6). |
| 389 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 390 | int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 391 | { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 392 | if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING), |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 393 | &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 394 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 395 | return 1; |
| 396 | } |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 397 | else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 398 | pattern->type = SMP_T_IPV6; |
| 399 | return 1; |
| 400 | } |
| 401 | else { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 402 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | } |
| 406 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 407 | /* |
| 408 | * |
| 409 | * These functions are exported and may be used by any other component. |
| 410 | * |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 411 | * This function just takes a sample <smp> and checks if this sample matches |
| 412 | * with the pattern <pattern>. This function returns only PAT_MATCH or |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 413 | * PAT_NOMATCH. |
| 414 | * |
| 415 | */ |
| 416 | |
| 417 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 418 | struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 419 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 420 | if (smp->data.u.sint) { |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 421 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 422 | static_pattern.data = NULL; |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 423 | static_pattern.ref = NULL; |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 424 | static_pattern.type = 0; |
| 425 | static_pattern.ptr.str = NULL; |
| 426 | } |
| 427 | return &static_pattern; |
| 428 | } |
| 429 | else |
| 430 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 434 | /* NB: For two strings to be identical, it is required that their length match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 435 | struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 436 | { |
| 437 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 438 | struct ebmb_node *node; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 439 | struct pattern_tree *elt; |
| 440 | struct pattern_list *lst; |
| 441 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 442 | struct pattern *ret = NULL; |
| 443 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 444 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 445 | /* Lookup a string in the expression's pattern tree. */ |
| 446 | if (!eb_is_empty(&expr->pattern_tree)) { |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 447 | char prev = 0; |
| 448 | |
| 449 | if (smp->data.u.str.data < smp->data.u.str.size) { |
| 450 | /* we may have to force a trailing zero on the test pattern and |
Thierry Fournier | a68affe | 2020-11-10 20:51:36 +0100 | [diff] [blame] | 451 | * the buffer is large enough to accommodate it. If the flag |
| 452 | * CONST is set, duplicate the string |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 453 | */ |
| 454 | prev = smp->data.u.str.area[smp->data.u.str.data]; |
Thierry Fournier | a68affe | 2020-11-10 20:51:36 +0100 | [diff] [blame] | 455 | if (prev) { |
| 456 | if (smp->flags & SMP_F_CONST) { |
| 457 | if (!smp_dup(smp)) |
| 458 | return NULL; |
| 459 | } else { |
| 460 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
| 461 | } |
| 462 | } |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 463 | } |
| 464 | else { |
| 465 | /* Otherwise, the sample is duplicated. A trailing zero |
| 466 | * is automatically added to the string. |
| 467 | */ |
| 468 | if (!smp_dup(smp)) |
| 469 | return NULL; |
| 470 | } |
| 471 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 472 | node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 473 | if (prev) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 474 | smp->data.u.str.area[smp->data.u.str.data] = prev; |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 475 | |
| 476 | while (node) { |
| 477 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 478 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 479 | node = ebmb_next_dup(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 480 | continue; |
| 481 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 482 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 483 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 484 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 485 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 486 | static_pattern.type = SMP_T_STR; |
| 487 | static_pattern.ptr.str = (char *)elt->node.key; |
| 488 | } |
| 489 | return &static_pattern; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 494 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 495 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 496 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 497 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 498 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 499 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 500 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 501 | return ret; |
| 502 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 505 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 506 | list_for_each_entry(lst, &expr->patterns, list) { |
| 507 | pattern = &lst->pat; |
| 508 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 509 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 510 | continue; |
| 511 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 512 | if (pattern->len != smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 513 | continue; |
| 514 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 515 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 516 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) || |
| 517 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 518 | ret = pattern; |
| 519 | break; |
| 520 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 523 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 524 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 525 | |
| 526 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* NB: For two binaries buf to be identical, it is required that their lengths match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 530 | struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 531 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 532 | struct pattern_list *lst; |
| 533 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 534 | struct pattern *ret = NULL; |
| 535 | struct lru64 *lru = NULL; |
| 536 | |
| 537 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 538 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 539 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 540 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 541 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 542 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 543 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 544 | return ret; |
| 545 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 548 | list_for_each_entry(lst, &expr->patterns, list) { |
| 549 | pattern = &lst->pat; |
| 550 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 551 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 552 | continue; |
| 553 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 554 | if (pattern->len != smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 555 | continue; |
| 556 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 557 | if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 558 | ret = pattern; |
| 559 | break; |
| 560 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 563 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 564 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 565 | |
| 566 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 570 | * and restores the previous character when leaving. This function fills |
| 571 | * a matching array. |
| 572 | */ |
| 573 | struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill) |
| 574 | { |
| 575 | struct pattern_list *lst; |
| 576 | struct pattern *pattern; |
| 577 | struct pattern *ret = NULL; |
| 578 | |
| 579 | list_for_each_entry(lst, &expr->patterns, list) { |
| 580 | pattern = &lst->pat; |
| 581 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 582 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 583 | continue; |
| 584 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 585 | if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 586 | MAX_MATCH, pmatch, 0)) { |
| 587 | ret = pattern; |
| 588 | smp->ctx.a[0] = pmatch; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 597 | * and restores the previous character when leaving. |
| 598 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 599 | struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 600 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 601 | struct pattern_list *lst; |
| 602 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 603 | struct pattern *ret = NULL; |
| 604 | struct lru64 *lru = NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 605 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 606 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 607 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 608 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 609 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 610 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 611 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 612 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 613 | return ret; |
| 614 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 617 | list_for_each_entry(lst, &expr->patterns, list) { |
| 618 | pattern = &lst->pat; |
| 619 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 620 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 621 | continue; |
| 622 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 623 | if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 624 | ret = pattern; |
| 625 | break; |
| 626 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 627 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 628 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 629 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 630 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 631 | |
| 632 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 636 | struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 637 | { |
| 638 | int icase; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 639 | struct ebmb_node *node; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 640 | struct pattern_tree *elt; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 641 | struct pattern_list *lst; |
| 642 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 643 | struct pattern *ret = NULL; |
| 644 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 645 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 646 | /* Lookup a string in the expression's pattern tree. */ |
| 647 | if (!eb_is_empty(&expr->pattern_tree)) { |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 648 | char prev = 0; |
| 649 | |
| 650 | if (smp->data.u.str.data < smp->data.u.str.size) { |
| 651 | /* we may have to force a trailing zero on the test pattern and |
Aurelien DARRAGON | 47a08e8 | 2024-09-06 16:21:02 +0200 | [diff] [blame] | 652 | * the buffer is large enough to accommodate it. If the flag |
| 653 | * CONST is set, duplicate the string |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 654 | */ |
| 655 | prev = smp->data.u.str.area[smp->data.u.str.data]; |
Aurelien DARRAGON | 47a08e8 | 2024-09-06 16:21:02 +0200 | [diff] [blame] | 656 | if (prev) { |
| 657 | if (smp->flags & SMP_F_CONST) { |
| 658 | if (!smp_dup(smp)) |
| 659 | return NULL; |
| 660 | } else { |
| 661 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
| 662 | } |
| 663 | } |
Christopher Faulet | b4cf7ab | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 664 | } |
| 665 | else { |
| 666 | /* Otherwise, the sample is duplicated. A trailing zero |
| 667 | * is automatically added to the string. |
| 668 | */ |
| 669 | if (!smp_dup(smp)) |
| 670 | return NULL; |
| 671 | } |
| 672 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 673 | node = ebmb_lookup_longest(&expr->pattern_tree, |
| 674 | smp->data.u.str.area); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 675 | if (prev) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 676 | smp->data.u.str.area[smp->data.u.str.data] = prev; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 677 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 678 | while (node) { |
| 679 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 680 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 681 | node = ebmb_lookup_shorter(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 682 | continue; |
| 683 | } |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 684 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 685 | static_pattern.data = elt->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 686 | static_pattern.ref = elt->ref; |
| 687 | static_pattern.sflags = PAT_SF_TREE; |
| 688 | static_pattern.type = SMP_T_STR; |
| 689 | static_pattern.ptr.str = (char *)elt->node.key; |
| 690 | } |
| 691 | return &static_pattern; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 696 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 697 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 698 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 699 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 700 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 701 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 702 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 703 | return ret; |
| 704 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 707 | list_for_each_entry(lst, &expr->patterns, list) { |
| 708 | pattern = &lst->pat; |
| 709 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 710 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 711 | continue; |
| 712 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 714 | continue; |
| 715 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 716 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 717 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) || |
| 718 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 719 | continue; |
| 720 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 721 | ret = pattern; |
| 722 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 723 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 724 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 725 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 726 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 727 | |
| 728 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 732 | struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 733 | { |
| 734 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 735 | struct pattern_list *lst; |
| 736 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 737 | struct pattern *ret = NULL; |
| 738 | struct lru64 *lru = NULL; |
| 739 | |
| 740 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 741 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 742 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 743 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 744 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 745 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 746 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 747 | return ret; |
| 748 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 749 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 750 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 751 | list_for_each_entry(lst, &expr->patterns, list) { |
| 752 | pattern = &lst->pat; |
| 753 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 754 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 755 | continue; |
| 756 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 758 | continue; |
| 759 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 760 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 761 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) || |
| 762 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 763 | continue; |
| 764 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 765 | ret = pattern; |
| 766 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 767 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 768 | |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 769 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 770 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 771 | |
| 772 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* Checks that the pattern is included inside the tested string. |
| 776 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 777 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 778 | struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 779 | { |
| 780 | int icase; |
| 781 | char *end; |
| 782 | char *c; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 783 | struct pattern_list *lst; |
| 784 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 785 | struct pattern *ret = NULL; |
| 786 | struct lru64 *lru = NULL; |
| 787 | |
| 788 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 789 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 790 | |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 791 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 792 | pat_lru_tree, expr, expr->ref->revision); |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 793 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 794 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 795 | return ret; |
| 796 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 797 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 798 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 799 | list_for_each_entry(lst, &expr->patterns, list) { |
| 800 | pattern = &lst->pat; |
| 801 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 802 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 803 | continue; |
| 804 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 805 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 806 | continue; |
| 807 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 808 | end = smp->data.u.str.area + smp->data.u.str.data - pattern->len; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 809 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 810 | if (icase) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 811 | for (c = smp->data.u.str.area; c <= end; c++) { |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 812 | if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 813 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 814 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 815 | ret = pattern; |
| 816 | goto leave; |
| 817 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 818 | } |
| 819 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 820 | for (c = smp->data.u.str.area; c <= end; c++) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 821 | if (*c != *pattern->ptr.str) |
| 822 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 823 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 824 | ret = pattern; |
| 825 | goto leave; |
| 826 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 827 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 828 | } |
| 829 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 830 | leave: |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 831 | if (lru) |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 832 | lru64_commit(lru, ret, expr, expr->ref->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 833 | |
| 834 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* This one is used by other real functions. It checks that the pattern is |
| 838 | * included inside the tested string, but enclosed between the specified |
| 839 | * delimiters or at the beginning or end of the string. The delimiters are |
| 840 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 841 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 842 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 843 | static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 844 | { |
| 845 | int may_match, icase; |
| 846 | char *c, *end; |
| 847 | char *ps; |
| 848 | int pl; |
| 849 | |
| 850 | pl = pattern->len; |
| 851 | ps = pattern->ptr.str; |
| 852 | |
| 853 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 854 | pl--; |
| 855 | ps++; |
| 856 | } |
| 857 | |
| 858 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 859 | pl--; |
| 860 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 861 | if (pl > smp->data.u.str.data) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 862 | return PAT_NOMATCH; |
| 863 | |
| 864 | may_match = 1; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 865 | icase = mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 866 | end = smp->data.u.str.area + smp->data.u.str.data - pl; |
| 867 | for (c = smp->data.u.str.area; c <= end; c++) { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 868 | if (is_delimiter(*c, delimiters)) { |
| 869 | may_match = 1; |
| 870 | continue; |
| 871 | } |
| 872 | |
| 873 | if (!may_match) |
| 874 | continue; |
| 875 | |
| 876 | if (icase) { |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 877 | if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) && |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 878 | (strncasecmp(ps, c, pl) == 0) && |
| 879 | (c == end || is_delimiter(c[pl], delimiters))) |
| 880 | return PAT_MATCH; |
| 881 | } else { |
| 882 | if ((*c == *ps) && |
| 883 | (strncmp(ps, c, pl) == 0) && |
| 884 | (c == end || is_delimiter(c[pl], delimiters))) |
| 885 | return PAT_MATCH; |
| 886 | } |
| 887 | may_match = 0; |
| 888 | } |
| 889 | return PAT_NOMATCH; |
| 890 | } |
| 891 | |
| 892 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 893 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 894 | * Delimiters at the beginning or end of the pattern are ignored. |
| 895 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 896 | struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 897 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 898 | struct pattern_list *lst; |
| 899 | struct pattern *pattern; |
| 900 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 901 | list_for_each_entry(lst, &expr->patterns, list) { |
| 902 | pattern = &lst->pat; |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 903 | |
| 904 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 905 | continue; |
| 906 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 907 | if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?'))) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 908 | return pattern; |
| 909 | } |
| 910 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 914 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 915 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 916 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 917 | struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 918 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 919 | struct pattern_list *lst; |
| 920 | struct pattern *pattern; |
| 921 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 922 | list_for_each_entry(lst, &expr->patterns, list) { |
| 923 | pattern = &lst->pat; |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 924 | |
| 925 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 926 | continue; |
| 927 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 928 | if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':'))) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 929 | return pattern; |
| 930 | } |
| 931 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 935 | struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 936 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 937 | struct pattern_list *lst; |
| 938 | struct pattern *pattern; |
| 939 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 940 | list_for_each_entry(lst, &expr->patterns, list) { |
| 941 | pattern = &lst->pat; |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 942 | |
| 943 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 944 | continue; |
| 945 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 946 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) && |
| 947 | (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 948 | return pattern; |
| 949 | } |
| 950 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 954 | struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 955 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 956 | struct pattern_list *lst; |
| 957 | struct pattern *pattern; |
| 958 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 959 | list_for_each_entry(lst, &expr->patterns, list) { |
| 960 | pattern = &lst->pat; |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 961 | |
| 962 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 963 | continue; |
| 964 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 965 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) && |
| 966 | (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 967 | return pattern; |
| 968 | } |
| 969 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 972 | struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 973 | { |
| 974 | unsigned int v4; /* in network byte order */ |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 975 | struct in6_addr tmp6; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 976 | struct in_addr *s; |
| 977 | struct ebmb_node *node; |
| 978 | struct pattern_tree *elt; |
| 979 | struct pattern_list *lst; |
| 980 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 981 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 982 | /* The input sample is IPv4. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 983 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 984 | /* Lookup an IPv4 address in the expression's pattern tree using |
| 985 | * the longest match method. |
| 986 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 987 | s = &smp->data.u.ipv4; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 988 | node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 989 | while (node) { |
| 990 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 991 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 992 | node = ebmb_lookup_shorter(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 993 | continue; |
| 994 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 995 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 996 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 997 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 998 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 999 | static_pattern.type = SMP_T_IPV4; |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1000 | static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1001 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1005 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1006 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 1007 | /* The IPv4 sample don't match the IPv4 tree. Convert the IPv4 |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1008 | * sample address to IPv6 with the mapping method using the ::ffff: |
| 1009 | * prefix, and try to lookup in the IPv6 tree. |
| 1010 | */ |
| 1011 | memset(&tmp6, 0, 10); |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1012 | write_u16(&tmp6.s6_addr[10], htons(0xffff)); |
| 1013 | write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1014 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1015 | while (node) { |
| 1016 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 1017 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 1018 | node = ebmb_lookup_shorter(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1019 | continue; |
| 1020 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1021 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1022 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1023 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1024 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1025 | static_pattern.type = SMP_T_IPV6; |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1026 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1027 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
| 1028 | } |
| 1029 | return &static_pattern; |
| 1030 | } |
| 1031 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1032 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1033 | /* The input sample is IPv6. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1034 | if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1035 | /* Lookup an IPv6 address in the expression's pattern tree using |
| 1036 | * the longest match method. |
| 1037 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1038 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1039 | while (node) { |
| 1040 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 1041 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 1042 | node = ebmb_lookup_shorter(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1043 | continue; |
| 1044 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1045 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1046 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1047 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1048 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1049 | static_pattern.type = SMP_T_IPV6; |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1050 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1051 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1052 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1053 | return &static_pattern; |
| 1054 | } |
| 1055 | |
| 1056 | /* Try to convert 6 to 4 when the start of the ipv6 address match the |
| 1057 | * following forms : |
| 1058 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 1059 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 1060 | * - 2002:ip:v4:: (6to4) |
| 1061 | */ |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1062 | if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 && |
| 1063 | (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 || |
| 1064 | read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) || |
| 1065 | read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) { |
| 1066 | if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0) |
| 1067 | v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1068 | else |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1069 | v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) + |
| 1070 | ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4]))); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1071 | |
| 1072 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 1073 | * match method. |
| 1074 | */ |
| 1075 | node = ebmb_lookup_longest(&expr->pattern_tree, &v4); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1076 | while (node) { |
| 1077 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 1078 | if (elt->ref->gen_id != expr->ref->curr_gen) { |
Willy Tarreau | 51d38a2 | 2022-08-01 11:46:27 +0200 | [diff] [blame] | 1079 | node = ebmb_lookup_shorter(node); |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1080 | continue; |
| 1081 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1082 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1083 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1084 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1085 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1086 | static_pattern.type = SMP_T_IPV4; |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1087 | static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1088 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 1089 | return NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1090 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1091 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1092 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1093 | } |
| 1094 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1095 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1096 | /* Lookup in the list. the list contain only IPv4 patterns */ |
| 1097 | list_for_each_entry(lst, &expr->patterns, list) { |
| 1098 | pattern = &lst->pat; |
| 1099 | |
Willy Tarreau | c93da69 | 2020-10-29 09:41:34 +0100 | [diff] [blame] | 1100 | if (pattern->ref->gen_id != expr->ref->curr_gen) |
| 1101 | continue; |
| 1102 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1103 | /* The input sample is IPv4, use it as is. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1104 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1105 | v4 = smp->data.u.ipv4.s_addr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1106 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1107 | else if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1108 | /* v4 match on a V6 sample. We want to check at least for |
| 1109 | * the following forms : |
| 1110 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 1111 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 1112 | * - 2002:ip:v4:: (6to4) |
| 1113 | */ |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1114 | if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 && |
| 1115 | (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 || |
| 1116 | read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) { |
| 1117 | v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1118 | } |
Willy Tarreau | 296cfd1 | 2020-02-25 09:58:41 +0100 | [diff] [blame] | 1119 | else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) { |
| 1120 | v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) + |
| 1121 | ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4]))); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1122 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1123 | else |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1124 | continue; |
Andreas Seltenreich | f065319 | 2016-03-03 20:08:35 +0100 | [diff] [blame] | 1125 | } else { |
| 1126 | /* impossible */ |
| 1127 | continue; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1128 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1129 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1130 | /* Check if the input sample match the current pattern. */ |
| 1131 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1132 | return pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1133 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1134 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1137 | /* finds the pattern holding <list> from list head <head> and deletes it. |
| 1138 | * This is made for use for pattern removal within an expression. |
| 1139 | */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1140 | static void pat_unlink_from_head(void **head, void **list) |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1141 | { |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1142 | while (*head) { |
| 1143 | if (*head == list) { |
| 1144 | *head = *list; |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1145 | return; |
| 1146 | } |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1147 | head = *head; |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1151 | void free_pattern_tree(struct eb_root *root) |
| 1152 | { |
| 1153 | struct eb_node *node, *next; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1154 | struct pattern_tree *elt; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1155 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1156 | node = eb_first(root); |
| 1157 | while (node) { |
| 1158 | next = eb_next(node); |
| 1159 | eb_delete(node); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1160 | elt = container_of(node, struct pattern_tree, node); |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1161 | pat_unlink_from_head(&elt->ref->tree_head, &elt->from_ref); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1162 | free(elt->data); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1163 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1164 | node = next; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Willy Tarreau | 6d8a689 | 2020-11-02 19:26:02 +0100 | [diff] [blame] | 1168 | void pat_prune_gen(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1169 | { |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1170 | struct pattern_list *pat, *tmp; |
| 1171 | |
| 1172 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1173 | LIST_DELETE(&pat->list); |
Willy Tarreau | 867a8a5 | 2020-11-03 11:22:04 +0100 | [diff] [blame] | 1174 | pat_unlink_from_head(&pat->pat.ref->list_head, &pat->from_ref); |
Willy Tarreau | 6d8a689 | 2020-11-02 19:26:02 +0100 | [diff] [blame] | 1175 | if (pat->pat.sflags & PAT_SF_REGFREE) |
| 1176 | regex_free(pat->pat.ptr.ptr); |
| 1177 | else |
| 1178 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1179 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1180 | free(pat); |
| 1181 | } |
| 1182 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1183 | free_pattern_tree(&expr->pattern_tree); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1184 | free_pattern_tree(&expr->pattern_tree_2); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1185 | LIST_INIT(&expr->patterns); |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1186 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1187 | expr->ref->entry_cnt = 0; |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1190 | /* |
| 1191 | * |
| 1192 | * The following functions are used for the pattern indexation |
| 1193 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1194 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1195 | |
| 1196 | int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1197 | { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1198 | struct pattern_list *patl; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1199 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1200 | /* allocate pattern */ |
| 1201 | patl = calloc(1, sizeof(*patl)); |
| 1202 | if (!patl) { |
| 1203 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1206 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1207 | /* duplicate pattern */ |
| 1208 | memcpy(&patl->pat, pat, sizeof(*pat)); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1209 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1210 | /* chain pattern in the expression */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1211 | LIST_APPEND(&expr->patterns, &patl->list); |
Willy Tarreau | 4bdd0a1 | 2020-11-02 12:10:48 +0100 | [diff] [blame] | 1212 | /* and from the reference */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1213 | patl->from_ref = pat->ref->list_head; |
| 1214 | pat->ref->list_head = &patl->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1215 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1216 | expr->ref->entry_cnt++; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1217 | |
| 1218 | /* that's ok */ |
| 1219 | return 1; |
| 1220 | } |
| 1221 | |
| 1222 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1223 | { |
| 1224 | struct pattern_list *patl; |
| 1225 | |
| 1226 | /* allocate pattern */ |
| 1227 | patl = calloc(1, sizeof(*patl)); |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1228 | if (!patl) { |
| 1229 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1230 | return 0; |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1231 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1232 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1233 | /* duplicate pattern */ |
| 1234 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1235 | patl->pat.ptr.ptr = malloc(patl->pat.len); |
| 1236 | if (!patl->pat.ptr.ptr) { |
| 1237 | free(patl); |
| 1238 | memprintf(err, "out of memory while indexing pattern"); |
| 1239 | return 0; |
| 1240 | } |
| 1241 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1242 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1243 | /* chain pattern in the expression */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1244 | LIST_APPEND(&expr->patterns, &patl->list); |
Willy Tarreau | 4bdd0a1 | 2020-11-02 12:10:48 +0100 | [diff] [blame] | 1245 | /* and from the reference */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1246 | patl->from_ref = pat->ref->list_head; |
| 1247 | pat->ref->list_head = &patl->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1248 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1249 | expr->ref->entry_cnt++; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1250 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1251 | /* that's ok */ |
| 1252 | return 1; |
| 1253 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1254 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1255 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1256 | { |
| 1257 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1258 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1259 | /* allocate pattern */ |
| 1260 | patl = calloc(1, sizeof(*patl)); |
| 1261 | if (!patl) { |
| 1262 | memprintf(err, "out of memory while indexing pattern"); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | /* duplicate pattern */ |
| 1267 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1268 | patl->pat.ptr.str = malloc(patl->pat.len + 1); |
| 1269 | if (!patl->pat.ptr.str) { |
| 1270 | free(patl); |
| 1271 | memprintf(err, "out of memory while indexing pattern"); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
| 1275 | patl->pat.ptr.str[patl->pat.len] = '\0'; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1276 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1277 | /* chain pattern in the expression */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1278 | LIST_APPEND(&expr->patterns, &patl->list); |
Willy Tarreau | 4bdd0a1 | 2020-11-02 12:10:48 +0100 | [diff] [blame] | 1279 | /* and from the reference */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1280 | patl->from_ref = pat->ref->list_head; |
| 1281 | pat->ref->list_head = &patl->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1282 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1283 | expr->ref->entry_cnt++; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1284 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1285 | /* that's ok */ |
| 1286 | return 1; |
| 1287 | } |
| 1288 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1289 | int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err) |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1290 | { |
| 1291 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1292 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1293 | /* allocate pattern */ |
| 1294 | patl = calloc(1, sizeof(*patl)); |
| 1295 | if (!patl) { |
| 1296 | memprintf(err, "out of memory while indexing pattern"); |
| 1297 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1298 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1299 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1300 | /* duplicate pattern */ |
| 1301 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1302 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1303 | /* compile regex */ |
Willy Tarreau | 9b5c8bb | 2020-11-02 19:16:23 +0100 | [diff] [blame] | 1304 | patl->pat.sflags |= PAT_SF_REGFREE; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1305 | if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE), |
| 1306 | cap, err))) { |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1307 | free(patl); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1311 | /* chain pattern in the expression */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1312 | LIST_APPEND(&expr->patterns, &patl->list); |
Willy Tarreau | 4bdd0a1 | 2020-11-02 12:10:48 +0100 | [diff] [blame] | 1313 | /* and from the reference */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1314 | patl->from_ref = pat->ref->list_head; |
| 1315 | pat->ref->list_head = &patl->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1316 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1317 | expr->ref->entry_cnt++; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1318 | |
| 1319 | /* that's ok */ |
| 1320 | return 1; |
| 1321 | } |
| 1322 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1323 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1324 | { |
| 1325 | return pat_idx_list_reg_cap(expr, pat, 0, err); |
| 1326 | } |
| 1327 | |
| 1328 | int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1329 | { |
| 1330 | return pat_idx_list_reg_cap(expr, pat, 1, err); |
| 1331 | } |
| 1332 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1333 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1334 | { |
| 1335 | unsigned int mask; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1336 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1337 | |
| 1338 | /* Only IPv4 can be indexed */ |
| 1339 | if (pat->type == SMP_T_IPV4) { |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1340 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 1341 | * insert the network into the tree. A continuous mask has only |
| 1342 | * ones on the left. This means that this mask + its lower bit |
| 1343 | * added once again is null. |
| 1344 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1345 | mask = ntohl(pat->val.ipv4.mask.s_addr); |
| 1346 | if (mask + (mask & -mask) == 0) { |
| 1347 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1348 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1349 | /* node memory allocation */ |
| 1350 | node = calloc(1, sizeof(*node) + 4); |
| 1351 | if (!node) { |
| 1352 | memprintf(err, "out of memory while loading pattern"); |
| 1353 | return 0; |
| 1354 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1355 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1356 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1357 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1358 | node->ref = pat->ref; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1359 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1360 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1361 | memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */ |
| 1362 | node->node.node.pfx = mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1363 | |
| 1364 | /* Insert the entry. */ |
| 1365 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4); |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1366 | node->from_ref = pat->ref->tree_head; |
| 1367 | pat->ref->tree_head = &node->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1368 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1369 | expr->ref->entry_cnt++; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1370 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1371 | /* that's ok */ |
| 1372 | return 1; |
| 1373 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1374 | else { |
| 1375 | /* If the mask is not contiguous, just add the pattern to the list */ |
| 1376 | return pat_idx_list_val(expr, pat, err); |
| 1377 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1378 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1379 | else if (pat->type == SMP_T_IPV6) { |
| 1380 | /* IPv6 also can be indexed */ |
| 1381 | node = calloc(1, sizeof(*node) + 16); |
| 1382 | if (!node) { |
| 1383 | memprintf(err, "out of memory while loading pattern"); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1388 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1389 | node->ref = pat->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1392 | memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */ |
| 1393 | node->node.node.pfx = pat->val.ipv6.mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1394 | |
| 1395 | /* Insert the entry. */ |
| 1396 | ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16); |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1397 | node->from_ref = pat->ref->tree_head; |
| 1398 | pat->ref->tree_head = &node->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1399 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1400 | expr->ref->entry_cnt++; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* that's ok */ |
| 1403 | return 1; |
| 1404 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1405 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1406 | return 0; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1410 | { |
| 1411 | int len; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1412 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1413 | |
| 1414 | /* Only string can be indexed */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1415 | if (pat->type != SMP_T_STR) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1416 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1417 | smp_to_type[pat->type]); |
| 1418 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1419 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1420 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1421 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1422 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1423 | return pat_idx_list_str(expr, pat, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1424 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1425 | /* Process the key len */ |
| 1426 | len = strlen(pat->ptr.str) + 1; |
| 1427 | |
| 1428 | /* node memory allocation */ |
| 1429 | node = calloc(1, sizeof(*node) + len); |
| 1430 | if (!node) { |
| 1431 | memprintf(err, "out of memory while loading pattern"); |
| 1432 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1435 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1436 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1437 | node->ref = pat->ref; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1438 | |
| 1439 | /* copy the string */ |
| 1440 | memcpy(node->node.key, pat->ptr.str, len); |
| 1441 | |
| 1442 | /* index the new node */ |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1443 | ebst_insert(&expr->pattern_tree, &node->node); |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1444 | node->from_ref = pat->ref->tree_head; |
| 1445 | pat->ref->tree_head = &node->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1446 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1447 | expr->ref->entry_cnt++; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1448 | |
| 1449 | /* that's ok */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1450 | return 1; |
| 1451 | } |
| 1452 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1453 | int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1454 | { |
| 1455 | int len; |
| 1456 | struct pattern_tree *node; |
| 1457 | |
| 1458 | /* Only string can be indexed */ |
| 1459 | if (pat->type != SMP_T_STR) { |
| 1460 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1461 | smp_to_type[pat->type]); |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1466 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
| 1467 | return pat_idx_list_str(expr, pat, err); |
| 1468 | |
| 1469 | /* Process the key len */ |
| 1470 | len = strlen(pat->ptr.str); |
| 1471 | |
| 1472 | /* node memory allocation */ |
| 1473 | node = calloc(1, sizeof(*node) + len + 1); |
| 1474 | if (!node) { |
| 1475 | memprintf(err, "out of memory while loading pattern"); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1480 | node->data = pat->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1481 | node->ref = pat->ref; |
| 1482 | |
| 1483 | /* copy the string and the trailing zero */ |
| 1484 | memcpy(node->node.key, pat->ptr.str, len + 1); |
| 1485 | node->node.node.pfx = len * 8; |
| 1486 | |
| 1487 | /* index the new node */ |
| 1488 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, len); |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1489 | node->from_ref = pat->ref->tree_head; |
| 1490 | pat->ref->tree_head = &node->from_ref; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1491 | expr->ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1492 | expr->ref->entry_cnt++; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1493 | |
| 1494 | /* that's ok */ |
| 1495 | return 1; |
| 1496 | } |
| 1497 | |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1498 | /* Deletes all patterns from reference <elt>. Note that all of their |
Willy Tarreau | 78777ea | 2020-11-02 13:55:22 +0100 | [diff] [blame] | 1499 | * expressions must be locked, and the pattern lock must be held as well. |
| 1500 | */ |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1501 | void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1502 | { |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1503 | struct pattern_tree *tree; |
| 1504 | struct pattern_list *pat; |
| 1505 | void **node; |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1506 | |
| 1507 | /* delete all known tree nodes. They are all allocated inline */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1508 | for (node = elt->tree_head; node;) { |
| 1509 | tree = container_of(node, struct pattern_tree, from_ref); |
| 1510 | node = *node; |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1511 | BUG_ON(tree->ref != elt); |
| 1512 | |
| 1513 | ebmb_delete(&tree->node); |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1514 | free(tree->data); |
| 1515 | free(tree); |
| 1516 | } |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1517 | |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1518 | /* delete all list nodes and free their pattern entries (str/reg) */ |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1519 | for (node = elt->list_head; node;) { |
| 1520 | pat = container_of(node, struct pattern_list, from_ref); |
| 1521 | node = *node; |
Willy Tarreau | 78777ea | 2020-11-02 13:55:22 +0100 | [diff] [blame] | 1522 | BUG_ON(pat->pat.ref != elt); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1523 | |
| 1524 | /* Delete and free entry. */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1525 | LIST_DELETE(&pat->list); |
Willy Tarreau | 6d8a689 | 2020-11-02 19:26:02 +0100 | [diff] [blame] | 1526 | if (pat->pat.sflags & PAT_SF_REGFREE) |
| 1527 | regex_free(pat->pat.ptr.reg); |
| 1528 | else |
| 1529 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1530 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1531 | free(pat); |
| 1532 | } |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1533 | |
Willy Tarreau | f1c0892 | 2020-11-02 19:53:16 +0100 | [diff] [blame] | 1534 | /* update revision number to refresh the cache */ |
| 1535 | ref->revision = rdtsc(); |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1536 | ref->entry_cnt--; |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1537 | elt->tree_head = NULL; |
| 1538 | elt->list_head = NULL; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1541 | void pattern_init_expr(struct pattern_expr *expr) |
| 1542 | { |
| 1543 | LIST_INIT(&expr->patterns); |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1544 | expr->pattern_tree = EB_ROOT; |
| 1545 | expr->pattern_tree_2 = EB_ROOT; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | void pattern_init_head(struct pattern_head *head) |
| 1549 | { |
| 1550 | LIST_INIT(&head->head); |
| 1551 | } |
| 1552 | |
| 1553 | /* The following functions are relative to the management of the reference |
| 1554 | * lists. These lists are used to store the original pattern and associated |
| 1555 | * value as string form. |
| 1556 | * |
| 1557 | * This is used with modifiable ACL and MAPS |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1558 | * |
| 1559 | * The pattern reference are stored with two identifiers: the unique_id and |
| 1560 | * the reference. |
| 1561 | * |
| 1562 | * The reference identify a file. Each file with the same name point to the |
| 1563 | * same reference. We can register many times one file. If the file is modified, |
| 1564 | * all his dependencies are also modified. The reference can be used with map or |
| 1565 | * acl. |
| 1566 | * |
| 1567 | * The unique_id identify inline acl. The unique id is unique for each acl. |
| 1568 | * You cannot force the same id in the configuration file, because this repoort |
| 1569 | * an error. |
| 1570 | * |
| 1571 | * A particular case appears if the filename is a number. In this case, the |
| 1572 | * unique_id is set with the number represented by the filename and the |
| 1573 | * reference is also set. This method prevent double unique_id. |
| 1574 | * |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1575 | */ |
| 1576 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1577 | /* This function looks up a reference by name. If the reference is found, a |
| 1578 | * pointer to the struct pat_ref is returned, otherwise NULL is returned. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1579 | */ |
| 1580 | struct pat_ref *pat_ref_lookup(const char *reference) |
| 1581 | { |
| 1582 | struct pat_ref *ref; |
| 1583 | |
| 1584 | list_for_each_entry(ref, &pattern_reference, list) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1585 | if (ref->reference && strcmp(reference, ref->reference) == 0) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1586 | return ref; |
| 1587 | return NULL; |
| 1588 | } |
| 1589 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1590 | /* This function looks up a reference's unique id. If the reference is found, a |
| 1591 | * pointer to the struct pat_ref is returned, otherwise NULL is returned. |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1592 | */ |
| 1593 | struct pat_ref *pat_ref_lookupid(int unique_id) |
| 1594 | { |
| 1595 | struct pat_ref *ref; |
| 1596 | |
| 1597 | list_for_each_entry(ref, &pattern_reference, list) |
| 1598 | if (ref->unique_id == unique_id) |
| 1599 | return ref; |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1603 | /* This function removes from the pattern reference <ref> all the patterns |
| 1604 | * attached to the reference element <elt>, and the element itself. The |
| 1605 | * reference must be locked. |
| 1606 | */ |
| 1607 | void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt) |
| 1608 | { |
| 1609 | struct pattern_expr *expr; |
| 1610 | struct bref *bref, *back; |
| 1611 | |
| 1612 | /* |
| 1613 | * we have to unlink all watchers from this reference pattern. We must |
| 1614 | * not relink them if this elt was the last one in the list. |
| 1615 | */ |
| 1616 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1617 | LIST_DELETE(&bref->users); |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1618 | LIST_INIT(&bref->users); |
| 1619 | if (elt->list.n != &ref->head) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1620 | LIST_APPEND(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1621 | bref->ref = elt->list.n; |
| 1622 | } |
| 1623 | |
| 1624 | /* delete all entries from all expressions for this pattern */ |
| 1625 | list_for_each_entry(expr, &ref->pat, list) |
| 1626 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
| 1627 | |
| 1628 | pat_delete_gen(ref, elt); |
| 1629 | |
| 1630 | list_for_each_entry(expr, &ref->pat, list) |
| 1631 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
| 1632 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1633 | LIST_DELETE(&elt->list); |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1634 | free(elt->sample); |
| 1635 | free(elt->pattern); |
| 1636 | free(elt); |
| 1637 | } |
| 1638 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1639 | /* This function removes all the patterns matching the pointer <refelt> from |
| 1640 | * the reference and from each expr member of this reference. This function |
| 1641 | * returns 1 if the entry was found and deleted, otherwise zero. |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1642 | */ |
| 1643 | int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt) |
| 1644 | { |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1645 | struct pat_ref_elt *elt, *safe; |
| 1646 | |
| 1647 | /* delete pattern from reference */ |
| 1648 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1649 | if (elt == refelt) { |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1650 | pat_ref_delete_by_ptr(ref, elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1651 | return 1; |
| 1652 | } |
| 1653 | } |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1657 | /* This function removes all patterns matching <key> from the reference |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 1658 | * and from each expr member of the reference. This function returns 1 |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1659 | * if the deletion is done and returns 0 is the entry is not found. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1660 | */ |
| 1661 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1662 | { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1663 | struct pat_ref_elt *elt, *safe; |
| 1664 | int found = 0; |
| 1665 | |
| 1666 | /* delete pattern from reference */ |
| 1667 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1668 | if (strcmp(key, elt->pattern) == 0) { |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1669 | pat_ref_delete_by_ptr(ref, elt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1670 | found = 1; |
| 1671 | } |
| 1672 | } |
| 1673 | |
Willy Tarreau | 1fd52f7 | 2020-11-02 17:30:17 +0100 | [diff] [blame] | 1674 | return found; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 1677 | /* |
| 1678 | * find and return an element <elt> matching <key> in a reference <ref> |
| 1679 | * return NULL if not found |
| 1680 | */ |
| 1681 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key) |
| 1682 | { |
| 1683 | struct pat_ref_elt *elt; |
| 1684 | |
| 1685 | list_for_each_entry(elt, &ref->head, list) { |
| 1686 | if (strcmp(key, elt->pattern) == 0) |
| 1687 | return elt; |
| 1688 | } |
| 1689 | |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | |
| 1693 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1694 | /* This function modifies the sample of pat_ref_elt <elt> in all expressions |
| 1695 | * found under <ref> to become <value>. It is assumed that the caller has |
| 1696 | * already verified that <elt> belongs to <ref>. |
| 1697 | */ |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1698 | static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt, |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1699 | const char *value, char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1700 | { |
| 1701 | struct pattern_expr *expr; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1702 | struct sample_data **data; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1703 | char *sample; |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 1704 | struct sample_data test; |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1705 | |
| 1706 | /* Try all needed converters. */ |
| 1707 | list_for_each_entry(expr, &ref->pat, list) { |
| 1708 | if (!expr->pat_head->parse_smp) |
| 1709 | continue; |
| 1710 | |
| 1711 | if (!expr->pat_head->parse_smp(value, &test)) { |
| 1712 | memprintf(err, "unable to parse '%s'", value); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1716 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1717 | /* Modify pattern from reference. */ |
| 1718 | sample = strdup(value); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1719 | if (!sample) { |
| 1720 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1721 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1722 | } |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1723 | /* Load sample in each reference. All the conversions are tested |
| 1724 | * below, normally these calls don't fail. |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1725 | */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1726 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1727 | if (!expr->pat_head->parse_smp) |
| 1728 | continue; |
| 1729 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1730 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1731 | data = pattern_find_smp(expr, elt); |
| 1732 | if (data && *data && !expr->pat_head->parse_smp(sample, *data)) |
| 1733 | *data = NULL; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1734 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 1737 | /* free old sample only when all exprs are updated */ |
| 1738 | free(elt->sample); |
| 1739 | elt->sample = sample; |
| 1740 | |
| 1741 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1742 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1743 | } |
| 1744 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1745 | /* This function modifies the sample of pat_ref_elt <refelt> in all expressions |
| 1746 | * found under <ref> to become <value>, after checking that <refelt> really |
| 1747 | * belongs to <ref>. |
| 1748 | */ |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1749 | int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err) |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1750 | { |
| 1751 | struct pat_ref_elt *elt; |
| 1752 | |
| 1753 | /* Look for pattern in the reference. */ |
| 1754 | list_for_each_entry(elt, &ref->head, list) { |
| 1755 | if (elt == refelt) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1756 | if (!pat_ref_set_elt(ref, elt, value, err)) |
| 1757 | return 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1758 | return 1; |
| 1759 | } |
| 1760 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1761 | |
| 1762 | memprintf(err, "key or pattern not found"); |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1763 | return 0; |
| 1764 | } |
| 1765 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1766 | /* This function modifies to <value> the sample of all patterns matching <key> |
| 1767 | * under <ref>. |
| 1768 | */ |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1769 | int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err) |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1770 | { |
| 1771 | struct pat_ref_elt *elt; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1772 | int found = 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1773 | |
| 1774 | /* Look for pattern in the reference. */ |
| 1775 | list_for_each_entry(elt, &ref->head, list) { |
| 1776 | if (strcmp(key, elt->pattern) == 0) { |
Valentine Krasnobaeva | 697d9f5 | 2024-08-12 15:32:00 +0200 | [diff] [blame] | 1777 | char *tmp_err = NULL; |
| 1778 | |
| 1779 | if (!pat_ref_set_elt(ref, elt, value, &tmp_err)) { |
Willy Tarreau | f8f1fc7 | 2024-09-10 08:55:29 +0200 | [diff] [blame] | 1780 | if (err) |
| 1781 | *err = tmp_err; |
| 1782 | else |
| 1783 | ha_free(&tmp_err); |
Valentine Krasnobaeva | d9bae70 | 2024-08-12 19:21:00 +0200 | [diff] [blame] | 1784 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1785 | } |
| 1786 | found = 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1787 | } |
| 1788 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1789 | |
| 1790 | if (!found) { |
| 1791 | memprintf(err, "entry not found"); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | return 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1795 | } |
| 1796 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 1797 | /* This function creates a new reference. <ref> is the reference name. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1798 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1799 | * be unique. The user must check the reference with "pat_ref_lookup()" |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1800 | * before calling this function. If the function fails, it returns NULL, |
| 1801 | * otherwise it returns the new struct pat_ref. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1802 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1803 | struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1804 | { |
| 1805 | struct pat_ref *ref; |
| 1806 | |
Willy Tarreau | 8135d9b | 2020-10-30 15:35:11 +0100 | [diff] [blame] | 1807 | ref = calloc(1, sizeof(*ref)); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1808 | if (!ref) |
| 1809 | return NULL; |
| 1810 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1811 | if (display) { |
| 1812 | ref->display = strdup(display); |
| 1813 | if (!ref->display) { |
| 1814 | free(ref); |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | } |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1818 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1819 | ref->reference = strdup(reference); |
| 1820 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1821 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1822 | free(ref); |
| 1823 | return NULL; |
| 1824 | } |
| 1825 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1826 | ref->flags = flags; |
| 1827 | ref->unique_id = -1; |
Willy Tarreau | 3ee0de1 | 2020-11-02 15:26:51 +0100 | [diff] [blame] | 1828 | ref->revision = 0; |
Dragan Dosen | a75eea7 | 2021-05-21 16:59:15 +0200 | [diff] [blame] | 1829 | ref->entry_cnt = 0; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1830 | |
| 1831 | LIST_INIT(&ref->head); |
| 1832 | LIST_INIT(&ref->pat); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1833 | HA_SPIN_INIT(&ref->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1834 | LIST_APPEND(&pattern_reference, &ref->list); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1835 | |
| 1836 | return ref; |
| 1837 | } |
| 1838 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1839 | /* This function creates a new reference. <unique_id> is the unique id. If |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1840 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1841 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1842 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1843 | * or pat_ref_lookupid before calling this function. If the function |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1844 | * fails, it returns NULL, otherwise it returns the new struct pat_ref. |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1845 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1846 | struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1847 | { |
| 1848 | struct pat_ref *ref; |
| 1849 | |
Willy Tarreau | 8135d9b | 2020-10-30 15:35:11 +0100 | [diff] [blame] | 1850 | ref = calloc(1, sizeof(*ref)); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1851 | if (!ref) |
| 1852 | return NULL; |
| 1853 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1854 | if (display) { |
| 1855 | ref->display = strdup(display); |
| 1856 | if (!ref->display) { |
| 1857 | free(ref); |
| 1858 | return NULL; |
| 1859 | } |
| 1860 | } |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1861 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1862 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1863 | ref->flags = flags; |
Willy Tarreau | 2994774 | 2020-10-28 11:43:49 +0100 | [diff] [blame] | 1864 | ref->curr_gen = 0; |
| 1865 | ref->next_gen = 0; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1866 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1867 | LIST_INIT(&ref->head); |
| 1868 | LIST_INIT(&ref->pat); |
Aurélien Nephtali | 564d15a | 2018-04-19 16:56:07 +0200 | [diff] [blame] | 1869 | HA_SPIN_INIT(&ref->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1870 | LIST_APPEND(&pattern_reference, &ref->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1871 | |
| 1872 | return ref; |
| 1873 | } |
| 1874 | |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1875 | /* This function adds entry to <ref>. It can fail on memory error. It returns |
| 1876 | * the newly added element on success, or NULL on failure. The PATREF_LOCK on |
Willy Tarreau | 2994774 | 2020-10-28 11:43:49 +0100 | [diff] [blame] | 1877 | * <ref> must be held. It sets the newly created pattern's generation number |
| 1878 | * to the same value as the reference's. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1879 | */ |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1880 | struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1881 | { |
| 1882 | struct pat_ref_elt *elt; |
| 1883 | |
Willy Tarreau | 8135d9b | 2020-10-30 15:35:11 +0100 | [diff] [blame] | 1884 | elt = calloc(1, sizeof(*elt)); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1885 | if (!elt) |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1886 | goto fail; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1887 | |
Willy Tarreau | 2994774 | 2020-10-28 11:43:49 +0100 | [diff] [blame] | 1888 | elt->gen_id = ref->curr_gen; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1889 | elt->line = line; |
| 1890 | |
| 1891 | elt->pattern = strdup(pattern); |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1892 | if (!elt->pattern) |
| 1893 | goto fail; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1894 | |
| 1895 | if (sample) { |
| 1896 | elt->sample = strdup(sample); |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1897 | if (!elt->sample) |
| 1898 | goto fail; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1899 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1900 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1901 | LIST_INIT(&elt->back_refs); |
Willy Tarreau | 38d4199 | 2020-11-03 14:50:29 +0100 | [diff] [blame] | 1902 | elt->list_head = NULL; |
| 1903 | elt->tree_head = NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1904 | LIST_APPEND(&ref->head, &elt->list); |
Willy Tarreau | f4edb72 | 2020-10-28 10:52:46 +0100 | [diff] [blame] | 1905 | return elt; |
| 1906 | fail: |
| 1907 | if (elt) |
| 1908 | free(elt->pattern); |
| 1909 | free(elt); |
| 1910 | return NULL; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 1913 | /* This function creates sample found in <elt>, parses the pattern also |
| 1914 | * found in <elt> and inserts it in <expr>. The function copies <patflags> |
| 1915 | * into <expr>. If the function fails, it returns 0 and <err> is filled. |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1916 | * In success case, the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1917 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1918 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1919 | int patflags, char **err) |
| 1920 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1921 | struct sample_data *data; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1922 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1923 | |
| 1924 | /* Create sample */ |
| 1925 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1926 | /* New sample. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1927 | data = malloc(sizeof(*data)); |
| 1928 | if (!data) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1929 | return 0; |
| 1930 | |
| 1931 | /* Parse value. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1932 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1933 | memprintf(err, "unable to parse '%s'", elt->sample); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1934 | free(data); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
| 1937 | |
| 1938 | } |
| 1939 | else |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1940 | data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1941 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1942 | /* initialise pattern */ |
| 1943 | memset(&pattern, 0, sizeof(pattern)); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1944 | pattern.data = data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1945 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* parse pattern */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1948 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1949 | free(data); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1950 | return 0; |
| 1951 | } |
| 1952 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1953 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1954 | /* index pattern */ |
| 1955 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1956 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1957 | free(data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1958 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1959 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1960 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1961 | |
| 1962 | return 1; |
| 1963 | } |
| 1964 | |
Willy Tarreau | 0439e5e | 2020-10-28 18:45:45 +0100 | [diff] [blame] | 1965 | /* This function tries to commit entry <elt> into <ref>. The new entry must |
| 1966 | * have already been inserted using pat_ref_append(), and its generation number |
| 1967 | * may have been adjusted as it will not be changed. <err> must point to a NULL |
| 1968 | * pointer. The PATREF lock on <ref> must be held. All the pattern_expr for |
| 1969 | * this reference will be updated (parsing, indexing). On success, non-zero is |
| 1970 | * returned. On failure, all the operation is rolled back (the element is |
| 1971 | * deleted from all expressions and is freed), zero is returned and the error |
| 1972 | * pointer <err> may have been updated (and the caller must free it). Failure |
| 1973 | * causes include memory allocation, parsing error or indexing error. |
| 1974 | */ |
Willy Tarreau | dc2410d | 2021-01-15 14:11:59 +0100 | [diff] [blame] | 1975 | int pat_ref_commit_elt(struct pat_ref *ref, struct pat_ref_elt *elt, char **err) |
Willy Tarreau | 0439e5e | 2020-10-28 18:45:45 +0100 | [diff] [blame] | 1976 | { |
| 1977 | struct pattern_expr *expr; |
| 1978 | |
| 1979 | list_for_each_entry(expr, &ref->pat, list) { |
| 1980 | if (!pat_ref_push(elt, expr, 0, err)) { |
| 1981 | pat_ref_delete_by_ptr(ref, elt); |
| 1982 | return 0; |
| 1983 | } |
| 1984 | } |
| 1985 | return 1; |
| 1986 | } |
| 1987 | |
Willy Tarreau | 1a6857b | 2020-10-29 09:21:43 +0100 | [diff] [blame] | 1988 | /* Loads <pattern>:<sample> into <ref> for generation <gen>. <sample> may be |
| 1989 | * NULL if none exists (e.g. ACL). If not needed, the generation number should |
| 1990 | * be set to ref->curr_gen. The error pointer must initially point to NULL. The |
| 1991 | * new entry will be propagated to all use places, involving allocation, parsing |
| 1992 | * and indexing. On error (parsing, allocation), the operation will be rolled |
| 1993 | * back, an error may be reported, and NULL will be reported. On success, the |
| 1994 | * freshly allocated element will be returned. The PATREF lock on <ref> must be |
| 1995 | * held during the operation. |
| 1996 | */ |
| 1997 | struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen, |
| 1998 | const char *pattern, const char *sample, |
| 1999 | int line, char **err) |
| 2000 | { |
| 2001 | struct pat_ref_elt *elt; |
| 2002 | |
| 2003 | elt = pat_ref_append(ref, pattern, sample, line); |
| 2004 | if (elt) { |
| 2005 | elt->gen_id = gen; |
Willy Tarreau | dc2410d | 2021-01-15 14:11:59 +0100 | [diff] [blame] | 2006 | if (!pat_ref_commit_elt(ref, elt, err)) |
Willy Tarreau | 1a6857b | 2020-10-29 09:21:43 +0100 | [diff] [blame] | 2007 | elt = NULL; |
| 2008 | } else |
| 2009 | memprintf(err, "out of memory error"); |
| 2010 | |
| 2011 | return elt; |
| 2012 | } |
| 2013 | |
Willy Tarreau | 6a17407 | 2020-10-28 10:58:05 +0100 | [diff] [blame] | 2014 | /* This function adds entry to <ref>. It can fail on memory error. The new |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 2015 | * entry is added at all the pattern_expr registered in this reference. The |
Willy Tarreau | 6a17407 | 2020-10-28 10:58:05 +0100 | [diff] [blame] | 2016 | * function stops on the first error encountered. It returns 0 and <err> is |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 2017 | * filled. If an error is encountered, the complete add operation is cancelled. |
| 2018 | * If the insertion is a success the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2019 | */ |
| 2020 | int pat_ref_add(struct pat_ref *ref, |
| 2021 | const char *pattern, const char *sample, |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2022 | char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2023 | { |
Willy Tarreau | 1a6857b | 2020-10-29 09:21:43 +0100 | [diff] [blame] | 2024 | return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
Willy Tarreau | a13afe6 | 2021-04-30 13:19:37 +0200 | [diff] [blame] | 2027 | /* This function purges all elements from <ref> whose generation is included in |
| 2028 | * the range of <from> to <to> (inclusive), taking wrapping into consideration. |
| 2029 | * It will not purge more than <budget> entries at once, in order to remain |
| 2030 | * responsive. If budget is negative, no limit is applied. |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2031 | * The caller must already hold the PATREF_LOCK on <ref>. The function will |
| 2032 | * take the PATEXP_LOCK on all expressions of the pattern as needed. It returns |
| 2033 | * non-zero on completion, or zero if it had to stop before the end after |
| 2034 | * <budget> was depleted. |
| 2035 | */ |
Willy Tarreau | a13afe6 | 2021-04-30 13:19:37 +0200 | [diff] [blame] | 2036 | int pat_ref_purge_range(struct pat_ref *ref, uint from, uint to, int budget) |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2037 | { |
| 2038 | struct pat_ref_elt *elt, *elt_bck; |
| 2039 | struct bref *bref, *bref_bck; |
| 2040 | struct pattern_expr *expr; |
| 2041 | int done; |
| 2042 | |
| 2043 | list_for_each_entry(expr, &ref->pat, list) |
| 2044 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
| 2045 | |
| 2046 | /* all expr are locked, we can safely remove all pat_ref */ |
| 2047 | |
| 2048 | /* assume completion for e.g. empty lists */ |
| 2049 | done = 1; |
| 2050 | list_for_each_entry_safe(elt, elt_bck, &ref->head, list) { |
Willy Tarreau | a13afe6 | 2021-04-30 13:19:37 +0200 | [diff] [blame] | 2051 | if (elt->gen_id - from > to - from) |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2052 | continue; |
| 2053 | |
| 2054 | if (budget >= 0 && !budget--) { |
| 2055 | done = 0; |
| 2056 | break; |
| 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * we have to unlink all watchers from this reference pattern. We must |
| 2061 | * not relink them if this elt was the last one in the list. |
| 2062 | */ |
| 2063 | list_for_each_entry_safe(bref, bref_bck, &elt->back_refs, users) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2064 | LIST_DELETE(&bref->users); |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2065 | LIST_INIT(&bref->users); |
| 2066 | if (elt->list.n != &ref->head) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2067 | LIST_APPEND(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2068 | bref->ref = elt->list.n; |
| 2069 | } |
| 2070 | |
| 2071 | /* delete the storage for all representations of this pattern. */ |
| 2072 | pat_delete_gen(ref, elt); |
| 2073 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2074 | LIST_DELETE(&elt->list); |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2075 | free(elt->pattern); |
| 2076 | free(elt->sample); |
| 2077 | free(elt); |
| 2078 | } |
| 2079 | |
| 2080 | list_for_each_entry(expr, &ref->pat, list) |
| 2081 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
| 2082 | |
Willy Tarreau | 94b9abe | 2020-10-28 18:23:49 +0100 | [diff] [blame] | 2083 | return done; |
| 2084 | } |
| 2085 | |
Willy Tarreau | ae83e63 | 2020-11-03 10:37:31 +0100 | [diff] [blame] | 2086 | /* This function prunes all entries of <ref> and all their associated |
| 2087 | * pattern_expr. It may return before the end of the list is reached, |
| 2088 | * returning 0, to yield, indicating to the caller that it must call it again. |
| 2089 | * until it returns non-zero. All patterns are purged, both current ones and |
| 2090 | * future or incomplete ones. This is used by "clear map" or "clear acl". |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2091 | */ |
Willy Tarreau | d1d005d | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2092 | int pat_ref_prune(struct pat_ref *ref) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2093 | { |
Willy Tarreau | a13afe6 | 2021-04-30 13:19:37 +0200 | [diff] [blame] | 2094 | return pat_ref_purge_range(ref, 0, ~0, 100); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 2097 | /* This function looks up any existing reference <ref> in pattern_head <head>, and |
| 2098 | * returns the associated pattern_expr pointer if found, otherwise NULL. |
| 2099 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2100 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 2101 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2102 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2103 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2104 | list_for_each_entry(expr, &head->head, list) |
| 2105 | if (expr->expr->ref == ref) |
| 2106 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2107 | return NULL; |
| 2108 | } |
| 2109 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2110 | /* This function creates new pattern_expr associated to the reference <ref>. |
| 2111 | * <ref> can be NULL. If an error occurs, the function returns NULL and |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2112 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 2113 | * with <head> and <ref>. |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2114 | * |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2115 | * The returned value can be an already filled pattern list, in this case the |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2116 | * flag <reuse> is set. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2117 | */ |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2118 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2119 | int patflags, char **err, int *reuse) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2120 | { |
| 2121 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2122 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2123 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2124 | if (reuse) |
| 2125 | *reuse = 0; |
| 2126 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2127 | /* Memory and initialization of the chain element. */ |
Willy Tarreau | 8135d9b | 2020-10-30 15:35:11 +0100 | [diff] [blame] | 2128 | list = calloc(1, sizeof(*list)); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2129 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2130 | memprintf(err, "out of memory"); |
| 2131 | return NULL; |
| 2132 | } |
| 2133 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2134 | /* Look for existing similar expr. No that only the index, parse and |
| 2135 | * parse_smp function must be identical for having similar pattern. |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2136 | * The other function depends of these first. |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2137 | */ |
| 2138 | if (ref) { |
| 2139 | list_for_each_entry(expr, &ref->pat, list) |
| 2140 | if (expr->pat_head->index == head->index && |
| 2141 | expr->pat_head->parse == head->parse && |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2142 | expr->pat_head->parse_smp == head->parse_smp && |
| 2143 | expr->mflags == patflags) |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2144 | break; |
| 2145 | if (&expr->list == &ref->pat) |
| 2146 | expr = NULL; |
| 2147 | } |
| 2148 | else |
| 2149 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2150 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2151 | /* If no similar expr was found, we create new expr. */ |
| 2152 | if (!expr) { |
| 2153 | /* Get a lot of memory for the expr struct. */ |
Willy Tarreau | 8135d9b | 2020-10-30 15:35:11 +0100 | [diff] [blame] | 2154 | expr = calloc(1, sizeof(*expr)); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2155 | if (!expr) { |
Andreas Seltenreich | e6e22e8 | 2016-03-03 20:20:23 +0100 | [diff] [blame] | 2156 | free(list); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2157 | memprintf(err, "out of memory"); |
| 2158 | return NULL; |
| 2159 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2160 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2161 | /* Initialize this new expr. */ |
| 2162 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2163 | |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2164 | /* Copy the pattern matching and indexing flags. */ |
| 2165 | expr->mflags = patflags; |
| 2166 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2167 | /* This new pattern expression reference one of his heads. */ |
| 2168 | expr->pat_head = head; |
| 2169 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2170 | /* Link with ref, or to self to facilitate LIST_DELETE() */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2171 | if (ref) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2172 | LIST_APPEND(&ref->pat, &expr->list); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2173 | else |
| 2174 | LIST_INIT(&expr->list); |
| 2175 | |
| 2176 | expr->ref = ref; |
| 2177 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2178 | HA_RWLOCK_INIT(&expr->lock); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2179 | } |
| 2180 | else { |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2181 | if (reuse) |
| 2182 | *reuse = 1; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Aurelien DARRAGON | 2f33952 | 2024-09-09 14:59:19 +0200 | [diff] [blame] | 2185 | HA_ATOMIC_INC(&expr->refcount); |
| 2186 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2187 | /* The new list element reference the pattern_expr. */ |
| 2188 | list->expr = expr; |
| 2189 | |
| 2190 | /* Link the list element with the pattern_head. */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2191 | LIST_APPEND(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2192 | return expr; |
| 2193 | } |
| 2194 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2195 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2196 | * be returned there on errors and the caller will have to free it. |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2197 | * |
| 2198 | * The file contains one key + value per line. Lines which start with '#' are |
| 2199 | * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is |
| 2200 | * then the first "word" (series of non-space/tabs characters), and the value is |
| 2201 | * what follows this series of space/tab till the end of the line excluding |
| 2202 | * trailing spaces/tabs. |
| 2203 | * |
| 2204 | * Example : |
| 2205 | * |
| 2206 | * # this is a comment and is ignored |
| 2207 | * 62.212.114.60 1wt.eu \n |
| 2208 | * <-><-----------><---><----><----> |
| 2209 | * | | | | `--- trailing spaces ignored |
| 2210 | * | | | `-------- value |
| 2211 | * | | `--------------- middle spaces ignored |
| 2212 | * | `------------------------ key |
| 2213 | * `-------------------------------- leading spaces ignored |
| 2214 | * |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 2215 | * Return non-zero in case of success, otherwise 0. |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2216 | */ |
| 2217 | int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err) |
| 2218 | { |
| 2219 | FILE *file; |
| 2220 | char *c; |
| 2221 | int ret = 0; |
| 2222 | int line = 0; |
| 2223 | char *key_beg; |
| 2224 | char *key_end; |
| 2225 | char *value_beg; |
| 2226 | char *value_end; |
| 2227 | |
| 2228 | file = fopen(filename, "r"); |
| 2229 | if (!file) { |
| 2230 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | /* now parse all patterns. The file may contain only one pattern |
| 2235 | * followed by one value per line. The start spaces, separator spaces |
| 2236 | * and and spaces are stripped. Each can contain comment started by '#' |
| 2237 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2238 | while (fgets(trash.area, trash.size, file) != NULL) { |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2239 | line++; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2240 | c = trash.area; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2241 | |
| 2242 | /* ignore lines beginning with a dash */ |
| 2243 | if (*c == '#') |
| 2244 | continue; |
| 2245 | |
| 2246 | /* strip leading spaces and tabs */ |
| 2247 | while (*c == ' ' || *c == '\t') |
| 2248 | c++; |
| 2249 | |
| 2250 | /* empty lines are ignored too */ |
| 2251 | if (*c == '\0' || *c == '\r' || *c == '\n') |
| 2252 | continue; |
| 2253 | |
| 2254 | /* look for the end of the key */ |
| 2255 | key_beg = c; |
| 2256 | while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2257 | c++; |
| 2258 | |
| 2259 | key_end = c; |
| 2260 | |
| 2261 | /* strip middle spaces and tabs */ |
| 2262 | while (*c == ' ' || *c == '\t') |
| 2263 | c++; |
| 2264 | |
| 2265 | /* look for the end of the value, it is the end of the line */ |
| 2266 | value_beg = c; |
| 2267 | while (*c && *c != '\n' && *c != '\r') |
| 2268 | c++; |
| 2269 | value_end = c; |
| 2270 | |
| 2271 | /* trim possibly trailing spaces and tabs */ |
| 2272 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2273 | value_end--; |
| 2274 | |
| 2275 | /* set final \0 and check entries */ |
| 2276 | *key_end = '\0'; |
| 2277 | *value_end = '\0'; |
| 2278 | |
| 2279 | /* insert values */ |
| 2280 | if (!pat_ref_append(ref, key_beg, value_beg, line)) { |
| 2281 | memprintf(err, "out of memory"); |
| 2282 | goto out_close; |
| 2283 | } |
| 2284 | } |
| 2285 | |
Jerome Magnin | 3c79d4b | 2020-01-17 16:09:33 +0100 | [diff] [blame] | 2286 | if (ferror(file)) { |
| 2287 | memprintf(err, "error encountered while reading <%s> : %s", |
| 2288 | filename, strerror(errno)); |
| 2289 | goto out_close; |
| 2290 | } |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 2291 | /* success */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2292 | ret = 1; |
| 2293 | |
| 2294 | out_close: |
| 2295 | fclose(file); |
| 2296 | return ret; |
| 2297 | } |
| 2298 | |
| 2299 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2300 | * be returned there on errors and the caller will have to free it. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2301 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2302 | int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2303 | { |
| 2304 | FILE *file; |
| 2305 | char *c; |
| 2306 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2307 | int ret = 0; |
| 2308 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2309 | |
| 2310 | file = fopen(filename, "r"); |
| 2311 | if (!file) { |
| 2312 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | /* now parse all patterns. The file may contain only one pattern per |
| 2317 | * line. If the line contains spaces, they will be part of the pattern. |
| 2318 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2319 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2320 | while (fgets(trash.area, trash.size, file) != NULL) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2321 | line++; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2322 | c = trash.area; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2323 | |
| 2324 | /* ignore lines beginning with a dash */ |
| 2325 | if (*c == '#') |
| 2326 | continue; |
| 2327 | |
| 2328 | /* strip leading spaces and tabs */ |
| 2329 | while (*c == ' ' || *c == '\t') |
| 2330 | c++; |
| 2331 | |
| 2332 | |
| 2333 | arg = c; |
| 2334 | while (*c && *c != '\n' && *c != '\r') |
| 2335 | c++; |
| 2336 | *c = 0; |
| 2337 | |
| 2338 | /* empty lines are ignored too */ |
| 2339 | if (c == arg) |
| 2340 | continue; |
| 2341 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2342 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2343 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2344 | goto out_close; |
| 2345 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Jerome Magnin | 3c79d4b | 2020-01-17 16:09:33 +0100 | [diff] [blame] | 2348 | if (ferror(file)) { |
| 2349 | memprintf(err, "error encountered while reading <%s> : %s", |
| 2350 | filename, strerror(errno)); |
| 2351 | goto out_close; |
| 2352 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2353 | ret = 1; /* success */ |
| 2354 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2355 | out_close: |
| 2356 | fclose(file); |
| 2357 | return ret; |
| 2358 | } |
| 2359 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2360 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2361 | const char *filename, int patflags, int load_smp, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2362 | char **err, const char *file, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2363 | { |
| 2364 | struct pat_ref *ref; |
| 2365 | struct pattern_expr *expr; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2366 | struct pat_ref_elt *elt; |
Willy Tarreau | 4deaf39 | 2014-11-26 13:17:03 +0100 | [diff] [blame] | 2367 | int reuse = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2368 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2369 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2370 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2371 | |
| 2372 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2373 | if (!ref) { |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2374 | chunk_printf(&trash, |
| 2375 | "pattern loaded from file '%s' used by %s at file '%s' line %d", |
| 2376 | filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2377 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2378 | ref = pat_ref_new(filename, trash.area, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2379 | if (!ref) { |
| 2380 | memprintf(err, "out of memory"); |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2384 | if (load_smp) { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2385 | ref->flags |= PAT_REF_SMP; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2386 | if (!pat_ref_read_from_file_smp(ref, filename, err)) |
| 2387 | return 0; |
| 2388 | } |
| 2389 | else { |
| 2390 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2391 | return 0; |
| 2392 | } |
| 2393 | } |
| 2394 | else { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2395 | /* The reference already exists, check the map compatibility. */ |
| 2396 | |
| 2397 | /* If the load require samples and the flag PAT_REF_SMP is not set, |
| 2398 | * the reference doesn't contain sample, and cannot be used. |
| 2399 | */ |
| 2400 | if (load_smp) { |
| 2401 | if (!(ref->flags & PAT_REF_SMP)) { |
| 2402 | memprintf(err, "The file \"%s\" is already used as one column file " |
| 2403 | "and cannot be used by as two column file.", |
| 2404 | filename); |
| 2405 | return 0; |
| 2406 | } |
| 2407 | } |
| 2408 | else { |
| 2409 | /* The load doesn't require samples. If the flag PAT_REF_SMP is |
| 2410 | * set, the reference contains a sample, and cannot be used. |
| 2411 | */ |
| 2412 | if (ref->flags & PAT_REF_SMP) { |
| 2413 | memprintf(err, "The file \"%s\" is already used as two column file " |
| 2414 | "and cannot be used by as one column file.", |
| 2415 | filename); |
| 2416 | return 0; |
| 2417 | } |
| 2418 | } |
| 2419 | |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2420 | /* Extends display */ |
| 2421 | chunk_printf(&trash, "%s", ref->display); |
| 2422 | chunk_appendf(&trash, ", by %s at file '%s' line %d", |
| 2423 | refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2424 | free(ref->display); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2425 | ref->display = strdup(trash.area); |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2426 | if (!ref->display) { |
| 2427 | memprintf(err, "out of memory"); |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2431 | /* Merge flags. */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2432 | ref->flags |= refflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /* Now, we can loading patterns from the reference. */ |
| 2436 | |
| 2437 | /* Lookup for existing reference in the head. If the reference |
| 2438 | * doesn't exists, create it. |
| 2439 | */ |
| 2440 | expr = pattern_lookup_expr(head, ref); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2441 | if (!expr || (expr->mflags != patflags)) { |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2442 | expr = pattern_new_expr(head, ref, patflags, err, &reuse); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2443 | if (!expr) |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2447 | /* The returned expression may be not empty, because the function |
| 2448 | * "pattern_new_expr" lookup for similar pattern list and can |
| 2449 | * reuse a already filled pattern list. In this case, we can not |
| 2450 | * reload the patterns. |
| 2451 | */ |
| 2452 | if (reuse) |
| 2453 | return 1; |
| 2454 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2455 | /* Load reference content in the pattern expression. */ |
| 2456 | list_for_each_entry(elt, &ref->head, list) { |
| 2457 | if (!pat_ref_push(elt, expr, patflags, err)) { |
| 2458 | if (elt->line > 0) |
| 2459 | memprintf(err, "%s at line %d of file '%s'", |
| 2460 | *err, elt->line, filename); |
| 2461 | return 0; |
| 2462 | } |
| 2463 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2464 | |
| 2465 | return 1; |
| 2466 | } |
| 2467 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2468 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 2469 | * to sample <smp>. The function returns NULL if the sample don't match. It returns |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2470 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2471 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2472 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2473 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2474 | struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2475 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2476 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2477 | struct pattern *pat; |
| 2478 | |
| 2479 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2480 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2481 | static_pattern.data = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2482 | static_pattern.ref = NULL; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2483 | static_pattern.sflags = 0; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2484 | static_pattern.type = SMP_T_SINT; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2485 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2486 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2487 | return &static_pattern; |
| 2488 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2490 | /* convert input to string */ |
| 2491 | if (!sample_convert(smp, head->expect_type)) |
| 2492 | return NULL; |
| 2493 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2494 | list_for_each_entry(list, &head->head, list) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2495 | HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2496 | pat = head->match(smp, list->expr, fill); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2497 | if (pat) { |
| 2498 | /* We duplicate the pattern cause it could be modified |
| 2499 | by another thread */ |
| 2500 | if (pat != &static_pattern) { |
| 2501 | memcpy(&static_pattern, pat, sizeof(struct pattern)); |
| 2502 | pat = &static_pattern; |
| 2503 | } |
| 2504 | |
| 2505 | /* We also duplicate the sample data for |
| 2506 | same reason */ |
| 2507 | if (pat->data && (pat->data != &static_sample_data)) { |
Christopher Faulet | 09fdf4b | 2017-11-09 16:14:16 +0100 | [diff] [blame] | 2508 | switch(pat->data->type) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2509 | case SMP_T_STR: |
| 2510 | static_sample_data.type = SMP_T_STR; |
| 2511 | static_sample_data.u.str = *get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2512 | static_sample_data.u.str.data = pat->data->u.str.data; |
| 2513 | if (static_sample_data.u.str.data >= static_sample_data.u.str.size) |
| 2514 | static_sample_data.u.str.data = static_sample_data.u.str.size - 1; |
| 2515 | memcpy(static_sample_data.u.str.area, |
Willy Tarreau | 2fc761e | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2516 | pat->data->u.str.area, static_sample_data.u.str.data); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2517 | static_sample_data.u.str.area[static_sample_data.u.str.data] = 0; |
Willy Tarreau | 2fc761e | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2518 | pat->data = &static_sample_data; |
| 2519 | break; |
| 2520 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2521 | case SMP_T_IPV4: |
| 2522 | case SMP_T_IPV6: |
| 2523 | case SMP_T_SINT: |
| 2524 | memcpy(&static_sample_data, pat->data, sizeof(struct sample_data)); |
Willy Tarreau | 2fc761e | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2525 | pat->data = &static_sample_data; |
| 2526 | break; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2527 | default: |
Willy Tarreau | 2fc761e | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2528 | /* unimplemented pattern type */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2529 | pat->data = NULL; |
Willy Tarreau | 2fc761e | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2530 | break; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2531 | } |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2532 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2533 | HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2534 | return pat; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2535 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2536 | HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2537 | } |
| 2538 | return NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 2541 | /* This function prunes the pattern expressions starting at pattern_head <head>. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2542 | void pattern_prune(struct pattern_head *head) |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2543 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2544 | struct pattern_expr_list *list, *safe; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2545 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2546 | list_for_each_entry_safe(list, safe, &head->head, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2547 | LIST_DELETE(&list->list); |
Aurelien DARRAGON | 2f33952 | 2024-09-09 14:59:19 +0200 | [diff] [blame] | 2548 | if (HA_ATOMIC_SUB_FETCH(&list->expr->refcount, 1) == 0) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2549 | LIST_DELETE(&list->expr->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2550 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2551 | head->prune(list->expr); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2552 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2553 | free(list->expr); |
| 2554 | } |
| 2555 | free(list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2556 | } |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2557 | } |
| 2558 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 2559 | /* This function searches occurrences of pattern reference element <ref> in |
| 2560 | * expression <expr> and returns a pointer to a pointer of the sample storage. |
| 2561 | * If <ref> is not found, NULL is returned. |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2562 | */ |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 2563 | struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2564 | { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2565 | struct ebmb_node *node; |
| 2566 | struct pattern_tree *elt; |
| 2567 | struct pattern_list *pat; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2568 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2569 | for (node = ebmb_first(&expr->pattern_tree); |
| 2570 | node; |
| 2571 | node = ebmb_next(node)) { |
| 2572 | elt = container_of(node, struct pattern_tree, node); |
| 2573 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2574 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | for (node = ebmb_first(&expr->pattern_tree_2); |
| 2578 | node; |
| 2579 | node = ebmb_next(node)) { |
| 2580 | elt = container_of(node, struct pattern_tree, node); |
| 2581 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2582 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | list_for_each_entry(pat, &expr->patterns, list) |
| 2586 | if (pat->pat.ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2587 | return &pat->pat.data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2588 | |
| 2589 | return NULL; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 2592 | /* This function compares two pat_ref** on their unique_id, and returns -1/0/1 |
| 2593 | * depending on their order (suitable for sorting). |
| 2594 | */ |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2595 | static int cmp_pat_ref(const void *_a, const void *_b) |
| 2596 | { |
| 2597 | struct pat_ref * const *a = _a; |
| 2598 | struct pat_ref * const *b = _b; |
| 2599 | |
| 2600 | if ((*a)->unique_id < (*b)->unique_id) |
| 2601 | return -1; |
| 2602 | else if ((*a)->unique_id > (*b)->unique_id) |
| 2603 | return 1; |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
Willy Tarreau | a5bbaaf | 2020-10-30 16:03:50 +0100 | [diff] [blame] | 2607 | /* This function finalizes the configuration parsing. It sets all the |
| 2608 | * automatic ids. |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2609 | */ |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2610 | int pattern_finalize_config(void) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2611 | { |
Tim Duesterhus | b584b44 | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2612 | size_t len = 0; |
| 2613 | size_t unassigned_pos = 0; |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2614 | int next_unique_id = 0; |
Tim Duesterhus | b584b44 | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2615 | size_t i, j; |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2616 | struct pat_ref *ref, **arr; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2617 | struct list pr = LIST_HEAD_INIT(pr); |
| 2618 | |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 2619 | pat_lru_seed = ha_random(); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 2620 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2621 | /* Count pat_refs with user defined unique_id and totalt count */ |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2622 | list_for_each_entry(ref, &pattern_reference, list) { |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2623 | len++; |
| 2624 | if (ref->unique_id != -1) |
| 2625 | unassigned_pos++; |
| 2626 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2627 | |
Tim Duesterhus | b584b44 | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2628 | if (len == 0) { |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2632 | arr = calloc(len, sizeof(*arr)); |
| 2633 | if (arr == NULL) { |
| 2634 | ha_alert("Out of memory error.\n"); |
| 2635 | return ERR_ALERT | ERR_FATAL; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2638 | i = 0; |
| 2639 | j = unassigned_pos; |
| 2640 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2641 | if (ref->unique_id != -1) |
| 2642 | arr[i++] = ref; |
| 2643 | else |
| 2644 | arr[j++] = ref; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2647 | /* Sort first segment of array with user-defined unique ids for |
| 2648 | * fast lookup when generating unique ids |
| 2649 | */ |
| 2650 | qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref); |
| 2651 | |
| 2652 | /* Assign unique ids to the rest of the elements */ |
| 2653 | for (i = unassigned_pos; i < len; i++) { |
| 2654 | do { |
| 2655 | arr[i]->unique_id = next_unique_id++; |
| 2656 | } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref)); |
| 2657 | } |
| 2658 | |
| 2659 | /* Sort complete array */ |
| 2660 | qsort(arr, len, sizeof(*arr), cmp_pat_ref); |
| 2661 | |
| 2662 | /* Convert back to linked list */ |
| 2663 | for (i = 0; i < len; i++) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2664 | LIST_APPEND(&pr, &arr[i]->list); |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2665 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2666 | /* swap root */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2667 | LIST_INSERT(&pr, &pattern_reference); |
| 2668 | LIST_DELETE(&pr); |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2669 | |
| 2670 | free(arr); |
| 2671 | return 0; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2672 | } |
Willy Tarreau | 403bfbb | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 2673 | |
| 2674 | static int pattern_per_thread_lru_alloc() |
| 2675 | { |
| 2676 | if (!global.tune.pattern_cache) |
| 2677 | return 1; |
| 2678 | pat_lru_tree = lru64_new(global.tune.pattern_cache); |
| 2679 | return !!pat_lru_tree; |
| 2680 | } |
| 2681 | |
| 2682 | static void pattern_per_thread_lru_free() |
| 2683 | { |
| 2684 | lru64_destroy(pat_lru_tree); |
| 2685 | } |
| 2686 | |
| 2687 | REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc); |
| 2688 | REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free); |