blob: 0d98008d08915224f946534db69ee827ed0f2b6f [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
68 router_id_zebra = router_id.u.prefix4;
69
70 ospf = ospf_lookup ();
71 if (ospf != NULL)
72 {
73 if (ospf->t_router_id_update == NULL)
74 OSPF_TIMER_ON (ospf->t_router_id_update, ospf_router_id_update_timer,
75 OSPF_ROUTER_ID_UPDATE_DELAY);
76 }
77 return 0;
78}
paul718e3742002-12-13 20:15:29 +000079
80/* Inteface addition message from zebra. */
paul4dadc292005-05-06 21:37:42 +000081static int
paul718e3742002-12-13 20:15:29 +000082ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
83{
84 struct interface *ifp;
paul020709f2003-04-04 02:44:16 +000085 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +000086
87 ifp = zebra_interface_add_read (zclient->ibuf);
88
89 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +000090 zlog_debug ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +000091 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000092
paulcf795c52003-06-19 02:13:25 +000093 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000094
paul718e3742002-12-13 20:15:29 +000095 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
96 {
97 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +000098 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +000099 }
100
paul020709f2003-04-04 02:44:16 +0000101 ospf = ospf_lookup ();
102 if (ospf != NULL)
103 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000104
105#ifdef HAVE_SNMP
106 ospf_snmp_if_update (ifp);
107#endif /* HAVE_SNMP */
108
109 return 0;
110}
111
paul4dadc292005-05-06 21:37:42 +0000112static int
paul718e3742002-12-13 20:15:29 +0000113ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000114 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000115{
116 struct interface *ifp;
117 struct stream *s;
118 struct route_node *rn;
119
paulcf795c52003-06-19 02:13:25 +0000120 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000121 /* zebra_interface_state_read() updates interface structure in iflist */
122 ifp = zebra_interface_state_read (s);
123
124 if (ifp == NULL)
125 return 0;
126
127 if (if_is_up (ifp))
128 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000129 ifp->name);
130
paul718e3742002-12-13 20:15:29 +0000131 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000132 zlog_debug
paulcf795c52003-06-19 02:13:25 +0000133 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
134 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000135
136#ifdef HAVE_SNMP
137 ospf_snmp_if_delete (ifp);
138#endif /* HAVE_SNMP */
139
140 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
141 if (rn->info)
142 ospf_if_free ((struct ospf_interface *) rn->info);
143
ajsd2fc8892005-04-02 18:38:43 +0000144 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000145 return 0;
146}
147
ajsd2fc8892005-04-02 18:38:43 +0000148static struct interface *
paul718e3742002-12-13 20:15:29 +0000149zebra_interface_if_lookup (struct stream *s)
150{
ajs21fefa92005-04-02 23:16:41 +0000151 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000152
153 /* Read interface name. */
154 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
155
ajsd2fc8892005-04-02 18:38:43 +0000156 /* And look it up. */
ajs21fefa92005-04-02 23:16:41 +0000157 return if_lookup_by_name_len(ifname_tmp,
158 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000159}
160
paul4dadc292005-05-06 21:37:42 +0000161static int
paul718e3742002-12-13 20:15:29 +0000162ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000163 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000164{
165 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000166 struct ospf_interface *oi;
167 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000168
paul718e3742002-12-13 20:15:29 +0000169 ifp = zebra_interface_if_lookup (zclient->ibuf);
170
171 if (ifp == NULL)
172 return 0;
173
174 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000175 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000176 {
177 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000178 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000179 memcpy (&if_tmp, ifp, sizeof (struct interface));
180
181 zebra_interface_if_set_value (zclient->ibuf, ifp);
182
183 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000184 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000185
186 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000187 {
188 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000189 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000190 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000191
paulcf795c52003-06-19 02:13:25 +0000192 ospf_if_recalculate_output_cost (ifp);
193 }
ajsa608bbf2005-03-29 17:03:49 +0000194
195 if (if_tmp.mtu != ifp->mtu)
196 {
197 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
198 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
199 ifp->name, if_tmp.mtu, ifp->mtu);
200
201 /* Must reset the interface (simulate down/up) when MTU changes. */
202 ospf_if_reset(ifp);
203 }
paul718e3742002-12-13 20:15:29 +0000204 return 0;
205 }
paulcf795c52003-06-19 02:13:25 +0000206
paul718e3742002-12-13 20:15:29 +0000207 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000208
paul718e3742002-12-13 20:15:29 +0000209 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000210 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000211
212 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000213 {
paulcf795c52003-06-19 02:13:25 +0000214 if ((oi = rn->info) == NULL)
215 continue;
216
paul718e3742002-12-13 20:15:29 +0000217 ospf_if_up (oi);
218 }
paulcf795c52003-06-19 02:13:25 +0000219
paul718e3742002-12-13 20:15:29 +0000220 return 0;
221}
222
paul4dadc292005-05-06 21:37:42 +0000223static int
paul718e3742002-12-13 20:15:29 +0000224ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000225 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000226{
227 struct interface *ifp;
228 struct ospf_interface *oi;
229 struct route_node *node;
230
231 ifp = zebra_interface_state_read (zclient->ibuf);
232
233 if (ifp == NULL)
234 return 0;
235
236 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000237 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000238
paulcf795c52003-06-19 02:13:25 +0000239 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000240 {
paulcf795c52003-06-19 02:13:25 +0000241 if ((oi = node->info) == NULL)
242 continue;
paul718e3742002-12-13 20:15:29 +0000243 ospf_if_down (oi);
244 }
245
246 return 0;
247}
248
paul4dadc292005-05-06 21:37:42 +0000249static int
paul718e3742002-12-13 20:15:29 +0000250ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000251 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000252{
paul020709f2003-04-04 02:44:16 +0000253 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000254 struct connected *c;
255
paul0a589352004-05-08 11:48:26 +0000256 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000257
258 if (c == NULL)
259 return 0;
260
paul020709f2003-04-04 02:44:16 +0000261 ospf = ospf_lookup ();
262 if (ospf != NULL)
263 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000264
265#ifdef HAVE_SNMP
266 ospf_snmp_if_update (c->ifp);
267#endif /* HAVE_SNMP */
268
269 return 0;
270}
271
paul4dadc292005-05-06 21:37:42 +0000272static int
paul718e3742002-12-13 20:15:29 +0000273ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000274 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000275{
paul020709f2003-04-04 02:44:16 +0000276 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000277 struct connected *c;
278 struct interface *ifp;
279 struct ospf_interface *oi;
280 struct route_node *rn;
281 struct prefix p;
282
paul0a589352004-05-08 11:48:26 +0000283 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000284
285 if (c == NULL)
286 return 0;
287
288 ifp = c->ifp;
289 p = *c->address;
290 p.prefixlen = IPV4_MAX_PREFIXLEN;
291
292 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000293 if (!rn)
paul718e3742002-12-13 20:15:29 +0000294 return 0;
295
296 assert (rn->info);
297 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000298
paul718e3742002-12-13 20:15:29 +0000299 /* Call interface hook functions to clean up */
300 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000301
paul718e3742002-12-13 20:15:29 +0000302#ifdef HAVE_SNMP
303 ospf_snmp_if_update (c->ifp);
304#endif /* HAVE_SNMP */
305
306 connected_free (c);
307
paul020709f2003-04-04 02:44:16 +0000308 ospf = ospf_lookup ();
309 if (ospf != NULL)
310 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000311
312 return 0;
313}
paul72357f22003-06-19 02:11:23 +0000314
paul718e3742002-12-13 20:15:29 +0000315void
316ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
317{
318 u_char message;
319 u_char distance;
320 u_char flags;
321 int psize;
322 struct stream *s;
323 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000324 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000325
326 if (zclient->redist[ZEBRA_ROUTE_OSPF])
327 {
328 message = 0;
329 flags = 0;
330
331 /* OSPF pass nexthop and metric */
332 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
333 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
334
335 /* Distance value. */
336 distance = ospf_distance_apply (p, or);
337 if (distance)
paul72357f22003-06-19 02:11:23 +0000338 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000339
340 /* Make packet. */
341 s = zclient->obuf;
342 stream_reset (s);
343
344 /* Length place holder. */
345 stream_putw (s, 0);
346
347 /* Put command, type, flags, message. */
348 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
349 stream_putc (s, ZEBRA_ROUTE_OSPF);
350 stream_putc (s, flags);
351 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000352
paul718e3742002-12-13 20:15:29 +0000353 /* Put prefix information. */
354 psize = PSIZE (p->prefixlen);
355 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000356 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000357
358 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000359 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000360
361 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000362 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000363 {
paulcf795c52003-06-19 02:13:25 +0000364 if (path->nexthop.s_addr != INADDR_ANY)
365 {
366 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
367 stream_put_in_addr (s, &path->nexthop);
368 }
369 else
370 {
371 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
372 if (path->oi)
373 stream_putl (s, path->oi->ifp->ifindex);
374 else
375 stream_putl (s, 0);
376 }
paul72357f22003-06-19 02:11:23 +0000377
378 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
379 {
ajs9b0e25c2004-12-08 19:06:51 +0000380 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000381 inet_ntoa (p->prefix),
382 p->prefixlen, inet_ntoa (path->nexthop));
383 }
384 }
paul718e3742002-12-13 20:15:29 +0000385
386 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000387 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000388 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000389 {
390 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
391 stream_putl (s, or->cost + or->u.ext.type2_cost);
392 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
393 stream_putl (s, or->u.ext.type2_cost);
394 else
395 stream_putl (s, or->cost);
396 }
paul718e3742002-12-13 20:15:29 +0000397
398 stream_putw_at (s, 0, stream_get_endp (s));
399
ajs634f9ea2005-04-11 15:51:40 +0000400 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000401 }
402}
403
404void
405ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
406{
407 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000408 struct ospf_path *path;
409 struct in_addr *nexthop;
paul1eb8ef22005-04-07 07:30:20 +0000410 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000411
412 if (zclient->redist[ZEBRA_ROUTE_OSPF])
413 {
414 api.type = ZEBRA_ROUTE_OSPF;
415 api.flags = 0;
416 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000417 api.ifindex_num = 0;
418 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000419
paul1eb8ef22005-04-07 07:30:20 +0000420 for (ALL_LIST_ELEMENTS (or->paths, node, nnode, path))
paulcf795c52003-06-19 02:13:25 +0000421 {
paulcf795c52003-06-19 02:13:25 +0000422 if (path->nexthop.s_addr != INADDR_ANY)
423 {
pauld8e1d6b2003-08-10 04:04:41 +0000424 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000425 api.nexthop_num = 1;
426 nexthop = &path->nexthop;
427 api.nexthop = &nexthop;
428 }
hasso2db3d052004-02-11 21:52:13 +0000429 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000430 {
431 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
432 api.ifindex_num = 1;
433 api.ifindex = &path->oi->ifp->ifindex;
434 }
435 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
436 {
ajs9b0e25c2004-12-08 19:06:51 +0000437 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000438 inet_ntoa(p->prefix),
439 p->prefixlen);
440 }
paul72357f22003-06-19 02:11:23 +0000441
paul0a589352004-05-08 11:48:26 +0000442 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000443
444 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000445 {
ajs9b0e25c2004-12-08 19:06:51 +0000446 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000447 inet_ntoa (p->prefix),
448 p->prefixlen, inet_ntoa (**api.nexthop));
449 }
paulbb8ff1e2003-08-12 06:00:30 +0000450 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
451 {
ajs9b0e25c2004-12-08 19:06:51 +0000452 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000453 inet_ntoa (p->prefix),
454 p->prefixlen, *api.ifindex);
455 }
paulcf795c52003-06-19 02:13:25 +0000456 }
paul718e3742002-12-13 20:15:29 +0000457 }
458}
459
460void
461ospf_zebra_add_discard (struct prefix_ipv4 *p)
462{
463 struct zapi_ipv4 api;
464
465 if (zclient->redist[ZEBRA_ROUTE_OSPF])
466 {
467 api.type = ZEBRA_ROUTE_OSPF;
468 api.flags = ZEBRA_FLAG_BLACKHOLE;
469 api.message = 0;
470 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
471 api.nexthop_num = 0;
472 api.ifindex_num = 0;
473
paul0a589352004-05-08 11:48:26 +0000474 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000475 }
476}
477
478void
479ospf_zebra_delete_discard (struct prefix_ipv4 *p)
480{
481 struct zapi_ipv4 api;
482
483 if (zclient->redist[ZEBRA_ROUTE_OSPF])
484 {
485 api.type = ZEBRA_ROUTE_OSPF;
486 api.flags = ZEBRA_FLAG_BLACKHOLE;
487 api.message = 0;
488 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
489 api.nexthop_num = 0;
490 api.ifindex_num = 0;
491
paul0a589352004-05-08 11:48:26 +0000492 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000493
494 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000495 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000496 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000497
paul718e3742002-12-13 20:15:29 +0000498 }
499}
500
501int
502ospf_is_type_redistributed (int type)
503{
504 return (DEFAULT_ROUTE_TYPE (type)) ?
505 zclient->default_information : zclient->redist[type];
506}
507
508int
paul020709f2003-04-04 02:44:16 +0000509ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000510{
511 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000512
paul718e3742002-12-13 20:15:29 +0000513 if (ospf_is_type_redistributed (type))
514 {
paul68980082003-03-25 05:07:42 +0000515 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000516 {
517 ospf->dmetric[type].type = mtype;
518 force = LSA_REFRESH_FORCE;
519 }
paul68980082003-03-25 05:07:42 +0000520 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000521 {
522 ospf->dmetric[type].value = mvalue;
523 force = LSA_REFRESH_FORCE;
524 }
525
paul68980082003-03-25 05:07:42 +0000526 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000527
paul718e3742002-12-13 20:15:29 +0000528 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000529 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000530 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000531 metric_type (ospf, type), metric_value (ospf, type));
532
paul718e3742002-12-13 20:15:29 +0000533 return CMD_SUCCESS;
534 }
535
paul68980082003-03-25 05:07:42 +0000536 ospf->dmetric[type].type = mtype;
537 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000538
paul0a589352004-05-08 11:48:26 +0000539 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000540
541 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000542 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000543 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000544 metric_type (ospf, type), metric_value (ospf, type));
545
paul68980082003-03-25 05:07:42 +0000546 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000547
548 return CMD_SUCCESS;
549}
550
551int
paul020709f2003-04-04 02:44:16 +0000552ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000553{
554 if (type == zclient->redist_default)
555 return CMD_SUCCESS;
556
paulcf795c52003-06-19 02:13:25 +0000557 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000558 return CMD_SUCCESS;
559
paul0a589352004-05-08 11:48:26 +0000560 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000561
paul718e3742002-12-13 20:15:29 +0000562 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000563 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000564 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000565
paul68980082003-03-25 05:07:42 +0000566 ospf->dmetric[type].type = -1;
567 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000568
569 /* Remove the routes from OSPF table. */
570 ospf_redistribute_withdraw (type);
571
paul68980082003-03-25 05:07:42 +0000572 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000573
574 return CMD_SUCCESS;
575}
576
577int
paul020709f2003-04-04 02:44:16 +0000578ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000579 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000580{
581 int force = 0;
paul020709f2003-04-04 02:44:16 +0000582
paul718e3742002-12-13 20:15:29 +0000583 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
584 {
paul68980082003-03-25 05:07:42 +0000585 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000586 {
587 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
588 force = 1;
589 }
paul68980082003-03-25 05:07:42 +0000590 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000591 {
592 force = 1;
593 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
594 }
595
paul68980082003-03-25 05:07:42 +0000596 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000597
paul718e3742002-12-13 20:15:29 +0000598 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000599 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000600 ospf_redist_string(DEFAULT_ROUTE),
paulcf795c52003-06-19 02:13:25 +0000601 metric_type (ospf, DEFAULT_ROUTE),
602 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000603 return CMD_SUCCESS;
604 }
605
paul68980082003-03-25 05:07:42 +0000606 ospf->default_originate = originate;
607 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
608 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000609
paul0a589352004-05-08 11:48:26 +0000610 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
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[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000614 metric_type (ospf, DEFAULT_ROUTE),
615 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000616
paul68980082003-03-25 05:07:42 +0000617 if (ospf->router_id.s_addr == 0)
618 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000619 else
620 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000621 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000622
paul68980082003-03-25 05:07:42 +0000623 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000624
625 return CMD_SUCCESS;
626}
627
628int
paul020709f2003-04-04 02:44:16 +0000629ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000630{
631 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
632 return CMD_SUCCESS;
633
paul68980082003-03-25 05:07:42 +0000634 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
635 ospf->dmetric[DEFAULT_ROUTE].type = -1;
636 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000637
paul0a589352004-05-08 11:48:26 +0000638 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000639
640 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000641 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000642
paul68980082003-03-25 05:07:42 +0000643 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000644
645 return CMD_SUCCESS;
646}
647
paul4dadc292005-05-06 21:37:42 +0000648static int
paul020709f2003-04-04 02:44:16 +0000649ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000650 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000651{
652 /* If prefix is multicast, then do not originate LSA. */
653 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
654 {
655 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000656 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000657 return 0;
658 }
659
660 /* Take care of default-originate. */
661 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000662 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000663 {
paulcf795c52003-06-19 02:13:25 +0000664 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
665 "for default");
666 return 0;
paul718e3742002-12-13 20:15:29 +0000667 }
668
669 return 1;
670}
671
672/* If connected prefix is OSPF enable interface, then do not announce. */
673int
paulcf795c52003-06-19 02:13:25 +0000674ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000675{
676 struct route_node *rn;
677
paul68980082003-03-25 05:07:42 +0000678 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000679 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000680 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
681 {
682 route_unlock_node (rn);
683 return 0;
684 }
paul718e3742002-12-13 20:15:29 +0000685
686 return 1;
687}
688
689/* return 1 if external LSA must be originated, 0 otherwise */
690int
paul68980082003-03-25 05:07:42 +0000691ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000692 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000693{
694 struct route_map_set_values save_values;
695 struct prefix_ipv4 *p = &ei->p;
696 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000697
paul718e3742002-12-13 20:15:29 +0000698 if (changed)
699 *changed = 0;
700
paul020709f2003-04-04 02:44:16 +0000701 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000702 return 0;
703
704 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000705 if (type == ZEBRA_ROUTE_CONNECT &&
706 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000707 return 0;
708
paul020709f2003-04-04 02:44:16 +0000709 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000710 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000711 if (DISTRIBUTE_LIST (ospf, type))
712 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000713 {
714 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000715 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000716 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000717 inet_ntoa (p->prefix), p->prefixlen);
718 return 0;
719 }
paul718e3742002-12-13 20:15:29 +0000720
721 save_values = ei->route_map_set;
722 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000723
paul718e3742002-12-13 20:15:29 +0000724 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000725 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000726 {
727 int ret;
728
paulcf795c52003-06-19 02:13:25 +0000729 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
730 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000731
732 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000733 {
734 ei->route_map_set = save_values;
735 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000736 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000737 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000738 inet_ntoa (p->prefix), p->prefixlen);
739 return 0;
740 }
741
paul718e3742002-12-13 20:15:29 +0000742 /* check if 'route-map set' changed something */
743 if (changed)
paulcf795c52003-06-19 02:13:25 +0000744 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
745 &save_values);
paul718e3742002-12-13 20:15:29 +0000746 }
747
748 return 1;
749}
750
751/* OSPF route-map set for redistribution */
752void
paul6c835672004-10-11 11:00:30 +0000753ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000754{
paul020709f2003-04-04 02:44:16 +0000755 if (ROUTEMAP_NAME (ospf, type))
756 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000757
paul020709f2003-04-04 02:44:16 +0000758 ROUTEMAP_NAME (ospf, type) = strdup (name);
759 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000760}
761
762void
paul020709f2003-04-04 02:44:16 +0000763ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000764{
paul020709f2003-04-04 02:44:16 +0000765 if (ROUTEMAP_NAME (ospf, type))
766 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000767
paul020709f2003-04-04 02:44:16 +0000768 ROUTEMAP_NAME (ospf, type) = NULL;
769 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000770}
771
772/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000773static int
paul718e3742002-12-13 20:15:29 +0000774ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000775 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000776{
777 struct stream *s;
778 struct zapi_ipv4 api;
779 unsigned long ifindex;
780 struct in_addr nexthop;
781 struct prefix_ipv4 p;
782 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000783 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000784
785 s = zclient->ibuf;
786 ifindex = 0;
787 nexthop.s_addr = 0;
788
789 /* Type, flags, message. */
790 api.type = stream_getc (s);
791 api.flags = stream_getc (s);
792 api.message = stream_getc (s);
793
794 /* IPv4 prefix. */
795 memset (&p, 0, sizeof (struct prefix_ipv4));
796 p.family = AF_INET;
797 p.prefixlen = stream_getc (s);
798 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
799
hasso8585d4e2004-04-20 17:25:12 +0000800 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
801 return 0;
802
paul718e3742002-12-13 20:15:29 +0000803 /* Nexthop, ifindex, distance, metric. */
804 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
805 {
806 api.nexthop_num = stream_getc (s);
807 nexthop.s_addr = stream_get_ipv4 (s);
808 }
809 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
810 {
811 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000812 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000813 ifindex = stream_getl (s);
814 }
815 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
816 api.distance = stream_getc (s);
817 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
818 api.metric = stream_getl (s);
819
paul020709f2003-04-04 02:44:16 +0000820 ospf = ospf_lookup ();
821 if (ospf == NULL)
822 return 0;
823
paul718e3742002-12-13 20:15:29 +0000824 if (command == ZEBRA_IPV4_ROUTE_ADD)
825 {
paul7021c422003-07-15 12:52:22 +0000826 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000827 * Maybe we should ignore reject/blackhole routes? Testing shows that
828 * there is no problems though and this is only way to "summarize"
829 * routes in ASBR at the moment. Maybe we need just a better generalised
830 * solution for these types?
831 *
832 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
833 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
834 * return 0;
paul7021c422003-07-15 12:52:22 +0000835 */
paul7021c422003-07-15 12:52:22 +0000836
paul718e3742002-12-13 20:15:29 +0000837 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
838
paul68980082003-03-25 05:07:42 +0000839 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000840 /* Set flags to generate AS-external-LSA originate event
841 for each redistributed protocols later. */
842 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000843 else
paulcf795c52003-06-19 02:13:25 +0000844 {
845 if (ei)
846 {
847 if (is_prefix_default (&p))
848 ospf_external_lsa_refresh_default (ospf);
849 else
850 {
851 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000852
paulcf795c52003-06-19 02:13:25 +0000853 current = ospf_external_info_find_lsa (ospf, &ei->p);
854 if (!current)
855 ospf_external_lsa_originate (ospf, ei);
856 else if (IS_LSA_MAXAGE (current))
857 ospf_external_lsa_refresh (ospf, current,
858 ei, LSA_REFRESH_FORCE);
859 else
860 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
861 inet_ntoa (p.prefix));
862 }
863 }
864 }
paul718e3742002-12-13 20:15:29 +0000865 }
paulcf795c52003-06-19 02:13:25 +0000866 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000867 {
868 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000869 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000870 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000871 else
ajs5339cfd2005-09-19 13:28:05 +0000872 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000873 }
874
875 return 0;
876}
paul718e3742002-12-13 20:15:29 +0000877
paulcf795c52003-06-19 02:13:25 +0000878
paul718e3742002-12-13 20:15:29 +0000879int
paul6c835672004-10-11 11:00:30 +0000880ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000881{
882 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000883 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000884
885 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000886 if (DISTRIBUTE_NAME (ospf, type))
887 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000888
889 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000890 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000891
892 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000893 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000894 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000895
896 return CMD_SUCCESS;
897}
898
899int
paul6c835672004-10-11 11:00:30 +0000900ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000901{
902 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000903 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000904 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000905
906 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000907 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000908
909 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000910 if (DISTRIBUTE_NAME (ospf, type))
911 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000912
paul020709f2003-04-04 02:44:16 +0000913 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000914
915 return CMD_SUCCESS;
916}
917
918/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +0000919static int
paul718e3742002-12-13 20:15:29 +0000920ospf_distribute_list_update_timer (struct thread *thread)
921{
922 struct route_node *rn;
923 struct external_info *ei;
924 struct route_table *rt;
925 struct ospf_lsa *lsa;
paul4dadc292005-05-06 21:37:42 +0000926 intptr_t type;
paul020709f2003-04-04 02:44:16 +0000927 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000928
paul4dadc292005-05-06 21:37:42 +0000929 type = (intptr_t)THREAD_ARG (thread);
hasso01018ce2005-08-05 07:40:15 +0000930 assert (type <= ZEBRA_ROUTE_MAX);
paul64511f32004-10-31 18:01:13 +0000931
paul718e3742002-12-13 20:15:29 +0000932 rt = EXTERNAL_INFO (type);
933
paul020709f2003-04-04 02:44:16 +0000934 ospf = ospf_lookup ();
935 if (ospf == NULL)
936 return 0;
937
paul68980082003-03-25 05:07:42 +0000938 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000939
940 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
941
942 /* foreach all external info. */
943 if (rt)
944 for (rn = route_top (rt); rn; rn = route_next (rn))
945 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000946 {
947 if (is_prefix_default (&ei->p))
948 ospf_external_lsa_refresh_default (ospf);
949 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
950 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
951 else
952 ospf_external_lsa_originate (ospf, ei);
953 }
paul718e3742002-12-13 20:15:29 +0000954 return 0;
955}
956
957#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
958
959/* Update distribute-list and set timer to apply access-list. */
960void
paul68980082003-03-25 05:07:42 +0000961ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000962{
963 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000964
paul718e3742002-12-13 20:15:29 +0000965 /* External info does not exist. */
966 if (!(rt = EXTERNAL_INFO (type)))
967 return;
968
969 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000970 if (ospf->t_distribute_update)
971 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000972
973 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000974 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000975 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000976 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000977}
978
979/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +0000980static void
paul718e3742002-12-13 20:15:29 +0000981ospf_filter_update (struct access_list *access)
982{
paul020709f2003-04-04 02:44:16 +0000983 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000984 int type;
985 int abr_inv = 0;
986 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000987 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000988
989 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000990 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000991 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000992 return;
993
paul718e3742002-12-13 20:15:29 +0000994 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +0000995 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +0000996 {
paul020709f2003-04-04 02:44:16 +0000997 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +0000998 {
999 /* if route-map is not NULL it may be using this access list */
1000 ospf_distribute_list_update (ospf, type);
1001 continue;
1002 }
1003
hasso01018ce2005-08-05 07:40:15 +00001004 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1005 * but no distribute list. */
1006 if (type == ZEBRA_ROUTE_MAX)
1007 break;
paul718e3742002-12-13 20:15:29 +00001008
paul020709f2003-04-04 02:44:16 +00001009 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001010 {
1011 /* Keep old access-list for distribute-list. */
1012 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1013
1014 /* Update access-list for distribute-list. */
1015 DISTRIBUTE_LIST (ospf, type) =
1016 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1017
1018 /* No update for this distribute type. */
1019 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1020 continue;
1021
1022 /* Schedule distribute-list update timer. */
1023 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1024 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1025 ospf_distribute_list_update (ospf, type);
1026 }
paul718e3742002-12-13 20:15:29 +00001027 }
1028
1029 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001030 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1031 {
1032 if (EXPORT_NAME (area))
1033 {
1034 EXPORT_LIST (area) = NULL;
1035 abr_inv++;
1036 }
paul718e3742002-12-13 20:15:29 +00001037
paul1eb8ef22005-04-07 07:30:20 +00001038 if (IMPORT_NAME (area))
1039 {
1040 IMPORT_LIST (area) = NULL;
1041 abr_inv++;
1042 }
1043 }
paul718e3742002-12-13 20:15:29 +00001044
1045 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001046 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001047 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001048}
hassodd669bb2004-05-10 07:43:59 +00001049
1050/* If prefix-list is updated, do some updates. */
1051void
1052ospf_prefix_list_update (struct prefix_list *plist)
1053{
1054 struct ospf *ospf;
1055 int type;
1056 int abr_inv = 0;
1057 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001058 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001059
1060 /* If OSPF instatnce does not exist, return right now. */
1061 ospf = ospf_lookup ();
1062 if (ospf == NULL)
1063 return;
1064
1065 /* Update all route-maps which are used as redistribution filters.
1066 * They might use prefix-list.
1067 */
hasso01018ce2005-08-05 07:40:15 +00001068 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001069 {
1070 if (ROUTEMAP (ospf, type) != NULL)
1071 {
1072 /* If route-map is not NULL it may be using this prefix list */
1073 ospf_distribute_list_update (ospf, type);
1074 continue;
1075 }
1076 }
1077
1078 /* Update area filter-lists. */
paul1eb8ef22005-04-07 07:30:20 +00001079 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1080 {
1081 /* Update filter-list in. */
1082 if (PREFIX_NAME_IN (area))
1083 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1084 {
1085 PREFIX_LIST_IN (area) =
1086 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1087 abr_inv++;
1088 }
hassodd669bb2004-05-10 07:43:59 +00001089
paul1eb8ef22005-04-07 07:30:20 +00001090 /* Update filter-list out. */
1091 if (PREFIX_NAME_OUT (area))
1092 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1093 {
1094 PREFIX_LIST_IN (area) =
1095 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1096 abr_inv++;
1097 }
1098 }
hassodd669bb2004-05-10 07:43:59 +00001099
1100 /* Schedule ABR task. */
1101 if (IS_OSPF_ABR (ospf) && abr_inv)
1102 ospf_schedule_abr_task (ospf);
1103}
paulcf795c52003-06-19 02:13:25 +00001104
paul4dadc292005-05-06 21:37:42 +00001105static struct ospf_distance *
1106ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001107{
1108 struct ospf_distance *new;
1109 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1110 memset (new, 0, sizeof (struct ospf_distance));
1111 return new;
1112}
1113
paul4dadc292005-05-06 21:37:42 +00001114static void
paul718e3742002-12-13 20:15:29 +00001115ospf_distance_free (struct ospf_distance *odistance)
1116{
1117 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1118}
1119
1120int
paul6c835672004-10-11 11:00:30 +00001121ospf_distance_set (struct vty *vty, struct ospf *ospf,
1122 const char *distance_str,
1123 const char *ip_str,
1124 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001125{
1126 int ret;
1127 struct prefix_ipv4 p;
1128 u_char distance;
1129 struct route_node *rn;
1130 struct ospf_distance *odistance;
1131
1132 ret = str2prefix_ipv4 (ip_str, &p);
1133 if (ret == 0)
1134 {
1135 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1136 return CMD_WARNING;
1137 }
1138
1139 distance = atoi (distance_str);
1140
1141 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001142 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001143 if (rn->info)
1144 {
1145 odistance = rn->info;
1146 route_unlock_node (rn);
1147 }
1148 else
1149 {
1150 odistance = ospf_distance_new ();
1151 rn->info = odistance;
1152 }
1153
1154 /* Set distance value. */
1155 odistance->distance = distance;
1156
1157 /* Reset access-list configuration. */
1158 if (odistance->access_list)
1159 {
1160 free (odistance->access_list);
1161 odistance->access_list = NULL;
1162 }
1163 if (access_list_str)
1164 odistance->access_list = strdup (access_list_str);
1165
1166 return CMD_SUCCESS;
1167}
1168
1169int
paul6c835672004-10-11 11:00:30 +00001170ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1171 const char *distance_str,
1172 const char *ip_str, char
1173 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001174{
1175 int ret;
1176 struct prefix_ipv4 p;
1177 u_char distance;
1178 struct route_node *rn;
1179 struct ospf_distance *odistance;
1180
1181 ret = str2prefix_ipv4 (ip_str, &p);
1182 if (ret == 0)
1183 {
1184 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1185 return CMD_WARNING;
1186 }
1187
1188 distance = atoi (distance_str);
1189
paulcf795c52003-06-19 02:13:25 +00001190 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1191 if (!rn)
paul718e3742002-12-13 20:15:29 +00001192 {
1193 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1194 return CMD_WARNING;
1195 }
1196
1197 odistance = rn->info;
1198
1199 if (odistance->access_list)
1200 free (odistance->access_list);
1201 ospf_distance_free (odistance);
1202
1203 rn->info = NULL;
1204 route_unlock_node (rn);
1205 route_unlock_node (rn);
1206
1207 return CMD_SUCCESS;
1208}
1209
1210void
paul68980082003-03-25 05:07:42 +00001211ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001212{
1213 struct route_node *rn;
1214 struct ospf_distance *odistance;
1215
paul68980082003-03-25 05:07:42 +00001216 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001217 if ((odistance = rn->info) != NULL)
1218 {
paulcf795c52003-06-19 02:13:25 +00001219 if (odistance->access_list)
1220 free (odistance->access_list);
1221 ospf_distance_free (odistance);
1222 rn->info = NULL;
1223 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001224 }
1225}
1226
1227u_char
1228ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1229{
paul020709f2003-04-04 02:44:16 +00001230 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001231
paul020709f2003-04-04 02:44:16 +00001232 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001233 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001234 return 0;
1235
paul68980082003-03-25 05:07:42 +00001236 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001237 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001238 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001239
paul68980082003-03-25 05:07:42 +00001240 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001241 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001242 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001243
paul68980082003-03-25 05:07:42 +00001244 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001245 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001246 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001247 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001248
paul68980082003-03-25 05:07:42 +00001249 if (ospf->distance_all)
1250 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001251
1252 return 0;
1253}
1254
1255void
1256ospf_zebra_init ()
1257{
1258 /* Allocate zebra structure. */
1259 zclient = zclient_new ();
1260 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001261 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001262 zclient->interface_add = ospf_interface_add;
1263 zclient->interface_delete = ospf_interface_delete;
1264 zclient->interface_up = ospf_interface_state_up;
1265 zclient->interface_down = ospf_interface_state_down;
1266 zclient->interface_address_add = ospf_interface_address_add;
1267 zclient->interface_address_delete = ospf_interface_address_delete;
1268 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1269 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1270
1271 access_list_add_hook (ospf_filter_update);
1272 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001273 prefix_list_add_hook (ospf_prefix_list_update);
1274 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001275}