blob: 60d98529d7420672d4212f2f0813900f3069e37b [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "thread.h"
26#include "command.h"
27#include "network.h"
28#include "prefix.h"
29#include "routemap.h"
30#include "table.h"
31#include "stream.h"
32#include "memory.h"
33#include "zclient.h"
34#include "filter.h"
hassodd669bb2004-05-10 07:43:59 +000035#include "plist.h"
paul718e3742002-12-13 20:15:29 +000036#include "log.h"
37
38#include "ospfd/ospfd.h"
39#include "ospfd/ospf_interface.h"
40#include "ospfd/ospf_ism.h"
41#include "ospfd/ospf_asbr.h"
42#include "ospfd/ospf_asbr.h"
43#include "ospfd/ospf_abr.h"
44#include "ospfd/ospf_lsa.h"
45#include "ospfd/ospf_dump.h"
46#include "ospfd/ospf_route.h"
47#include "ospfd/ospf_zebra.h"
48#ifdef HAVE_SNMP
49#include "ospfd/ospf_snmp.h"
50#endif /* HAVE_SNMP */
Olivier Dugeon29a14012016-04-19 18:42:40 +020051#include "ospfd/ospf_te.h"
paul718e3742002-12-13 20:15:29 +000052
53/* Zebra structure to hold current status. */
54struct zclient *zclient = NULL;
55
56/* For registering threads. */
57extern struct thread_master *master;
hasso18a6dce2004-10-03 18:18:34 +000058struct in_addr router_id_zebra;
59
60/* Router-id update message from zebra. */
paul4dadc292005-05-06 21:37:42 +000061static int
hasso18a6dce2004-10-03 18:18:34 +000062ospf_router_id_update_zebra (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +080063 zebra_size_t length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +000064{
65 struct ospf *ospf;
66 struct prefix router_id;
67 zebra_router_id_update_read(zclient->ibuf,&router_id);
68
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +000069 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
70 {
71 char buf[128];
72 prefix2str(&router_id, buf, sizeof(buf));
73 zlog_debug("Zebra rcvd: router id update %s", buf);
74 }
75
hasso18a6dce2004-10-03 18:18:34 +000076 router_id_zebra = router_id.u.prefix4;
77
78 ospf = ospf_lookup ();
paulb29800a2005-11-20 14:50:45 +000079
hasso18a6dce2004-10-03 18:18:34 +000080 if (ospf != NULL)
paulb29800a2005-11-20 14:50:45 +000081 ospf_router_id_update (ospf);
82
hasso18a6dce2004-10-03 18:18:34 +000083 return 0;
84}
paul718e3742002-12-13 20:15:29 +000085
86/* Inteface addition message from zebra. */
paul4dadc292005-05-06 21:37:42 +000087static int
Feng Luc99f3482014-10-16 09:52:36 +080088ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length,
89 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +000090{
91 struct interface *ifp;
92
Feng Luc99f3482014-10-16 09:52:36 +080093 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
paul718e3742002-12-13 20:15:29 +000094
95 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
Stephen Hemminger30d20592009-07-28 11:58:51 +010096 zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
97 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
98 ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000099
paulcf795c52003-06-19 02:13:25 +0000100 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +0000101
paul718e3742002-12-13 20:15:29 +0000102 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
103 {
104 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +0000105 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +0000106 }
107
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100108 ospf_if_update (NULL, ifp);
paul718e3742002-12-13 20:15:29 +0000109
110#ifdef HAVE_SNMP
111 ospf_snmp_if_update (ifp);
112#endif /* HAVE_SNMP */
113
114 return 0;
115}
116
paul4dadc292005-05-06 21:37:42 +0000117static int
paul718e3742002-12-13 20:15:29 +0000118ospf_interface_delete (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800119 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000120{
121 struct interface *ifp;
122 struct stream *s;
123 struct route_node *rn;
124
paulcf795c52003-06-19 02:13:25 +0000125 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000126 /* zebra_interface_state_read() updates interface structure in iflist */
Feng Luc99f3482014-10-16 09:52:36 +0800127 ifp = zebra_interface_state_read (s, vrf_id);
paul718e3742002-12-13 20:15:29 +0000128
129 if (ifp == NULL)
130 return 0;
131
132 if (if_is_up (ifp))
133 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000134 ifp->name);
135
paul718e3742002-12-13 20:15:29 +0000136 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000137 zlog_debug
Andrew Certain0798cee2012-12-04 13:43:42 -0800138 ("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
139 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000140
141#ifdef HAVE_SNMP
142 ospf_snmp_if_delete (ifp);
143#endif /* HAVE_SNMP */
144
145 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
146 if (rn->info)
147 ospf_if_free ((struct ospf_interface *) rn->info);
148
ajsd2fc8892005-04-02 18:38:43 +0000149 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000150 return 0;
151}
152
ajsd2fc8892005-04-02 18:38:43 +0000153static struct interface *
Feng Luc99f3482014-10-16 09:52:36 +0800154zebra_interface_if_lookup (struct stream *s, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000155{
ajs21fefa92005-04-02 23:16:41 +0000156 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000157
158 /* Read interface name. */
159 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
160
ajsd2fc8892005-04-02 18:38:43 +0000161 /* And look it up. */
ajs21fefa92005-04-02 23:16:41 +0000162 return if_lookup_by_name_len(ifname_tmp,
163 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000164}
165
paul4dadc292005-05-06 21:37:42 +0000166static int
paul718e3742002-12-13 20:15:29 +0000167ospf_interface_state_up (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800168 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000169{
170 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000171 struct ospf_interface *oi;
172 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000173
Feng Luc99f3482014-10-16 09:52:36 +0800174 ifp = zebra_interface_if_lookup (zclient->ibuf, vrf_id);
paul718e3742002-12-13 20:15:29 +0000175
176 if (ifp == NULL)
177 return 0;
178
179 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000180 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000181 {
182 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000183 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000184 memcpy (&if_tmp, ifp, sizeof (struct interface));
185
186 zebra_interface_if_set_value (zclient->ibuf, ifp);
187
188 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000189 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000190
191 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000192 {
193 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000194 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000195 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000196
paulcf795c52003-06-19 02:13:25 +0000197 ospf_if_recalculate_output_cost (ifp);
198 }
ajsa608bbf2005-03-29 17:03:49 +0000199
200 if (if_tmp.mtu != ifp->mtu)
201 {
202 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
203 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
204 ifp->name, if_tmp.mtu, ifp->mtu);
205
206 /* Must reset the interface (simulate down/up) when MTU changes. */
207 ospf_if_reset(ifp);
208 }
paul718e3742002-12-13 20:15:29 +0000209 return 0;
210 }
paulcf795c52003-06-19 02:13:25 +0000211
paul718e3742002-12-13 20:15:29 +0000212 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000213
paul718e3742002-12-13 20:15:29 +0000214 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000215 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000216
217 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000218 {
paulcf795c52003-06-19 02:13:25 +0000219 if ((oi = rn->info) == NULL)
220 continue;
221
paul718e3742002-12-13 20:15:29 +0000222 ospf_if_up (oi);
223 }
paulcf795c52003-06-19 02:13:25 +0000224
paul718e3742002-12-13 20:15:29 +0000225 return 0;
226}
227
paul4dadc292005-05-06 21:37:42 +0000228static int
paul718e3742002-12-13 20:15:29 +0000229ospf_interface_state_down (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800230 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000231{
232 struct interface *ifp;
233 struct ospf_interface *oi;
234 struct route_node *node;
235
Feng Luc99f3482014-10-16 09:52:36 +0800236 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
paul718e3742002-12-13 20:15:29 +0000237
238 if (ifp == NULL)
239 return 0;
240
241 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000242 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000243
paulcf795c52003-06-19 02:13:25 +0000244 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000245 {
paulcf795c52003-06-19 02:13:25 +0000246 if ((oi = node->info) == NULL)
247 continue;
paul718e3742002-12-13 20:15:29 +0000248 ospf_if_down (oi);
249 }
250
251 return 0;
252}
253
paul4dadc292005-05-06 21:37:42 +0000254static int
paul718e3742002-12-13 20:15:29 +0000255ospf_interface_address_add (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800256 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000257{
258 struct connected *c;
259
Feng Luc99f3482014-10-16 09:52:36 +0800260 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
paul718e3742002-12-13 20:15:29 +0000261
262 if (c == NULL)
263 return 0;
264
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000265 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
266 {
267 char buf[128];
268 prefix2str(c->address, buf, sizeof(buf));
269 zlog_debug("Zebra: interface %s address add %s", c->ifp->name, buf);
270 }
271
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100272 ospf_if_update (NULL, c->ifp);
paul718e3742002-12-13 20:15:29 +0000273
274#ifdef HAVE_SNMP
275 ospf_snmp_if_update (c->ifp);
276#endif /* HAVE_SNMP */
277
278 return 0;
279}
280
paul4dadc292005-05-06 21:37:42 +0000281static int
paul718e3742002-12-13 20:15:29 +0000282ospf_interface_address_delete (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800283 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000284{
285 struct connected *c;
286 struct interface *ifp;
287 struct ospf_interface *oi;
288 struct route_node *rn;
289 struct prefix p;
290
Feng Luc99f3482014-10-16 09:52:36 +0800291 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
paul718e3742002-12-13 20:15:29 +0000292
293 if (c == NULL)
294 return 0;
295
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000296 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
297 {
298 char buf[128];
299 prefix2str(c->address, buf, sizeof(buf));
300 zlog_debug("Zebra: interface %s address delete %s", c->ifp->name, buf);
301 }
302
paul718e3742002-12-13 20:15:29 +0000303 ifp = c->ifp;
304 p = *c->address;
305 p.prefixlen = IPV4_MAX_PREFIXLEN;
306
307 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000308 if (!rn)
paul98429f62006-01-10 22:11:54 +0000309 {
310 connected_free (c);
311 return 0;
312 }
paul718e3742002-12-13 20:15:29 +0000313
314 assert (rn->info);
315 oi = rn->info;
Joakim Tjernlundfbb6c862010-03-08 13:58:09 +0100316 route_unlock_node (rn);
paulcf795c52003-06-19 02:13:25 +0000317
paul718e3742002-12-13 20:15:29 +0000318 /* Call interface hook functions to clean up */
319 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000320
paul718e3742002-12-13 20:15:29 +0000321#ifdef HAVE_SNMP
322 ospf_snmp_if_update (c->ifp);
323#endif /* HAVE_SNMP */
324
325 connected_free (c);
326
paul718e3742002-12-13 20:15:29 +0000327 return 0;
328}
paul72357f22003-06-19 02:11:23 +0000329
Olivier Dugeon29a14012016-04-19 18:42:40 +0200330static int
331ospf_interface_link_params (int command, struct zclient *zclient,
332 zebra_size_t length)
333{
334 struct interface *ifp;
335
336 ifp = zebra_interface_link_params_read (zclient->ibuf);
337
338 if (ifp == NULL)
339 return 0;
340
341 /* Update TE TLV */
342 ospf_mpls_te_update_if (ifp);
343
344 return 0;
345}
346
347
paul718e3742002-12-13 20:15:29 +0000348void
349ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
350{
351 u_char message;
352 u_char distance;
353 u_char flags;
354 int psize;
355 struct stream *s;
356 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000357 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000358
Feng Luc99f3482014-10-16 09:52:36 +0800359 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000360 {
361 message = 0;
362 flags = 0;
363
364 /* OSPF pass nexthop and metric */
365 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
366 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
367
368 /* Distance value. */
369 distance = ospf_distance_apply (p, or);
370 if (distance)
paul72357f22003-06-19 02:11:23 +0000371 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000372
Paul Jakma96d10602016-07-01 14:23:45 +0100373 /* Check if path type is ASE */
Piotr Chytła2b2e38c2015-12-01 10:10:41 -0500374 if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL) ||
375 (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)) &&
Paul Jakma96d10602016-07-01 14:23:45 +0100376 (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX))
Piotr Chytła2b2e38c2015-12-01 10:10:41 -0500377 SET_FLAG (message, ZAPI_MESSAGE_TAG);
378
paul718e3742002-12-13 20:15:29 +0000379 /* Make packet. */
380 s = zclient->obuf;
381 stream_reset (s);
382
paul718e3742002-12-13 20:15:29 +0000383 /* Put command, type, flags, message. */
Feng Luc99f3482014-10-16 09:52:36 +0800384 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000385 stream_putc (s, ZEBRA_ROUTE_OSPF);
386 stream_putc (s, flags);
387 stream_putc (s, message);
Denis Ovsienkob4e45f62011-12-05 16:35:14 +0400388 stream_putw (s, SAFI_UNICAST);
paulcf795c52003-06-19 02:13:25 +0000389
paul718e3742002-12-13 20:15:29 +0000390 /* Put prefix information. */
391 psize = PSIZE (p->prefixlen);
392 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000393 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000394
395 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000396 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000397
398 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000399 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000400 {
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200401 if (path->nexthop.s_addr != INADDR_ANY &&
402 path->ifindex != 0)
403 {
404 stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
405 stream_put_in_addr (s, &path->nexthop);
406 stream_putl (s, path->ifindex);
407 }
408 else if (path->nexthop.s_addr != INADDR_ANY)
paulcf795c52003-06-19 02:13:25 +0000409 {
410 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
411 stream_put_in_addr (s, &path->nexthop);
412 }
413 else
414 {
415 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +0200416 if (path->ifindex)
417 stream_putl (s, path->ifindex);
paulcf795c52003-06-19 02:13:25 +0000418 else
419 stream_putl (s, 0);
420 }
paul72357f22003-06-19 02:11:23 +0000421
422 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
423 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000424 char buf[2][INET_ADDRSTRLEN];
425 zlog_debug("Zebra: Route add %s/%d nexthop %s",
426 inet_ntop(AF_INET, &p->prefix,
427 buf[0], sizeof(buf[0])),
428 p->prefixlen,
429 inet_ntop(AF_INET, &path->nexthop,
430 buf[1], sizeof(buf[1])));
paulcf795c52003-06-19 02:13:25 +0000431 }
432 }
paul718e3742002-12-13 20:15:29 +0000433
434 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000435 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000436 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000437 {
438 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
439 stream_putl (s, or->cost + or->u.ext.type2_cost);
440 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
441 stream_putl (s, or->u.ext.type2_cost);
442 else
443 stream_putl (s, or->cost);
444 }
paul718e3742002-12-13 20:15:29 +0000445
Piotr Chytła2b2e38c2015-12-01 10:10:41 -0500446 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +0100447 stream_putl (s, or->u.ext.tag);
Piotr Chytła2b2e38c2015-12-01 10:10:41 -0500448
paul718e3742002-12-13 20:15:29 +0000449 stream_putw_at (s, 0, stream_get_endp (s));
450
ajs634f9ea2005-04-11 15:51:40 +0000451 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000452 }
453}
454
455void
456ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
457{
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200458 u_char message;
459 u_char distance;
460 u_char flags;
461 int psize;
462 struct stream *s;
paul72357f22003-06-19 02:11:23 +0000463 struct ospf_path *path;
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200464 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000465
Feng Luc99f3482014-10-16 09:52:36 +0800466 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000467 {
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200468 message = 0;
469 flags = 0;
470 /* Distance value. */
471 distance = ospf_distance_apply (p, or);
472 /* Make packet. */
473 s = zclient->obuf;
474 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000475
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200476 /* Put command, type, flags, message. */
Feng Luc99f3482014-10-16 09:52:36 +0800477 zclient_create_header (s, ZEBRA_IPV4_ROUTE_DELETE, VRF_DEFAULT);
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200478 stream_putc (s, ZEBRA_ROUTE_OSPF);
479 stream_putc (s, flags);
480 stream_putc (s, message);
481 stream_putw (s, SAFI_UNICAST);
paul72357f22003-06-19 02:11:23 +0000482
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200483 /* Put prefix information. */
484 psize = PSIZE (p->prefixlen);
485 stream_putc (s, p->prefixlen);
486 stream_write (s, (u_char *) & p->prefix, psize);
paul72357f22003-06-19 02:11:23 +0000487
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200488 /* Nexthop count. */
489 stream_putc (s, or->paths->count);
490
491 /* Nexthop, ifindex, distance and metric information. */
492 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
493 {
494 if (path->nexthop.s_addr != INADDR_ANY &&
495 path->ifindex != 0)
496 {
497 stream_putc (s, ZEBRA_NEXTHOP_IPV4_IFINDEX);
498 stream_put_in_addr (s, &path->nexthop);
499 stream_putl (s, path->ifindex);
500 }
501 else if (path->nexthop.s_addr != INADDR_ANY)
502 {
503 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
504 stream_put_in_addr (s, &path->nexthop);
505 }
506 else
507 {
508 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
509 stream_putl (s, path->ifindex);
510 }
511
512 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
513 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000514 char buf[2][INET_ADDRSTRLEN];
Christian Frankea25a1262013-11-27 14:36:05 +0000515 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200516 inet_ntop(AF_INET, &p->prefix,
517 buf[0], sizeof(buf[0])),
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000518 p->prefixlen,
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200519 inet_ntop(AF_INET, &path->nexthop,
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000520 buf[1], sizeof(buf[1])));
Joakim Tjernlundba281d32012-07-07 17:06:13 +0200521 }
522 }
523
524 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
525 stream_putc (s, distance);
526 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
527 {
528 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
529 stream_putl (s, or->cost + or->u.ext.type2_cost);
530 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
531 stream_putl (s, or->u.ext.type2_cost);
532 else
533 stream_putl (s, or->cost);
534 }
535
536 stream_putw_at (s, 0, stream_get_endp (s));
537
538 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000539 }
540}
541
542void
543ospf_zebra_add_discard (struct prefix_ipv4 *p)
544{
545 struct zapi_ipv4 api;
546
Feng Luc99f3482014-10-16 09:52:36 +0800547 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000548 {
Feng Luc99f3482014-10-16 09:52:36 +0800549 api.vrf_id = VRF_DEFAULT;
paul718e3742002-12-13 20:15:29 +0000550 api.type = ZEBRA_ROUTE_OSPF;
551 api.flags = ZEBRA_FLAG_BLACKHOLE;
552 api.message = 0;
Denis Ovsienkob4e45f62011-12-05 16:35:14 +0400553 api.safi = SAFI_UNICAST;
paul718e3742002-12-13 20:15:29 +0000554 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
555 api.nexthop_num = 0;
556 api.ifindex_num = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500557 api.tag = 0;
paul718e3742002-12-13 20:15:29 +0000558
paul0a589352004-05-08 11:48:26 +0000559 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000560
561 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
562 zlog_debug ("Zebra: Route add discard %s/%d",
563 inet_ntoa (p->prefix), p->prefixlen);
paul718e3742002-12-13 20:15:29 +0000564 }
565}
566
567void
568ospf_zebra_delete_discard (struct prefix_ipv4 *p)
569{
570 struct zapi_ipv4 api;
571
Feng Luc99f3482014-10-16 09:52:36 +0800572 if (vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_OSPF], VRF_DEFAULT))
paul718e3742002-12-13 20:15:29 +0000573 {
Feng Luc99f3482014-10-16 09:52:36 +0800574 api.vrf_id = VRF_DEFAULT;
paul718e3742002-12-13 20:15:29 +0000575 api.type = ZEBRA_ROUTE_OSPF;
576 api.flags = ZEBRA_FLAG_BLACKHOLE;
577 api.message = 0;
Denis Ovsienkob4e45f62011-12-05 16:35:14 +0400578 api.safi = SAFI_UNICAST;
paul718e3742002-12-13 20:15:29 +0000579 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
580 api.nexthop_num = 0;
581 api.ifindex_num = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500582 api.tag = 0;
paul718e3742002-12-13 20:15:29 +0000583
paul0a589352004-05-08 11:48:26 +0000584 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000585
586 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000587 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000588 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000589
paul718e3742002-12-13 20:15:29 +0000590 }
591}
592
593int
594ospf_is_type_redistributed (int type)
595{
596 return (DEFAULT_ROUTE_TYPE (type)) ?
Feng Luc99f3482014-10-16 09:52:36 +0800597 vrf_bitmap_check (zclient->default_information, VRF_DEFAULT) : \
598 vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000599}
600
601int
paul020709f2003-04-04 02:44:16 +0000602ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000603{
604 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000605
paul718e3742002-12-13 20:15:29 +0000606 if (ospf_is_type_redistributed (type))
607 {
paul68980082003-03-25 05:07:42 +0000608 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000609 {
610 ospf->dmetric[type].type = mtype;
611 force = LSA_REFRESH_FORCE;
612 }
paul68980082003-03-25 05:07:42 +0000613 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000614 {
615 ospf->dmetric[type].value = mvalue;
616 force = LSA_REFRESH_FORCE;
617 }
618
paul68980082003-03-25 05:07:42 +0000619 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000620
paul718e3742002-12-13 20:15:29 +0000621 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000622 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000623 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000624 metric_type (ospf, type), metric_value (ospf, type));
625
paul718e3742002-12-13 20:15:29 +0000626 return CMD_SUCCESS;
627 }
628
paul68980082003-03-25 05:07:42 +0000629 ospf->dmetric[type].type = mtype;
630 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000631
Feng Luc99f3482014-10-16 09:52:36 +0800632 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000633
634 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000635 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000636 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000637 metric_type (ospf, type), metric_value (ospf, type));
638
paul68980082003-03-25 05:07:42 +0000639 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000640
641 return CMD_SUCCESS;
642}
643
644int
paul020709f2003-04-04 02:44:16 +0000645ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000646{
647 if (type == zclient->redist_default)
648 return CMD_SUCCESS;
649
paulcf795c52003-06-19 02:13:25 +0000650 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000651 return CMD_SUCCESS;
652
Feng Luc99f3482014-10-16 09:52:36 +0800653 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type, VRF_DEFAULT);
paulcf795c52003-06-19 02:13:25 +0000654
paul718e3742002-12-13 20:15:29 +0000655 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000656 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000657 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000658
paul68980082003-03-25 05:07:42 +0000659 ospf->dmetric[type].type = -1;
660 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000661
662 /* Remove the routes from OSPF table. */
Paul Jakma6db3a6f2006-05-12 23:02:46 +0000663 ospf_redistribute_withdraw (ospf, type);
paul718e3742002-12-13 20:15:29 +0000664
paul68980082003-03-25 05:07:42 +0000665 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000666
667 return CMD_SUCCESS;
668}
669
670int
paul020709f2003-04-04 02:44:16 +0000671ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000672 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000673{
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000674 ospf->default_originate = originate;
675 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
676 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul020709f2003-04-04 02:44:16 +0000677
paul718e3742002-12-13 20:15:29 +0000678 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
679 {
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000680 /* if ospf->default_originate changes value, is calling
681 ospf_external_lsa_refresh_default sufficient to implement
682 the change? */
paul68980082003-03-25 05:07:42 +0000683 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000684
paul718e3742002-12-13 20:15:29 +0000685 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000686 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000687 ospf_redist_string(DEFAULT_ROUTE),
paulcf795c52003-06-19 02:13:25 +0000688 metric_type (ospf, DEFAULT_ROUTE),
689 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000690 return CMD_SUCCESS;
691 }
692
Feng Luc99f3482014-10-16 09:52:36 +0800693 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient,
694 VRF_DEFAULT);
paulcf795c52003-06-19 02:13:25 +0000695
paul718e3742002-12-13 20:15:29 +0000696 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000697 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000698 metric_type (ospf, DEFAULT_ROUTE),
699 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000700
paul68980082003-03-25 05:07:42 +0000701 if (ospf->router_id.s_addr == 0)
702 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000703 else
Paul Jakma4021b602006-05-12 22:55:41 +0000704 thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
paul718e3742002-12-13 20:15:29 +0000705
paul68980082003-03-25 05:07:42 +0000706 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000707
708 return CMD_SUCCESS;
709}
710
711int
paul020709f2003-04-04 02:44:16 +0000712ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000713{
714 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
715 return CMD_SUCCESS;
716
paul68980082003-03-25 05:07:42 +0000717 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
718 ospf->dmetric[DEFAULT_ROUTE].type = -1;
719 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000720
Feng Luc99f3482014-10-16 09:52:36 +0800721 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient,
722 VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000723
724 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000725 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000726
paul68980082003-03-25 05:07:42 +0000727 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000728
729 return CMD_SUCCESS;
730}
731
paul4dadc292005-05-06 21:37:42 +0000732static int
paul020709f2003-04-04 02:44:16 +0000733ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000734 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000735{
736 /* If prefix is multicast, then do not originate LSA. */
737 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
738 {
739 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000740 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000741 return 0;
742 }
743
744 /* Take care of default-originate. */
745 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000746 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000747 {
Denis Ovsienkobcc6c592011-09-10 23:29:19 +0400748 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
paulcf795c52003-06-19 02:13:25 +0000749 "for default");
750 return 0;
paul718e3742002-12-13 20:15:29 +0000751 }
752
753 return 1;
754}
755
756/* If connected prefix is OSPF enable interface, then do not announce. */
757int
paulcf795c52003-06-19 02:13:25 +0000758ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000759{
Joakim Tjernlund5d8de932009-08-07 13:48:15 +0200760 struct listnode *node;
761 struct ospf_interface *oi;
paul718e3742002-12-13 20:15:29 +0000762
Joakim Tjernlund5d8de932009-08-07 13:48:15 +0200763
764 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
765 if (prefix_match (oi->address, (struct prefix *) &ei->p))
paulcf795c52003-06-19 02:13:25 +0000766 return 0;
paul718e3742002-12-13 20:15:29 +0000767 return 1;
768}
769
770/* return 1 if external LSA must be originated, 0 otherwise */
771int
paul68980082003-03-25 05:07:42 +0000772ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000773 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000774{
775 struct route_map_set_values save_values;
776 struct prefix_ipv4 *p = &ei->p;
777 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000778
paul718e3742002-12-13 20:15:29 +0000779 if (changed)
780 *changed = 0;
781
paul020709f2003-04-04 02:44:16 +0000782 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000783 return 0;
784
785 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000786 if (type == ZEBRA_ROUTE_CONNECT &&
787 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000788 return 0;
789
paul020709f2003-04-04 02:44:16 +0000790 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000791 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000792 if (DISTRIBUTE_LIST (ospf, type))
793 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000794 {
795 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000796 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000797 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000798 inet_ntoa (p->prefix), p->prefixlen);
799 return 0;
800 }
paul718e3742002-12-13 20:15:29 +0000801
802 save_values = ei->route_map_set;
803 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000804
paul718e3742002-12-13 20:15:29 +0000805 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000806 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000807 {
808 int ret;
809
paulcf795c52003-06-19 02:13:25 +0000810 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
811 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000812
813 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000814 {
815 ei->route_map_set = save_values;
816 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000817 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000818 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000819 inet_ntoa (p->prefix), p->prefixlen);
820 return 0;
821 }
822
paul718e3742002-12-13 20:15:29 +0000823 /* check if 'route-map set' changed something */
824 if (changed)
paulcf795c52003-06-19 02:13:25 +0000825 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
826 &save_values);
paul718e3742002-12-13 20:15:29 +0000827 }
828
829 return 1;
830}
831
832/* OSPF route-map set for redistribution */
833void
paul6c835672004-10-11 11:00:30 +0000834ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000835{
paul020709f2003-04-04 02:44:16 +0000836 if (ROUTEMAP_NAME (ospf, type))
837 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000838
paul020709f2003-04-04 02:44:16 +0000839 ROUTEMAP_NAME (ospf, type) = strdup (name);
840 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000841}
842
843void
paul020709f2003-04-04 02:44:16 +0000844ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000845{
paul020709f2003-04-04 02:44:16 +0000846 if (ROUTEMAP_NAME (ospf, type))
847 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000848
paul020709f2003-04-04 02:44:16 +0000849 ROUTEMAP_NAME (ospf, type) = NULL;
850 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000851}
852
853/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000854static int
paul718e3742002-12-13 20:15:29 +0000855ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
Feng Luc99f3482014-10-16 09:52:36 +0800856 zebra_size_t length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000857{
858 struct stream *s;
859 struct zapi_ipv4 api;
860 unsigned long ifindex;
861 struct in_addr nexthop;
862 struct prefix_ipv4 p;
863 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000864 struct ospf *ospf;
Donald Sharp5e57b5f2016-03-11 16:28:34 -0500865 unsigned char plength = 0;
paul718e3742002-12-13 20:15:29 +0000866
867 s = zclient->ibuf;
868 ifindex = 0;
869 nexthop.s_addr = 0;
870
871 /* Type, flags, message. */
872 api.type = stream_getc (s);
873 api.flags = stream_getc (s);
874 api.message = stream_getc (s);
875
876 /* IPv4 prefix. */
877 memset (&p, 0, sizeof (struct prefix_ipv4));
878 p.family = AF_INET;
Donald Sharp5e57b5f2016-03-11 16:28:34 -0500879 plength = stream_getc (s);
880 p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
paul718e3742002-12-13 20:15:29 +0000881 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
882
hasso8585d4e2004-04-20 17:25:12 +0000883 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
884 return 0;
885
paul718e3742002-12-13 20:15:29 +0000886 /* Nexthop, ifindex, distance, metric. */
887 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
888 {
889 api.nexthop_num = stream_getc (s);
890 nexthop.s_addr = stream_get_ipv4 (s);
891 }
892 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
893 {
894 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000895 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000896 ifindex = stream_getl (s);
897 }
898 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
899 api.distance = stream_getc (s);
900 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
901 api.metric = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500902 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +0100903 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500904 else
905 api.tag = 0;
paul718e3742002-12-13 20:15:29 +0000906
paul020709f2003-04-04 02:44:16 +0000907 ospf = ospf_lookup ();
908 if (ospf == NULL)
909 return 0;
910
paul718e3742002-12-13 20:15:29 +0000911 if (command == ZEBRA_IPV4_ROUTE_ADD)
912 {
paul7021c422003-07-15 12:52:22 +0000913 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000914 * Maybe we should ignore reject/blackhole routes? Testing shows that
915 * there is no problems though and this is only way to "summarize"
916 * routes in ASBR at the moment. Maybe we need just a better generalised
917 * solution for these types?
918 *
919 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
920 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
921 * return 0;
paul7021c422003-07-15 12:52:22 +0000922 */
Piotr Chytła2b2e38c2015-12-01 10:10:41 -0500923
924 /* Protocol tag overwrites all other tag value send by zebra */
925 if (ospf->dtag[api.type] > 0)
926 api.tag = ospf->dtag[api.type];
927
928 ei = ospf_external_info_add (api.type, p, ifindex, nexthop, api.tag);
paul718e3742002-12-13 20:15:29 +0000929
paul68980082003-03-25 05:07:42 +0000930 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000931 /* Set flags to generate AS-external-LSA originate event
932 for each redistributed protocols later. */
933 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000934 else
paulcf795c52003-06-19 02:13:25 +0000935 {
936 if (ei)
937 {
938 if (is_prefix_default (&p))
939 ospf_external_lsa_refresh_default (ospf);
940 else
941 {
942 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000943
paulcf795c52003-06-19 02:13:25 +0000944 current = ospf_external_info_find_lsa (ospf, &ei->p);
945 if (!current)
946 ospf_external_lsa_originate (ospf, ei);
947 else if (IS_LSA_MAXAGE (current))
948 ospf_external_lsa_refresh (ospf, current,
949 ei, LSA_REFRESH_FORCE);
950 else
951 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
952 inet_ntoa (p.prefix));
953 }
954 }
955 }
paul718e3742002-12-13 20:15:29 +0000956 }
paulcf795c52003-06-19 02:13:25 +0000957 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000958 {
959 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000960 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000961 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000962 else
ajs5339cfd2005-09-19 13:28:05 +0000963 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000964 }
965
966 return 0;
967}
David Lamparter6b0655a2014-06-04 06:53:35 +0200968
paulcf795c52003-06-19 02:13:25 +0000969
paul718e3742002-12-13 20:15:29 +0000970int
paul6c835672004-10-11 11:00:30 +0000971ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000972{
973 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000974 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000975
976 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000977 if (DISTRIBUTE_NAME (ospf, type))
978 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000979
980 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000981 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000982
983 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000984 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000985 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000986
987 return CMD_SUCCESS;
988}
989
990int
paul6c835672004-10-11 11:00:30 +0000991ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000992{
993 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000994 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000995 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000996
997 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000998 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000999
1000 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +00001001 if (DISTRIBUTE_NAME (ospf, type))
1002 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +00001003
paul020709f2003-04-04 02:44:16 +00001004 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +00001005
1006 return CMD_SUCCESS;
1007}
1008
1009/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +00001010static int
paul718e3742002-12-13 20:15:29 +00001011ospf_distribute_list_update_timer (struct thread *thread)
1012{
1013 struct route_node *rn;
1014 struct external_info *ei;
1015 struct route_table *rt;
1016 struct ospf_lsa *lsa;
Joakim Tjernlund46154fe2010-04-14 16:01:25 +02001017 int type, default_refresh = 0;
paul020709f2003-04-04 02:44:16 +00001018 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001019
paul020709f2003-04-04 02:44:16 +00001020 ospf = ospf_lookup ();
1021 if (ospf == NULL)
1022 return 0;
1023
paul68980082003-03-25 05:07:42 +00001024 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +00001025
1026 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
1027
1028 /* foreach all external info. */
Joakim Tjernlund274d3f02010-04-14 11:05:27 +02001029 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
1030 {
1031 rt = EXTERNAL_INFO (type);
1032 if (!rt)
1033 continue;
1034 for (rn = route_top (rt); rn; rn = route_next (rn))
1035 if ((ei = rn->info) != NULL)
1036 {
1037 if (is_prefix_default (&ei->p))
Joakim Tjernlund46154fe2010-04-14 16:01:25 +02001038 default_refresh = 1;
Joakim Tjernlund274d3f02010-04-14 11:05:27 +02001039 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
1040 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
1041 else
1042 ospf_external_lsa_originate (ospf, ei);
1043 }
1044 }
Joakim Tjernlund46154fe2010-04-14 16:01:25 +02001045 if (default_refresh)
1046 ospf_external_lsa_refresh_default (ospf);
paul718e3742002-12-13 20:15:29 +00001047 return 0;
1048}
1049
paul718e3742002-12-13 20:15:29 +00001050/* Update distribute-list and set timer to apply access-list. */
1051void
Andrew Certain0798cee2012-12-04 13:43:42 -08001052ospf_distribute_list_update (struct ospf *ospf, uintptr_t type)
paul718e3742002-12-13 20:15:29 +00001053{
1054 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +00001055
paul718e3742002-12-13 20:15:29 +00001056 /* External info does not exist. */
1057 if (!(rt = EXTERNAL_INFO (type)))
1058 return;
1059
Joakim Tjernlund45acaa02010-04-14 11:05:28 +02001060 /* If exists previously invoked thread, then let it continue. */
paul68980082003-03-25 05:07:42 +00001061 if (ospf->t_distribute_update)
Joakim Tjernlund45acaa02010-04-14 11:05:28 +02001062 return;
paul718e3742002-12-13 20:15:29 +00001063
1064 /* Set timer. */
paul68980082003-03-25 05:07:42 +00001065 ospf->t_distribute_update =
Michael Rossberg2ef762e2015-07-27 07:56:25 +02001066 thread_add_timer_msec (master, ospf_distribute_list_update_timer,
1067 (void *) type, ospf->min_ls_interval);
paul718e3742002-12-13 20:15:29 +00001068}
1069
1070/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +00001071static void
paul718e3742002-12-13 20:15:29 +00001072ospf_filter_update (struct access_list *access)
1073{
paul020709f2003-04-04 02:44:16 +00001074 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001075 int type;
1076 int abr_inv = 0;
1077 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001078 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001079
1080 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +00001081 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001082 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001083 return;
1084
paul718e3742002-12-13 20:15:29 +00001085 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +00001086 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +00001087 {
paul020709f2003-04-04 02:44:16 +00001088 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001089 {
1090 /* if route-map is not NULL it may be using this access list */
1091 ospf_distribute_list_update (ospf, type);
1092 continue;
1093 }
1094
hasso01018ce2005-08-05 07:40:15 +00001095 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1096 * but no distribute list. */
1097 if (type == ZEBRA_ROUTE_MAX)
1098 break;
paul718e3742002-12-13 20:15:29 +00001099
paul020709f2003-04-04 02:44:16 +00001100 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001101 {
1102 /* Keep old access-list for distribute-list. */
1103 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1104
1105 /* Update access-list for distribute-list. */
1106 DISTRIBUTE_LIST (ospf, type) =
1107 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1108
1109 /* No update for this distribute type. */
1110 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1111 continue;
1112
1113 /* Schedule distribute-list update timer. */
1114 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1115 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1116 ospf_distribute_list_update (ospf, type);
1117 }
paul718e3742002-12-13 20:15:29 +00001118 }
1119
1120 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001121 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1122 {
1123 if (EXPORT_NAME (area))
1124 {
1125 EXPORT_LIST (area) = NULL;
1126 abr_inv++;
1127 }
paul718e3742002-12-13 20:15:29 +00001128
paul1eb8ef22005-04-07 07:30:20 +00001129 if (IMPORT_NAME (area))
1130 {
1131 IMPORT_LIST (area) = NULL;
1132 abr_inv++;
1133 }
1134 }
paul718e3742002-12-13 20:15:29 +00001135
1136 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001137 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001138 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001139}
hassodd669bb2004-05-10 07:43:59 +00001140
1141/* If prefix-list is updated, do some updates. */
1142void
1143ospf_prefix_list_update (struct prefix_list *plist)
1144{
1145 struct ospf *ospf;
1146 int type;
1147 int abr_inv = 0;
1148 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001149 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001150
1151 /* If OSPF instatnce does not exist, return right now. */
1152 ospf = ospf_lookup ();
1153 if (ospf == NULL)
1154 return;
1155
1156 /* Update all route-maps which are used as redistribution filters.
1157 * They might use prefix-list.
1158 */
hasso01018ce2005-08-05 07:40:15 +00001159 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001160 {
1161 if (ROUTEMAP (ospf, type) != NULL)
1162 {
1163 /* If route-map is not NULL it may be using this prefix list */
1164 ospf_distribute_list_update (ospf, type);
1165 continue;
1166 }
1167 }
1168
1169 /* Update area filter-lists. */
paul1eb8ef22005-04-07 07:30:20 +00001170 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1171 {
1172 /* Update filter-list in. */
1173 if (PREFIX_NAME_IN (area))
David Lampartere66cbd12015-04-13 10:21:34 +02001174 if (strcmp (PREFIX_NAME_IN (area), prefix_list_name (plist)) == 0)
paul1eb8ef22005-04-07 07:30:20 +00001175 {
1176 PREFIX_LIST_IN (area) =
1177 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1178 abr_inv++;
1179 }
hassodd669bb2004-05-10 07:43:59 +00001180
paul1eb8ef22005-04-07 07:30:20 +00001181 /* Update filter-list out. */
1182 if (PREFIX_NAME_OUT (area))
David Lampartere66cbd12015-04-13 10:21:34 +02001183 if (strcmp (PREFIX_NAME_OUT (area), prefix_list_name (plist)) == 0)
paul1eb8ef22005-04-07 07:30:20 +00001184 {
1185 PREFIX_LIST_IN (area) =
1186 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1187 abr_inv++;
1188 }
1189 }
hassodd669bb2004-05-10 07:43:59 +00001190
1191 /* Schedule ABR task. */
1192 if (IS_OSPF_ABR (ospf) && abr_inv)
1193 ospf_schedule_abr_task (ospf);
1194}
paulcf795c52003-06-19 02:13:25 +00001195
paul4dadc292005-05-06 21:37:42 +00001196static struct ospf_distance *
1197ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001198{
Stephen Hemminger393deb92008-08-18 14:13:29 -07001199 return XCALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
paul718e3742002-12-13 20:15:29 +00001200}
1201
paul4dadc292005-05-06 21:37:42 +00001202static void
paul718e3742002-12-13 20:15:29 +00001203ospf_distance_free (struct ospf_distance *odistance)
1204{
1205 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1206}
1207
1208int
paul6c835672004-10-11 11:00:30 +00001209ospf_distance_set (struct vty *vty, struct ospf *ospf,
1210 const char *distance_str,
1211 const char *ip_str,
1212 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001213{
1214 int ret;
1215 struct prefix_ipv4 p;
1216 u_char distance;
1217 struct route_node *rn;
1218 struct ospf_distance *odistance;
1219
1220 ret = str2prefix_ipv4 (ip_str, &p);
1221 if (ret == 0)
1222 {
1223 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1224 return CMD_WARNING;
1225 }
1226
1227 distance = atoi (distance_str);
1228
1229 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001230 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001231 if (rn->info)
1232 {
1233 odistance = rn->info;
1234 route_unlock_node (rn);
1235 }
1236 else
1237 {
1238 odistance = ospf_distance_new ();
1239 rn->info = odistance;
1240 }
1241
1242 /* Set distance value. */
1243 odistance->distance = distance;
1244
1245 /* Reset access-list configuration. */
1246 if (odistance->access_list)
1247 {
1248 free (odistance->access_list);
1249 odistance->access_list = NULL;
1250 }
1251 if (access_list_str)
1252 odistance->access_list = strdup (access_list_str);
1253
1254 return CMD_SUCCESS;
1255}
1256
1257int
paul6c835672004-10-11 11:00:30 +00001258ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1259 const char *distance_str,
1260 const char *ip_str, char
1261 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001262{
1263 int ret;
1264 struct prefix_ipv4 p;
paul718e3742002-12-13 20:15:29 +00001265 struct route_node *rn;
1266 struct ospf_distance *odistance;
1267
1268 ret = str2prefix_ipv4 (ip_str, &p);
1269 if (ret == 0)
1270 {
1271 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1272 return CMD_WARNING;
1273 }
1274
paulcf795c52003-06-19 02:13:25 +00001275 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1276 if (!rn)
paul718e3742002-12-13 20:15:29 +00001277 {
1278 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1279 return CMD_WARNING;
1280 }
1281
1282 odistance = rn->info;
1283
1284 if (odistance->access_list)
1285 free (odistance->access_list);
1286 ospf_distance_free (odistance);
1287
1288 rn->info = NULL;
1289 route_unlock_node (rn);
1290 route_unlock_node (rn);
1291
1292 return CMD_SUCCESS;
1293}
1294
1295void
paul68980082003-03-25 05:07:42 +00001296ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001297{
1298 struct route_node *rn;
1299 struct ospf_distance *odistance;
1300
paul68980082003-03-25 05:07:42 +00001301 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001302 if ((odistance = rn->info) != NULL)
1303 {
paulcf795c52003-06-19 02:13:25 +00001304 if (odistance->access_list)
1305 free (odistance->access_list);
1306 ospf_distance_free (odistance);
1307 rn->info = NULL;
1308 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001309 }
1310}
1311
1312u_char
1313ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1314{
paul020709f2003-04-04 02:44:16 +00001315 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001316
paul020709f2003-04-04 02:44:16 +00001317 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001318 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001319 return 0;
1320
paul68980082003-03-25 05:07:42 +00001321 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001322 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001323 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001324
paul68980082003-03-25 05:07:42 +00001325 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001326 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001327 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001328
paul68980082003-03-25 05:07:42 +00001329 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001330 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001331 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001332 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001333
paul68980082003-03-25 05:07:42 +00001334 if (ospf->distance_all)
1335 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001336
1337 return 0;
1338}
1339
Feng Luc99f3482014-10-16 09:52:36 +08001340static void
1341ospf_zebra_connected (struct zclient *zclient)
1342{
1343 zclient_send_requests (zclient, VRF_DEFAULT);
1344}
1345
paul718e3742002-12-13 20:15:29 +00001346void
Donald Sharp71252932015-09-24 09:25:19 -04001347ospf_zebra_init (struct thread_master *master)
paul718e3742002-12-13 20:15:29 +00001348{
1349 /* Allocate zebra structure. */
Donald Sharp71252932015-09-24 09:25:19 -04001350 zclient = zclient_new (master);
paul718e3742002-12-13 20:15:29 +00001351 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
Feng Luc99f3482014-10-16 09:52:36 +08001352 zclient->zebra_connected = ospf_zebra_connected;
hasso18a6dce2004-10-03 18:18:34 +00001353 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001354 zclient->interface_add = ospf_interface_add;
1355 zclient->interface_delete = ospf_interface_delete;
1356 zclient->interface_up = ospf_interface_state_up;
1357 zclient->interface_down = ospf_interface_state_down;
1358 zclient->interface_address_add = ospf_interface_address_add;
1359 zclient->interface_address_delete = ospf_interface_address_delete;
Olivier Dugeon29a14012016-04-19 18:42:40 +02001360 zclient->interface_link_params = ospf_interface_link_params;
1361
paul718e3742002-12-13 20:15:29 +00001362 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1363 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1364
1365 access_list_add_hook (ospf_filter_update);
1366 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001367 prefix_list_add_hook (ospf_prefix_list_update);
1368 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001369}