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