blob: 116bf2b4cac08f05e39436b6d20e9497787984fc [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>
Willy Tarreaub2551052020-06-09 09:07:15 +020019
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020021#include <haproxy/global.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020023#include <haproxy/net_helper.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020024#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020025#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020026#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/tools.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020028#include <haproxy/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
Thierry FOURNIERed66c292013-11-28 11:05:19 +010030
Willy Tarreau9057a002021-04-10 17:44:27 +020031const char *const pat_match_names[PAT_MATCH_NUM] = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010032 [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
Willy Tarreau9057a002021-04-10 17:44:27 +020048int (*const 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
Willy Tarreau9057a002021-04-10 17:44:27 +020065int (*const pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010066 [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
Willy Tarreau9057a002021-04-10 17:44:27 +020082void (*const 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
Willy Tarreau9057a002021-04-10 17:44:27 +020099struct pattern *(*const 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 */
Willy Tarreau9057a002021-04-10 17:44:27 +0200117int const 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 Tarreau295a89c2021-04-10 17:42:04 +0200142static unsigned long long pat_lru_seed __read_mostly;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200143
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
Thierry Fourniera68affe2020-11-10 20:51:36 +0100451 * the buffer is large enough to accommodate it. If the flag
452 * CONST is set, duplicate the string
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200453 */
454 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry Fourniera68affe2020-11-10 20:51:36 +0100455 if (prev) {
456 if (smp->flags & SMP_F_CONST) {
457 if (!smp_dup(smp))
458 return NULL;
459 } else {
460 smp->data.u.str.area[smp->data.u.str.data] = '\0';
461 }
462 }
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200463 }
464 else {
465 /* Otherwise, the sample is duplicated. A trailing zero
466 * is automatically added to the string.
467 */
468 if (!smp_dup(smp))
469 return NULL;
470 }
471
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200472 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100473 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200474 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreauc93da692020-10-29 09:41:34 +0100475
476 while (node) {
477 elt = ebmb_entry(node, struct pattern_tree, node);
478 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +0200479 node = ebmb_next_dup(node);
Willy Tarreauc93da692020-10-29 09:41:34 +0100480 continue;
481 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100482 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200483 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100484 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200485 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100486 static_pattern.type = SMP_T_STR;
487 static_pattern.ptr.str = (char *)elt->node.key;
488 }
489 return &static_pattern;
490 }
491 }
492
493 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200494 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200495 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200496
Dragan Dosen967e7e72020-12-22 13:22:34 +0100497 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100498 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200499 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200500 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200501 return ret;
502 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 }
504
Emeric Brunb5997f72017-07-03 11:34:05 +0200505
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 list_for_each_entry(lst, &expr->patterns, list) {
507 pattern = &lst->pat;
508
Willy Tarreauc93da692020-10-29 09:41:34 +0100509 if (pattern->ref->gen_id != expr->ref->curr_gen)
510 continue;
511
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100513 continue;
514
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200515 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
517 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200518 ret = pattern;
519 break;
520 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100521 }
522
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200523 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100524 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200525
526 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100527}
528
529/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100530struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100531{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100532 struct pattern_list *lst;
533 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200534 struct pattern *ret = NULL;
535 struct lru64 *lru = NULL;
536
537 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200538 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100539
Dragan Dosen967e7e72020-12-22 13:22:34 +0100540 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100541 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200542 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200543 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200544 return ret;
545 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200546 }
547
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 list_for_each_entry(lst, &expr->patterns, list) {
549 pattern = &lst->pat;
550
Willy Tarreauc93da692020-10-29 09:41:34 +0100551 if (pattern->ref->gen_id != expr->ref->curr_gen)
552 continue;
553
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200554 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100555 continue;
556
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200558 ret = pattern;
559 break;
560 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100561 }
562
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200563 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100564 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200565
566 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100567}
568
569/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100570 * and restores the previous character when leaving. This function fills
571 * a matching array.
572 */
573struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
574{
575 struct pattern_list *lst;
576 struct pattern *pattern;
577 struct pattern *ret = NULL;
578
579 list_for_each_entry(lst, &expr->patterns, list) {
580 pattern = &lst->pat;
581
Willy Tarreauc93da692020-10-29 09:41:34 +0100582 if (pattern->ref->gen_id != expr->ref->curr_gen)
583 continue;
584
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200585 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100586 MAX_MATCH, pmatch, 0)) {
587 ret = pattern;
588 smp->ctx.a[0] = pmatch;
589 break;
590 }
591 }
592
593 return ret;
594}
595
596/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100597 * and restores the previous character when leaving.
598 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100600{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601 struct pattern_list *lst;
602 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200603 struct pattern *ret = NULL;
604 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100605
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200607 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200608
Dragan Dosen967e7e72020-12-22 13:22:34 +0100609 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100610 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200611 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200612 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200613 return ret;
614 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200615 }
616
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100617 list_for_each_entry(lst, &expr->patterns, list) {
618 pattern = &lst->pat;
619
Willy Tarreauc93da692020-10-29 09:41:34 +0100620 if (pattern->ref->gen_id != expr->ref->curr_gen)
621 continue;
622
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624 ret = pattern;
625 break;
626 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100627 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200628
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200629 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100630 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200631
632 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100633}
634
635/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100636struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100637{
638 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200640 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100641 struct pattern_list *lst;
642 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200643 struct pattern *ret = NULL;
644 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100645
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200646 /* Lookup a string in the expression's pattern tree. */
647 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200648 char prev = 0;
649
650 if (smp->data.u.str.data < smp->data.u.str.size) {
651 /* we may have to force a trailing zero on the test pattern and
Aurelien DARRAGON47a08e82024-09-06 16:21:02 +0200652 * the buffer is large enough to accommodate it. If the flag
653 * CONST is set, duplicate the string
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200654 */
655 prev = smp->data.u.str.area[smp->data.u.str.data];
Aurelien DARRAGON47a08e82024-09-06 16:21:02 +0200656 if (prev) {
657 if (smp->flags & SMP_F_CONST) {
658 if (!smp_dup(smp))
659 return NULL;
660 } else {
661 smp->data.u.str.area[smp->data.u.str.data] = '\0';
662 }
663 }
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200664 }
665 else {
666 /* Otherwise, the sample is duplicated. A trailing zero
667 * is automatically added to the string.
668 */
669 if (!smp_dup(smp))
670 return NULL;
671 }
672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 node = ebmb_lookup_longest(&expr->pattern_tree,
674 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200675 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200676 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200677
Willy Tarreauc93da692020-10-29 09:41:34 +0100678 while (node) {
679 elt = ebmb_entry(node, struct pattern_tree, node);
680 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +0200681 node = ebmb_lookup_shorter(node);
Willy Tarreauc93da692020-10-29 09:41:34 +0100682 continue;
683 }
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200684 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200685 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200686 static_pattern.ref = elt->ref;
687 static_pattern.sflags = PAT_SF_TREE;
688 static_pattern.type = SMP_T_STR;
689 static_pattern.ptr.str = (char *)elt->node.key;
690 }
691 return &static_pattern;
692 }
693 }
694
695 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200696 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200697 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200698
Dragan Dosen967e7e72020-12-22 13:22:34 +0100699 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100700 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200701 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200702 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200703 return ret;
704 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200705 }
706
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100707 list_for_each_entry(lst, &expr->patterns, list) {
708 pattern = &lst->pat;
709
Willy Tarreauc93da692020-10-29 09:41:34 +0100710 if (pattern->ref->gen_id != expr->ref->curr_gen)
711 continue;
712
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100714 continue;
715
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200716 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
718 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 continue;
720
Willy Tarreauf3045d22015-04-29 16:24:50 +0200721 ret = pattern;
722 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100723 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200724
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200725 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100726 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727
728 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729}
730
731/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100732struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733{
734 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100735 struct pattern_list *lst;
736 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200737 struct pattern *ret = NULL;
738 struct lru64 *lru = NULL;
739
740 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200741 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200742
Dragan Dosen967e7e72020-12-22 13:22:34 +0100743 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100744 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200745 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200746 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200747 return ret;
748 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200749 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100750
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100751 list_for_each_entry(lst, &expr->patterns, list) {
752 pattern = &lst->pat;
753
Willy Tarreauc93da692020-10-29 09:41:34 +0100754 if (pattern->ref->gen_id != expr->ref->curr_gen)
755 continue;
756
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100758 continue;
759
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200760 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
762 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100763 continue;
764
Willy Tarreauf3045d22015-04-29 16:24:50 +0200765 ret = pattern;
766 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100767 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200768
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200769 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100770 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200771
772 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100773}
774
775/* Checks that the pattern is included inside the tested string.
776 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
777 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100778struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100779{
780 int icase;
781 char *end;
782 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100783 struct pattern_list *lst;
784 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200785 struct pattern *ret = NULL;
786 struct lru64 *lru = NULL;
787
788 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200789 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200790
Dragan Dosen967e7e72020-12-22 13:22:34 +0100791 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100792 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200793 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200794 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200795 return ret;
796 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200797 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100798
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 list_for_each_entry(lst, &expr->patterns, list) {
800 pattern = &lst->pat;
801
Willy Tarreauc93da692020-10-29 09:41:34 +0100802 if (pattern->ref->gen_id != expr->ref->curr_gen)
803 continue;
804
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200805 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100806 continue;
807
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200809 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100810 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200811 for (c = smp->data.u.str.area; c <= end; c++) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200812 if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100813 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200814 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
815 ret = pattern;
816 goto leave;
817 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100818 }
819 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200820 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100821 if (*c != *pattern->ptr.str)
822 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200823 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
824 ret = pattern;
825 goto leave;
826 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100827 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100828 }
829 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200830 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200831 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100832 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200833
834 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100835}
836
837/* This one is used by other real functions. It checks that the pattern is
838 * included inside the tested string, but enclosed between the specified
839 * delimiters or at the beginning or end of the string. The delimiters are
840 * provided as an unsigned int made by make_4delim() and match up to 4 different
841 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
842 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200843static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100844{
845 int may_match, icase;
846 char *c, *end;
847 char *ps;
848 int pl;
849
850 pl = pattern->len;
851 ps = pattern->ptr.str;
852
853 while (pl > 0 && is_delimiter(*ps, delimiters)) {
854 pl--;
855 ps++;
856 }
857
858 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
859 pl--;
860
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200861 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100862 return PAT_NOMATCH;
863
864 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200865 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200866 end = smp->data.u.str.area + smp->data.u.str.data - pl;
867 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100868 if (is_delimiter(*c, delimiters)) {
869 may_match = 1;
870 continue;
871 }
872
873 if (!may_match)
874 continue;
875
876 if (icase) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200877 if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) &&
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100878 (strncasecmp(ps, c, pl) == 0) &&
879 (c == end || is_delimiter(c[pl], delimiters)))
880 return PAT_MATCH;
881 } else {
882 if ((*c == *ps) &&
883 (strncmp(ps, c, pl) == 0) &&
884 (c == end || is_delimiter(c[pl], delimiters)))
885 return PAT_MATCH;
886 }
887 may_match = 0;
888 }
889 return PAT_NOMATCH;
890}
891
892/* Checks that the pattern is included inside the tested string, but enclosed
893 * between the delimiters '?' or '/' or at the beginning or end of the string.
894 * Delimiters at the beginning or end of the pattern are ignored.
895 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100896struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100898 struct pattern_list *lst;
899 struct pattern *pattern;
900
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100901 list_for_each_entry(lst, &expr->patterns, list) {
902 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100903
904 if (pattern->ref->gen_id != expr->ref->curr_gen)
905 continue;
906
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200907 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100908 return pattern;
909 }
910 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100911}
912
913/* Checks that the pattern is included inside the tested string, but enclosed
914 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
915 * the string. Delimiters at the beginning or end of the pattern are ignored.
916 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100917struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100918{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100919 struct pattern_list *lst;
920 struct pattern *pattern;
921
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100922 list_for_each_entry(lst, &expr->patterns, list) {
923 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100924
925 if (pattern->ref->gen_id != expr->ref->curr_gen)
926 continue;
927
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200928 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100929 return pattern;
930 }
931 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100932}
933
934/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100935struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100936{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100937 struct pattern_list *lst;
938 struct pattern *pattern;
939
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940 list_for_each_entry(lst, &expr->patterns, list) {
941 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100942
943 if (pattern->ref->gen_id != expr->ref->curr_gen)
944 continue;
945
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200946 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
947 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100948 return pattern;
949 }
950 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100951}
952
953/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100954struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100955{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100956 struct pattern_list *lst;
957 struct pattern *pattern;
958
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100959 list_for_each_entry(lst, &expr->patterns, list) {
960 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100961
962 if (pattern->ref->gen_id != expr->ref->curr_gen)
963 continue;
964
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200965 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
966 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100967 return pattern;
968 }
969 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100970}
971
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100972struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100973{
974 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100975 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100976 struct in_addr *s;
977 struct ebmb_node *node;
978 struct pattern_tree *elt;
979 struct pattern_list *lst;
980 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100981
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100982 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200983 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100984 /* Lookup an IPv4 address in the expression's pattern tree using
985 * the longest match method.
986 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200987 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100988 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Willy Tarreauc93da692020-10-29 09:41:34 +0100989 while (node) {
990 elt = ebmb_entry(node, struct pattern_tree, node);
991 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +0200992 node = ebmb_lookup_shorter(node);
Willy Tarreauc93da692020-10-29 09:41:34 +0100993 continue;
994 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100995 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200996 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100997 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200998 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100999 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001000 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001001 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1002 return NULL;
1003 }
1004 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001005 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001006
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001007 /* The IPv4 sample don't match the IPv4 tree. Convert the IPv4
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001008 * sample address to IPv6 with the mapping method using the ::ffff:
1009 * prefix, and try to lookup in the IPv6 tree.
1010 */
1011 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +01001012 write_u16(&tmp6.s6_addr[10], htons(0xffff));
1013 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001014 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001015 while (node) {
1016 elt = ebmb_entry(node, struct pattern_tree, node);
1017 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +02001018 node = ebmb_lookup_shorter(node);
Willy Tarreauc93da692020-10-29 09:41:34 +01001019 continue;
1020 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001021 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001022 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001023 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001024 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001025 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001026 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001027 static_pattern.val.ipv6.mask = elt->node.node.pfx;
1028 }
1029 return &static_pattern;
1030 }
1031 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001032
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001033 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001034 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001035 /* Lookup an IPv6 address in the expression's pattern tree using
1036 * the longest match method.
1037 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001038 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001039 while (node) {
1040 elt = ebmb_entry(node, struct pattern_tree, node);
1041 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +02001042 node = ebmb_lookup_shorter(node);
Willy Tarreauc93da692020-10-29 09:41:34 +01001043 continue;
1044 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001045 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001046 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001047 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001048 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001049 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001050 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001051 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001052 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001053 return &static_pattern;
1054 }
1055
1056 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1057 * following forms :
1058 * - ::ffff:ip:v4 (ipv4 mapped)
1059 * - ::0000:ip:v4 (old ipv4 mapped)
1060 * - 2002:ip:v4:: (6to4)
1061 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001062 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1063 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1064 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
1065 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1066 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
1067 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001068 else
Willy Tarreau296cfd12020-02-25 09:58:41 +01001069 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1070 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001071
1072 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1073 * match method.
1074 */
1075 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
Willy Tarreauc93da692020-10-29 09:41:34 +01001076 while (node) {
1077 elt = ebmb_entry(node, struct pattern_tree, node);
1078 if (elt->ref->gen_id != expr->ref->curr_gen) {
Willy Tarreau51d38a22022-08-01 11:46:27 +02001079 node = ebmb_lookup_shorter(node);
Willy Tarreauc93da692020-10-29 09:41:34 +01001080 continue;
1081 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001082 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001083 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001084 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001085 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001086 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001087 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001088 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1089 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001090 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001091 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001092 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001093 }
1094 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001095
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001096 /* Lookup in the list. the list contain only IPv4 patterns */
1097 list_for_each_entry(lst, &expr->patterns, list) {
1098 pattern = &lst->pat;
1099
Willy Tarreauc93da692020-10-29 09:41:34 +01001100 if (pattern->ref->gen_id != expr->ref->curr_gen)
1101 continue;
1102
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001103 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001104 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001105 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001106 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001107 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001108 /* v4 match on a V6 sample. We want to check at least for
1109 * the following forms :
1110 * - ::ffff:ip:v4 (ipv4 mapped)
1111 * - ::0000:ip:v4 (old ipv4 mapped)
1112 * - 2002:ip:v4:: (6to4)
1113 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001114 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1115 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1116 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1117 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001118 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001119 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1120 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1121 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001122 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001123 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001124 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001125 } else {
1126 /* impossible */
1127 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001128 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001129
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001130 /* Check if the input sample match the current pattern. */
1131 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001132 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001133 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001134 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001135}
1136
Willy Tarreau867a8a52020-11-03 11:22:04 +01001137/* finds the pattern holding <list> from list head <head> and deletes it.
1138 * This is made for use for pattern removal within an expression.
1139 */
Willy Tarreau38d41992020-11-03 14:50:29 +01001140static void pat_unlink_from_head(void **head, void **list)
Willy Tarreau867a8a52020-11-03 11:22:04 +01001141{
Willy Tarreau38d41992020-11-03 14:50:29 +01001142 while (*head) {
1143 if (*head == list) {
1144 *head = *list;
Willy Tarreau867a8a52020-11-03 11:22:04 +01001145 return;
1146 }
Willy Tarreau38d41992020-11-03 14:50:29 +01001147 head = *head;
Willy Tarreau867a8a52020-11-03 11:22:04 +01001148 }
1149}
1150
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001151void free_pattern_tree(struct eb_root *root)
1152{
1153 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001154 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001155
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001156 node = eb_first(root);
1157 while (node) {
1158 next = eb_next(node);
1159 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001160 elt = container_of(node, struct pattern_tree, node);
Willy Tarreau867a8a52020-11-03 11:22:04 +01001161 pat_unlink_from_head(&elt->ref->tree_head, &elt->from_ref);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001162 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001163 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001164 node = next;
1165 }
1166}
1167
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001168void pat_prune_gen(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001169{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001170 struct pattern_list *pat, *tmp;
1171
1172 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001173 LIST_DELETE(&pat->list);
Willy Tarreau867a8a52020-11-03 11:22:04 +01001174 pat_unlink_from_head(&pat->pat.ref->list_head, &pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001175 if (pat->pat.sflags & PAT_SF_REGFREE)
1176 regex_free(pat->pat.ptr.ptr);
1177 else
1178 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001179 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001180 free(pat);
1181 }
1182
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001183 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001184 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001185 LIST_INIT(&expr->patterns);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001186 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001187 expr->ref->entry_cnt = 0;
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001188}
1189
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001190/*
1191 *
1192 * The following functions are used for the pattern indexation
1193 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001194 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001195
1196int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001197{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001198 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001199
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001200 /* allocate pattern */
1201 patl = calloc(1, sizeof(*patl));
1202 if (!patl) {
1203 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001204 return 0;
1205 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001206
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001207 /* duplicate pattern */
1208 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001209
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 /* chain pattern in the expression */
Willy Tarreau2b718102021-04-21 07:32:39 +02001211 LIST_APPEND(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001212 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001213 patl->from_ref = pat->ref->list_head;
1214 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001215 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001216 expr->ref->entry_cnt++;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001217
1218 /* that's ok */
1219 return 1;
1220}
1221
1222int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1223{
1224 struct pattern_list *patl;
1225
1226 /* allocate pattern */
1227 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001228 if (!patl) {
1229 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001230 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001231 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001232
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001233 /* duplicate pattern */
1234 memcpy(&patl->pat, pat, sizeof(*pat));
1235 patl->pat.ptr.ptr = malloc(patl->pat.len);
1236 if (!patl->pat.ptr.ptr) {
1237 free(patl);
1238 memprintf(err, "out of memory while indexing pattern");
1239 return 0;
1240 }
1241 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001242
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243 /* chain pattern in the expression */
Willy Tarreau2b718102021-04-21 07:32:39 +02001244 LIST_APPEND(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001245 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001246 patl->from_ref = pat->ref->list_head;
1247 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001248 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001249 expr->ref->entry_cnt++;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001250
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001251 /* that's ok */
1252 return 1;
1253}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001254
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001255int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1256{
1257 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001258
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001259 /* allocate pattern */
1260 patl = calloc(1, sizeof(*patl));
1261 if (!patl) {
1262 memprintf(err, "out of memory while indexing pattern");
1263 return 0;
1264 }
1265
1266 /* duplicate pattern */
1267 memcpy(&patl->pat, pat, sizeof(*pat));
1268 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1269 if (!patl->pat.ptr.str) {
1270 free(patl);
1271 memprintf(err, "out of memory while indexing pattern");
1272 return 0;
1273 }
1274 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1275 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001276
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001277 /* chain pattern in the expression */
Willy Tarreau2b718102021-04-21 07:32:39 +02001278 LIST_APPEND(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001279 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001280 patl->from_ref = pat->ref->list_head;
1281 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001282 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001283 expr->ref->entry_cnt++;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001284
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001285 /* that's ok */
1286 return 1;
1287}
1288
Thierry Fournier8feaa662016-02-10 22:55:20 +01001289int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001290{
1291 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001292
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001293 /* allocate pattern */
1294 patl = calloc(1, sizeof(*patl));
1295 if (!patl) {
1296 memprintf(err, "out of memory while indexing pattern");
1297 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001298 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001299
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001300 /* duplicate pattern */
1301 memcpy(&patl->pat, pat, sizeof(*pat));
1302
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001303 /* compile regex */
Willy Tarreau9b5c8bb2020-11-02 19:16:23 +01001304 patl->pat.sflags |= PAT_SF_REGFREE;
Dragan Dosen26743032019-04-30 15:54:36 +02001305 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1306 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001307 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001308 return 0;
1309 }
1310
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001311 /* chain pattern in the expression */
Willy Tarreau2b718102021-04-21 07:32:39 +02001312 LIST_APPEND(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001313 /* and from the reference */
Willy Tarreau38d41992020-11-03 14:50:29 +01001314 patl->from_ref = pat->ref->list_head;
1315 pat->ref->list_head = &patl->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001316 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001317 expr->ref->entry_cnt++;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001318
1319 /* that's ok */
1320 return 1;
1321}
1322
Thierry Fournier8feaa662016-02-10 22:55:20 +01001323int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1324{
1325 return pat_idx_list_reg_cap(expr, pat, 0, err);
1326}
1327
1328int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1329{
1330 return pat_idx_list_reg_cap(expr, pat, 1, err);
1331}
1332
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001333int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1334{
1335 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001336 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337
1338 /* Only IPv4 can be indexed */
1339 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001340 /* in IPv4 case, check if the mask is contiguous so that we can
1341 * insert the network into the tree. A continuous mask has only
1342 * ones on the left. This means that this mask + its lower bit
1343 * added once again is null.
1344 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001345 mask = ntohl(pat->val.ipv4.mask.s_addr);
1346 if (mask + (mask & -mask) == 0) {
1347 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001348
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001349 /* node memory allocation */
1350 node = calloc(1, sizeof(*node) + 4);
1351 if (!node) {
1352 memprintf(err, "out of memory while loading pattern");
1353 return 0;
1354 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001355
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001356 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001357 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001358 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001359
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001360 /* FIXME: insert <addr>/<mask> into the tree here */
1361 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1362 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001363
1364 /* Insert the entry. */
1365 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau38d41992020-11-03 14:50:29 +01001366 node->from_ref = pat->ref->tree_head;
1367 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001368 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001369 expr->ref->entry_cnt++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001370
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001371 /* that's ok */
1372 return 1;
1373 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001374 else {
1375 /* If the mask is not contiguous, just add the pattern to the list */
1376 return pat_idx_list_val(expr, pat, err);
1377 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001378 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001379 else if (pat->type == SMP_T_IPV6) {
1380 /* IPv6 also can be indexed */
1381 node = calloc(1, sizeof(*node) + 16);
1382 if (!node) {
1383 memprintf(err, "out of memory while loading pattern");
1384 return 0;
1385 }
1386
1387 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001388 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001389 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001390
1391 /* FIXME: insert <addr>/<mask> into the tree here */
1392 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1393 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001394
1395 /* Insert the entry. */
1396 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau38d41992020-11-03 14:50:29 +01001397 node->from_ref = pat->ref->tree_head;
1398 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001399 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001400 expr->ref->entry_cnt++;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001401
1402 /* that's ok */
1403 return 1;
1404 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001405
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001406 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001407}
1408
1409int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1410{
1411 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001412 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001413
1414 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001415 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001416 memprintf(err, "internal error: string expected, but the type is '%s'",
1417 smp_to_type[pat->type]);
1418 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001419 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001420
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001421 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001422 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001423 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001424
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001425 /* Process the key len */
1426 len = strlen(pat->ptr.str) + 1;
1427
1428 /* node memory allocation */
1429 node = calloc(1, sizeof(*node) + len);
1430 if (!node) {
1431 memprintf(err, "out of memory while loading pattern");
1432 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001433 }
1434
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001435 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001436 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001437 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001438
1439 /* copy the string */
1440 memcpy(node->node.key, pat->ptr.str, len);
1441
1442 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001443 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau38d41992020-11-03 14:50:29 +01001444 node->from_ref = pat->ref->tree_head;
1445 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001446 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001447 expr->ref->entry_cnt++;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001448
1449 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001450 return 1;
1451}
1452
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001453int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1454{
1455 int len;
1456 struct pattern_tree *node;
1457
1458 /* Only string can be indexed */
1459 if (pat->type != SMP_T_STR) {
1460 memprintf(err, "internal error: string expected, but the type is '%s'",
1461 smp_to_type[pat->type]);
1462 return 0;
1463 }
1464
1465 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1466 if (expr->mflags & PAT_MF_IGNORE_CASE)
1467 return pat_idx_list_str(expr, pat, err);
1468
1469 /* Process the key len */
1470 len = strlen(pat->ptr.str);
1471
1472 /* node memory allocation */
1473 node = calloc(1, sizeof(*node) + len + 1);
1474 if (!node) {
1475 memprintf(err, "out of memory while loading pattern");
1476 return 0;
1477 }
1478
1479 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001480 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001481 node->ref = pat->ref;
1482
1483 /* copy the string and the trailing zero */
1484 memcpy(node->node.key, pat->ptr.str, len + 1);
1485 node->node.node.pfx = len * 8;
1486
1487 /* index the new node */
1488 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau38d41992020-11-03 14:50:29 +01001489 node->from_ref = pat->ref->tree_head;
1490 pat->ref->tree_head = &node->from_ref;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001491 expr->ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001492 expr->ref->entry_cnt++;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001493
1494 /* that's ok */
1495 return 1;
1496}
1497
Willy Tarreauf1c08922020-11-02 19:53:16 +01001498/* Deletes all patterns from reference <elt>. Note that all of their
Willy Tarreau78777ea2020-11-02 13:55:22 +01001499 * expressions must be locked, and the pattern lock must be held as well.
1500 */
Willy Tarreauf1c08922020-11-02 19:53:16 +01001501void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001502{
Willy Tarreau38d41992020-11-03 14:50:29 +01001503 struct pattern_tree *tree;
1504 struct pattern_list *pat;
1505 void **node;
Willy Tarreauf1c08922020-11-02 19:53:16 +01001506
1507 /* delete all known tree nodes. They are all allocated inline */
Willy Tarreau38d41992020-11-03 14:50:29 +01001508 for (node = elt->tree_head; node;) {
1509 tree = container_of(node, struct pattern_tree, from_ref);
1510 node = *node;
Willy Tarreauf1c08922020-11-02 19:53:16 +01001511 BUG_ON(tree->ref != elt);
1512
1513 ebmb_delete(&tree->node);
Willy Tarreauf1c08922020-11-02 19:53:16 +01001514 free(tree->data);
1515 free(tree);
1516 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001517
Willy Tarreauf1c08922020-11-02 19:53:16 +01001518 /* delete all list nodes and free their pattern entries (str/reg) */
Willy Tarreau38d41992020-11-03 14:50:29 +01001519 for (node = elt->list_head; node;) {
1520 pat = container_of(node, struct pattern_list, from_ref);
1521 node = *node;
Willy Tarreau78777ea2020-11-02 13:55:22 +01001522 BUG_ON(pat->pat.ref != elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001523
1524 /* Delete and free entry. */
Willy Tarreau2b718102021-04-21 07:32:39 +02001525 LIST_DELETE(&pat->list);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001526 if (pat->pat.sflags & PAT_SF_REGFREE)
1527 regex_free(pat->pat.ptr.reg);
1528 else
1529 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001530 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001531 free(pat);
1532 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001533
Willy Tarreauf1c08922020-11-02 19:53:16 +01001534 /* update revision number to refresh the cache */
1535 ref->revision = rdtsc();
Dragan Dosena75eea72021-05-21 16:59:15 +02001536 ref->entry_cnt--;
Willy Tarreau38d41992020-11-03 14:50:29 +01001537 elt->tree_head = NULL;
1538 elt->list_head = NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001539}
1540
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001541void pattern_init_expr(struct pattern_expr *expr)
1542{
1543 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001544 expr->pattern_tree = EB_ROOT;
1545 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001546}
1547
1548void pattern_init_head(struct pattern_head *head)
1549{
1550 LIST_INIT(&head->head);
1551}
1552
1553/* The following functions are relative to the management of the reference
1554 * lists. These lists are used to store the original pattern and associated
1555 * value as string form.
1556 *
1557 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001558 *
1559 * The pattern reference are stored with two identifiers: the unique_id and
1560 * the reference.
1561 *
1562 * The reference identify a file. Each file with the same name point to the
1563 * same reference. We can register many times one file. If the file is modified,
1564 * all his dependencies are also modified. The reference can be used with map or
1565 * acl.
1566 *
1567 * The unique_id identify inline acl. The unique id is unique for each acl.
1568 * You cannot force the same id in the configuration file, because this repoort
1569 * an error.
1570 *
1571 * A particular case appears if the filename is a number. In this case, the
1572 * unique_id is set with the number represented by the filename and the
1573 * reference is also set. This method prevent double unique_id.
1574 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575 */
1576
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001577/* This function looks up a reference by name. If the reference is found, a
1578 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001579 */
1580struct pat_ref *pat_ref_lookup(const char *reference)
1581{
1582 struct pat_ref *ref;
1583
1584 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001585 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001586 return ref;
1587 return NULL;
1588}
1589
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001590/* This function looks up a reference's unique id. If the reference is found, a
1591 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001592 */
1593struct pat_ref *pat_ref_lookupid(int unique_id)
1594{
1595 struct pat_ref *ref;
1596
1597 list_for_each_entry(ref, &pattern_reference, list)
1598 if (ref->unique_id == unique_id)
1599 return ref;
1600 return NULL;
1601}
1602
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001603/* This function removes from the pattern reference <ref> all the patterns
1604 * attached to the reference element <elt>, and the element itself. The
1605 * reference must be locked.
1606 */
1607void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt)
1608{
1609 struct pattern_expr *expr;
1610 struct bref *bref, *back;
1611
1612 /*
1613 * we have to unlink all watchers from this reference pattern. We must
1614 * not relink them if this elt was the last one in the list.
1615 */
1616 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001617 LIST_DELETE(&bref->users);
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001618 LIST_INIT(&bref->users);
1619 if (elt->list.n != &ref->head)
Willy Tarreau2b718102021-04-21 07:32:39 +02001620 LIST_APPEND(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001621 bref->ref = elt->list.n;
1622 }
1623
1624 /* delete all entries from all expressions for this pattern */
1625 list_for_each_entry(expr, &ref->pat, list)
1626 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1627
1628 pat_delete_gen(ref, elt);
1629
1630 list_for_each_entry(expr, &ref->pat, list)
1631 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
1632
Willy Tarreau2b718102021-04-21 07:32:39 +02001633 LIST_DELETE(&elt->list);
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001634 free(elt->sample);
1635 free(elt->pattern);
1636 free(elt);
1637}
1638
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001639/* This function removes all the patterns matching the pointer <refelt> from
1640 * the reference and from each expr member of this reference. This function
1641 * returns 1 if the entry was found and deleted, otherwise zero.
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001642 */
1643int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1644{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001645 struct pat_ref_elt *elt, *safe;
1646
1647 /* delete pattern from reference */
1648 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1649 if (elt == refelt) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001650 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001651 return 1;
1652 }
1653 }
1654 return 0;
1655}
1656
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001657/* This function removes all patterns matching <key> from the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001658 * and from each expr member of the reference. This function returns 1
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001659 * if the deletion is done and returns 0 is the entry is not found.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001660 */
1661int pat_ref_delete(struct pat_ref *ref, const char *key)
1662{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001663 struct pat_ref_elt *elt, *safe;
1664 int found = 0;
1665
1666 /* delete pattern from reference */
1667 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1668 if (strcmp(key, elt->pattern) == 0) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001669 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001670 found = 1;
1671 }
1672 }
1673
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001674 return found;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675}
1676
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001677/*
1678 * find and return an element <elt> matching <key> in a reference <ref>
1679 * return NULL if not found
1680 */
1681struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1682{
1683 struct pat_ref_elt *elt;
1684
1685 list_for_each_entry(elt, &ref->head, list) {
1686 if (strcmp(key, elt->pattern) == 0)
1687 return elt;
1688 }
1689
1690 return NULL;
1691}
1692
1693
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001694/* This function modifies the sample of pat_ref_elt <elt> in all expressions
1695 * found under <ref> to become <value>. It is assumed that the caller has
1696 * already verified that <elt> belongs to <ref>.
1697 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001698static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001699 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001700{
1701 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001702 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001703 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001704 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001705
1706 /* Try all needed converters. */
1707 list_for_each_entry(expr, &ref->pat, list) {
1708 if (!expr->pat_head->parse_smp)
1709 continue;
1710
1711 if (!expr->pat_head->parse_smp(value, &test)) {
1712 memprintf(err, "unable to parse '%s'", value);
1713 return 0;
1714 }
1715 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001716
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001717 /* Modify pattern from reference. */
1718 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001719 if (!sample) {
1720 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001721 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001722 }
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001723 /* Load sample in each reference. All the conversions are tested
1724 * below, normally these calls don't fail.
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001725 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001726 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001727 if (!expr->pat_head->parse_smp)
1728 continue;
1729
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001730 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001731 data = pattern_find_smp(expr, elt);
1732 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1733 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001734 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001735 }
1736
Emeric Brunb5997f72017-07-03 11:34:05 +02001737 /* free old sample only when all exprs are updated */
1738 free(elt->sample);
1739 elt->sample = sample;
1740
1741
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001742 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001743}
1744
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001745/* This function modifies the sample of pat_ref_elt <refelt> in all expressions
1746 * found under <ref> to become <value>, after checking that <refelt> really
1747 * belongs to <ref>.
1748 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001749int 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 +01001750{
1751 struct pat_ref_elt *elt;
1752
1753 /* Look for pattern in the reference. */
1754 list_for_each_entry(elt, &ref->head, list) {
1755 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001756 if (!pat_ref_set_elt(ref, elt, value, err))
1757 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001758 return 1;
1759 }
1760 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001761
1762 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001763 return 0;
1764}
1765
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001766/* This function modifies to <value> the sample of all patterns matching <key>
1767 * under <ref>.
1768 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001769int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001770{
1771 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001772 int found = 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001773
1774 /* Look for pattern in the reference. */
1775 list_for_each_entry(elt, &ref->head, list) {
1776 if (strcmp(key, elt->pattern) == 0) {
Valentine Krasnobaeva697d9f52024-08-12 15:32:00 +02001777 char *tmp_err = NULL;
1778
1779 if (!pat_ref_set_elt(ref, elt, value, &tmp_err)) {
Willy Tarreauf8f1fc72024-09-10 08:55:29 +02001780 if (err)
1781 *err = tmp_err;
1782 else
1783 ha_free(&tmp_err);
Valentine Krasnobaevad9bae702024-08-12 19:21:00 +02001784 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001785 }
1786 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001787 }
1788 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001789
1790 if (!found) {
1791 memprintf(err, "entry not found");
1792 return 0;
1793 }
1794 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001795}
1796
Joseph Herlant4189d672018-11-15 10:22:31 -08001797/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001798 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1799 * be unique. The user must check the reference with "pat_ref_lookup()"
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001800 * before calling this function. If the function fails, it returns NULL,
1801 * otherwise it returns the new struct pat_ref.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001802 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001803struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001804{
1805 struct pat_ref *ref;
1806
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001807 ref = calloc(1, sizeof(*ref));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001808 if (!ref)
1809 return NULL;
1810
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001811 if (display) {
1812 ref->display = strdup(display);
1813 if (!ref->display) {
1814 free(ref);
1815 return NULL;
1816 }
1817 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001818
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001819 ref->reference = strdup(reference);
1820 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001821 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001822 free(ref);
1823 return NULL;
1824 }
1825
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001826 ref->flags = flags;
1827 ref->unique_id = -1;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001828 ref->revision = 0;
Dragan Dosena75eea72021-05-21 16:59:15 +02001829 ref->entry_cnt = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001830
1831 LIST_INIT(&ref->head);
1832 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001833 HA_SPIN_INIT(&ref->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02001834 LIST_APPEND(&pattern_reference, &ref->list);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001835
1836 return ref;
1837}
1838
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001839/* This function creates a new reference. <unique_id> is the unique id. If
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001840 * the value of <unique_id> is -1, the unique id is calculated later.
1841 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1842 * be unique. The user must check the reference with "pat_ref_lookup()"
1843 * or pat_ref_lookupid before calling this function. If the function
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001844 * fails, it returns NULL, otherwise it returns the new struct pat_ref.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001845 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001846struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001847{
1848 struct pat_ref *ref;
1849
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001850 ref = calloc(1, sizeof(*ref));
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001851 if (!ref)
1852 return NULL;
1853
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001854 if (display) {
1855 ref->display = strdup(display);
1856 if (!ref->display) {
1857 free(ref);
1858 return NULL;
1859 }
1860 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001861
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001862 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001863 ref->flags = flags;
Willy Tarreau29947742020-10-28 11:43:49 +01001864 ref->curr_gen = 0;
1865 ref->next_gen = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001866 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001867 LIST_INIT(&ref->head);
1868 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001869 HA_SPIN_INIT(&ref->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02001870 LIST_APPEND(&pattern_reference, &ref->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001871
1872 return ref;
1873}
1874
Willy Tarreauf4edb722020-10-28 10:52:46 +01001875/* This function adds entry to <ref>. It can fail on memory error. It returns
1876 * the newly added element on success, or NULL on failure. The PATREF_LOCK on
Willy Tarreau29947742020-10-28 11:43:49 +01001877 * <ref> must be held. It sets the newly created pattern's generation number
1878 * to the same value as the reference's.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001879 */
Willy Tarreauf4edb722020-10-28 10:52:46 +01001880struct 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 +01001881{
1882 struct pat_ref_elt *elt;
1883
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001884 elt = calloc(1, sizeof(*elt));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001885 if (!elt)
Willy Tarreauf4edb722020-10-28 10:52:46 +01001886 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001887
Willy Tarreau29947742020-10-28 11:43:49 +01001888 elt->gen_id = ref->curr_gen;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001889 elt->line = line;
1890
1891 elt->pattern = strdup(pattern);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001892 if (!elt->pattern)
1893 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001894
1895 if (sample) {
1896 elt->sample = strdup(sample);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001897 if (!elt->sample)
1898 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001899 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001900
Emeric Brun8d85aa42017-06-29 15:40:33 +02001901 LIST_INIT(&elt->back_refs);
Willy Tarreau38d41992020-11-03 14:50:29 +01001902 elt->list_head = NULL;
1903 elt->tree_head = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +02001904 LIST_APPEND(&ref->head, &elt->list);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001905 return elt;
1906 fail:
1907 if (elt)
1908 free(elt->pattern);
1909 free(elt);
1910 return NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001911}
1912
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001913/* This function creates sample found in <elt>, parses the pattern also
1914 * found in <elt> and inserts it in <expr>. The function copies <patflags>
1915 * into <expr>. If the function fails, it returns 0 and <err> is filled.
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001916 * In success case, the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001917 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001918int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1919 int patflags, char **err)
1920{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001921 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001922 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001923
1924 /* Create sample */
1925 if (elt->sample && expr->pat_head->parse_smp) {
1926 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001927 data = malloc(sizeof(*data));
1928 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001929 return 0;
1930
1931 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001932 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001933 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001934 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001935 return 0;
1936 }
1937
1938 }
1939 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001940 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001941
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001942 /* initialise pattern */
1943 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001944 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001945 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001946
1947 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001948 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001949 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001950 return 0;
1951 }
1952
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001953 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001954 /* index pattern */
1955 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001956 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001957 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001958 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001959 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001960 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001961
1962 return 1;
1963}
1964
Willy Tarreau0439e5e2020-10-28 18:45:45 +01001965/* This function tries to commit entry <elt> into <ref>. The new entry must
1966 * have already been inserted using pat_ref_append(), and its generation number
1967 * may have been adjusted as it will not be changed. <err> must point to a NULL
1968 * pointer. The PATREF lock on <ref> must be held. All the pattern_expr for
1969 * this reference will be updated (parsing, indexing). On success, non-zero is
1970 * returned. On failure, all the operation is rolled back (the element is
1971 * deleted from all expressions and is freed), zero is returned and the error
1972 * pointer <err> may have been updated (and the caller must free it). Failure
1973 * causes include memory allocation, parsing error or indexing error.
1974 */
Willy Tarreaudc2410d2021-01-15 14:11:59 +01001975int pat_ref_commit_elt(struct pat_ref *ref, struct pat_ref_elt *elt, char **err)
Willy Tarreau0439e5e2020-10-28 18:45:45 +01001976{
1977 struct pattern_expr *expr;
1978
1979 list_for_each_entry(expr, &ref->pat, list) {
1980 if (!pat_ref_push(elt, expr, 0, err)) {
1981 pat_ref_delete_by_ptr(ref, elt);
1982 return 0;
1983 }
1984 }
1985 return 1;
1986}
1987
Willy Tarreau1a6857b2020-10-29 09:21:43 +01001988/* Loads <pattern>:<sample> into <ref> for generation <gen>. <sample> may be
1989 * NULL if none exists (e.g. ACL). If not needed, the generation number should
1990 * be set to ref->curr_gen. The error pointer must initially point to NULL. The
1991 * new entry will be propagated to all use places, involving allocation, parsing
1992 * and indexing. On error (parsing, allocation), the operation will be rolled
1993 * back, an error may be reported, and NULL will be reported. On success, the
1994 * freshly allocated element will be returned. The PATREF lock on <ref> must be
1995 * held during the operation.
1996 */
1997struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen,
1998 const char *pattern, const char *sample,
1999 int line, char **err)
2000{
2001 struct pat_ref_elt *elt;
2002
2003 elt = pat_ref_append(ref, pattern, sample, line);
2004 if (elt) {
2005 elt->gen_id = gen;
Willy Tarreaudc2410d2021-01-15 14:11:59 +01002006 if (!pat_ref_commit_elt(ref, elt, err))
Willy Tarreau1a6857b2020-10-29 09:21:43 +01002007 elt = NULL;
2008 } else
2009 memprintf(err, "out of memory error");
2010
2011 return elt;
2012}
2013
Willy Tarreau6a174072020-10-28 10:58:05 +01002014/* This function adds entry to <ref>. It can fail on memory error. The new
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002015 * entry is added at all the pattern_expr registered in this reference. The
Willy Tarreau6a174072020-10-28 10:58:05 +01002016 * function stops on the first error encountered. It returns 0 and <err> is
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002017 * filled. If an error is encountered, the complete add operation is cancelled.
2018 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002019 */
2020int pat_ref_add(struct pat_ref *ref,
2021 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002022 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002023{
Willy Tarreau1a6857b2020-10-29 09:21:43 +01002024 return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002025}
2026
Willy Tarreaua13afe62021-04-30 13:19:37 +02002027/* This function purges all elements from <ref> whose generation is included in
2028 * the range of <from> to <to> (inclusive), taking wrapping into consideration.
2029 * It will not purge more than <budget> entries at once, in order to remain
2030 * responsive. If budget is negative, no limit is applied.
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002031 * The caller must already hold the PATREF_LOCK on <ref>. The function will
2032 * take the PATEXP_LOCK on all expressions of the pattern as needed. It returns
2033 * non-zero on completion, or zero if it had to stop before the end after
2034 * <budget> was depleted.
2035 */
Willy Tarreaua13afe62021-04-30 13:19:37 +02002036int pat_ref_purge_range(struct pat_ref *ref, uint from, uint to, int budget)
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002037{
2038 struct pat_ref_elt *elt, *elt_bck;
2039 struct bref *bref, *bref_bck;
2040 struct pattern_expr *expr;
2041 int done;
2042
2043 list_for_each_entry(expr, &ref->pat, list)
2044 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
2045
2046 /* all expr are locked, we can safely remove all pat_ref */
2047
2048 /* assume completion for e.g. empty lists */
2049 done = 1;
2050 list_for_each_entry_safe(elt, elt_bck, &ref->head, list) {
Willy Tarreaua13afe62021-04-30 13:19:37 +02002051 if (elt->gen_id - from > to - from)
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002052 continue;
2053
2054 if (budget >= 0 && !budget--) {
2055 done = 0;
2056 break;
2057 }
2058
2059 /*
2060 * we have to unlink all watchers from this reference pattern. We must
2061 * not relink them if this elt was the last one in the list.
2062 */
2063 list_for_each_entry_safe(bref, bref_bck, &elt->back_refs, users) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002064 LIST_DELETE(&bref->users);
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002065 LIST_INIT(&bref->users);
2066 if (elt->list.n != &ref->head)
Willy Tarreau2b718102021-04-21 07:32:39 +02002067 LIST_APPEND(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002068 bref->ref = elt->list.n;
2069 }
2070
2071 /* delete the storage for all representations of this pattern. */
2072 pat_delete_gen(ref, elt);
2073
Willy Tarreau2b718102021-04-21 07:32:39 +02002074 LIST_DELETE(&elt->list);
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002075 free(elt->pattern);
2076 free(elt->sample);
2077 free(elt);
2078 }
2079
2080 list_for_each_entry(expr, &ref->pat, list)
2081 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
2082
Willy Tarreau94b9abe2020-10-28 18:23:49 +01002083 return done;
2084}
2085
Willy Tarreauae83e632020-11-03 10:37:31 +01002086/* This function prunes all entries of <ref> and all their associated
2087 * pattern_expr. It may return before the end of the list is reached,
2088 * returning 0, to yield, indicating to the caller that it must call it again.
2089 * until it returns non-zero. All patterns are purged, both current ones and
2090 * future or incomplete ones. This is used by "clear map" or "clear acl".
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002091 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002092int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002093{
Willy Tarreaua13afe62021-04-30 13:19:37 +02002094 return pat_ref_purge_range(ref, 0, ~0, 100);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002095}
2096
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002097/* This function looks up any existing reference <ref> in pattern_head <head>, and
2098 * returns the associated pattern_expr pointer if found, otherwise NULL.
2099 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002100struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2101{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002102 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002103
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002104 list_for_each_entry(expr, &head->head, list)
2105 if (expr->expr->ref == ref)
2106 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002107 return NULL;
2108}
2109
Joseph Herlant4189d672018-11-15 10:22:31 -08002110/* This function creates new pattern_expr associated to the reference <ref>.
2111 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002112 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2113 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002114 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002115 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002116 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002117 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002118struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002119 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002120{
2121 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002122 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002123
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002124 if (reuse)
2125 *reuse = 0;
2126
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002127 /* Memory and initialization of the chain element. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002128 list = calloc(1, sizeof(*list));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002129 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002130 memprintf(err, "out of memory");
2131 return NULL;
2132 }
2133
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002134 /* Look for existing similar expr. No that only the index, parse and
2135 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002136 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002137 */
2138 if (ref) {
2139 list_for_each_entry(expr, &ref->pat, list)
2140 if (expr->pat_head->index == head->index &&
2141 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002142 expr->pat_head->parse_smp == head->parse_smp &&
2143 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002144 break;
2145 if (&expr->list == &ref->pat)
2146 expr = NULL;
2147 }
2148 else
2149 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002150
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002151 /* If no similar expr was found, we create new expr. */
2152 if (!expr) {
2153 /* Get a lot of memory for the expr struct. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002154 expr = calloc(1, sizeof(*expr));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002155 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002156 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002157 memprintf(err, "out of memory");
2158 return NULL;
2159 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002160
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002161 /* Initialize this new expr. */
2162 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002163
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002164 /* Copy the pattern matching and indexing flags. */
2165 expr->mflags = patflags;
2166
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002167 /* This new pattern expression reference one of his heads. */
2168 expr->pat_head = head;
2169
Willy Tarreau2b718102021-04-21 07:32:39 +02002170 /* Link with ref, or to self to facilitate LIST_DELETE() */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002171 if (ref)
Willy Tarreau2b718102021-04-21 07:32:39 +02002172 LIST_APPEND(&ref->pat, &expr->list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002173 else
2174 LIST_INIT(&expr->list);
2175
2176 expr->ref = ref;
2177
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002178 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002179
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002180 /* We must free this pattern if it is no more used. */
2181 list->do_free = 1;
2182 }
2183 else {
2184 /* If the pattern used already exists, it is already linked
2185 * with ref and we must not free it.
2186 */
2187 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002188 if (reuse)
2189 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002190 }
2191
2192 /* The new list element reference the pattern_expr. */
2193 list->expr = expr;
2194
2195 /* Link the list element with the pattern_head. */
Willy Tarreau2b718102021-04-21 07:32:39 +02002196 LIST_APPEND(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002197 return expr;
2198}
2199
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002200/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2201 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002202 *
2203 * The file contains one key + value per line. Lines which start with '#' are
2204 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2205 * then the first "word" (series of non-space/tabs characters), and the value is
2206 * what follows this series of space/tab till the end of the line excluding
2207 * trailing spaces/tabs.
2208 *
2209 * Example :
2210 *
2211 * # this is a comment and is ignored
2212 * 62.212.114.60 1wt.eu \n
2213 * <-><-----------><---><----><---->
2214 * | | | | `--- trailing spaces ignored
2215 * | | | `-------- value
2216 * | | `--------------- middle spaces ignored
2217 * | `------------------------ key
2218 * `-------------------------------- leading spaces ignored
2219 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002220 * Return non-zero in case of success, otherwise 0.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002221 */
2222int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2223{
2224 FILE *file;
2225 char *c;
2226 int ret = 0;
2227 int line = 0;
2228 char *key_beg;
2229 char *key_end;
2230 char *value_beg;
2231 char *value_end;
2232
2233 file = fopen(filename, "r");
2234 if (!file) {
2235 memprintf(err, "failed to open pattern file <%s>", filename);
2236 return 0;
2237 }
2238
2239 /* now parse all patterns. The file may contain only one pattern
2240 * followed by one value per line. The start spaces, separator spaces
2241 * and and spaces are stripped. Each can contain comment started by '#'
2242 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002243 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002244 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002245 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002246
2247 /* ignore lines beginning with a dash */
2248 if (*c == '#')
2249 continue;
2250
2251 /* strip leading spaces and tabs */
2252 while (*c == ' ' || *c == '\t')
2253 c++;
2254
2255 /* empty lines are ignored too */
2256 if (*c == '\0' || *c == '\r' || *c == '\n')
2257 continue;
2258
2259 /* look for the end of the key */
2260 key_beg = c;
2261 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2262 c++;
2263
2264 key_end = c;
2265
2266 /* strip middle spaces and tabs */
2267 while (*c == ' ' || *c == '\t')
2268 c++;
2269
2270 /* look for the end of the value, it is the end of the line */
2271 value_beg = c;
2272 while (*c && *c != '\n' && *c != '\r')
2273 c++;
2274 value_end = c;
2275
2276 /* trim possibly trailing spaces and tabs */
2277 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2278 value_end--;
2279
2280 /* set final \0 and check entries */
2281 *key_end = '\0';
2282 *value_end = '\0';
2283
2284 /* insert values */
2285 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2286 memprintf(err, "out of memory");
2287 goto out_close;
2288 }
2289 }
2290
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002291 if (ferror(file)) {
2292 memprintf(err, "error encountered while reading <%s> : %s",
2293 filename, strerror(errno));
2294 goto out_close;
2295 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002296 /* success */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002297 ret = 1;
2298
2299 out_close:
2300 fclose(file);
2301 return ret;
2302}
2303
2304/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2305 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002306 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002307int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002308{
2309 FILE *file;
2310 char *c;
2311 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002312 int ret = 0;
2313 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002314
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 per
2322 * line. If the line contains spaces, they will be part of the pattern.
2323 * The pattern stops at the first CR, LF or EOF encountered.
2324 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002326 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +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
2338 arg = c;
2339 while (*c && *c != '\n' && *c != '\r')
2340 c++;
2341 *c = 0;
2342
2343 /* empty lines are ignored too */
2344 if (c == arg)
2345 continue;
2346
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002347 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002348 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2349 goto out_close;
2350 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002351 }
2352
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002353 if (ferror(file)) {
2354 memprintf(err, "error encountered while reading <%s> : %s",
2355 filename, strerror(errno));
2356 goto out_close;
2357 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002358 ret = 1; /* success */
2359
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002360 out_close:
2361 fclose(file);
2362 return ret;
2363}
2364
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002365int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002366 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002367 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002368{
2369 struct pat_ref *ref;
2370 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002371 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002372 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002373
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002374 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002375 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002376
2377 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002378 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002379 chunk_printf(&trash,
2380 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2381 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002384 if (!ref) {
2385 memprintf(err, "out of memory");
2386 return 0;
2387 }
2388
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002389 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002390 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002391 if (!pat_ref_read_from_file_smp(ref, filename, err))
2392 return 0;
2393 }
2394 else {
2395 if (!pat_ref_read_from_file(ref, filename, err))
2396 return 0;
2397 }
2398 }
2399 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002400 /* The reference already exists, check the map compatibility. */
2401
2402 /* If the load require samples and the flag PAT_REF_SMP is not set,
2403 * the reference doesn't contain sample, and cannot be used.
2404 */
2405 if (load_smp) {
2406 if (!(ref->flags & PAT_REF_SMP)) {
2407 memprintf(err, "The file \"%s\" is already used as one column file "
2408 "and cannot be used by as two column file.",
2409 filename);
2410 return 0;
2411 }
2412 }
2413 else {
2414 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2415 * set, the reference contains a sample, and cannot be used.
2416 */
2417 if (ref->flags & PAT_REF_SMP) {
2418 memprintf(err, "The file \"%s\" is already used as two column file "
2419 "and cannot be used by as one column file.",
2420 filename);
2421 return 0;
2422 }
2423 }
2424
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002425 /* Extends display */
2426 chunk_printf(&trash, "%s", ref->display);
2427 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2428 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2429 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002430 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002431 if (!ref->display) {
2432 memprintf(err, "out of memory");
2433 return 0;
2434 }
2435
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002436 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002437 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002438 }
2439
2440 /* Now, we can loading patterns from the reference. */
2441
2442 /* Lookup for existing reference in the head. If the reference
2443 * doesn't exists, create it.
2444 */
2445 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002446 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002447 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002448 if (!expr)
2449 return 0;
2450 }
2451
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002452 /* The returned expression may be not empty, because the function
2453 * "pattern_new_expr" lookup for similar pattern list and can
2454 * reuse a already filled pattern list. In this case, we can not
2455 * reload the patterns.
2456 */
2457 if (reuse)
2458 return 1;
2459
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002460 /* Load reference content in the pattern expression. */
2461 list_for_each_entry(elt, &ref->head, list) {
2462 if (!pat_ref_push(elt, expr, patflags, err)) {
2463 if (elt->line > 0)
2464 memprintf(err, "%s at line %d of file '%s'",
2465 *err, elt->line, filename);
2466 return 0;
2467 }
2468 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002469
2470 return 1;
2471}
2472
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002473/* This function executes a pattern match on a sample. It applies pattern <expr>
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07002474 * to sample <smp>. The function returns NULL if the sample don't match. It returns
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002475 * non-null if the sample match. If <fill> is true and the sample match, the
2476 * function returns the matched pattern. In many cases, this pattern can be a
2477 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002478 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002479struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002480{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002481 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002482 struct pattern *pat;
2483
2484 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002485 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002486 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002487 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002488 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002489 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002490 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002491 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002492 return &static_pattern;
2493 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002494
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002495 /* convert input to string */
2496 if (!sample_convert(smp, head->expect_type))
2497 return NULL;
2498
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002499 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002500 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002501 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002502 if (pat) {
2503 /* We duplicate the pattern cause it could be modified
2504 by another thread */
2505 if (pat != &static_pattern) {
2506 memcpy(&static_pattern, pat, sizeof(struct pattern));
2507 pat = &static_pattern;
2508 }
2509
2510 /* We also duplicate the sample data for
2511 same reason */
2512 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002513 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002514 case SMP_T_STR:
2515 static_sample_data.type = SMP_T_STR;
2516 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002517 static_sample_data.u.str.data = pat->data->u.str.data;
2518 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2519 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2520 memcpy(static_sample_data.u.str.area,
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002521 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002522 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002523 pat->data = &static_sample_data;
2524 break;
2525
Emeric Brunb5997f72017-07-03 11:34:05 +02002526 case SMP_T_IPV4:
2527 case SMP_T_IPV6:
2528 case SMP_T_SINT:
2529 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002530 pat->data = &static_sample_data;
2531 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002532 default:
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002533 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002534 pat->data = NULL;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002535 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002536 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002537 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002538 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002539 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002540 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002541 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002542 }
2543 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002544}
2545
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002546/* This function prunes the pattern expressions starting at pattern_head <head>. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002547void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002548{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002549 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002550
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002551 list_for_each_entry_safe(list, safe, &head->head, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002552 LIST_DELETE(&list->list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002553 if (list->do_free) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002554 LIST_DELETE(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002555 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002556 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002557 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002558 free(list->expr);
2559 }
2560 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002561 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002562}
2563
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002564/* This function searches occurrences of pattern reference element <ref> in
2565 * expression <expr> and returns a pointer to a pointer of the sample storage.
2566 * If <ref> is not found, NULL is returned.
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002567 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002568struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002569{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002570 struct ebmb_node *node;
2571 struct pattern_tree *elt;
2572 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002573
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002574 for (node = ebmb_first(&expr->pattern_tree);
2575 node;
2576 node = ebmb_next(node)) {
2577 elt = container_of(node, struct pattern_tree, node);
2578 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002579 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002580 }
2581
2582 for (node = ebmb_first(&expr->pattern_tree_2);
2583 node;
2584 node = ebmb_next(node)) {
2585 elt = container_of(node, struct pattern_tree, node);
2586 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002587 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002588 }
2589
2590 list_for_each_entry(pat, &expr->patterns, list)
2591 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002592 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002593
2594 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002595}
2596
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002597/* This function compares two pat_ref** on their unique_id, and returns -1/0/1
2598 * depending on their order (suitable for sorting).
2599 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002600static int cmp_pat_ref(const void *_a, const void *_b)
2601{
2602 struct pat_ref * const *a = _a;
2603 struct pat_ref * const *b = _b;
2604
2605 if ((*a)->unique_id < (*b)->unique_id)
2606 return -1;
2607 else if ((*a)->unique_id > (*b)->unique_id)
2608 return 1;
2609 return 0;
2610}
2611
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002612/* This function finalizes the configuration parsing. It sets all the
2613 * automatic ids.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002614 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002615int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002616{
Tim Duesterhusb584b442020-03-17 21:08:24 +01002617 size_t len = 0;
2618 size_t unassigned_pos = 0;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002619 int next_unique_id = 0;
Tim Duesterhusb584b442020-03-17 21:08:24 +01002620 size_t i, j;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002621 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002622 struct list pr = LIST_HEAD_INIT(pr);
2623
Willy Tarreau52bf8392020-03-08 00:42:37 +01002624 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002625
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002626 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002627 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002628 len++;
2629 if (ref->unique_id != -1)
2630 unassigned_pos++;
2631 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002632
Tim Duesterhusb584b442020-03-17 21:08:24 +01002633 if (len == 0) {
2634 return 0;
2635 }
2636
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002637 arr = calloc(len, sizeof(*arr));
2638 if (arr == NULL) {
2639 ha_alert("Out of memory error.\n");
2640 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002641 }
2642
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002643 i = 0;
2644 j = unassigned_pos;
2645 list_for_each_entry(ref, &pattern_reference, list) {
2646 if (ref->unique_id != -1)
2647 arr[i++] = ref;
2648 else
2649 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002650 }
2651
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002652 /* Sort first segment of array with user-defined unique ids for
2653 * fast lookup when generating unique ids
2654 */
2655 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2656
2657 /* Assign unique ids to the rest of the elements */
2658 for (i = unassigned_pos; i < len; i++) {
2659 do {
2660 arr[i]->unique_id = next_unique_id++;
2661 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2662 }
2663
2664 /* Sort complete array */
2665 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2666
2667 /* Convert back to linked list */
2668 for (i = 0; i < len; i++)
Willy Tarreau2b718102021-04-21 07:32:39 +02002669 LIST_APPEND(&pr, &arr[i]->list);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002670
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002671 /* swap root */
Willy Tarreau2b718102021-04-21 07:32:39 +02002672 LIST_INSERT(&pr, &pattern_reference);
2673 LIST_DELETE(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002674
2675 free(arr);
2676 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002677}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002678
2679static int pattern_per_thread_lru_alloc()
2680{
2681 if (!global.tune.pattern_cache)
2682 return 1;
2683 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2684 return !!pat_lru_tree;
2685}
2686
2687static void pattern_per_thread_lru_free()
2688{
2689 lru64_destroy(pat_lru_tree);
2690}
2691
2692REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2693REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);