blob: 27299c59946d479a11cb7cd113038fda5adabe2b [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. */
60int
61ospf_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. */
81int
82ospf_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
112int
113ospf_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
paul718e3742002-12-13 20:15:29 +0000161int
162ospf_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
223int
224ospf_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
249int
250ospf_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
272int
273ospf_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. */
paul96735ee2003-08-10 02:51:22 +0000362 for (node = listhead (or->paths); node; nextnode (node))
paul72357f22003-06-19 02:11:23 +0000363 {
paulcf795c52003-06-19 02:13:25 +0000364 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000365
paulcf795c52003-06-19 02:13:25 +0000366 if (path->nexthop.s_addr != INADDR_ANY)
367 {
368 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
369 stream_put_in_addr (s, &path->nexthop);
370 }
371 else
372 {
373 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
374 if (path->oi)
375 stream_putl (s, path->oi->ifp->ifindex);
376 else
377 stream_putl (s, 0);
378 }
paul72357f22003-06-19 02:11:23 +0000379
380 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
381 {
ajs9b0e25c2004-12-08 19:06:51 +0000382 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000383 inet_ntoa (p->prefix),
384 p->prefixlen, inet_ntoa (path->nexthop));
385 }
386 }
paul718e3742002-12-13 20:15:29 +0000387
388 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000389 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000390 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000391 {
392 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
393 stream_putl (s, or->cost + or->u.ext.type2_cost);
394 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
395 stream_putl (s, or->u.ext.type2_cost);
396 else
397 stream_putl (s, or->cost);
398 }
paul718e3742002-12-13 20:15:29 +0000399
400 stream_putw_at (s, 0, stream_get_endp (s));
401
402 writen (zclient->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000403 }
404}
405
406void
407ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
408{
409 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000410 struct ospf_path *path;
411 struct in_addr *nexthop;
hasso52dc7ee2004-09-23 19:18:23 +0000412 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000413
414 if (zclient->redist[ZEBRA_ROUTE_OSPF])
415 {
416 api.type = ZEBRA_ROUTE_OSPF;
417 api.flags = 0;
418 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000419 api.ifindex_num = 0;
420 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000421
paul96735ee2003-08-10 02:51:22 +0000422 for (node = listhead (or->paths); node; nextnode (node))
paulcf795c52003-06-19 02:13:25 +0000423 {
424 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000425
paulcf795c52003-06-19 02:13:25 +0000426 if (path->nexthop.s_addr != INADDR_ANY)
427 {
pauld8e1d6b2003-08-10 04:04:41 +0000428 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000429 api.nexthop_num = 1;
430 nexthop = &path->nexthop;
431 api.nexthop = &nexthop;
432 }
hasso2db3d052004-02-11 21:52:13 +0000433 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000434 {
435 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
436 api.ifindex_num = 1;
437 api.ifindex = &path->oi->ifp->ifindex;
438 }
439 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
440 {
ajs9b0e25c2004-12-08 19:06:51 +0000441 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000442 inet_ntoa(p->prefix),
443 p->prefixlen);
444 }
paul72357f22003-06-19 02:11:23 +0000445
paul0a589352004-05-08 11:48:26 +0000446 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000447
448 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000449 {
ajs9b0e25c2004-12-08 19:06:51 +0000450 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000451 inet_ntoa (p->prefix),
452 p->prefixlen, inet_ntoa (**api.nexthop));
453 }
paulbb8ff1e2003-08-12 06:00:30 +0000454 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
455 {
ajs9b0e25c2004-12-08 19:06:51 +0000456 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000457 inet_ntoa (p->prefix),
458 p->prefixlen, *api.ifindex);
459 }
paulcf795c52003-06-19 02:13:25 +0000460 }
paul718e3742002-12-13 20:15:29 +0000461 }
462}
463
464void
465ospf_zebra_add_discard (struct prefix_ipv4 *p)
466{
467 struct zapi_ipv4 api;
468
469 if (zclient->redist[ZEBRA_ROUTE_OSPF])
470 {
471 api.type = ZEBRA_ROUTE_OSPF;
472 api.flags = ZEBRA_FLAG_BLACKHOLE;
473 api.message = 0;
474 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
475 api.nexthop_num = 0;
476 api.ifindex_num = 0;
477
paul0a589352004-05-08 11:48:26 +0000478 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000479 }
480}
481
482void
483ospf_zebra_delete_discard (struct prefix_ipv4 *p)
484{
485 struct zapi_ipv4 api;
486
487 if (zclient->redist[ZEBRA_ROUTE_OSPF])
488 {
489 api.type = ZEBRA_ROUTE_OSPF;
490 api.flags = ZEBRA_FLAG_BLACKHOLE;
491 api.message = 0;
492 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
493 api.nexthop_num = 0;
494 api.ifindex_num = 0;
495
paul0a589352004-05-08 11:48:26 +0000496 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000497
498 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000499 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000500 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000501
paul718e3742002-12-13 20:15:29 +0000502 }
503}
504
505int
506ospf_is_type_redistributed (int type)
507{
508 return (DEFAULT_ROUTE_TYPE (type)) ?
509 zclient->default_information : zclient->redist[type];
510}
511
512int
paul020709f2003-04-04 02:44:16 +0000513ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000514{
515 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000516
paul718e3742002-12-13 20:15:29 +0000517 if (ospf_is_type_redistributed (type))
518 {
paul68980082003-03-25 05:07:42 +0000519 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000520 {
521 ospf->dmetric[type].type = mtype;
522 force = LSA_REFRESH_FORCE;
523 }
paul68980082003-03-25 05:07:42 +0000524 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000525 {
526 ospf->dmetric[type].value = mvalue;
527 force = LSA_REFRESH_FORCE;
528 }
529
paul68980082003-03-25 05:07:42 +0000530 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000531
paul718e3742002-12-13 20:15:29 +0000532 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000533 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000534 LOOKUP (ospf_redistributed_proto, type),
535 metric_type (ospf, type), metric_value (ospf, type));
536
paul718e3742002-12-13 20:15:29 +0000537 return CMD_SUCCESS;
538 }
539
paul68980082003-03-25 05:07:42 +0000540 ospf->dmetric[type].type = mtype;
541 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000542
paul0a589352004-05-08 11:48:26 +0000543 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000544
545 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000546 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000547 LOOKUP (ospf_redistributed_proto, type),
548 metric_type (ospf, type), metric_value (ospf, type));
549
paul68980082003-03-25 05:07:42 +0000550 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000551
552 return CMD_SUCCESS;
553}
554
555int
paul020709f2003-04-04 02:44:16 +0000556ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000557{
558 if (type == zclient->redist_default)
559 return CMD_SUCCESS;
560
paulcf795c52003-06-19 02:13:25 +0000561 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000562 return CMD_SUCCESS;
563
paul0a589352004-05-08 11:48:26 +0000564 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000565
paul718e3742002-12-13 20:15:29 +0000566 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000567 zlog_debug ("Redistribute[%s]: Stop",
paulcf795c52003-06-19 02:13:25 +0000568 LOOKUP (ospf_redistributed_proto, type));
paul718e3742002-12-13 20:15:29 +0000569
paul68980082003-03-25 05:07:42 +0000570 ospf->dmetric[type].type = -1;
571 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000572
573 /* Remove the routes from OSPF table. */
574 ospf_redistribute_withdraw (type);
575
paul68980082003-03-25 05:07:42 +0000576 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000577
578 return CMD_SUCCESS;
579}
580
581int
paul020709f2003-04-04 02:44:16 +0000582ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000583 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000584{
585 int force = 0;
paul020709f2003-04-04 02:44:16 +0000586
paul718e3742002-12-13 20:15:29 +0000587 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
588 {
paul68980082003-03-25 05:07:42 +0000589 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000590 {
591 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
592 force = 1;
593 }
paul68980082003-03-25 05:07:42 +0000594 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000595 {
596 force = 1;
597 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
598 }
599
paul68980082003-03-25 05:07:42 +0000600 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000601
paul718e3742002-12-13 20:15:29 +0000602 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000603 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000604 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
605 metric_type (ospf, DEFAULT_ROUTE),
606 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000607 return CMD_SUCCESS;
608 }
609
paul68980082003-03-25 05:07:42 +0000610 ospf->default_originate = originate;
611 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
612 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000613
paul0a589352004-05-08 11:48:26 +0000614 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000615
paul718e3742002-12-13 20:15:29 +0000616 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000617 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000618 metric_type (ospf, DEFAULT_ROUTE),
619 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000620
paul68980082003-03-25 05:07:42 +0000621 if (ospf->router_id.s_addr == 0)
622 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000623 else
624 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000625 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000626
paul68980082003-03-25 05:07:42 +0000627 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000628
629 return CMD_SUCCESS;
630}
631
632int
paul020709f2003-04-04 02:44:16 +0000633ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000634{
635 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
636 return CMD_SUCCESS;
637
paul68980082003-03-25 05:07:42 +0000638 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
639 ospf->dmetric[DEFAULT_ROUTE].type = -1;
640 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000641
paul0a589352004-05-08 11:48:26 +0000642 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000643
644 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000645 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000646
paul68980082003-03-25 05:07:42 +0000647 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000648
649 return CMD_SUCCESS;
650}
651
652int
paul020709f2003-04-04 02:44:16 +0000653ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000654 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000655{
656 /* If prefix is multicast, then do not originate LSA. */
657 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
658 {
659 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000660 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000661 return 0;
662 }
663
664 /* Take care of default-originate. */
665 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000666 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000667 {
paulcf795c52003-06-19 02:13:25 +0000668 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
669 "for default");
670 return 0;
paul718e3742002-12-13 20:15:29 +0000671 }
672
673 return 1;
674}
675
676/* If connected prefix is OSPF enable interface, then do not announce. */
677int
paulcf795c52003-06-19 02:13:25 +0000678ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000679{
680 struct route_node *rn;
681
paul68980082003-03-25 05:07:42 +0000682 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000683 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000684 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
685 {
686 route_unlock_node (rn);
687 return 0;
688 }
paul718e3742002-12-13 20:15:29 +0000689
690 return 1;
691}
692
693/* return 1 if external LSA must be originated, 0 otherwise */
694int
paul68980082003-03-25 05:07:42 +0000695ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000696 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000697{
698 struct route_map_set_values save_values;
699 struct prefix_ipv4 *p = &ei->p;
700 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000701
paul718e3742002-12-13 20:15:29 +0000702 if (changed)
703 *changed = 0;
704
paul020709f2003-04-04 02:44:16 +0000705 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000706 return 0;
707
708 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000709 if (type == ZEBRA_ROUTE_CONNECT &&
710 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000711 return 0;
712
paul020709f2003-04-04 02:44:16 +0000713 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000714 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000715 if (DISTRIBUTE_LIST (ospf, type))
716 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000717 {
718 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000719 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
paulcf795c52003-06-19 02:13:25 +0000720 LOOKUP (ospf_redistributed_proto, type),
721 inet_ntoa (p->prefix), p->prefixlen);
722 return 0;
723 }
paul718e3742002-12-13 20:15:29 +0000724
725 save_values = ei->route_map_set;
726 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000727
paul718e3742002-12-13 20:15:29 +0000728 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000729 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000730 {
731 int ret;
732
paulcf795c52003-06-19 02:13:25 +0000733 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
734 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000735
736 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000737 {
738 ei->route_map_set = save_values;
739 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000740 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
paulcf795c52003-06-19 02:13:25 +0000741 LOOKUP (ospf_redistributed_proto, type),
742 inet_ntoa (p->prefix), p->prefixlen);
743 return 0;
744 }
745
paul718e3742002-12-13 20:15:29 +0000746 /* check if 'route-map set' changed something */
747 if (changed)
paulcf795c52003-06-19 02:13:25 +0000748 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
749 &save_values);
paul718e3742002-12-13 20:15:29 +0000750 }
751
752 return 1;
753}
754
755/* OSPF route-map set for redistribution */
756void
paul6c835672004-10-11 11:00:30 +0000757ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000758{
paul020709f2003-04-04 02:44:16 +0000759 if (ROUTEMAP_NAME (ospf, type))
760 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000761
paul020709f2003-04-04 02:44:16 +0000762 ROUTEMAP_NAME (ospf, type) = strdup (name);
763 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000764}
765
766void
paul020709f2003-04-04 02:44:16 +0000767ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000768{
paul020709f2003-04-04 02:44:16 +0000769 if (ROUTEMAP_NAME (ospf, type))
770 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000771
paul020709f2003-04-04 02:44:16 +0000772 ROUTEMAP_NAME (ospf, type) = NULL;
773 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000774}
775
776/* Zebra route add and delete treatment. */
777int
778ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000779 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000780{
781 struct stream *s;
782 struct zapi_ipv4 api;
783 unsigned long ifindex;
784 struct in_addr nexthop;
785 struct prefix_ipv4 p;
786 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000787 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000788
789 s = zclient->ibuf;
790 ifindex = 0;
791 nexthop.s_addr = 0;
792
793 /* Type, flags, message. */
794 api.type = stream_getc (s);
795 api.flags = stream_getc (s);
796 api.message = stream_getc (s);
797
798 /* IPv4 prefix. */
799 memset (&p, 0, sizeof (struct prefix_ipv4));
800 p.family = AF_INET;
801 p.prefixlen = stream_getc (s);
802 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
803
hasso8585d4e2004-04-20 17:25:12 +0000804 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
805 return 0;
806
paul718e3742002-12-13 20:15:29 +0000807 /* Nexthop, ifindex, distance, metric. */
808 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
809 {
810 api.nexthop_num = stream_getc (s);
811 nexthop.s_addr = stream_get_ipv4 (s);
812 }
813 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
814 {
815 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000816 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000817 ifindex = stream_getl (s);
818 }
819 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
820 api.distance = stream_getc (s);
821 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
822 api.metric = stream_getl (s);
823
paul020709f2003-04-04 02:44:16 +0000824 ospf = ospf_lookup ();
825 if (ospf == NULL)
826 return 0;
827
paul718e3742002-12-13 20:15:29 +0000828 if (command == ZEBRA_IPV4_ROUTE_ADD)
829 {
paul7021c422003-07-15 12:52:22 +0000830 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000831 * Maybe we should ignore reject/blackhole routes? Testing shows that
832 * there is no problems though and this is only way to "summarize"
833 * routes in ASBR at the moment. Maybe we need just a better generalised
834 * solution for these types?
835 *
836 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
837 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
838 * return 0;
paul7021c422003-07-15 12:52:22 +0000839 */
paul7021c422003-07-15 12:52:22 +0000840
paul718e3742002-12-13 20:15:29 +0000841 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
842
paul68980082003-03-25 05:07:42 +0000843 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000844 /* Set flags to generate AS-external-LSA originate event
845 for each redistributed protocols later. */
846 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000847 else
paulcf795c52003-06-19 02:13:25 +0000848 {
849 if (ei)
850 {
851 if (is_prefix_default (&p))
852 ospf_external_lsa_refresh_default (ospf);
853 else
854 {
855 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000856
paulcf795c52003-06-19 02:13:25 +0000857 current = ospf_external_info_find_lsa (ospf, &ei->p);
858 if (!current)
859 ospf_external_lsa_originate (ospf, ei);
860 else if (IS_LSA_MAXAGE (current))
861 ospf_external_lsa_refresh (ospf, current,
862 ei, LSA_REFRESH_FORCE);
863 else
864 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
865 inet_ntoa (p.prefix));
866 }
867 }
868 }
paul718e3742002-12-13 20:15:29 +0000869 }
paulcf795c52003-06-19 02:13:25 +0000870 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000871 {
872 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000873 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000874 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000875 else
876 ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop);
paul718e3742002-12-13 20:15:29 +0000877 }
878
879 return 0;
880}
paul718e3742002-12-13 20:15:29 +0000881
paulcf795c52003-06-19 02:13:25 +0000882
paul718e3742002-12-13 20:15:29 +0000883int
paul6c835672004-10-11 11:00:30 +0000884ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000885{
886 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000887 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000888
889 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000890 if (DISTRIBUTE_NAME (ospf, type))
891 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000892
893 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000894 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000895
896 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000897 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000898 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000899
900 return CMD_SUCCESS;
901}
902
903int
paul6c835672004-10-11 11:00:30 +0000904ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000905{
906 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000907 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000908 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000909
910 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000911 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000912
913 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000914 if (DISTRIBUTE_NAME (ospf, type))
915 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000916
paul020709f2003-04-04 02:44:16 +0000917 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000918
919 return CMD_SUCCESS;
920}
921
922/* distribute-list update timer. */
923int
924ospf_distribute_list_update_timer (struct thread *thread)
925{
926 struct route_node *rn;
927 struct external_info *ei;
928 struct route_table *rt;
929 struct ospf_lsa *lsa;
paul64511f32004-10-31 18:01:13 +0000930 int type;
paul020709f2003-04-04 02:44:16 +0000931 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000932
933 type = (int) THREAD_ARG (thread);
paul64511f32004-10-31 18:01:13 +0000934 assert (type < ZEBRA_ROUTE_MAX);
935
paul718e3742002-12-13 20:15:29 +0000936 rt = EXTERNAL_INFO (type);
937
paul020709f2003-04-04 02:44:16 +0000938 ospf = ospf_lookup ();
939 if (ospf == NULL)
940 return 0;
941
paul68980082003-03-25 05:07:42 +0000942 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000943
944 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
945
946 /* foreach all external info. */
947 if (rt)
948 for (rn = route_top (rt); rn; rn = route_next (rn))
949 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000950 {
951 if (is_prefix_default (&ei->p))
952 ospf_external_lsa_refresh_default (ospf);
953 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
954 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
955 else
956 ospf_external_lsa_originate (ospf, ei);
957 }
paul718e3742002-12-13 20:15:29 +0000958 return 0;
959}
960
961#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
962
963/* Update distribute-list and set timer to apply access-list. */
964void
paul68980082003-03-25 05:07:42 +0000965ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000966{
967 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000968
paul718e3742002-12-13 20:15:29 +0000969 /* External info does not exist. */
970 if (!(rt = EXTERNAL_INFO (type)))
971 return;
972
973 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000974 if (ospf->t_distribute_update)
975 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000976
977 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000978 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000979 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000980 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000981}
982
983/* If access-list is updated, apply some check. */
984void
985ospf_filter_update (struct access_list *access)
986{
paul020709f2003-04-04 02:44:16 +0000987 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000988 int type;
989 int abr_inv = 0;
990 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000991 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000992
993 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000994 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000995 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000996 return;
997
paul718e3742002-12-13 20:15:29 +0000998 /* Update distribute-list, and apply filter. */
999 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1000 {
paul020709f2003-04-04 02:44:16 +00001001 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001002 {
1003 /* if route-map is not NULL it may be using this access list */
1004 ospf_distribute_list_update (ospf, type);
1005 continue;
1006 }
1007
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. */
paul68980082003-03-25 05:07:42 +00001030 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001031 if ((area = getdata (node)) != NULL)
1032 {
paulcf795c52003-06-19 02:13:25 +00001033 if (EXPORT_NAME (area))
1034 {
1035 EXPORT_LIST (area) = NULL;
1036 abr_inv++;
1037 }
paul718e3742002-12-13 20:15:29 +00001038
paulcf795c52003-06-19 02:13:25 +00001039 if (IMPORT_NAME (area))
1040 {
1041 IMPORT_LIST (area) = NULL;
1042 abr_inv++;
1043 }
paul718e3742002-12-13 20:15:29 +00001044 }
1045
1046 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001047 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001048 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001049}
hassodd669bb2004-05-10 07:43:59 +00001050
1051/* If prefix-list is updated, do some updates. */
1052void
1053ospf_prefix_list_update (struct prefix_list *plist)
1054{
1055 struct ospf *ospf;
1056 int type;
1057 int abr_inv = 0;
1058 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001059 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001060
1061 /* If OSPF instatnce does not exist, return right now. */
1062 ospf = ospf_lookup ();
1063 if (ospf == NULL)
1064 return;
1065
1066 /* Update all route-maps which are used as redistribution filters.
1067 * They might use prefix-list.
1068 */
1069 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1070 {
1071 if (ROUTEMAP (ospf, type) != NULL)
1072 {
1073 /* If route-map is not NULL it may be using this prefix list */
1074 ospf_distribute_list_update (ospf, type);
1075 continue;
1076 }
1077 }
1078
1079 /* Update area filter-lists. */
1080 for (node = listhead (ospf->areas); node; nextnode (node))
1081 if ((area = getdata (node)) != NULL)
1082 {
1083 /* Update filter-list in. */
1084 if (PREFIX_NAME_IN (area))
1085 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1086 {
1087 PREFIX_LIST_IN (area) =
1088 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1089 abr_inv++;
1090 }
1091
1092 /* Update filter-list out. */
1093 if (PREFIX_NAME_OUT (area))
1094 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1095 {
1096 PREFIX_LIST_IN (area) =
1097 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1098 abr_inv++;
1099 }
1100 }
1101
1102 /* Schedule ABR task. */
1103 if (IS_OSPF_ABR (ospf) && abr_inv)
1104 ospf_schedule_abr_task (ospf);
1105}
paulcf795c52003-06-19 02:13:25 +00001106
paul718e3742002-12-13 20:15:29 +00001107struct ospf_distance *
1108ospf_distance_new ()
1109{
1110 struct ospf_distance *new;
1111 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1112 memset (new, 0, sizeof (struct ospf_distance));
1113 return new;
1114}
1115
1116void
1117ospf_distance_free (struct ospf_distance *odistance)
1118{
1119 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1120}
1121
1122int
paul6c835672004-10-11 11:00:30 +00001123ospf_distance_set (struct vty *vty, struct ospf *ospf,
1124 const char *distance_str,
1125 const char *ip_str,
1126 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001127{
1128 int ret;
1129 struct prefix_ipv4 p;
1130 u_char distance;
1131 struct route_node *rn;
1132 struct ospf_distance *odistance;
1133
1134 ret = str2prefix_ipv4 (ip_str, &p);
1135 if (ret == 0)
1136 {
1137 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1138 return CMD_WARNING;
1139 }
1140
1141 distance = atoi (distance_str);
1142
1143 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001144 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001145 if (rn->info)
1146 {
1147 odistance = rn->info;
1148 route_unlock_node (rn);
1149 }
1150 else
1151 {
1152 odistance = ospf_distance_new ();
1153 rn->info = odistance;
1154 }
1155
1156 /* Set distance value. */
1157 odistance->distance = distance;
1158
1159 /* Reset access-list configuration. */
1160 if (odistance->access_list)
1161 {
1162 free (odistance->access_list);
1163 odistance->access_list = NULL;
1164 }
1165 if (access_list_str)
1166 odistance->access_list = strdup (access_list_str);
1167
1168 return CMD_SUCCESS;
1169}
1170
1171int
paul6c835672004-10-11 11:00:30 +00001172ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1173 const char *distance_str,
1174 const char *ip_str, char
1175 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001176{
1177 int ret;
1178 struct prefix_ipv4 p;
1179 u_char distance;
1180 struct route_node *rn;
1181 struct ospf_distance *odistance;
1182
1183 ret = str2prefix_ipv4 (ip_str, &p);
1184 if (ret == 0)
1185 {
1186 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1187 return CMD_WARNING;
1188 }
1189
1190 distance = atoi (distance_str);
1191
paulcf795c52003-06-19 02:13:25 +00001192 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1193 if (!rn)
paul718e3742002-12-13 20:15:29 +00001194 {
1195 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1196 return CMD_WARNING;
1197 }
1198
1199 odistance = rn->info;
1200
1201 if (odistance->access_list)
1202 free (odistance->access_list);
1203 ospf_distance_free (odistance);
1204
1205 rn->info = NULL;
1206 route_unlock_node (rn);
1207 route_unlock_node (rn);
1208
1209 return CMD_SUCCESS;
1210}
1211
1212void
paul68980082003-03-25 05:07:42 +00001213ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001214{
1215 struct route_node *rn;
1216 struct ospf_distance *odistance;
1217
paul68980082003-03-25 05:07:42 +00001218 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001219 if ((odistance = rn->info) != NULL)
1220 {
paulcf795c52003-06-19 02:13:25 +00001221 if (odistance->access_list)
1222 free (odistance->access_list);
1223 ospf_distance_free (odistance);
1224 rn->info = NULL;
1225 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001226 }
1227}
1228
1229u_char
1230ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1231{
paul020709f2003-04-04 02:44:16 +00001232 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001233
paul020709f2003-04-04 02:44:16 +00001234 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001235 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001236 return 0;
1237
paul68980082003-03-25 05:07:42 +00001238 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001239 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001240 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001241
paul68980082003-03-25 05:07:42 +00001242 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001243 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001244 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001245
paul68980082003-03-25 05:07:42 +00001246 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001247 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001248 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001249 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001250
paul68980082003-03-25 05:07:42 +00001251 if (ospf->distance_all)
1252 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001253
1254 return 0;
1255}
1256
1257void
1258ospf_zebra_init ()
1259{
1260 /* Allocate zebra structure. */
1261 zclient = zclient_new ();
1262 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001263 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001264 zclient->interface_add = ospf_interface_add;
1265 zclient->interface_delete = ospf_interface_delete;
1266 zclient->interface_up = ospf_interface_state_up;
1267 zclient->interface_down = ospf_interface_state_down;
1268 zclient->interface_address_add = ospf_interface_address_add;
1269 zclient->interface_address_delete = ospf_interface_address_delete;
1270 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1271 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1272
1273 access_list_add_hook (ospf_filter_update);
1274 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001275 prefix_list_add_hook (ospf_prefix_list_update);
1276 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001277}