1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/*
* This file is part of RTRlib.
*
* This file is subject to the terms and conditions of the MIT license.
* See the file LICENSE in the top level directory for more details.
*
* Website: http://rtrlib.realmv6.org/
*/
#include "rtrlib/lib/ip_private.h"
#include "rtrlib/lib/utils_private.h"
#include "rtrlib/pfx/pfx.h"
#include "rtrlib/pfx/pfx_private.h"
#include "rtrlib/pfx/trie/trie_private.h"
#include "rtrlib/rtr/rtr.h"
#include <arpa/inet.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
static void validate(struct pfx_table *pfxt, uint32_t asn, const char *prefix, uint8_t prefix_len,
enum pfxv_state expected_result)
{
struct lrtr_ip_addr ip;
enum pfxv_state val_res;
assert(!lrtr_ip_str_to_addr(prefix, &ip));
assert(pfx_table_validate(pfxt, asn, &ip, prefix_len, &val_res) == PFX_SUCCESS);
assert(val_res == expected_result);
}
/**
* @brief remove_src_test
* This test verifies pfx_table_src_remove function. It first adds certain
* records with different sockets into a pfx_table. Afterwards entries with
* socket tr1 are removed, and the remaining records are verified.
*/
static void remove_src_test(void)
{
struct pfx_table pfxt;
struct rtr_socket tr1;
struct pfx_record pfx;
/* TEST1: init prefix table ----------------------------------------- */
pfx_table_init(&pfxt, NULL);
pfx.min_len = 32;
pfx.max_len = 32;
/* TEST2: add and verify different prefixes -------------------------- */
pfx.asn = 80;
pfx.socket = &tr1;
lrtr_ip_str_to_addr("10.11.10.0", &pfx.prefix);
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
pfx.asn = 90;
pfx.socket = NULL;
lrtr_ip_str_to_addr("10.11.10.0", &pfx.prefix);
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
pfx.socket = NULL;
pfx.min_len = 24;
lrtr_ip_str_to_addr("192.168.0.0", &pfx.prefix);
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
pfx.socket = &tr1;
pfx.min_len = 8;
lrtr_ip_str_to_addr("10.0.0.0", &pfx.prefix);
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
unsigned int len = 0;
struct trie_node **array = NULL;
/* verify that table has 3 distinct prefix entries */
assert(trie_get_children(pfxt.ipv4, &array, &len) != -1);
free(array);
array = NULL;
assert((len + 1) == 3);
/* remove entries with socket tr1, verify remaining 2 records */
pfx_table_src_remove(&pfxt, &tr1);
len = 0;
assert(trie_get_children(pfxt.ipv4, &array, &len) != -1);
free(array);
assert((len + 1) == 2);
/* verify validation of prefixes */
validate(&pfxt, 90, "10.0.0.0", 8, BGP_PFXV_STATE_NOT_FOUND);
validate(&pfxt, 90, "10.11.10.0", 32, BGP_PFXV_STATE_VALID);
validate(&pfxt, 80, "10.11.10.0", 32, BGP_PFXV_STATE_INVALID);
/* cleanup: free table */
pfx_table_free(&pfxt);
printf("%s() successful\n", __func__);
}
/**
* @brief Test pfx_table operations with many records
* This test adds, validates, and removes a huge number of records in/to/from
* a pfx_table.
*/
static void mass_test(void)
{
struct pfx_table pfxt;
struct pfx_record pfx;
enum pfxv_state res;
const uint32_t min_i = 0xFFFF0000;
const uint32_t max_i = 0xFFFFFFF0;
/* init table with huge number of records */
pfx_table_init(&pfxt, NULL);
printf("Inserting %u records\n", (max_i - min_i) * 3);
for (uint32_t i = max_i; i >= min_i; i--) {
pfx.min_len = 32;
pfx.max_len = 32;
pfx.socket = NULL;
pfx.asn = i;
pfx.prefix.u.addr4.addr = htonl(i);
pfx.prefix.ver = LRTR_IPV4;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
/* add same prefix, with diff asn */
pfx.asn = i + 1;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
/* add same prefix, with diff len */
pfx.min_len = 128;
pfx.max_len = 128;
pfx.prefix.ver = LRTR_IPV6;
((uint64_t *)pfx.prefix.u.addr6.addr)[1] = max_i;
((uint64_t *)pfx.prefix.u.addr6.addr)[0] = min_i + i;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
}
/* verify validation of huge number of records */
printf("validating..\n");
for (uint32_t i = max_i; i >= min_i; i--) {
pfx.min_len = 32;
pfx.max_len = 32;
pfx.prefix.ver = LRTR_IPV4;
pfx.prefix.u.addr4.addr = htonl(i);
assert(pfx_table_validate(&pfxt, i, &pfx.prefix, pfx.min_len, &res) == PFX_SUCCESS);
assert(res == BGP_PFXV_STATE_VALID);
assert(pfx_table_validate(&pfxt, i + 1, &pfx.prefix, pfx.min_len, &res) == PFX_SUCCESS);
assert(res == BGP_PFXV_STATE_VALID);
pfx.min_len = 128;
pfx.max_len = 128;
pfx.prefix.ver = LRTR_IPV6;
((uint64_t *)pfx.prefix.u.addr6.addr)[1] = max_i;
((uint64_t *)pfx.prefix.u.addr6.addr)[0] = min_i + i;
assert(pfx_table_validate(&pfxt, i + 1, &pfx.prefix, pfx.min_len, &res) == PFX_SUCCESS);
assert(res == BGP_PFXV_STATE_VALID);
}
/* verify removal of huge number of records */
printf("removing records\n");
for (uint32_t i = max_i; i >= min_i; i--) {
pfx.socket = NULL;
pfx.min_len = 32;
pfx.max_len = 32;
pfx.asn = i;
pfx.prefix.ver = LRTR_IPV4;
pfx.prefix.u.addr4.addr = htonl(i);
assert(pfx_table_remove(&pfxt, &pfx) == PFX_SUCCESS);
pfx.asn = i + 1;
assert(pfx_table_remove(&pfxt, &pfx) == PFX_SUCCESS);
pfx.prefix.ver = LRTR_IPV6;
pfx.min_len = 128;
pfx.max_len = 128;
((uint64_t *)pfx.prefix.u.addr6.addr)[1] = max_i;
((uint64_t *)pfx.prefix.u.addr6.addr)[0] = min_i + i;
assert(pfx_table_remove(&pfxt, &pfx) == PFX_SUCCESS);
}
/* cleanup: free table */
pfx_table_free(&pfxt);
printf("%s() successful\n", __func__);
}
/**
* @brief Test of pfx_table functions
* This test verifies pfx_table and its core functions, namely
* pfx_table_add and pfx_table_validate.
*/
static void pfx_table_test(void)
{
struct pfx_table pfxt;
struct pfx_record pfx;
enum pfxv_state res;
pfx_table_init(&pfxt, NULL);
pfx.asn = 123;
pfx.prefix.ver = LRTR_IPV4;
lrtr_ip_str_to_addr("10.10.0.0", &pfx.prefix);
pfx.min_len = 16;
pfx.max_len = 24;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 123, "10.10.0.0", 16, BGP_PFXV_STATE_VALID);
validate(&pfxt, 124, "10.10.0.0", 16, BGP_PFXV_STATE_INVALID);
validate(&pfxt, 123, "10.10.0.0", 24, BGP_PFXV_STATE_VALID);
validate(&pfxt, 123, "10.10.10.0", 20, BGP_PFXV_STATE_VALID);
validate(&pfxt, 123, "10.10.10.0", 25, BGP_PFXV_STATE_INVALID);
validate(&pfxt, 123, "10.11.10.0", 16, BGP_PFXV_STATE_NOT_FOUND);
lrtr_ip_str_to_addr("10.10.0.0", &pfx.prefix);
pfx.asn = 122;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 122, "10.10.0.0", 18, BGP_PFXV_STATE_VALID);
lrtr_ip_str_to_addr("11.10.0.0", &pfx.prefix);
pfx.asn = 22;
pfx.min_len = 17;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 22, "11.10.0.0", 17, BGP_PFXV_STATE_VALID);
lrtr_ip_str_to_addr("2a01:4f8:131::", &pfx.prefix);
pfx.prefix.ver = LRTR_IPV6;
pfx.min_len = 48;
pfx.max_len = 48;
pfx.asn = 124;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
assert(pfx_table_validate(&pfxt, 124, &pfx.prefix, 48, &res) == PFX_SUCCESS);
validate(&pfxt, 124, "2a01:4f8:131::", 48, BGP_PFXV_STATE_VALID);
validate(&pfxt, 124, "2a01:4f8:131:15::", 56, BGP_PFXV_STATE_INVALID);
assert(lrtr_ip_str_to_addr("1.0.4.0", &pfx.prefix) == 0);
pfx.min_len = 22;
pfx.max_len = 22;
pfx.asn = 56203;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, pfx.asn, "1.0.4.0", pfx.min_len, BGP_PFXV_STATE_VALID);
assert(lrtr_ip_str_to_addr("1.8.1.0", &pfx.prefix) == 0);
pfx.min_len = 24;
pfx.max_len = 24;
pfx.asn = 38345;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, pfx.asn, "1.8.1.0", pfx.min_len, BGP_PFXV_STATE_VALID);
assert(lrtr_ip_str_to_addr("1.8.8.0", &pfx.prefix) == 0);
pfx.min_len = 24;
pfx.max_len = 24;
pfx.asn = 38345;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, pfx.asn, "1.8.8.0", pfx.min_len, BGP_PFXV_STATE_VALID);
pfx_table_free(&pfxt);
assert(lrtr_ip_str_to_addr("1.0.65.0", &pfx.prefix) == 0);
pfx.min_len = 18;
pfx.max_len = 18;
pfx.asn = 18144;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, pfx.asn, "1.0.65.0", pfx.min_len, BGP_PFXV_STATE_VALID);
pfx_table_free(&pfxt);
assert(lrtr_ip_str_to_addr("10.0.0.0", &pfx.prefix) == 0);
pfx.min_len = 16;
pfx.max_len = 16;
pfx.asn = 123;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, pfx.asn, "10.0.0.0", pfx.min_len, BGP_PFXV_STATE_VALID);
validate(&pfxt, 124, "10.0.5.0", 24, BGP_PFXV_STATE_INVALID);
pfx_table_free(&pfxt);
assert(lrtr_ip_str_to_addr("109.105.96.0", &pfx.prefix) == 0);
pfx.min_len = 19;
pfx.max_len = 19;
pfx.asn = 123;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 456, "109.105.128.0", 20, BGP_PFXV_STATE_NOT_FOUND);
pfx_table_free(&pfxt);
assert(lrtr_ip_str_to_addr("190.57.224.0", &pfx.prefix) == 0);
pfx.min_len = 19;
pfx.max_len = 24;
pfx.asn = 123;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 123, "190.57.72.0", 21, BGP_PFXV_STATE_NOT_FOUND);
pfx_table_free(&pfxt);
assert(lrtr_ip_str_to_addr("80.253.128.0", &pfx.prefix) == 0);
pfx.min_len = 19;
pfx.max_len = 19;
pfx.asn = 123;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 123, "80.253.144.0", 20, BGP_PFXV_STATE_INVALID);
assert(lrtr_ip_str_to_addr("10.10.0.0", &pfx.prefix) == 0);
pfx.min_len = 16;
pfx.max_len = 16;
pfx.asn = 0;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 123, "10.10.0.0", 16, BGP_PFXV_STATE_INVALID);
assert(lrtr_ip_str_to_addr("10.0.0.0", &pfx.prefix) == 0);
pfx.min_len = 8;
pfx.max_len = 15;
pfx.asn = 6;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
assert(lrtr_ip_str_to_addr("10.0.0.0", &pfx.prefix) == 0);
pfx.min_len = 8;
pfx.max_len = 15;
pfx.asn = 5;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
assert(lrtr_ip_str_to_addr("10.1.0.0", &pfx.prefix) == 0);
pfx.min_len = 16;
pfx.max_len = 16;
pfx.asn = 5;
assert(pfx_table_add(&pfxt, &pfx) == PFX_SUCCESS);
validate(&pfxt, 123, "10.1.0.0", 16, BGP_PFXV_STATE_INVALID);
/* cleanup: free record and table */
pfx_table_free(&pfxt);
printf("%s() successful\n", __func__);
}
static void create_ip4_pfx_record(struct pfx_record *pfx, uint32_t asn, const char *ip, uint8_t min_mask_len,
uint8_t max_mask_len)
{
pfx->asn = asn;
pfx->min_len = min_mask_len;
pfx->max_len = max_mask_len;
pfx->socket = (struct rtr_socket *)1;
assert(!lrtr_ip_str_to_addr(ip, &pfx->prefix));
}
static void add_ip4_pfx_record(struct pfx_table *pfxt, uint32_t asn, const char *ip, uint8_t min_mask_len,
uint8_t max_mask_len)
{
struct pfx_record pfx;
enum pfxv_state val_res;
create_ip4_pfx_record(&pfx, asn, ip, min_mask_len, max_mask_len);
assert(pfx_table_add(pfxt, &pfx) == PFX_SUCCESS);
assert(pfx_table_validate(pfxt, pfx.asn, &pfx.prefix, pfx.min_len, &val_res) == PFX_SUCCESS);
assert(val_res == BGP_PFXV_STATE_VALID);
}
static void test_issue99(void)
{
struct pfx_table pfxt;
pfx_table_init(&pfxt, NULL);
add_ip4_pfx_record(&pfxt, 200, "10.100.255.0", 24, 24);
add_ip4_pfx_record(&pfxt, 300, "255.0.0.0", 24, 24);
add_ip4_pfx_record(&pfxt, 400, "128.0.0.0", 1, 24);
validate(&pfxt, 400, "255.0.0.0", 24, BGP_PFXV_STATE_VALID);
pfx_table_free(&pfxt);
}
static void test_issue152(void)
{
struct pfx_table pfxt;
struct pfx_record *records = calloc(6, sizeof(struct pfx_record));
pfx_table_init(&pfxt, NULL);
create_ip4_pfx_record(&records[0], 1, "89.18.183.0", 24, 24);
create_ip4_pfx_record(&records[1], 2, "109.164.0.0", 17, 25);
create_ip4_pfx_record(&records[2], 3, "185.131.60.0", 22, 24);
create_ip4_pfx_record(&records[3], 4, "185.146.28.0", 22, 22);
create_ip4_pfx_record(&records[4], 5, "212.5.51.0", 24, 24);
create_ip4_pfx_record(&records[5], 6, "213.175.86.0", 24, 24);
for (size_t i = 0; i < 6; i++)
assert(pfx_table_add(&pfxt, &records[i]) == PFX_SUCCESS);
for (size_t i = 0; i < 6; i++)
assert(pfx_table_remove(&pfxt, &records[i]) == PFX_SUCCESS);
free(records);
}
static void update_cb1(struct pfx_table *p __attribute__((unused)), const struct pfx_record rec, const bool added)
{
char ip[INET6_ADDRSTRLEN];
if (added)
printf("+ ");
else
printf("- ");
lrtr_ip_addr_to_str(&rec.prefix, ip, sizeof(ip));
printf("%-40s %3u - %3u %10u\n", ip, rec.min_len, rec.max_len, rec.asn);
if (rec.asn == 1)
assert(!added);
if (rec.asn == 2)
assert(false);
if (rec.asn == 3)
assert(added);
}
static void test_pfx_merge(void)
{
struct pfx_table pfxt1;
struct pfx_table pfxt2;
struct pfx_record records[3];
pfx_table_init(&pfxt1, NULL);
pfx_table_init(&pfxt2, NULL);
create_ip4_pfx_record(&records[0], 1, "1.1.1.1", 24, 24);
create_ip4_pfx_record(&records[1], 2, "2.2.2.2", 24, 24);
create_ip4_pfx_record(&records[2], 3, "3.3.3.3", 24, 24);
printf("Adding to table one\n");
assert(pfx_table_add(&pfxt1, &records[0]) == PFX_SUCCESS);
assert(pfx_table_add(&pfxt1, &records[1]) == PFX_SUCCESS);
printf("Adding to table two\n");
assert(pfx_table_add(&pfxt2, &records[1]) == PFX_SUCCESS);
assert(pfx_table_add(&pfxt2, &records[2]) == PFX_SUCCESS);
printf("Computing diff\n");
pfxt1.update_fp = update_cb1;
pfxt2.update_fp = update_cb1;
pfx_table_notify_diff(&pfxt2, &pfxt1, (struct rtr_socket *)1);
pfxt1.update_fp = NULL;
pfxt2.update_fp = NULL;
printf("Freeing table one\n");
pfx_table_free(&pfxt1);
printf("Freeing table two\n");
pfx_table_free(&pfxt2);
}
int main(void)
{
pfx_table_test();
remove_src_test();
mass_test();
test_issue99();
test_issue152();
test_pfx_merge();
return EXIT_SUCCESS;
}
|