blob: 170955f02fd68285cb3bb5ae62d7ef51b2aa510b [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>
15
16#include <common/config.h>
17#include <common/standard.h>
18
19#include <types/global.h>
20#include <types/pattern.h>
21
22#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010023#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024
25#include <ebsttree.h>
26
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010027char *pat_match_names[PAT_MATCH_NUM] = {
28 [PAT_MATCH_FOUND] = "found",
29 [PAT_MATCH_BOOL] = "bool",
30 [PAT_MATCH_INT] = "int",
31 [PAT_MATCH_IP] = "ip",
32 [PAT_MATCH_BIN] = "bin",
33 [PAT_MATCH_LEN] = "len",
34 [PAT_MATCH_STR] = "str",
35 [PAT_MATCH_BEG] = "beg",
36 [PAT_MATCH_SUB] = "sub",
37 [PAT_MATCH_DIR] = "dir",
38 [PAT_MATCH_DOM] = "dom",
39 [PAT_MATCH_END] = "end",
40 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010041};
42
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044 [PAT_MATCH_FOUND] = pat_parse_nothing,
45 [PAT_MATCH_BOOL] = pat_parse_nothing,
46 [PAT_MATCH_INT] = pat_parse_int,
47 [PAT_MATCH_IP] = pat_parse_ip,
48 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010049 [PAT_MATCH_LEN] = pat_parse_len,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_STR] = pat_parse_str,
51 [PAT_MATCH_BEG] = pat_parse_str,
52 [PAT_MATCH_SUB] = pat_parse_str,
53 [PAT_MATCH_DIR] = pat_parse_str,
54 [PAT_MATCH_DOM] = pat_parse_str,
55 [PAT_MATCH_END] = pat_parse_str,
56 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057};
58
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010059int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
60 [PAT_MATCH_FOUND] = pat_idx_list_val,
61 [PAT_MATCH_BOOL] = pat_idx_list_val,
62 [PAT_MATCH_INT] = pat_idx_list_val,
63 [PAT_MATCH_IP] = pat_idx_tree_ip,
64 [PAT_MATCH_BIN] = pat_idx_list_ptr,
65 [PAT_MATCH_LEN] = pat_idx_list_val,
66 [PAT_MATCH_STR] = pat_idx_tree_str,
67 [PAT_MATCH_BEG] = pat_idx_list_str,
68 [PAT_MATCH_SUB] = pat_idx_list_str,
69 [PAT_MATCH_DIR] = pat_idx_list_str,
70 [PAT_MATCH_DOM] = pat_idx_list_str,
71 [PAT_MATCH_END] = pat_idx_list_str,
72 [PAT_MATCH_REG] = pat_idx_list_reg,
73};
74
Thierry FOURNIERb1136502014-01-15 11:38:49 +010075void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *) = {
76 [PAT_MATCH_FOUND] = pat_del_list_val,
77 [PAT_MATCH_BOOL] = pat_del_list_val,
78 [PAT_MATCH_INT] = pat_del_list_val,
79 [PAT_MATCH_IP] = pat_del_tree_ip,
80 [PAT_MATCH_BIN] = pat_del_list_ptr,
81 [PAT_MATCH_LEN] = pat_del_list_val,
82 [PAT_MATCH_STR] = pat_del_tree_str,
83 [PAT_MATCH_BEG] = pat_del_list_str,
84 [PAT_MATCH_SUB] = pat_del_list_str,
85 [PAT_MATCH_DIR] = pat_del_list_str,
86 [PAT_MATCH_DOM] = pat_del_list_str,
87 [PAT_MATCH_END] = pat_del_list_str,
88 [PAT_MATCH_REG] = pat_del_list_reg,
89};
90
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010091void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
92 [PAT_MATCH_FOUND] = pat_prune_val,
93 [PAT_MATCH_BOOL] = pat_prune_val,
94 [PAT_MATCH_INT] = pat_prune_val,
95 [PAT_MATCH_IP] = pat_prune_val,
96 [PAT_MATCH_BIN] = pat_prune_ptr,
97 [PAT_MATCH_LEN] = pat_prune_val,
98 [PAT_MATCH_STR] = pat_prune_ptr,
99 [PAT_MATCH_BEG] = pat_prune_ptr,
100 [PAT_MATCH_SUB] = pat_prune_ptr,
101 [PAT_MATCH_DIR] = pat_prune_ptr,
102 [PAT_MATCH_DOM] = pat_prune_ptr,
103 [PAT_MATCH_END] = pat_prune_ptr,
104 [PAT_MATCH_REG] = pat_prune_reg,
105};
106
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100107struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100108 [PAT_MATCH_FOUND] = NULL,
109 [PAT_MATCH_BOOL] = pat_match_nothing,
110 [PAT_MATCH_INT] = pat_match_int,
111 [PAT_MATCH_IP] = pat_match_ip,
112 [PAT_MATCH_BIN] = pat_match_bin,
113 [PAT_MATCH_LEN] = pat_match_len,
114 [PAT_MATCH_STR] = pat_match_str,
115 [PAT_MATCH_BEG] = pat_match_beg,
116 [PAT_MATCH_SUB] = pat_match_sub,
117 [PAT_MATCH_DIR] = pat_match_dir,
118 [PAT_MATCH_DOM] = pat_match_dom,
119 [PAT_MATCH_END] = pat_match_end,
120 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100121};
122
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100123/* Just used for checking configuration compatibility */
124int pat_match_types[PAT_MATCH_NUM] = {
125 [PAT_MATCH_FOUND] = SMP_T_UINT,
126 [PAT_MATCH_BOOL] = SMP_T_UINT,
127 [PAT_MATCH_INT] = SMP_T_UINT,
128 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100129 [PAT_MATCH_BIN] = SMP_T_BIN,
130 [PAT_MATCH_LEN] = SMP_T_STR,
131 [PAT_MATCH_STR] = SMP_T_STR,
132 [PAT_MATCH_BEG] = SMP_T_STR,
133 [PAT_MATCH_SUB] = SMP_T_STR,
134 [PAT_MATCH_DIR] = SMP_T_STR,
135 [PAT_MATCH_DOM] = SMP_T_STR,
136 [PAT_MATCH_END] = SMP_T_STR,
137 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100138};
139
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100140/* this struct is used to return information */
141static struct pattern static_pattern;
142
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100143/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100144 *
145 * The following functions are not exported and are used by internals process
146 * of pattern matching
147 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100148 */
149
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100150/* Background: Fast way to find a zero byte in a word
151 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
152 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
153 *
154 * To look for 4 different byte values, xor the word with those bytes and
155 * then check for zero bytes:
156 *
157 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
158 * where <delimiter> is the 4 byte values to look for (as an uint)
159 * and <c> is the character that is being tested
160 */
161static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
162{
163 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
164 return (mask - 0x01010101) & ~mask & 0x80808080U;
165}
166
167static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
168{
169 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
170}
171
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100172
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100173/*
174 *
175 * These functions are exported and may be used by any other component.
176 *
177 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100178 * input value. The <text> contain the string to be parsed. <pattern>
179 * must be a preallocated pattern. The pat_parse_* functions fill this
180 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
181 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
182 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
183 * use "trash" or return pointers to the input strings. In both cases,
184 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
185 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100186 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100187 * In succes case, the pat_parse_* function return 1. If the function
188 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189 *
190 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100191
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100192/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100193int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100194{
195 return 1;
196}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100199int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100200{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100201 pattern->type = SMP_T_STR;
202 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100203 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100204 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206}
207
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100209int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100210{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100211 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100212
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100213 pattern->type = SMP_T_BIN;
214 pattern->expect_type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100215 trash = get_trash_chunk();
216 pattern->len = trash->size;
217 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100218 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219}
220
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100222int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100224 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100226 trash = get_trash_chunk();
227 if (trash->size < sizeof(*pattern->ptr.reg)) {
228 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
229 (int)sizeof(*pattern->ptr.reg), trash->size);
230 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231 }
232
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100233 pattern->ptr.reg = (struct my_regex *)trash->str;
234 pattern->ptr.reg->regstr = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100236 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100255int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
259 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100260 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100261
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100262 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100263 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100264 goto not_valid_range;
265
266 /* Search ':' or '-' separator. */
267 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
268 ptr++;
269
270 /* If separator not found. */
271 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100272 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
273 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100274 return 0;
275 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100276 pattern->val.range.max = pattern->val.range.min;
277 pattern->val.range.min_set = 1;
278 pattern->val.range.max_set = 1;
279 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100280 }
281
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100282 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100283 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
285 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100286
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100287 pattern->val.range.min_set = 0;
288 pattern->val.range.max_set = 1;
289 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100290 }
291
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 /* If separator is the last character. */
293 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100294 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100295 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297 pattern->val.range.min_set = 1;
298 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100299 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100300 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100301
302 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100303 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100304 goto not_valid_range;
305
306 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
307 goto not_valid_range;
308
309 if (pattern->val.range.min > pattern->val.range.max)
310 goto not_valid_range;
311
312 pattern->val.range.min_set = 1;
313 pattern->val.range.max_set = 1;
314 return 1;
315
316 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100317 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100318 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100319}
320
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100321int pat_parse_len(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100322{
323 int ret;
324
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100325 ret = pat_parse_int(text, pattern, err);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100326 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100327 return ret;
328}
329
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100330/* Parse a range of positive 2-component versions delimited by either ':' or
331 * '-'. The version consists in a major and a minor, both of which must be
332 * smaller than 65536, because internally they will be represented as a 32-bit
333 * integer.
334 * If only one version is read, it is set as both min and max. Just like for
335 * pure integers, an operator may be specified as the prefix, among this list
336 * of 5 :
337 *
338 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
339 *
340 * The default operator is "eq". It supports range matching. Ranges are
341 * rejected for other operators. The operator may be changed at any time.
342 * The operator is stored in the 'opaque' argument. This allows constructs
343 * such as the following one :
344 *
345 * acl obsolete_ssl ssl_req_proto lt 3
346 * acl unsupported_ssl ssl_req_proto gt 3.1
347 * acl valid_ssl ssl_req_proto 3.0-3.1
348 *
349 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100350int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100351{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100352 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->type = SMP_T_UINT;
355 pattern->expect_type = SMP_T_UINT;
356
357 /* Search ':' or '-' separator. */
358 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
359 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100361 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100362 if (*ptr == '\0' && ptr > text) {
363 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
364 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 return 0;
366 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 pattern->val.range.max = pattern->val.range.min;
368 pattern->val.range.min_set = 1;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100378 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 pattern->val.range.min_set = 0;
380 pattern->val.range.max_set = 1;
381 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100382 }
383
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100384 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (ptr == &text[strlen(text)-1]) {
386 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
387 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100388 return 0;
389 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100390 pattern->val.range.min_set = 1;
391 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100392 return 1;
393 }
394
395 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100396 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
397 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100398 return 0;
399 }
400 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100401 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100402 return 0;
403 }
404 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100405 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100406 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100407 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100408 pattern->val.range.min_set = 1;
409 pattern->val.range.max_set = 1;
410 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411}
412
413/* Parse an IP address and an optional mask in the form addr[/mask].
414 * The addr may either be an IPv4 address or a hostname. The mask
415 * may either be a dotted mask or a number of bits. Returns 1 if OK,
416 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
417 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100418int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100419{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100420 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100421 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100422 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100423 return 1;
424 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100425 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100426 pattern->type = SMP_T_IPV6;
427 return 1;
428 }
429 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100430 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100431 return 0;
432 }
433}
434
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100435/*
436 *
437 * These functions are exported and may be used by any other component.
438 *
439 * This fucntion just take a sample <smp> and check if this sample match
440 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
441 * PAT_NOMATCH.
442 *
443 */
444
445/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100446struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100447{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100448 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100449}
450
451
452/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100454{
455 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct ebmb_node *node;
457 char prev;
458 struct pattern_tree *elt;
459 struct pattern_list *lst;
460 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* convert input to string */
463 if (!sample_convert(smp, SMP_T_STR))
464 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100465
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100466 /* Lookup a string in the expression's pattern tree. */
467 if (!eb_is_empty(&expr->pattern_tree)) {
468 /* we may have to force a trailing zero on the test pattern */
469 prev = smp->data.str.str[smp->data.str.len];
470 if (prev)
471 smp->data.str.str[smp->data.str.len] = '\0';
472 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
473 if (prev)
474 smp->data.str.str[smp->data.str.len] = prev;
475
476 if (node) {
477 if (fill) {
478 elt = ebmb_entry(node, struct pattern_tree, node);
479 static_pattern.smp = elt->smp;
480 static_pattern.flags = PAT_F_TREE;
481 static_pattern.type = SMP_T_STR;
482 static_pattern.ptr.str = (char *)elt->node.key;
483 }
484 return &static_pattern;
485 }
486 }
487
488 /* look in the list */
489 list_for_each_entry(lst, &expr->patterns, list) {
490 pattern = &lst->pat;
491
492 if (pattern->len != smp->data.str.len)
493 continue;
494
495 icase = pattern->flags & PAT_F_IGNORE_CASE;
496 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
497 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
498 return pattern;
499 }
500
501 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100502}
503
504/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100505struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100506{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100507 struct pattern_list *lst;
508 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100509
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100510 /* Convert input to binary. */
511 if (!sample_convert(smp, SMP_T_BIN))
512 return NULL;
513
514 /* Look in the list. */
515 list_for_each_entry(lst, &expr->patterns, list) {
516 pattern = &lst->pat;
517
518 if (pattern->len != smp->data.str.len)
519 continue;
520
521 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
522 return pattern;
523 }
524
525 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100526}
527
528/* Executes a regex. It temporarily changes the data to add a trailing zero,
529 * and restores the previous character when leaving.
530 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100532{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100533 struct pattern_list *lst;
534 struct pattern *pattern;
535
536 /* convert input to string */
537 if (!sample_convert(smp, SMP_T_STR))
538 return NULL;
539
540 /* look in the list */
541 list_for_each_entry(lst, &expr->patterns, list) {
542 pattern = &lst->pat;
543
544 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
545 return pattern;
546 }
547 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100548}
549
550/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100551struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100552{
553 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 struct pattern_list *lst;
555 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100556
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100557 /* convert input to string */
558 if (!sample_convert(smp, SMP_T_STR))
559 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100560
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100561 list_for_each_entry(lst, &expr->patterns, list) {
562 pattern = &lst->pat;
563
564 if (pattern->len > smp->data.str.len)
565 continue;
566
567 icase = pattern->flags & PAT_F_IGNORE_CASE;
568 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
569 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
570 continue;
571
572 return pattern;
573 }
574 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100575}
576
577/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100579{
580 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100581 struct pattern_list *lst;
582 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100583
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100584 /* convert input to string */
585 if (!sample_convert(smp, SMP_T_STR))
586 return NULL;
587
588 list_for_each_entry(lst, &expr->patterns, list) {
589 pattern = &lst->pat;
590
591 if (pattern->len > smp->data.str.len)
592 continue;
593
594 icase = pattern->flags & PAT_F_IGNORE_CASE;
595 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
596 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
597 continue;
598
599 return pattern;
600 }
601 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100602}
603
604/* Checks that the pattern is included inside the tested string.
605 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
606 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100607struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100608{
609 int icase;
610 char *end;
611 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100612 struct pattern_list *lst;
613 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100614
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100615 /* convert input to string */
616 if (!sample_convert(smp, SMP_T_STR))
617 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100619 list_for_each_entry(lst, &expr->patterns, list) {
620 pattern = &lst->pat;
621
622 if (pattern->len > smp->data.str.len)
623 continue;
624
625 end = smp->data.str.str + smp->data.str.len - pattern->len;
626 icase = pattern->flags & PAT_F_IGNORE_CASE;
627 if (icase) {
628 for (c = smp->data.str.str; c <= end; c++) {
629 if (tolower(*c) != tolower(*pattern->ptr.str))
630 continue;
631 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
632 return pattern;
633 }
634 } else {
635 for (c = smp->data.str.str; c <= end; c++) {
636 if (*c != *pattern->ptr.str)
637 continue;
638 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
639 return pattern;
640 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100641 }
642 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100643 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100644}
645
646/* This one is used by other real functions. It checks that the pattern is
647 * included inside the tested string, but enclosed between the specified
648 * delimiters or at the beginning or end of the string. The delimiters are
649 * provided as an unsigned int made by make_4delim() and match up to 4 different
650 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
651 */
652static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
653{
654 int may_match, icase;
655 char *c, *end;
656 char *ps;
657 int pl;
658
659 pl = pattern->len;
660 ps = pattern->ptr.str;
661
662 while (pl > 0 && is_delimiter(*ps, delimiters)) {
663 pl--;
664 ps++;
665 }
666
667 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
668 pl--;
669
670 if (pl > smp->data.str.len)
671 return PAT_NOMATCH;
672
673 may_match = 1;
674 icase = pattern->flags & PAT_F_IGNORE_CASE;
675 end = smp->data.str.str + smp->data.str.len - pl;
676 for (c = smp->data.str.str; c <= end; c++) {
677 if (is_delimiter(*c, delimiters)) {
678 may_match = 1;
679 continue;
680 }
681
682 if (!may_match)
683 continue;
684
685 if (icase) {
686 if ((tolower(*c) == tolower(*ps)) &&
687 (strncasecmp(ps, c, pl) == 0) &&
688 (c == end || is_delimiter(c[pl], delimiters)))
689 return PAT_MATCH;
690 } else {
691 if ((*c == *ps) &&
692 (strncmp(ps, c, pl) == 0) &&
693 (c == end || is_delimiter(c[pl], delimiters)))
694 return PAT_MATCH;
695 }
696 may_match = 0;
697 }
698 return PAT_NOMATCH;
699}
700
701/* Checks that the pattern is included inside the tested string, but enclosed
702 * between the delimiters '?' or '/' or at the beginning or end of the string.
703 * Delimiters at the beginning or end of the pattern are ignored.
704 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100706{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100707 struct pattern_list *lst;
708 struct pattern *pattern;
709
710 /* convert input to string */
711 if (!sample_convert(smp, SMP_T_STR))
712 return NULL;
713
714 list_for_each_entry(lst, &expr->patterns, list) {
715 pattern = &lst->pat;
716 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
717 return pattern;
718 }
719 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100720}
721
722/* Checks that the pattern is included inside the tested string, but enclosed
723 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
724 * the string. Delimiters at the beginning or end of the pattern are ignored.
725 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100726struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100727{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100728 struct pattern_list *lst;
729 struct pattern *pattern;
730
731 /* convert input to string */
732 if (!sample_convert(smp, SMP_T_STR))
733 return NULL;
734
735 list_for_each_entry(lst, &expr->patterns, list) {
736 pattern = &lst->pat;
737 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
738 return pattern;
739 }
740 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100741}
742
743/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100745{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100746 struct pattern_list *lst;
747 struct pattern *pattern;
748
749 /* convert input to integer */
750 if (!sample_convert(smp, SMP_T_UINT))
751 return NULL;
752
753 list_for_each_entry(lst, &expr->patterns, list) {
754 pattern = &lst->pat;
755 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
756 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
757 return pattern;
758 }
759 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100760}
761
762/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100763struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100764{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100765 struct pattern_list *lst;
766 struct pattern *pattern;
767
768 /* convert input to string */
769 if (!sample_convert(smp, SMP_T_STR))
770 return NULL;
771
772 list_for_each_entry(lst, &expr->patterns, list) {
773 pattern = &lst->pat;
774 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
775 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
776 return pattern;
777 }
778 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100779}
780
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100781struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100782{
783 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100784 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100785 struct in_addr *s;
786 struct ebmb_node *node;
787 struct pattern_tree *elt;
788 struct pattern_list *lst;
789 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100790
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 /* convert input to addr */
792 if (!sample_convert(smp, SMP_T_ADDR))
793 return NULL;
794
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100795 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100796 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100797 /* Lookup an IPv4 address in the expression's pattern tree using
798 * the longest match method.
799 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100800 s = &smp->data.ipv4;
801 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
802 if (node) {
803 if (fill) {
804 elt = ebmb_entry(node, struct pattern_tree, node);
805 static_pattern.smp = elt->smp;
806 static_pattern.flags = PAT_F_TREE;
807 static_pattern.type = SMP_T_IPV4;
808 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
809 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
810 return NULL;
811 }
812 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100813 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100814
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100815 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
816 * sample address to IPv6 with the mapping method using the ::ffff:
817 * prefix, and try to lookup in the IPv6 tree.
818 */
819 memset(&tmp6, 0, 10);
820 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
821 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
822 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
823 if (node) {
824 if (fill) {
825 elt = ebmb_entry(node, struct pattern_tree, node);
826 static_pattern.smp = elt->smp;
827 static_pattern.flags = PAT_F_TREE;
828 static_pattern.type = SMP_T_IPV6;
829 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
830 static_pattern.val.ipv6.mask = elt->node.node.pfx;
831 }
832 return &static_pattern;
833 }
834 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100835
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100836 /* The input sample is IPv6. Try to match in the trees. */
837 if (smp->type == SMP_T_IPV6) {
838 /* Lookup an IPv6 address in the expression's pattern tree using
839 * the longest match method.
840 */
841 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
842 if (node) {
843 if (fill) {
844 elt = ebmb_entry(node, struct pattern_tree, node);
845 static_pattern.smp = elt->smp;
846 static_pattern.flags = PAT_F_TREE;
847 static_pattern.type = SMP_T_IPV6;
848 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
849 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100850 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100851 return &static_pattern;
852 }
853
854 /* Try to convert 6 to 4 when the start of the ipv6 address match the
855 * following forms :
856 * - ::ffff:ip:v4 (ipv4 mapped)
857 * - ::0000:ip:v4 (old ipv4 mapped)
858 * - 2002:ip:v4:: (6to4)
859 */
860 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
861 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
862 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
863 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
864 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
865 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
866 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
867 else
868 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
869 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
870
871 /* Lookup an IPv4 address in the expression's pattern tree using the longest
872 * match method.
873 */
874 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
875 if (node) {
876 if (fill) {
877 elt = ebmb_entry(node, struct pattern_tree, node);
878 static_pattern.smp = elt->smp;
879 static_pattern.flags = PAT_F_TREE;
880 static_pattern.type = SMP_T_IPV4;
881 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
882 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
883 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100885 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100887 }
888 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100889
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100890 /* Lookup in the list. the list contain only IPv4 patterns */
891 list_for_each_entry(lst, &expr->patterns, list) {
892 pattern = &lst->pat;
893
894 /* The input sample is IPv4, use it as is. */
895 if (smp->type == SMP_T_IPV4) {
896 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100898 else if (smp->type == SMP_T_IPV6) {
899 /* v4 match on a V6 sample. We want to check at least for
900 * the following forms :
901 * - ::ffff:ip:v4 (ipv4 mapped)
902 * - ::0000:ip:v4 (old ipv4 mapped)
903 * - 2002:ip:v4:: (6to4)
904 */
905 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
906 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
907 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
908 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
909 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100910 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100911 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
912 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
913 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100914 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100915 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100916 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100917 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100918
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100919 /* Check if the input sample match the current pattern. */
920 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100921 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100922 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100923 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100924}
925
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100926void free_pattern_tree(struct eb_root *root)
927{
928 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100929 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100930
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100931 node = eb_first(root);
932 while (node) {
933 next = eb_next(node);
934 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100935 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100936 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100937 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100938 node = next;
939 }
940}
941
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100942void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100943{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100944 struct pattern_list *pat, *tmp;
945
946 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
947 free(pat->pat.smp);
948 free(pat);
949 }
950
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100951 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100952 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100953 LIST_INIT(&expr->patterns);
954}
955
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100956void pat_prune_ptr(struct pattern_expr *expr)
957{
958 struct pattern_list *pat, *tmp;
959
960 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
961 free(pat->pat.ptr.ptr);
962 free(pat->pat.smp);
963 free(pat);
964 }
965
966 free_pattern_tree(&expr->pattern_tree);
967 free_pattern_tree(&expr->pattern_tree_2);
968 LIST_INIT(&expr->patterns);
969}
970
971void pat_prune_reg(struct pattern_expr *expr)
972{
973 struct pattern_list *pat, *tmp;
974
975 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
976 regex_free(pat->pat.ptr.ptr);
977 free(pat->pat.smp);
978 free(pat);
979 }
980
981 free_pattern_tree(&expr->pattern_tree);
982 free_pattern_tree(&expr->pattern_tree_2);
983 LIST_INIT(&expr->patterns);
984}
985
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100986void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100987{
988 LIST_INIT(&expr->patterns);
989 expr->pattern_tree = EB_ROOT_UNIQUE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100990 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100991}
992
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100993/*
994 *
995 * The following functions are used for the pattern indexation
996 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100997 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100998
999int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001000{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001001 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001002
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001003 /* allocate pattern */
1004 patl = calloc(1, sizeof(*patl));
1005 if (!patl) {
1006 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001007 return 0;
1008 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001009
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001010 /* duplicate pattern */
1011 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001012
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001013 /* chain pattern in the expression */
1014 LIST_ADDQ(&expr->patterns, &patl->list);
1015
1016 /* that's ok */
1017 return 1;
1018}
1019
1020int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1021{
1022 struct pattern_list *patl;
1023
1024 /* allocate pattern */
1025 patl = calloc(1, sizeof(*patl));
1026 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001027 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001028
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001029 /* duplicate pattern */
1030 memcpy(&patl->pat, pat, sizeof(*pat));
1031 patl->pat.ptr.ptr = malloc(patl->pat.len);
1032 if (!patl->pat.ptr.ptr) {
1033 free(patl);
1034 memprintf(err, "out of memory while indexing pattern");
1035 return 0;
1036 }
1037 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001038
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001039 /* chain pattern in the expression */
1040 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001041
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001042 /* that's ok */
1043 return 1;
1044}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001045
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001046int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1047{
1048 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001049
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001050 /* allocate pattern */
1051 patl = calloc(1, sizeof(*patl));
1052 if (!patl) {
1053 memprintf(err, "out of memory while indexing pattern");
1054 return 0;
1055 }
1056
1057 /* duplicate pattern */
1058 memcpy(&patl->pat, pat, sizeof(*pat));
1059 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1060 if (!patl->pat.ptr.str) {
1061 free(patl);
1062 memprintf(err, "out of memory while indexing pattern");
1063 return 0;
1064 }
1065 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1066 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001067
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001068 /* chain pattern in the expression */
1069 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001070
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001071 /* that's ok */
1072 return 1;
1073}
1074
1075int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1076{
1077 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001078
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001079 /* allocate pattern */
1080 patl = calloc(1, sizeof(*patl));
1081 if (!patl) {
1082 memprintf(err, "out of memory while indexing pattern");
1083 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001084 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001085
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001086 /* duplicate pattern */
1087 memcpy(&patl->pat, pat, sizeof(*pat));
1088
1089 /* allocate regex */
1090 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1091 if (!patl->pat.ptr.reg) {
1092 free(patl);
1093 memprintf(err, "out of memory while indexing pattern");
1094 return 0;
1095 }
1096
1097 /* compile regex */
1098 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1099 free(patl);
1100 free(patl->pat.ptr.reg);
1101 return 0;
1102 }
1103
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001104 /* chain pattern in the expression */
1105 LIST_ADDQ(&expr->patterns, &patl->list);
1106
1107 /* that's ok */
1108 return 1;
1109}
1110
1111int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1112{
1113 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001114 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001115
1116 /* Only IPv4 can be indexed */
1117 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001118 /* in IPv4 case, check if the mask is contiguous so that we can
1119 * insert the network into the tree. A continuous mask has only
1120 * ones on the left. This means that this mask + its lower bit
1121 * added once again is null.
1122 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001123 mask = ntohl(pat->val.ipv4.mask.s_addr);
1124 if (mask + (mask & -mask) == 0) {
1125 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001126
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001127 /* node memory allocation */
1128 node = calloc(1, sizeof(*node) + 4);
1129 if (!node) {
1130 memprintf(err, "out of memory while loading pattern");
1131 return 0;
1132 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001133
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001134 /* copy the pointer to sample associated to this node */
1135 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001136
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001137 /* FIXME: insert <addr>/<mask> into the tree here */
1138 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1139 node->node.node.pfx = mask;
1140 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1141 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001142
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001143 /* that's ok */
1144 return 1;
1145 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001146 else {
1147 /* If the mask is not contiguous, just add the pattern to the list */
1148 return pat_idx_list_val(expr, pat, err);
1149 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001150 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001151 else if (pat->type == SMP_T_IPV6) {
1152 /* IPv6 also can be indexed */
1153 node = calloc(1, sizeof(*node) + 16);
1154 if (!node) {
1155 memprintf(err, "out of memory while loading pattern");
1156 return 0;
1157 }
1158
1159 /* copy the pointer to sample associated to this node */
1160 node->smp = pat->smp;
1161
1162 /* FIXME: insert <addr>/<mask> into the tree here */
1163 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1164 node->node.node.pfx = pat->val.ipv6.mask;
1165 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1166 free(node); /* was a duplicate */
1167
1168 /* that's ok */
1169 return 1;
1170 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001171
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001172 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173}
1174
1175int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1176{
1177 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001178 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001179
1180 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001181 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001182 memprintf(err, "internal error: string expected, but the type is '%s'",
1183 smp_to_type[pat->type]);
1184 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001185 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001186
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001187 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1188 if (pat->flags & PAT_F_IGNORE_CASE)
1189 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001190
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001191 /* Process the key len */
1192 len = strlen(pat->ptr.str) + 1;
1193
1194 /* node memory allocation */
1195 node = calloc(1, sizeof(*node) + len);
1196 if (!node) {
1197 memprintf(err, "out of memory while loading pattern");
1198 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001199 }
1200
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001201 /* copy the pointer to sample associated to this node */
1202 node->smp = pat->smp;
1203
1204 /* copy the string */
1205 memcpy(node->node.key, pat->ptr.str, len);
1206
1207 /* index the new node */
1208 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1209 free(node); /* was a duplicate */
1210
1211 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001212 return 1;
1213}
1214
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001215void pat_del_list_val(struct pattern_expr *expr, struct pattern *pattern)
1216{
1217 struct pattern_list *pat;
1218 struct pattern_list *safe;
1219
1220 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1221 /* Check equality. */
1222 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1223 continue;
1224 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1225 continue;
1226 if (pattern->val.range.min_set &&
1227 pattern->val.range.min != pat->pat.val.range.min)
1228 continue;
1229 if (pattern->val.range.max_set &&
1230 pattern->val.range.max != pat->pat.val.range.max)
1231 continue;
1232
1233 /* Delete and free entry. */
1234 LIST_DEL(&pat->list);
1235 free(pat->pat.smp);
1236 free(pat);
1237 }
1238}
1239
1240void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1241{
1242 struct ebmb_node *node, *next_node;
1243 struct pattern_tree *elt;
1244 struct pattern_list *pat;
1245 struct pattern_list *safe;
1246 unsigned int mask;
1247
1248 /* browse each node of the tree for IPv4 addresses. */
1249 if (pattern->type == SMP_T_IPV4) {
1250 /* Convert mask. If the mask is contiguous, browse each node
1251 * of the tree for IPv4 addresses.
1252 */
1253 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1254 if (mask + (mask & -mask) == 0) {
1255 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1256
1257 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1258 node;
1259 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1260 /* Extract container of the tree node. */
1261 elt = container_of(node, struct pattern_tree, node);
1262
1263 /* Check equality. */
1264 if (memcmp(&pattern->val.ipv4.addr, elt->node.key,
1265 sizeof(pattern->val.ipv4.addr)) != 0)
1266 continue;
1267 if (elt->node.node.pfx != mask)
1268 continue;
1269
1270 /* Delete and free entry. */
1271 ebmb_delete(node);
1272 free(elt->smp);
1273 free(elt);
1274 }
1275 }
1276 else {
1277 /* Browse each node of the list for IPv4 addresses. */
1278 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1279 /* Check equality, addr then mask */
1280 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1281 sizeof(pat->pat.val.ipv4.addr)) != 0)
1282 continue;
1283
1284 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1285 sizeof(pat->pat.val.ipv4.addr)) != 0)
1286 continue;
1287
1288 /* Delete and free entry. */
1289 LIST_DEL(&pat->list);
1290 free(pat->pat.smp);
1291 free(pat);
1292 }
1293 }
1294 }
1295 else if (pattern->type == SMP_T_IPV6) {
1296 /* browse each node of the tree for IPv6 addresses. */
1297 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1298 node;
1299 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1300 /* Extract container of the tree node. */
1301 elt = container_of(node, struct pattern_tree, node);
1302
1303 /* Check equality. */
1304 if (memcmp(&pattern->val.ipv6.addr, elt->node.key,
1305 sizeof(pattern->val.ipv6.addr)) != 0)
1306 continue;
1307 if (elt->node.node.pfx != pattern->val.ipv6.mask)
1308 continue;
1309
1310 /* Delete and free entry. */
1311 ebmb_delete(node);
1312 free(elt->smp);
1313 free(elt);
1314 }
1315 }
1316}
1317
1318void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1319{
1320 struct pattern_list *pat;
1321 struct pattern_list *safe;
1322
1323 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1324 /* Check equality. */
1325 if (pattern->len != pat->pat.len)
1326 continue;
1327 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1328 continue;
1329
1330 /* Delete and free entry. */
1331 LIST_DEL(&pat->list);
1332 free(pat->pat.ptr.ptr);
1333 free(pat->pat.smp);
1334 free(pat);
1335 }
1336}
1337
1338void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1339{
1340 struct ebmb_node *node, *next_node;
1341 struct pattern_tree *elt;
1342
1343 /* browse each node of the tree. */
1344 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1345 node;
1346 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1347 /* Extract container of the tree node. */
1348 elt = container_of(node, struct pattern_tree, node);
1349
1350 /* Check equality. */
1351 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1352 continue;
1353
1354 /* Delete and free entry. */
1355 ebmb_delete(node);
1356 free(elt->smp);
1357 free(elt);
1358 }
1359}
1360
1361void pat_del_list_str(struct pattern_expr *expr, struct pattern *pattern)
1362{
1363 struct pattern_list *pat;
1364 struct pattern_list *safe;
1365
1366 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1367 /* Check equality. */
1368 if (pattern->len != pat->pat.len)
1369 continue;
1370 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1371 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1372 continue;
1373 }
1374 else {
1375 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1376 continue;
1377 }
1378
1379 /* Delete and free entry. */
1380 LIST_DEL(&pat->list);
1381 free(pat->pat.ptr.str);
1382 free(pat->pat.smp);
1383 free(pat);
1384 }
1385}
1386
1387void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1388{
1389 struct pattern_list *pat;
1390 struct pattern_list *safe;
1391
1392 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1393 /* Check equality. */
1394 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1395 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1396 continue;
1397 }
1398 else {
1399 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1400 continue;
1401 }
1402
1403 /* Delete and free entry. */
1404 LIST_DEL(&pat->list);
1405 regex_free(pat->pat.ptr.ptr);
1406 free(pat->pat.smp);
1407 free(pat);
1408 }
1409}
1410
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001411/* return 1 if the process is ok
1412 * return -1 if the parser fail. The err message is filled.
1413 * return -2 if out of memory
1414 */
1415int pattern_register(struct pattern_expr *expr, const char *arg,
1416 struct sample_storage *smp,
1417 int patflags, char **err)
1418{
1419 int ret;
1420 struct pattern pattern;
1421
1422 /* initialise pattern */
1423 memset(&pattern, 0, sizeof(pattern));
1424 pattern.flags = patflags;
1425 pattern.smp = smp;
1426
1427 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001428 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001429 if (!ret)
1430 return 0;
1431
1432 /* index pattern */
1433 if (!expr->index(expr, &pattern, err))
1434 return 0;
1435
1436 return 1;
1437}
1438
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001439/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1440 * be returned there on errors and the caller will have to free it.
1441 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001442int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001443 const char *filename, int patflags,
1444 char **err)
1445{
1446 FILE *file;
1447 char *c;
1448 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001449 int ret = 0;
1450 int line = 0;
1451 int code;
1452
1453 file = fopen(filename, "r");
1454 if (!file) {
1455 memprintf(err, "failed to open pattern file <%s>", filename);
1456 return 0;
1457 }
1458
1459 /* now parse all patterns. The file may contain only one pattern per
1460 * line. If the line contains spaces, they will be part of the pattern.
1461 * The pattern stops at the first CR, LF or EOF encountered.
1462 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001463 while (fgets(trash.str, trash.size, file) != NULL) {
1464 line++;
1465 c = trash.str;
1466
1467 /* ignore lines beginning with a dash */
1468 if (*c == '#')
1469 continue;
1470
1471 /* strip leading spaces and tabs */
1472 while (*c == ' ' || *c == '\t')
1473 c++;
1474
1475
1476 arg = c;
1477 while (*c && *c != '\n' && *c != '\r')
1478 c++;
1479 *c = 0;
1480
1481 /* empty lines are ignored too */
1482 if (c == arg)
1483 continue;
1484
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001485 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001486 if (code == -2) {
1487 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1488 goto out_close;
1489 }
1490 else if (code < 0) {
1491 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001492 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001493 }
1494 }
1495
1496 ret = 1; /* success */
1497
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001498 out_close:
1499 fclose(file);
1500 return ret;
1501}
1502
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001503/* This function executes a pattern match on a sample. It applies pattern <expr>
1504 * to sample <smp>. The function returns NULL if the sample dont match. It returns
1505 * non-null if the sample match. If <fill> is true and the sample match, the
1506 * function returns the matched pattern. In many cases, this pattern can be a
1507 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001508 */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001509struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001510{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001511 if (!expr->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001512 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001513 static_pattern.smp = NULL;
1514 static_pattern.flags = 0;
1515 static_pattern.type = SMP_T_UINT;
1516 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001517 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001518 return &static_pattern;
1519 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001520 return expr->match(smp, expr, fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001521}
1522
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001523/* This function prune the pattern expression. */
1524void pattern_prune(struct pattern_expr *expr)
1525{
1526 expr->prune(expr);
1527}
1528
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001529/* This function search all the pattern matching the <key> and delete it.
1530 * If the parsing of the input key fails, the function returns 0 and the
1531 * <err> is filled, else return 1;
1532 */
1533int pattern_delete(const char *key, struct pattern_expr *expr, char **err)
1534{
1535 struct pattern pattern;
1536
1537 if (!expr->parse(key, &pattern, err))
1538 return 0;
1539 expr->delete(expr, &pattern);
1540 return 1;
1541}
1542
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001543/* This function browse the pattern expr <expr> to lookup the key <key>. On
1544 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1545 * or <idx_elt> with the respectively matched pointers, and the other one with
1546 * NULL. Pointers are not set if they're passed as NULL.
1547 */
1548int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001549 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001550{
1551 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001552 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001553 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001554 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001555 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001556
1557 /* no real pattern */
1558 if (!expr->match || expr->match == pat_match_nothing)
1559 return 0;
1560
1561 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001562 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001563 return 0;
1564
1565 pat = NULL;
1566 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001567
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001568 /* Try to look up the tree first. IPv6 is not indexed */
1569 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001570 /* Check the pattern type */
1571 if (pattern.type != SMP_T_STR &&
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001572 pattern.type != SMP_T_IPV4) {
1573 memprintf(err, "Unexpected pattern type.");
1574 return 0;
1575 }
1576
1577 /* Convert mask. If the mask is not contiguous, ignore the lookup
1578 * in the tree, and browse the list.
1579 */
1580 if (expr->match == pat_match_ip) {
1581 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1582 if (mask + (mask & -mask) != 0)
1583 goto browse_list;
1584 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1585 }
1586
1587 /* browse each node of the tree, and check string */
1588 if (expr->match == pat_match_str) {
1589 for (node = ebmb_first(&expr->pattern_tree);
1590 node;
1591 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001592 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001593 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1594 goto found;
1595 }
1596 }
1597 else if (expr->match == pat_match_ip) {
1598 for (node = ebmb_first(&expr->pattern_tree);
1599 node;
1600 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001601 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001602 if (elt->node.node.pfx == mask &&
1603 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1604 goto found;
1605 }
1606 }
1607 }
1608
1609browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001610 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001611 if (expr->parse == pat_parse_int ||
1612 expr->parse == pat_parse_len) {
1613 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001614 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001615 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001616 if (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001617 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001618 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001619 continue;
1620 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001621 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001622 continue;
1623 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001624 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001625 continue;
1626 goto found;
1627 }
1628 }
1629 else if (expr->parse == pat_parse_ip) {
1630 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001631 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001632 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001633 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001634 continue;
1635 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001636 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001637 continue;
1638 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001639 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001640 continue;
1641 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001642 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001643 continue;
1644 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001645 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001646 continue;
1647 goto found;
1648 }
1649 }
1650 else if (expr->parse == pat_parse_str) {
1651 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001652 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001653 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001654 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001655 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001656 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1657 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001658 continue;
1659 }
1660 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001661 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001662 continue;
1663 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001664 goto found;
1665 }
1666 }
1667 else if (expr->parse == pat_parse_bin) {
1668 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001669 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001670 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001671 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001672 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001673 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001674 continue;
1675 goto found;
1676 }
1677 }
1678 else if (expr->parse == pat_parse_reg) {
1679 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001680 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001681 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001682 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1683 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001684 continue;
1685 }
1686 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001687 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001688 continue;
1689 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001690 goto found;
1691 }
1692 }
1693
1694 /* if we get there, we didn't find the pattern */
1695 return 0;
1696found:
1697 if (idx_elt)
1698 *idx_elt = elt;
1699
1700 if (pat_elt)
1701 *pat_elt = pat;
1702
1703 return 1;
1704}