blob: bffa90dbad96095e2736ed473f9ec6591770a3e0 [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 */
51
52/* Zebra structure to hold current status. */
53struct zclient *zclient = NULL;
54
55/* For registering threads. */
56extern struct thread_master *master;
hasso18a6dce2004-10-03 18:18:34 +000057struct in_addr router_id_zebra;
58
59/* Router-id update message from zebra. */
paul4dadc292005-05-06 21:37:42 +000060static int
hasso18a6dce2004-10-03 18:18:34 +000061ospf_router_id_update_zebra (int command, struct zclient *zclient,
62 zebra_size_t length)
63{
64 struct ospf *ospf;
65 struct prefix router_id;
66 zebra_router_id_update_read(zclient->ibuf,&router_id);
67
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +000068 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
69 {
70 char buf[128];
71 prefix2str(&router_id, buf, sizeof(buf));
72 zlog_debug("Zebra rcvd: router id update %s", buf);
73 }
74
hasso18a6dce2004-10-03 18:18:34 +000075 router_id_zebra = router_id.u.prefix4;
76
77 ospf = ospf_lookup ();
paulb29800a2005-11-20 14:50:45 +000078
hasso18a6dce2004-10-03 18:18:34 +000079 if (ospf != NULL)
paulb29800a2005-11-20 14:50:45 +000080 ospf_router_id_update (ospf);
81
hasso18a6dce2004-10-03 18:18:34 +000082 return 0;
83}
paul718e3742002-12-13 20:15:29 +000084
85/* Inteface addition message from zebra. */
paul4dadc292005-05-06 21:37:42 +000086static int
paul718e3742002-12-13 20:15:29 +000087ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
88{
89 struct interface *ifp;
90
91 ifp = zebra_interface_add_read (zclient->ibuf);
92
93 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
Stephen Hemminger30d20592009-07-28 11:58:51 +010094 zlog_debug ("Zebra: interface add %s index %d flags %llx metric %d mtu %d",
95 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
96 ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000097
paulcf795c52003-06-19 02:13:25 +000098 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000099
paul718e3742002-12-13 20:15:29 +0000100 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
101 {
102 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +0000103 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +0000104 }
105
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100106 ospf_if_update (NULL, ifp);
paul718e3742002-12-13 20:15:29 +0000107
108#ifdef HAVE_SNMP
109 ospf_snmp_if_update (ifp);
110#endif /* HAVE_SNMP */
111
112 return 0;
113}
114
paul4dadc292005-05-06 21:37:42 +0000115static int
paul718e3742002-12-13 20:15:29 +0000116ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000117 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000118{
119 struct interface *ifp;
120 struct stream *s;
121 struct route_node *rn;
122
paulcf795c52003-06-19 02:13:25 +0000123 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000124 /* zebra_interface_state_read() updates interface structure in iflist */
125 ifp = zebra_interface_state_read (s);
126
127 if (ifp == NULL)
128 return 0;
129
130 if (if_is_up (ifp))
131 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000132 ifp->name);
133
paul718e3742002-12-13 20:15:29 +0000134 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000135 zlog_debug
Stephen Hemminger30d20592009-07-28 11:58:51 +0100136 ("Zebra: interface delete %s index %d flags %lld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +0000137 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000138
139#ifdef HAVE_SNMP
140 ospf_snmp_if_delete (ifp);
141#endif /* HAVE_SNMP */
142
143 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
144 if (rn->info)
145 ospf_if_free ((struct ospf_interface *) rn->info);
146
ajsd2fc8892005-04-02 18:38:43 +0000147 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000148 return 0;
149}
150
ajsd2fc8892005-04-02 18:38:43 +0000151static struct interface *
paul718e3742002-12-13 20:15:29 +0000152zebra_interface_if_lookup (struct stream *s)
153{
ajs21fefa92005-04-02 23:16:41 +0000154 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000155
156 /* Read interface name. */
157 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
158
ajsd2fc8892005-04-02 18:38:43 +0000159 /* And look it up. */
ajs21fefa92005-04-02 23:16:41 +0000160 return if_lookup_by_name_len(ifname_tmp,
161 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000162}
163
paul4dadc292005-05-06 21:37:42 +0000164static int
paul718e3742002-12-13 20:15:29 +0000165ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000166 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000167{
168 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000169 struct ospf_interface *oi;
170 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000171
paul718e3742002-12-13 20:15:29 +0000172 ifp = zebra_interface_if_lookup (zclient->ibuf);
173
174 if (ifp == NULL)
175 return 0;
176
177 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000178 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000179 {
180 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000181 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000182 memcpy (&if_tmp, ifp, sizeof (struct interface));
183
184 zebra_interface_if_set_value (zclient->ibuf, ifp);
185
186 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000187 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000188
189 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000190 {
191 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000192 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000193 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000194
paulcf795c52003-06-19 02:13:25 +0000195 ospf_if_recalculate_output_cost (ifp);
196 }
ajsa608bbf2005-03-29 17:03:49 +0000197
198 if (if_tmp.mtu != ifp->mtu)
199 {
200 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
201 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
202 ifp->name, if_tmp.mtu, ifp->mtu);
203
204 /* Must reset the interface (simulate down/up) when MTU changes. */
205 ospf_if_reset(ifp);
206 }
paul718e3742002-12-13 20:15:29 +0000207 return 0;
208 }
paulcf795c52003-06-19 02:13:25 +0000209
paul718e3742002-12-13 20:15:29 +0000210 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000211
paul718e3742002-12-13 20:15:29 +0000212 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000213 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000214
215 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000216 {
paulcf795c52003-06-19 02:13:25 +0000217 if ((oi = rn->info) == NULL)
218 continue;
219
paul718e3742002-12-13 20:15:29 +0000220 ospf_if_up (oi);
221 }
paulcf795c52003-06-19 02:13:25 +0000222
paul718e3742002-12-13 20:15:29 +0000223 return 0;
224}
225
paul4dadc292005-05-06 21:37:42 +0000226static int
paul718e3742002-12-13 20:15:29 +0000227ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000228 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000229{
230 struct interface *ifp;
231 struct ospf_interface *oi;
232 struct route_node *node;
233
234 ifp = zebra_interface_state_read (zclient->ibuf);
235
236 if (ifp == NULL)
237 return 0;
238
239 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000240 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000241
paulcf795c52003-06-19 02:13:25 +0000242 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000243 {
paulcf795c52003-06-19 02:13:25 +0000244 if ((oi = node->info) == NULL)
245 continue;
paul718e3742002-12-13 20:15:29 +0000246 ospf_if_down (oi);
247 }
248
249 return 0;
250}
251
paul4dadc292005-05-06 21:37:42 +0000252static int
paul718e3742002-12-13 20:15:29 +0000253ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000254 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000255{
256 struct connected *c;
257
paul0a589352004-05-08 11:48:26 +0000258 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000259
260 if (c == NULL)
261 return 0;
262
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000263 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
264 {
265 char buf[128];
266 prefix2str(c->address, buf, sizeof(buf));
267 zlog_debug("Zebra: interface %s address add %s", c->ifp->name, buf);
268 }
269
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100270 ospf_if_update (NULL, c->ifp);
paul718e3742002-12-13 20:15:29 +0000271
272#ifdef HAVE_SNMP
273 ospf_snmp_if_update (c->ifp);
274#endif /* HAVE_SNMP */
275
276 return 0;
277}
278
paul4dadc292005-05-06 21:37:42 +0000279static int
paul718e3742002-12-13 20:15:29 +0000280ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000281 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000282{
283 struct connected *c;
284 struct interface *ifp;
285 struct ospf_interface *oi;
286 struct route_node *rn;
287 struct prefix p;
288
paul0a589352004-05-08 11:48:26 +0000289 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000290
291 if (c == NULL)
292 return 0;
293
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000294 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
295 {
296 char buf[128];
297 prefix2str(c->address, buf, sizeof(buf));
298 zlog_debug("Zebra: interface %s address delete %s", c->ifp->name, buf);
299 }
300
paul718e3742002-12-13 20:15:29 +0000301 ifp = c->ifp;
302 p = *c->address;
303 p.prefixlen = IPV4_MAX_PREFIXLEN;
304
305 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000306 if (!rn)
paul98429f62006-01-10 22:11:54 +0000307 {
308 connected_free (c);
309 return 0;
310 }
paul718e3742002-12-13 20:15:29 +0000311
312 assert (rn->info);
313 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000314
paul718e3742002-12-13 20:15:29 +0000315 /* Call interface hook functions to clean up */
316 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000317
paul718e3742002-12-13 20:15:29 +0000318#ifdef HAVE_SNMP
319 ospf_snmp_if_update (c->ifp);
320#endif /* HAVE_SNMP */
321
322 connected_free (c);
323
paul718e3742002-12-13 20:15:29 +0000324 return 0;
325}
paul72357f22003-06-19 02:11:23 +0000326
paul718e3742002-12-13 20:15:29 +0000327void
328ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
329{
330 u_char message;
331 u_char distance;
332 u_char flags;
333 int psize;
334 struct stream *s;
335 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000336 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000337
338 if (zclient->redist[ZEBRA_ROUTE_OSPF])
339 {
340 message = 0;
341 flags = 0;
342
343 /* OSPF pass nexthop and metric */
344 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
345 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
346
347 /* Distance value. */
348 distance = ospf_distance_apply (p, or);
349 if (distance)
paul72357f22003-06-19 02:11:23 +0000350 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000351
352 /* Make packet. */
353 s = zclient->obuf;
354 stream_reset (s);
355
paul718e3742002-12-13 20:15:29 +0000356 /* Put command, type, flags, message. */
paulc6371712006-01-17 17:49:53 +0000357 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD);
paul718e3742002-12-13 20:15:29 +0000358 stream_putc (s, ZEBRA_ROUTE_OSPF);
359 stream_putc (s, flags);
360 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000361
paul718e3742002-12-13 20:15:29 +0000362 /* Put prefix information. */
363 psize = PSIZE (p->prefixlen);
364 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000365 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000366
367 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000368 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000369
370 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000371 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000372 {
paulcf795c52003-06-19 02:13:25 +0000373 if (path->nexthop.s_addr != INADDR_ANY)
374 {
375 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
376 stream_put_in_addr (s, &path->nexthop);
377 }
378 else
379 {
380 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +0200381 if (path->ifindex)
382 stream_putl (s, path->ifindex);
paulcf795c52003-06-19 02:13:25 +0000383 else
384 stream_putl (s, 0);
385 }
paul72357f22003-06-19 02:11:23 +0000386
387 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
388 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000389 char buf[2][INET_ADDRSTRLEN];
390 zlog_debug("Zebra: Route add %s/%d nexthop %s",
391 inet_ntop(AF_INET, &p->prefix,
392 buf[0], sizeof(buf[0])),
393 p->prefixlen,
394 inet_ntop(AF_INET, &path->nexthop,
395 buf[1], sizeof(buf[1])));
paulcf795c52003-06-19 02:13:25 +0000396 }
397 }
paul718e3742002-12-13 20:15:29 +0000398
399 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000400 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000401 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000402 {
403 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
404 stream_putl (s, or->cost + or->u.ext.type2_cost);
405 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
406 stream_putl (s, or->u.ext.type2_cost);
407 else
408 stream_putl (s, or->cost);
409 }
paul718e3742002-12-13 20:15:29 +0000410
411 stream_putw_at (s, 0, stream_get_endp (s));
412
ajs634f9ea2005-04-11 15:51:40 +0000413 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000414 }
415}
416
417void
418ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
419{
420 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000421 struct ospf_path *path;
422 struct in_addr *nexthop;
paul1eb8ef22005-04-07 07:30:20 +0000423 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000424
425 if (zclient->redist[ZEBRA_ROUTE_OSPF])
426 {
427 api.type = ZEBRA_ROUTE_OSPF;
428 api.flags = 0;
429 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000430 api.ifindex_num = 0;
431 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000432
paul1eb8ef22005-04-07 07:30:20 +0000433 for (ALL_LIST_ELEMENTS (or->paths, node, nnode, path))
paulcf795c52003-06-19 02:13:25 +0000434 {
paulcf795c52003-06-19 02:13:25 +0000435 if (path->nexthop.s_addr != INADDR_ANY)
436 {
pauld8e1d6b2003-08-10 04:04:41 +0000437 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000438 api.nexthop_num = 1;
439 nexthop = &path->nexthop;
440 api.nexthop = &nexthop;
441 }
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +0200442 else if (if_lookup_by_index(path->ifindex))
paulbb8ff1e2003-08-12 06:00:30 +0000443 {
444 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
445 api.ifindex_num = 1;
Joakim Tjernlunda8ba8472009-07-27 12:42:34 +0200446 api.ifindex = &path->ifindex;
paulbb8ff1e2003-08-12 06:00:30 +0000447 }
448 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
449 {
ajs9b0e25c2004-12-08 19:06:51 +0000450 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000451 inet_ntoa(p->prefix),
452 p->prefixlen);
453 }
paul72357f22003-06-19 02:11:23 +0000454
paul0a589352004-05-08 11:48:26 +0000455 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000456
457 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000458 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000459 char buf[2][INET_ADDRSTRLEN];
460 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
461 inet_ntop(AF_INET, &p->prefix, buf[0], sizeof(buf[0])),
462 p->prefixlen,
463 inet_ntop(AF_INET, *api.nexthop,
464 buf[1], sizeof(buf[1])));
paulcf795c52003-06-19 02:13:25 +0000465 }
paulbb8ff1e2003-08-12 06:00:30 +0000466 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
467 {
ajs9b0e25c2004-12-08 19:06:51 +0000468 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000469 inet_ntoa (p->prefix),
470 p->prefixlen, *api.ifindex);
471 }
paulcf795c52003-06-19 02:13:25 +0000472 }
paul718e3742002-12-13 20:15:29 +0000473 }
474}
475
476void
477ospf_zebra_add_discard (struct prefix_ipv4 *p)
478{
479 struct zapi_ipv4 api;
480
481 if (zclient->redist[ZEBRA_ROUTE_OSPF])
482 {
483 api.type = ZEBRA_ROUTE_OSPF;
484 api.flags = ZEBRA_FLAG_BLACKHOLE;
485 api.message = 0;
486 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
487 api.nexthop_num = 0;
488 api.ifindex_num = 0;
489
paul0a589352004-05-08 11:48:26 +0000490 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000491
492 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
493 zlog_debug ("Zebra: Route add discard %s/%d",
494 inet_ntoa (p->prefix), p->prefixlen);
paul718e3742002-12-13 20:15:29 +0000495 }
496}
497
498void
499ospf_zebra_delete_discard (struct prefix_ipv4 *p)
500{
501 struct zapi_ipv4 api;
502
503 if (zclient->redist[ZEBRA_ROUTE_OSPF])
504 {
505 api.type = ZEBRA_ROUTE_OSPF;
506 api.flags = ZEBRA_FLAG_BLACKHOLE;
507 api.message = 0;
508 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
509 api.nexthop_num = 0;
510 api.ifindex_num = 0;
511
paul0a589352004-05-08 11:48:26 +0000512 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000513
514 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000515 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000516 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000517
paul718e3742002-12-13 20:15:29 +0000518 }
519}
520
521int
522ospf_is_type_redistributed (int type)
523{
524 return (DEFAULT_ROUTE_TYPE (type)) ?
525 zclient->default_information : zclient->redist[type];
526}
527
528int
paul020709f2003-04-04 02:44:16 +0000529ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000530{
531 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000532
paul718e3742002-12-13 20:15:29 +0000533 if (ospf_is_type_redistributed (type))
534 {
paul68980082003-03-25 05:07:42 +0000535 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000536 {
537 ospf->dmetric[type].type = mtype;
538 force = LSA_REFRESH_FORCE;
539 }
paul68980082003-03-25 05:07:42 +0000540 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000541 {
542 ospf->dmetric[type].value = mvalue;
543 force = LSA_REFRESH_FORCE;
544 }
545
paul68980082003-03-25 05:07:42 +0000546 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000547
paul718e3742002-12-13 20:15:29 +0000548 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000549 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000550 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000551 metric_type (ospf, type), metric_value (ospf, type));
552
paul718e3742002-12-13 20:15:29 +0000553 return CMD_SUCCESS;
554 }
555
paul68980082003-03-25 05:07:42 +0000556 ospf->dmetric[type].type = mtype;
557 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000558
paul0a589352004-05-08 11:48:26 +0000559 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000560
561 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000562 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000563 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000564 metric_type (ospf, type), metric_value (ospf, type));
565
paul68980082003-03-25 05:07:42 +0000566 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000567
568 return CMD_SUCCESS;
569}
570
571int
paul020709f2003-04-04 02:44:16 +0000572ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000573{
574 if (type == zclient->redist_default)
575 return CMD_SUCCESS;
576
paulcf795c52003-06-19 02:13:25 +0000577 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000578 return CMD_SUCCESS;
579
paul0a589352004-05-08 11:48:26 +0000580 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000581
paul718e3742002-12-13 20:15:29 +0000582 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000583 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000584 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000585
paul68980082003-03-25 05:07:42 +0000586 ospf->dmetric[type].type = -1;
587 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000588
589 /* Remove the routes from OSPF table. */
Paul Jakma6db3a6f2006-05-12 23:02:46 +0000590 ospf_redistribute_withdraw (ospf, type);
paul718e3742002-12-13 20:15:29 +0000591
paul68980082003-03-25 05:07:42 +0000592 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000593
594 return CMD_SUCCESS;
595}
596
597int
paul020709f2003-04-04 02:44:16 +0000598ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000599 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000600{
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000601 ospf->default_originate = originate;
602 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
603 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul020709f2003-04-04 02:44:16 +0000604
paul718e3742002-12-13 20:15:29 +0000605 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
606 {
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000607 /* if ospf->default_originate changes value, is calling
608 ospf_external_lsa_refresh_default sufficient to implement
609 the change? */
paul68980082003-03-25 05:07:42 +0000610 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000611
paul718e3742002-12-13 20:15:29 +0000612 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000613 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000614 ospf_redist_string(DEFAULT_ROUTE),
paulcf795c52003-06-19 02:13:25 +0000615 metric_type (ospf, DEFAULT_ROUTE),
616 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000617 return CMD_SUCCESS;
618 }
619
paul0a589352004-05-08 11:48:26 +0000620 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000621
paul718e3742002-12-13 20:15:29 +0000622 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000623 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000624 metric_type (ospf, DEFAULT_ROUTE),
625 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000626
paul68980082003-03-25 05:07:42 +0000627 if (ospf->router_id.s_addr == 0)
628 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000629 else
Paul Jakma4021b602006-05-12 22:55:41 +0000630 thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
paul718e3742002-12-13 20:15:29 +0000631
paul68980082003-03-25 05:07:42 +0000632 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000633
634 return CMD_SUCCESS;
635}
636
637int
paul020709f2003-04-04 02:44:16 +0000638ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000639{
640 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
641 return CMD_SUCCESS;
642
paul68980082003-03-25 05:07:42 +0000643 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
644 ospf->dmetric[DEFAULT_ROUTE].type = -1;
645 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000646
paul0a589352004-05-08 11:48:26 +0000647 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000648
649 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000650 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000651
paul68980082003-03-25 05:07:42 +0000652 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000653
654 return CMD_SUCCESS;
655}
656
paul4dadc292005-05-06 21:37:42 +0000657static int
paul020709f2003-04-04 02:44:16 +0000658ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000659 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000660{
661 /* If prefix is multicast, then do not originate LSA. */
662 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
663 {
664 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000665 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000666 return 0;
667 }
668
669 /* Take care of default-originate. */
670 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000671 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000672 {
Denis Ovsienkoe00d4e22011-09-10 23:29:19 +0400673 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
paulcf795c52003-06-19 02:13:25 +0000674 "for default");
675 return 0;
paul718e3742002-12-13 20:15:29 +0000676 }
677
678 return 1;
679}
680
681/* If connected prefix is OSPF enable interface, then do not announce. */
682int
paulcf795c52003-06-19 02:13:25 +0000683ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000684{
Joakim Tjernlund5d8de932009-08-07 13:48:15 +0200685 struct listnode *node;
686 struct ospf_interface *oi;
paul718e3742002-12-13 20:15:29 +0000687
Joakim Tjernlund5d8de932009-08-07 13:48:15 +0200688
689 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
690 if (prefix_match (oi->address, (struct prefix *) &ei->p))
paulcf795c52003-06-19 02:13:25 +0000691 return 0;
paul718e3742002-12-13 20:15:29 +0000692 return 1;
693}
694
695/* return 1 if external LSA must be originated, 0 otherwise */
696int
paul68980082003-03-25 05:07:42 +0000697ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000698 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000699{
700 struct route_map_set_values save_values;
701 struct prefix_ipv4 *p = &ei->p;
702 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000703
paul718e3742002-12-13 20:15:29 +0000704 if (changed)
705 *changed = 0;
706
paul020709f2003-04-04 02:44:16 +0000707 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000708 return 0;
709
710 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000711 if (type == ZEBRA_ROUTE_CONNECT &&
712 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000713 return 0;
714
paul020709f2003-04-04 02:44:16 +0000715 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000716 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000717 if (DISTRIBUTE_LIST (ospf, type))
718 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000719 {
720 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000721 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000722 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000723 inet_ntoa (p->prefix), p->prefixlen);
724 return 0;
725 }
paul718e3742002-12-13 20:15:29 +0000726
727 save_values = ei->route_map_set;
728 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000729
paul718e3742002-12-13 20:15:29 +0000730 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000731 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000732 {
733 int ret;
734
paulcf795c52003-06-19 02:13:25 +0000735 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
736 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000737
738 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000739 {
740 ei->route_map_set = save_values;
741 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000742 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000743 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000744 inet_ntoa (p->prefix), p->prefixlen);
745 return 0;
746 }
747
paul718e3742002-12-13 20:15:29 +0000748 /* check if 'route-map set' changed something */
749 if (changed)
paulcf795c52003-06-19 02:13:25 +0000750 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
751 &save_values);
paul718e3742002-12-13 20:15:29 +0000752 }
753
754 return 1;
755}
756
757/* OSPF route-map set for redistribution */
758void
paul6c835672004-10-11 11:00:30 +0000759ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000760{
paul020709f2003-04-04 02:44:16 +0000761 if (ROUTEMAP_NAME (ospf, type))
762 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000763
paul020709f2003-04-04 02:44:16 +0000764 ROUTEMAP_NAME (ospf, type) = strdup (name);
765 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000766}
767
768void
paul020709f2003-04-04 02:44:16 +0000769ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000770{
paul020709f2003-04-04 02:44:16 +0000771 if (ROUTEMAP_NAME (ospf, type))
772 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000773
paul020709f2003-04-04 02:44:16 +0000774 ROUTEMAP_NAME (ospf, type) = NULL;
775 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000776}
777
778/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000779static int
paul718e3742002-12-13 20:15:29 +0000780ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000781 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000782{
783 struct stream *s;
784 struct zapi_ipv4 api;
785 unsigned long ifindex;
786 struct in_addr nexthop;
787 struct prefix_ipv4 p;
788 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000789 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000790
791 s = zclient->ibuf;
792 ifindex = 0;
793 nexthop.s_addr = 0;
794
795 /* Type, flags, message. */
796 api.type = stream_getc (s);
797 api.flags = stream_getc (s);
798 api.message = stream_getc (s);
799
800 /* IPv4 prefix. */
801 memset (&p, 0, sizeof (struct prefix_ipv4));
802 p.family = AF_INET;
803 p.prefixlen = stream_getc (s);
804 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
805
hasso8585d4e2004-04-20 17:25:12 +0000806 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
807 return 0;
808
paul718e3742002-12-13 20:15:29 +0000809 /* Nexthop, ifindex, distance, metric. */
810 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
811 {
812 api.nexthop_num = stream_getc (s);
813 nexthop.s_addr = stream_get_ipv4 (s);
814 }
815 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
816 {
817 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000818 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000819 ifindex = stream_getl (s);
820 }
821 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
822 api.distance = stream_getc (s);
823 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
824 api.metric = stream_getl (s);
825
paul020709f2003-04-04 02:44:16 +0000826 ospf = ospf_lookup ();
827 if (ospf == NULL)
828 return 0;
829
paul718e3742002-12-13 20:15:29 +0000830 if (command == ZEBRA_IPV4_ROUTE_ADD)
831 {
paul7021c422003-07-15 12:52:22 +0000832 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000833 * Maybe we should ignore reject/blackhole routes? Testing shows that
834 * there is no problems though and this is only way to "summarize"
835 * routes in ASBR at the moment. Maybe we need just a better generalised
836 * solution for these types?
837 *
838 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
839 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
840 * return 0;
paul7021c422003-07-15 12:52:22 +0000841 */
paul7021c422003-07-15 12:52:22 +0000842
paul718e3742002-12-13 20:15:29 +0000843 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
844
paul68980082003-03-25 05:07:42 +0000845 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000846 /* Set flags to generate AS-external-LSA originate event
847 for each redistributed protocols later. */
848 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000849 else
paulcf795c52003-06-19 02:13:25 +0000850 {
851 if (ei)
852 {
853 if (is_prefix_default (&p))
854 ospf_external_lsa_refresh_default (ospf);
855 else
856 {
857 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000858
paulcf795c52003-06-19 02:13:25 +0000859 current = ospf_external_info_find_lsa (ospf, &ei->p);
860 if (!current)
861 ospf_external_lsa_originate (ospf, ei);
862 else if (IS_LSA_MAXAGE (current))
863 ospf_external_lsa_refresh (ospf, current,
864 ei, LSA_REFRESH_FORCE);
865 else
866 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
867 inet_ntoa (p.prefix));
868 }
869 }
870 }
paul718e3742002-12-13 20:15:29 +0000871 }
paulcf795c52003-06-19 02:13:25 +0000872 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000873 {
874 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000875 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000876 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000877 else
ajs5339cfd2005-09-19 13:28:05 +0000878 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000879 }
880
881 return 0;
882}
paul718e3742002-12-13 20:15:29 +0000883
paulcf795c52003-06-19 02:13:25 +0000884
paul718e3742002-12-13 20:15:29 +0000885int
paul6c835672004-10-11 11:00:30 +0000886ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000887{
888 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000889 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000890
891 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000892 if (DISTRIBUTE_NAME (ospf, type))
893 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000894
895 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000896 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000897
898 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000899 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000900 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000901
902 return CMD_SUCCESS;
903}
904
905int
paul6c835672004-10-11 11:00:30 +0000906ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000907{
908 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000909 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000910 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000911
912 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000913 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000914
915 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000916 if (DISTRIBUTE_NAME (ospf, type))
917 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000918
paul020709f2003-04-04 02:44:16 +0000919 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000920
921 return CMD_SUCCESS;
922}
923
924/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +0000925static int
paul718e3742002-12-13 20:15:29 +0000926ospf_distribute_list_update_timer (struct thread *thread)
927{
928 struct route_node *rn;
929 struct external_info *ei;
930 struct route_table *rt;
931 struct ospf_lsa *lsa;
Joakim Tjernlund46154fe2010-04-14 16:01:25 +0200932 int type, default_refresh = 0;
paul020709f2003-04-04 02:44:16 +0000933 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000934
paul020709f2003-04-04 02:44:16 +0000935 ospf = ospf_lookup ();
936 if (ospf == NULL)
937 return 0;
938
paul68980082003-03-25 05:07:42 +0000939 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000940
941 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
942
943 /* foreach all external info. */
Joakim Tjernlund274d3f02010-04-14 11:05:27 +0200944 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
945 {
946 rt = EXTERNAL_INFO (type);
947 if (!rt)
948 continue;
949 for (rn = route_top (rt); rn; rn = route_next (rn))
950 if ((ei = rn->info) != NULL)
951 {
952 if (is_prefix_default (&ei->p))
Joakim Tjernlund46154fe2010-04-14 16:01:25 +0200953 default_refresh = 1;
Joakim Tjernlund274d3f02010-04-14 11:05:27 +0200954 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
955 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
956 else
957 ospf_external_lsa_originate (ospf, ei);
958 }
959 }
Joakim Tjernlund46154fe2010-04-14 16:01:25 +0200960 if (default_refresh)
961 ospf_external_lsa_refresh_default (ospf);
paul718e3742002-12-13 20:15:29 +0000962 return 0;
963}
964
965#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
966
967/* Update distribute-list and set timer to apply access-list. */
968void
paul68980082003-03-25 05:07:42 +0000969ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000970{
971 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000972
paul718e3742002-12-13 20:15:29 +0000973 /* External info does not exist. */
974 if (!(rt = EXTERNAL_INFO (type)))
975 return;
976
Joakim Tjernlund45acaa02010-04-14 11:05:28 +0200977 /* If exists previously invoked thread, then let it continue. */
paul68980082003-03-25 05:07:42 +0000978 if (ospf->t_distribute_update)
Joakim Tjernlund45acaa02010-04-14 11:05:28 +0200979 return;
paul718e3742002-12-13 20:15:29 +0000980
981 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000982 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000983 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000984 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000985}
986
987/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +0000988static void
paul718e3742002-12-13 20:15:29 +0000989ospf_filter_update (struct access_list *access)
990{
paul020709f2003-04-04 02:44:16 +0000991 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000992 int type;
993 int abr_inv = 0;
994 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000995 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000996
997 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000998 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000999 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001000 return;
1001
paul718e3742002-12-13 20:15:29 +00001002 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +00001003 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +00001004 {
paul020709f2003-04-04 02:44:16 +00001005 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001006 {
1007 /* if route-map is not NULL it may be using this access list */
1008 ospf_distribute_list_update (ospf, type);
1009 continue;
1010 }
1011
hasso01018ce2005-08-05 07:40:15 +00001012 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1013 * but no distribute list. */
1014 if (type == ZEBRA_ROUTE_MAX)
1015 break;
paul718e3742002-12-13 20:15:29 +00001016
paul020709f2003-04-04 02:44:16 +00001017 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001018 {
1019 /* Keep old access-list for distribute-list. */
1020 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1021
1022 /* Update access-list for distribute-list. */
1023 DISTRIBUTE_LIST (ospf, type) =
1024 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1025
1026 /* No update for this distribute type. */
1027 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1028 continue;
1029
1030 /* Schedule distribute-list update timer. */
1031 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1032 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1033 ospf_distribute_list_update (ospf, type);
1034 }
paul718e3742002-12-13 20:15:29 +00001035 }
1036
1037 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001038 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1039 {
1040 if (EXPORT_NAME (area))
1041 {
1042 EXPORT_LIST (area) = NULL;
1043 abr_inv++;
1044 }
paul718e3742002-12-13 20:15:29 +00001045
paul1eb8ef22005-04-07 07:30:20 +00001046 if (IMPORT_NAME (area))
1047 {
1048 IMPORT_LIST (area) = NULL;
1049 abr_inv++;
1050 }
1051 }
paul718e3742002-12-13 20:15:29 +00001052
1053 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001054 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001055 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001056}
hassodd669bb2004-05-10 07:43:59 +00001057
1058/* If prefix-list is updated, do some updates. */
1059void
1060ospf_prefix_list_update (struct prefix_list *plist)
1061{
1062 struct ospf *ospf;
1063 int type;
1064 int abr_inv = 0;
1065 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001066 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001067
1068 /* If OSPF instatnce does not exist, return right now. */
1069 ospf = ospf_lookup ();
1070 if (ospf == NULL)
1071 return;
1072
1073 /* Update all route-maps which are used as redistribution filters.
1074 * They might use prefix-list.
1075 */
hasso01018ce2005-08-05 07:40:15 +00001076 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001077 {
1078 if (ROUTEMAP (ospf, type) != NULL)
1079 {
1080 /* If route-map is not NULL it may be using this prefix list */
1081 ospf_distribute_list_update (ospf, type);
1082 continue;
1083 }
1084 }
1085
1086 /* Update area filter-lists. */
paul1eb8ef22005-04-07 07:30:20 +00001087 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1088 {
1089 /* Update filter-list in. */
1090 if (PREFIX_NAME_IN (area))
1091 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1092 {
1093 PREFIX_LIST_IN (area) =
1094 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1095 abr_inv++;
1096 }
hassodd669bb2004-05-10 07:43:59 +00001097
paul1eb8ef22005-04-07 07:30:20 +00001098 /* Update filter-list out. */
1099 if (PREFIX_NAME_OUT (area))
1100 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1101 {
1102 PREFIX_LIST_IN (area) =
1103 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1104 abr_inv++;
1105 }
1106 }
hassodd669bb2004-05-10 07:43:59 +00001107
1108 /* Schedule ABR task. */
1109 if (IS_OSPF_ABR (ospf) && abr_inv)
1110 ospf_schedule_abr_task (ospf);
1111}
paulcf795c52003-06-19 02:13:25 +00001112
paul4dadc292005-05-06 21:37:42 +00001113static struct ospf_distance *
1114ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001115{
Stephen Hemminger393deb92008-08-18 14:13:29 -07001116 return XCALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
paul718e3742002-12-13 20:15:29 +00001117}
1118
paul4dadc292005-05-06 21:37:42 +00001119static void
paul718e3742002-12-13 20:15:29 +00001120ospf_distance_free (struct ospf_distance *odistance)
1121{
1122 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1123}
1124
1125int
paul6c835672004-10-11 11:00:30 +00001126ospf_distance_set (struct vty *vty, struct ospf *ospf,
1127 const char *distance_str,
1128 const char *ip_str,
1129 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001130{
1131 int ret;
1132 struct prefix_ipv4 p;
1133 u_char distance;
1134 struct route_node *rn;
1135 struct ospf_distance *odistance;
1136
1137 ret = str2prefix_ipv4 (ip_str, &p);
1138 if (ret == 0)
1139 {
1140 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1141 return CMD_WARNING;
1142 }
1143
1144 distance = atoi (distance_str);
1145
1146 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001147 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001148 if (rn->info)
1149 {
1150 odistance = rn->info;
1151 route_unlock_node (rn);
1152 }
1153 else
1154 {
1155 odistance = ospf_distance_new ();
1156 rn->info = odistance;
1157 }
1158
1159 /* Set distance value. */
1160 odistance->distance = distance;
1161
1162 /* Reset access-list configuration. */
1163 if (odistance->access_list)
1164 {
1165 free (odistance->access_list);
1166 odistance->access_list = NULL;
1167 }
1168 if (access_list_str)
1169 odistance->access_list = strdup (access_list_str);
1170
1171 return CMD_SUCCESS;
1172}
1173
1174int
paul6c835672004-10-11 11:00:30 +00001175ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1176 const char *distance_str,
1177 const char *ip_str, char
1178 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001179{
1180 int ret;
1181 struct prefix_ipv4 p;
1182 u_char distance;
1183 struct route_node *rn;
1184 struct ospf_distance *odistance;
1185
1186 ret = str2prefix_ipv4 (ip_str, &p);
1187 if (ret == 0)
1188 {
1189 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1190 return CMD_WARNING;
1191 }
1192
1193 distance = atoi (distance_str);
1194
paulcf795c52003-06-19 02:13:25 +00001195 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1196 if (!rn)
paul718e3742002-12-13 20:15:29 +00001197 {
1198 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1199 return CMD_WARNING;
1200 }
1201
1202 odistance = rn->info;
1203
1204 if (odistance->access_list)
1205 free (odistance->access_list);
1206 ospf_distance_free (odistance);
1207
1208 rn->info = NULL;
1209 route_unlock_node (rn);
1210 route_unlock_node (rn);
1211
1212 return CMD_SUCCESS;
1213}
1214
1215void
paul68980082003-03-25 05:07:42 +00001216ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001217{
1218 struct route_node *rn;
1219 struct ospf_distance *odistance;
1220
paul68980082003-03-25 05:07:42 +00001221 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001222 if ((odistance = rn->info) != NULL)
1223 {
paulcf795c52003-06-19 02:13:25 +00001224 if (odistance->access_list)
1225 free (odistance->access_list);
1226 ospf_distance_free (odistance);
1227 rn->info = NULL;
1228 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001229 }
1230}
1231
1232u_char
1233ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1234{
paul020709f2003-04-04 02:44:16 +00001235 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001236
paul020709f2003-04-04 02:44:16 +00001237 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001238 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001239 return 0;
1240
paul68980082003-03-25 05:07:42 +00001241 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001242 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001243 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001244
paul68980082003-03-25 05:07:42 +00001245 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001246 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001247 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001248
paul68980082003-03-25 05:07:42 +00001249 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001250 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001251 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001252 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001253
paul68980082003-03-25 05:07:42 +00001254 if (ospf->distance_all)
1255 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001256
1257 return 0;
1258}
1259
1260void
1261ospf_zebra_init ()
1262{
1263 /* Allocate zebra structure. */
1264 zclient = zclient_new ();
1265 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001266 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001267 zclient->interface_add = ospf_interface_add;
1268 zclient->interface_delete = ospf_interface_delete;
1269 zclient->interface_up = ospf_interface_state_up;
1270 zclient->interface_down = ospf_interface_state_down;
1271 zclient->interface_address_add = ospf_interface_address_add;
1272 zclient->interface_address_delete = ospf_interface_address_delete;
1273 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1274 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1275
1276 access_list_add_hook (ospf_filter_update);
1277 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001278 prefix_list_add_hook (ospf_prefix_list_update);
1279 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001280}