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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
/* A generic nexthop structure
* Copyright (C) 2013 Cumulus Networks, Inc.
*
* This file is part of Quagga.
*
* Quagga is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* Quagga is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "if.h"
#include "log.h"
#include "sockunion.h"
#include "linklist.h"
#include "thread.h"
#include "prefix.h"
#include "nexthop.h"
#include "mpls.h"
#include "jhash.h"
#include "printfrr.h"
#include "vrf.h"
#include "nexthop_group.h"
DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop")
DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label")
static int _nexthop_labels_cmp(const struct nexthop *nh1,
const struct nexthop *nh2)
{
const struct mpls_label_stack *nhl1 = NULL;
const struct mpls_label_stack *nhl2 = NULL;
nhl1 = nh1->nh_label;
nhl2 = nh2->nh_label;
/* No labels is a match */
if (!nhl1 && !nhl2)
return 0;
if (nhl1 && !nhl2)
return 1;
if (nhl2 && !nhl1)
return -1;
if (nhl1->num_labels > nhl2->num_labels)
return 1;
if (nhl1->num_labels < nhl2->num_labels)
return -1;
return memcmp(nhl1->label, nhl2->label, nhl1->num_labels);
}
int nexthop_g_addr_cmp(enum nexthop_types_t type, const union g_addr *addr1,
const union g_addr *addr2)
{
int ret = 0;
switch (type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
ret = IPV4_ADDR_CMP(&addr1->ipv4, &addr2->ipv4);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
ret = IPV6_ADDR_CMP(&addr1->ipv6, &addr2->ipv6);
break;
case NEXTHOP_TYPE_IFINDEX:
case NEXTHOP_TYPE_BLACKHOLE:
/* No addr here */
break;
}
return ret;
}
static int _nexthop_gateway_cmp(const struct nexthop *nh1,
const struct nexthop *nh2)
{
return nexthop_g_addr_cmp(nh1->type, &nh1->gate, &nh2->gate);
}
static int _nexthop_source_cmp(const struct nexthop *nh1,
const struct nexthop *nh2)
{
return nexthop_g_addr_cmp(nh1->type, &nh1->src, &nh2->src);
}
static int _nexthop_cmp_no_labels(const struct nexthop *next1,
const struct nexthop *next2)
{
int ret = 0;
if (next1->vrf_id < next2->vrf_id)
return -1;
if (next1->vrf_id > next2->vrf_id)
return 1;
if (next1->type < next2->type)
return -1;
if (next1->type > next2->type)
return 1;
if (next1->weight < next2->weight)
return -1;
if (next1->weight > next2->weight)
return 1;
switch (next1->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV6:
ret = _nexthop_gateway_cmp(next1, next2);
if (ret != 0)
return ret;
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
case NEXTHOP_TYPE_IPV6_IFINDEX:
ret = _nexthop_gateway_cmp(next1, next2);
if (ret != 0)
return ret;
/* Intentional Fall-Through */
case NEXTHOP_TYPE_IFINDEX:
if (next1->ifindex < next2->ifindex)
return -1;
if (next1->ifindex > next2->ifindex)
return 1;
break;
case NEXTHOP_TYPE_BLACKHOLE:
if (next1->bh_type < next2->bh_type)
return -1;
if (next1->bh_type > next2->bh_type)
return 1;
break;
}
ret = _nexthop_source_cmp(next1, next2);
return ret;
}
int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
{
int ret = 0;
ret = _nexthop_cmp_no_labels(next1, next2);
if (ret != 0)
return ret;
ret = _nexthop_labels_cmp(next1, next2);
return ret;
}
int nexthop_same_firsthop(struct nexthop *next1, struct nexthop *next2)
{
int type1 = NEXTHOP_FIRSTHOPTYPE(next1->type);
int type2 = NEXTHOP_FIRSTHOPTYPE(next2->type);
if (type1 != type2)
return 0;
switch (type1) {
case NEXTHOP_TYPE_IPV4_IFINDEX:
if (!IPV4_ADDR_SAME(&next1->gate.ipv4, &next2->gate.ipv4))
return 0;
if (next1->ifindex != next2->ifindex)
return 0;
break;
case NEXTHOP_TYPE_IFINDEX:
if (next1->ifindex != next2->ifindex)
return 0;
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6))
return 0;
if (next1->ifindex != next2->ifindex)
return 0;
break;
default:
/* do nothing */
break;
}
return 1;
}
/*
* nexthop_type_to_str
*/
const char *nexthop_type_to_str(enum nexthop_types_t nh_type)
{
static const char *const desc[] = {
"none", "Directly connected",
"IPv4 nexthop", "IPv4 nexthop with ifindex",
"IPv6 nexthop", "IPv6 nexthop with ifindex",
"Null0 nexthop",
};
return desc[nh_type];
}
/*
* Check if the labels match for the 2 nexthops specified.
*/
bool nexthop_labels_match(const struct nexthop *nh1, const struct nexthop *nh2)
{
if (_nexthop_labels_cmp(nh1, nh2) != 0)
return false;
return true;
}
struct nexthop *nexthop_new(void)
{
struct nexthop *nh;
nh = XCALLOC(MTYPE_NEXTHOP, sizeof(struct nexthop));
/*
* Default the weight to 1 here for all nexthops.
* The linux kernel does some weird stuff with adding +1 to
* all nexthop weights it gets over netlink.
* To handle this, just default everything to 1 right from
* from the beggining so we don't have to special case
* default weights in the linux netlink code.
*
* 1 should be a valid on all platforms anyway.
*/
nh->weight = 1;
return nh;
}
/* Free nexthop. */
void nexthop_free(struct nexthop *nexthop)
{
nexthop_del_labels(nexthop);
if (nexthop->resolved)
nexthops_free(nexthop->resolved);
XFREE(MTYPE_NEXTHOP, nexthop);
}
/* Frees a list of nexthops */
void nexthops_free(struct nexthop *nexthop)
{
struct nexthop *nh, *next;
for (nh = nexthop; nh; nh = next) {
next = nh->next;
nexthop_free(nh);
}
}
bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2)
{
if (nh1 && !nh2)
return false;
if (!nh1 && nh2)
return false;
if (nh1 == nh2)
return true;
if (nexthop_cmp(nh1, nh2) != 0)
return false;
return true;
}
bool nexthop_same_no_labels(const struct nexthop *nh1,
const struct nexthop *nh2)
{
if (nh1 && !nh2)
return false;
if (!nh1 && nh2)
return false;
if (nh1 == nh2)
return true;
if (_nexthop_cmp_no_labels(nh1, nh2) != 0)
return false;
return true;
}
/*
* Allocate a new nexthop object and initialize it from various args.
*/
struct nexthop *nexthop_from_ifindex(ifindex_t ifindex, vrf_id_t vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IFINDEX;
nexthop->ifindex = ifindex;
nexthop->vrf_id = vrf_id;
return nexthop;
}
struct nexthop *nexthop_from_ipv4(const struct in_addr *ipv4,
const struct in_addr *src,
vrf_id_t vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV4;
nexthop->vrf_id = vrf_id;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
return nexthop;
}
struct nexthop *nexthop_from_ipv4_ifindex(const struct in_addr *ipv4,
const struct in_addr *src,
ifindex_t ifindex, vrf_id_t vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
nexthop->vrf_id = vrf_id;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
nexthop->ifindex = ifindex;
return nexthop;
}
struct nexthop *nexthop_from_ipv6(const struct in6_addr *ipv6,
vrf_id_t vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = vrf_id;
nexthop->type = NEXTHOP_TYPE_IPV6;
nexthop->gate.ipv6 = *ipv6;
return nexthop;
}
struct nexthop *nexthop_from_ipv6_ifindex(const struct in6_addr *ipv6,
ifindex_t ifindex, vrf_id_t vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = vrf_id;
nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
nexthop->gate.ipv6 = *ipv6;
nexthop->ifindex = ifindex;
return nexthop;
}
struct nexthop *nexthop_from_blackhole(enum blackhole_type bh_type)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = VRF_DEFAULT;
nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
nexthop->bh_type = bh_type;
return nexthop;
}
/* Update nexthop with label information. */
void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t type,
uint8_t num_labels, mpls_label_t *label)
{
struct mpls_label_stack *nh_label;
int i;
if (num_labels == 0)
return;
nexthop->nh_label_type = type;
nh_label = XCALLOC(MTYPE_NH_LABEL,
sizeof(struct mpls_label_stack)
+ num_labels * sizeof(mpls_label_t));
nh_label->num_labels = num_labels;
for (i = 0; i < num_labels; i++)
nh_label->label[i] = *(label + i);
nexthop->nh_label = nh_label;
}
/* Free label information of nexthop, if present. */
void nexthop_del_labels(struct nexthop *nexthop)
{
XFREE(MTYPE_NH_LABEL, nexthop->nh_label);
nexthop->nh_label_type = ZEBRA_LSP_NONE;
}
const char *nexthop2str(const struct nexthop *nexthop, char *str, int size)
{
switch (nexthop->type) {
case NEXTHOP_TYPE_IFINDEX:
snprintf(str, size, "if %u", nexthop->ifindex);
break;
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
snprintf(str, size, "%s if %u", inet_ntoa(nexthop->gate.ipv4),
nexthop->ifindex);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
snprintf(str, size, "%s if %u", inet6_ntoa(nexthop->gate.ipv6),
nexthop->ifindex);
break;
case NEXTHOP_TYPE_BLACKHOLE:
snprintf(str, size, "blackhole");
break;
default:
snprintf(str, size, "unknown");
break;
}
return str;
}
/*
* Iteration step for ALL_NEXTHOPS macro:
* This is the tricky part. Check if `nexthop' has
* NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
* at least one nexthop attached to `nexthop->resolved', which will be
* the next one.
*
* If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
* current chain. In case its current chain end is reached, it will move
* upwards in the recursion levels and progress there. Whenever a step
* forward in a chain is done, recursion will be checked again.
* In a nustshell, it's equivalent to a pre-traversal order assuming that
* left branch is 'resolved' and right branch is 'next':
* https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
*/
struct nexthop *nexthop_next(const struct nexthop *nexthop)
{
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
return nexthop->resolved;
if (nexthop->next)
return nexthop->next;
for (struct nexthop *par = nexthop->rparent; par; par = par->rparent)
if (par->next)
return par->next;
return NULL;
}
/* Return the next nexthop in the tree that is resolved and active */
struct nexthop *nexthop_next_active_resolved(const struct nexthop *nexthop)
{
struct nexthop *next = nexthop_next(nexthop);
while (next
&& (CHECK_FLAG(next->flags, NEXTHOP_FLAG_RECURSIVE)
|| !CHECK_FLAG(next->flags, NEXTHOP_FLAG_ACTIVE)))
next = nexthop_next(next);
return next;
}
unsigned int nexthop_level(struct nexthop *nexthop)
{
unsigned int rv = 0;
for (struct nexthop *par = nexthop->rparent; par; par = par->rparent)
rv++;
return rv;
}
/* Only hash word-sized things, let cmp do the rest. */
uint32_t nexthop_hash_quick(const struct nexthop *nexthop)
{
uint32_t key = 0x45afe398;
key = jhash_3words(nexthop->type, nexthop->vrf_id,
nexthop->nh_label_type, key);
if (nexthop->nh_label) {
int labels = nexthop->nh_label->num_labels;
int i = 0;
while (labels >= 3) {
key = jhash_3words(nexthop->nh_label->label[i],
nexthop->nh_label->label[i + 1],
nexthop->nh_label->label[i + 2],
key);
labels -= 3;
i += 3;
}
if (labels >= 2) {
key = jhash_2words(nexthop->nh_label->label[i],
nexthop->nh_label->label[i + 1],
key);
labels -= 2;
i += 2;
}
if (labels >= 1)
key = jhash_1word(nexthop->nh_label->label[i], key);
}
key = jhash_2words(nexthop->ifindex,
CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK),
key);
return key;
}
#define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
/* For a more granular hash */
uint32_t nexthop_hash(const struct nexthop *nexthop)
{
uint32_t gate_src_rmap_raw[GATE_SIZE * 3] = {};
/* Get all the quick stuff */
uint32_t key = nexthop_hash_quick(nexthop);
assert(((sizeof(nexthop->gate) + sizeof(nexthop->src)
+ sizeof(nexthop->rmap_src))
/ 3)
== (GATE_SIZE * sizeof(uint32_t)));
memcpy(gate_src_rmap_raw, &nexthop->gate, GATE_SIZE);
memcpy(gate_src_rmap_raw + GATE_SIZE, &nexthop->src, GATE_SIZE);
memcpy(gate_src_rmap_raw + (2 * GATE_SIZE), &nexthop->rmap_src,
GATE_SIZE);
key = jhash2(gate_src_rmap_raw, (GATE_SIZE * 3), key);
return key;
}
void nexthop_copy_no_recurse(struct nexthop *copy,
const struct nexthop *nexthop,
struct nexthop *rparent)
{
copy->vrf_id = nexthop->vrf_id;
copy->ifindex = nexthop->ifindex;
copy->type = nexthop->type;
copy->flags = nexthop->flags;
copy->weight = nexthop->weight;
memcpy(©->gate, &nexthop->gate, sizeof(nexthop->gate));
memcpy(©->src, &nexthop->src, sizeof(nexthop->src));
memcpy(©->rmap_src, &nexthop->rmap_src, sizeof(nexthop->rmap_src));
copy->rparent = rparent;
if (nexthop->nh_label)
nexthop_add_labels(copy, nexthop->nh_label_type,
nexthop->nh_label->num_labels,
&nexthop->nh_label->label[0]);
}
void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop,
struct nexthop *rparent)
{
nexthop_copy_no_recurse(copy, nexthop, rparent);
/* Bit of a special case here, we need to handle the case
* of a nexthop resolving to agroup. Hence, we need to
* use a nexthop_group API.
*/
if (CHECK_FLAG(copy->flags, NEXTHOP_FLAG_RECURSIVE))
copy_nexthops(©->resolved, nexthop->resolved, copy);
}
struct nexthop *nexthop_dup_no_recurse(const struct nexthop *nexthop,
struct nexthop *rparent)
{
struct nexthop *new = nexthop_new();
nexthop_copy_no_recurse(new, nexthop, rparent);
return new;
}
struct nexthop *nexthop_dup(const struct nexthop *nexthop,
struct nexthop *rparent)
{
struct nexthop *new = nexthop_new();
nexthop_copy(new, nexthop, rparent);
return new;
}
/*
* nexthop printing variants:
* %pNHvv
* via 1.2.3.4
* via 1.2.3.4, eth0
* is directly connected, eth0
* unreachable (blackhole)
* %pNHv
* 1.2.3.4
* 1.2.3.4, via eth0
* directly connected, eth0
* unreachable (blackhole)
* %pNHs
* nexthop2str()
*/
printfrr_ext_autoreg_p("NH", printfrr_nh)
static ssize_t printfrr_nh(char *buf, size_t bsz, const char *fmt,
int prec, const void *ptr)
{
const struct nexthop *nexthop = ptr;
struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 };
bool do_ifi = false;
const char *s, *v_is = "", *v_via = "", *v_viaif = "via ";
ssize_t ret = 3;
switch (fmt[2]) {
case 'v':
if (fmt[3] == 'v') {
v_is = "is ";
v_via = "via ";
v_viaif = "";
ret++;
}
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
bprintfrr(&fb, "%s%pI4", v_via, &nexthop->gate.ipv4);
do_ifi = true;
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
bprintfrr(&fb, "%s%pI6", v_via, &nexthop->gate.ipv6);
do_ifi = true;
break;
case NEXTHOP_TYPE_IFINDEX:
bprintfrr(&fb, "%sdirectly connected, %s", v_is,
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
switch (nexthop->bh_type) {
case BLACKHOLE_REJECT:
s = " (ICMP unreachable)";
break;
case BLACKHOLE_ADMINPROHIB:
s = " (ICMP admin-prohibited)";
break;
case BLACKHOLE_NULL:
s = " (blackhole)";
break;
default:
s = "";
break;
}
bprintfrr(&fb, "unreachable%s", s);
break;
default:
break;
}
if (do_ifi && nexthop->ifindex)
bprintfrr(&fb, ", %s%s", v_viaif, ifindex2ifname(
nexthop->ifindex,
nexthop->vrf_id));
*fb.pos = '\0';
return ret;
case 's':
nexthop2str(nexthop, buf, bsz);
return 3;
}
return 0;
}
|