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