blob: aa3ab24ed197c222b98f3ad3b630d554a27620e2 [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * include/types/pattern.h
3 * This file provides structures and types for ACLs.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_PATTERN_H
23#define _TYPES_PATTERN_H
24
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api-t.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020026#include <haproxy/list-t.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010027#include <common/regex.h>
28
29#include <types/sample.h>
30
Willy Tarreau8d2b7772020-05-27 10:58:19 +020031#include <import/ebmbtree.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010032
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010033/* Pattern matching function result.
34 *
35 * We're using a 3-state matching system to match samples against patterns in
36 * ACLs :
37 * - PASS : at least one pattern already matches
38 * - MISS : some data is missing to decide if some rules may finally match.
39 * - FAIL : no mattern may ever match
40 *
41 * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050042 * can make use of standard arithmetic for the truth tables below :
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010043 *
44 * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
45 * ------+----- -----+------+------+----- -----+------+------+-----
46 * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
47 * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
48 * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
49 *
50 * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
51 *
52 * For efficiency, the ACL return flags are directly mapped from the pattern
53 * match flags. A pattern can't return "MISS" since it's always presented an
54 * existing sample. So that leaves us with only two possible values :
55 * MATCH = 0
56 * NOMATCH = 3
57 */
Willy Tarreau0cba6072013-11-28 22:21:02 +010058enum pat_match_res {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010059 PAT_NOMATCH = 0, /* sample didn't match any pattern */
60 PAT_MATCH = 3, /* sample matched at least one pattern */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010061};
62
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020063/* possible flags for patterns matching or parsing */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064enum {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020065 PAT_MF_IGNORE_CASE = 1 << 0, /* ignore case */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050066 PAT_MF_NO_DNS = 1 << 1, /* don't perform any DNS requests */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010067};
68
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020069/* possible flags for patterns storage */
70enum {
71 PAT_SF_TREE = 1 << 0, /* some patterns are arranged in a tree */
72};
73
Thierry FOURNIERed66c292013-11-28 11:05:19 +010074/* ACL match methods */
75enum {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010076 PAT_MATCH_FOUND, /* just ensure that fetch found the sample */
77 PAT_MATCH_BOOL, /* match fetch's integer value as boolean */
78 PAT_MATCH_INT, /* unsigned integer (int) */
79 PAT_MATCH_IP, /* IPv4/IPv6 address (IP) */
80 PAT_MATCH_BIN, /* hex string (bin) */
81 PAT_MATCH_LEN, /* string length (str -> int) */
82 PAT_MATCH_STR, /* exact string match (str) */
83 PAT_MATCH_BEG, /* beginning of string (str) */
84 PAT_MATCH_SUB, /* substring (str) */
85 PAT_MATCH_DIR, /* directory-like sub-string (str) */
86 PAT_MATCH_DOM, /* domain-like sub-string (str) */
87 PAT_MATCH_END, /* end of string (str) */
88 PAT_MATCH_REG, /* regex (str -> reg) */
Thierry Fournier8feaa662016-02-10 22:55:20 +010089 PAT_MATCH_REGM, /* regex (str -> reg) with match zones */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010090 /* keep this one last */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010091 PAT_MATCH_NUM
Thierry FOURNIERed66c292013-11-28 11:05:19 +010092};
93
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010094#define PAT_REF_MAP 0x1 /* Set if the reference is used by at least one map. */
95#define PAT_REF_ACL 0x2 /* Set if the reference is used by at least one acl. */
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +010096#define PAT_REF_SMP 0x4 /* Flag used if the reference contains a sample. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010097
98/* This struct contain a list of reference strings for dunamically
99 * updatable patterns.
100 */
101struct pat_ref {
102 struct list list; /* Used to chain refs. */
103 unsigned int flags; /* flags PAT_REF_*. */
104 char *reference; /* The reference name. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100105 int unique_id; /* Each pattern reference have unique id. */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100106 char *display; /* String displayed to identify the pattern origin. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100107 struct list head; /* The head of the list of struct pat_ref_elt. */
108 struct list pat; /* The head of the list of struct pattern_expr. */
Willy Tarreauaf613e82020-06-05 08:40:51 +0200109 __decl_thread(HA_SPINLOCK_T lock); /* Lock used to protect pat ref elements */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100110};
111
112/* This is a part of struct pat_ref. Each entry contain one
113 * pattern and one associated value as original string.
114 */
115struct pat_ref_elt {
116 struct list list; /* Used to chain elements. */
Emeric Brun8d85aa42017-06-29 15:40:33 +0200117 struct list back_refs; /* list of users tracking this pat ref */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100118 char *pattern;
119 char *sample;
120 int line;
121};
122
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100123/* This contain each tree indexed entry. This struct permit to associate
124 * "sample" with a tree entry. It is used with maps.
125 */
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100126struct pattern_tree {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200127 struct sample_data *data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100128 struct pat_ref_elt *ref;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100129 struct ebmb_node node;
130};
131
132/* This describes one ACL pattern, which might be a single value or a tree of
133 * values. All patterns for a single ACL expression are linked together. Some
134 * of them might have a type (eg: IP). Right now, the types are shared with
135 * the samples, though it is possible that in the future this will change to
136 * accommodate for other types (eg: meth, regex). Unsigned and constant types
137 * are preferred when there is a doubt.
138 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100139struct pattern {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100140 int type; /* type of the ACL pattern (SMP_T_*) */
141 union {
142 int i; /* integer value */
143 struct {
144 signed long long min, max;
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100145 unsigned int min_set:1;
146 unsigned int max_set:1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100147 } range; /* integer range */
148 struct {
149 struct in_addr addr;
150 struct in_addr mask;
151 } ipv4; /* IPv4 address */
152 struct {
153 struct in6_addr addr;
154 unsigned char mask; /* number of bits */
155 } ipv6; /* IPv6 address/mask */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100156 struct eb_root *tree; /* tree storing all values if any */
157 } val; /* direct value */
158 union {
159 void *ptr; /* any data */
160 char *str; /* any string */
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100161 struct my_regex *reg; /* a compiled regex */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162 } ptr; /* indirect values, allocated */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100163 int len; /* data length when required */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200164 int sflags; /* flags relative to the storage method. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200165 struct sample_data *data; /* used to store a pointer to sample value associated
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166 with the match. It is used with maps */
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100167 struct pat_ref_elt *ref;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168};
169
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100170/* This struct is just used for chaining patterns */
171struct pattern_list {
172 struct list list;
173 struct pattern pat;
174};
175
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100176/* Description of a pattern expression.
177 * It contains pointers to the parse and match functions, and a list or tree of
178 * patterns to test against. The structure is organized so that the hot parts
179 * are grouped together in order to optimize caching.
180 */
181struct pattern_expr {
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100182 struct list list; /* Used for chaining pattern_expr in pat_ref. */
Willy Tarreau72f073b2015-04-29 17:53:47 +0200183 unsigned long long revision; /* updated for each update */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100184 struct pat_ref *ref; /* The pattern reference if exists. */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100185 struct pattern_head *pat_head; /* Point to the pattern_head that contain manipulation functions.
186 * Note that this link point on compatible head but not on the real
187 * head. You can use only the function, and you must not use the
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500188 * "head". Don't write "(struct pattern_expr *)any->pat_head->expr".
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100189 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100190 struct list patterns; /* list of acl_patterns */
191 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
192 struct eb_root pattern_tree_2; /* may be used for different types */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200193 int mflags; /* flags relative to the parsing or matching method. */
Willy Tarreauaf613e82020-06-05 08:40:51 +0200194 __decl_thread(HA_RWLOCK_T lock); /* lock used to protect patterns */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100195};
196
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100197/* This is a list of expression. A struct pattern_expr can be used by
198 * more than one "struct pattern_head". this intermediate struct
199 * permit more than one list.
200 */
201struct pattern_expr_list {
202 struct list list; /* Used for chaining pattern_expr in pattern_head. */
203 int do_free;
204 struct pattern_expr *expr; /* The used expr. */
205};
206
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100207/* This struct contain a list of pattern expr */
208struct pattern_head {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200209 int (*parse)(const char *text, struct pattern *pattern, int flags, char **err);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200210 int (*parse_smp)(const char *text, struct sample_data *data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100211 int (*index)(struct pattern_expr *, struct pattern *, char **);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +0100212 void (*delete)(struct pattern_expr *, struct pat_ref_elt *);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100213 void (*prune)(struct pattern_expr *);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100214 struct pattern *(*match)(struct sample *, struct pattern_expr *, int);
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100215 int expect_type; /* type of the expected sample (SMP_T_*) */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100216
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100217 struct list head; /* This is a list of struct pattern_expr_list. */
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100218};
219
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100220/* This is the root of the list of all pattern_ref avalaibles. */
221extern struct list pattern_reference;
222
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223#endif /* _TYPES_PATTERN_H */