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
|
/* $OpenBSD: rde_rib.c,v 1.43 2004/04/28 07:05:27 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <string.h>
#include "bgpd.h"
#include "ensure.h"
#include "rde.h"
/*
* BGP RIB -- Routing Information Base
*
* The RIB is build with one aspect in mind. Speed -- actually update speed.
* Therefor one thing needs to be absolutely avoided, long table walks.
* This is achieved by heavily linking the different parts together.
*/
struct rib_stats {
u_int64_t path_update;
u_int64_t path_get;
u_int64_t path_add;
u_int64_t path_remove;
u_int64_t path_updateall;
u_int64_t path_destroy;
u_int64_t path_link;
u_int64_t path_unlink;
u_int64_t path_alloc;
u_int64_t path_free;
u_int64_t prefix_get;
u_int64_t prefix_add;
u_int64_t prefix_move;
u_int64_t prefix_remove;
u_int64_t prefix_updateall;
u_int64_t prefix_link;
u_int64_t prefix_unlink;
u_int64_t prefix_alloc;
u_int64_t prefix_free;
u_int64_t nexthop_add;
u_int64_t nexthop_remove;
u_int64_t nexthop_update;
u_int64_t nexthop_get;
u_int64_t nexthop_alloc;
u_int64_t nexthop_free;
} ribstats;
#define RIB_STAT(x) (ribstats.x++)
/*
* Maximum number of prefixes we allow per prefix. The number should
* not be too big and ensure only that the prefix count is properly
* increased and decreased. Only useful if ENSURE is active.
*/
#define MAX_PREFIX_PER_AS 1500
/* path specific functions */
static void path_link(struct rde_aspath *, struct rde_peer *);
static void path_unlink(struct rde_aspath *);
static struct rde_aspath *path_alloc(void);
static void path_free(struct rde_aspath *);
struct path_table pathtable;
#define PATH_HASH(x) \
&pathtable.path_hashtbl[aspath_hash((x)) & pathtable.path_hashmask]
void
path_init(u_int32_t hashsize)
{
u_int32_t hs, i;
for (hs = 1; hs < hashsize; hs <<= 1)
;
pathtable.path_hashtbl = calloc(hs, sizeof(struct aspath_head));
if (pathtable.path_hashtbl == NULL)
fatal("path_init");
for (i = 0; i < hs; i++)
LIST_INIT(&pathtable.path_hashtbl[i]);
pathtable.path_hashmask = hs - 1;
}
void
path_shutdown(void)
{
u_int32_t i;
for (i = 0; i <= pathtable.path_hashmask; i++)
if (!LIST_EMPTY(&pathtable.path_hashtbl[i]))
log_warnx("path_free: free non-free table");
free(pathtable.path_hashtbl);
}
void
path_update(struct rde_peer *peer, struct attr_flags *attrs,
struct bgpd_addr *prefix, int prefixlen)
{
struct rde_aspath *asp;
struct prefix *p;
struct pt_entry *pte;
RIB_STAT(path_update);
if ((asp = path_get(attrs->aspath, peer)) == NULL) {
/* path not available */
asp = path_add(peer, attrs);
pte = prefix_add(asp, prefix, prefixlen);
} else {
if (attr_compare(&asp->flags, attrs) == 0) {
/* path are equal, just add prefix */
pte = prefix_add(asp, prefix, prefixlen);
attr_free(attrs);
} else {
/* non equal path attributes create new path */
if ((p = prefix_get(asp, prefix, prefixlen)) == NULL) {
asp = path_add(peer, attrs);
pte = prefix_add(asp, prefix, prefixlen);
} else {
asp = path_add(peer, attrs);
pte = prefix_move(asp, p);
}
}
}
}
struct rde_aspath *
path_get(struct aspath *aspath, struct rde_peer *peer)
{
struct aspath_head *head;
struct rde_aspath *asp;
RIB_STAT(path_get);
head = PATH_HASH(aspath);
ENSURE(head != NULL);
LIST_FOREACH(asp, head, path_l) {
if (aspath_compare(asp->flags.aspath, aspath) == 0 &&
peer == asp->peer)
return asp;
}
return NULL;
}
struct rde_aspath *
path_add(struct rde_peer *peer, struct attr_flags *attr)
{
struct rde_aspath *asp;
RIB_STAT(path_add);
ENSURE(peer != NULL);
asp = path_alloc();
attr_move(&asp->flags, attr);
path_link(asp, peer);
return asp;
}
void
path_remove(struct rde_aspath *asp)
{
struct prefix *p;
RIB_STAT(path_remove);
while ((p = LIST_FIRST(&asp->prefix_h)) != NULL) {
prefix_destroy(p);
}
path_destroy(asp);
}
void
path_updateall(struct rde_aspath *asp, enum nexthop_state state)
{
RIB_STAT(path_updateall);
if (rde_noevaluate())
/* if the decision process is turned off this is a no-op */
return;
prefix_updateall(asp, state);
}
/* this function is only called by prefix_remove and path_remove */
void
path_destroy(struct rde_aspath *asp)
{
RIB_STAT(path_destroy);
/* path_destroy can only unlink and free empty rde_aspath */
ENSURE(path_empty(asp));
path_unlink(asp);
path_free(asp);
}
int
path_empty(struct rde_aspath *asp)
{
ENSURE(asp != NULL);
return LIST_EMPTY(&asp->prefix_h);
}
/*
* the path object is linked into multiple lists for fast access.
* These are peer_l, path_l and nexthop_l.
* peer_l: list of all aspaths that belong to that peer
* path_l: hash list to find paths quickly
* nexthop_l: list of all aspaths with an equal exit nexthop
*/
static void
path_link(struct rde_aspath *asp, struct rde_peer *peer)
{
struct aspath_head *head;
RIB_STAT(path_link);
head = PATH_HASH(asp->flags.aspath);
ENSURE(head != NULL);
LIST_INSERT_HEAD(head, asp, path_l);
LIST_INSERT_HEAD(&peer->path_h, asp, peer_l);
asp->peer = peer;
ENSURE(asp->nexthop == NULL);
nexthop_add(asp);
}
static void
path_unlink(struct rde_aspath *asp)
{
RIB_STAT(path_unlink);
ENSURE(path_empty(asp));
ENSURE(asp->prefix_cnt == 0 && asp->active_cnt == 0);
nexthop_remove(asp);
LIST_REMOVE(asp, path_l);
LIST_REMOVE(asp, peer_l);
asp->peer = NULL;
asp->nexthop = NULL;
attr_free(&asp->flags);
}
/* alloc and initialize new entry. May not fail. */
static struct rde_aspath *
path_alloc(void)
{
struct rde_aspath *asp;
RIB_STAT(path_alloc);
asp = calloc(1, sizeof(*asp));
if (asp == NULL)
fatal("path_alloc");
LIST_INIT(&asp->prefix_h);
return asp;
}
/* free a unlinked element */
static void
path_free(struct rde_aspath *asp)
{
RIB_STAT(path_free);
ENSURE(asp->peer == NULL &&
asp->flags.aspath == NULL &&
TAILQ_EMPTY(&asp->flags.others));
free(asp);
}
/* prefix specific functions */
static struct prefix *prefix_alloc(void);
static void prefix_free(struct prefix *);
static void prefix_link(struct prefix *, struct pt_entry *,
struct rde_aspath *);
static void prefix_unlink(struct prefix *);
/*
* search in the path list for specified prefix. Returns NULL if not found.
*/
struct prefix *
prefix_get(struct rde_aspath *asp, struct bgpd_addr *prefix, int prefixlen)
{
struct prefix *p;
RIB_STAT(prefix_get);
ENSURE(asp != NULL);
LIST_FOREACH(p, &asp->prefix_h, path_l) {
ENSURE(p->prefix != NULL);
if (p->prefix->prefixlen == prefixlen &&
p->prefix->prefix.v4.s_addr == prefix->v4.s_addr) {
ENSURE(p->aspath == asp);
return p;
}
}
return NULL;
}
/*
* Adds or updates a prefix.
*/
struct pt_entry *
prefix_add(struct rde_aspath *asp, struct bgpd_addr *prefix, int prefixlen)
{
struct prefix *p;
struct pt_entry *pte;
int needlink = 0;
RIB_STAT(prefix_add);
pte = pt_get(prefix, prefixlen);
if (pte == NULL) {
pte = pt_add(prefix, prefixlen);
}
p = prefix_bypeer(pte, asp->peer);
if (p == NULL) {
needlink = 1;
p = prefix_alloc();
}
if (needlink == 1)
prefix_link(p, pte, asp);
else {
if (p->aspath != asp)
/* prefix belongs to a different aspath so move */
return prefix_move(asp, p);
p->lastchange = time(NULL);
}
return pte;
}
/*
* Move the prefix to the specified as path, removes the old asp if needed.
*/
struct pt_entry *
prefix_move(struct rde_aspath *asp, struct prefix *p)
{
struct prefix *np;
struct rde_aspath *oasp;
RIB_STAT(prefix_move);
ENSURE(asp->peer == p->aspath->peer);
/* create new prefix node */
np = prefix_alloc();
np->aspath = asp;
/* peer and prefix pointers are still equal */
np->prefix = p->prefix;
np->peer = p->peer;
np->lastchange = time(NULL);
/* add to new as path */
LIST_INSERT_HEAD(&asp->prefix_h, np, path_l);
asp->prefix_cnt++;
/*
* no need to update the peer prefix count because we are only moving
* the prefix without changing the peer.
*/
/* XXX for debugging */
ENSURE(asp->prefix_cnt < MAX_PREFIX_PER_AS);
/*
* First kick the old prefix node out of the prefix list,
* afterwards run the route decision for new prefix node.
* Because of this only one update is generated if the prefix
* was active.
* This is save because we create a new prefix and so the change
* is noticed by prefix_evaluate().
*/
LIST_REMOVE(p, prefix_l);
prefix_evaluate(np, np->prefix);
/* remove old prefix node */
oasp = p->aspath;
LIST_REMOVE(p, path_l);
ENSURE(oasp->prefix_cnt > 0);
ENSURE(oasp->peer->prefix_cnt > 0);
oasp->prefix_cnt--;
/* as before peer count needs no update because of move */
/* destroy all references to other objects and free the old prefix */
p->aspath = NULL;
p->prefix = NULL;
prefix_free(p);
/* destroy old path if empty */
if (path_empty(oasp))
path_destroy(oasp);
return np->prefix;
}
/*
* Removes a prefix from all lists. If the parent objects -- path or
* pt_entry -- become empty remove them too.
*/
void
prefix_remove(struct rde_peer *peer, struct bgpd_addr *prefix, int prefixlen)
{
struct prefix *p;
struct pt_entry *pte;
struct rde_aspath *asp;
RIB_STAT(prefix_remove);
pte = pt_get(prefix, prefixlen);
if (pte == NULL) /* Got a dummy withdrawn request */
return;
p = prefix_bypeer(pte, peer);
if (p == NULL) /* Got a dummy withdrawn request. */
return;
asp = p->aspath;
prefix_unlink(p);
prefix_free(p);
if (pt_empty(pte))
pt_remove(pte);
if (path_empty(asp))
path_destroy(asp);
}
/*
* Searches in the prefix list of specified pt_entry for a prefix entry
* belonging to the peer peer. Returns NULL if no match found.
*/
struct prefix *
prefix_bypeer(struct pt_entry *pte, struct rde_peer *peer)
{
struct prefix *p;
ENSURE(pte != NULL);
LIST_FOREACH(p, &pte->prefix_h, prefix_l) {
if (p->aspath->peer == peer)
return p;
}
return NULL;
}
void
prefix_updateall(struct rde_aspath *asp, enum nexthop_state state)
{
struct prefix *p;
RIB_STAT(prefix_updateall);
ENSURE(asp != NULL);
if (rde_noevaluate())
/* if the decision process is turned off this is a no-op */
return;
LIST_FOREACH(p, &asp->prefix_h, path_l) {
/* redo the route decision */
LIST_REMOVE(p, prefix_l);
/*
* If the prefix is the active one remove it first,
* this has to be done because we can not detect when
* the active prefix changes it's state. In this case
* we know that this is a withdrawl and so the second
* prefix_evaluate() will generate no update because
* the nexthop is unreachable or ineligible.
*/
if (p == p->prefix->active)
prefix_evaluate(NULL, p->prefix);
prefix_evaluate(p, p->prefix);
}
}
/* kill a prefix. Only called by path_remove. */
void
prefix_destroy(struct prefix *p)
{
struct pt_entry *pte;
pte = p->prefix;
prefix_unlink(p);
prefix_free(p);
if (pt_empty(pte))
pt_remove(pte);
}
/*
* helper function to clean up the connected networks after a reload
*/
void
prefix_network_clean(struct rde_peer *peer, time_t reloadtime)
{
struct rde_aspath *asp, *xasp;
struct prefix *p, *xp;
struct pt_entry *pte;
for (asp = LIST_FIRST(&peer->path_h); asp != NULL; asp = xasp) {
xasp = LIST_NEXT(asp, peer_l);
for (p = LIST_FIRST(&asp->prefix_h); p != NULL; p = xp) {
xp = LIST_NEXT(p, path_l);
if (reloadtime > p->lastchange) {
pte = p->prefix;
prefix_unlink(p);
prefix_free(p);
if (pt_empty(pte))
pt_remove(pte);
if (path_empty(asp))
path_destroy(asp);
}
}
}
}
/*
* Link a prefix into the different parent objects.
*/
static void
prefix_link(struct prefix *pref, struct pt_entry *pte, struct rde_aspath *asp)
{
RIB_STAT(prefix_link);
ENSURE(pref->aspath == NULL &&
pref->prefix == NULL);
ENSURE(pref != NULL && pte != NULL && asp != NULL);
ENSURE(prefix_bypeer(pte, asp->peer) == NULL);
LIST_INSERT_HEAD(&asp->prefix_h, pref, path_l);
asp->prefix_cnt++;
asp->peer->prefix_cnt++;
/* XXX for debugging */
ENSURE(asp->prefix_cnt < MAX_PREFIX_PER_AS);
pref->aspath = asp;
pref->prefix = pte;
pref->peer = asp->peer;
pref->lastchange = time(NULL);
/* make route decision */
prefix_evaluate(pref, pte);
}
/*
* Unlink a prefix from the different parent objects.
*/
static void
prefix_unlink(struct prefix *pref)
{
RIB_STAT(prefix_unlink);
ENSURE(pref != NULL);
ENSURE(pref->prefix != NULL && pref->aspath != NULL);
/* make route decision */
LIST_REMOVE(pref, prefix_l);
prefix_evaluate(NULL, pref->prefix);
LIST_REMOVE(pref, path_l);
ENSURE(pref->aspath->prefix_cnt > 0);
ENSURE(pref->aspath->peer->prefix_cnt > 0);
pref->aspath->prefix_cnt--;
pref->aspath->peer->prefix_cnt--;
/* destroy all references to other objects */
pref->aspath = NULL;
pref->prefix = NULL;
/*
* It's the caller's duty to remove empty aspath respectively pt_entry
* structures. Also freeing the unlinked prefix is the caller's duty.
*/
}
/* alloc and bzero new entry. May not fail. */
static struct prefix *
prefix_alloc(void)
{
struct prefix *p;
RIB_STAT(prefix_alloc);
p = calloc(1, sizeof(*p));
if (p == NULL)
fatal("prefix_alloc");
return p;
}
/* free a unlinked entry */
static void
prefix_free(struct prefix *pref)
{
RIB_STAT(prefix_free);
ENSURE(pref->aspath == NULL &&
pref->prefix == NULL);
free(pref);
}
/* nexthop functions */
/*
* XXX
* Storing the nexthop info in a hash table is not optimal. The problem is
* that updates (invalidate and validate) come in as prefixes and so storing
* the nexthops in a hash is not optimal. An (in)validate needs to do a table
* walk to find all candidates.
* Currently I think that there are many more adds and removes so that a
* hash table has more benefits and the table walk should not happen too often.
*/
static struct nexthop *nexthop_get(struct in_addr);
static struct nexthop *nexthop_alloc(void);
static void nexthop_free(struct nexthop *);
/*
* In BGP there exist two nexthops: the exit nexthop which was announced via
* BGP and the true nexthop which is used in the FIB -- forward information
* base a.k.a kernel routing table. When sending updates it is even more
* confusing. In IBGP we pass the unmodified exit nexthop to the neighbors
* while in EBGP normaly the address of the router is sent. The exit nexthop
* may be passed to the external neighbor if the neighbor and the exit nexthop
* reside in the same subnet -- directly connected.
*/
struct nexthop_table {
LIST_HEAD(, nexthop) *nexthop_hashtbl;
u_int32_t nexthop_hashmask;
} nexthoptable;
#define NEXTHOP_HASH(x) \
&nexthoptable.nexthop_hashtbl[ntohl((x.s_addr)) & \
nexthoptable.nexthop_hashmask]
void
nexthop_init(u_int32_t hashsize)
{
struct nexthop *nh;
u_int32_t hs, i;
for (hs = 1; hs < hashsize; hs <<= 1)
;
nexthoptable.nexthop_hashtbl = calloc(hs, sizeof(struct nexthop_table));
if (nexthoptable.nexthop_hashtbl == NULL)
fatal("nextop_init");
for (i = 0; i < hs; i++)
LIST_INIT(&nexthoptable.nexthop_hashtbl[i]);
nexthoptable.nexthop_hashmask = hs - 1;
/* add dummy entry for connected networks */
nh = nexthop_alloc();
nh->state = NEXTHOP_REACH;
nh->exit_nexthop.af = AF_INET;
nh->exit_nexthop.v4.s_addr = INADDR_ANY;
LIST_INSERT_HEAD(NEXTHOP_HASH(nh->exit_nexthop.v4), nh,
nexthop_l);
memcpy(&nh->true_nexthop, &nh->exit_nexthop,
sizeof(nh->true_nexthop));
nh->nexthop_netlen = 0;
nh->nexthop_net.af = AF_INET;
nh->nexthop_net.v4.s_addr = INADDR_ANY;
nh->flags = NEXTHOP_ANNOUNCE;
}
void
nexthop_shutdown(void)
{
struct in_addr addr;
struct nexthop *nh;
u_int32_t i;
/* remove the dummy entry for connected networks */
addr.s_addr = INADDR_ANY;
nh = nexthop_get(addr);
if (nh != NULL) {
if (!LIST_EMPTY(&nh->path_h))
log_warnx("nexthop_free: free non-free announce node");
LIST_REMOVE(nh, nexthop_l);
nexthop_free(nh);
}
for (i = 0; i <= nexthoptable.nexthop_hashmask; i++)
if (!LIST_EMPTY(&nexthoptable.nexthop_hashtbl[i]))
log_warnx("nexthop_free: free non-free table");
free(nexthoptable.nexthop_hashtbl);
}
void
nexthop_add(struct rde_aspath *asp)
{
struct nexthop *nh;
RIB_STAT(nexthop_add);
ENSURE(asp != NULL);
if ((nh = asp->nexthop) == NULL)
nh = nexthop_get(asp->flags.nexthop);
if (nh == NULL) {
nh = nexthop_alloc();
nh->state = NEXTHOP_LOOKUP;
nh->exit_nexthop.af = AF_INET;
nh->exit_nexthop.v4 = asp->flags.nexthop;
LIST_INSERT_HEAD(NEXTHOP_HASH(asp->flags.nexthop), nh,
nexthop_l);
rde_send_nexthop(&nh->exit_nexthop, 1);
}
asp->nexthop = nh;
LIST_INSERT_HEAD(&nh->path_h, asp, nexthop_l);
}
void
nexthop_remove(struct rde_aspath *asp)
{
struct nexthop *nh;
RIB_STAT(nexthop_remove);
ENSURE(asp != NULL);
LIST_REMOVE(asp, nexthop_l);
/* see if list is empty */
nh = asp->nexthop;
/* never remove the dummy announce entry */
if (nh->flags & NEXTHOP_ANNOUNCE)
return;
if (LIST_EMPTY(&nh->path_h)) {
LIST_REMOVE(nh, nexthop_l);
rde_send_nexthop(&nh->exit_nexthop, 0);
nexthop_free(nh);
}
}
static struct nexthop *
nexthop_get(struct in_addr nexthop)
{
struct nexthop *nh;
RIB_STAT(nexthop_get);
LIST_FOREACH(nh, NEXTHOP_HASH(nexthop), nexthop_l) {
if (nh->exit_nexthop.v4.s_addr == nexthop.s_addr)
return nh;
}
return NULL;
}
void
nexthop_update(struct kroute_nexthop *msg)
{
struct nexthop *nh;
struct rde_aspath *asp;
RIB_STAT(nexthop_update);
nh = nexthop_get(msg->nexthop.v4);
if (nh == NULL) {
log_warnx("nexthop_update: non-existent nexthop");
return;
}
ENSURE(nh->exit_nexthop.v4.s_addr == msg->nexthop.v4.s_addr);
if (msg->valid)
nh->state = NEXTHOP_REACH;
else
nh->state = NEXTHOP_UNREACH;
if (msg->connected)
memcpy(&nh->true_nexthop, &nh->exit_nexthop,
sizeof(nh->true_nexthop));
else
memcpy(&nh->true_nexthop, &msg->gateway,
sizeof(nh->true_nexthop));
nh->nexthop_netlen = msg->kr.prefixlen;
nh->nexthop_net.af = AF_INET;
nh->nexthop_net.v4.s_addr = msg->kr.prefix.s_addr;
if (msg->connected)
nh->flags |= NEXTHOP_CONNECTED;
if (rde_noevaluate())
/*
* if the decision process is turned off there is no need
* for the aspath list walk.
*/
return;
LIST_FOREACH(asp, &nh->path_h, nexthop_l) {
path_updateall(asp, nh->state);
}
}
static struct nexthop *
nexthop_alloc(void)
{
struct nexthop *nh;
RIB_STAT(nexthop_alloc);
nh = calloc(1, sizeof(*nh));
if (nh == NULL)
fatal("nexthop_alloc");
LIST_INIT(&nh->path_h);
return nh;
}
static void
nexthop_free(struct nexthop *nh)
{
RIB_STAT(nexthop_free);
ENSURE(LIST_EMPTY(&nh->path_h));
free(nh);
}
|