]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_zebra.c
Merge pull request #5810 from donaldsharp/fix_yang_routemap
[mirror_frr.git] / ospfd / ospf_zebra.c
CommitLineData
718e3744 1/*
2 * Zebra connect library for OSPFd
3 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
24#include "thread.h"
25#include "command.h"
26#include "network.h"
27#include "prefix.h"
28#include "routemap.h"
29#include "table.h"
30#include "stream.h"
31#include "memory.h"
32#include "zclient.h"
33#include "filter.h"
dd669bb0 34#include "plist.h"
718e3744 35#include "log.h"
2376c3f2 36#include "lib/bfd.h"
5b30316e 37#include "nexthop.h"
718e3744 38
39#include "ospfd/ospfd.h"
40#include "ospfd/ospf_interface.h"
41#include "ospfd/ospf_ism.h"
42#include "ospfd/ospf_asbr.h"
43#include "ospfd/ospf_asbr.h"
44#include "ospfd/ospf_abr.h"
45#include "ospfd/ospf_lsa.h"
46#include "ospfd/ospf_dump.h"
47#include "ospfd/ospf_route.h"
d5a5c8f0
DS
48#include "ospfd/ospf_lsdb.h"
49#include "ospfd/ospf_neighbor.h"
50#include "ospfd/ospf_nsm.h"
718e3744 51#include "ospfd/ospf_zebra.h"
16f1b9ee 52#include "ospfd/ospf_te.h"
718e3744 53
dfac5d39
CS
54DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table")
55DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute")
b5a8894d 56DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments")
dfac5d39 57
718e3744 58/* Zebra structure to hold current status. */
59struct zclient *zclient = NULL;
60
61/* For registering threads. */
62extern struct thread_master *master;
18a6dce6 63
64/* Router-id update message from zebra. */
121f9dee 65static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
18a6dce6 66{
b5a8894d 67 struct ospf *ospf = NULL;
d62a17ae 68 struct prefix router_id;
69 zebra_router_id_update_read(zclient->ibuf, &router_id);
70
71 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
72 char buf[PREFIX2STR_BUFFER];
73 prefix2str(&router_id, buf, sizeof(buf));
996c9314
LB
74 zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf,
75 ospf_vrf_id_to_name(vrf_id), vrf_id);
d62a17ae 76 }
77
b5a8894d 78 ospf = ospf_lookup_by_vrf_id(vrf_id);
d62a17ae 79
6021c6c0
CS
80 if (ospf != NULL) {
81 ospf->router_id_zebra = router_id.u.prefix4;
d62a17ae 82 ospf_router_id_update(ospf);
6021c6c0 83 } else {
b5a8894d
CS
84 if (IS_DEBUG_OSPF_EVENT) {
85 char buf[PREFIX2STR_BUFFER];
86
87 prefix2str(&router_id, buf, sizeof(buf));
996c9314
LB
88 zlog_debug(
89 "%s: ospf instance not found for vrf %s id %u router_id %s",
90 __PRETTY_FUNCTION__,
91 ospf_vrf_id_to_name(vrf_id), vrf_id, buf);
b5a8894d
CS
92 }
93 }
d62a17ae 94 return 0;
18a6dce6 95}
718e3744 96
121f9dee 97static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS)
718e3744 98{
d62a17ae 99 struct connected *c;
b5a8894d
CS
100 struct ospf *ospf = NULL;
101
718e3744 102
121f9dee 103 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
718e3744 104
d62a17ae 105 if (c == NULL)
106 return 0;
718e3744 107
d62a17ae 108 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
109 char buf[PREFIX2STR_BUFFER];
110 prefix2str(c->address, buf, sizeof(buf));
b5a8894d
CS
111 zlog_debug("Zebra: interface %s address add %s vrf %s id %u",
112 c->ifp->name, buf, ospf_vrf_id_to_name(vrf_id),
113 vrf_id);
d62a17ae 114 }
7f643ebf 115
b5a8894d 116 ospf = ospf_lookup_by_vrf_id(vrf_id);
43b8d1d8
CS
117 if (!ospf)
118 return 0;
b5a8894d
CS
119
120 ospf_if_update(ospf, c->ifp);
718e3744 121
ef7bd2a3 122 ospf_if_interface(c->ifp);
718e3744 123
d62a17ae 124 return 0;
718e3744 125}
126
121f9dee 127static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)
718e3744 128{
d62a17ae 129 struct connected *c;
130 struct interface *ifp;
131 struct ospf_interface *oi;
132 struct route_node *rn;
133 struct prefix p;
134
121f9dee 135 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
d62a17ae 136
137 if (c == NULL)
138 return 0;
139
140 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
141 char buf[PREFIX2STR_BUFFER];
142 prefix2str(c->address, buf, sizeof(buf));
143 zlog_debug("Zebra: interface %s address delete %s",
144 c->ifp->name, buf);
145 }
7f643ebf 146
d62a17ae 147 ifp = c->ifp;
148 p = *c->address;
149 p.prefixlen = IPV4_MAX_PREFIXLEN;
718e3744 150
d62a17ae 151 rn = route_node_lookup(IF_OIFS(ifp), &p);
152 if (!rn) {
721c0857 153 connected_free(&c);
d62a17ae 154 return 0;
155 }
718e3744 156
d62a17ae 157 assert(rn->info);
158 oi = rn->info;
159 route_unlock_node(rn);
cf795c5d 160
d62a17ae 161 /* Call interface hook functions to clean up */
162 ospf_if_free(oi);
cf795c5d 163
ef7bd2a3 164 ospf_if_interface(c->ifp);
718e3744 165
721c0857 166 connected_free(&c);
718e3744 167
d62a17ae 168 return 0;
718e3744 169}
72357f2b 170
121f9dee 171static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS)
16f1b9ee 172{
d62a17ae 173 struct interface *ifp;
16f1b9ee 174
edc12762 175 ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id);
16f1b9ee 176
d62a17ae 177 if (ifp == NULL)
178 return 0;
16f1b9ee 179
d62a17ae 180 /* Update TE TLV */
181 ospf_mpls_te_update_if(ifp);
16f1b9ee 182
d62a17ae 183 return 0;
16f1b9ee
OD
184}
185
b5a8894d 186/* VRF update for an interface. */
121f9dee 187static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)
b5a8894d
CS
188{
189 struct interface *ifp = NULL;
190 vrf_id_t new_vrf_id;
16f1b9ee 191
b5a8894d 192 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
996c9314 193 &new_vrf_id);
b5a8894d
CS
194 if (!ifp)
195 return 0;
196
197 if (IS_DEBUG_OSPF_EVENT)
996c9314
LB
198 zlog_debug(
199 "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
200 __PRETTY_FUNCTION__, ifp->name, vrf_id,
201 ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
b5a8894d
CS
202
203 /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
a36898e7 204 if_update_to_new_vrf(ifp, new_vrf_id);
b5a8894d 205
996c9314 206 return 0;
b5a8894d
CS
207}
208
209void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
996c9314 210 struct ospf_route * or)
718e3744 211{
5fef910e
RW
212 struct zapi_route api;
213 struct zapi_nexthop *api_nh;
d7c0a89a 214 uint8_t distance;
d62a17ae 215 struct ospf_path *path;
216 struct listnode *node;
5fef910e 217 int count = 0;
d62a17ae 218
5fef910e 219 memset(&api, 0, sizeof(api));
b5a8894d 220 api.vrf_id = ospf->vrf_id;
5fef910e
RW
221 api.type = ZEBRA_ROUTE_OSPF;
222 api.instance = ospf->instance;
223 api.safi = SAFI_UNICAST;
d00061ea 224
5fef910e
RW
225 memcpy(&api.prefix, p, sizeof(*p));
226 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
d00061ea 227
5fef910e
RW
228 /* Metric value. */
229 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
230 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
231 api.metric = or->cost + or->u.ext.type2_cost;
232 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
233 api.metric = or->u.ext.type2_cost;
234 else
235 api.metric = or->cost;
d00061ea
RW
236
237 /* Check if path type is ASE */
238 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
239 || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
5fef910e
RW
240 && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
241 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
242 api.tag = or->u.ext.tag;
243 }
d00061ea 244
5fef910e 245 /* Distance value. */
b5a8894d 246 distance = ospf_distance_apply(ospf, p, or);
5fef910e
RW
247 if (distance) {
248 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
249 api.distance = distance;
250 }
d00061ea
RW
251
252 /* Nexthop, ifindex, distance and metric information. */
253 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
a74e593b
RW
254 if (count >= MULTIPATH_NUM)
255 break;
5fef910e 256 api_nh = &api.nexthops[count];
525c1839 257#ifdef HAVE_NETLINK
d00061ea
RW
258 if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY
259 && path->ifindex != 0)) {
525c1839 260#else /* HAVE_NETLINK */
d00061ea 261 if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
5fef910e
RW
262#endif /* HAVE_NETLINK */
263 api_nh->gate.ipv4 = path->nexthop;
264 api_nh->ifindex = path->ifindex;
265 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
d00061ea 266 } else if (path->nexthop.s_addr != INADDR_ANY) {
5fef910e
RW
267 api_nh->gate.ipv4 = path->nexthop;
268 api_nh->type = NEXTHOP_TYPE_IPV4;
d00061ea 269 } else {
5fef910e
RW
270 api_nh->ifindex = path->ifindex;
271 api_nh->type = NEXTHOP_TYPE_IFINDEX;
d00061ea 272 }
4a7371e9 273 api_nh->vrf_id = ospf->vrf_id;
5fef910e 274 count++;
72357f2b 275
d00061ea 276 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
96065dc3
CS
277 char buf[2][INET_ADDRSTRLEN];
278 struct interface *ifp;
279
280 ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
4259ea81 281
d00061ea 282 zlog_debug(
96065dc3 283 "Zebra: Route add %s nexthop %s, ifindex=%d %s",
4259ea81
MS
284 prefix2str(p, buf[0], sizeof(buf[0])),
285 inet_ntop(AF_INET, &path->nexthop,
286 buf[1], sizeof(buf[1])),
96065dc3 287 path->ifindex, ifp ? ifp->name : " ");
d62a17ae 288 }
d00061ea 289 }
a74e593b 290 api.nexthop_num = count;
d62a17ae 291
5fef910e 292 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
718e3744 293}
294
b5a8894d 295void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
996c9314 296 struct ospf_route * or)
718e3744 297{
5fef910e 298 struct zapi_route api;
d62a17ae 299
5fef910e 300 memset(&api, 0, sizeof(api));
b5a8894d 301 api.vrf_id = ospf->vrf_id;
5fef910e
RW
302 api.type = ZEBRA_ROUTE_OSPF;
303 api.instance = ospf->instance;
304 api.safi = SAFI_UNICAST;
305 memcpy(&api.prefix, p, sizeof(*p));
d62a17ae 306
5fef910e 307 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
4259ea81
MS
308 char buf[PREFIX2STR_BUFFER];
309 zlog_debug("Zebra: Route delete %s",
310 prefix2str(p, buf, sizeof(buf)));
ba281d3d 311 }
d00061ea 312
5fef910e 313 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
718e3744 314}
315
b5a8894d 316void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
718e3744 317{
5fef910e 318 struct zapi_route api;
d62a17ae 319
5fef910e 320 memset(&api, 0, sizeof(api));
b5a8894d 321 api.vrf_id = ospf->vrf_id;
d00061ea
RW
322 api.type = ZEBRA_ROUTE_OSPF;
323 api.instance = ospf->instance;
d00061ea 324 api.safi = SAFI_UNICAST;
5fef910e 325 memcpy(&api.prefix, p, sizeof(*p));
09a484dd 326 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 327
5fef910e 328 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
d00061ea 329
4259ea81
MS
330 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
331 char buf[PREFIX2STR_BUFFER];
332 zlog_debug("Zebra: Route add discard %s",
333 prefix2str(p, buf, sizeof(buf)));
334 }
718e3744 335}
336
b5a8894d 337void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
718e3744 338{
5fef910e 339 struct zapi_route api;
d62a17ae 340
5fef910e 341 memset(&api, 0, sizeof(api));
b5a8894d 342 api.vrf_id = ospf->vrf_id;
d00061ea
RW
343 api.type = ZEBRA_ROUTE_OSPF;
344 api.instance = ospf->instance;
d00061ea 345 api.safi = SAFI_UNICAST;
5fef910e 346 memcpy(&api.prefix, p, sizeof(*p));
09a484dd 347 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d00061ea 348
5fef910e 349 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
d00061ea 350
4259ea81
MS
351 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
352 char buf[PREFIX2STR_BUFFER];
353 zlog_debug("Zebra: Route delete discard %s",
354 prefix2str(p, buf, sizeof(buf)));
355 }
718e3744 356}
357
d7c0a89a
QY
358struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
359 unsigned short instance)
7c8ff89e 360{
d62a17ae 361 struct list *ext_list;
362 struct listnode *node;
363 struct ospf_external *ext;
7c8ff89e 364
de1ac5fd 365 ext_list = ospf->external[type];
d62a17ae 366 if (!ext_list)
367 return (NULL);
7c8ff89e 368
d62a17ae 369 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
370 if (ext->instance == instance)
371 return ext;
7c8ff89e 372
d62a17ae 373 return NULL;
7c8ff89e
DS
374}
375
d7c0a89a
QY
376struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
377 unsigned short instance)
7c8ff89e 378{
d62a17ae 379 struct list *ext_list;
380 struct ospf_external *ext;
7c8ff89e 381
de1ac5fd 382 ext = ospf_external_lookup(ospf, type, instance);
d62a17ae 383 if (ext)
384 return ext;
7c8ff89e 385
de1ac5fd
CS
386 if (!ospf->external[type])
387 ospf->external[type] = list_new();
7c8ff89e 388
de1ac5fd 389 ext_list = ospf->external[type];
9f5dc319 390 ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
d62a17ae 391 ext->instance = instance;
392 EXTERNAL_INFO(ext) = route_table_init();
7c8ff89e 393
d62a17ae 394 listnode_add(ext_list, ext);
7c8ff89e 395
d62a17ae 396 return ext;
7c8ff89e
DS
397}
398
d7c0a89a 399void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
7c8ff89e 400{
d62a17ae 401 struct ospf_external *ext;
402
de1ac5fd 403 ext = ospf_external_lookup(ospf, type, instance);
d62a17ae 404
405 if (ext) {
406 if (EXTERNAL_INFO(ext))
407 route_table_finish(EXTERNAL_INFO(ext));
408
de1ac5fd
CS
409 listnode_delete(ospf->external[type], ext);
410
411 if (!ospf->external[type]->count)
6a154c88 412 list_delete(&ospf->external[type]);
de1ac5fd 413
7f586094 414 XFREE(MTYPE_OSPF_EXTERNAL, ext);
d62a17ae 415 }
7c8ff89e
DS
416}
417
d7c0a89a
QY
418struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
419 unsigned short instance)
7c8ff89e 420{
d62a17ae 421 struct list *red_list;
422 struct listnode *node;
423 struct ospf_redist *red;
7c8ff89e 424
d62a17ae 425 red_list = ospf->redist[type];
426 if (!red_list)
427 return (NULL);
7c8ff89e 428
d62a17ae 429 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
430 if (red->instance == instance)
431 return red;
7c8ff89e 432
d62a17ae 433 return NULL;
7c8ff89e
DS
434}
435
d7c0a89a
QY
436struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
437 unsigned short instance)
7c8ff89e 438{
d62a17ae 439 struct list *red_list;
440 struct ospf_redist *red;
7c8ff89e 441
d62a17ae 442 red = ospf_redist_lookup(ospf, type, instance);
443 if (red)
444 return red;
7c8ff89e 445
d62a17ae 446 if (!ospf->redist[type])
447 ospf->redist[type] = list_new();
7c8ff89e 448
d62a17ae 449 red_list = ospf->redist[type];
9f5dc319 450 red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
d62a17ae 451 red->instance = instance;
452 red->dmetric.type = -1;
453 red->dmetric.value = -1;
1fb93326 454 ROUTEMAP_NAME(red) = NULL;
455 ROUTEMAP(red) = NULL;
7c8ff89e 456
d62a17ae 457 listnode_add(red_list, red);
7c8ff89e 458
d62a17ae 459 return red;
7c8ff89e
DS
460}
461
d7c0a89a 462void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
7c8ff89e 463{
d62a17ae 464 struct ospf_redist *red;
465
466 red = ospf_redist_lookup(ospf, type, instance);
467
468 if (red) {
469 listnode_delete(ospf->redist[type], red);
470 if (!ospf->redist[type]->count) {
6a154c88 471 list_delete(&ospf->redist[type]);
d62a17ae 472 }
97a69b31 473 ospf_routemap_unset(red);
7f586094 474 XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
d62a17ae 475 }
7c8ff89e
DS
476}
477
478
d7c0a89a
QY
479int ospf_is_type_redistributed(struct ospf *ospf, int type,
480 unsigned short instance)
718e3744 481{
d62a17ae 482 return (DEFAULT_ROUTE_TYPE(type)
49db7a7b 483 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
b5a8894d 484 ospf->vrf_id)
d62a17ae 485 : ((instance
486 && redist_check_instance(
487 &zclient->mi_redist[AFI_IP][type],
488 instance))
489 || (!instance
490 && vrf_bitmap_check(
491 zclient->redist[AFI_IP][type],
b5a8894d 492 ospf->vrf_id))));
718e3744 493}
494
d7c0a89a 495int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance,
d62a17ae 496 int mtype, int mvalue)
718e3744 497{
d62a17ae 498 int force = 0;
499 struct ospf_redist *red;
500
501 red = ospf_redist_lookup(ospf, type, instance);
162dbe41 502
503 if (red == NULL) {
504 zlog_err(
505 "Redistribute[%s][%d]: Lookup failed Type[%d] , Metric[%d]",
506 ospf_redist_string(type), instance,
507 metric_type(ospf, type, instance),
508 metric_value(ospf, type, instance));
509 return CMD_WARNING_CONFIG_FAILED;
510 }
511
b5a8894d 512 if (ospf_is_type_redistributed(ospf, type, instance)) {
d62a17ae 513 if (mtype != red->dmetric.type) {
514 red->dmetric.type = mtype;
515 force = LSA_REFRESH_FORCE;
516 }
517 if (mvalue != red->dmetric.value) {
518 red->dmetric.value = mvalue;
519 force = LSA_REFRESH_FORCE;
520 }
521
522 ospf_external_lsa_refresh_type(ospf, type, instance, force);
523
524 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
525 zlog_debug(
526 "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]",
527 ospf_redist_string(type), instance,
528 metric_type(ospf, type, instance),
529 metric_value(ospf, type, instance));
530
531 return CMD_SUCCESS;
532 }
533
534 red->dmetric.type = mtype;
535 red->dmetric.value = mvalue;
536
de1ac5fd 537 ospf_external_add(ospf, type, instance);
d62a17ae 538
539 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
b5a8894d 540 instance, ospf->vrf_id);
d62a17ae 541
542 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
996c9314
LB
543 zlog_debug(
544 "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]",
545 ospf_redist_string(type), instance, ospf->vrf_id,
546 metric_type(ospf, type, instance),
547 metric_value(ospf, type, instance));
d62a17ae 548
549 ospf_asbr_status_update(ospf, ++ospf->redistribute);
550
551 return CMD_SUCCESS;
718e3744 552}
553
d7c0a89a
QY
554int ospf_redistribute_unset(struct ospf *ospf, int type,
555 unsigned short instance)
718e3744 556{
d62a17ae 557 if (type == zclient->redist_default && instance == zclient->instance)
558 return CMD_SUCCESS;
718e3744 559
b5a8894d 560 if (!ospf_is_type_redistributed(ospf, type, instance))
d62a17ae 561 return CMD_SUCCESS;
718e3744 562
d62a17ae 563 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
b5a8894d 564 instance, ospf->vrf_id);
cf795c5d 565
d62a17ae 566 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
b5a8894d
CS
567 zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
568 ospf_redist_string(type), instance, ospf->vrf_id);
718e3744 569
d62a17ae 570 /* Remove the routes from OSPF table. */
571 ospf_redistribute_withdraw(ospf, type, instance);
7c8ff89e 572
de1ac5fd 573 ospf_external_del(ospf, type, instance);
718e3744 574
d62a17ae 575 ospf_asbr_status_update(ospf, --ospf->redistribute);
718e3744 576
d62a17ae 577 return CMD_SUCCESS;
718e3744 578}
579
d62a17ae 580int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
581 int mvalue)
718e3744 582{
1fb93326 583 struct prefix_ipv4 p;
584 struct in_addr nexthop;
585 int cur_originate = ospf->default_originate;
d5eac1e0 586 const char *type_str = NULL;
1fb93326 587
975a328e 588 nexthop.s_addr = INADDR_ANY;
1fb93326 589 p.family = AF_INET;
975a328e 590 p.prefix.s_addr = INADDR_ANY;
1fb93326 591 p.prefixlen = 0;
592
d62a17ae 593 ospf->default_originate = originate;
020709f9 594
d5eac1e0 595 if (cur_originate == originate) {
1fb93326 596 /* Refresh the lsa since metric might different */
d62a17ae 597 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
598 zlog_debug(
599 "Redistribute[%s]: Refresh Type[%d], Metric[%d]",
600 ospf_redist_string(DEFAULT_ROUTE),
601 metric_type(ospf, DEFAULT_ROUTE, 0),
602 metric_value(ospf, DEFAULT_ROUTE, 0));
718e3744 603
1fb93326 604 ospf_external_lsa_refresh_default(ospf);
d5eac1e0 605 return CMD_SUCCESS;
1fb93326 606 }
718e3744 607
d5eac1e0
DL
608 switch (cur_originate) {
609 case DEFAULT_ORIGINATE_NONE:
610 break;
611 case DEFAULT_ORIGINATE_ZEBRA:
1fb93326 612 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
49db7a7b 613 zclient, AFI_IP, ospf->vrf_id);
d5eac1e0
DL
614 ospf->redistribute--;
615 break;
616 case DEFAULT_ORIGINATE_ALWAYS:
617 ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
618 ospf_external_del(ospf, DEFAULT_ROUTE, 0);
619 ospf->redistribute--;
620 break;
1fb93326 621 }
718e3744 622
d5eac1e0
DL
623 switch (originate) {
624 case DEFAULT_ORIGINATE_NONE:
625 type_str = "none";
626 break;
627 case DEFAULT_ORIGINATE_ZEBRA:
628 type_str = "normal";
629 ospf->redistribute++;
630 zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
631 zclient, AFI_IP, ospf->vrf_id);
632 break;
633 case DEFAULT_ORIGINATE_ALWAYS:
634 type_str = "always";
635 ospf->redistribute++;
636 ospf_external_add(ospf, DEFAULT_ROUTE, 0);
637 ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop,
638 0);
639 break;
640 }
718e3744 641
d62a17ae 642 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
d5eac1e0
DL
643 zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
644 type_str,
645 metric_type(ospf, DEFAULT_ROUTE, 0),
646 metric_value(ospf, DEFAULT_ROUTE, 0));
718e3744 647
d5eac1e0
DL
648 ospf_external_lsa_refresh_default(ospf);
649 ospf_asbr_status_update(ospf, ospf->redistribute);
d62a17ae 650 return CMD_SUCCESS;
718e3744 651}
652
d62a17ae 653static int ospf_external_lsa_originate_check(struct ospf *ospf,
654 struct external_info *ei)
718e3744 655{
d62a17ae 656 /* If prefix is multicast, then do not originate LSA. */
657 if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
658 zlog_info(
659 "LSA[Type5:%s]: Not originate AS-external-LSA, "
660 "Prefix belongs multicast",
661 inet_ntoa(ei->p.prefix));
662 return 0;
663 }
664
665 /* Take care of default-originate. */
666 if (is_prefix_default(&ei->p))
667 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
668 zlog_info(
669 "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
670 "for default");
671 return 0;
672 }
673
674 return 1;
718e3744 675}
676
677/* If connected prefix is OSPF enable interface, then do not announce. */
d62a17ae 678int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
718e3744 679{
d62a17ae 680 struct listnode *node;
681 struct ospf_interface *oi;
718e3744 682
718e3744 683
d62a17ae 684 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
685 if (prefix_match(oi->address, (struct prefix *)&ei->p))
686 return 0;
687 return 1;
718e3744 688}
689
690/* return 1 if external LSA must be originated, 0 otherwise */
d62a17ae 691int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
692 int *changed)
718e3744 693{
d62a17ae 694 struct route_map_set_values save_values;
695 struct prefix_ipv4 *p = &ei->p;
696 struct ospf_redist *red;
d7c0a89a
QY
697 uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
698 unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
d62a17ae 699
700 if (changed)
701 *changed = 0;
702
703 if (!ospf_external_lsa_originate_check(ospf, ei))
704 return 0;
705
706 /* Take care connected route. */
707 if (type == ZEBRA_ROUTE_CONNECT
708 && !ospf_distribute_check_connected(ospf, ei))
709 return 0;
710
711 if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
712 /* distirbute-list exists, but access-list may not? */
713 if (DISTRIBUTE_LIST(ospf, type))
714 if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
715 == FILTER_DENY) {
4259ea81
MS
716 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
717 char buf[PREFIX2STR_BUFFER];
d62a17ae 718 zlog_debug(
4259ea81 719 "Redistribute[%s]: %s filtered by distribute-list.",
d62a17ae 720 ospf_redist_string(type),
4259ea81
MS
721 prefix2str(p, buf, sizeof(buf)));
722 }
d62a17ae 723 return 0;
724 }
725
726 save_values = ei->route_map_set;
727 ospf_reset_route_map_set_values(&ei->route_map_set);
728
729 /* apply route-map if needed */
730 red = ospf_redist_lookup(ospf, type, instance);
731 if (red && ROUTEMAP_NAME(red)) {
b68885f9 732 route_map_result_t ret;
d62a17ae 733
734 ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
735 RMAP_OSPF, ei);
736
737 if (ret == RMAP_DENYMATCH) {
738 ei->route_map_set = save_values;
4259ea81
MS
739 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
740 char buf[PREFIX2STR_BUFFER];
d62a17ae 741 zlog_debug(
4259ea81 742 "Redistribute[%s]: %s filtered by route-map.",
d62a17ae 743 ospf_redist_string(type),
4259ea81
MS
744 prefix2str(p, buf, sizeof(buf)));
745 }
d62a17ae 746 return 0;
747 }
748
749 /* check if 'route-map set' changed something */
750 if (changed)
751 *changed = !ospf_route_map_set_compare(
752 &ei->route_map_set, &save_values);
753 }
754
755 return 1;
718e3744 756}
757
758/* OSPF route-map set for redistribution */
d62a17ae 759void ospf_routemap_set(struct ospf_redist *red, const char *name)
718e3744 760{
93d836e6 761 if (ROUTEMAP_NAME(red)) {
762 route_map_counter_decrement(ROUTEMAP(red));
d62a17ae 763 free(ROUTEMAP_NAME(red));
93d836e6 764 }
718e3744 765
d62a17ae 766 ROUTEMAP_NAME(red) = strdup(name);
767 ROUTEMAP(red) = route_map_lookup_by_name(name);
93d836e6 768 route_map_counter_increment(ROUTEMAP(red));
718e3744 769}
770
d62a17ae 771void ospf_routemap_unset(struct ospf_redist *red)
718e3744 772{
93d836e6 773 if (ROUTEMAP_NAME(red)) {
774 route_map_counter_decrement(ROUTEMAP(red));
d62a17ae 775 free(ROUTEMAP_NAME(red));
93d836e6 776 }
718e3744 777
d62a17ae 778 ROUTEMAP_NAME(red) = NULL;
779 ROUTEMAP(red) = NULL;
718e3744 780}
781
782/* Zebra route add and delete treatment. */
121f9dee 783static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
718e3744 784{
74489921
RW
785 struct zapi_route api;
786 struct prefix_ipv4 p;
d62a17ae 787 unsigned long ifindex;
788 struct in_addr nexthop;
d62a17ae 789 struct external_info *ei;
790 struct ospf *ospf;
791 int i;
1fb93326 792 uint8_t rt_type;
d62a17ae 793
b5a8894d 794 ospf = ospf_lookup_by_vrf_id(vrf_id);
74489921
RW
795 if (ospf == NULL)
796 return 0;
d62a17ae 797
74489921
RW
798 if (zapi_route_decode(zclient->ibuf, &api) < 0)
799 return -1;
d62a17ae 800
74489921
RW
801 ifindex = api.nexthops[0].ifindex;
802 nexthop = api.nexthops[0].gate.ipv4;
1fb93326 803 rt_type = api.type;
d62a17ae 804
74489921 805 memcpy(&p, &api.prefix, sizeof(p));
d62a17ae 806 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
807 return 0;
808
1fb93326 809 /* Re-destributed route is default route.
810 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
811 * updating ex-info. But in resetting (no default-info
812 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
813 * Resolved this inconsistency by maintaining same route type.
814 */
815 if (is_prefix_default(&p))
816 rt_type = DEFAULT_ROUTE;
817
5c780bc9
PZ
818 if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
819 char buf_prefix[PREFIX_STRLEN];
820 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
821
ae4080c5
MS
822 zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %s",
823 __func__, zserv_command_string(cmd),
5c780bc9
PZ
824 zebra_route_string(api.type), vrf_id, buf_prefix);
825 }
826
121f9dee 827 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
d62a17ae 828 /* XXX|HACK|TODO|FIXME:
09a484dd
DL
829 * Maybe we should ignore reject/blackhole routes? Testing
830 * shows that there is no problems though and this is only way
831 * to "summarize" routes in ASBR at the moment. Maybe we need
832 * just a better generalised solution for these types?
d62a17ae 833 */
834
74489921 835 /* Protocol tag overwrites all other tag value sent by zebra */
1fb93326 836 if (ospf->dtag[rt_type] > 0)
837 api.tag = ospf->dtag[rt_type];
d62a17ae 838
839 /*
840 * Given zebra sends update for a prefix via ADD message, it
841 * should
842 * be considered as an implicit DEL for that prefix with other
843 * source
844 * types.
845 */
1fb93326 846 for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
847 if (i != rt_type)
996c9314
LB
848 ospf_external_info_delete(ospf, i, api.instance,
849 p);
d62a17ae 850
1fb93326 851 ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
de1ac5fd 852 ifindex, nexthop, api.tag);
d62a17ae 853 if (ei == NULL) {
854 /* Nothing has changed, so nothing to do; return */
855 return 0;
856 }
975a328e 857 if (ospf->router_id.s_addr != INADDR_ANY) {
d62a17ae 858 if (ei) {
859 if (is_prefix_default(&p))
860 ospf_external_lsa_refresh_default(ospf);
861 else {
862 struct ospf_lsa *current;
863
864 current = ospf_external_info_find_lsa(
865 ospf, &ei->p);
866 if (!current)
867 ospf_external_lsa_originate(
868 ospf, ei);
869 else {
870 if (IS_DEBUG_OSPF(
871 zebra,
872 ZEBRA_REDISTRIBUTE))
873 zlog_debug(
74489921 874 "ospf_zebra_read_route() : %s refreshing LSA",
d62a17ae 875 inet_ntoa(
876 p.prefix));
877 ospf_external_lsa_refresh(
878 ospf, current, ei,
879 LSA_REFRESH_FORCE);
880 }
881 }
882 }
883 }
121f9dee 884 } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
5048fe14 885 {
1fb93326 886 ospf_external_info_delete(ospf, rt_type, api.instance, p);
d62a17ae 887 if (is_prefix_default(&p))
888 ospf_external_lsa_refresh_default(ospf);
889 else
1fb93326 890 ospf_external_lsa_flush(ospf, rt_type, &p,
d62a17ae 891 ifindex /*, nexthop */);
5048fe14 892 }
d62a17ae 893
894 return 0;
718e3744 895}
6b0655a2 896
cf795c5d 897
d62a17ae 898int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
718e3744 899{
d62a17ae 900 /* Lookup access-list for distribute-list. */
901 DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
718e3744 902
d62a17ae 903 /* Clear previous distribute-name. */
904 if (DISTRIBUTE_NAME(ospf, type))
905 free(DISTRIBUTE_NAME(ospf, type));
718e3744 906
d62a17ae 907 /* Set distribute-name. */
908 DISTRIBUTE_NAME(ospf, type) = strdup(name);
718e3744 909
d62a17ae 910 /* If access-list have been set, schedule update timer. */
911 if (DISTRIBUTE_LIST(ospf, type))
912 ospf_distribute_list_update(ospf, type, 0);
718e3744 913
d62a17ae 914 return CMD_SUCCESS;
718e3744 915}
916
d62a17ae 917int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
918 const char *name)
718e3744 919{
d62a17ae 920 /* Schedule update timer. */
921 if (DISTRIBUTE_LIST(ospf, type))
922 ospf_distribute_list_update(ospf, type, 0);
718e3744 923
d62a17ae 924 /* Unset distribute-list. */
925 DISTRIBUTE_LIST(ospf, type) = NULL;
718e3744 926
d62a17ae 927 /* Clear distribute-name. */
928 if (DISTRIBUTE_NAME(ospf, type))
929 free(DISTRIBUTE_NAME(ospf, type));
cf795c5d 930
d62a17ae 931 DISTRIBUTE_NAME(ospf, type) = NULL;
718e3744 932
d62a17ae 933 return CMD_SUCCESS;
718e3744 934}
935
936/* distribute-list update timer. */
d62a17ae 937static int ospf_distribute_list_update_timer(struct thread *thread)
718e3744 938{
d62a17ae 939 struct route_node *rn;
940 struct external_info *ei;
941 struct route_table *rt;
942 struct ospf_lsa *lsa;
b5a8894d
CS
943 int type, default_refresh = 0, arg_type;
944 struct ospf *ospf = NULL;
996c9314 945 void **arg = THREAD_ARG(thread);
b5a8894d
CS
946
947 ospf = (struct ospf *)arg[0];
948 arg_type = (int)(intptr_t)arg[1];
d62a17ae 949
d62a17ae 950 if (ospf == NULL)
951 return 0;
952
953 ospf->t_distribute_update = NULL;
954
955 zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
956
b5a8894d 957 if (IS_DEBUG_OSPF_EVENT) {
996c9314
LB
958 zlog_debug(
959 "%s: ospf distribute-list update arg_type %d vrf %s id %d",
960 __PRETTY_FUNCTION__, arg_type,
961 ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id);
b5a8894d
CS
962 }
963
d62a17ae 964 /* foreach all external info. */
965 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
966 struct list *ext_list;
967 struct listnode *node;
968 struct ospf_external *ext;
969
de1ac5fd 970 ext_list = ospf->external[type];
d62a17ae 971 if (!ext_list)
972 continue;
973
974 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
975 rt = ext->external_info;
976 if (!rt)
977 continue;
978 for (rn = route_top(rt); rn; rn = route_next(rn))
979 if ((ei = rn->info) != NULL) {
980 if (is_prefix_default(&ei->p))
981 default_refresh = 1;
982 else if (
983 (lsa = ospf_external_info_find_lsa(
984 ospf, &ei->p)))
985 ospf_external_lsa_refresh(
986 ospf, lsa, ei,
987 LSA_REFRESH_IF_CHANGED);
988 else
989 ospf_external_lsa_originate(
990 ospf, ei);
991 }
992 }
993 }
994 if (default_refresh)
995 ospf_external_lsa_refresh_default(ospf);
b5a8894d
CS
996
997 XFREE(MTYPE_OSPF_DIST_ARGS, arg);
d62a17ae 998 return 0;
718e3744 999}
1000
718e3744 1001/* Update distribute-list and set timer to apply access-list. */
d7c0a89a
QY
1002void ospf_distribute_list_update(struct ospf *ospf, int type,
1003 unsigned short instance)
718e3744 1004{
d62a17ae 1005 struct route_table *rt;
1006 struct ospf_external *ext;
996c9314 1007 void **args = XCALLOC(MTYPE_OSPF_DIST_ARGS, sizeof(void *) * 2);
d62a17ae 1008
b5a8894d 1009 args[0] = ospf;
996c9314 1010 args[1] = (void *)((ptrdiff_t)type);
b5a8894d 1011
d62a17ae 1012 /* External info does not exist. */
de1ac5fd 1013 ext = ospf_external_lookup(ospf, type, instance);
a68730c6
DS
1014 if (!ext || !(rt = EXTERNAL_INFO(ext))) {
1015 XFREE(MTYPE_OSPF_DIST_ARGS, args);
d62a17ae 1016 return;
a68730c6 1017 }
d62a17ae 1018
1019 /* If exists previously invoked thread, then let it continue. */
a68730c6
DS
1020 if (ospf->t_distribute_update) {
1021 XFREE(MTYPE_OSPF_DIST_ARGS, args);
d62a17ae 1022 return;
a68730c6 1023 }
d62a17ae 1024
1025 /* Set timer. */
1026 ospf->t_distribute_update = NULL;
1027 thread_add_timer_msec(master, ospf_distribute_list_update_timer,
b5a8894d 1028 (void **)args, ospf->min_ls_interval,
d62a17ae 1029 &ospf->t_distribute_update);
718e3744 1030}
1031
1032/* If access-list is updated, apply some check. */
d62a17ae 1033static void ospf_filter_update(struct access_list *access)
718e3744 1034{
d62a17ae 1035 struct ospf *ospf;
1036 int type;
1037 int abr_inv = 0;
1038 struct ospf_area *area;
b5a8894d 1039 struct listnode *node, *n1;
d62a17ae 1040
1041 /* If OSPF instance does not exist, return right now. */
b5a8894d 1042 if (listcount(om->ospf) == 0)
d62a17ae 1043 return;
1044
b5a8894d 1045 /* Iterate all ospf [VRF] instances */
43b8d1d8 1046 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
b5a8894d
CS
1047 /* Update distribute-list, and apply filter. */
1048 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1049 struct list *red_list;
b5a8894d
CS
1050 struct ospf_redist *red;
1051
1052 red_list = ospf->redist[type];
1053 if (red_list)
996c9314
LB
1054 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1055 red)) {
b5a8894d 1056 if (ROUTEMAP(red)) {
996c9314
LB
1057 /* if route-map is not NULL it
1058 * may be
b5a8894d
CS
1059 * using this access list */
1060 ospf_distribute_list_update(
996c9314
LB
1061 ospf, type,
1062 red->instance);
b5a8894d 1063 }
d62a17ae 1064 }
d62a17ae 1065
b5a8894d
CS
1066 /* There is place for route-map for default-information
1067 * (ZEBRA_ROUTE_MAX),
1068 * but no distribute list. */
1069 if (type == ZEBRA_ROUTE_MAX)
1070 break;
1071
1072 if (DISTRIBUTE_NAME(ospf, type)) {
1073 /* Keep old access-list for distribute-list. */
996c9314
LB
1074 struct access_list *old =
1075 DISTRIBUTE_LIST(ospf, type);
b5a8894d
CS
1076
1077 /* Update access-list for distribute-list. */
996c9314
LB
1078 DISTRIBUTE_LIST(ospf, type) =
1079 access_list_lookup(
1080 AFI_IP,
1081 DISTRIBUTE_NAME(ospf, type));
b5a8894d
CS
1082
1083 /* No update for this distribute type. */
996c9314
LB
1084 if (old == NULL
1085 && DISTRIBUTE_LIST(ospf, type) == NULL)
b5a8894d
CS
1086 continue;
1087
1088 /* Schedule distribute-list update timer. */
1089 if (DISTRIBUTE_LIST(ospf, type) == NULL
996c9314
LB
1090 || strcmp(DISTRIBUTE_NAME(ospf, type),
1091 access->name)
1092 == 0)
1093 ospf_distribute_list_update(ospf, type,
1094 0);
b5a8894d 1095 }
d62a17ae 1096 }
d62a17ae 1097
b5a8894d
CS
1098 /* Update Area access-list. */
1099 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1100 if (EXPORT_NAME(area)) {
1101 EXPORT_LIST(area) = NULL;
1102 abr_inv++;
1103 }
d62a17ae 1104
b5a8894d
CS
1105 if (IMPORT_NAME(area)) {
1106 IMPORT_LIST(area) = NULL;
1107 abr_inv++;
1108 }
d62a17ae 1109 }
d62a17ae 1110
b5a8894d
CS
1111 /* Schedule ABR tasks -- this will be changed -- takada. */
1112 if (IS_OSPF_ABR(ospf) && abr_inv)
1113 ospf_schedule_abr_task(ospf);
1114 }
718e3744 1115}
dd669bb0 1116
1117/* If prefix-list is updated, do some updates. */
d62a17ae 1118void ospf_prefix_list_update(struct prefix_list *plist)
dd669bb0 1119{
b5a8894d 1120 struct ospf *ospf = NULL;
d62a17ae 1121 int type;
1122 int abr_inv = 0;
1123 struct ospf_area *area;
b5a8894d 1124 struct listnode *node, *n1;
d62a17ae 1125
1126 /* If OSPF instatnce does not exist, return right now. */
b5a8894d 1127 if (listcount(om->ospf) == 0)
d62a17ae 1128 return;
1129
b5a8894d
CS
1130 /* Iterate all ospf [VRF] instances */
1131 for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1132
1133 /* Update all route-maps which are used
1134 * as redistribution filters.
1135 * They might use prefix-list.
1136 */
1137 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1138 struct list *red_list;
b5a8894d
CS
1139 struct ospf_redist *red;
1140
1141 red_list = ospf->redist[type];
1142 if (red_list) {
996c9314
LB
1143 for (ALL_LIST_ELEMENTS_RO(red_list, node,
1144 red)) {
b5a8894d
CS
1145 if (ROUTEMAP(red)) {
1146 /* if route-map is not NULL
1147 * it may be using
1148 * this prefix list */
1149 ospf_distribute_list_update(
1150 ospf, type,
1151 red->instance);
1152 }
d62a17ae 1153 }
1154 }
b5a8894d 1155 }
d62a17ae 1156
b5a8894d
CS
1157 /* Update area filter-lists. */
1158 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1159 /* Update filter-list in. */
1160 if (PREFIX_NAME_IN(area))
1161 if (strcmp(PREFIX_NAME_IN(area),
996c9314
LB
1162 prefix_list_name(plist))
1163 == 0) {
b5a8894d
CS
1164 PREFIX_LIST_IN(area) =
1165 prefix_list_lookup(
996c9314
LB
1166 AFI_IP,
1167 PREFIX_NAME_IN(area));
b5a8894d
CS
1168 abr_inv++;
1169 }
d62a17ae 1170
b5a8894d
CS
1171 /* Update filter-list out. */
1172 if (PREFIX_NAME_OUT(area))
1173 if (strcmp(PREFIX_NAME_OUT(area),
996c9314
LB
1174 prefix_list_name(plist))
1175 == 0) {
b5a8894d
CS
1176 PREFIX_LIST_IN(area) =
1177 prefix_list_lookup(
996c9314
LB
1178 AFI_IP,
1179 PREFIX_NAME_OUT(area));
b5a8894d
CS
1180 abr_inv++;
1181 }
1182 }
d62a17ae 1183
b5a8894d
CS
1184 /* Schedule ABR task. */
1185 if (IS_OSPF_ABR(ospf) && abr_inv)
1186 ospf_schedule_abr_task(ospf);
1187 }
dd669bb0 1188}
cf795c5d 1189
d62a17ae 1190static struct ospf_distance *ospf_distance_new(void)
718e3744 1191{
d62a17ae 1192 return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
718e3744 1193}
1194
d62a17ae 1195static void ospf_distance_free(struct ospf_distance *odistance)
718e3744 1196{
d62a17ae 1197 XFREE(MTYPE_OSPF_DISTANCE, odistance);
718e3744 1198}
1199
d62a17ae 1200int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1201 const char *distance_str, const char *ip_str,
1202 const char *access_list_str)
718e3744 1203{
d62a17ae 1204 int ret;
1205 struct prefix_ipv4 p;
d7c0a89a 1206 uint8_t distance;
d62a17ae 1207 struct route_node *rn;
1208 struct ospf_distance *odistance;
1209
1210 ret = str2prefix_ipv4(ip_str, &p);
1211 if (ret == 0) {
1212 vty_out(vty, "Malformed prefix\n");
1213 return CMD_WARNING_CONFIG_FAILED;
1214 }
1215
1216 distance = atoi(distance_str);
1217
1218 /* Get OSPF distance node. */
1219 rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1220 if (rn->info) {
1221 odistance = rn->info;
1222 route_unlock_node(rn);
1223 } else {
1224 odistance = ospf_distance_new();
1225 rn->info = odistance;
1226 }
1227
1228 /* Set distance value. */
1229 odistance->distance = distance;
1230
1231 /* Reset access-list configuration. */
1232 if (odistance->access_list) {
1233 free(odistance->access_list);
1234 odistance->access_list = NULL;
1235 }
1236 if (access_list_str)
1237 odistance->access_list = strdup(access_list_str);
1238
1239 return CMD_SUCCESS;
718e3744 1240}
1241
d62a17ae 1242int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1243 const char *distance_str, const char *ip_str,
1244 char const *access_list_str)
718e3744 1245{
d62a17ae 1246 int ret;
1247 struct prefix_ipv4 p;
1248 struct route_node *rn;
1249 struct ospf_distance *odistance;
1250
1251 ret = str2prefix_ipv4(ip_str, &p);
1252 if (ret == 0) {
1253 vty_out(vty, "Malformed prefix\n");
1254 return CMD_WARNING_CONFIG_FAILED;
1255 }
1256
1257 rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1258 if (!rn) {
1259 vty_out(vty, "Can't find specified prefix\n");
1260 return CMD_WARNING_CONFIG_FAILED;
1261 }
1262
1263 odistance = rn->info;
1264
1265 if (odistance->access_list)
1266 free(odistance->access_list);
1267 ospf_distance_free(odistance);
1268
1269 rn->info = NULL;
1270 route_unlock_node(rn);
1271 route_unlock_node(rn);
1272
1273 return CMD_SUCCESS;
718e3744 1274}
1275
d62a17ae 1276void ospf_distance_reset(struct ospf *ospf)
718e3744 1277{
d62a17ae 1278 struct route_node *rn;
1279 struct ospf_distance *odistance;
1280
1281 for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn))
1282 if ((odistance = rn->info) != NULL) {
1283 if (odistance->access_list)
1284 free(odistance->access_list);
1285 ospf_distance_free(odistance);
1286 rn->info = NULL;
1287 route_unlock_node(rn);
1288 }
718e3744 1289}
1290
d7c0a89a
QY
1291uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1292 struct ospf_route * or)
718e3744 1293{
718e3744 1294
d62a17ae 1295 if (ospf == NULL)
1296 return 0;
718e3744 1297
d62a17ae 1298 if (ospf->distance_intra)
1299 if (or->path_type == OSPF_PATH_INTRA_AREA)
1300 return ospf->distance_intra;
718e3744 1301
d62a17ae 1302 if (ospf->distance_inter)
1303 if (or->path_type == OSPF_PATH_INTER_AREA)
1304 return ospf->distance_inter;
718e3744 1305
d62a17ae 1306 if (ospf->distance_external)
1307 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1308 or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1309 return ospf->distance_external;
cf795c5d 1310
d62a17ae 1311 if (ospf->distance_all)
1312 return ospf->distance_all;
718e3744 1313
d62a17ae 1314 return 0;
718e3744 1315}
1316
b5a8894d
CS
1317void ospf_zebra_vrf_register(struct ospf *ospf)
1318{
1319 if (!zclient || zclient->sock < 0 || !ospf)
1320 return;
1321
6021c6c0 1322 if (ospf->vrf_id != VRF_UNKNOWN) {
b5a8894d
CS
1323 if (IS_DEBUG_OSPF_EVENT)
1324 zlog_debug("%s: Register VRF %s id %u",
1325 __PRETTY_FUNCTION__,
1326 ospf_vrf_id_to_name(ospf->vrf_id),
1327 ospf->vrf_id);
1328 zclient_send_reg_requests(zclient, ospf->vrf_id);
1329 }
1330}
1331
1332void ospf_zebra_vrf_deregister(struct ospf *ospf)
1333{
1334 if (!zclient || zclient->sock < 0 || !ospf)
1335 return;
1336
1337 if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1338 if (IS_DEBUG_OSPF_EVENT)
6021c6c0 1339 zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
b5a8894d
CS
1340 __PRETTY_FUNCTION__,
1341 ospf_vrf_id_to_name(ospf->vrf_id),
1342 ospf->vrf_id);
1343 /* Deregister for router-id, interfaces,
1344 * redistributed routes. */
1345 zclient_send_dereg_requests(zclient, ospf->vrf_id);
1346 }
1347}
d62a17ae 1348static void ospf_zebra_connected(struct zclient *zclient)
7076bb2f 1349{
d62a17ae 1350 /* Send the client registration */
0945d5ed 1351 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2376c3f2 1352
d62a17ae 1353 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
1354}
1355
d7c0a89a 1356void ospf_zebra_init(struct thread_master *master, unsigned short instance)
718e3744 1357{
d62a17ae 1358 /* Allocate zebra structure. */
26f63a1e 1359 zclient = zclient_new(master, &zclient_options_default);
342213ea 1360 zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
d62a17ae 1361 zclient->zebra_connected = ospf_zebra_connected;
1362 zclient->router_id_update = ospf_router_id_update_zebra;
d62a17ae 1363 zclient->interface_address_add = ospf_interface_address_add;
1364 zclient->interface_address_delete = ospf_interface_address_delete;
1365 zclient->interface_link_params = ospf_interface_link_params;
b5a8894d 1366 zclient->interface_vrf_update = ospf_interface_vrf_update;
d62a17ae 1367
74489921
RW
1368 zclient->redistribute_route_add = ospf_zebra_read_route;
1369 zclient->redistribute_route_del = ospf_zebra_read_route;
d62a17ae 1370
1371 access_list_add_hook(ospf_filter_update);
1372 access_list_delete_hook(ospf_filter_update);
1373 prefix_list_add_hook(ospf_prefix_list_update);
1374 prefix_list_delete_hook(ospf_prefix_list_update);
718e3744 1375}