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