blob: fc228db106e7e94a06fb5f0d0900bc65d8bb5c78 [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
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 Magnin556a94a2020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
17#include <common/config.h>
18#include <common/standard.h>
19
20#include <types/global.h>
21#include <types/pattern.h>
22
Thierry FOURNIER46006bd2014-03-21 21:45:15 +010023#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010025#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010026
27#include <ebsttree.h>
Willy Tarreauf3045d22015-04-29 16:24:50 +020028#include <import/lru.h>
29#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010030
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010031char *pat_match_names[PAT_MATCH_NUM] = {
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 Fournier8feaa662016-02-10 22:55:20 +010045 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010046};
47
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020048int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010049 [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 FOURNIER5d344082014-01-27 14:19:53 +010054 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010055 [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 Fournier8feaa662016-02-10 22:55:20 +010062 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063};
64
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010065int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
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 Tarreaub1dd9bf2014-05-10 08:53:48 +020073 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010074 [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 Fournier8feaa662016-02-10 22:55:20 +010079 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010080};
81
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010082void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010083 [PAT_MATCH_FOUND] = pat_del_list_val,
84 [PAT_MATCH_BOOL] = pat_del_list_val,
85 [PAT_MATCH_INT] = pat_del_list_val,
86 [PAT_MATCH_IP] = pat_del_tree_ip,
87 [PAT_MATCH_BIN] = pat_del_list_ptr,
88 [PAT_MATCH_LEN] = pat_del_list_val,
89 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020090 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010091 [PAT_MATCH_SUB] = pat_del_list_ptr,
92 [PAT_MATCH_DIR] = pat_del_list_ptr,
93 [PAT_MATCH_DOM] = pat_del_list_ptr,
94 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010095 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010096 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010097};
98
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010099void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
100 [PAT_MATCH_FOUND] = pat_prune_val,
101 [PAT_MATCH_BOOL] = pat_prune_val,
102 [PAT_MATCH_INT] = pat_prune_val,
103 [PAT_MATCH_IP] = pat_prune_val,
104 [PAT_MATCH_BIN] = pat_prune_ptr,
105 [PAT_MATCH_LEN] = pat_prune_val,
106 [PAT_MATCH_STR] = pat_prune_ptr,
107 [PAT_MATCH_BEG] = pat_prune_ptr,
108 [PAT_MATCH_SUB] = pat_prune_ptr,
109 [PAT_MATCH_DIR] = pat_prune_ptr,
110 [PAT_MATCH_DOM] = pat_prune_ptr,
111 [PAT_MATCH_END] = pat_prune_ptr,
112 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100113 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100114};
115
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100116struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117 [PAT_MATCH_FOUND] = NULL,
118 [PAT_MATCH_BOOL] = pat_match_nothing,
119 [PAT_MATCH_INT] = pat_match_int,
120 [PAT_MATCH_IP] = pat_match_ip,
121 [PAT_MATCH_BIN] = pat_match_bin,
122 [PAT_MATCH_LEN] = pat_match_len,
123 [PAT_MATCH_STR] = pat_match_str,
124 [PAT_MATCH_BEG] = pat_match_beg,
125 [PAT_MATCH_SUB] = pat_match_sub,
126 [PAT_MATCH_DIR] = pat_match_dir,
127 [PAT_MATCH_DOM] = pat_match_dom,
128 [PAT_MATCH_END] = pat_match_end,
129 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100130 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100131};
132
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100133/* Just used for checking configuration compatibility */
134int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200135 [PAT_MATCH_FOUND] = SMP_T_SINT,
136 [PAT_MATCH_BOOL] = SMP_T_SINT,
137 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100138 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100139 [PAT_MATCH_BIN] = SMP_T_BIN,
140 [PAT_MATCH_LEN] = SMP_T_STR,
141 [PAT_MATCH_STR] = SMP_T_STR,
142 [PAT_MATCH_BEG] = SMP_T_STR,
143 [PAT_MATCH_SUB] = SMP_T_STR,
144 [PAT_MATCH_DIR] = SMP_T_STR,
145 [PAT_MATCH_DOM] = SMP_T_STR,
146 [PAT_MATCH_END] = SMP_T_STR,
147 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100148 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100149};
150
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100151/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200152static THREAD_LOCAL struct pattern static_pattern;
153static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100154
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100155/* This is the root of the list of all pattern_ref avalaibles. */
156struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
157
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200158static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200159static unsigned long long pat_lru_seed;
160
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100161/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100162 *
163 * The following functions are not exported and are used by internals process
164 * of pattern matching
165 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166 */
167
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168/* Background: Fast way to find a zero byte in a word
169 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
170 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
171 *
172 * To look for 4 different byte values, xor the word with those bytes and
173 * then check for zero bytes:
174 *
175 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
176 * where <delimiter> is the 4 byte values to look for (as an uint)
177 * and <c> is the character that is being tested
178 */
179static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
180{
181 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
182 return (mask - 0x01010101) & ~mask & 0x80808080U;
183}
184
185static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
186{
187 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
188}
189
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100190
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191/*
192 *
193 * These functions are exported and may be used by any other component.
194 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200195 * The following functions are used for parsing pattern matching input value.
196 * The <text> contain the string to be parsed. <pattern> must be a preallocated
197 * pattern. The pat_parse_* functions fill this structure with the parsed value.
198 * <err> is filled with an error message built with memprintf() function. It is
199 * allowed to use a trash as a temporary storage for the returned pattern, as
200 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200202 * In success case, the pat_parse_* function returns 1. If the function
203 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200207int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208{
209 return 1;
210}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200213int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100215 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100217 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219}
220
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100221/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200222int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223{
Willy Tarreau83061a82018-07-13 11:56:34 +0200224 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100226 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100227 trash = get_trash_chunk();
228 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100230 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231}
232
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200234int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100236 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200255int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200259 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100260
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 goto not_valid_range;
264
265 /* Search ':' or '-' separator. */
266 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
267 ptr++;
268
269 /* If separator not found. */
270 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 return 0;
274 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 pattern->val.range.max = pattern->val.range.min;
276 pattern->val.range.min_set = 1;
277 pattern->val.range.max_set = 1;
278 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 }
280
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100282 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 pattern->val.range.min_set = 0;
287 pattern->val.range.max_set = 1;
288 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289 }
290
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291 /* If separator is the last character. */
292 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 pattern->val.range.min_set = 1;
297 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100299 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300
301 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
304
305 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306 goto not_valid_range;
307
308 if (pattern->val.range.min > pattern->val.range.max)
309 goto not_valid_range;
310
311 pattern->val.range.min_set = 1;
312 pattern->val.range.max_set = 1;
313 return 1;
314
315 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100316 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
320/* Parse a range of positive 2-component versions delimited by either ':' or
321 * '-'. The version consists in a major and a minor, both of which must be
322 * smaller than 65536, because internally they will be represented as a 32-bit
323 * integer.
324 * If only one version is read, it is set as both min and max. Just like for
325 * pure integers, an operator may be specified as the prefix, among this list
326 * of 5 :
327 *
328 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
329 *
330 * The default operator is "eq". It supports range matching. Ranges are
331 * rejected for other operators. The operator may be changed at any time.
332 * The operator is stored in the 'opaque' argument. This allows constructs
333 * such as the following one :
334 *
335 * acl obsolete_ssl ssl_req_proto lt 3
336 * acl unsupported_ssl ssl_req_proto gt 3.1
337 * acl valid_ssl ssl_req_proto 3.0-3.1
338 *
339 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200344 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345
346 /* Search ':' or '-' separator. */
347 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
348 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100349
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 if (*ptr == '\0' && ptr > text) {
352 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 return 0;
355 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 pattern->val.range.max = pattern->val.range.min;
357 pattern->val.range.min_set = 1;
358 pattern->val.range.max_set = 1;
359 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360 }
361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100367 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 pattern->val.range.min_set = 0;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == &text[strlen(text)-1]) {
375 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 pattern->val.range.min_set = 1;
380 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 1;
382 }
383
384 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 return 0;
388 }
389 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100391 return 0;
392 }
393 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100394 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 pattern->val.range.min_set = 1;
398 pattern->val.range.max_set = 1;
399 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100400}
401
402/* Parse an IP address and an optional mask in the form addr[/mask].
403 * The addr may either be an IPv4 address or a hostname. The mask
404 * may either be a dotted mask or a number of bits. Returns 1 if OK,
405 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
406 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100410 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 return 1;
413 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100414 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 pattern->type = SMP_T_IPV6;
416 return 1;
417 }
418 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100419 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100420 return 0;
421 }
422}
423
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424/*
425 *
426 * These functions are exported and may be used by any other component.
427 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800428 * This function just takes a sample <smp> and checks if this sample matches
429 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100430 * PAT_NOMATCH.
431 *
432 */
433
434/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200437 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200439 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.type = 0;
442 static_pattern.ptr.str = NULL;
443 }
444 return &static_pattern;
445 }
446 else
447 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448}
449
450
Joseph Herlant4189d672018-11-15 10:22:31 -0800451/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100452struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453{
454 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100455 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct pattern_tree *elt;
457 struct pattern_list *lst;
458 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200459 struct pattern *ret = NULL;
460 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* Lookup a string in the expression's pattern tree. */
463 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Faulet135899e2020-06-30 18:52:32 +0200464 char prev = 0;
465
466 if (smp->data.u.str.data < smp->data.u.str.size) {
467 /* we may have to force a trailing zero on the test pattern and
Thierry Fournier80100d82020-11-10 20:51:36 +0100468 * the buffer is large enough to accommodate it. If the flag
469 * CONST is set, duplicate the string
Christopher Faulet135899e2020-06-30 18:52:32 +0200470 */
471 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry Fournier80100d82020-11-10 20:51:36 +0100472 if (prev) {
473 if (smp->flags & SMP_F_CONST) {
474 if (!smp_dup(smp))
475 return NULL;
476 } else {
477 smp->data.u.str.area[smp->data.u.str.data] = '\0';
478 }
479 }
Christopher Faulet135899e2020-06-30 18:52:32 +0200480 }
481 else {
482 /* Otherwise, the sample is duplicated. A trailing zero
483 * is automatically added to the string.
484 */
485 if (!smp_dup(smp))
486 return NULL;
487 }
488
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200489 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100490 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200491 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100492 if (node) {
493 if (fill) {
494 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200495 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100496 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200497 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 static_pattern.type = SMP_T_STR;
499 static_pattern.ptr.str = (char *)elt->node.key;
500 }
501 return &static_pattern;
502 }
503 }
504
505 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200506 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200507 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200510 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200511 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200512 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200513 return ret;
514 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200515 }
516
Emeric Brunb5997f72017-07-03 11:34:05 +0200517
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100518 list_for_each_entry(lst, &expr->patterns, list) {
519 pattern = &lst->pat;
520
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100522 continue;
523
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200524 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200525 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
526 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200527 ret = pattern;
528 break;
529 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100530 }
531
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200532 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200533 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200534
535 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100536}
537
538/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100539struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100540{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 struct pattern_list *lst;
542 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200543 struct pattern *ret = NULL;
544 struct lru64 *lru = NULL;
545
546 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200547 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100548
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200550 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200551 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200552 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200553 return ret;
554 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200555 }
556
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100557 list_for_each_entry(lst, &expr->patterns, list) {
558 pattern = &lst->pat;
559
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200560 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100561 continue;
562
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200563 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200564 ret = pattern;
565 break;
566 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100567 }
568
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200569 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200570 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200571
572 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100573}
574
575/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100576 * and restores the previous character when leaving. This function fills
577 * a matching array.
578 */
579struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
580{
581 struct pattern_list *lst;
582 struct pattern *pattern;
583 struct pattern *ret = NULL;
584
585 list_for_each_entry(lst, &expr->patterns, list) {
586 pattern = &lst->pat;
587
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100589 MAX_MATCH, pmatch, 0)) {
590 ret = pattern;
591 smp->ctx.a[0] = pmatch;
592 break;
593 }
594 }
595
596 return ret;
597}
598
599/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100600 * and restores the previous character when leaving.
601 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100602struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100603{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100604 struct pattern_list *lst;
605 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606 struct pattern *ret = NULL;
607 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608
Willy Tarreauf3045d22015-04-29 16:24:50 +0200609 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200610 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200611
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200612 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200613 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200614 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200615 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200616 return ret;
617 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200618 }
619
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 list_for_each_entry(lst, &expr->patterns, list) {
621 pattern = &lst->pat;
622
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624 ret = pattern;
625 break;
626 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100627 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200628
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200629 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200630 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200631
632 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100633}
634
635/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100636struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100637{
638 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200640 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100641 struct pattern_list *lst;
642 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200643 struct pattern *ret = NULL;
644 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100645
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200646 /* Lookup a string in the expression's pattern tree. */
647 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Faulet135899e2020-06-30 18:52:32 +0200648 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
652 * the buffer is large enough to accommodate it.
653 */
654 prev = smp->data.u.str.area[smp->data.u.str.data];
655 if (prev)
656 smp->data.u.str.area[smp->data.u.str.data] = '\0';
657 }
658 else {
659 /* Otherwise, the sample is duplicated. A trailing zero
660 * is automatically added to the string.
661 */
662 if (!smp_dup(smp))
663 return NULL;
664 }
665
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200666 node = ebmb_lookup_longest(&expr->pattern_tree,
667 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200668 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200669 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200670
671 if (node) {
672 if (fill) {
673 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200674 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200675 static_pattern.ref = elt->ref;
676 static_pattern.sflags = PAT_SF_TREE;
677 static_pattern.type = SMP_T_STR;
678 static_pattern.ptr.str = (char *)elt->node.key;
679 }
680 return &static_pattern;
681 }
682 }
683
684 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200685 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200686 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200687
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200689 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200690 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200691 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200692 return ret;
693 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200694 }
695
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100696 list_for_each_entry(lst, &expr->patterns, list) {
697 pattern = &lst->pat;
698
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700 continue;
701
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200702 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
704 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705 continue;
706
Willy Tarreauf3045d22015-04-29 16:24:50 +0200707 ret = pattern;
708 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100709 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200710
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200711 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200712 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200713
714 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100715}
716
717/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100719{
720 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100721 struct pattern_list *lst;
722 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200723 struct pattern *ret = NULL;
724 struct lru64 *lru = NULL;
725
726 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200727 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200730 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200731 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200732 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200733 return ret;
734 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200735 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737 list_for_each_entry(lst, &expr->patterns, list) {
738 pattern = &lst->pat;
739
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200740 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100741 continue;
742
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200743 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
745 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100746 continue;
747
Willy Tarreauf3045d22015-04-29 16:24:50 +0200748 ret = pattern;
749 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200751
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200752 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200753 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200754
755 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100756}
757
758/* Checks that the pattern is included inside the tested string.
759 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
760 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100761struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100762{
763 int icase;
764 char *end;
765 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100766 struct pattern_list *lst;
767 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200768 struct pattern *ret = NULL;
769 struct lru64 *lru = NULL;
770
771 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200772 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200773
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200775 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200776 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200777 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200778 return ret;
779 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200780 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100781
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100782 list_for_each_entry(lst, &expr->patterns, list) {
783 pattern = &lst->pat;
784
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200785 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100786 continue;
787
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200788 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200789 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100790 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200791 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100792 if (tolower(*c) != tolower(*pattern->ptr.str))
793 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200794 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
795 ret = pattern;
796 goto leave;
797 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100798 }
799 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200800 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100801 if (*c != *pattern->ptr.str)
802 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200803 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
804 ret = pattern;
805 goto leave;
806 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100807 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100808 }
809 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200810 leave:
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200811 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200812 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200813
814 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100815}
816
817/* This one is used by other real functions. It checks that the pattern is
818 * included inside the tested string, but enclosed between the specified
819 * delimiters or at the beginning or end of the string. The delimiters are
820 * provided as an unsigned int made by make_4delim() and match up to 4 different
821 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
822 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200823static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100824{
825 int may_match, icase;
826 char *c, *end;
827 char *ps;
828 int pl;
829
830 pl = pattern->len;
831 ps = pattern->ptr.str;
832
833 while (pl > 0 && is_delimiter(*ps, delimiters)) {
834 pl--;
835 ps++;
836 }
837
838 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
839 pl--;
840
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200841 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100842 return PAT_NOMATCH;
843
844 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200845 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 end = smp->data.u.str.area + smp->data.u.str.data - pl;
847 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100848 if (is_delimiter(*c, delimiters)) {
849 may_match = 1;
850 continue;
851 }
852
853 if (!may_match)
854 continue;
855
856 if (icase) {
857 if ((tolower(*c) == tolower(*ps)) &&
858 (strncasecmp(ps, c, pl) == 0) &&
859 (c == end || is_delimiter(c[pl], delimiters)))
860 return PAT_MATCH;
861 } else {
862 if ((*c == *ps) &&
863 (strncmp(ps, c, pl) == 0) &&
864 (c == end || is_delimiter(c[pl], delimiters)))
865 return PAT_MATCH;
866 }
867 may_match = 0;
868 }
869 return PAT_NOMATCH;
870}
871
872/* Checks that the pattern is included inside the tested string, but enclosed
873 * between the delimiters '?' or '/' or at the beginning or end of the string.
874 * Delimiters at the beginning or end of the pattern are ignored.
875 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100877{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100878 struct pattern_list *lst;
879 struct pattern *pattern;
880
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100881 list_for_each_entry(lst, &expr->patterns, list) {
882 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200883 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 return pattern;
885 }
886 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100887}
888
889/* Checks that the pattern is included inside the tested string, but enclosed
890 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
891 * the string. Delimiters at the beginning or end of the pattern are ignored.
892 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100893struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100894{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100895 struct pattern_list *lst;
896 struct pattern *pattern;
897
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100898 list_for_each_entry(lst, &expr->patterns, list) {
899 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200900 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100901 return pattern;
902 }
903 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100904}
905
906/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100907struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100908{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909 struct pattern_list *lst;
910 struct pattern *pattern;
911
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100912 list_for_each_entry(lst, &expr->patterns, list) {
913 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200914 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
915 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100916 return pattern;
917 }
918 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100919}
920
921/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100922struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100923{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100924 struct pattern_list *lst;
925 struct pattern *pattern;
926
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100927 list_for_each_entry(lst, &expr->patterns, list) {
928 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200929 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
930 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100931 return pattern;
932 }
933 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100934}
935
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100936struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100937{
938 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100939 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940 struct in_addr *s;
941 struct ebmb_node *node;
942 struct pattern_tree *elt;
943 struct pattern_list *lst;
944 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100945
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100946 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200947 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100948 /* Lookup an IPv4 address in the expression's pattern tree using
949 * the longest match method.
950 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200951 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100952 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
953 if (node) {
954 if (fill) {
955 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200956 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100957 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200958 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100959 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200960 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100961 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
962 return NULL;
963 }
964 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100965 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100966
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100967 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
968 * sample address to IPv6 with the mapping method using the ::ffff:
969 * prefix, and try to lookup in the IPv6 tree.
970 */
971 memset(&tmp6, 0, 10);
972 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200973 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100974 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
975 if (node) {
976 if (fill) {
977 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200978 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100979 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200980 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100981 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200982 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100983 static_pattern.val.ipv6.mask = elt->node.node.pfx;
984 }
985 return &static_pattern;
986 }
987 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100988
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100989 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200990 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100991 /* Lookup an IPv6 address in the expression's pattern tree using
992 * the longest match method.
993 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200994 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100995 if (node) {
996 if (fill) {
997 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200998 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100999 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001000 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001001 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +02001002 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001003 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001004 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001005 return &static_pattern;
1006 }
1007
1008 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1009 * following forms :
1010 * - ::ffff:ip:v4 (ipv4 mapped)
1011 * - ::0000:ip:v4 (old ipv4 mapped)
1012 * - 2002:ip:v4:: (6to4)
1013 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001014 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1015 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1016 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1017 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
1018 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1019 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
1020 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001021 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001022 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1023 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001024
1025 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1026 * match method.
1027 */
1028 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1029 if (node) {
1030 if (fill) {
1031 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001032 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001033 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001034 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001035 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +02001036 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001037 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1038 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001039 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001040 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001041 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001042 }
1043 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001044
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001045 /* Lookup in the list. the list contain only IPv4 patterns */
1046 list_for_each_entry(lst, &expr->patterns, list) {
1047 pattern = &lst->pat;
1048
1049 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001050 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001051 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001052 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001053 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001054 /* v4 match on a V6 sample. We want to check at least for
1055 * the following forms :
1056 * - ::ffff:ip:v4 (ipv4 mapped)
1057 * - ::0000:ip:v4 (old ipv4 mapped)
1058 * - 2002:ip:v4:: (6to4)
1059 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001060 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1061 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1062 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1063 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1064 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001065 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001066 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1067 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1068 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001069 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001070 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001071 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001072 } else {
1073 /* impossible */
1074 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001075 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001076
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001077 /* Check if the input sample match the current pattern. */
1078 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001079 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001080 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001081 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001082}
1083
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001084void free_pattern_tree(struct eb_root *root)
1085{
1086 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001087 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001088
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001089 node = eb_first(root);
1090 while (node) {
1091 next = eb_next(node);
1092 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001093 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001094 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001095 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001096 node = next;
1097 }
1098}
1099
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001100void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001101{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001102 struct pattern_list *pat, *tmp;
1103
1104 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001105 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001106 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001107 free(pat);
1108 }
1109
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001110 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001111 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001112 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001113 expr->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001114}
1115
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001116void pat_prune_ptr(struct pattern_expr *expr)
1117{
1118 struct pattern_list *pat, *tmp;
1119
1120 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001121 LIST_DEL(&pat->list);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001122 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001123 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001124 free(pat);
1125 }
1126
1127 free_pattern_tree(&expr->pattern_tree);
1128 free_pattern_tree(&expr->pattern_tree_2);
1129 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001130 expr->revision = rdtsc();
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001131}
1132
1133void pat_prune_reg(struct pattern_expr *expr)
1134{
1135 struct pattern_list *pat, *tmp;
1136
1137 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001138 LIST_DEL(&pat->list);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001139 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001140 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001141 free(pat);
1142 }
1143
1144 free_pattern_tree(&expr->pattern_tree);
1145 free_pattern_tree(&expr->pattern_tree_2);
1146 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001147 expr->revision = rdtsc();
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001148}
1149
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001150/*
1151 *
1152 * The following functions are used for the pattern indexation
1153 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001154 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155
1156int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001157{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001158 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001159
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160 /* allocate pattern */
1161 patl = calloc(1, sizeof(*patl));
1162 if (!patl) {
1163 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001164 return 0;
1165 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001166
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001167 /* duplicate pattern */
1168 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001169
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001170 /* chain pattern in the expression */
1171 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001172 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173
1174 /* that's ok */
1175 return 1;
1176}
1177
1178int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1179{
1180 struct pattern_list *patl;
1181
1182 /* allocate pattern */
1183 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001184 if (!patl) {
1185 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001186 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001187 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001188
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001189 /* duplicate pattern */
1190 memcpy(&patl->pat, pat, sizeof(*pat));
1191 patl->pat.ptr.ptr = malloc(patl->pat.len);
1192 if (!patl->pat.ptr.ptr) {
1193 free(patl);
1194 memprintf(err, "out of memory while indexing pattern");
1195 return 0;
1196 }
1197 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001198
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001199 /* chain pattern in the expression */
1200 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001201 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001202
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001203 /* that's ok */
1204 return 1;
1205}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001206
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001207int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1208{
1209 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001210
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001211 /* allocate pattern */
1212 patl = calloc(1, sizeof(*patl));
1213 if (!patl) {
1214 memprintf(err, "out of memory while indexing pattern");
1215 return 0;
1216 }
1217
1218 /* duplicate pattern */
1219 memcpy(&patl->pat, pat, sizeof(*pat));
1220 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1221 if (!patl->pat.ptr.str) {
1222 free(patl);
1223 memprintf(err, "out of memory while indexing pattern");
1224 return 0;
1225 }
1226 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1227 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001228
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001229 /* chain pattern in the expression */
1230 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001231 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001232
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001233 /* that's ok */
1234 return 1;
1235}
1236
Thierry Fournier8feaa662016-02-10 22:55:20 +01001237int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001238{
1239 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001240
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001241 /* allocate pattern */
1242 patl = calloc(1, sizeof(*patl));
1243 if (!patl) {
1244 memprintf(err, "out of memory while indexing pattern");
1245 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001246 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001247
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001248 /* duplicate pattern */
1249 memcpy(&patl->pat, pat, sizeof(*pat));
1250
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001251 /* compile regex */
Dragan Dosen26743032019-04-30 15:54:36 +02001252 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1253 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001254 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001255 return 0;
1256 }
1257
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001258 /* chain pattern in the expression */
1259 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001260 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001261
1262 /* that's ok */
1263 return 1;
1264}
1265
Thierry Fournier8feaa662016-02-10 22:55:20 +01001266int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1267{
1268 return pat_idx_list_reg_cap(expr, pat, 0, err);
1269}
1270
1271int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1272{
1273 return pat_idx_list_reg_cap(expr, pat, 1, err);
1274}
1275
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001276int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1277{
1278 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001279 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001280
1281 /* Only IPv4 can be indexed */
1282 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001283 /* in IPv4 case, check if the mask is contiguous so that we can
1284 * insert the network into the tree. A continuous mask has only
1285 * ones on the left. This means that this mask + its lower bit
1286 * added once again is null.
1287 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001288 mask = ntohl(pat->val.ipv4.mask.s_addr);
1289 if (mask + (mask & -mask) == 0) {
1290 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001291
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001292 /* node memory allocation */
1293 node = calloc(1, sizeof(*node) + 4);
1294 if (!node) {
1295 memprintf(err, "out of memory while loading pattern");
1296 return 0;
1297 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001298
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001299 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001300 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001301 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001302
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001303 /* FIXME: insert <addr>/<mask> into the tree here */
1304 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1305 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001306
1307 /* Insert the entry. */
1308 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001309 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001310
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001311 /* that's ok */
1312 return 1;
1313 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001314 else {
1315 /* If the mask is not contiguous, just add the pattern to the list */
1316 return pat_idx_list_val(expr, pat, err);
1317 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001318 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001319 else if (pat->type == SMP_T_IPV6) {
1320 /* IPv6 also can be indexed */
1321 node = calloc(1, sizeof(*node) + 16);
1322 if (!node) {
1323 memprintf(err, "out of memory while loading pattern");
1324 return 0;
1325 }
1326
1327 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001328 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001329 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001330
1331 /* FIXME: insert <addr>/<mask> into the tree here */
1332 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1333 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001334
1335 /* Insert the entry. */
1336 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001337 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001338
1339 /* that's ok */
1340 return 1;
1341 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001342
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001343 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001344}
1345
1346int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1347{
1348 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001349 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001350
1351 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001352 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001353 memprintf(err, "internal error: string expected, but the type is '%s'",
1354 smp_to_type[pat->type]);
1355 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001356 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001357
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001358 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001359 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001360 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001361
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001362 /* Process the key len */
1363 len = strlen(pat->ptr.str) + 1;
1364
1365 /* node memory allocation */
1366 node = calloc(1, sizeof(*node) + len);
1367 if (!node) {
1368 memprintf(err, "out of memory while loading pattern");
1369 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001370 }
1371
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001372 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001373 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001374 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001375
1376 /* copy the string */
1377 memcpy(node->node.key, pat->ptr.str, len);
1378
1379 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001380 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001381 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001382
1383 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001384 return 1;
1385}
1386
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001387int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1388{
1389 int len;
1390 struct pattern_tree *node;
1391
1392 /* Only string can be indexed */
1393 if (pat->type != SMP_T_STR) {
1394 memprintf(err, "internal error: string expected, but the type is '%s'",
1395 smp_to_type[pat->type]);
1396 return 0;
1397 }
1398
1399 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1400 if (expr->mflags & PAT_MF_IGNORE_CASE)
1401 return pat_idx_list_str(expr, pat, err);
1402
1403 /* Process the key len */
1404 len = strlen(pat->ptr.str);
1405
1406 /* node memory allocation */
1407 node = calloc(1, sizeof(*node) + len + 1);
1408 if (!node) {
1409 memprintf(err, "out of memory while loading pattern");
1410 return 0;
1411 }
1412
1413 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001414 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001415 node->ref = pat->ref;
1416
1417 /* copy the string and the trailing zero */
1418 memcpy(node->node.key, pat->ptr.str, len + 1);
1419 node->node.node.pfx = len * 8;
1420
1421 /* index the new node */
1422 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001423 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001424
1425 /* that's ok */
1426 return 1;
1427}
1428
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001429void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001430{
1431 struct pattern_list *pat;
1432 struct pattern_list *safe;
1433
1434 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1435 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001436 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001437 continue;
1438
1439 /* Delete and free entry. */
1440 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001441 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001442 free(pat);
1443 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001444 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001445}
1446
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001447void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001448{
1449 struct ebmb_node *node, *next_node;
1450 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001451
1452 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001453 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1454 node;
1455 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1456 /* Extract container of the tree node. */
1457 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001458
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001459 /* Check equality. */
1460 if (elt->ref != ref)
1461 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001462
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001463 /* Delete and free entry. */
1464 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001465 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001466 free(elt);
1467 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001468
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001469 /* Browse each node of the list for IPv4 addresses. */
1470 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001471
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001472 /* browse each node of the tree for IPv6 addresses. */
1473 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1474 node;
1475 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1476 /* Extract container of the tree node. */
1477 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001478
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001479 /* Check equality. */
1480 if (elt->ref != ref)
1481 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001482
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001483 /* Delete and free entry. */
1484 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001485 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001486 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001487 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001488 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001489}
1490
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001491void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001492{
1493 struct pattern_list *pat;
1494 struct pattern_list *safe;
1495
1496 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1497 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001498 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001499 continue;
1500
1501 /* Delete and free entry. */
1502 LIST_DEL(&pat->list);
1503 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001504 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001505 free(pat);
1506 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001507 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001508}
1509
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001510void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001511{
1512 struct ebmb_node *node, *next_node;
1513 struct pattern_tree *elt;
1514
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001515 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1516 if (expr->mflags & PAT_MF_IGNORE_CASE)
1517 return pat_del_list_ptr(expr, ref);
1518
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001519 /* browse each node of the tree. */
1520 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1521 node;
1522 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1523 /* Extract container of the tree node. */
1524 elt = container_of(node, struct pattern_tree, node);
1525
1526 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001527 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001528 continue;
1529
1530 /* Delete and free entry. */
1531 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001532 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001533 free(elt);
1534 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001535 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001536}
1537
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001538void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001539{
1540 struct pattern_list *pat;
1541 struct pattern_list *safe;
1542
1543 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1544 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001545 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001546 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001547
1548 /* Delete and free entry. */
1549 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001550 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001551 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001552 free(pat);
1553 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001554 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001555}
1556
1557void pattern_init_expr(struct pattern_expr *expr)
1558{
1559 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001560 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001561 expr->pattern_tree = EB_ROOT;
1562 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001563}
1564
1565void pattern_init_head(struct pattern_head *head)
1566{
1567 LIST_INIT(&head->head);
1568}
1569
1570/* The following functions are relative to the management of the reference
1571 * lists. These lists are used to store the original pattern and associated
1572 * value as string form.
1573 *
1574 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001575 *
1576 * The pattern reference are stored with two identifiers: the unique_id and
1577 * the reference.
1578 *
1579 * The reference identify a file. Each file with the same name point to the
1580 * same reference. We can register many times one file. If the file is modified,
1581 * all his dependencies are also modified. The reference can be used with map or
1582 * acl.
1583 *
1584 * The unique_id identify inline acl. The unique id is unique for each acl.
1585 * You cannot force the same id in the configuration file, because this repoort
1586 * an error.
1587 *
1588 * A particular case appears if the filename is a number. In this case, the
1589 * unique_id is set with the number represented by the filename and the
1590 * reference is also set. This method prevent double unique_id.
1591 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001592 */
1593
1594/* This function lookup for reference. If the reference is found, they return
1595 * pointer to the struct pat_ref, else return NULL.
1596 */
1597struct pat_ref *pat_ref_lookup(const char *reference)
1598{
1599 struct pat_ref *ref;
1600
1601 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001602 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001603 return ref;
1604 return NULL;
1605}
1606
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001607/* This function lookup for unique id. If the reference is found, they return
1608 * pointer to the struct pat_ref, else return NULL.
1609 */
1610struct pat_ref *pat_ref_lookupid(int unique_id)
1611{
1612 struct pat_ref *ref;
1613
1614 list_for_each_entry(ref, &pattern_reference, list)
1615 if (ref->unique_id == unique_id)
1616 return ref;
1617 return NULL;
1618}
1619
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001620/* This function remove all pattern matching the pointer <refelt> from
1621 * the the reference and from each expr member of the reference. This
1622 * function returns 1 if the deletion is done and return 0 is the entry
1623 * is not found.
1624 */
1625int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1626{
1627 struct pattern_expr *expr;
1628 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001629 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001630
1631 /* delete pattern from reference */
1632 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1633 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001634 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1635 /*
1636 * we have to unlink all watchers. We must not relink them if
1637 * this elt was the last one in the list.
1638 */
1639 LIST_DEL(&bref->users);
1640 LIST_INIT(&bref->users);
1641 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001642 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001643 bref->ref = elt->list.n;
1644 }
peter caiaede6dd2015-10-07 00:07:43 -07001645 list_for_each_entry(expr, &ref->pat, list)
1646 pattern_delete(expr, elt);
1647
Emeric Brunb5997f72017-07-03 11:34:05 +02001648 /* pat_ref_elt is trashed once all expr
1649 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001650 LIST_DEL(&elt->list);
1651 free(elt->sample);
1652 free(elt->pattern);
1653 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001654 return 1;
1655 }
1656 }
1657 return 0;
1658}
1659
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001660/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001661 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001662 * if the deletion is done and return 0 is the entry is not found.
1663 */
1664int pat_ref_delete(struct pat_ref *ref, const char *key)
1665{
1666 struct pattern_expr *expr;
1667 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001668 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001669 int found = 0;
1670
1671 /* delete pattern from reference */
1672 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1673 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001674 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1675 /*
1676 * we have to unlink all watchers. We must not relink them if
1677 * this elt was the last one in the list.
1678 */
1679 LIST_DEL(&bref->users);
1680 LIST_INIT(&bref->users);
1681 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001682 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001683 bref->ref = elt->list.n;
1684 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001685 list_for_each_entry(expr, &ref->pat, list)
1686 pattern_delete(expr, elt);
1687
Emeric Brunb5997f72017-07-03 11:34:05 +02001688 /* pat_ref_elt is trashed once all expr
1689 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001690 LIST_DEL(&elt->list);
1691 free(elt->sample);
1692 free(elt->pattern);
1693 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001694
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001695 found = 1;
1696 }
1697 }
1698
1699 if (!found)
1700 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001701 return 1;
1702}
1703
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001704/*
1705 * find and return an element <elt> matching <key> in a reference <ref>
1706 * return NULL if not found
1707 */
1708struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1709{
1710 struct pat_ref_elt *elt;
1711
1712 list_for_each_entry(elt, &ref->head, list) {
1713 if (strcmp(key, elt->pattern) == 0)
1714 return elt;
1715 }
1716
1717 return NULL;
1718}
1719
1720
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001721 /* This function modify the sample of the first pattern that match the <key>. */
1722static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001723 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001724{
1725 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001726 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001727 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001728 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001729
1730 /* Try all needed converters. */
1731 list_for_each_entry(expr, &ref->pat, list) {
1732 if (!expr->pat_head->parse_smp)
1733 continue;
1734
1735 if (!expr->pat_head->parse_smp(value, &test)) {
1736 memprintf(err, "unable to parse '%s'", value);
1737 return 0;
1738 }
1739 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001740
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001741 /* Modify pattern from reference. */
1742 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001743 if (!sample) {
1744 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001745 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001746 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001747 /* Load sample in each reference. All the conversion are tested
1748 * below, normally these calls dosn't fail.
1749 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001750 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001751 if (!expr->pat_head->parse_smp)
1752 continue;
1753
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001754 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001755 data = pattern_find_smp(expr, elt);
1756 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1757 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001758 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001759 }
1760
Emeric Brunb5997f72017-07-03 11:34:05 +02001761 /* free old sample only when all exprs are updated */
1762 free(elt->sample);
1763 elt->sample = sample;
1764
1765
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001766 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001767}
1768
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001769/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001770int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001771{
1772 struct pat_ref_elt *elt;
1773
1774 /* Look for pattern in the reference. */
1775 list_for_each_entry(elt, &ref->head, list) {
1776 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001777 if (!pat_ref_set_elt(ref, elt, value, err))
1778 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001779 return 1;
1780 }
1781 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001782
1783 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001784 return 0;
1785}
1786
1787/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001788int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001789{
1790 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001791 int found = 0;
1792 char *_merr;
1793 char **merr;
1794
1795 if (err) {
1796 merr = &_merr;
1797 *merr = NULL;
1798 }
1799 else
1800 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001801
1802 /* Look for pattern in the reference. */
1803 list_for_each_entry(elt, &ref->head, list) {
1804 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001805 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001806 if (err && merr) {
1807 if (!found) {
1808 *err = *merr;
1809 } else {
1810 memprintf(err, "%s, %s", *err, *merr);
1811 free(*merr);
1812 *merr = NULL;
1813 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001814 }
1815 }
1816 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001817 }
1818 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001819
1820 if (!found) {
1821 memprintf(err, "entry not found");
1822 return 0;
1823 }
1824 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001825}
1826
Joseph Herlant4189d672018-11-15 10:22:31 -08001827/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001828 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1829 * be unique. The user must check the reference with "pat_ref_lookup()"
Joseph Herlant4189d672018-11-15 10:22:31 -08001830 * before calling this function. If the function fail, it return NULL,
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001831 * else return new struct pat_ref.
1832 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001833struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001834{
1835 struct pat_ref *ref;
1836
1837 ref = malloc(sizeof(*ref));
1838 if (!ref)
1839 return NULL;
1840
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001841 if (display) {
1842 ref->display = strdup(display);
1843 if (!ref->display) {
1844 free(ref);
1845 return NULL;
1846 }
1847 }
1848 else
1849 ref->display = NULL;
1850
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001851 ref->reference = strdup(reference);
1852 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001853 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001854 free(ref);
1855 return NULL;
1856 }
1857
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001858 ref->flags = flags;
1859 ref->unique_id = -1;
1860
1861 LIST_INIT(&ref->head);
1862 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001863 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001864 LIST_ADDQ(&pattern_reference, &ref->list);
1865
1866 return ref;
1867}
1868
1869/* This function create new reference. <unique_id> is the unique id. If
1870 * the value of <unique_id> is -1, the unique id is calculated later.
1871 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1872 * be unique. The user must check the reference with "pat_ref_lookup()"
1873 * or pat_ref_lookupid before calling this function. If the function
1874 * fail, it return NULL, else return new struct pat_ref.
1875 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001876struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001877{
1878 struct pat_ref *ref;
1879
1880 ref = malloc(sizeof(*ref));
1881 if (!ref)
1882 return NULL;
1883
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001884 if (display) {
1885 ref->display = strdup(display);
1886 if (!ref->display) {
1887 free(ref);
1888 return NULL;
1889 }
1890 }
1891 else
1892 ref->display = NULL;
1893
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001894 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001895 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001896 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001897 LIST_INIT(&ref->head);
1898 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001899 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001900 LIST_ADDQ(&pattern_reference, &ref->list);
1901
1902 return ref;
1903}
1904
1905/* This function adds entry to <ref>. It can failed with memory error.
1906 * If the function fails, it returns 0.
1907 */
1908int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1909{
1910 struct pat_ref_elt *elt;
1911
1912 elt = malloc(sizeof(*elt));
1913 if (!elt)
1914 return 0;
1915
1916 elt->line = line;
1917
1918 elt->pattern = strdup(pattern);
1919 if (!elt->pattern) {
1920 free(elt);
1921 return 0;
1922 }
1923
1924 if (sample) {
1925 elt->sample = strdup(sample);
1926 if (!elt->sample) {
1927 free(elt->pattern);
1928 free(elt);
1929 return 0;
1930 }
1931 }
1932 else
1933 elt->sample = NULL;
1934
Emeric Brun8d85aa42017-06-29 15:40:33 +02001935 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001936 LIST_ADDQ(&ref->head, &elt->list);
1937
1938 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001939}
1940
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001941/* This function create sample found in <elt>, parse the pattern also
1942 * found in <elt> and insert it in <expr>. The function copy <patflags>
1943 * in <expr>. If the function fails, it returns0 and <err> is filled.
1944 * In succes case, the function returns 1.
1945 */
1946static inline
1947int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1948 int patflags, char **err)
1949{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001950 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001951 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001952
1953 /* Create sample */
1954 if (elt->sample && expr->pat_head->parse_smp) {
1955 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001956 data = malloc(sizeof(*data));
1957 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001958 return 0;
1959
1960 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001961 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001962 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001963 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001964 return 0;
1965 }
1966
1967 }
1968 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001969 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001970
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001971 /* initialise pattern */
1972 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001973 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001974 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001975
1976 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001977 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001978 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001979 return 0;
1980 }
1981
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001982 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001983 /* index pattern */
1984 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001985 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001986 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001987 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001988 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001990
1991 return 1;
1992}
1993
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001994/* This function adds entry to <ref>. It can failed with memory error. The new
1995 * entry is added at all the pattern_expr registered in this reference. The
1996 * function stop on the first error encountered. It returns 0 and err is
1997 * filled. If an error is encountered, the complete add operation is cancelled.
1998 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001999 */
2000int pat_ref_add(struct pat_ref *ref,
2001 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002002 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002003{
2004 struct pat_ref_elt *elt;
2005 struct pattern_expr *expr;
2006
2007 elt = malloc(sizeof(*elt));
2008 if (!elt) {
2009 memprintf(err, "out of memory error");
2010 return 0;
2011 }
2012
2013 elt->line = -1;
2014
2015 elt->pattern = strdup(pattern);
2016 if (!elt->pattern) {
2017 free(elt);
2018 memprintf(err, "out of memory error");
2019 return 0;
2020 }
2021
2022 if (sample) {
2023 elt->sample = strdup(sample);
2024 if (!elt->sample) {
2025 free(elt->pattern);
2026 free(elt);
2027 memprintf(err, "out of memory error");
2028 return 0;
2029 }
2030 }
2031 else
2032 elt->sample = NULL;
2033
Emeric Brun8d85aa42017-06-29 15:40:33 +02002034 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002035 LIST_ADDQ(&ref->head, &elt->list);
2036
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002037 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002038 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002039 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002040 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002041 return 0;
2042 }
2043 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002044
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 return 1;
2046}
2047
Joseph Herlant4189d672018-11-15 10:22:31 -08002048/* This function prunes <ref>, replaces all references by the references
2049 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002050 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002051 * The patterns are loaded in best effort and the errors are ignored,
2052 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002053 */
2054void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2055{
2056 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002057 struct pat_ref_elt *elt, *safe;
2058 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002059 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002060
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002061
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002062 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002063 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002064 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002065 }
2066
2067 /* all expr are locked, we can safely remove all pat_ref */
2068 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2069 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2070 /*
2071 * we have to unlink all watchers. We must not relink them if
2072 * this elt was the last one in the list.
2073 */
2074 LIST_DEL(&bref->users);
2075 LIST_INIT(&bref->users);
2076 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002077 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brunb5997f72017-07-03 11:34:05 +02002078 bref->ref = elt->list.n;
2079 }
2080 LIST_DEL(&elt->list);
2081 free(elt->pattern);
2082 free(elt->sample);
2083 free(elt);
2084 }
2085
2086 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002087 LIST_ADD(&replace->head, &ref->head);
2088 LIST_DEL(&replace->head);
2089
Emeric Brunb5997f72017-07-03 11:34:05 +02002090 list_for_each_entry(expr, &ref->pat, list) {
2091 expr->pat_head->prune(expr);
2092 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002093 char *err = NULL;
2094 struct sample_data *data = NULL;
2095
Emeric Brunb5997f72017-07-03 11:34:05 +02002096 /* Create sample */
2097 if (elt->sample && expr->pat_head->parse_smp) {
2098 /* New sample. */
2099 data = malloc(sizeof(*data));
2100 if (!data)
2101 continue;
2102
2103 /* Parse value. */
2104 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2105 memprintf(&err, "unable to parse '%s'", elt->sample);
2106 send_log(NULL, LOG_NOTICE, "%s", err);
2107 free(err);
2108 free(data);
2109 continue;
2110 }
2111
2112 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002113
2114 /* initialise pattern */
2115 memset(&pattern, 0, sizeof(pattern));
2116 pattern.data = data;
2117 pattern.ref = elt;
2118
2119 /* parse pattern */
2120 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002121 send_log(NULL, LOG_NOTICE, "%s", err);
2122 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002123 free(data);
2124 continue;
2125 }
2126
2127 /* index pattern */
2128 if (!expr->pat_head->index(expr, &pattern, &err)) {
2129 send_log(NULL, LOG_NOTICE, "%s", err);
2130 free(err);
2131 free(data);
2132 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002133 }
2134 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002135 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002136 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002137 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002138}
2139
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002140/* This function prune all entries of <ref>. This function
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002141 * prunes the associated pattern_expr. It may return before the end of
2142 * the list is reached, returning 0, to yield. The caller must call it
2143 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144 */
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002145int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002146{
2147 struct pat_ref_elt *elt, *safe;
2148 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002149 struct bref *bref, *back;
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002150 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151
Emeric Brunb5997f72017-07-03 11:34:05 +02002152 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002153 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002154 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002155 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002156 loops++;
2157 /* yield often, some lists may be huge, especially those
2158 * having to be freed through free_pattern_tree()
2159 */
2160 if (loops > 10)
2161 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002162 }
2163
2164 /* we trash pat_ref_elt in a second time to ensure that data is
2165 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002166 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002167 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2168 /*
2169 * we have to unlink all watchers. We must not relink them if
2170 * this elt was the last one in the list.
2171 */
2172 LIST_DEL(&bref->users);
2173 LIST_INIT(&bref->users);
2174 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002175 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002176 bref->ref = elt->list.n;
2177 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002178 LIST_DEL(&elt->list);
2179 free(elt->pattern);
2180 free(elt->sample);
2181 free(elt);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002182 loops++;
2183 if (loops > 100000)
2184 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 }
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002186 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002187}
2188
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189/* This function lookup for existing reference <ref> in pattern_head <head>. */
2190struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2191{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002192 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002193
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002194 list_for_each_entry(expr, &head->head, list)
2195 if (expr->expr->ref == ref)
2196 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002197 return NULL;
2198}
2199
Joseph Herlant4189d672018-11-15 10:22:31 -08002200/* This function creates new pattern_expr associated to the reference <ref>.
2201 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2203 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002204 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002205 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002206 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002207 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002208struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002209 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002210{
2211 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002212 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002213
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002214 if (reuse)
2215 *reuse = 0;
2216
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002217 /* Memory and initialization of the chain element. */
2218 list = malloc(sizeof(*list));
2219 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002220 memprintf(err, "out of memory");
2221 return NULL;
2222 }
2223
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002224 /* Look for existing similar expr. No that only the index, parse and
2225 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002226 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002227 */
2228 if (ref) {
2229 list_for_each_entry(expr, &ref->pat, list)
2230 if (expr->pat_head->index == head->index &&
2231 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002232 expr->pat_head->parse_smp == head->parse_smp &&
2233 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002234 break;
2235 if (&expr->list == &ref->pat)
2236 expr = NULL;
2237 }
2238 else
2239 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002240
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002241 /* If no similar expr was found, we create new expr. */
2242 if (!expr) {
2243 /* Get a lot of memory for the expr struct. */
2244 expr = malloc(sizeof(*expr));
2245 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002246 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002247 memprintf(err, "out of memory");
2248 return NULL;
2249 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002250
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002251 /* Initialize this new expr. */
2252 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002253
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002254 /* Copy the pattern matching and indexing flags. */
2255 expr->mflags = patflags;
2256
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002257 /* This new pattern expression reference one of his heads. */
2258 expr->pat_head = head;
2259
2260 /* Link with ref, or to self to facilitate LIST_DEL() */
2261 if (ref)
2262 LIST_ADDQ(&ref->pat, &expr->list);
2263 else
2264 LIST_INIT(&expr->list);
2265
2266 expr->ref = ref;
2267
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002268 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002269
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002270 /* We must free this pattern if it is no more used. */
2271 list->do_free = 1;
2272 }
2273 else {
2274 /* If the pattern used already exists, it is already linked
2275 * with ref and we must not free it.
2276 */
2277 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002278 if (reuse)
2279 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002280 }
2281
2282 /* The new list element reference the pattern_expr. */
2283 list->expr = expr;
2284
2285 /* Link the list element with the pattern_head. */
2286 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002287 return expr;
2288}
2289
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002290/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2291 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002292 *
2293 * The file contains one key + value per line. Lines which start with '#' are
2294 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2295 * then the first "word" (series of non-space/tabs characters), and the value is
2296 * what follows this series of space/tab till the end of the line excluding
2297 * trailing spaces/tabs.
2298 *
2299 * Example :
2300 *
2301 * # this is a comment and is ignored
2302 * 62.212.114.60 1wt.eu \n
2303 * <-><-----------><---><----><---->
2304 * | | | | `--- trailing spaces ignored
2305 * | | | `-------- value
2306 * | | `--------------- middle spaces ignored
2307 * | `------------------------ key
2308 * `-------------------------------- leading spaces ignored
2309 *
2310 * Return non-zero in case of succes, otherwise 0.
2311 */
2312int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2313{
2314 FILE *file;
2315 char *c;
2316 int ret = 0;
2317 int line = 0;
2318 char *key_beg;
2319 char *key_end;
2320 char *value_beg;
2321 char *value_end;
2322
2323 file = fopen(filename, "r");
2324 if (!file) {
2325 memprintf(err, "failed to open pattern file <%s>", filename);
2326 return 0;
2327 }
2328
2329 /* now parse all patterns. The file may contain only one pattern
2330 * followed by one value per line. The start spaces, separator spaces
2331 * and and spaces are stripped. Each can contain comment started by '#'
2332 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002334 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002336
2337 /* ignore lines beginning with a dash */
2338 if (*c == '#')
2339 continue;
2340
2341 /* strip leading spaces and tabs */
2342 while (*c == ' ' || *c == '\t')
2343 c++;
2344
2345 /* empty lines are ignored too */
2346 if (*c == '\0' || *c == '\r' || *c == '\n')
2347 continue;
2348
2349 /* look for the end of the key */
2350 key_beg = c;
2351 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2352 c++;
2353
2354 key_end = c;
2355
2356 /* strip middle spaces and tabs */
2357 while (*c == ' ' || *c == '\t')
2358 c++;
2359
2360 /* look for the end of the value, it is the end of the line */
2361 value_beg = c;
2362 while (*c && *c != '\n' && *c != '\r')
2363 c++;
2364 value_end = c;
2365
2366 /* trim possibly trailing spaces and tabs */
2367 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2368 value_end--;
2369
2370 /* set final \0 and check entries */
2371 *key_end = '\0';
2372 *value_end = '\0';
2373
2374 /* insert values */
2375 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2376 memprintf(err, "out of memory");
2377 goto out_close;
2378 }
2379 }
2380
Jerome Magnin8991ce12020-01-17 16:09:33 +01002381 if (ferror(file)) {
2382 memprintf(err, "error encountered while reading <%s> : %s",
2383 filename, strerror(errno));
2384 goto out_close;
2385 }
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002386 /* succes */
2387 ret = 1;
2388
2389 out_close:
2390 fclose(file);
2391 return ret;
2392}
2393
2394/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2395 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002396 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002397int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002398{
2399 FILE *file;
2400 char *c;
2401 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002402 int ret = 0;
2403 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002404
2405 file = fopen(filename, "r");
2406 if (!file) {
2407 memprintf(err, "failed to open pattern file <%s>", filename);
2408 return 0;
2409 }
2410
2411 /* now parse all patterns. The file may contain only one pattern per
2412 * line. If the line contains spaces, they will be part of the pattern.
2413 * The pattern stops at the first CR, LF or EOF encountered.
2414 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002415 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002416 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002417 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002418
2419 /* ignore lines beginning with a dash */
2420 if (*c == '#')
2421 continue;
2422
2423 /* strip leading spaces and tabs */
2424 while (*c == ' ' || *c == '\t')
2425 c++;
2426
2427
2428 arg = c;
2429 while (*c && *c != '\n' && *c != '\r')
2430 c++;
2431 *c = 0;
2432
2433 /* empty lines are ignored too */
2434 if (c == arg)
2435 continue;
2436
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002437 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002438 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2439 goto out_close;
2440 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002441 }
2442
Jerome Magnin8991ce12020-01-17 16:09:33 +01002443 if (ferror(file)) {
2444 memprintf(err, "error encountered while reading <%s> : %s",
2445 filename, strerror(errno));
2446 goto out_close;
2447 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002448 ret = 1; /* success */
2449
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002450 out_close:
2451 fclose(file);
2452 return ret;
2453}
2454
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002455int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002456 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002457 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002458{
2459 struct pat_ref *ref;
2460 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002461 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002462 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002463
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002464 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002465 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002466
2467 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002468 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002469 chunk_printf(&trash,
2470 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2471 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2472
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002473 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002474 if (!ref) {
2475 memprintf(err, "out of memory");
2476 return 0;
2477 }
2478
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002479 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002480 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002481 if (!pat_ref_read_from_file_smp(ref, filename, err))
2482 return 0;
2483 }
2484 else {
2485 if (!pat_ref_read_from_file(ref, filename, err))
2486 return 0;
2487 }
2488 }
2489 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002490 /* The reference already exists, check the map compatibility. */
2491
2492 /* If the load require samples and the flag PAT_REF_SMP is not set,
2493 * the reference doesn't contain sample, and cannot be used.
2494 */
2495 if (load_smp) {
2496 if (!(ref->flags & PAT_REF_SMP)) {
2497 memprintf(err, "The file \"%s\" is already used as one column file "
2498 "and cannot be used by as two column file.",
2499 filename);
2500 return 0;
2501 }
2502 }
2503 else {
2504 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2505 * set, the reference contains a sample, and cannot be used.
2506 */
2507 if (ref->flags & PAT_REF_SMP) {
2508 memprintf(err, "The file \"%s\" is already used as two column file "
2509 "and cannot be used by as one column file.",
2510 filename);
2511 return 0;
2512 }
2513 }
2514
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002515 /* Extends display */
2516 chunk_printf(&trash, "%s", ref->display);
2517 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2518 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2519 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002520 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002521 if (!ref->display) {
2522 memprintf(err, "out of memory");
2523 return 0;
2524 }
2525
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002526 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002527 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002528 }
2529
2530 /* Now, we can loading patterns from the reference. */
2531
2532 /* Lookup for existing reference in the head. If the reference
2533 * doesn't exists, create it.
2534 */
2535 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002536 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002537 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002538 if (!expr)
2539 return 0;
2540 }
2541
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002542 /* The returned expression may be not empty, because the function
2543 * "pattern_new_expr" lookup for similar pattern list and can
2544 * reuse a already filled pattern list. In this case, we can not
2545 * reload the patterns.
2546 */
2547 if (reuse)
2548 return 1;
2549
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002550 /* Load reference content in the pattern expression. */
2551 list_for_each_entry(elt, &ref->head, list) {
2552 if (!pat_ref_push(elt, expr, patflags, err)) {
2553 if (elt->line > 0)
2554 memprintf(err, "%s at line %d of file '%s'",
2555 *err, elt->line, filename);
2556 return 0;
2557 }
2558 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002559
2560 return 1;
2561}
2562
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002563/* This function executes a pattern match on a sample. It applies pattern <expr>
2564 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2565 * non-null if the sample match. If <fill> is true and the sample match, the
2566 * function returns the matched pattern. In many cases, this pattern can be a
2567 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002568 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002569struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002570{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002571 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002572 struct pattern *pat;
2573
2574 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002575 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002576 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002577 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002578 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002579 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002580 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002581 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002582 return &static_pattern;
2583 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002584
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002585 /* convert input to string */
2586 if (!sample_convert(smp, head->expect_type))
2587 return NULL;
2588
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002589 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002590 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002591 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002592 if (pat) {
2593 /* We duplicate the pattern cause it could be modified
2594 by another thread */
2595 if (pat != &static_pattern) {
2596 memcpy(&static_pattern, pat, sizeof(struct pattern));
2597 pat = &static_pattern;
2598 }
2599
2600 /* We also duplicate the sample data for
2601 same reason */
2602 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002603 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002604 case SMP_T_STR:
2605 static_sample_data.type = SMP_T_STR;
2606 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 static_sample_data.u.str.data = pat->data->u.str.data;
2608 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2609 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2610 memcpy(static_sample_data.u.str.area,
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002611 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002612 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002613 pat->data = &static_sample_data;
2614 break;
2615
Emeric Brunb5997f72017-07-03 11:34:05 +02002616 case SMP_T_IPV4:
2617 case SMP_T_IPV6:
2618 case SMP_T_SINT:
2619 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002620 pat->data = &static_sample_data;
2621 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002622 default:
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002623 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002624 pat->data = NULL;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002625 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002626 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002627 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002628 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002629 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002630 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002631 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002632 }
2633 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002634}
2635
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002636/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002637void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002638{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002639 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002640
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002641 list_for_each_entry_safe(list, safe, &head->head, list) {
2642 LIST_DEL(&list->list);
2643 if (list->do_free) {
2644 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002645 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002646 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002647 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002648 free(list->expr);
2649 }
2650 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002651 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002652}
2653
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002654/* This function lookup for a pattern matching the <key> and return a
2655 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2656 * the function returns NULL. If the key cannot be parsed, the function
2657 * fill <err>.
2658 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002659struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002660{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002661 struct ebmb_node *node;
2662 struct pattern_tree *elt;
2663 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002664
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002665 for (node = ebmb_first(&expr->pattern_tree);
2666 node;
2667 node = ebmb_next(node)) {
2668 elt = container_of(node, struct pattern_tree, node);
2669 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002670 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002671 }
2672
2673 for (node = ebmb_first(&expr->pattern_tree_2);
2674 node;
2675 node = ebmb_next(node)) {
2676 elt = container_of(node, struct pattern_tree, node);
2677 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002678 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002679 }
2680
2681 list_for_each_entry(pat, &expr->patterns, list)
2682 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002683 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002684
2685 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002686}
2687
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002688/* This function search all the pattern matching the <key> and delete it.
2689 * If the parsing of the input key fails, the function returns 0 and the
2690 * <err> is filled, else return 1;
2691 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002692int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002693{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002694 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002695 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002696 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002697 return 1;
2698}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002699
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002700/* This function compares two pat_ref** on unique_id */
2701static int cmp_pat_ref(const void *_a, const void *_b)
2702{
2703 struct pat_ref * const *a = _a;
2704 struct pat_ref * const *b = _b;
2705
2706 if ((*a)->unique_id < (*b)->unique_id)
2707 return -1;
2708 else if ((*a)->unique_id > (*b)->unique_id)
2709 return 1;
2710 return 0;
2711}
2712
2713/* This function finalize the configuration parsing. It sets all the
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002714 * automatic ids
2715 */
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002716int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002717{
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002718 size_t len = 0;
2719 size_t unassigned_pos = 0;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002720 int next_unique_id = 0;
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002721 size_t i, j;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002722 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002723 struct list pr = LIST_HEAD_INIT(pr);
2724
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002725 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002726
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002727 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002728 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002729 len++;
2730 if (ref->unique_id != -1)
2731 unassigned_pos++;
2732 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002733
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002734 if (len == 0) {
2735 return 0;
2736 }
2737
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002738 arr = calloc(len, sizeof(*arr));
2739 if (arr == NULL) {
2740 ha_alert("Out of memory error.\n");
2741 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002742 }
2743
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002744 i = 0;
2745 j = unassigned_pos;
2746 list_for_each_entry(ref, &pattern_reference, list) {
2747 if (ref->unique_id != -1)
2748 arr[i++] = ref;
2749 else
2750 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002751 }
2752
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002753 /* Sort first segment of array with user-defined unique ids for
2754 * fast lookup when generating unique ids
2755 */
2756 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2757
2758 /* Assign unique ids to the rest of the elements */
2759 for (i = unassigned_pos; i < len; i++) {
2760 do {
2761 arr[i]->unique_id = next_unique_id++;
2762 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2763 }
2764
2765 /* Sort complete array */
2766 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2767
2768 /* Convert back to linked list */
2769 for (i = 0; i < len; i++)
2770 LIST_ADDQ(&pr, &arr[i]->list);
2771
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002772 /* swap root */
2773 LIST_ADD(&pr, &pattern_reference);
2774 LIST_DEL(&pr);
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002775
2776 free(arr);
2777 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002778}
Willy Tarreau7fdd81c2019-10-23 06:59:31 +02002779
2780static int pattern_per_thread_lru_alloc()
2781{
2782 if (!global.tune.pattern_cache)
2783 return 1;
2784 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2785 return !!pat_lru_tree;
2786}
2787
2788static void pattern_per_thread_lru_free()
2789{
2790 lru64_destroy(pat_lru_tree);
2791}
2792
2793REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2794REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);