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