blob: 839eae686f2c464d79bd6a77a9a70ea774e5f355 [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
25#include <common/compat.h>
26#include <common/config.h>
27#include <common/mini-clist.h>
28#include <common/regex.h>
29
30#include <types/sample.h>
31
32#include <ebmbtree.h>
33
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010034/* Pattern matching function result.
35 *
36 * We're using a 3-state matching system to match samples against patterns in
37 * ACLs :
38 * - PASS : at least one pattern already matches
39 * - MISS : some data is missing to decide if some rules may finally match.
40 * - FAIL : no mattern may ever match
41 *
42 * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
43 * can make use of standard arithmetics for the truth tables below :
44 *
45 * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
46 * ------+----- -----+------+------+----- -----+------+------+-----
47 * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
48 * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
49 * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
50 *
51 * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
52 *
53 * For efficiency, the ACL return flags are directly mapped from the pattern
54 * match flags. A pattern can't return "MISS" since it's always presented an
55 * existing sample. So that leaves us with only two possible values :
56 * MATCH = 0
57 * NOMATCH = 3
58 */
Willy Tarreau0cba6072013-11-28 22:21:02 +010059enum pat_match_res {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010060 PAT_NOMATCH = 0, /* sample didn't match any pattern */
61 PAT_MATCH = 3, /* sample matched at least one pattern */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062};
63
64/* possible flags for expressions or patterns */
65enum {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010066 PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
Thierry FOURNIER66eb9bf2014-02-11 16:19:46 +010067 PAT_F_TREE = 1 << 1, /* some patterns are arranged in a tree */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010068};
69
70/* ACL match methods */
71enum {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010072 PAT_MATCH_FOUND, /* just ensure that fetch found the sample */
73 PAT_MATCH_BOOL, /* match fetch's integer value as boolean */
74 PAT_MATCH_INT, /* unsigned integer (int) */
75 PAT_MATCH_IP, /* IPv4/IPv6 address (IP) */
76 PAT_MATCH_BIN, /* hex string (bin) */
77 PAT_MATCH_LEN, /* string length (str -> int) */
78 PAT_MATCH_STR, /* exact string match (str) */
79 PAT_MATCH_BEG, /* beginning of string (str) */
80 PAT_MATCH_SUB, /* substring (str) */
81 PAT_MATCH_DIR, /* directory-like sub-string (str) */
82 PAT_MATCH_DOM, /* domain-like sub-string (str) */
83 PAT_MATCH_END, /* end of string (str) */
84 PAT_MATCH_REG, /* regex (str -> reg) */
Thierry FOURNIERed66c292013-11-28 11:05:19 +010085 /* keep this one last */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010086 PAT_MATCH_NUM
Thierry FOURNIERed66c292013-11-28 11:05:19 +010087};
88
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010089#define PAT_REF_MAP 0x1 /* Set if the reference is used by at least one map. */
90#define PAT_REF_ACL 0x2 /* Set if the reference is used by at least one acl. */
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +010091#define PAT_REF_SMP 0x4 /* Flag used if the reference contains a sample. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010092
93/* This struct contain a list of reference strings for dunamically
94 * updatable patterns.
95 */
96struct pat_ref {
97 struct list list; /* Used to chain refs. */
98 unsigned int flags; /* flags PAT_REF_*. */
99 char *reference; /* The reference name. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100100 int unique_id; /* Each pattern reference have unique id. */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100101 char *display; /* String displayed to identify the pattern origin. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100102 struct list head; /* The head of the list of struct pat_ref_elt. */
103 struct list pat; /* The head of the list of struct pattern_expr. */
104};
105
106/* This is a part of struct pat_ref. Each entry contain one
107 * pattern and one associated value as original string.
108 */
109struct pat_ref_elt {
110 struct list list; /* Used to chain elements. */
111 char *pattern;
112 char *sample;
113 int line;
114};
115
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116/* How to store a time range and the valid days in 29 bits */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117struct pat_time {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100118 int dow:7; /* 1 bit per day of week: 0-6 */
119 int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
120 int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
121};
122
123/* 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 FOURNIERed66c292013-11-28 11:05:19 +0100127 struct sample_storage *smp;
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;
145 int min_set :1;
146 int max_set :1;
147 } 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 FOURNIERa65b3432013-11-28 18:22:00 +0100156 struct pat_time time; /* valid hours and days */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100157 struct eb_root *tree; /* tree storing all values if any */
158 } val; /* direct value */
159 union {
160 void *ptr; /* any data */
161 char *str; /* any string */
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100162 struct my_regex *reg; /* a compiled regex */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100163 } ptr; /* indirect values, allocated */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100164 int len; /* data length when required */
165 int flags; /* expr or pattern flags. */
166 struct sample_storage *smp; /* used to store a pointer to sample value associated
167 with the match. It is used with maps */
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100168 struct pat_ref_elt *ref;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100169};
170
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100171/* This struct is just used for chaining patterns */
172struct pattern_list {
173 struct list list;
174 struct pattern pat;
175};
176
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100177/* Description of a pattern expression.
178 * It contains pointers to the parse and match functions, and a list or tree of
179 * patterns to test against. The structure is organized so that the hot parts
180 * are grouped together in order to optimize caching.
181 */
182struct pattern_expr {
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100183 struct list list; /* Used for chaining pattern_expr in pat_ref. */
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
188 * "head". Dont write "(struct pattern_expr *)any->pat_head->expr".
189 */
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 */
193};
194
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100195/* This is a list of expression. A struct pattern_expr can be used by
196 * more than one "struct pattern_head". this intermediate struct
197 * permit more than one list.
198 */
199struct pattern_expr_list {
200 struct list list; /* Used for chaining pattern_expr in pattern_head. */
201 int do_free;
202 struct pattern_expr *expr; /* The used expr. */
203};
204
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100205/* This struct contain a list of pattern expr */
206struct pattern_head {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100207 int (*parse)(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIER88886352014-01-15 15:17:23 +0100208 int (*parse_smp)(const char *text, struct sample_storage *smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100209 int (*index)(struct pattern_expr *, struct pattern *, char **);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +0100210 void (*delete)(struct pattern_expr *, struct pat_ref_elt *);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100211 void (*prune)(struct pattern_expr *);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100212 struct pattern *(*match)(struct sample *, struct pattern_expr *, int);
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100213 int expect_type; /* type of the expected sample (SMP_T_*) */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100214
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100215 struct list head; /* This is a list of struct pattern_expr_list. */
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100216};
217
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100218extern char *pat_match_names[PAT_MATCH_NUM];
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100219extern int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100220extern int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +0100221extern void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100222void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *);
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100223extern struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int);
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100224extern int pat_match_types[PAT_MATCH_NUM];
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100226/* This is the root of the list of all pattern_ref avalaibles. */
227extern struct list pattern_reference;
228
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229#endif /* _TYPES_PATTERN_H */