forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathospfd.c
2306 lines (1836 loc) · 55.5 KB
/
ospfd.c
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OSPF version 2 daemon program.
* Copyright (C) 1999, 2000 Toshiaki Takada
*
* 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 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 "thread.h"
#include "vty.h"
#include "command.h"
#include "linklist.h"
#include "prefix.h"
#include "table.h"
#include "if.h"
#include "memory.h"
#include "stream.h"
#include "log.h"
#include "sockunion.h" /* for inet_aton () */
#include "zclient.h"
#include "routemap.h"
#include "plist.h"
#include "sockopt.h"
#include "bfd.h"
#include "libfrr.h"
#include "defaults.h"
#include "lib_errors.h"
#include "ldp_sync.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_bfd.h"
#include "ospfd/ospf_network.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_abr.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_ase.h"
#include "ospfd/ospf_ldp_sync.h"
#include "ospfd/ospf_gr.h"
DEFINE_QOBJ_TYPE(ospf);
/* OSPF process wide configuration. */
static struct ospf_master ospf_master;
/* OSPF process wide configuration pointer to export. */
struct ospf_master *om;
unsigned short ospf_instance;
extern struct zclient *zclient;
static void ospf_remove_vls_through_area(struct ospf *, struct ospf_area *);
static void ospf_network_free(struct ospf *, struct ospf_network *);
static void ospf_area_free(struct ospf_area *);
static void ospf_network_run(struct prefix *, struct ospf_area *);
static void ospf_network_run_interface(struct ospf *, struct interface *,
struct prefix *, struct ospf_area *);
static void ospf_network_run_subnet(struct ospf *, struct connected *,
struct prefix *, struct ospf_area *);
static int ospf_network_match_iface(const struct connected *,
const struct prefix *);
static void ospf_finish_final(struct ospf *);
#define OSPF_EXTERNAL_LSA_ORIGINATE_DELAY 1
int p_spaces_compare_func(const struct p_space *a, const struct p_space *b)
{
if (a->protected_resource->type == OSPF_TI_LFA_LINK_PROTECTION
&& b->protected_resource->type == OSPF_TI_LFA_LINK_PROTECTION)
return (a->protected_resource->link->link_id.s_addr
- b->protected_resource->link->link_id.s_addr);
if (a->protected_resource->type == OSPF_TI_LFA_NODE_PROTECTION
&& b->protected_resource->type == OSPF_TI_LFA_NODE_PROTECTION)
return (a->protected_resource->router_id.s_addr
- b->protected_resource->router_id.s_addr);
/* This should not happen */
return 0;
}
int q_spaces_compare_func(const struct q_space *a, const struct q_space *b)
{
return (a->root->id.s_addr - b->root->id.s_addr);
}
DECLARE_RBTREE_UNIQ(p_spaces, struct p_space, p_spaces_item,
p_spaces_compare_func);
void ospf_process_refresh_data(struct ospf *ospf, bool reset)
{
struct vrf *vrf = vrf_lookup_by_id(ospf->vrf_id);
struct in_addr router_id, router_id_old;
struct ospf_interface *oi;
struct interface *ifp;
struct listnode *node, *nnode;
struct ospf_area *area;
bool rid_change = false;
if (!ospf->oi_running) {
if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"Router ospf not configured -- Router-ID update postponed");
return;
}
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("Router-ID[OLD:%pI4]: Update",
&ospf->router_id);
router_id_old = ospf->router_id;
/* Select the router ID based on these priorities:
1. Statically assigned router ID is always the first choice.
2. If there is no statically assigned router ID, then try to stick
with the most recent value, since changing router ID's is very
disruptive.
3. Last choice: just go with whatever the zebra daemon recommends.
*/
if (ospf->router_id_static.s_addr != INADDR_ANY)
router_id = ospf->router_id_static;
else if (ospf->router_id.s_addr != INADDR_ANY)
router_id = ospf->router_id;
else
router_id = ospf->router_id_zebra;
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("Router-ID[OLD:%pI4]: Update to %pI4",
&ospf->router_id, &router_id);
rid_change = !(IPV4_ADDR_SAME(&router_id_old, &router_id));
if (rid_change || (reset)) {
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
/* Some nbrs are identified by router_id, these needs
* to be rebuilt. Possible optimization would be to do
* oi->nbr_self->router_id = router_id for
* !(virtual | ptop) links
*/
ospf_nbr_self_reset(oi, router_id);
/*
* If the old router id was not set, but now it
* is and the interface is operative and the
* state is ISM_Down we should kick the state
* machine as that we processed the interfaces
* based upon the network statement( or intf config )
* but could not start it at that time.
*/
if (if_is_operative(oi->ifp) && oi->state == ISM_Down
&& router_id_old.s_addr == INADDR_ANY)
ospf_if_up(oi);
}
/* Flush (inline) all the self originated LSAs */
ospf_flush_self_originated_lsas_now(ospf);
ospf->router_id = router_id;
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("Router-ID[NEW:%pI4]: Update",
&ospf->router_id);
/* Flush (inline) all external LSAs which now match the new
router-id,
need to adjust the OSPF_LSA_SELF flag, so the flush doesn't
hit
asserts in ospf_refresher_unregister_lsa(). This step is
needed
because the current quagga code does look-up for
self-originated LSAs
based on the self router-id alone but expects OSPF_LSA_SELF
to be
properly set */
if (ospf->lsdb) {
struct route_node *rn;
struct ospf_lsa *lsa;
LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa) {
/* AdvRouter and Router ID is the same. */
if (IPV4_ADDR_SAME(&lsa->data->adv_router,
&ospf->router_id) && rid_change) {
SET_FLAG(lsa->flags,
OSPF_LSA_SELF_CHECKED);
SET_FLAG(lsa->flags, OSPF_LSA_SELF);
ospf_lsa_flush_schedule(ospf, lsa);
}
/* The above flush will send immediately
* So discard the LSA to originate new
*/
ospf_discard_from_db(ospf, ospf->lsdb, lsa);
}
LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
ospf_discard_from_db(ospf, ospf->lsdb, lsa);
ospf_lsdb_delete_all(ospf->lsdb);
}
/* Since the LSAs are deleted, need reset the aggr flag */
ospf_unset_all_aggr_flag(ospf);
/* Delete the LSDB */
for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area))
ospf_area_lsdb_discard_delete(area);
/* update router-lsa's for each area */
ospf_router_lsa_update(ospf);
/* update ospf_interface's */
FOR_ALL_INTERFACES (vrf, ifp) {
if (reset)
ospf_if_reset(ifp);
else
ospf_if_update(ospf, ifp);
}
ospf_external_lsa_rid_change(ospf);
}
ospf->inst_shutdown = 0;
}
void ospf_router_id_update(struct ospf *ospf)
{
ospf_process_refresh_data(ospf, false);
}
void ospf_process_reset(struct ospf *ospf)
{
ospf_process_refresh_data(ospf, true);
}
void ospf_neighbor_reset(struct ospf *ospf, struct in_addr nbr_id,
const char *nbr_str)
{
struct route_node *rn;
struct ospf_neighbor *nbr;
struct ospf_interface *oi;
struct listnode *node;
/* Clear only a particular nbr with nbr router id as nbr_id */
if (nbr_str != NULL) {
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, &nbr_id);
if (nbr)
OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
}
return;
}
/* send Neighbor event KillNbr to all associated neighbors. */
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
nbr = rn->info;
if (nbr && (nbr != oi->nbr_self))
OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
}
}
}
/* For OSPF area sort by area id. */
static int ospf_area_id_cmp(struct ospf_area *a1, struct ospf_area *a2)
{
if (ntohl(a1->area_id.s_addr) > ntohl(a2->area_id.s_addr))
return 1;
if (ntohl(a1->area_id.s_addr) < ntohl(a2->area_id.s_addr))
return -1;
return 0;
}
static void ospf_add(struct ospf *ospf)
{
listnode_add(om->ospf, ospf);
}
static void ospf_delete(struct ospf *ospf)
{
listnode_delete(om->ospf, ospf);
}
struct ospf *ospf_new_alloc(unsigned short instance, const char *name)
{
int i;
struct vrf *vrf = NULL;
struct ospf *new = XCALLOC(MTYPE_OSPF_TOP, sizeof(struct ospf));
new->instance = instance;
new->router_id.s_addr = htonl(0);
new->router_id_static.s_addr = htonl(0);
if (name) {
vrf = vrf_lookup_by_name(name);
if (vrf)
new->vrf_id = vrf->vrf_id;
else
new->vrf_id = VRF_UNKNOWN;
/* Freed in ospf_finish_final */
new->name = XSTRDUP(MTYPE_OSPF_TOP, name);
if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"%s: Create new ospf instance with vrf_name %s vrf_id %u",
__func__, name, new->vrf_id);
} else {
new->vrf_id = VRF_DEFAULT;
vrf = vrf_lookup_by_id(VRF_DEFAULT);
}
if (vrf)
ospf_vrf_link(new, vrf);
ospf_zebra_vrf_register(new);
new->abr_type = OSPF_ABR_DEFAULT;
new->oiflist = list_new();
new->vlinks = list_new();
new->areas = list_new();
new->areas->cmp = (int (*)(void *, void *))ospf_area_id_cmp;
new->networks = route_table_init();
new->nbr_nbma = route_table_init();
new->lsdb = ospf_lsdb_new();
new->default_originate = DEFAULT_ORIGINATE_NONE;
new->passive_interface_default = OSPF_IF_ACTIVE;
new->new_external_route = route_table_init();
new->old_external_route = route_table_init();
new->external_lsas = route_table_init();
new->stub_router_startup_time = OSPF_STUB_ROUTER_UNCONFIGURED;
new->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
new->stub_router_admin_set = OSPF_STUB_ROUTER_ADMINISTRATIVE_UNSET;
/* Distribute parameter init. */
for (i = 0; i <= ZEBRA_ROUTE_MAX; i++) {
new->dtag[i] = 0;
}
new->default_metric = -1;
new->ref_bandwidth = OSPF_DEFAULT_REF_BANDWIDTH;
/* LSA timers */
new->min_ls_interval = OSPF_MIN_LS_INTERVAL;
new->min_ls_arrival = OSPF_MIN_LS_ARRIVAL;
/* SPF timer value init. */
new->spf_delay = OSPF_SPF_DELAY_DEFAULT;
new->spf_holdtime = OSPF_SPF_HOLDTIME_DEFAULT;
new->spf_max_holdtime = OSPF_SPF_MAX_HOLDTIME_DEFAULT;
new->spf_hold_multiplier = 1;
/* MaxAge init. */
new->maxage_delay = OSPF_LSA_MAXAGE_REMOVE_DELAY_DEFAULT;
new->maxage_lsa = route_table_init();
new->t_maxage_walker = NULL;
thread_add_timer(master, ospf_lsa_maxage_walker, new,
OSPF_LSA_MAXAGE_CHECK_INTERVAL, &new->t_maxage_walker);
/* Max paths initialization */
new->max_multipath = MULTIPATH_NUM;
/* Distance table init. */
new->distance_table = route_table_init();
new->lsa_refresh_queue.index = 0;
new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
new->t_lsa_refresher = NULL;
thread_add_timer(master, ospf_lsa_refresh_walker, new,
new->lsa_refresh_interval, &new->t_lsa_refresher);
new->lsa_refresher_started = monotime(NULL);
new->ibuf = stream_new(OSPF_MAX_PACKET_SIZE + 1);
new->t_read = NULL;
new->oi_write_q = list_new();
new->write_oi_count = OSPF_WRITE_INTERFACE_COUNT_DEFAULT;
new->proactive_arp = OSPF_PROACTIVE_ARP_DEFAULT;
ospf_gr_helper_instance_init(new);
ospf_asbr_external_aggregator_init(new);
ospf_opaque_type11_lsa_init(new);
QOBJ_REG(new, ospf);
new->fd = -1;
return new;
}
/* Allocate new ospf structure. */
static struct ospf *ospf_new(unsigned short instance, const char *name)
{
struct ospf *new;
new = ospf_new_alloc(instance, name);
ospf_add(new);
if (new->vrf_id == VRF_UNKNOWN)
return new;
if ((ospf_sock_init(new)) < 0) {
flog_err(EC_LIB_SOCKET,
"%s: ospf_sock_init is unable to open a socket",
__func__);
return new;
}
thread_add_read(master, ospf_read, new, new->fd, &new->t_read);
new->oi_running = 1;
ospf_router_id_update(new);
/*
* Read from non-volatile memory whether this instance is performing a
* graceful restart or not.
*/
ospf_gr_nvm_read(new);
return new;
}
struct ospf *ospf_lookup_instance(unsigned short instance)
{
struct ospf *ospf;
struct listnode *node, *nnode;
if (listcount(om->ospf) == 0)
return NULL;
for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
if ((ospf->instance == 0 && instance == 0)
|| (ospf->instance && instance
&& ospf->instance == instance))
return ospf;
return NULL;
}
static int ospf_is_ready(struct ospf *ospf)
{
/* OSPF must be on and Router-ID must be configured. */
if (!ospf || ospf->router_id.s_addr == INADDR_ANY)
return 0;
return 1;
}
struct ospf *ospf_lookup_by_inst_name(unsigned short instance, const char *name)
{
struct ospf *ospf = NULL;
struct listnode *node, *nnode;
if (name == NULL || strmatch(name, VRF_DEFAULT_NAME))
return ospf_lookup_by_vrf_id(VRF_DEFAULT);
for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf)) {
if ((ospf->instance == instance)
&& ((ospf->name == NULL && name == NULL)
|| (ospf->name && name
&& strcmp(ospf->name, name) == 0)))
return ospf;
}
return NULL;
}
struct ospf *ospf_lookup(unsigned short instance, const char *name)
{
struct ospf *ospf;
if (ospf_instance) {
ospf = ospf_lookup_instance(instance);
} else {
ospf = ospf_lookup_by_inst_name(instance, name);
}
return ospf;
}
struct ospf *ospf_get(unsigned short instance, const char *name, bool *created)
{
struct ospf *ospf;
ospf = ospf_lookup(instance, name);
*created = (ospf == NULL);
if (ospf == NULL)
ospf = ospf_new(instance, name);
return ospf;
}
struct ospf *ospf_lookup_by_vrf_id(vrf_id_t vrf_id)
{
struct vrf *vrf = NULL;
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
return (vrf->info) ? (struct ospf *)vrf->info : NULL;
}
uint32_t ospf_count_area_params(struct ospf *ospf)
{
struct vrf *vrf;
struct interface *ifp;
uint32_t count = 0;
if (ospf->vrf_id != VRF_UNKNOWN) {
vrf = vrf_lookup_by_id(ospf->vrf_id);
FOR_ALL_INTERFACES (vrf, ifp) {
count += ospf_if_count_area_params(ifp);
}
}
return count;
}
/* It should only be used when processing incoming info update from zebra.
* Other situations, it is not sufficient to lookup the ospf instance by
* vrf_name only without using the instance number.
*/
static struct ospf *ospf_lookup_by_name(const char *vrf_name)
{
struct ospf *ospf = NULL;
struct listnode *node, *nnode;
for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
if ((ospf->name == NULL && vrf_name == NULL)
|| (ospf->name && vrf_name
&& strcmp(ospf->name, vrf_name) == 0))
return ospf;
return NULL;
}
/* Handle the second half of deferred shutdown. This is called either
* from the deferred-shutdown timer thread, or directly through
* ospf_deferred_shutdown_check.
*
* Function is to cleanup G-R state, if required then call ospf_finish_final
* to complete shutdown of this ospf instance. Possibly exit if the
* whole process is being shutdown and this was the last OSPF instance.
*/
static void ospf_deferred_shutdown_finish(struct ospf *ospf)
{
ospf->stub_router_shutdown_time = OSPF_STUB_ROUTER_UNCONFIGURED;
OSPF_TIMER_OFF(ospf->t_deferred_shutdown);
ospf_finish_final(ospf);
/* *ospf is now invalid */
/* ospfd being shut-down? If so, was this the last ospf instance? */
if (CHECK_FLAG(om->options, OSPF_MASTER_SHUTDOWN)
&& (listcount(om->ospf) == 0)) {
exit(0);
}
return;
}
/* Timer thread for G-R */
static int ospf_deferred_shutdown_timer(struct thread *t)
{
struct ospf *ospf = THREAD_ARG(t);
ospf_deferred_shutdown_finish(ospf);
return 0;
}
/* Check whether deferred-shutdown must be scheduled, otherwise call
* down directly into second-half of instance shutdown.
*/
static void ospf_deferred_shutdown_check(struct ospf *ospf)
{
unsigned long timeout;
struct listnode *ln;
struct ospf_area *area;
/* deferred shutdown already running? */
if (ospf->t_deferred_shutdown)
return;
/* Should we try push out max-metric LSAs? */
if (ospf->stub_router_shutdown_time != OSPF_STUB_ROUTER_UNCONFIGURED) {
for (ALL_LIST_ELEMENTS_RO(ospf->areas, ln, area)) {
SET_FLAG(area->stub_router_state,
OSPF_AREA_ADMIN_STUB_ROUTED);
if (!CHECK_FLAG(area->stub_router_state,
OSPF_AREA_IS_STUB_ROUTED))
ospf_router_lsa_update_area(area);
}
timeout = ospf->stub_router_shutdown_time;
} else {
/* No timer needed */
ospf_deferred_shutdown_finish(ospf);
return;
}
OSPF_TIMER_ON(ospf->t_deferred_shutdown, ospf_deferred_shutdown_timer,
timeout);
return;
}
/* Shut down the entire process */
void ospf_terminate(void)
{
struct ospf *ospf;
struct listnode *node, *nnode;
/* shutdown already in progress */
if (CHECK_FLAG(om->options, OSPF_MASTER_SHUTDOWN))
return;
SET_FLAG(om->options, OSPF_MASTER_SHUTDOWN);
/* Skip some steps if OSPF not actually running */
if (listcount(om->ospf) == 0)
goto done;
for (ALL_LIST_ELEMENTS(om->ospf, node, nnode, ospf))
ospf_finish(ospf);
/* Cleanup GR */
ospf_gr_helper_stop();
/* Cleanup route maps */
route_map_finish();
/* reverse prefix_list_init */
prefix_list_add_hook(NULL);
prefix_list_delete_hook(NULL);
prefix_list_reset();
/* Cleanup vrf info */
ospf_vrf_terminate();
/* Deliberately go back up, hopefully to thread scheduler, as
* One or more ospf_finish()'s may have deferred shutdown to a timer
* thread
*/
zclient_stop(zclient);
zclient_free(zclient);
done:
frr_fini();
}
void ospf_finish(struct ospf *ospf)
{
/* let deferred shutdown decide */
ospf_deferred_shutdown_check(ospf);
/* if ospf_deferred_shutdown returns, then ospf_finish_final is
* deferred to expiry of G-S timer thread. Return back up, hopefully
* to thread scheduler.
*/
return;
}
/* Final cleanup of ospf instance */
static void ospf_finish_final(struct ospf *ospf)
{
struct vrf *vrf = vrf_lookup_by_id(ospf->vrf_id);
struct route_node *rn;
struct ospf_nbr_nbma *nbr_nbma;
struct ospf_lsa *lsa;
struct ospf_interface *oi;
struct ospf_area *area;
struct ospf_vl_data *vl_data;
struct listnode *node, *nnode;
struct ospf_redist *red;
int i;
QOBJ_UNREG(ospf);
ospf_opaque_type11_lsa_term(ospf);
ospf_opaque_finish();
if (!ospf->gr_info.prepare_in_progress)
ospf_flush_self_originated_lsas_now(ospf);
/* Unregister redistribution */
for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
struct list *red_list;
red_list = ospf->redist[i];
if (!red_list)
continue;
for (ALL_LIST_ELEMENTS(red_list, node, nnode, red)) {
ospf_redistribute_unset(ospf, i, red->instance);
ospf_redist_del(ospf, i, red->instance);
}
}
red = ospf_redist_lookup(ospf, DEFAULT_ROUTE, 0);
if (red) {
ospf_routemap_unset(red);
ospf_redist_del(ospf, DEFAULT_ROUTE, 0);
ospf_redistribute_default_set(ospf, DEFAULT_ORIGINATE_NONE, 0, 0);
}
for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area))
ospf_remove_vls_through_area(ospf, area);
for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
ospf_vl_delete(ospf, vl_data);
list_delete(&ospf->vlinks);
/* shutdown LDP-Sync */
if (ospf->vrf_id == VRF_DEFAULT)
ospf_ldp_sync_gbl_exit(ospf, true);
/* Reset interface. */
for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi))
ospf_if_free(oi);
list_delete(&ospf->oiflist);
ospf->oi_running = 0;
/* De-Register VRF */
ospf_zebra_vrf_deregister(ospf);
/* Clear static neighbors */
for (rn = route_top(ospf->nbr_nbma); rn; rn = route_next(rn))
if ((nbr_nbma = rn->info)) {
OSPF_POLL_TIMER_OFF(nbr_nbma->t_poll);
if (nbr_nbma->nbr) {
nbr_nbma->nbr->nbr_nbma = NULL;
nbr_nbma->nbr = NULL;
}
if (nbr_nbma->oi) {
listnode_delete(nbr_nbma->oi->nbr_nbma,
nbr_nbma);
nbr_nbma->oi = NULL;
}
XFREE(MTYPE_OSPF_NEIGHBOR_STATIC, nbr_nbma);
}
route_table_finish(ospf->nbr_nbma);
/* Clear networks and Areas. */
for (rn = route_top(ospf->networks); rn; rn = route_next(rn)) {
struct ospf_network *network;
if ((network = rn->info) != NULL) {
ospf_network_free(ospf, network);
rn->info = NULL;
route_unlock_node(rn);
}
}
route_table_finish(ospf->networks);
for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
listnode_delete(ospf->areas, area);
ospf_area_free(area);
}
/* Cancel all timers. */
OSPF_TIMER_OFF(ospf->t_read);
OSPF_TIMER_OFF(ospf->t_write);
OSPF_TIMER_OFF(ospf->t_spf_calc);
OSPF_TIMER_OFF(ospf->t_ase_calc);
OSPF_TIMER_OFF(ospf->t_maxage);
OSPF_TIMER_OFF(ospf->t_maxage_walker);
OSPF_TIMER_OFF(ospf->t_abr_task);
OSPF_TIMER_OFF(ospf->t_asbr_check);
OSPF_TIMER_OFF(ospf->t_asbr_nssa_redist_update);
OSPF_TIMER_OFF(ospf->t_distribute_update);
OSPF_TIMER_OFF(ospf->t_lsa_refresher);
OSPF_TIMER_OFF(ospf->t_opaque_lsa_self);
OSPF_TIMER_OFF(ospf->t_sr_update);
OSPF_TIMER_OFF(ospf->t_default_routemap_timer);
OSPF_TIMER_OFF(ospf->t_external_aggr);
OSPF_TIMER_OFF(ospf->gr_info.t_grace_period);
LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
ospf_discard_from_db(ospf, ospf->lsdb, lsa);
LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
ospf_discard_from_db(ospf, ospf->lsdb, lsa);
ospf_lsdb_delete_all(ospf->lsdb);
ospf_lsdb_free(ospf->lsdb);
for (rn = route_top(ospf->maxage_lsa); rn; rn = route_next(rn)) {
if ((lsa = rn->info) != NULL) {
ospf_lsa_unlock(&lsa);
rn->info = NULL;
route_unlock_node(rn);
}
}
route_table_finish(ospf->maxage_lsa);
if (ospf->old_table)
ospf_route_table_free(ospf->old_table);
if (ospf->new_table) {
if (!ospf->gr_info.prepare_in_progress)
ospf_route_delete(ospf, ospf->new_table);
ospf_route_table_free(ospf->new_table);
}
if (ospf->old_rtrs)
ospf_rtrs_free(ospf->old_rtrs);
if (ospf->new_rtrs)
ospf_rtrs_free(ospf->new_rtrs);
if (ospf->new_external_route) {
if (!ospf->gr_info.prepare_in_progress)
ospf_route_delete(ospf, ospf->new_external_route);
ospf_route_table_free(ospf->new_external_route);
}
if (ospf->old_external_route) {
if (!ospf->gr_info.prepare_in_progress)
ospf_route_delete(ospf, ospf->old_external_route);
ospf_route_table_free(ospf->old_external_route);
}
if (ospf->external_lsas) {
ospf_ase_external_lsas_finish(ospf->external_lsas);
}
for (i = ZEBRA_ROUTE_SYSTEM; i <= ZEBRA_ROUTE_MAX; i++) {
struct list *ext_list;
struct ospf_external *ext;
ext_list = ospf->external[i];
if (!ext_list)
continue;
for (ALL_LIST_ELEMENTS(ext_list, node, nnode, ext)) {
if (ext->external_info)
for (rn = route_top(ext->external_info); rn;
rn = route_next(rn)) {
if (rn->info == NULL)
continue;
XFREE(MTYPE_OSPF_EXTERNAL_INFO,
rn->info);
rn->info = NULL;
route_unlock_node(rn);
}
ospf_external_del(ospf, i, ext->instance);
}
}
ospf_distance_reset(ospf);
route_table_finish(ospf->distance_table);
/* Release extrenal Aggregator table */
for (rn = route_top(ospf->rt_aggr_tbl); rn; rn = route_next(rn)) {
struct ospf_external_aggr_rt *aggr;
aggr = rn->info;
if (aggr) {
ospf_external_aggregator_free(aggr);
rn->info = NULL;
route_unlock_node(rn);
}
}
route_table_finish(ospf->rt_aggr_tbl);
list_delete(&ospf->areas);
list_delete(&ospf->oi_write_q);
/* Reset GR helper data structers */
ospf_gr_helper_instance_stop(ospf);
close(ospf->fd);
stream_free(ospf->ibuf);
ospf->fd = -1;
ospf->max_multipath = MULTIPATH_NUM;
ospf_delete(ospf);
if (vrf)
ospf_vrf_unlink(ospf, vrf);
if (ospf->name)
XFREE(MTYPE_OSPF_TOP, ospf->name);
XFREE(MTYPE_OSPF_TOP, ospf);
}
/* allocate new OSPF Area object */
struct ospf_area *ospf_area_new(struct ospf *ospf, struct in_addr area_id)
{
struct ospf_area *new;
/* Allocate new config_network. */
new = XCALLOC(MTYPE_OSPF_AREA, sizeof(struct ospf_area));
new->ospf = ospf;
new->area_id = area_id;
new->area_id_fmt = OSPF_AREA_ID_FMT_DOTTEDQUAD;
new->external_routing = OSPF_AREA_DEFAULT;
new->default_cost = 1;
new->auth_type = OSPF_AUTH_NULL;
/* New LSDB init. */
new->lsdb = ospf_lsdb_new();
/* Self-originated LSAs initialize. */
new->router_lsa_self = NULL;
ospf_opaque_type10_lsa_init(new);
new->oiflist = list_new();
new->ranges = route_table_init();
if (area_id.s_addr == OSPF_AREA_BACKBONE)
ospf->backbone = new;
return new;
}
void ospf_area_lsdb_discard_delete(struct ospf_area *area)
{
struct route_node *rn;
struct ospf_lsa *lsa;
LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
ospf_discard_from_db(area->ospf, area->lsdb, lsa);
ospf_lsdb_delete_all(area->lsdb);
}
static void ospf_area_free(struct ospf_area *area)
{
ospf_opaque_type10_lsa_term(area);
/* Free LSDBs. */
ospf_area_lsdb_discard_delete(area);
ospf_lsdb_free(area->lsdb);
ospf_lsa_unlock(&area->router_lsa_self);
route_table_finish(area->ranges);
list_delete(&area->oiflist);
if (EXPORT_NAME(area))
free(EXPORT_NAME(area));
if (IMPORT_NAME(area))
free(IMPORT_NAME(area));
/* Cancel timer. */
OSPF_TIMER_OFF(area->t_stub_router);
OSPF_TIMER_OFF(area->t_opaque_lsa_self);