1 /*
2     pmacct (Promiscuous mode IP Accounting package)
3     pmacct is Copyright (C) 2003-2020 by Paolo Lucente
4 */
5 
6 /*
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program 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
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 
22 #ifndef PRETAG_H
23 #define PRETAG_H
24 
25 /* Pre-Tag map stuff */
26 #define N_MAP_HANDLERS N_PRIMITIVES
27 #define MAX_LABEL_LEN 32
28 #define MAX_BITMAP_ENTRIES 64 /* pt_bitmap_t -> u_int64_t */
29 #define MAX_PRETAG_MAP_ENTRIES 384
30 
31 #define MAX_ID_TABLE_INDEXES 8
32 #define ID_TABLE_INDEX_DEPTH 8
33 #define ID_TABLE_INDEX_RESULTS (MAX_ID_TABLE_INDEXES * 8)
34 
35 #define PRETAG_IN_IFACE			0x0000000000000001ULL
36 #define PRETAG_OUT_IFACE		0x0000000000000002ULL
37 #define PRETAG_NEXTHOP			0x0000000000000004ULL
38 #define PRETAG_BGP_NEXTHOP		0x0000000000000008ULL
39 #define PRETAG_ENGINE_TYPE		0x0000000000000010ULL
40 #define PRETAG_ENGINE_ID		0x0000000000000020ULL
41 #define PRETAG_FILTER			0x0000000000000040ULL
42 #define PRETAG_NAT_EVENT		0x0000000000000080ULL
43 #define PRETAG_SF_AGENTID		0x0000000000000100ULL
44 #define PRETAG_SAMPLING_RATE		0x0000000000000200ULL
45 #define PRETAG_DIRECTION		0x0000000000000400ULL
46 #define PRETAG_SRC_AS			0x0000000000000800ULL
47 #define PRETAG_DST_AS			0x0000000000001000ULL
48 #define PRETAG_PEER_SRC_AS		0x0000000000002000ULL
49 #define PRETAG_PEER_DST_AS		0x0000000000004000ULL
50 #define PRETAG_SRC_LOCAL_PREF		0x0000000000008000ULL
51 #define PRETAG_LOCAL_PREF		0x0000000000010000ULL
52 #define PRETAG_SRC_STD_COMM		0x0000000000020000ULL
53 #define PRETAG_STD_COMM			0x0000000000040000ULL
54 #define PRETAG_MPLS_VPN_RD		0x0000000000080000ULL
55 #define PRETAG_SAMPLE_TYPE      	0x0000000000100000ULL
56 #define PRETAG_SET_TOS			0x0000000000200000ULL
57 #define PRETAG_LOOKUP_BGP_PORT		0x0000000000400000ULL
58 #define PRETAG_SET_TAG			0x0000000000800000ULL
59 #define PRETAG_SET_TAG2			0x0000000001000000ULL
60 #define PRETAG_MPLS_LABEL_BOTTOM	0x0000000002000000ULL
61 #define PRETAG_FLOWSET_ID		0x0000000004000000ULL
62 #define PRETAG_SRC_MAC			0x0000000008000000ULL
63 #define PRETAG_DST_MAC			0x0000000010000000ULL
64 #define PRETAG_VLAN_ID			0x0000000020000000ULL
65 #define PRETAG_IP			0x0000000040000000ULL
66 #define PRETAG_SET_LABEL		0x0000000080000000ULL
67 #define PRETAG_CVLAN_ID			0x0000000100000000ULL
68 #define PRETAG_MPLS_VPN_ID		0x0000000200000000ULL
69 #define PRETAG_FWDSTATUS_ID		0x0000000400000000ULL
70 #define PRETAG_SRC_NET			0x0000000800000000ULL
71 #define PRETAG_DST_NET			0x0000001000000000ULL
72 #define PRETAG_MPLS_PW_ID		0x0000002000000000ULL
73 #define PRETAG_SRC_ROA			0x0000004000000000ULL
74 #define PRETAG_DST_ROA			0x0000008000000000ULL
75 
76 #define PRETAG_MAP_RCODE_ID		0x00000100
77 #define PRETAG_MAP_RCODE_ID2		0x00000200
78 #define PRETAG_MAP_RCODE_SET_TOS	0x00000400
79 #define PRETAG_MAP_RCODE_JEQ		0x00000800
80 #define BTA_MAP_RCODE_ID_ID2		0x00001000
81 #define BTA_MAP_RCODE_LOOKUP_BGP_PORT	0x00002000
82 #define BPAS_MAP_RCODE_BGP		0x00004000
83 #define PRETAG_MAP_RCODE_LABEL		0x00008000
84 
85 #define PRETAG_FLAG_NEG			0x00000001
86 
87 typedef int (*pretag_handler) (struct packet_ptrs *, void *, void *);
88 typedef pm_id_t (*pretag_stack_handler) (pm_id_t, pm_id_t);
89 
90 typedef u_int64_t pt_bitmap_t;
91 
92 typedef struct {
93   u_int8_t neg;
94   u_int8_t n;
95 } pt_uint8_t;
96 
97 typedef struct {
98   u_int8_t neg;
99   u_int16_t n;
100 } pt_uint16_t;
101 
102 typedef struct {
103   u_int8_t neg;
104   u_int32_t n;
105 } pt_uint32_t;
106 
107 typedef struct {
108   u_int8_t neg;
109   struct host_addr a;
110 } pt_hostaddr_t;
111 
112 typedef struct host_mask pt_hostmask_t;
113 
114 typedef struct {
115   u_int8_t neg;
116   struct host_addr a;
117   struct host_mask m;
118 } pt_netaddr_t;
119 
120 typedef struct {
121   u_int8_t neg;
122   u_char a[ETH_ADDR_LEN];
123 } pt_etheraddr_t;
124 
125 typedef struct {
126   u_int8_t neg;
127   rd_t rd;
128 } pt_rd_t;
129 
130 typedef struct {
131   char *label;
132   struct id_entry *ptr;
133 } pt_jeq_t;
134 
135 typedef struct {
136   pretag_stack_handler func;
137 } pt_stack_t;
138 
139 /* Pre-Tag table (ptt) element definition */
140 typedef struct {
141   u_int8_t neg;
142   pm_id_t n;
143   pm_id_t r;
144 } ptt_t;
145 
146 /* Pre-Tag label table (ptlt) element definition */
147 typedef struct {
148   u_int8_t neg;
149   u_int32_t len;
150   char *v;
151 } ptlt_t;
152 
153 struct id_entry_key {
154   pt_hostaddr_t agent_ip;
155   pt_hostmask_t agent_mask;
156   pt_hostaddr_t nexthop;
157   pt_hostaddr_t bgp_nexthop;
158   pt_uint32_t input; /* input interface index */
159   pt_uint32_t output; /* output interface index */
160   pt_uint8_t engine_type;
161   pt_uint32_t engine_id;
162   pt_uint16_t flowset_id; /* applies to NetFlow v9/IPFIX flowset ID */
163   pt_uint32_t agent_id; /* applies to sFlow agentSubId */
164   pt_uint32_t sampling_rate; /* applies to sFlow sampling rate */
165   pt_uint32_t sample_type; /* applies to sFlow sample type */
166   pt_uint8_t direction;
167   pt_uint8_t nat_event;
168   pt_uint32_t src_as;
169   pt_uint32_t dst_as;
170   pt_uint32_t peer_src_as;
171   pt_uint32_t peer_dst_as;
172   pt_uint32_t src_local_pref;
173   pt_uint32_t local_pref;
174   pt_uint8_t src_roa;
175   pt_uint8_t dst_roa;
176   pt_uint32_t mpls_label_bottom;
177   pt_etheraddr_t src_mac;
178   pt_etheraddr_t dst_mac;
179   pt_uint16_t vlan_id;
180   pt_uint16_t cvlan_id;
181   pt_netaddr_t src_net;
182   pt_netaddr_t dst_net;
183   s_uint16_t lookup_bgp_port;
184   char *src_comms[16]; /* XXX: MAX_BGP_COMM_PATTERNS = 16 */
185   char *comms[16]; /* XXX: MAX_BGP_COMM_PATTERNS = 16 */
186   pt_uint32_t mpls_vpn_id;
187   pt_rd_t mpls_vpn_rd;
188   pt_uint32_t mpls_pw_id;
189   pt_uint32_t fwdstatus;
190   struct bpf_program filter;
191 };
192 
193 struct id_entry {
194   pm_id_t id;
195   pm_id_t id2;
196   pt_label_t label;
197   pm_id_t flags;
198   pm_id_t pos;
199   s_uint8_t set_tos;
200   struct id_entry_key key;
201   pretag_handler func[N_MAP_HANDLERS];
202   pt_bitmap_t func_type[N_MAP_HANDLERS];
203   pretag_handler set_func[N_MAP_HANDLERS];
204   pt_bitmap_t set_func_type[N_MAP_HANDLERS];
205   char entry_label[MAX_LABEL_LEN];
206   pt_jeq_t jeq;
207   u_int8_t ret;
208   pt_stack_t stack;
209   pt_bitmap_t last_matched;
210   u_int8_t id_inc;
211   u_int8_t id2_inc;
212 };
213 
214 typedef int (*pretag_copier)(struct id_entry *, pm_hash_serial_t *, void *);
215 
216 struct id_index_entry {
217   u_int16_t depth;
218   pm_hash_key_t hash_key[ID_TABLE_INDEX_DEPTH];
219   struct id_entry_key key[ID_TABLE_INDEX_DEPTH]; /* XXX: to be removed */
220   struct id_entry *result[ID_TABLE_INDEX_DEPTH];
221 };
222 
223 struct id_table_index {
224   pt_bitmap_t bitmap;
225   u_int32_t entries;
226   u_int32_t modulo;
227   pretag_copier idt_handler[MAX_BITMAP_ENTRIES];
228   pretag_copier fdata_handler[MAX_BITMAP_ENTRIES];
229   pm_hash_serial_t hash_serializer;
230   struct id_index_entry *idx_t;
231 };
232 
233 struct id_table {
234   char *filename;
235   int type;
236   unsigned int num;
237   struct id_entry *ipv4_base;
238   unsigned int ipv4_num;
239   struct id_entry *ipv6_base;
240   unsigned int ipv6_num;
241   struct id_entry *e;
242   struct id_table_index index[MAX_ID_TABLE_INDEXES];
243   unsigned int index_num;
244   time_t timestamp;
245   u_int32_t flags;
246 };
247 
248 struct _map_dictionary_line {
249   char key[SRVBUFLEN];
250   int (*func)(char *, struct id_entry *, char *, struct plugin_requests *, int);
251 };
252 
253 struct _map_index_dictionary_line {
254   pt_bitmap_t key;
255   pretag_copier func;
256 };
257 
258 struct pretag_filter {
259   u_int16_t num;
260   ptt_t table[MAX_PRETAG_MAP_ENTRIES/4];
261 };
262 
263 struct pretag_label_filter {
264   u_int16_t num;
265   ptlt_t table[MAX_PRETAG_MAP_ENTRIES/4];
266 };
267 
268 /* prototypes */
269 extern void load_id_file(int, char *, struct id_table *, struct plugin_requests *, int *);
270 extern void load_pre_tag_map(int, char *, struct id_table *, struct plugin_requests *, int *, int, int);
271 extern u_int8_t pt_check_neg(char **, u_int32_t *);
272 extern char * pt_check_range(char *);
273 extern void pretag_init_vars(struct packet_ptrs *, struct id_table *);
274 extern void pretag_init_label(pt_label_t *);
275 extern int pretag_malloc_label(pt_label_t *, int);
276 extern int pretag_realloc_label(pt_label_t *, int);
277 extern int pretag_copy_label(pt_label_t *, pt_label_t *);
278 extern int pretag_move_label(pt_label_t *, pt_label_t *);
279 extern int pretag_append_label(pt_label_t *, pt_label_t *);
280 extern void pretag_free_label(pt_label_t *);
281 extern int pretag_entry_process(struct id_entry *, struct packet_ptrs *, pm_id_t *, pm_id_t *);
282 extern pt_bitmap_t pretag_index_build_bitmap(struct id_entry *, int);
283 extern int pretag_index_insert_bitmap(struct id_table *, pt_bitmap_t);
284 extern int pretag_index_set_handlers(struct id_table *);
285 extern int pretag_index_allocate(struct id_table *);
286 extern int pretag_index_fill(struct id_table *, pt_bitmap_t, struct id_entry *);
287 extern void pretag_index_report(struct id_table *);
288 extern void pretag_index_destroy(struct id_table *);
289 extern u_int32_t pretag_index_lookup(struct id_table *, struct packet_ptrs *, struct id_entry **, int);
290 extern void pretag_index_results_sort(struct id_entry **, int);
291 extern void pretag_index_results_compress(struct id_entry **, int);
292 extern void pretag_index_results_compress_jeqs(struct id_entry **, int);
293 extern int pretag_index_have_one(struct id_table *);
294 
295 extern int bpas_map_allocated;
296 extern int blp_map_allocated;
297 extern int bmed_map_allocated;
298 extern int biss_map_allocated;
299 extern int bta_map_allocated;
300 extern int bitr_map_allocated;
301 extern int sampling_map_allocated;
302 extern int custom_primitives_allocated;
303 
304 extern int bta_map_caching;
305 extern int sampling_map_caching;
306 
307 extern int (*find_id_func)(struct id_table *, struct packet_ptrs *, pm_id_t *, pm_id_t *);
308 
309 #endif //PRETAG_H
310