blob: 88e760cc052f70514d00e5b3f289577c7aadd5fe [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 Magninb8bd6d72020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <import/ebsttree.h>
18#include <import/lru.h>
19#include <import/xxhash.h>
20
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020022#include <haproxy/global.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020023#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020024#include <haproxy/net_helper.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020025#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020026#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020027#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/tools.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
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 FOURNIER6f7203d2014-01-14 16:24:51 +010082void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
Willy Tarreau6d8a6892020-11-02 19:26:02 +010083 [PAT_MATCH_FOUND] = pat_prune_gen,
84 [PAT_MATCH_BOOL] = pat_prune_gen,
85 [PAT_MATCH_INT] = pat_prune_gen,
86 [PAT_MATCH_IP] = pat_prune_gen,
87 [PAT_MATCH_BIN] = pat_prune_gen,
88 [PAT_MATCH_LEN] = pat_prune_gen,
89 [PAT_MATCH_STR] = pat_prune_gen,
90 [PAT_MATCH_BEG] = pat_prune_gen,
91 [PAT_MATCH_SUB] = pat_prune_gen,
92 [PAT_MATCH_DIR] = pat_prune_gen,
93 [PAT_MATCH_DOM] = pat_prune_gen,
94 [PAT_MATCH_END] = pat_prune_gen,
95 [PAT_MATCH_REG] = pat_prune_gen,
96 [PAT_MATCH_REGM] = pat_prune_gen,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010097};
98
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010099struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100100 [PAT_MATCH_FOUND] = NULL,
101 [PAT_MATCH_BOOL] = pat_match_nothing,
102 [PAT_MATCH_INT] = pat_match_int,
103 [PAT_MATCH_IP] = pat_match_ip,
104 [PAT_MATCH_BIN] = pat_match_bin,
105 [PAT_MATCH_LEN] = pat_match_len,
106 [PAT_MATCH_STR] = pat_match_str,
107 [PAT_MATCH_BEG] = pat_match_beg,
108 [PAT_MATCH_SUB] = pat_match_sub,
109 [PAT_MATCH_DIR] = pat_match_dir,
110 [PAT_MATCH_DOM] = pat_match_dom,
111 [PAT_MATCH_END] = pat_match_end,
112 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100113 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100114};
115
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100116/* Just used for checking configuration compatibility */
117int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200118 [PAT_MATCH_FOUND] = SMP_T_SINT,
119 [PAT_MATCH_BOOL] = SMP_T_SINT,
120 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100121 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100122 [PAT_MATCH_BIN] = SMP_T_BIN,
123 [PAT_MATCH_LEN] = SMP_T_STR,
124 [PAT_MATCH_STR] = SMP_T_STR,
125 [PAT_MATCH_BEG] = SMP_T_STR,
126 [PAT_MATCH_SUB] = SMP_T_STR,
127 [PAT_MATCH_DIR] = SMP_T_STR,
128 [PAT_MATCH_DOM] = SMP_T_STR,
129 [PAT_MATCH_END] = SMP_T_STR,
130 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100131 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132};
133
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100134/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200135static THREAD_LOCAL struct pattern static_pattern;
136static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100137
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100138/* This is the root of the list of all pattern_ref avalaibles. */
139struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
140
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200141static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200142static unsigned long long pat_lru_seed;
143
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100145 *
146 * The following functions are not exported and are used by internals process
147 * of pattern matching
148 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100149 */
150
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100151/* Background: Fast way to find a zero byte in a word
152 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
153 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
154 *
155 * To look for 4 different byte values, xor the word with those bytes and
156 * then check for zero bytes:
157 *
158 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
159 * where <delimiter> is the 4 byte values to look for (as an uint)
160 * and <c> is the character that is being tested
161 */
162static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
163{
164 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
165 return (mask - 0x01010101) & ~mask & 0x80808080U;
166}
167
168static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
169{
170 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
171}
172
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100174/*
175 *
176 * These functions are exported and may be used by any other component.
177 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200178 * The following functions are used for parsing pattern matching input value.
179 * The <text> contain the string to be parsed. <pattern> must be a preallocated
180 * pattern. The pat_parse_* functions fill this structure with the parsed value.
181 * <err> is filled with an error message built with memprintf() function. It is
182 * allowed to use a trash as a temporary storage for the returned pattern, as
183 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100184 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200185 * In success case, the pat_parse_* function returns 1. If the function
186 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100187 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200190int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191{
192 return 1;
193}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100194
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200196int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100197{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100198 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100199 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100200 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202}
203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206{
Willy Tarreau83061a82018-07-13 11:56:34 +0200207 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100208
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100209 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210 trash = get_trash_chunk();
211 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100213 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214}
215
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100216/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200217int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100218{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100219 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220 return 1;
221}
222
223/* Parse a range of positive integers delimited by either ':' or '-'. If only
224 * one integer is read, it is set as both min and max. An operator may be
225 * specified as the prefix, among this list of 5 :
226 *
227 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
228 *
229 * The default operator is "eq". It supports range matching. Ranges are
230 * rejected for other operators. The operator may be changed at any time.
231 * The operator is stored in the 'opaque' argument.
232 *
233 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100234 * the caller will have to free it. The function returns zero on error, and
235 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236 *
237 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200238int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100240 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100241
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200242 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100243
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100244 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100245 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100246 goto not_valid_range;
247
248 /* Search ':' or '-' separator. */
249 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
250 ptr++;
251
252 /* If separator not found. */
253 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100254 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
255 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256 return 0;
257 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100258 pattern->val.range.max = pattern->val.range.min;
259 pattern->val.range.min_set = 1;
260 pattern->val.range.max_set = 1;
261 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100262 }
263
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100264 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100265 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100266 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
267 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100268
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100269 pattern->val.range.min_set = 0;
270 pattern->val.range.max_set = 1;
271 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 }
273
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 /* If separator is the last character. */
275 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100276 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 pattern->val.range.min_set = 1;
280 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283
284 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100285 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 goto not_valid_range;
287
288 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
289 goto not_valid_range;
290
291 if (pattern->val.range.min > pattern->val.range.max)
292 goto not_valid_range;
293
294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 1;
296 return 1;
297
298 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100299 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100301}
302
303/* Parse a range of positive 2-component versions delimited by either ':' or
304 * '-'. The version consists in a major and a minor, both of which must be
305 * smaller than 65536, because internally they will be represented as a 32-bit
306 * integer.
307 * If only one version is read, it is set as both min and max. Just like for
308 * pure integers, an operator may be specified as the prefix, among this list
309 * of 5 :
310 *
311 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
312 *
313 * The default operator is "eq". It supports range matching. Ranges are
314 * rejected for other operators. The operator may be changed at any time.
315 * The operator is stored in the 'opaque' argument. This allows constructs
316 * such as the following one :
317 *
318 * acl obsolete_ssl ssl_req_proto lt 3
319 * acl unsupported_ssl ssl_req_proto gt 3.1
320 * acl valid_ssl ssl_req_proto 3.0-3.1
321 *
322 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200323int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100324{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100325 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100326
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200327 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100328
329 /* Search ':' or '-' separator. */
330 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
331 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100332
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100333 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100334 if (*ptr == '\0' && ptr > text) {
335 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
336 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100337 return 0;
338 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100339 pattern->val.range.max = pattern->val.range.min;
340 pattern->val.range.min_set = 1;
341 pattern->val.range.max_set = 1;
342 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343 }
344
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100346 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100347 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100348 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100349 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100350 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100351 pattern->val.range.min_set = 0;
352 pattern->val.range.max_set = 1;
353 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 }
355
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100357 if (ptr == &text[strlen(text)-1]) {
358 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
359 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 return 0;
361 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100362 pattern->val.range.min_set = 1;
363 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 1;
365 }
366
367 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100368 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
369 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100370 return 0;
371 }
372 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100373 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100374 return 0;
375 }
376 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100377 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 pattern->val.range.min_set = 1;
381 pattern->val.range.max_set = 1;
382 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100383}
384
385/* Parse an IP address and an optional mask in the form addr[/mask].
386 * The addr may either be an IPv4 address or a hostname. The mask
387 * may either be a dotted mask or a number of bits. Returns 1 if OK,
388 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
389 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200390int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100391{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200392 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100393 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 return 1;
396 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100397 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 pattern->type = SMP_T_IPV6;
399 return 1;
400 }
401 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100402 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 return 0;
404 }
405}
406
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100407/*
408 *
409 * These functions are exported and may be used by any other component.
410 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800411 * This function just takes a sample <smp> and checks if this sample matches
412 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100413 * PAT_NOMATCH.
414 *
415 */
416
417/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100418struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100419{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200420 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100421 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200422 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100423 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100424 static_pattern.type = 0;
425 static_pattern.ptr.str = NULL;
426 }
427 return &static_pattern;
428 }
429 else
430 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100431}
432
433
Joseph Herlant4189d672018-11-15 10:22:31 -0800434/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
437 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100438 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100439 struct pattern_tree *elt;
440 struct pattern_list *lst;
441 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200442 struct pattern *ret = NULL;
443 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100444
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100445 /* Lookup a string in the expression's pattern tree. */
446 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200447 char prev = 0;
448
449 if (smp->data.u.str.data < smp->data.u.str.size) {
450 /* we may have to force a trailing zero on the test pattern and
451 * the buffer is large enough to accommodate it.
452 */
453 prev = smp->data.u.str.area[smp->data.u.str.data];
454 if (prev)
455 smp->data.u.str.area[smp->data.u.str.data] = '\0';
456 }
457 else {
458 /* Otherwise, the sample is duplicated. A trailing zero
459 * is automatically added to the string.
460 */
461 if (!smp_dup(smp))
462 return NULL;
463 }
464
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100466 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreauc93da692020-10-29 09:41:34 +0100468
469 while (node) {
470 elt = ebmb_entry(node, struct pattern_tree, node);
471 if (elt->ref->gen_id != expr->ref->curr_gen) {
472 node = ebmb_next(node);
473 continue;
474 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100475 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200476 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100477 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200478 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100479 static_pattern.type = SMP_T_STR;
480 static_pattern.ptr.str = (char *)elt->node.key;
481 }
482 return &static_pattern;
483 }
484 }
485
486 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200488 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200490 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100491 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200492 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200493 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200494 return ret;
495 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200496 }
497
Emeric Brunb5997f72017-07-03 11:34:05 +0200498
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100499 list_for_each_entry(lst, &expr->patterns, list) {
500 pattern = &lst->pat;
501
Willy Tarreauc93da692020-10-29 09:41:34 +0100502 if (pattern->ref->gen_id != expr->ref->curr_gen)
503 continue;
504
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 continue;
507
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200508 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
510 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200511 ret = pattern;
512 break;
513 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100514 }
515
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200516 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100517 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200518
519 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100520}
521
522/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100525 struct pattern_list *lst;
526 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200527 struct pattern *ret = NULL;
528 struct lru64 *lru = NULL;
529
530 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200531 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100532
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100534 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200535 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200536 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200537 return ret;
538 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200539 }
540
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 list_for_each_entry(lst, &expr->patterns, list) {
542 pattern = &lst->pat;
543
Willy Tarreauc93da692020-10-29 09:41:34 +0100544 if (pattern->ref->gen_id != expr->ref->curr_gen)
545 continue;
546
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 continue;
549
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200550 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200551 ret = pattern;
552 break;
553 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 }
555
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200556 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100557 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200558
559 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100560}
561
562/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100563 * and restores the previous character when leaving. This function fills
564 * a matching array.
565 */
566struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
567{
568 struct pattern_list *lst;
569 struct pattern *pattern;
570 struct pattern *ret = NULL;
571
572 list_for_each_entry(lst, &expr->patterns, list) {
573 pattern = &lst->pat;
574
Willy Tarreauc93da692020-10-29 09:41:34 +0100575 if (pattern->ref->gen_id != expr->ref->curr_gen)
576 continue;
577
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100579 MAX_MATCH, pmatch, 0)) {
580 ret = pattern;
581 smp->ctx.a[0] = pmatch;
582 break;
583 }
584 }
585
586 return ret;
587}
588
589/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100590 * and restores the previous character when leaving.
591 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100594 struct pattern_list *lst;
595 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200596 struct pattern *ret = NULL;
597 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100598
Willy Tarreauf3045d22015-04-29 16:24:50 +0200599 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200600 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200601
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100603 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200604 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200605 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200606 return ret;
607 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200608 }
609
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100610 list_for_each_entry(lst, &expr->patterns, list) {
611 pattern = &lst->pat;
612
Willy Tarreauc93da692020-10-29 09:41:34 +0100613 if (pattern->ref->gen_id != expr->ref->curr_gen)
614 continue;
615
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200617 ret = pattern;
618 break;
619 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200621
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200622 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100623 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624
625 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100626}
627
628/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100629struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100630{
631 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200632 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100634 struct pattern_list *lst;
635 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200636 struct pattern *ret = NULL;
637 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100638
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639 /* Lookup a string in the expression's pattern tree. */
640 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200641 char prev = 0;
642
643 if (smp->data.u.str.data < smp->data.u.str.size) {
644 /* we may have to force a trailing zero on the test pattern and
645 * the buffer is large enough to accommodate it.
646 */
647 prev = smp->data.u.str.area[smp->data.u.str.data];
648 if (prev)
649 smp->data.u.str.area[smp->data.u.str.data] = '\0';
650 }
651 else {
652 /* Otherwise, the sample is duplicated. A trailing zero
653 * is automatically added to the string.
654 */
655 if (!smp_dup(smp))
656 return NULL;
657 }
658
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 node = ebmb_lookup_longest(&expr->pattern_tree,
660 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200661 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200663
Willy Tarreauc93da692020-10-29 09:41:34 +0100664 while (node) {
665 elt = ebmb_entry(node, struct pattern_tree, node);
666 if (elt->ref->gen_id != expr->ref->curr_gen) {
667 node = ebmb_next(node);
668 continue;
669 }
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200670 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200671 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200672 static_pattern.ref = elt->ref;
673 static_pattern.sflags = PAT_SF_TREE;
674 static_pattern.type = SMP_T_STR;
675 static_pattern.ptr.str = (char *)elt->node.key;
676 }
677 return &static_pattern;
678 }
679 }
680
681 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200682 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200683 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200684
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100686 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200687 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200688 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200689 return ret;
690 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200691 }
692
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100693 list_for_each_entry(lst, &expr->patterns, list) {
694 pattern = &lst->pat;
695
Willy Tarreauc93da692020-10-29 09:41:34 +0100696 if (pattern->ref->gen_id != expr->ref->curr_gen)
697 continue;
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 Tarreau403bfbb2019-10-23 06:59:31 +0200711 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100712 lru64_commit(lru, ret, expr, expr->ref->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 Tarreau3ee0de12020-11-02 15:26:51 +0100730 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-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 Tarreauc93da692020-10-29 09:41:34 +0100740 if (pattern->ref->gen_id != expr->ref->curr_gen)
741 continue;
742
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 continue;
745
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200746 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
748 (!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 +0100749 continue;
750
Willy Tarreauf3045d22015-04-29 16:24:50 +0200751 ret = pattern;
752 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200754
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200755 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100756 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200757
758 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100759}
760
761/* Checks that the pattern is included inside the tested string.
762 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
763 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100764struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100765{
766 int icase;
767 char *end;
768 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 struct pattern_list *lst;
770 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200771 struct pattern *ret = NULL;
772 struct lru64 *lru = NULL;
773
774 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200775 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200776
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100778 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200779 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200780 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200781 return ret;
782 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200783 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100784
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100785 list_for_each_entry(lst, &expr->patterns, list) {
786 pattern = &lst->pat;
787
Willy Tarreauc93da692020-10-29 09:41:34 +0100788 if (pattern->ref->gen_id != expr->ref->curr_gen)
789 continue;
790
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200791 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100792 continue;
793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200795 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100796 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 for (c = smp->data.u.str.area; c <= end; c++) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200798 if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200800 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
801 ret = pattern;
802 goto leave;
803 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100804 }
805 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200806 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100807 if (*c != *pattern->ptr.str)
808 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200809 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
810 ret = pattern;
811 goto leave;
812 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100813 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100814 }
815 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200816 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200817 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100818 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200819
820 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100821}
822
823/* This one is used by other real functions. It checks that the pattern is
824 * included inside the tested string, but enclosed between the specified
825 * delimiters or at the beginning or end of the string. The delimiters are
826 * provided as an unsigned int made by make_4delim() and match up to 4 different
827 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
828 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200829static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100830{
831 int may_match, icase;
832 char *c, *end;
833 char *ps;
834 int pl;
835
836 pl = pattern->len;
837 ps = pattern->ptr.str;
838
839 while (pl > 0 && is_delimiter(*ps, delimiters)) {
840 pl--;
841 ps++;
842 }
843
844 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
845 pl--;
846
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100848 return PAT_NOMATCH;
849
850 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200851 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 end = smp->data.u.str.area + smp->data.u.str.data - pl;
853 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100854 if (is_delimiter(*c, delimiters)) {
855 may_match = 1;
856 continue;
857 }
858
859 if (!may_match)
860 continue;
861
862 if (icase) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200863 if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) &&
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100864 (strncasecmp(ps, c, pl) == 0) &&
865 (c == end || is_delimiter(c[pl], delimiters)))
866 return PAT_MATCH;
867 } else {
868 if ((*c == *ps) &&
869 (strncmp(ps, c, pl) == 0) &&
870 (c == end || is_delimiter(c[pl], delimiters)))
871 return PAT_MATCH;
872 }
873 may_match = 0;
874 }
875 return PAT_NOMATCH;
876}
877
878/* Checks that the pattern is included inside the tested string, but enclosed
879 * between the delimiters '?' or '/' or at the beginning or end of the string.
880 * Delimiters at the beginning or end of the pattern are ignored.
881 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100882struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100883{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 struct pattern_list *lst;
885 struct pattern *pattern;
886
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887 list_for_each_entry(lst, &expr->patterns, list) {
888 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100889
890 if (pattern->ref->gen_id != expr->ref->curr_gen)
891 continue;
892
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200893 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 return pattern;
895 }
896 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897}
898
899/* Checks that the pattern is included inside the tested string, but enclosed
900 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
901 * the string. Delimiters at the beginning or end of the pattern are ignored.
902 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100903struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100904{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 struct pattern_list *lst;
906 struct pattern *pattern;
907
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100908 list_for_each_entry(lst, &expr->patterns, list) {
909 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100910
911 if (pattern->ref->gen_id != expr->ref->curr_gen)
912 continue;
913
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200914 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100915 return pattern;
916 }
917 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100918}
919
920/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100921struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100922{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100923 struct pattern_list *lst;
924 struct pattern *pattern;
925
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100926 list_for_each_entry(lst, &expr->patterns, list) {
927 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100928
929 if (pattern->ref->gen_id != expr->ref->curr_gen)
930 continue;
931
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200932 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
933 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100934 return pattern;
935 }
936 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100937}
938
939/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100941{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100942 struct pattern_list *lst;
943 struct pattern *pattern;
944
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100945 list_for_each_entry(lst, &expr->patterns, list) {
946 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100947
948 if (pattern->ref->gen_id != expr->ref->curr_gen)
949 continue;
950
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200951 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
952 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100953 return pattern;
954 }
955 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956}
957
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100958struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100959{
960 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100961 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100962 struct in_addr *s;
963 struct ebmb_node *node;
964 struct pattern_tree *elt;
965 struct pattern_list *lst;
966 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100967
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100968 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200969 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100970 /* Lookup an IPv4 address in the expression's pattern tree using
971 * the longest match method.
972 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200973 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100974 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Willy Tarreauc93da692020-10-29 09:41:34 +0100975 while (node) {
976 elt = ebmb_entry(node, struct pattern_tree, node);
977 if (elt->ref->gen_id != expr->ref->curr_gen) {
978 node = ebmb_next(node);
979 continue;
980 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100981 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200982 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100983 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200984 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100985 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100986 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100987 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
988 return NULL;
989 }
990 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100991 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100992
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100993 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
994 * sample address to IPv6 with the mapping method using the ::ffff:
995 * prefix, and try to lookup in the IPv6 tree.
996 */
997 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +0100998 write_u16(&tmp6.s6_addr[10], htons(0xffff));
999 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001000 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001001 while (node) {
1002 elt = ebmb_entry(node, struct pattern_tree, node);
1003 if (elt->ref->gen_id != expr->ref->curr_gen) {
1004 node = ebmb_next(node);
1005 continue;
1006 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001007 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001008 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001009 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001010 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001011 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001012 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001013 static_pattern.val.ipv6.mask = elt->node.node.pfx;
1014 }
1015 return &static_pattern;
1016 }
1017 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001018
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001019 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001020 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001021 /* Lookup an IPv6 address in the expression's pattern tree using
1022 * the longest match method.
1023 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001024 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001025 while (node) {
1026 elt = ebmb_entry(node, struct pattern_tree, node);
1027 if (elt->ref->gen_id != expr->ref->curr_gen) {
1028 node = ebmb_next(node);
1029 continue;
1030 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001031 if (fill) {
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_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001036 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001037 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001038 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001039 return &static_pattern;
1040 }
1041
1042 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1043 * following forms :
1044 * - ::ffff:ip:v4 (ipv4 mapped)
1045 * - ::0000:ip:v4 (old ipv4 mapped)
1046 * - 2002:ip:v4:: (6to4)
1047 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001048 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1049 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1050 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
1051 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1052 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
1053 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001054 else
Willy Tarreau296cfd12020-02-25 09:58:41 +01001055 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1056 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001057
1058 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1059 * match method.
1060 */
1061 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
Willy Tarreauc93da692020-10-29 09:41:34 +01001062 while (node) {
1063 elt = ebmb_entry(node, struct pattern_tree, node);
1064 if (elt->ref->gen_id != expr->ref->curr_gen) {
1065 node = ebmb_next(node);
1066 continue;
1067 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001068 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001069 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001070 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001071 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001072 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001073 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001074 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1075 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001076 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001077 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001078 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001079 }
1080 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001081
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001082 /* Lookup in the list. the list contain only IPv4 patterns */
1083 list_for_each_entry(lst, &expr->patterns, list) {
1084 pattern = &lst->pat;
1085
Willy Tarreauc93da692020-10-29 09:41:34 +01001086 if (pattern->ref->gen_id != expr->ref->curr_gen)
1087 continue;
1088
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001089 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001090 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001091 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001092 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001093 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001094 /* v4 match on a V6 sample. We want to check at least for
1095 * the following forms :
1096 * - ::ffff:ip:v4 (ipv4 mapped)
1097 * - ::0000:ip:v4 (old ipv4 mapped)
1098 * - 2002:ip:v4:: (6to4)
1099 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001100 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1101 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1102 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1103 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001104 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001105 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1106 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1107 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001108 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001109 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001110 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001111 } else {
1112 /* impossible */
1113 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001114 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001115
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001116 /* Check if the input sample match the current pattern. */
1117 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001118 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001119 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001120 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001121}
1122
Willy Tarreau867a8a52020-11-03 11:22:04 +01001123/* finds the pattern holding <list> from list head <head> and deletes it.
1124 * This is made for use for pattern removal within an expression.
1125 */
Willy Tarreau38d41992020-11-03 14:50:29 +01001126static void pat_unlink_from_head(void **head, void **list)
Willy Tarreau867a8a52020-11-03 11:22:04 +01001127{
Willy Tarreau38d41992020-11-03 14:50:29 +01001128 while (*head) {
1129 if (*head == list) {
1130 *head = *list;
Willy Tarreau867a8a52020-11-03 11:22:04 +01001131 return;
1132 }
Willy Tarreau38d41992020-11-03 14:50:29 +01001133 head = *head;
Willy Tarreau867a8a52020-11-03 11:22:04 +01001134 }
1135}
1136
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001137void free_pattern_tree(struct eb_root *root)
1138{
1139 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001140 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001141
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001142 node = eb_first(root);
1143 while (node) {
1144 next = eb_next(node);
1145 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001146 elt = container_of(node, struct pattern_tree, node);
Willy Tarreau867a8a52020-11-03 11:22:04 +01001147 pat_unlink_from_head(&elt->ref->tree_head, &elt->from_ref);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001148 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001149 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001150 node = next;
1151 }
1152}
1153
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001154void pat_prune_gen(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001155{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001156 struct pattern_list *pat, *tmp;
1157
1158 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet6cfc8512020-09-09 16:09:44 +02001159 LIST_DEL(&pat->list);
Willy Tarreau867a8a52020-11-03 11:22:04 +01001160 pat_unlink_from_head(&pat->pat.ref->list_head, &pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001161 if (pat->pat.sflags & PAT_SF_REGFREE)
1162 regex_free(pat->pat.ptr.ptr);
1163 else
1164 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001165 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001166 free(pat);
1167 }
1168
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001169 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001170 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001171 LIST_INIT(&expr->patterns);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001172 expr->ref->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001173}
1174
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175/*
1176 *
1177 * The following functions are used for the pattern indexation
1178 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001179 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001180
1181int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001182{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001183 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001184
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001185 /* allocate pattern */
1186 patl = calloc(1, sizeof(*patl));
1187 if (!patl) {
1188 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001189 return 0;
1190 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001191
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001192 /* duplicate pattern */
1193 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001194
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001195 /* chain pattern in the expression */
1196 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001197 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001198 patl->from_ref = pat->ref->list_head;
1199 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001200 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001201
1202 /* that's ok */
1203 return 1;
1204}
1205
1206int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1207{
1208 struct pattern_list *patl;
1209
1210 /* allocate pattern */
1211 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001212 if (!patl) {
1213 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001214 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001215 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001216
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001217 /* duplicate pattern */
1218 memcpy(&patl->pat, pat, sizeof(*pat));
1219 patl->pat.ptr.ptr = malloc(patl->pat.len);
1220 if (!patl->pat.ptr.ptr) {
1221 free(patl);
1222 memprintf(err, "out of memory while indexing pattern");
1223 return 0;
1224 }
1225 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001226
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001227 /* chain pattern in the expression */
1228 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001229 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001230 patl->from_ref = pat->ref->list_head;
1231 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001232 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001233
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001234 /* that's ok */
1235 return 1;
1236}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001237
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001238int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1239{
1240 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001241
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001242 /* allocate pattern */
1243 patl = calloc(1, sizeof(*patl));
1244 if (!patl) {
1245 memprintf(err, "out of memory while indexing pattern");
1246 return 0;
1247 }
1248
1249 /* duplicate pattern */
1250 memcpy(&patl->pat, pat, sizeof(*pat));
1251 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1252 if (!patl->pat.ptr.str) {
1253 free(patl);
1254 memprintf(err, "out of memory while indexing pattern");
1255 return 0;
1256 }
1257 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1258 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001259
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001260 /* chain pattern in the expression */
1261 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001262 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001263 patl->from_ref = pat->ref->list_head;
1264 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001265 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001266
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001267 /* that's ok */
1268 return 1;
1269}
1270
Thierry Fournier8feaa662016-02-10 22:55:20 +01001271int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001272{
1273 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001274
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001275 /* allocate pattern */
1276 patl = calloc(1, sizeof(*patl));
1277 if (!patl) {
1278 memprintf(err, "out of memory while indexing pattern");
1279 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001280 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001281
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001282 /* duplicate pattern */
1283 memcpy(&patl->pat, pat, sizeof(*pat));
1284
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001285 /* compile regex */
Willy Tarreau9b5c8bb2020-11-02 19:16:23 +01001286 patl->pat.sflags |= PAT_SF_REGFREE;
Dragan Dosen26743032019-04-30 15:54:36 +02001287 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1288 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001289 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001290 return 0;
1291 }
1292
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001293 /* chain pattern in the expression */
1294 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001295 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001296 patl->from_ref = pat->ref->list_head;
1297 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001298 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001299
1300 /* that's ok */
1301 return 1;
1302}
1303
Thierry Fournier8feaa662016-02-10 22:55:20 +01001304int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1305{
1306 return pat_idx_list_reg_cap(expr, pat, 0, err);
1307}
1308
1309int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1310{
1311 return pat_idx_list_reg_cap(expr, pat, 1, err);
1312}
1313
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001314int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1315{
1316 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001317 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001318
1319 /* Only IPv4 can be indexed */
1320 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001321 /* in IPv4 case, check if the mask is contiguous so that we can
1322 * insert the network into the tree. A continuous mask has only
1323 * ones on the left. This means that this mask + its lower bit
1324 * added once again is null.
1325 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001326 mask = ntohl(pat->val.ipv4.mask.s_addr);
1327 if (mask + (mask & -mask) == 0) {
1328 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001329
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001330 /* node memory allocation */
1331 node = calloc(1, sizeof(*node) + 4);
1332 if (!node) {
1333 memprintf(err, "out of memory while loading pattern");
1334 return 0;
1335 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001336
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001338 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001339 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001340
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001341 /* FIXME: insert <addr>/<mask> into the tree here */
1342 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1343 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001344
1345 /* Insert the entry. */
1346 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau38d41992020-11-03 14:50:29 +01001347 node->from_ref = pat->ref->tree_head;
1348 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001349 expr->ref->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001350
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001351 /* that's ok */
1352 return 1;
1353 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001354 else {
1355 /* If the mask is not contiguous, just add the pattern to the list */
1356 return pat_idx_list_val(expr, pat, err);
1357 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001358 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001359 else if (pat->type == SMP_T_IPV6) {
1360 /* IPv6 also can be indexed */
1361 node = calloc(1, sizeof(*node) + 16);
1362 if (!node) {
1363 memprintf(err, "out of memory while loading pattern");
1364 return 0;
1365 }
1366
1367 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001368 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001369 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001370
1371 /* FIXME: insert <addr>/<mask> into the tree here */
1372 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1373 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001374
1375 /* Insert the entry. */
1376 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau38d41992020-11-03 14:50:29 +01001377 node->from_ref = pat->ref->tree_head;
1378 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001379 expr->ref->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001380
1381 /* that's ok */
1382 return 1;
1383 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001384
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001385 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001386}
1387
1388int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1389{
1390 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001391 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001392
1393 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001394 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001395 memprintf(err, "internal error: string expected, but the type is '%s'",
1396 smp_to_type[pat->type]);
1397 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001398 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001399
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001400 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001401 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001402 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001403
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001404 /* Process the key len */
1405 len = strlen(pat->ptr.str) + 1;
1406
1407 /* node memory allocation */
1408 node = calloc(1, sizeof(*node) + len);
1409 if (!node) {
1410 memprintf(err, "out of memory while loading pattern");
1411 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001412 }
1413
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001414 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001415 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001416 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001417
1418 /* copy the string */
1419 memcpy(node->node.key, pat->ptr.str, len);
1420
1421 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001422 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau38d41992020-11-03 14:50:29 +01001423 node->from_ref = pat->ref->tree_head;
1424 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001425 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001426
1427 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001428 return 1;
1429}
1430
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001431int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1432{
1433 int len;
1434 struct pattern_tree *node;
1435
1436 /* Only string can be indexed */
1437 if (pat->type != SMP_T_STR) {
1438 memprintf(err, "internal error: string expected, but the type is '%s'",
1439 smp_to_type[pat->type]);
1440 return 0;
1441 }
1442
1443 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1444 if (expr->mflags & PAT_MF_IGNORE_CASE)
1445 return pat_idx_list_str(expr, pat, err);
1446
1447 /* Process the key len */
1448 len = strlen(pat->ptr.str);
1449
1450 /* node memory allocation */
1451 node = calloc(1, sizeof(*node) + len + 1);
1452 if (!node) {
1453 memprintf(err, "out of memory while loading pattern");
1454 return 0;
1455 }
1456
1457 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001458 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001459 node->ref = pat->ref;
1460
1461 /* copy the string and the trailing zero */
1462 memcpy(node->node.key, pat->ptr.str, len + 1);
1463 node->node.node.pfx = len * 8;
1464
1465 /* index the new node */
1466 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau38d41992020-11-03 14:50:29 +01001467 node->from_ref = pat->ref->tree_head;
1468 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001469 expr->ref->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001470
1471 /* that's ok */
1472 return 1;
1473}
1474
Willy Tarreauf1c08922020-11-02 19:53:16 +01001475/* Deletes all patterns from reference <elt>. Note that all of their
Willy Tarreau78777ea2020-11-02 13:55:22 +01001476 * expressions must be locked, and the pattern lock must be held as well.
1477 */
Willy Tarreauf1c08922020-11-02 19:53:16 +01001478void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001479{
Willy Tarreau38d41992020-11-03 14:50:29 +01001480 struct pattern_tree *tree;
1481 struct pattern_list *pat;
1482 void **node;
Willy Tarreauf1c08922020-11-02 19:53:16 +01001483
1484 /* delete all known tree nodes. They are all allocated inline */
Willy Tarreau38d41992020-11-03 14:50:29 +01001485 for (node = elt->tree_head; node;) {
1486 tree = container_of(node, struct pattern_tree, from_ref);
1487 node = *node;
Willy Tarreauf1c08922020-11-02 19:53:16 +01001488 BUG_ON(tree->ref != elt);
1489
1490 ebmb_delete(&tree->node);
Willy Tarreauf1c08922020-11-02 19:53:16 +01001491 free(tree->data);
1492 free(tree);
1493 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001494
Willy Tarreauf1c08922020-11-02 19:53:16 +01001495 /* delete all list nodes and free their pattern entries (str/reg) */
Willy Tarreau38d41992020-11-03 14:50:29 +01001496 for (node = elt->list_head; node;) {
1497 pat = container_of(node, struct pattern_list, from_ref);
1498 node = *node;
Willy Tarreau78777ea2020-11-02 13:55:22 +01001499 BUG_ON(pat->pat.ref != elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001500
1501 /* Delete and free entry. */
1502 LIST_DEL(&pat->list);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001503 if (pat->pat.sflags & PAT_SF_REGFREE)
1504 regex_free(pat->pat.ptr.reg);
1505 else
1506 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001507 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001508 free(pat);
1509 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001510
Willy Tarreauf1c08922020-11-02 19:53:16 +01001511 /* update revision number to refresh the cache */
1512 ref->revision = rdtsc();
Willy Tarreau38d41992020-11-03 14:50:29 +01001513 elt->tree_head = NULL;
1514 elt->list_head = NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001515}
1516
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001517void pattern_init_expr(struct pattern_expr *expr)
1518{
1519 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001520 expr->pattern_tree = EB_ROOT;
1521 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001522}
1523
1524void pattern_init_head(struct pattern_head *head)
1525{
1526 LIST_INIT(&head->head);
1527}
1528
1529/* The following functions are relative to the management of the reference
1530 * lists. These lists are used to store the original pattern and associated
1531 * value as string form.
1532 *
1533 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001534 *
1535 * The pattern reference are stored with two identifiers: the unique_id and
1536 * the reference.
1537 *
1538 * The reference identify a file. Each file with the same name point to the
1539 * same reference. We can register many times one file. If the file is modified,
1540 * all his dependencies are also modified. The reference can be used with map or
1541 * acl.
1542 *
1543 * The unique_id identify inline acl. The unique id is unique for each acl.
1544 * You cannot force the same id in the configuration file, because this repoort
1545 * an error.
1546 *
1547 * A particular case appears if the filename is a number. In this case, the
1548 * unique_id is set with the number represented by the filename and the
1549 * reference is also set. This method prevent double unique_id.
1550 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001551 */
1552
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001553/* This function looks up a reference by name. If the reference is found, a
1554 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001555 */
1556struct pat_ref *pat_ref_lookup(const char *reference)
1557{
1558 struct pat_ref *ref;
1559
1560 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001561 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001562 return ref;
1563 return NULL;
1564}
1565
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001566/* This function looks up a reference's unique id. If the reference is found, a
1567 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001568 */
1569struct pat_ref *pat_ref_lookupid(int unique_id)
1570{
1571 struct pat_ref *ref;
1572
1573 list_for_each_entry(ref, &pattern_reference, list)
1574 if (ref->unique_id == unique_id)
1575 return ref;
1576 return NULL;
1577}
1578
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001579/* This function removes from the pattern reference <ref> all the patterns
1580 * attached to the reference element <elt>, and the element itself. The
1581 * reference must be locked.
1582 */
1583void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt)
1584{
1585 struct pattern_expr *expr;
1586 struct bref *bref, *back;
1587
1588 /*
1589 * we have to unlink all watchers from this reference pattern. We must
1590 * not relink them if this elt was the last one in the list.
1591 */
1592 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1593 LIST_DEL(&bref->users);
1594 LIST_INIT(&bref->users);
1595 if (elt->list.n != &ref->head)
1596 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
1597 bref->ref = elt->list.n;
1598 }
1599
1600 /* delete all entries from all expressions for this pattern */
1601 list_for_each_entry(expr, &ref->pat, list)
1602 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1603
1604 pat_delete_gen(ref, elt);
1605
1606 list_for_each_entry(expr, &ref->pat, list)
1607 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
1608
1609 LIST_DEL(&elt->list);
1610 free(elt->sample);
1611 free(elt->pattern);
1612 free(elt);
1613}
1614
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001615/* This function removes all the patterns matching the pointer <refelt> from
1616 * the reference and from each expr member of this reference. This function
1617 * returns 1 if the entry was found and deleted, otherwise zero.
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001618 */
1619int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1620{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001621 struct pat_ref_elt *elt, *safe;
1622
1623 /* delete pattern from reference */
1624 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1625 if (elt == refelt) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001626 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001627 return 1;
1628 }
1629 }
1630 return 0;
1631}
1632
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001633/* This function removes all patterns matching <key> from the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001634 * and from each expr member of the reference. This function returns 1
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001635 * if the deletion is done and returns 0 is the entry is not found.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001636 */
1637int pat_ref_delete(struct pat_ref *ref, const char *key)
1638{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001639 struct pat_ref_elt *elt, *safe;
1640 int found = 0;
1641
1642 /* delete pattern from reference */
1643 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1644 if (strcmp(key, elt->pattern) == 0) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001645 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001646 found = 1;
1647 }
1648 }
1649
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001650 return found;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001651}
1652
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001653/*
1654 * find and return an element <elt> matching <key> in a reference <ref>
1655 * return NULL if not found
1656 */
1657struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1658{
1659 struct pat_ref_elt *elt;
1660
1661 list_for_each_entry(elt, &ref->head, list) {
1662 if (strcmp(key, elt->pattern) == 0)
1663 return elt;
1664 }
1665
1666 return NULL;
1667}
1668
1669
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001670/* This function modifies the sample of pat_ref_elt <elt> in all expressions
1671 * found under <ref> to become <value>. It is assumed that the caller has
1672 * already verified that <elt> belongs to <ref>.
1673 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001674static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001675 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001676{
1677 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001678 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001679 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001680 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001681
1682 /* Try all needed converters. */
1683 list_for_each_entry(expr, &ref->pat, list) {
1684 if (!expr->pat_head->parse_smp)
1685 continue;
1686
1687 if (!expr->pat_head->parse_smp(value, &test)) {
1688 memprintf(err, "unable to parse '%s'", value);
1689 return 0;
1690 }
1691 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001692
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001693 /* Modify pattern from reference. */
1694 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001695 if (!sample) {
1696 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001697 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001698 }
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001699 /* Load sample in each reference. All the conversions are tested
1700 * below, normally these calls don't fail.
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001701 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001702 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001703 if (!expr->pat_head->parse_smp)
1704 continue;
1705
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001706 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001707 data = pattern_find_smp(expr, elt);
1708 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1709 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001710 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001711 }
1712
Emeric Brunb5997f72017-07-03 11:34:05 +02001713 /* free old sample only when all exprs are updated */
1714 free(elt->sample);
1715 elt->sample = sample;
1716
1717
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001718 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001719}
1720
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001721/* This function modifies the sample of pat_ref_elt <refelt> in all expressions
1722 * found under <ref> to become <value>, after checking that <refelt> really
1723 * belongs to <ref>.
1724 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001725int 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 +01001726{
1727 struct pat_ref_elt *elt;
1728
1729 /* Look for pattern in the reference. */
1730 list_for_each_entry(elt, &ref->head, list) {
1731 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001732 if (!pat_ref_set_elt(ref, elt, value, err))
1733 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001734 return 1;
1735 }
1736 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001737
1738 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001739 return 0;
1740}
1741
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001742/* This function modifies to <value> the sample of all patterns matching <key>
1743 * under <ref>.
1744 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001745int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001746{
1747 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001748 int found = 0;
1749 char *_merr;
1750 char **merr;
1751
1752 if (err) {
1753 merr = &_merr;
1754 *merr = NULL;
1755 }
1756 else
1757 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001758
1759 /* Look for pattern in the reference. */
1760 list_for_each_entry(elt, &ref->head, list) {
1761 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001762 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001763 if (err && merr) {
1764 if (!found) {
1765 *err = *merr;
1766 } else {
1767 memprintf(err, "%s, %s", *err, *merr);
1768 free(*merr);
1769 *merr = NULL;
1770 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001771 }
1772 }
1773 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001774 }
1775 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001776
1777 if (!found) {
1778 memprintf(err, "entry not found");
1779 return 0;
1780 }
1781 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001782}
1783
Joseph Herlant4189d672018-11-15 10:22:31 -08001784/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001785 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1786 * be unique. The user must check the reference with "pat_ref_lookup()"
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001787 * before calling this function. If the function fails, it returns NULL,
1788 * otherwise it returns the new struct pat_ref.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001789 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001790struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001791{
1792 struct pat_ref *ref;
1793
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001794 ref = calloc(1, sizeof(*ref));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001795 if (!ref)
1796 return NULL;
1797
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001798 if (display) {
1799 ref->display = strdup(display);
1800 if (!ref->display) {
1801 free(ref);
1802 return NULL;
1803 }
1804 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001805
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001806 ref->reference = strdup(reference);
1807 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001808 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001809 free(ref);
1810 return NULL;
1811 }
1812
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001813 ref->flags = flags;
1814 ref->unique_id = -1;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001815 ref->revision = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001816
1817 LIST_INIT(&ref->head);
1818 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001819 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001820 LIST_ADDQ(&pattern_reference, &ref->list);
1821
1822 return ref;
1823}
1824
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001825/* This function creates a new reference. <unique_id> is the unique id. If
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001826 * the value of <unique_id> is -1, the unique id is calculated later.
1827 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1828 * be unique. The user must check the reference with "pat_ref_lookup()"
1829 * or pat_ref_lookupid before calling this function. If the function
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001830 * fails, it returns NULL, otherwise it returns the new struct pat_ref.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001831 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001832struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001833{
1834 struct pat_ref *ref;
1835
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001836 ref = calloc(1, sizeof(*ref));
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001837 if (!ref)
1838 return NULL;
1839
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001840 if (display) {
1841 ref->display = strdup(display);
1842 if (!ref->display) {
1843 free(ref);
1844 return NULL;
1845 }
1846 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001847
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001848 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001849 ref->flags = flags;
Willy Tarreau29947742020-10-28 11:43:49 +01001850 ref->curr_gen = 0;
1851 ref->next_gen = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001852 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001853 LIST_INIT(&ref->head);
1854 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001855 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001856 LIST_ADDQ(&pattern_reference, &ref->list);
1857
1858 return ref;
1859}
1860
Willy Tarreauf4edb722020-10-28 10:52:46 +01001861/* This function adds entry to <ref>. It can fail on memory error. It returns
1862 * the newly added element on success, or NULL on failure. The PATREF_LOCK on
Willy Tarreau29947742020-10-28 11:43:49 +01001863 * <ref> must be held. It sets the newly created pattern's generation number
1864 * to the same value as the reference's.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001865 */
Willy Tarreauf4edb722020-10-28 10:52:46 +01001866struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001867{
1868 struct pat_ref_elt *elt;
1869
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001870 elt = calloc(1, sizeof(*elt));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001871 if (!elt)
Willy Tarreauf4edb722020-10-28 10:52:46 +01001872 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001873
Willy Tarreau29947742020-10-28 11:43:49 +01001874 elt->gen_id = ref->curr_gen;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001875 elt->line = line;
1876
1877 elt->pattern = strdup(pattern);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001878 if (!elt->pattern)
1879 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001880
1881 if (sample) {
1882 elt->sample = strdup(sample);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001883 if (!elt->sample)
1884 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001885 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001886
Emeric Brun8d85aa42017-06-29 15:40:33 +02001887 LIST_INIT(&elt->back_refs);
Willy Tarreau38d41992020-11-03 14:50:29 +01001888 elt->list_head = NULL;
1889 elt->tree_head = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001890 LIST_ADDQ(&ref->head, &elt->list);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001891 return elt;
1892 fail:
1893 if (elt)
1894 free(elt->pattern);
1895 free(elt);
1896 return NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001897}
1898
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001899/* This function creates sample found in <elt>, parses the pattern also
1900 * found in <elt> and inserts it in <expr>. The function copies <patflags>
1901 * into <expr>. If the function fails, it returns 0 and <err> is filled.
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001902 * In success case, the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001904int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1905 int patflags, char **err)
1906{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001907 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001908 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001909
1910 /* Create sample */
1911 if (elt->sample && expr->pat_head->parse_smp) {
1912 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001913 data = malloc(sizeof(*data));
1914 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001915 return 0;
1916
1917 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001918 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001919 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001920 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001921 return 0;
1922 }
1923
1924 }
1925 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001926 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001927
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001928 /* initialise pattern */
1929 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001930 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001931 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001932
1933 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001934 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001935 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001936 return 0;
1937 }
1938
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001939 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001940 /* index pattern */
1941 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001942 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001943 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001944 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001945 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001946 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001947
1948 return 1;
1949}
1950
Willy Tarreau0439e5e2020-10-28 18:45:45 +01001951/* This function tries to commit entry <elt> into <ref>. The new entry must
1952 * have already been inserted using pat_ref_append(), and its generation number
1953 * may have been adjusted as it will not be changed. <err> must point to a NULL
1954 * pointer. The PATREF lock on <ref> must be held. All the pattern_expr for
1955 * this reference will be updated (parsing, indexing). On success, non-zero is
1956 * returned. On failure, all the operation is rolled back (the element is
1957 * deleted from all expressions and is freed), zero is returned and the error
1958 * pointer <err> may have been updated (and the caller must free it). Failure
1959 * causes include memory allocation, parsing error or indexing error.
1960 */
1961int pat_ref_commit(struct pat_ref *ref, struct pat_ref_elt *elt, char **err)
1962{
1963 struct pattern_expr *expr;
1964
1965 list_for_each_entry(expr, &ref->pat, list) {
1966 if (!pat_ref_push(elt, expr, 0, err)) {
1967 pat_ref_delete_by_ptr(ref, elt);
1968 return 0;
1969 }
1970 }
1971 return 1;
1972}
1973
Willy Tarreau1a6857b2020-10-29 09:21:43 +01001974/* Loads <pattern>:<sample> into <ref> for generation <gen>. <sample> may be
1975 * NULL if none exists (e.g. ACL). If not needed, the generation number should
1976 * be set to ref->curr_gen. The error pointer must initially point to NULL. The
1977 * new entry will be propagated to all use places, involving allocation, parsing
1978 * and indexing. On error (parsing, allocation), the operation will be rolled
1979 * back, an error may be reported, and NULL will be reported. On success, the
1980 * freshly allocated element will be returned. The PATREF lock on <ref> must be
1981 * held during the operation.
1982 */
1983struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen,
1984 const char *pattern, const char *sample,
1985 int line, char **err)
1986{
1987 struct pat_ref_elt *elt;
1988
1989 elt = pat_ref_append(ref, pattern, sample, line);
1990 if (elt) {
1991 elt->gen_id = gen;
1992 if (!pat_ref_commit(ref, elt, err))
1993 elt = NULL;
1994 } else
1995 memprintf(err, "out of memory error");
1996
1997 return elt;
1998}
1999
Willy Tarreau6a174072020-10-28 10:58:05 +01002000/* This function adds entry to <ref>. It can fail on memory error. The new
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002001 * entry is added at all the pattern_expr registered in this reference. The
Willy Tarreau6a174072020-10-28 10:58:05 +01002002 * function stops on the first error encountered. It returns 0 and <err> is
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002003 * filled. If an error is encountered, the complete add operation is cancelled.
2004 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002005 */
2006int pat_ref_add(struct pat_ref *ref,
2007 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002008 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002009{
Willy Tarreau1a6857b2020-10-29 09:21:43 +01002010 return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002011}
2012
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002013/* This function purges all elements from <ref> that are older than generation
2014 * <oldest>. It will not purge more than <budget> entries at once, in order to
2015 * remain responsive. If budget is negative, no limit is applied.
2016 * The caller must already hold the PATREF_LOCK on <ref>. The function will
2017 * take the PATEXP_LOCK on all expressions of the pattern as needed. It returns
2018 * non-zero on completion, or zero if it had to stop before the end after
2019 * <budget> was depleted.
2020 */
2021int pat_ref_purge_older(struct pat_ref *ref, unsigned int oldest, int budget)
2022{
2023 struct pat_ref_elt *elt, *elt_bck;
2024 struct bref *bref, *bref_bck;
2025 struct pattern_expr *expr;
2026 int done;
2027
2028 list_for_each_entry(expr, &ref->pat, list)
2029 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
2030
2031 /* all expr are locked, we can safely remove all pat_ref */
2032
2033 /* assume completion for e.g. empty lists */
2034 done = 1;
2035 list_for_each_entry_safe(elt, elt_bck, &ref->head, list) {
2036 if ((int)(elt->gen_id - oldest) >= 0)
2037 continue;
2038
2039 if (budget >= 0 && !budget--) {
2040 done = 0;
2041 break;
2042 }
2043
2044 /*
2045 * we have to unlink all watchers from this reference pattern. We must
2046 * not relink them if this elt was the last one in the list.
2047 */
2048 list_for_each_entry_safe(bref, bref_bck, &elt->back_refs, users) {
2049 LIST_DEL(&bref->users);
2050 LIST_INIT(&bref->users);
2051 if (elt->list.n != &ref->head)
2052 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
2053 bref->ref = elt->list.n;
2054 }
2055
2056 /* delete the storage for all representations of this pattern. */
2057 pat_delete_gen(ref, elt);
2058
2059 LIST_DEL(&elt->list);
2060 free(elt->pattern);
2061 free(elt->sample);
2062 free(elt);
2063 }
2064
2065 list_for_each_entry(expr, &ref->pat, list)
2066 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
2067
2068#if defined(HA_HAVE_MALLOC_TRIM)
2069 if (done) {
2070 malloc_trim(0);
2071 }
2072#endif
2073
2074 return done;
2075}
2076
Joseph Herlant4189d672018-11-15 10:22:31 -08002077/* This function prunes <ref>, replaces all references by the references
2078 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002079 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002080 * The patterns are loaded in best effort and the errors are ignored,
2081 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002082 */
2083void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2084{
2085 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002086 struct pat_ref_elt *elt, *safe;
2087 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002088 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002089
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002090
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002091 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002092 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002093 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002094 }
2095
2096 /* all expr are locked, we can safely remove all pat_ref */
2097 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2098 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
Willy Tarreaud4164dc2020-10-27 18:55:20 +01002099 /* we have to unlink all watchers. */
2100 LIST_DEL_INIT(&bref->users);
2101 bref->ref = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02002102 }
Willy Tarreau28174722020-11-03 13:36:58 +01002103 pat_delete_gen(ref, elt);
Emeric Brunb5997f72017-07-03 11:34:05 +02002104 LIST_DEL(&elt->list);
2105 free(elt->pattern);
2106 free(elt->sample);
2107 free(elt);
2108 }
2109
2110 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002111 LIST_ADD(&replace->head, &ref->head);
2112 LIST_DEL(&replace->head);
2113
Emeric Brunb5997f72017-07-03 11:34:05 +02002114 list_for_each_entry(expr, &ref->pat, list) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002115 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002116 char *err = NULL;
2117 struct sample_data *data = NULL;
2118
Emeric Brunb5997f72017-07-03 11:34:05 +02002119 /* Create sample */
2120 if (elt->sample && expr->pat_head->parse_smp) {
2121 /* New sample. */
2122 data = malloc(sizeof(*data));
2123 if (!data)
2124 continue;
2125
2126 /* Parse value. */
2127 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2128 memprintf(&err, "unable to parse '%s'", elt->sample);
2129 send_log(NULL, LOG_NOTICE, "%s", err);
2130 free(err);
2131 free(data);
2132 continue;
2133 }
2134
2135 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002136
2137 /* initialise pattern */
2138 memset(&pattern, 0, sizeof(pattern));
2139 pattern.data = data;
2140 pattern.ref = elt;
2141
2142 /* parse pattern */
2143 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002144 send_log(NULL, LOG_NOTICE, "%s", err);
2145 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002146 free(data);
2147 continue;
2148 }
2149
2150 /* index pattern */
2151 if (!expr->pat_head->index(expr, &pattern, &err)) {
2152 send_log(NULL, LOG_NOTICE, "%s", err);
2153 free(err);
2154 free(data);
2155 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002156 }
2157 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002158 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002159 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002160 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau114d6982020-11-03 15:55:35 +01002161
2162#if defined(HA_HAVE_MALLOC_TRIM)
2163 malloc_trim(0);
2164#endif
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002165}
2166
Willy Tarreauae83e632020-11-03 10:37:31 +01002167/* This function prunes all entries of <ref> and all their associated
2168 * pattern_expr. It may return before the end of the list is reached,
2169 * returning 0, to yield, indicating to the caller that it must call it again.
2170 * until it returns non-zero. All patterns are purged, both current ones and
2171 * future or incomplete ones. This is used by "clear map" or "clear acl".
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002172 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002173int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002174{
Willy Tarreauae83e632020-11-03 10:37:31 +01002175 return pat_ref_purge_older(ref, ref->curr_gen + 1, 100) &&
2176 pat_ref_purge_older(ref, ref->next_gen + 1, 100);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002177}
2178
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002179/* This function looks up any existing reference <ref> in pattern_head <head>, and
2180 * returns the associated pattern_expr pointer if found, otherwise NULL.
2181 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2183{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002184 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002186 list_for_each_entry(expr, &head->head, list)
2187 if (expr->expr->ref == ref)
2188 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189 return NULL;
2190}
2191
Joseph Herlant4189d672018-11-15 10:22:31 -08002192/* This function creates new pattern_expr associated to the reference <ref>.
2193 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002194 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2195 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002196 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002197 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002198 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002199 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002200struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002201 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202{
2203 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002204 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002205
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002206 if (reuse)
2207 *reuse = 0;
2208
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002209 /* Memory and initialization of the chain element. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002210 list = calloc(1, sizeof(*list));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002211 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002212 memprintf(err, "out of memory");
2213 return NULL;
2214 }
2215
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002216 /* Look for existing similar expr. No that only the index, parse and
2217 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002218 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002219 */
2220 if (ref) {
2221 list_for_each_entry(expr, &ref->pat, list)
2222 if (expr->pat_head->index == head->index &&
2223 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002224 expr->pat_head->parse_smp == head->parse_smp &&
2225 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002226 break;
2227 if (&expr->list == &ref->pat)
2228 expr = NULL;
2229 }
2230 else
2231 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002232
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002233 /* If no similar expr was found, we create new expr. */
2234 if (!expr) {
2235 /* Get a lot of memory for the expr struct. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002236 expr = calloc(1, sizeof(*expr));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002237 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002238 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002239 memprintf(err, "out of memory");
2240 return NULL;
2241 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002242
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002243 /* Initialize this new expr. */
2244 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002245
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002246 /* Copy the pattern matching and indexing flags. */
2247 expr->mflags = patflags;
2248
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002249 /* This new pattern expression reference one of his heads. */
2250 expr->pat_head = head;
2251
2252 /* Link with ref, or to self to facilitate LIST_DEL() */
2253 if (ref)
2254 LIST_ADDQ(&ref->pat, &expr->list);
2255 else
2256 LIST_INIT(&expr->list);
2257
2258 expr->ref = ref;
2259
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002260 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002261
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002262 /* We must free this pattern if it is no more used. */
2263 list->do_free = 1;
2264 }
2265 else {
2266 /* If the pattern used already exists, it is already linked
2267 * with ref and we must not free it.
2268 */
2269 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002270 if (reuse)
2271 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002272 }
2273
2274 /* The new list element reference the pattern_expr. */
2275 list->expr = expr;
2276
2277 /* Link the list element with the pattern_head. */
2278 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002279 return expr;
2280}
2281
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002282/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2283 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002284 *
2285 * The file contains one key + value per line. Lines which start with '#' are
2286 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2287 * then the first "word" (series of non-space/tabs characters), and the value is
2288 * what follows this series of space/tab till the end of the line excluding
2289 * trailing spaces/tabs.
2290 *
2291 * Example :
2292 *
2293 * # this is a comment and is ignored
2294 * 62.212.114.60 1wt.eu \n
2295 * <-><-----------><---><----><---->
2296 * | | | | `--- trailing spaces ignored
2297 * | | | `-------- value
2298 * | | `--------------- middle spaces ignored
2299 * | `------------------------ key
2300 * `-------------------------------- leading spaces ignored
2301 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002302 * Return non-zero in case of success, otherwise 0.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002303 */
2304int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2305{
2306 FILE *file;
2307 char *c;
2308 int ret = 0;
2309 int line = 0;
2310 char *key_beg;
2311 char *key_end;
2312 char *value_beg;
2313 char *value_end;
2314
2315 file = fopen(filename, "r");
2316 if (!file) {
2317 memprintf(err, "failed to open pattern file <%s>", filename);
2318 return 0;
2319 }
2320
2321 /* now parse all patterns. The file may contain only one pattern
2322 * followed by one value per line. The start spaces, separator spaces
2323 * and and spaces are stripped. Each can contain comment started by '#'
2324 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002326 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002328
2329 /* ignore lines beginning with a dash */
2330 if (*c == '#')
2331 continue;
2332
2333 /* strip leading spaces and tabs */
2334 while (*c == ' ' || *c == '\t')
2335 c++;
2336
2337 /* empty lines are ignored too */
2338 if (*c == '\0' || *c == '\r' || *c == '\n')
2339 continue;
2340
2341 /* look for the end of the key */
2342 key_beg = c;
2343 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2344 c++;
2345
2346 key_end = c;
2347
2348 /* strip middle spaces and tabs */
2349 while (*c == ' ' || *c == '\t')
2350 c++;
2351
2352 /* look for the end of the value, it is the end of the line */
2353 value_beg = c;
2354 while (*c && *c != '\n' && *c != '\r')
2355 c++;
2356 value_end = c;
2357
2358 /* trim possibly trailing spaces and tabs */
2359 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2360 value_end--;
2361
2362 /* set final \0 and check entries */
2363 *key_end = '\0';
2364 *value_end = '\0';
2365
2366 /* insert values */
2367 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2368 memprintf(err, "out of memory");
2369 goto out_close;
2370 }
2371 }
2372
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002373 if (ferror(file)) {
2374 memprintf(err, "error encountered while reading <%s> : %s",
2375 filename, strerror(errno));
2376 goto out_close;
2377 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002378 /* success */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002379 ret = 1;
2380
2381 out_close:
2382 fclose(file);
2383 return ret;
2384}
2385
2386/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2387 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002388 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002389int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002390{
2391 FILE *file;
2392 char *c;
2393 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002394 int ret = 0;
2395 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002396
2397 file = fopen(filename, "r");
2398 if (!file) {
2399 memprintf(err, "failed to open pattern file <%s>", filename);
2400 return 0;
2401 }
2402
2403 /* now parse all patterns. The file may contain only one pattern per
2404 * line. If the line contains spaces, they will be part of the pattern.
2405 * The pattern stops at the first CR, LF or EOF encountered.
2406 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002408 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002409 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002410
2411 /* ignore lines beginning with a dash */
2412 if (*c == '#')
2413 continue;
2414
2415 /* strip leading spaces and tabs */
2416 while (*c == ' ' || *c == '\t')
2417 c++;
2418
2419
2420 arg = c;
2421 while (*c && *c != '\n' && *c != '\r')
2422 c++;
2423 *c = 0;
2424
2425 /* empty lines are ignored too */
2426 if (c == arg)
2427 continue;
2428
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002429 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002430 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2431 goto out_close;
2432 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002433 }
2434
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002435 if (ferror(file)) {
2436 memprintf(err, "error encountered while reading <%s> : %s",
2437 filename, strerror(errno));
2438 goto out_close;
2439 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002440 ret = 1; /* success */
2441
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002442 out_close:
2443 fclose(file);
2444 return ret;
2445}
2446
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002447int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002448 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002449 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002450{
2451 struct pat_ref *ref;
2452 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002453 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002454 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002455
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002456 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002457 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002458
2459 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002460 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002461 chunk_printf(&trash,
2462 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2463 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2464
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002465 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002466 if (!ref) {
2467 memprintf(err, "out of memory");
2468 return 0;
2469 }
2470
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002471 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002472 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002473 if (!pat_ref_read_from_file_smp(ref, filename, err))
2474 return 0;
2475 }
2476 else {
2477 if (!pat_ref_read_from_file(ref, filename, err))
2478 return 0;
2479 }
2480 }
2481 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002482 /* The reference already exists, check the map compatibility. */
2483
2484 /* If the load require samples and the flag PAT_REF_SMP is not set,
2485 * the reference doesn't contain sample, and cannot be used.
2486 */
2487 if (load_smp) {
2488 if (!(ref->flags & PAT_REF_SMP)) {
2489 memprintf(err, "The file \"%s\" is already used as one column file "
2490 "and cannot be used by as two column file.",
2491 filename);
2492 return 0;
2493 }
2494 }
2495 else {
2496 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2497 * set, the reference contains a sample, and cannot be used.
2498 */
2499 if (ref->flags & PAT_REF_SMP) {
2500 memprintf(err, "The file \"%s\" is already used as two column file "
2501 "and cannot be used by as one column file.",
2502 filename);
2503 return 0;
2504 }
2505 }
2506
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002507 /* Extends display */
2508 chunk_printf(&trash, "%s", ref->display);
2509 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2510 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2511 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002513 if (!ref->display) {
2514 memprintf(err, "out of memory");
2515 return 0;
2516 }
2517
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002518 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002519 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002520 }
2521
2522 /* Now, we can loading patterns from the reference. */
2523
2524 /* Lookup for existing reference in the head. If the reference
2525 * doesn't exists, create it.
2526 */
2527 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002528 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002529 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002530 if (!expr)
2531 return 0;
2532 }
2533
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002534 /* The returned expression may be not empty, because the function
2535 * "pattern_new_expr" lookup for similar pattern list and can
2536 * reuse a already filled pattern list. In this case, we can not
2537 * reload the patterns.
2538 */
2539 if (reuse)
2540 return 1;
2541
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002542 /* Load reference content in the pattern expression. */
2543 list_for_each_entry(elt, &ref->head, list) {
2544 if (!pat_ref_push(elt, expr, patflags, err)) {
2545 if (elt->line > 0)
2546 memprintf(err, "%s at line %d of file '%s'",
2547 *err, elt->line, filename);
2548 return 0;
2549 }
2550 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002551
2552 return 1;
2553}
2554
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002555/* This function executes a pattern match on a sample. It applies pattern <expr>
2556 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2557 * non-null if the sample match. If <fill> is true and the sample match, the
2558 * function returns the matched pattern. In many cases, this pattern can be a
2559 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002560 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002561struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002562{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002563 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 struct pattern *pat;
2565
2566 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002567 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002568 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002569 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002570 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002571 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002572 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002573 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002574 return &static_pattern;
2575 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002576
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002577 /* convert input to string */
2578 if (!sample_convert(smp, head->expect_type))
2579 return NULL;
2580
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002581 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002582 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002583 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002584 if (pat) {
2585 /* We duplicate the pattern cause it could be modified
2586 by another thread */
2587 if (pat != &static_pattern) {
2588 memcpy(&static_pattern, pat, sizeof(struct pattern));
2589 pat = &static_pattern;
2590 }
2591
2592 /* We also duplicate the sample data for
2593 same reason */
2594 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002595 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002596 case SMP_T_STR:
2597 static_sample_data.type = SMP_T_STR;
2598 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002599 static_sample_data.u.str.data = pat->data->u.str.data;
2600 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2601 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2602 memcpy(static_sample_data.u.str.area,
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002603 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002605 pat->data = &static_sample_data;
2606 break;
2607
Emeric Brunb5997f72017-07-03 11:34:05 +02002608 case SMP_T_IPV4:
2609 case SMP_T_IPV6:
2610 case SMP_T_SINT:
2611 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002612 pat->data = &static_sample_data;
2613 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002614 default:
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002615 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002616 pat->data = NULL;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002617 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002618 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002619 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002620 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002621 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002622 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002623 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002624 }
2625 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002626}
2627
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002628/* This function prunes the pattern expressions starting at pattern_head <head>. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002629void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002630{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002631 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002632
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002633 list_for_each_entry_safe(list, safe, &head->head, list) {
2634 LIST_DEL(&list->list);
2635 if (list->do_free) {
2636 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002637 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002638 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002639 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002640 free(list->expr);
2641 }
2642 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002643 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002644}
2645
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002646/* This function searches occurrences of pattern reference element <ref> in
2647 * expression <expr> and returns a pointer to a pointer of the sample storage.
2648 * If <ref> is not found, NULL is returned.
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002649 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002650struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002651{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002652 struct ebmb_node *node;
2653 struct pattern_tree *elt;
2654 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002655
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002656 for (node = ebmb_first(&expr->pattern_tree);
2657 node;
2658 node = ebmb_next(node)) {
2659 elt = container_of(node, struct pattern_tree, node);
2660 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002661 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002662 }
2663
2664 for (node = ebmb_first(&expr->pattern_tree_2);
2665 node;
2666 node = ebmb_next(node)) {
2667 elt = container_of(node, struct pattern_tree, node);
2668 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002669 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002670 }
2671
2672 list_for_each_entry(pat, &expr->patterns, list)
2673 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002674 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002675
2676 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002677}
2678
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002679/* This function compares two pat_ref** on their unique_id, and returns -1/0/1
2680 * depending on their order (suitable for sorting).
2681 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002682static int cmp_pat_ref(const void *_a, const void *_b)
2683{
2684 struct pat_ref * const *a = _a;
2685 struct pat_ref * const *b = _b;
2686
2687 if ((*a)->unique_id < (*b)->unique_id)
2688 return -1;
2689 else if ((*a)->unique_id > (*b)->unique_id)
2690 return 1;
2691 return 0;
2692}
2693
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002694/* This function finalizes the configuration parsing. It sets all the
2695 * automatic ids.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002696 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002697int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002698{
Tim Duesterhusb584b442020-03-17 21:08:24 +01002699 size_t len = 0;
2700 size_t unassigned_pos = 0;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002701 int next_unique_id = 0;
Tim Duesterhusb584b442020-03-17 21:08:24 +01002702 size_t i, j;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002703 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002704 struct list pr = LIST_HEAD_INIT(pr);
2705
Willy Tarreau52bf8392020-03-08 00:42:37 +01002706 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002707
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002708 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002709 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002710 len++;
2711 if (ref->unique_id != -1)
2712 unassigned_pos++;
2713 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002714
Tim Duesterhusb584b442020-03-17 21:08:24 +01002715 if (len == 0) {
2716 return 0;
2717 }
2718
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002719 arr = calloc(len, sizeof(*arr));
2720 if (arr == NULL) {
2721 ha_alert("Out of memory error.\n");
2722 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002723 }
2724
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002725 i = 0;
2726 j = unassigned_pos;
2727 list_for_each_entry(ref, &pattern_reference, list) {
2728 if (ref->unique_id != -1)
2729 arr[i++] = ref;
2730 else
2731 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002732 }
2733
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002734 /* Sort first segment of array with user-defined unique ids for
2735 * fast lookup when generating unique ids
2736 */
2737 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2738
2739 /* Assign unique ids to the rest of the elements */
2740 for (i = unassigned_pos; i < len; i++) {
2741 do {
2742 arr[i]->unique_id = next_unique_id++;
2743 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2744 }
2745
2746 /* Sort complete array */
2747 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2748
2749 /* Convert back to linked list */
2750 for (i = 0; i < len; i++)
2751 LIST_ADDQ(&pr, &arr[i]->list);
2752
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002753 /* swap root */
2754 LIST_ADD(&pr, &pattern_reference);
2755 LIST_DEL(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002756
2757 free(arr);
2758 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002759}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002760
2761static int pattern_per_thread_lru_alloc()
2762{
2763 if (!global.tune.pattern_cache)
2764 return 1;
2765 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2766 return !!pat_lru_tree;
2767}
2768
2769static void pattern_per_thread_lru_free()
2770{
2771 lru64_destroy(pat_lru_tree);
2772}
2773
2774REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2775REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);