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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
|
/* Zebra next hop tracking code
* Copyright (C) 2013 Cumulus Networks, Inc.
*
* This file is part of GNU Zebra.
*
* GNU Zebra 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.
*
* GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "str.h"
#include "command.h"
#include "if.h"
#include "log.h"
#include "sockunion.h"
#include "linklist.h"
#include "thread.h"
#include "workqueue.h"
#include "prefix.h"
#include "routemap.h"
#include "stream.h"
#include "nexthop.h"
#include "vrf.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zserv.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/zebra_rnh.h"
/* Default rtm_table for all clients */
extern struct zebra_t zebrad;
static void free_state(struct rib *rib, struct route_node *rn);
static void copy_state(struct rnh *rnh, struct rib *rib,
struct route_node *rn);
#define lookup_rnh_table(v, f) \
({ \
struct zebra_vrf *zvrf; \
struct route_table *t = NULL; \
zvrf = zebra_vrf_lookup(v); \
if (zvrf) \
t = zvrf->rnh_table[family2afi(f)]; \
t; \
})
static int compare_state(struct rib *r1, struct rib *r2);
static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
vrf_id_t vrf_id);
static void print_rnh(struct route_node *rn, struct vty *vty);
int zebra_rnh_ip_default_route = 0;
int zebra_rnh_ipv6_default_route = 0;
static inline struct route_table *get_rnh_table(vrf_id_t vrfid, int family,
rnh_type_t type)
{
struct zebra_vrf *zvrf;
struct route_table *t = NULL;
zvrf = zebra_vrf_lookup(vrfid);
if (zvrf)
switch (type)
{
case RNH_NEXTHOP_TYPE:
t = zvrf->rnh_table[family2afi(family)];
break;
case RNH_IMPORT_CHECK_TYPE:
t = zvrf->import_check_table[family2afi(family)];
break;
}
return t;
}
char *rnh_str (struct rnh *rnh, char *buf, int size)
{
prefix2str(&(rnh->node->p), buf, size);
return buf;
}
struct rnh *
zebra_add_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
{
struct route_table *table;
struct route_node *rn;
struct rnh *rnh = NULL;
if (IS_ZEBRA_DEBUG_NHT)
{
char buf[PREFIX2STR_BUFFER];
prefix2str(p, buf, sizeof (buf));
zlog_debug("add rnh %s in vrf %d", buf, vrfid);
}
table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
if (!table)
{
zlog_debug("add_rnh: rnh table not found\n");
return NULL;
}
/* Make it sure prefixlen is applied to the prefix. */
apply_mask (p);
/* Lookup (or add) route node.*/
rn = route_node_get (table, p);
if (!rn->info)
{
rnh = XCALLOC(MTYPE_RNH, sizeof(struct rnh));
rnh->client_list = list_new();
rnh->zebra_static_route_list = list_new();
route_lock_node (rn);
rn->info = rnh;
rnh->node = rn;
}
route_unlock_node (rn);
return (rn->info);
}
struct rnh *
zebra_lookup_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
{
struct route_table *table;
struct route_node *rn;
table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
if (!table)
return NULL;
/* Make it sure prefixlen is applied to the prefix. */
apply_mask (p);
/* Lookup route node.*/
rn = route_node_lookup (table, p);
if (!rn)
return NULL;
route_unlock_node (rn);
return (rn->info);
}
void
zebra_delete_rnh (struct rnh *rnh, rnh_type_t type)
{
struct route_node *rn;
if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node))
return;
if (IS_ZEBRA_DEBUG_NHT)
{
char buf[PREFIX2STR_BUFFER];
zlog_debug("delete rnh %s", rnh_str(rnh, buf, sizeof (buf)));
}
rnh->flags |= ZEBRA_NHT_DELETED;
list_free(rnh->client_list);
list_free(rnh->zebra_static_route_list);
free_state(rnh->state, rn);
XFREE(MTYPE_RNH, rn->info);
rn->info = NULL;
route_unlock_node (rn);
return;
}
void
zebra_add_rnh_client (struct rnh *rnh, struct zserv *client, rnh_type_t type,
vrf_id_t vrf_id)
{
if (IS_ZEBRA_DEBUG_NHT)
{
char buf[PREFIX2STR_BUFFER];
zlog_debug("client %s registers rnh %s",
zebra_route_string(client->proto),
rnh_str(rnh, buf, sizeof (buf)));
}
if (!listnode_lookup(rnh->client_list, client))
{
listnode_add(rnh->client_list, client);
send_client(rnh, client, type, vrf_id); // Pending: check if its needed
}
}
void
zebra_remove_rnh_client (struct rnh *rnh, struct zserv *client, rnh_type_t type)
{
if (IS_ZEBRA_DEBUG_NHT)
{
char buf[PREFIX2STR_BUFFER];
zlog_debug("client %s unregisters rnh %s",
zebra_route_string(client->proto),
rnh_str(rnh, buf, sizeof (buf)));
}
listnode_delete(rnh->client_list, client);
if (list_isempty(rnh->client_list) &&
list_isempty(rnh->zebra_static_route_list))
zebra_delete_rnh(rnh, type);
}
void
zebra_register_rnh_static_nh(struct prefix *nh, struct route_node *static_rn)
{
struct rnh *rnh;
rnh = zebra_add_rnh(nh, 0, RNH_NEXTHOP_TYPE);
if (rnh && !listnode_lookup(rnh->zebra_static_route_list, static_rn))
{
listnode_add(rnh->zebra_static_route_list, static_rn);
}
}
void
zebra_deregister_rnh_static_nh(struct prefix *nh, struct route_node *static_rn)
{
struct rnh *rnh;
rnh = zebra_lookup_rnh(nh, 0, RNH_NEXTHOP_TYPE);
if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED))
return;
listnode_delete(rnh->zebra_static_route_list, static_rn);
if (list_isempty(rnh->client_list) &&
list_isempty(rnh->zebra_static_route_list))
zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
}
void
zebra_deregister_rnh_static_nexthops (struct nexthop *nexthop, struct route_node *rn)
{
struct nexthop *nh;
struct prefix nh_p;
for (nh = nexthop; nh ; nh = nh->next)
{
if (nh->type == NEXTHOP_TYPE_IPV4)
{
nh_p.family = AF_INET;
nh_p.prefixlen = IPV4_MAX_BITLEN;
nh_p.u.prefix4 = nh->gate.ipv4;
}
else if (nh->type == NEXTHOP_TYPE_IPV6)
{
nh_p.family = AF_INET6;
nh_p.prefixlen = IPV6_MAX_BITLEN;
nh_p.u.prefix6 = nh->gate.ipv6;
}
zebra_deregister_rnh_static_nh(&nh_p, rn);
}
}
static int
zebra_evaluate_rnh_nexthops(int family, struct rib *rib, struct route_node *prn,
int proto)
{
int at_least_one = 0;
int rmap_family; /* Route map has diff AF family enum */
struct nexthop *nexthop;
int ret;
rmap_family = (family == AF_INET) ? AFI_IP : AFI_IP6;
if (prn && rib)
{
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
{
ret = zebra_nht_route_map_check(rmap_family, proto, &prn->p, rib,
nexthop);
if (ret != RMAP_DENYMATCH)
{
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
at_least_one++; /* at least one valid NH */
}
else
{
UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
}
}
return (at_least_one);
}
int
zebra_evaluate_rnh (vrf_id_t vrfid, int family, int force, rnh_type_t type,
struct prefix *p)
{
struct route_table *ptable;
struct route_table *ntable;
struct route_node *prn = NULL;
struct route_node *nrn = NULL;
struct rnh *rnh;
struct zserv *client;
struct listnode *node;
struct rib *rib, *srib;
int state_changed = 0;
int at_least_one = 0;
char bufn[PREFIX2STR_BUFFER];
char bufp[PREFIX2STR_BUFFER];
char bufs[PREFIX2STR_BUFFER];
struct route_node *static_rn;
struct nexthop *nexthop, *tnexthop;
int recursing;
ntable = get_rnh_table(vrfid, family, type);
if (!ntable)
{
zlog_debug("evaluate_rnh_table: rnh table not found\n");
return -1;
}
ptable = zebra_vrf_table(family2afi(family), SAFI_UNICAST, vrfid);
if (!ptable)
{
zlog_debug("evaluate_rnh_table: prefix table not found\n");
return -1;
}
if (p)
nrn = route_node_lookup(ntable, p);
else
nrn = route_top (ntable);
while (nrn != NULL)
{
if (!nrn->info)
goto loopend;
rnh = nrn->info;
at_least_one = 0;
/* free stale stuff first */
if (prn)
route_unlock_node(prn);
prn = route_node_match(ptable, &nrn->p);
/* Do not resolve over default route unless allowed &&
* match route to be exact if so specified
*/
if (!prn)
rib = NULL;
else if ((type == RNH_NEXTHOP_TYPE) &&
(is_default_prefix (&prn->p) &&
!nh_resolve_via_default(prn->p.family)))
rib = NULL;
else if ((type == RNH_IMPORT_CHECK_TYPE) &&
((is_default_prefix(&prn->p)) ||
((CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)) &&
!prefix_same(&nrn->p, &prn->p))))
rib = NULL;
else
{
RNODE_FOREACH_RIB(prn, rib)
{
if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
continue;
if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
{
if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
{
if (rib->type == ZEBRA_ROUTE_CONNECT)
break;
}
else if ((type == RNH_IMPORT_CHECK_TYPE) &&
(rib->type == ZEBRA_ROUTE_BGP))
continue;
else
break;
}
}
}
state_changed = 0;
/* Handle import check first as its simpler */
if (type == RNH_IMPORT_CHECK_TYPE)
{
if (rib && (rnh->state == NULL))
{
for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
{
state_changed = 1;
break;
}
}
else if (!rib && (rnh->state != NULL))
state_changed = 1;
if (compare_state(rib, rnh->state))
copy_state(rnh, rib, nrn);
if (state_changed || force)
{
if (IS_ZEBRA_DEBUG_NHT)
{
prefix2str(&nrn->p, bufn, sizeof (bufn));
zlog_debug("rnh import check %s for %s, notifying clients\n",
rnh->state ? "passed" : "failed", bufn);
}
/* state changed, notify clients */
for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
{
send_client(rnh, client, RNH_IMPORT_CHECK_TYPE, vrfid);
}
}
goto loopend;
}
/* If nexthop cannot be resolved and that is also the existing state,
* there is nothing further to do.
*/
if (!rib && rnh->state == NULL)
goto loopend;
/* Ensure prefixes we're resolving over have stayed the same */
if (!prefix_same(&rnh->resolved_route, &prn->p))
{
if (rib)
UNSET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
if (prn)
prefix_copy(&rnh->resolved_route, &prn->p);
else
memset(&rnh->resolved_route, 0, sizeof(struct prefix));
copy_state(rnh, rib, nrn);
state_changed = 1;
}
else if (compare_state(rib, rnh->state))
{
if (rib)
UNSET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
copy_state(rnh, rib, nrn);
state_changed = 1;
}
if (IS_ZEBRA_DEBUG_NHT && (state_changed || force))
{
prefix2str(&nrn->p, bufn, sizeof (bufn));
if (prn)
prefix2str(&prn->p, bufp, sizeof (bufp));
else
strcpy(bufp, "null");
zlog_debug("%s: State changed for %s/%s", __FUNCTION__, bufn, bufp);
}
/* Notify registered clients */
rib = rnh->state;
if (state_changed || force)
{
for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
{
if (prn && rib)
{
at_least_one = zebra_evaluate_rnh_nexthops(family, rib, prn,
client->proto);
if (at_least_one)
rnh->filtered[client->proto] = 0;
else
rnh->filtered[client->proto] = 1;
}
else if (state_changed)
rnh->filtered[client->proto] = 0;
if (IS_ZEBRA_DEBUG_NHT && (state_changed || force))
zlog_debug("%srnh %s resolved through route %s - sending "
"nexthop %s event to clients",
at_least_one ? "":"(filtered)", bufn, bufp,
rib ? "reachable" : "unreachable");
send_client(rnh, client, RNH_NEXTHOP_TYPE, vrfid); /* Route-map passed */
}
/* Now evaluate static client */
if (prn && rib)
{
at_least_one = zebra_evaluate_rnh_nexthops(family, rib, prn,
ZEBRA_ROUTE_STATIC);
if (at_least_one)
rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
else
rnh->filtered[ZEBRA_ROUTE_STATIC] = 1;
}
else if (state_changed)
rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
for (ALL_LIST_ELEMENTS_RO(rnh->zebra_static_route_list, node,
static_rn))
{
RNODE_FOREACH_RIB(static_rn, srib)
{
if (srib->type == ZEBRA_ROUTE_STATIC)
break; /* currently works for only 1 static route. */
}
if (!srib)
{
if (IS_ZEBRA_DEBUG_NHT)
{
prefix2str(&static_rn->p, bufs, sizeof (bufs));
zlog_debug("%s: Unable to find RIB for static route %s, skipping NH resolution",
__FUNCTION__, bufs);
continue;
}
}
/* Mark the appropriate static route's NH as filtered */
for (nexthop = srib->nexthop; nexthop; nexthop = nexthop->next)
{
switch (nexthop->type)
{
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
/* Don't see a use case for *_IFNAME */
if (nexthop->gate.ipv4.s_addr == nrn->p.u.prefix4.s_addr)
{
if (at_least_one)
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED);
else
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED);
}
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
/* Don't see a use case for *_IFNAME */
if (memcmp(&nexthop->gate.ipv6,&nrn->p.u.prefix6, 16) == 0)
{
if (at_least_one)
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED);
else
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED);
}
break;
default:
break;
}
}
if (IS_ZEBRA_DEBUG_NHT && (state_changed || force))
zlog_debug("%srnh %s resolved through route %s - sending "
"nexthop %s event to zebra",
at_least_one ? "":"(filtered)", bufn, bufp,
rib ? "reachable" : "unreachable");
if (srib && (state_changed || force))
{
SET_FLAG(srib->flags, ZEBRA_FLAG_CHANGED);
SET_FLAG(srib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
rib_queue_add(&zebrad, static_rn);
}
}
}
loopend:
if (p)
{
route_unlock_node(nrn);
nrn = NULL;
}
else
{
/* route_next takes care of unlocking nrn */
nrn = route_next(nrn);
}
}
if (prn)
route_unlock_node(prn);
return 1;
}
int
zebra_dispatch_rnh_table (vrf_id_t vrf_id, int family, struct zserv *client,
rnh_type_t type)
{
struct route_table *ntable;
struct route_node *nrn;
struct rnh *rnh;
ntable = get_rnh_table(vrf_id, family, type);
if (!ntable)
{
zlog_debug("dispatch_rnh_table: rnh table not found\n");
return -1;
}
for (nrn = route_top (ntable); nrn; nrn = route_next (nrn))
{
if (!nrn->info)
continue;
rnh = nrn->info;
if (IS_ZEBRA_DEBUG_NHT)
{
char bufn[PREFIX2STR_BUFFER];
prefix2str(&nrn->p, bufn, sizeof (bufn));
zlog_debug("rnh %s - sending nexthop %s event to client %s", bufn,
rnh->state ? "reachable" : "unreachable",
zebra_route_string(client->proto));
}
send_client(rnh, client, RNH_NEXTHOP_TYPE, vrf_id);
}
return 1;
}
void
zebra_print_rnh_table (vrf_id_t vrfid, int af, struct vty *vty, rnh_type_t type)
{
struct route_table *table;
struct route_node *rn;
table = get_rnh_table(vrfid, af, type);
if (!table)
{
zlog_debug("print_rnhs: rnh table not found\n");
return;
}
for (rn = route_top(table); rn; rn = route_next(rn))
if (rn->info)
print_rnh(rn, vty);
}
int
zebra_cleanup_rnh_client (vrf_id_t vrfid, int family, struct zserv *client,
rnh_type_t type)
{
struct route_table *ntable;
struct route_node *nrn;
struct rnh *rnh;
ntable = get_rnh_table(vrfid, family, type);
if (!ntable)
{
zlog_debug("cleanup_rnh_client: rnh table not found\n");
return -1;
}
for (nrn = route_top (ntable); nrn; nrn = route_next (nrn))
{
if (!nrn->info)
continue;
rnh = nrn->info;
if (IS_ZEBRA_DEBUG_NHT)
{
char bufn[PREFIX2STR_BUFFER];
prefix2str(&nrn->p, bufn, sizeof (bufn));
zlog_debug("rnh %s - cleaning state for client %s", bufn,
zebra_route_string(client->proto));
}
zebra_remove_rnh_client(rnh, client, type);
}
return 1;
}
/**
* free_state - free up the rib structure associated with the rnh.
*/
static void
free_state (struct rib *rib, struct route_node *rn)
{
if (!rib)
return;
/* free RIB and nexthops */
zebra_deregister_rnh_static_nexthops (rib->nexthop, rn);
nexthops_free(rib->nexthop);
XFREE (MTYPE_RIB, rib);
}
static void
copy_state (struct rnh *rnh, struct rib *rib, struct route_node *rn)
{
struct rib *state;
struct nexthop *nh;
if (rnh->state)
{
free_state(rnh->state, rn);
rnh->state = NULL;
}
if (!rib)
return;
state = XCALLOC (MTYPE_RIB, sizeof (struct rib));
state->type = rib->type;
state->metric = rib->metric;
for (nh = rib->nexthop; nh; nh = nh->next)
rib_copy_nexthops(state, nh);
rnh->state = state;
}
static int
compare_state (struct rib *r1, struct rib *r2)
{
if (!r1 && !r2)
return 0;
if ((!r1 && r2) || (r1 && !r2))
return 1;
if (r1->metric != r2->metric)
return 1;
if (r1->nexthop_num != r2->nexthop_num)
return 1;
if (CHECK_FLAG(r1->status, RIB_ENTRY_NEXTHOPS_CHANGED))
return 1;
return 0;
}
static int
send_client (struct rnh *rnh, struct zserv *client, rnh_type_t type, vrf_id_t vrf_id)
{
struct stream *s;
struct rib *rib;
unsigned long nump;
u_char num;
struct nexthop *nexthop;
struct route_node *rn;
int cmd = (type == RNH_IMPORT_CHECK_TYPE)
? ZEBRA_IMPORT_CHECK_UPDATE : ZEBRA_NEXTHOP_UPDATE;
rn = rnh->node;
rib = rnh->state;
/* Get output stream. */
s = client->obuf;
stream_reset (s);
zserv_create_header (s, cmd, vrf_id);
stream_putw(s, rn->p.family);
switch (rn->p.family)
{
case AF_INET:
stream_putc(s, rn->p.prefixlen);
stream_put_in_addr(s, &rn->p.u.prefix4);
break;
case AF_INET6:
stream_putc(s, rn->p.prefixlen);
stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN);
break;
default:
zlog_err("%s: Unknown family (%d) notification attempted\n",
__FUNCTION__, rn->p.family);
break;
}
if (rib)
{
stream_putl (s, rib->metric);
num = 0;
nump = stream_get_endp(s);
stream_putc (s, 0);
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
if ((CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ||
CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) &&
CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
{
stream_putc (s, nexthop->type);
switch (nexthop->type)
{
case ZEBRA_NEXTHOP_IPV4:
stream_put_in_addr (s, &nexthop->gate.ipv4);
break;
case ZEBRA_NEXTHOP_IFINDEX:
stream_putl (s, nexthop->ifindex);
break;
case ZEBRA_NEXTHOP_IPV4_IFINDEX:
stream_put_in_addr (s, &nexthop->gate.ipv4);
stream_putl (s, nexthop->ifindex);
break;
#ifdef HAVE_IPV6
case ZEBRA_NEXTHOP_IPV6:
stream_put (s, &nexthop->gate.ipv6, 16);
break;
case ZEBRA_NEXTHOP_IPV6_IFINDEX:
stream_put (s, &nexthop->gate.ipv6, 16);
stream_putl (s, nexthop->ifindex);
break;
#endif /* HAVE_IPV6 */
default:
/* do nothing */
break;
}
num++;
}
stream_putc_at (s, nump, num);
}
else
{
stream_putl (s, 0);
stream_putc (s, 0);
}
stream_putw_at (s, 0, stream_get_endp (s));
client->nh_last_upd_time = quagga_time(NULL);
client->last_write_cmd = cmd;
return zebra_server_send_message(client);
}
static void
print_nh (struct nexthop *nexthop, struct vty *vty)
{
char buf[BUFSIZ];
switch (nexthop->type)
{
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
vty_out (vty, " via %s", inet_ntoa (nexthop->gate.ipv4));
if (nexthop->ifindex)
vty_out (vty, ", %s", ifindex2ifname (nexthop->ifindex));
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
vty_out (vty, " %s",
inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
if (nexthop->ifindex)
vty_out (vty, ", via %s", ifindex2ifname (nexthop->ifindex));
break;
case NEXTHOP_TYPE_IFINDEX:
vty_out (vty, " is directly connected, %s",
ifindex2ifname (nexthop->ifindex));
break;
case NEXTHOP_TYPE_BLACKHOLE:
vty_out (vty, " is directly connected, Null0");
break;
default:
break;
}
vty_out(vty, "%s", VTY_NEWLINE);
}
static void
print_rnh (struct route_node *rn, struct vty *vty)
{
struct rnh *rnh;
struct nexthop *nexthop;
struct listnode *node;
struct zserv *client;
char buf[BUFSIZ];
rnh = rn->info;
vty_out(vty, "%s%s%s", inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)" : "",
VTY_NEWLINE);
if (rnh->state)
{
vty_out(vty, " resolved via %s%s",
zebra_route_string(rnh->state->type), VTY_NEWLINE);
for (nexthop = rnh->state->nexthop; nexthop; nexthop = nexthop->next)
print_nh(nexthop, vty);
}
else
vty_out(vty, " unresolved%s%s",
CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)" : "",
VTY_NEWLINE);
vty_out(vty, " Client list:");
for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
client->sock, rnh->filtered[client->proto] ? "(filtered)" : "");
if (!list_isempty(rnh->zebra_static_route_list))
vty_out(vty, " zebra%s", rnh->filtered[ZEBRA_ROUTE_STATIC] ? "(filtered)" : "");
vty_out(vty, "%s", VTY_NEWLINE);
}
|