/
ospf_zebra.c
1606 lines (1310 loc) · 40.7 KB
/
ospf_zebra.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
/*
* Zebra connect library for OSPFd
* Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, 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 "command.h"
#include "network.h"
#include "prefix.h"
#include "routemap.h"
#include "table.h"
#include "stream.h"
#include "memory.h"
#include "zclient.h"
#include "filter.h"
#include "plist.h"
#include "log.h"
#include "lib/bfd.h"
#include "nexthop.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_abr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_dump.h"
#include "ospfd/ospf_route.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_zebra.h"
#include "ospfd/ospf_te.h"
DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table")
DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute")
DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments")
DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp))
DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp))
/* Zebra structure to hold current status. */
struct zclient *zclient = NULL;
/* For registering threads. */
extern struct thread_master *master;
/* Router-id update message from zebra. */
static int ospf_router_id_update_zebra(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct ospf *ospf = NULL;
struct prefix router_id;
zebra_router_id_update_read(zclient->ibuf, &router_id);
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(&router_id, buf, sizeof(buf));
zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf,
ospf_vrf_id_to_name(vrf_id), vrf_id);
}
ospf = ospf_lookup_by_vrf_id(vrf_id);
if (ospf != NULL) {
ospf->router_id_zebra = router_id.u.prefix4;
ospf_router_id_update(ospf);
} else {
if (IS_DEBUG_OSPF_EVENT) {
char buf[PREFIX2STR_BUFFER];
prefix2str(&router_id, buf, sizeof(buf));
zlog_debug(
"%s: ospf instance not found for vrf %s id %u router_id %s",
__PRETTY_FUNCTION__,
ospf_vrf_id_to_name(vrf_id), vrf_id, buf);
}
}
return 0;
}
/* Inteface addition message from zebra. */
static int ospf_interface_add(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp = NULL;
struct ospf *ospf = NULL;
ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
if (ifp == NULL)
return 0;
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
ifp->vrf_id, ifp->ifindex,
(unsigned long long)ifp->flags, ifp->metric, ifp->mtu,
ifp->speed);
assert(ifp->info);
if (IF_DEF_PARAMS(ifp)
&& !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
}
ospf = ospf_lookup_by_vrf_id(vrf_id);
if (!ospf)
return 0;
ospf_if_recalculate_output_cost(ifp);
ospf_if_update(ospf, ifp);
hook_call(ospf_if_update, ifp);
return 0;
}
static int ospf_interface_delete(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
struct stream *s;
struct route_node *rn;
s = zclient->ibuf;
/* zebra_interface_state_read() updates interface structure in iflist */
ifp = zebra_interface_state_read(s, vrf_id);
if (ifp == NULL)
return 0;
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
ifp->vrf_id, ifp->ifindex,
(unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
hook_call(ospf_if_delete, ifp);
for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
if (rn->info)
ospf_if_free((struct ospf_interface *)rn->info);
if_set_index(ifp, IFINDEX_INTERNAL);
return 0;
}
static struct interface *zebra_interface_if_lookup(struct stream *s,
vrf_id_t vrf_id)
{
char ifname_tmp[INTERFACE_NAMSIZ];
/* Read interface name. */
stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
/* And look it up. */
return if_lookup_by_name(ifname_tmp, vrf_id);
}
static int ospf_interface_state_up(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
struct ospf_interface *oi;
struct route_node *rn;
ifp = zebra_interface_if_lookup(zclient->ibuf, vrf_id);
if (ifp == NULL)
return 0;
/* Interface is already up. */
if (if_is_operative(ifp)) {
/* Temporarily keep ifp values. */
struct interface if_tmp;
memcpy(&if_tmp, ifp, sizeof(struct interface));
zebra_interface_if_set_value(zclient->ibuf, ifp);
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: Interface[%s] state update speed %u -> %u, bw %d -> %d",
ifp->name, if_tmp.speed, ifp->speed,
if_tmp.bandwidth, ifp->bandwidth);
ospf_if_recalculate_output_cost(ifp);
if (if_tmp.mtu != ifp->mtu) {
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: Interface[%s] MTU change %u -> %u.",
ifp->name, if_tmp.mtu, ifp->mtu);
/* Must reset the interface (simulate down/up) when MTU
* changes. */
ospf_if_reset(ifp);
}
return 0;
}
zebra_interface_if_set_value(zclient->ibuf, ifp);
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug("Zebra: Interface[%s] state change to up.",
ifp->name);
for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
if ((oi = rn->info) == NULL)
continue;
ospf_if_up(oi);
}
return 0;
}
static int ospf_interface_state_down(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
struct ospf_interface *oi;
struct route_node *node;
ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
if (ifp == NULL)
return 0;
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug("Zebra: Interface[%s] state change to down.",
ifp->name);
for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
if ((oi = node->info) == NULL)
continue;
ospf_if_down(oi);
}
return 0;
}
static int ospf_interface_address_add(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *c;
struct ospf *ospf = NULL;
c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
if (c == NULL)
return 0;
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(c->address, buf, sizeof(buf));
zlog_debug("Zebra: interface %s address add %s vrf %s id %u",
c->ifp->name, buf, ospf_vrf_id_to_name(vrf_id),
vrf_id);
}
ospf = ospf_lookup_by_vrf_id(vrf_id);
if (!ospf)
return 0;
ospf_if_update(ospf, c->ifp);
hook_call(ospf_if_update, c->ifp);
return 0;
}
static int ospf_interface_address_delete(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *c;
struct interface *ifp;
struct ospf_interface *oi;
struct route_node *rn;
struct prefix p;
c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
if (c == NULL)
return 0;
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(c->address, buf, sizeof(buf));
zlog_debug("Zebra: interface %s address delete %s",
c->ifp->name, buf);
}
ifp = c->ifp;
p = *c->address;
p.prefixlen = IPV4_MAX_PREFIXLEN;
rn = route_node_lookup(IF_OIFS(ifp), &p);
if (!rn) {
connected_free(c);
return 0;
}
assert(rn->info);
oi = rn->info;
route_unlock_node(rn);
/* Call interface hook functions to clean up */
ospf_if_free(oi);
hook_call(ospf_if_update, c->ifp);
connected_free(c);
return 0;
}
static int ospf_interface_link_params(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id);
if (ifp == NULL)
return 0;
/* Update TE TLV */
ospf_mpls_te_update_if(ifp);
return 0;
}
/* VRF update for an interface. */
static int ospf_interface_vrf_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp = NULL;
vrf_id_t new_vrf_id;
ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
&new_vrf_id);
if (!ifp)
return 0;
if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
__PRETTY_FUNCTION__, ifp->name, vrf_id,
ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
/*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
if_update_to_new_vrf(ifp, new_vrf_id);
return 0;
}
void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
struct ospf_route * or)
{
struct zapi_route api;
struct zapi_nexthop *api_nh;
uint8_t distance;
struct ospf_path *path;
struct listnode *node;
int count = 0;
memset(&api, 0, sizeof(api));
api.vrf_id = ospf->vrf_id;
api.type = ZEBRA_ROUTE_OSPF;
api.instance = ospf->instance;
api.safi = SAFI_UNICAST;
memcpy(&api.prefix, p, sizeof(*p));
SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
/* Metric value. */
SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
api.metric = or->cost + or->u.ext.type2_cost;
else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
api.metric = or->u.ext.type2_cost;
else
api.metric = or->cost;
/* Check if path type is ASE */
if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
|| (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
&& (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
api.tag = or->u.ext.tag;
}
/* Distance value. */
distance = ospf_distance_apply(ospf, p, or);
if (distance) {
SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
api.distance = distance;
}
/* Nexthop, ifindex, distance and metric information. */
for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
if (count >= MULTIPATH_NUM)
break;
api_nh = &api.nexthops[count];
#ifdef HAVE_NETLINK
if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY
&& path->ifindex != 0)) {
#else /* HAVE_NETLINK */
if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
#endif /* HAVE_NETLINK */
api_nh->gate.ipv4 = path->nexthop;
api_nh->ifindex = path->ifindex;
api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
} else if (path->nexthop.s_addr != INADDR_ANY) {
api_nh->gate.ipv4 = path->nexthop;
api_nh->type = NEXTHOP_TYPE_IPV4;
} else {
api_nh->ifindex = path->ifindex;
api_nh->type = NEXTHOP_TYPE_IFINDEX;
}
api_nh->vrf_id = ospf->vrf_id;
count++;
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[2][INET_ADDRSTRLEN];
struct interface *ifp;
ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
zlog_debug(
"Zebra: Route add %s nexthop %s, ifindex=%d %s",
prefix2str(p, buf[0], sizeof(buf[0])),
inet_ntop(AF_INET, &path->nexthop,
buf[1], sizeof(buf[1])),
path->ifindex, ifp ? ifp->name : " ");
}
}
api.nexthop_num = count;
zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
}
void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
struct ospf_route * or)
{
struct zapi_route api;
memset(&api, 0, sizeof(api));
api.vrf_id = ospf->vrf_id;
api.type = ZEBRA_ROUTE_OSPF;
api.instance = ospf->instance;
api.safi = SAFI_UNICAST;
memcpy(&api.prefix, p, sizeof(*p));
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[PREFIX2STR_BUFFER];
zlog_debug("Zebra: Route delete %s",
prefix2str(p, buf, sizeof(buf)));
}
zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
}
void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
{
struct zapi_route api;
memset(&api, 0, sizeof(api));
api.vrf_id = ospf->vrf_id;
api.type = ZEBRA_ROUTE_OSPF;
api.instance = ospf->instance;
api.safi = SAFI_UNICAST;
memcpy(&api.prefix, p, sizeof(*p));
zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[PREFIX2STR_BUFFER];
zlog_debug("Zebra: Route add discard %s",
prefix2str(p, buf, sizeof(buf)));
}
}
void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
{
struct zapi_route api;
memset(&api, 0, sizeof(api));
api.vrf_id = ospf->vrf_id;
api.type = ZEBRA_ROUTE_OSPF;
api.instance = ospf->instance;
api.safi = SAFI_UNICAST;
memcpy(&api.prefix, p, sizeof(*p));
zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[PREFIX2STR_BUFFER];
zlog_debug("Zebra: Route delete discard %s",
prefix2str(p, buf, sizeof(buf)));
}
}
struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
unsigned short instance)
{
struct list *ext_list;
struct listnode *node;
struct ospf_external *ext;
ext_list = ospf->external[type];
if (!ext_list)
return (NULL);
for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
if (ext->instance == instance)
return ext;
return NULL;
}
struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
unsigned short instance)
{
struct list *ext_list;
struct ospf_external *ext;
ext = ospf_external_lookup(ospf, type, instance);
if (ext)
return ext;
if (!ospf->external[type])
ospf->external[type] = list_new();
ext_list = ospf->external[type];
ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
ext->instance = instance;
EXTERNAL_INFO(ext) = route_table_init();
listnode_add(ext_list, ext);
return ext;
}
void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
{
struct ospf_external *ext;
ext = ospf_external_lookup(ospf, type, instance);
if (ext) {
if (EXTERNAL_INFO(ext))
route_table_finish(EXTERNAL_INFO(ext));
listnode_delete(ospf->external[type], ext);
if (!ospf->external[type]->count)
list_delete(&ospf->external[type]);
XFREE(MTYPE_OSPF_EXTERNAL, ext);
}
}
struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
unsigned short instance)
{
struct list *red_list;
struct listnode *node;
struct ospf_redist *red;
red_list = ospf->redist[type];
if (!red_list)
return (NULL);
for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
if (red->instance == instance)
return red;
return NULL;
}
struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
unsigned short instance)
{
struct list *red_list;
struct ospf_redist *red;
red = ospf_redist_lookup(ospf, type, instance);
if (red)
return red;
if (!ospf->redist[type])
ospf->redist[type] = list_new();
red_list = ospf->redist[type];
red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
red->instance = instance;
red->dmetric.type = -1;
red->dmetric.value = -1;
ROUTEMAP_NAME(red) = NULL;
ROUTEMAP(red) = NULL;
listnode_add(red_list, red);
return red;
}
void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
{
struct ospf_redist *red;
red = ospf_redist_lookup(ospf, type, instance);
if (red) {
listnode_delete(ospf->redist[type], red);
if (!ospf->redist[type]->count) {
list_delete(&ospf->redist[type]);
}
ospf_routemap_unset(red);
XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
}
}
int ospf_is_type_redistributed(struct ospf *ospf, int type,
unsigned short instance)
{
return (DEFAULT_ROUTE_TYPE(type)
? vrf_bitmap_check(zclient->default_information[AFI_IP],
ospf->vrf_id)
: ((instance
&& redist_check_instance(
&zclient->mi_redist[AFI_IP][type],
instance))
|| (!instance
&& vrf_bitmap_check(
zclient->redist[AFI_IP][type],
ospf->vrf_id))));
}
int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance,
int mtype, int mvalue)
{
int force = 0;
struct ospf_redist *red;
red = ospf_redist_lookup(ospf, type, instance);
if (red == NULL) {
zlog_err(
"Redistribute[%s][%d]: Lookup failed Type[%d] , Metric[%d]",
ospf_redist_string(type), instance,
metric_type(ospf, type, instance),
metric_value(ospf, type, instance));
return CMD_WARNING_CONFIG_FAILED;
}
if (ospf_is_type_redistributed(ospf, type, instance)) {
if (mtype != red->dmetric.type) {
red->dmetric.type = mtype;
force = LSA_REFRESH_FORCE;
}
if (mvalue != red->dmetric.value) {
red->dmetric.value = mvalue;
force = LSA_REFRESH_FORCE;
}
ospf_external_lsa_refresh_type(ospf, type, instance, force);
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug(
"Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
ospf_redist_string(type), instance,
metric_type(ospf, type, instance),
metric_value(ospf, type, instance));
return CMD_SUCCESS;
}
red->dmetric.type = mtype;
red->dmetric.value = mvalue;
ospf_external_add(ospf, type, instance);
zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
instance, ospf->vrf_id);
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug(
"Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
ospf_redist_string(type), instance, ospf->vrf_id,
metric_type(ospf, type, instance),
metric_value(ospf, type, instance));
ospf_asbr_status_update(ospf, ++ospf->redistribute);
return CMD_SUCCESS;
}
int ospf_redistribute_unset(struct ospf *ospf, int type,
unsigned short instance)
{
if (type == zclient->redist_default && instance == zclient->instance)
return CMD_SUCCESS;
if (!ospf_is_type_redistributed(ospf, type, instance))
return CMD_SUCCESS;
zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
instance, ospf->vrf_id);
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
ospf_redist_string(type), instance, ospf->vrf_id);
/* Remove the routes from OSPF table. */
ospf_redistribute_withdraw(ospf, type, instance);
ospf_external_del(ospf, type, instance);
ospf_asbr_status_update(ospf, --ospf->redistribute);
return CMD_SUCCESS;
}
int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
int mvalue)
{
struct ospf_external *ext;
struct prefix_ipv4 p;
struct in_addr nexthop;
int cur_originate = ospf->default_originate;
nexthop.s_addr = 0;
p.family = AF_INET;
p.prefix.s_addr = 0;
p.prefixlen = 0;
ospf->default_originate = originate;
ospf_external_add(ospf, DEFAULT_ROUTE, 0);
if (cur_originate == DEFAULT_ORIGINATE_NONE) {
/* First time configuration */
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
metric_type(ospf, DEFAULT_ROUTE, 0),
metric_value(ospf, DEFAULT_ROUTE, 0));
if (ospf->router_id.s_addr == 0)
ospf->external_origin |= (1 << DEFAULT_ROUTE);
if ((originate == DEFAULT_ORIGINATE_ALWAYS)
&& (ospf->router_id.s_addr)) {
/* always , so originate lsa even it doesn't
* exist in RIB.
*/
ospf_external_info_add(ospf, DEFAULT_ROUTE, 0,
p, 0, nexthop, 0);
ospf_external_lsa_refresh_default(ospf);
} else if (originate == DEFAULT_ORIGINATE_ZEBRA) {
/* Send msg to Zebra to validate default route
* existance.
*/
zclient_redistribute_default(
ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient, AFI_IP,
ospf->vrf_id);
}
ospf_asbr_status_update(ospf, ++ospf->redistribute);
return CMD_SUCCESS;
} else if (originate == cur_originate) {
/* Refresh the lsa since metric might different */
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug(
"Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ospf_redist_string(DEFAULT_ROUTE),
metric_type(ospf, DEFAULT_ROUTE, 0),
metric_value(ospf, DEFAULT_ROUTE, 0));
ospf_external_lsa_refresh_default(ospf);
} else {
/* "default-info originate always" configured now,
* where "default-info originate" configured previoulsly.
*/
if (originate == DEFAULT_ORIGINATE_ALWAYS) {
zclient_redistribute_default(
ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
zclient, AFI_IP, ospf->vrf_id);
/* here , ex-info should be added since ex-info might
* have not updated earlier if def route is not exist.
* If ex-iinfo ex-info already exist , it will return
* smoothly.
*/
ospf_external_info_add(ospf, DEFAULT_ROUTE, 0,
p, 0, nexthop, 0);
ospf_external_lsa_refresh_default(ospf);
} else {
/* "default-info originate" configured now,where
* "default-info originate always" configured
* previoulsy.
*/
ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &p, 0);
ext = ospf_external_lookup(ospf, DEFAULT_ROUTE, 0);
if (ext && EXTERNAL_INFO(ext))
ospf_external_info_delete(ospf,
DEFAULT_ROUTE, 0, p);
zclient_redistribute_default(
ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
zclient, AFI_IP, ospf->vrf_id);
}
}
return CMD_SUCCESS;
}
int ospf_redistribute_default_unset(struct ospf *ospf)
{
if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA) {
if (!ospf_is_type_redistributed(ospf, DEFAULT_ROUTE, 0))
return CMD_SUCCESS;
zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
zclient, AFI_IP, ospf->vrf_id);
}
ospf->default_originate = DEFAULT_ORIGINATE_NONE;
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
zlog_debug("Redistribute[DEFAULT]: Stop");
// Pending: how does the external_info cleanup work in this case?
ospf_asbr_status_update(ospf, --ospf->redistribute);
/* clean up maxage default originate external lsa */
ospf_default_originate_lsa_update(ospf);
return CMD_SUCCESS;
}
static int ospf_external_lsa_originate_check(struct ospf *ospf,
struct external_info *ei)
{
/* If prefix is multicast, then do not originate LSA. */
if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
zlog_info(
"LSA[Type5:%s]: Not originate AS-external-LSA, "
"Prefix belongs multicast",
inet_ntoa(ei->p.prefix));
return 0;
}
/* Take care of default-originate. */
if (is_prefix_default(&ei->p))
if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
zlog_info(
"LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
"for default");
return 0;
}
return 1;
}
/* If connected prefix is OSPF enable interface, then do not announce. */
int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
{
struct listnode *node;
struct ospf_interface *oi;
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
if (prefix_match(oi->address, (struct prefix *)&ei->p))
return 0;
return 1;
}
/* return 1 if external LSA must be originated, 0 otherwise */
int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
int *changed)
{
struct route_map_set_values save_values;
struct prefix_ipv4 *p = &ei->p;
struct ospf_redist *red;
uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
if (changed)
*changed = 0;
if (!ospf_external_lsa_originate_check(ospf, ei))
return 0;
/* Take care connected route. */
if (type == ZEBRA_ROUTE_CONNECT
&& !ospf_distribute_check_connected(ospf, ei))
return 0;
if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
/* distirbute-list exists, but access-list may not? */
if (DISTRIBUTE_LIST(ospf, type))
if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
== FILTER_DENY) {
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[PREFIX2STR_BUFFER];
zlog_debug(
"Redistribute[%s]: %s filtered by distribute-list.",
ospf_redist_string(type),
prefix2str(p, buf, sizeof(buf)));
}
return 0;
}
save_values = ei->route_map_set;
ospf_reset_route_map_set_values(&ei->route_map_set);
/* apply route-map if needed */
red = ospf_redist_lookup(ospf, type, instance);
if (red && ROUTEMAP_NAME(red)) {
int ret;
ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
RMAP_OSPF, ei);
if (ret == RMAP_DENYMATCH) {
ei->route_map_set = save_values;
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
char buf[PREFIX2STR_BUFFER];
zlog_debug(
"Redistribute[%s]: %s filtered by route-map.",
ospf_redist_string(type),
prefix2str(p, buf, sizeof(buf)));
}
return 0;
}
/* check if 'route-map set' changed something */
if (changed)
*changed = !ospf_route_map_set_compare(
&ei->route_map_set, &save_values);
}
return 1;
}
/* OSPF route-map set for redistribution */
void ospf_routemap_set(struct ospf_redist *red, const char *name)
{
if (ROUTEMAP_NAME(red)) {
route_map_counter_decrement(ROUTEMAP(red));
free(ROUTEMAP_NAME(red));
}
ROUTEMAP_NAME(red) = strdup(name);
ROUTEMAP(red) = route_map_lookup_by_name(name);
route_map_counter_increment(ROUTEMAP(red));
}
void ospf_routemap_unset(struct ospf_redist *red)
{
if (ROUTEMAP_NAME(red)) {
route_map_counter_decrement(ROUTEMAP(red));
free(ROUTEMAP_NAME(red));
}