blob: aaee31e118a42442fcd953e1d65876344724fafa [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 ();
paulb29800a2005-11-20 14:50:45 +000071
hasso18a6dce2004-10-03 18:18:34 +000072 if (ospf != NULL)
paulb29800a2005-11-20 14:50:45 +000073 ospf_router_id_update (ospf);
74
hasso18a6dce2004-10-03 18:18:34 +000075 return 0;
76}
paul718e3742002-12-13 20:15:29 +000077
78/* Inteface addition message from zebra. */
paul4dadc292005-05-06 21:37:42 +000079static int
paul718e3742002-12-13 20:15:29 +000080ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
81{
82 struct interface *ifp;
paul020709f2003-04-04 02:44:16 +000083 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +000084
85 ifp = zebra_interface_add_read (zclient->ibuf);
86
87 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +000088 zlog_debug ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +000089 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000090
paulcf795c52003-06-19 02:13:25 +000091 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000092
paul718e3742002-12-13 20:15:29 +000093 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
94 {
95 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +000096 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +000097 }
98
paul020709f2003-04-04 02:44:16 +000099 ospf = ospf_lookup ();
100 if (ospf != NULL)
101 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000102
103#ifdef HAVE_SNMP
104 ospf_snmp_if_update (ifp);
105#endif /* HAVE_SNMP */
106
107 return 0;
108}
109
paul4dadc292005-05-06 21:37:42 +0000110static int
paul718e3742002-12-13 20:15:29 +0000111ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000112 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000113{
114 struct interface *ifp;
115 struct stream *s;
116 struct route_node *rn;
117
paulcf795c52003-06-19 02:13:25 +0000118 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000119 /* zebra_interface_state_read() updates interface structure in iflist */
120 ifp = zebra_interface_state_read (s);
121
122 if (ifp == NULL)
123 return 0;
124
125 if (if_is_up (ifp))
126 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000127 ifp->name);
128
paul718e3742002-12-13 20:15:29 +0000129 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000130 zlog_debug
paulcf795c52003-06-19 02:13:25 +0000131 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
132 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000133
134#ifdef HAVE_SNMP
135 ospf_snmp_if_delete (ifp);
136#endif /* HAVE_SNMP */
137
138 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
139 if (rn->info)
140 ospf_if_free ((struct ospf_interface *) rn->info);
141
ajsd2fc8892005-04-02 18:38:43 +0000142 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000143 return 0;
144}
145
ajsd2fc8892005-04-02 18:38:43 +0000146static struct interface *
paul718e3742002-12-13 20:15:29 +0000147zebra_interface_if_lookup (struct stream *s)
148{
ajs21fefa92005-04-02 23:16:41 +0000149 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000150
151 /* Read interface name. */
152 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
153
ajsd2fc8892005-04-02 18:38:43 +0000154 /* And look it up. */
ajs21fefa92005-04-02 23:16:41 +0000155 return if_lookup_by_name_len(ifname_tmp,
156 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000157}
158
paul4dadc292005-05-06 21:37:42 +0000159static int
paul718e3742002-12-13 20:15:29 +0000160ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000161 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000162{
163 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000164 struct ospf_interface *oi;
165 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000166
paul718e3742002-12-13 20:15:29 +0000167 ifp = zebra_interface_if_lookup (zclient->ibuf);
168
169 if (ifp == NULL)
170 return 0;
171
172 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000173 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000174 {
175 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000176 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000177 memcpy (&if_tmp, ifp, sizeof (struct interface));
178
179 zebra_interface_if_set_value (zclient->ibuf, ifp);
180
181 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000182 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000183
184 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000185 {
186 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000187 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000188 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000189
paulcf795c52003-06-19 02:13:25 +0000190 ospf_if_recalculate_output_cost (ifp);
191 }
ajsa608bbf2005-03-29 17:03:49 +0000192
193 if (if_tmp.mtu != ifp->mtu)
194 {
195 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
196 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
197 ifp->name, if_tmp.mtu, ifp->mtu);
198
199 /* Must reset the interface (simulate down/up) when MTU changes. */
200 ospf_if_reset(ifp);
201 }
paul718e3742002-12-13 20:15:29 +0000202 return 0;
203 }
paulcf795c52003-06-19 02:13:25 +0000204
paul718e3742002-12-13 20:15:29 +0000205 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000206
paul718e3742002-12-13 20:15:29 +0000207 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000208 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000209
210 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000211 {
paulcf795c52003-06-19 02:13:25 +0000212 if ((oi = rn->info) == NULL)
213 continue;
214
paul718e3742002-12-13 20:15:29 +0000215 ospf_if_up (oi);
216 }
paulcf795c52003-06-19 02:13:25 +0000217
paul718e3742002-12-13 20:15:29 +0000218 return 0;
219}
220
paul4dadc292005-05-06 21:37:42 +0000221static int
paul718e3742002-12-13 20:15:29 +0000222ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000223 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000224{
225 struct interface *ifp;
226 struct ospf_interface *oi;
227 struct route_node *node;
228
229 ifp = zebra_interface_state_read (zclient->ibuf);
230
231 if (ifp == NULL)
232 return 0;
233
234 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000235 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000236
paulcf795c52003-06-19 02:13:25 +0000237 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000238 {
paulcf795c52003-06-19 02:13:25 +0000239 if ((oi = node->info) == NULL)
240 continue;
paul718e3742002-12-13 20:15:29 +0000241 ospf_if_down (oi);
242 }
243
244 return 0;
245}
246
paul4dadc292005-05-06 21:37:42 +0000247static int
paul718e3742002-12-13 20:15:29 +0000248ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000249 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000250{
paul020709f2003-04-04 02:44:16 +0000251 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000252 struct connected *c;
253
paul0a589352004-05-08 11:48:26 +0000254 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000255
256 if (c == NULL)
257 return 0;
258
paul020709f2003-04-04 02:44:16 +0000259 ospf = ospf_lookup ();
260 if (ospf != NULL)
261 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000262
263#ifdef HAVE_SNMP
264 ospf_snmp_if_update (c->ifp);
265#endif /* HAVE_SNMP */
266
267 return 0;
268}
269
paul4dadc292005-05-06 21:37:42 +0000270static int
paul718e3742002-12-13 20:15:29 +0000271ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000272 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000273{
paul020709f2003-04-04 02:44:16 +0000274 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000275 struct connected *c;
276 struct interface *ifp;
277 struct ospf_interface *oi;
278 struct route_node *rn;
279 struct prefix p;
280
paul0a589352004-05-08 11:48:26 +0000281 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000282
283 if (c == NULL)
284 return 0;
285
286 ifp = c->ifp;
287 p = *c->address;
288 p.prefixlen = IPV4_MAX_PREFIXLEN;
289
290 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000291 if (!rn)
paul98429f62006-01-10 22:11:54 +0000292 {
293 connected_free (c);
294 return 0;
295 }
paul718e3742002-12-13 20:15:29 +0000296
297 assert (rn->info);
298 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000299
paul718e3742002-12-13 20:15:29 +0000300 /* Call interface hook functions to clean up */
301 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000302
paul718e3742002-12-13 20:15:29 +0000303#ifdef HAVE_SNMP
304 ospf_snmp_if_update (c->ifp);
305#endif /* HAVE_SNMP */
306
307 connected_free (c);
308
paul020709f2003-04-04 02:44:16 +0000309 ospf = ospf_lookup ();
310 if (ospf != NULL)
311 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000312
313 return 0;
314}
paul72357f22003-06-19 02:11:23 +0000315
paul718e3742002-12-13 20:15:29 +0000316void
317ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
318{
319 u_char message;
320 u_char distance;
321 u_char flags;
322 int psize;
323 struct stream *s;
324 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000325 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000326
327 if (zclient->redist[ZEBRA_ROUTE_OSPF])
328 {
329 message = 0;
330 flags = 0;
331
332 /* OSPF pass nexthop and metric */
333 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
334 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
335
336 /* Distance value. */
337 distance = ospf_distance_apply (p, or);
338 if (distance)
paul72357f22003-06-19 02:11:23 +0000339 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000340
341 /* Make packet. */
342 s = zclient->obuf;
343 stream_reset (s);
344
paul718e3742002-12-13 20:15:29 +0000345 /* Put command, type, flags, message. */
paulc6371712006-01-17 17:49:53 +0000346 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD);
paul718e3742002-12-13 20:15:29 +0000347 stream_putc (s, ZEBRA_ROUTE_OSPF);
348 stream_putc (s, flags);
349 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000350
paul718e3742002-12-13 20:15:29 +0000351 /* Put prefix information. */
352 psize = PSIZE (p->prefixlen);
353 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000354 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000355
356 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000357 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000358
359 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000360 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000361 {
paulcf795c52003-06-19 02:13:25 +0000362 if (path->nexthop.s_addr != INADDR_ANY)
363 {
364 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
365 stream_put_in_addr (s, &path->nexthop);
366 }
367 else
368 {
369 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
370 if (path->oi)
371 stream_putl (s, path->oi->ifp->ifindex);
372 else
373 stream_putl (s, 0);
374 }
paul72357f22003-06-19 02:11:23 +0000375
376 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
377 {
ajs9b0e25c2004-12-08 19:06:51 +0000378 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000379 inet_ntoa (p->prefix),
380 p->prefixlen, inet_ntoa (path->nexthop));
381 }
382 }
paul718e3742002-12-13 20:15:29 +0000383
384 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000385 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000386 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000387 {
388 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
389 stream_putl (s, or->cost + or->u.ext.type2_cost);
390 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
391 stream_putl (s, or->u.ext.type2_cost);
392 else
393 stream_putl (s, or->cost);
394 }
paul718e3742002-12-13 20:15:29 +0000395
396 stream_putw_at (s, 0, stream_get_endp (s));
397
ajs634f9ea2005-04-11 15:51:40 +0000398 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000399 }
400}
401
402void
403ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
404{
405 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000406 struct ospf_path *path;
407 struct in_addr *nexthop;
paul1eb8ef22005-04-07 07:30:20 +0000408 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000409
410 if (zclient->redist[ZEBRA_ROUTE_OSPF])
411 {
412 api.type = ZEBRA_ROUTE_OSPF;
413 api.flags = 0;
414 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000415 api.ifindex_num = 0;
416 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000417
paul1eb8ef22005-04-07 07:30:20 +0000418 for (ALL_LIST_ELEMENTS (or->paths, node, nnode, path))
paulcf795c52003-06-19 02:13:25 +0000419 {
paulcf795c52003-06-19 02:13:25 +0000420 if (path->nexthop.s_addr != INADDR_ANY)
421 {
pauld8e1d6b2003-08-10 04:04:41 +0000422 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000423 api.nexthop_num = 1;
424 nexthop = &path->nexthop;
425 api.nexthop = &nexthop;
426 }
hasso2db3d052004-02-11 21:52:13 +0000427 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000428 {
429 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
430 api.ifindex_num = 1;
431 api.ifindex = &path->oi->ifp->ifindex;
432 }
433 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
434 {
ajs9b0e25c2004-12-08 19:06:51 +0000435 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000436 inet_ntoa(p->prefix),
437 p->prefixlen);
438 }
paul72357f22003-06-19 02:11:23 +0000439
paul0a589352004-05-08 11:48:26 +0000440 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000441
442 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000443 {
ajs9b0e25c2004-12-08 19:06:51 +0000444 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000445 inet_ntoa (p->prefix),
446 p->prefixlen, inet_ntoa (**api.nexthop));
447 }
paulbb8ff1e2003-08-12 06:00:30 +0000448 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
449 {
ajs9b0e25c2004-12-08 19:06:51 +0000450 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000451 inet_ntoa (p->prefix),
452 p->prefixlen, *api.ifindex);
453 }
paulcf795c52003-06-19 02:13:25 +0000454 }
paul718e3742002-12-13 20:15:29 +0000455 }
456}
457
458void
459ospf_zebra_add_discard (struct prefix_ipv4 *p)
460{
461 struct zapi_ipv4 api;
462
463 if (zclient->redist[ZEBRA_ROUTE_OSPF])
464 {
465 api.type = ZEBRA_ROUTE_OSPF;
466 api.flags = ZEBRA_FLAG_BLACKHOLE;
467 api.message = 0;
468 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
469 api.nexthop_num = 0;
470 api.ifindex_num = 0;
471
paul0a589352004-05-08 11:48:26 +0000472 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000473 }
474}
475
476void
477ospf_zebra_delete_discard (struct prefix_ipv4 *p)
478{
479 struct zapi_ipv4 api;
480
481 if (zclient->redist[ZEBRA_ROUTE_OSPF])
482 {
483 api.type = ZEBRA_ROUTE_OSPF;
484 api.flags = ZEBRA_FLAG_BLACKHOLE;
485 api.message = 0;
486 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
487 api.nexthop_num = 0;
488 api.ifindex_num = 0;
489
paul0a589352004-05-08 11:48:26 +0000490 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000491
492 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000493 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000494 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000495
paul718e3742002-12-13 20:15:29 +0000496 }
497}
498
499int
500ospf_is_type_redistributed (int type)
501{
502 return (DEFAULT_ROUTE_TYPE (type)) ?
503 zclient->default_information : zclient->redist[type];
504}
505
506int
paul020709f2003-04-04 02:44:16 +0000507ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000508{
509 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000510
paul718e3742002-12-13 20:15:29 +0000511 if (ospf_is_type_redistributed (type))
512 {
paul68980082003-03-25 05:07:42 +0000513 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000514 {
515 ospf->dmetric[type].type = mtype;
516 force = LSA_REFRESH_FORCE;
517 }
paul68980082003-03-25 05:07:42 +0000518 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000519 {
520 ospf->dmetric[type].value = mvalue;
521 force = LSA_REFRESH_FORCE;
522 }
523
paul68980082003-03-25 05:07:42 +0000524 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000525
paul718e3742002-12-13 20:15:29 +0000526 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000527 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000528 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000529 metric_type (ospf, type), metric_value (ospf, type));
530
paul718e3742002-12-13 20:15:29 +0000531 return CMD_SUCCESS;
532 }
533
paul68980082003-03-25 05:07:42 +0000534 ospf->dmetric[type].type = mtype;
535 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000536
paul0a589352004-05-08 11:48:26 +0000537 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000538
539 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000540 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000541 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000542 metric_type (ospf, type), metric_value (ospf, type));
543
paul68980082003-03-25 05:07:42 +0000544 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000545
546 return CMD_SUCCESS;
547}
548
549int
paul020709f2003-04-04 02:44:16 +0000550ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000551{
552 if (type == zclient->redist_default)
553 return CMD_SUCCESS;
554
paulcf795c52003-06-19 02:13:25 +0000555 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000556 return CMD_SUCCESS;
557
paul0a589352004-05-08 11:48:26 +0000558 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000559
paul718e3742002-12-13 20:15:29 +0000560 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000561 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000562 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000563
paul68980082003-03-25 05:07:42 +0000564 ospf->dmetric[type].type = -1;
565 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000566
567 /* Remove the routes from OSPF table. */
Paul Jakma6db3a6f2006-05-12 23:02:46 +0000568 ospf_redistribute_withdraw (ospf, type);
paul718e3742002-12-13 20:15:29 +0000569
paul68980082003-03-25 05:07:42 +0000570 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000571
572 return CMD_SUCCESS;
573}
574
575int
paul020709f2003-04-04 02:44:16 +0000576ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000577 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000578{
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000579 ospf->default_originate = originate;
580 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
581 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul020709f2003-04-04 02:44:16 +0000582
paul718e3742002-12-13 20:15:29 +0000583 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
584 {
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000585 /* if ospf->default_originate changes value, is calling
586 ospf_external_lsa_refresh_default sufficient to implement
587 the change? */
paul68980082003-03-25 05:07:42 +0000588 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000589
paul718e3742002-12-13 20:15:29 +0000590 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000591 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000592 ospf_redist_string(DEFAULT_ROUTE),
paulcf795c52003-06-19 02:13:25 +0000593 metric_type (ospf, DEFAULT_ROUTE),
594 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000595 return CMD_SUCCESS;
596 }
597
paul0a589352004-05-08 11:48:26 +0000598 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000599
paul718e3742002-12-13 20:15:29 +0000600 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000601 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000602 metric_type (ospf, DEFAULT_ROUTE),
603 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000604
paul68980082003-03-25 05:07:42 +0000605 if (ospf->router_id.s_addr == 0)
606 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000607 else
Paul Jakma4021b602006-05-12 22:55:41 +0000608 thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
paul718e3742002-12-13 20:15:29 +0000609
paul68980082003-03-25 05:07:42 +0000610 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000611
612 return CMD_SUCCESS;
613}
614
615int
paul020709f2003-04-04 02:44:16 +0000616ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000617{
618 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
619 return CMD_SUCCESS;
620
paul68980082003-03-25 05:07:42 +0000621 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
622 ospf->dmetric[DEFAULT_ROUTE].type = -1;
623 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000624
paul0a589352004-05-08 11:48:26 +0000625 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000626
627 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000628 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000629
paul68980082003-03-25 05:07:42 +0000630 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000631
632 return CMD_SUCCESS;
633}
634
paul4dadc292005-05-06 21:37:42 +0000635static int
paul020709f2003-04-04 02:44:16 +0000636ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000637 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000638{
639 /* If prefix is multicast, then do not originate LSA. */
640 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
641 {
642 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000643 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000644 return 0;
645 }
646
647 /* Take care of default-originate. */
648 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000649 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000650 {
paulcf795c52003-06-19 02:13:25 +0000651 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
652 "for default");
653 return 0;
paul718e3742002-12-13 20:15:29 +0000654 }
655
656 return 1;
657}
658
659/* If connected prefix is OSPF enable interface, then do not announce. */
660int
paulcf795c52003-06-19 02:13:25 +0000661ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000662{
663 struct route_node *rn;
664
paul68980082003-03-25 05:07:42 +0000665 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000666 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000667 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
668 {
669 route_unlock_node (rn);
670 return 0;
671 }
paul718e3742002-12-13 20:15:29 +0000672
673 return 1;
674}
675
676/* return 1 if external LSA must be originated, 0 otherwise */
677int
paul68980082003-03-25 05:07:42 +0000678ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000679 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000680{
681 struct route_map_set_values save_values;
682 struct prefix_ipv4 *p = &ei->p;
683 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000684
paul718e3742002-12-13 20:15:29 +0000685 if (changed)
686 *changed = 0;
687
paul020709f2003-04-04 02:44:16 +0000688 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000689 return 0;
690
691 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000692 if (type == ZEBRA_ROUTE_CONNECT &&
693 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000694 return 0;
695
paul020709f2003-04-04 02:44:16 +0000696 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000697 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000698 if (DISTRIBUTE_LIST (ospf, type))
699 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000700 {
701 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000702 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000703 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000704 inet_ntoa (p->prefix), p->prefixlen);
705 return 0;
706 }
paul718e3742002-12-13 20:15:29 +0000707
708 save_values = ei->route_map_set;
709 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000710
paul718e3742002-12-13 20:15:29 +0000711 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000712 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000713 {
714 int ret;
715
paulcf795c52003-06-19 02:13:25 +0000716 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
717 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000718
719 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000720 {
721 ei->route_map_set = save_values;
722 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000723 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000724 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000725 inet_ntoa (p->prefix), p->prefixlen);
726 return 0;
727 }
728
paul718e3742002-12-13 20:15:29 +0000729 /* check if 'route-map set' changed something */
730 if (changed)
paulcf795c52003-06-19 02:13:25 +0000731 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
732 &save_values);
paul718e3742002-12-13 20:15:29 +0000733 }
734
735 return 1;
736}
737
738/* OSPF route-map set for redistribution */
739void
paul6c835672004-10-11 11:00:30 +0000740ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000741{
paul020709f2003-04-04 02:44:16 +0000742 if (ROUTEMAP_NAME (ospf, type))
743 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000744
paul020709f2003-04-04 02:44:16 +0000745 ROUTEMAP_NAME (ospf, type) = strdup (name);
746 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000747}
748
749void
paul020709f2003-04-04 02:44:16 +0000750ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000751{
paul020709f2003-04-04 02:44:16 +0000752 if (ROUTEMAP_NAME (ospf, type))
753 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000754
paul020709f2003-04-04 02:44:16 +0000755 ROUTEMAP_NAME (ospf, type) = NULL;
756 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000757}
758
759/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000760static int
paul718e3742002-12-13 20:15:29 +0000761ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000762 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000763{
764 struct stream *s;
765 struct zapi_ipv4 api;
766 unsigned long ifindex;
767 struct in_addr nexthop;
768 struct prefix_ipv4 p;
769 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000770 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000771
772 s = zclient->ibuf;
773 ifindex = 0;
774 nexthop.s_addr = 0;
775
776 /* Type, flags, message. */
777 api.type = stream_getc (s);
778 api.flags = stream_getc (s);
779 api.message = stream_getc (s);
780
781 /* IPv4 prefix. */
782 memset (&p, 0, sizeof (struct prefix_ipv4));
783 p.family = AF_INET;
784 p.prefixlen = stream_getc (s);
785 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
786
hasso8585d4e2004-04-20 17:25:12 +0000787 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
788 return 0;
789
paul718e3742002-12-13 20:15:29 +0000790 /* Nexthop, ifindex, distance, metric. */
791 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
792 {
793 api.nexthop_num = stream_getc (s);
794 nexthop.s_addr = stream_get_ipv4 (s);
795 }
796 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
797 {
798 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000799 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000800 ifindex = stream_getl (s);
801 }
802 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
803 api.distance = stream_getc (s);
804 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
805 api.metric = stream_getl (s);
806
paul020709f2003-04-04 02:44:16 +0000807 ospf = ospf_lookup ();
808 if (ospf == NULL)
809 return 0;
810
paul718e3742002-12-13 20:15:29 +0000811 if (command == ZEBRA_IPV4_ROUTE_ADD)
812 {
paul7021c422003-07-15 12:52:22 +0000813 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000814 * Maybe we should ignore reject/blackhole routes? Testing shows that
815 * there is no problems though and this is only way to "summarize"
816 * routes in ASBR at the moment. Maybe we need just a better generalised
817 * solution for these types?
818 *
819 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
820 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
821 * return 0;
paul7021c422003-07-15 12:52:22 +0000822 */
paul7021c422003-07-15 12:52:22 +0000823
paul718e3742002-12-13 20:15:29 +0000824 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
825
paul68980082003-03-25 05:07:42 +0000826 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000827 /* Set flags to generate AS-external-LSA originate event
828 for each redistributed protocols later. */
829 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000830 else
paulcf795c52003-06-19 02:13:25 +0000831 {
832 if (ei)
833 {
834 if (is_prefix_default (&p))
835 ospf_external_lsa_refresh_default (ospf);
836 else
837 {
838 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000839
paulcf795c52003-06-19 02:13:25 +0000840 current = ospf_external_info_find_lsa (ospf, &ei->p);
841 if (!current)
842 ospf_external_lsa_originate (ospf, ei);
843 else if (IS_LSA_MAXAGE (current))
844 ospf_external_lsa_refresh (ospf, current,
845 ei, LSA_REFRESH_FORCE);
846 else
847 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
848 inet_ntoa (p.prefix));
849 }
850 }
851 }
paul718e3742002-12-13 20:15:29 +0000852 }
paulcf795c52003-06-19 02:13:25 +0000853 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000854 {
855 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000856 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000857 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000858 else
ajs5339cfd2005-09-19 13:28:05 +0000859 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000860 }
861
862 return 0;
863}
paul718e3742002-12-13 20:15:29 +0000864
paulcf795c52003-06-19 02:13:25 +0000865
paul718e3742002-12-13 20:15:29 +0000866int
paul6c835672004-10-11 11:00:30 +0000867ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000868{
869 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000870 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000871
872 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000873 if (DISTRIBUTE_NAME (ospf, type))
874 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000875
876 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000877 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000878
879 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000880 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000881 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000882
883 return CMD_SUCCESS;
884}
885
886int
paul6c835672004-10-11 11:00:30 +0000887ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000888{
889 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000890 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000891 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000892
893 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000894 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000895
896 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000897 if (DISTRIBUTE_NAME (ospf, type))
898 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000899
paul020709f2003-04-04 02:44:16 +0000900 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000901
902 return CMD_SUCCESS;
903}
904
905/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +0000906static int
paul718e3742002-12-13 20:15:29 +0000907ospf_distribute_list_update_timer (struct thread *thread)
908{
909 struct route_node *rn;
910 struct external_info *ei;
911 struct route_table *rt;
912 struct ospf_lsa *lsa;
paul4dadc292005-05-06 21:37:42 +0000913 intptr_t type;
paul020709f2003-04-04 02:44:16 +0000914 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000915
paul4dadc292005-05-06 21:37:42 +0000916 type = (intptr_t)THREAD_ARG (thread);
hasso01018ce2005-08-05 07:40:15 +0000917 assert (type <= ZEBRA_ROUTE_MAX);
paul64511f32004-10-31 18:01:13 +0000918
paul718e3742002-12-13 20:15:29 +0000919 rt = EXTERNAL_INFO (type);
920
paul020709f2003-04-04 02:44:16 +0000921 ospf = ospf_lookup ();
922 if (ospf == NULL)
923 return 0;
924
paul68980082003-03-25 05:07:42 +0000925 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000926
927 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
928
929 /* foreach all external info. */
930 if (rt)
931 for (rn = route_top (rt); rn; rn = route_next (rn))
932 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000933 {
934 if (is_prefix_default (&ei->p))
935 ospf_external_lsa_refresh_default (ospf);
936 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
937 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
938 else
939 ospf_external_lsa_originate (ospf, ei);
940 }
paul718e3742002-12-13 20:15:29 +0000941 return 0;
942}
943
944#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
945
946/* Update distribute-list and set timer to apply access-list. */
947void
paul68980082003-03-25 05:07:42 +0000948ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000949{
950 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000951
paul718e3742002-12-13 20:15:29 +0000952 /* External info does not exist. */
953 if (!(rt = EXTERNAL_INFO (type)))
954 return;
955
956 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000957 if (ospf->t_distribute_update)
958 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000959
960 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000961 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000962 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000963 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000964}
965
966/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +0000967static void
paul718e3742002-12-13 20:15:29 +0000968ospf_filter_update (struct access_list *access)
969{
paul020709f2003-04-04 02:44:16 +0000970 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000971 int type;
972 int abr_inv = 0;
973 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000974 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000975
976 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000977 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000978 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000979 return;
980
paul718e3742002-12-13 20:15:29 +0000981 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +0000982 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +0000983 {
paul020709f2003-04-04 02:44:16 +0000984 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +0000985 {
986 /* if route-map is not NULL it may be using this access list */
987 ospf_distribute_list_update (ospf, type);
988 continue;
989 }
990
hasso01018ce2005-08-05 07:40:15 +0000991 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
992 * but no distribute list. */
993 if (type == ZEBRA_ROUTE_MAX)
994 break;
paul718e3742002-12-13 20:15:29 +0000995
paul020709f2003-04-04 02:44:16 +0000996 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +0000997 {
998 /* Keep old access-list for distribute-list. */
999 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1000
1001 /* Update access-list for distribute-list. */
1002 DISTRIBUTE_LIST (ospf, type) =
1003 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1004
1005 /* No update for this distribute type. */
1006 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1007 continue;
1008
1009 /* Schedule distribute-list update timer. */
1010 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1011 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1012 ospf_distribute_list_update (ospf, type);
1013 }
paul718e3742002-12-13 20:15:29 +00001014 }
1015
1016 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001017 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1018 {
1019 if (EXPORT_NAME (area))
1020 {
1021 EXPORT_LIST (area) = NULL;
1022 abr_inv++;
1023 }
paul718e3742002-12-13 20:15:29 +00001024
paul1eb8ef22005-04-07 07:30:20 +00001025 if (IMPORT_NAME (area))
1026 {
1027 IMPORT_LIST (area) = NULL;
1028 abr_inv++;
1029 }
1030 }
paul718e3742002-12-13 20:15:29 +00001031
1032 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001033 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001034 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001035}
hassodd669bb2004-05-10 07:43:59 +00001036
1037/* If prefix-list is updated, do some updates. */
1038void
1039ospf_prefix_list_update (struct prefix_list *plist)
1040{
1041 struct ospf *ospf;
1042 int type;
1043 int abr_inv = 0;
1044 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001045 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001046
1047 /* If OSPF instatnce does not exist, return right now. */
1048 ospf = ospf_lookup ();
1049 if (ospf == NULL)
1050 return;
1051
1052 /* Update all route-maps which are used as redistribution filters.
1053 * They might use prefix-list.
1054 */
hasso01018ce2005-08-05 07:40:15 +00001055 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001056 {
1057 if (ROUTEMAP (ospf, type) != NULL)
1058 {
1059 /* If route-map is not NULL it may be using this prefix list */
1060 ospf_distribute_list_update (ospf, type);
1061 continue;
1062 }
1063 }
1064
1065 /* Update area filter-lists. */
paul1eb8ef22005-04-07 07:30:20 +00001066 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1067 {
1068 /* Update filter-list in. */
1069 if (PREFIX_NAME_IN (area))
1070 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1071 {
1072 PREFIX_LIST_IN (area) =
1073 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1074 abr_inv++;
1075 }
hassodd669bb2004-05-10 07:43:59 +00001076
paul1eb8ef22005-04-07 07:30:20 +00001077 /* Update filter-list out. */
1078 if (PREFIX_NAME_OUT (area))
1079 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1080 {
1081 PREFIX_LIST_IN (area) =
1082 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1083 abr_inv++;
1084 }
1085 }
hassodd669bb2004-05-10 07:43:59 +00001086
1087 /* Schedule ABR task. */
1088 if (IS_OSPF_ABR (ospf) && abr_inv)
1089 ospf_schedule_abr_task (ospf);
1090}
paulcf795c52003-06-19 02:13:25 +00001091
paul4dadc292005-05-06 21:37:42 +00001092static struct ospf_distance *
1093ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001094{
1095 struct ospf_distance *new;
1096 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1097 memset (new, 0, sizeof (struct ospf_distance));
1098 return new;
1099}
1100
paul4dadc292005-05-06 21:37:42 +00001101static void
paul718e3742002-12-13 20:15:29 +00001102ospf_distance_free (struct ospf_distance *odistance)
1103{
1104 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1105}
1106
1107int
paul6c835672004-10-11 11:00:30 +00001108ospf_distance_set (struct vty *vty, struct ospf *ospf,
1109 const char *distance_str,
1110 const char *ip_str,
1111 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001112{
1113 int ret;
1114 struct prefix_ipv4 p;
1115 u_char distance;
1116 struct route_node *rn;
1117 struct ospf_distance *odistance;
1118
1119 ret = str2prefix_ipv4 (ip_str, &p);
1120 if (ret == 0)
1121 {
1122 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1123 return CMD_WARNING;
1124 }
1125
1126 distance = atoi (distance_str);
1127
1128 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001129 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001130 if (rn->info)
1131 {
1132 odistance = rn->info;
1133 route_unlock_node (rn);
1134 }
1135 else
1136 {
1137 odistance = ospf_distance_new ();
1138 rn->info = odistance;
1139 }
1140
1141 /* Set distance value. */
1142 odistance->distance = distance;
1143
1144 /* Reset access-list configuration. */
1145 if (odistance->access_list)
1146 {
1147 free (odistance->access_list);
1148 odistance->access_list = NULL;
1149 }
1150 if (access_list_str)
1151 odistance->access_list = strdup (access_list_str);
1152
1153 return CMD_SUCCESS;
1154}
1155
1156int
paul6c835672004-10-11 11:00:30 +00001157ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1158 const char *distance_str,
1159 const char *ip_str, char
1160 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001161{
1162 int ret;
1163 struct prefix_ipv4 p;
1164 u_char distance;
1165 struct route_node *rn;
1166 struct ospf_distance *odistance;
1167
1168 ret = str2prefix_ipv4 (ip_str, &p);
1169 if (ret == 0)
1170 {
1171 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1172 return CMD_WARNING;
1173 }
1174
1175 distance = atoi (distance_str);
1176
paulcf795c52003-06-19 02:13:25 +00001177 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1178 if (!rn)
paul718e3742002-12-13 20:15:29 +00001179 {
1180 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1181 return CMD_WARNING;
1182 }
1183
1184 odistance = rn->info;
1185
1186 if (odistance->access_list)
1187 free (odistance->access_list);
1188 ospf_distance_free (odistance);
1189
1190 rn->info = NULL;
1191 route_unlock_node (rn);
1192 route_unlock_node (rn);
1193
1194 return CMD_SUCCESS;
1195}
1196
1197void
paul68980082003-03-25 05:07:42 +00001198ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001199{
1200 struct route_node *rn;
1201 struct ospf_distance *odistance;
1202
paul68980082003-03-25 05:07:42 +00001203 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001204 if ((odistance = rn->info) != NULL)
1205 {
paulcf795c52003-06-19 02:13:25 +00001206 if (odistance->access_list)
1207 free (odistance->access_list);
1208 ospf_distance_free (odistance);
1209 rn->info = NULL;
1210 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001211 }
1212}
1213
1214u_char
1215ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1216{
paul020709f2003-04-04 02:44:16 +00001217 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001218
paul020709f2003-04-04 02:44:16 +00001219 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001220 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001221 return 0;
1222
paul68980082003-03-25 05:07:42 +00001223 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001224 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001225 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001226
paul68980082003-03-25 05:07:42 +00001227 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001228 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001229 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001230
paul68980082003-03-25 05:07:42 +00001231 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001232 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001233 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001234 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001235
paul68980082003-03-25 05:07:42 +00001236 if (ospf->distance_all)
1237 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001238
1239 return 0;
1240}
1241
1242void
1243ospf_zebra_init ()
1244{
1245 /* Allocate zebra structure. */
1246 zclient = zclient_new ();
1247 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001248 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001249 zclient->interface_add = ospf_interface_add;
1250 zclient->interface_delete = ospf_interface_delete;
1251 zclient->interface_up = ospf_interface_state_up;
1252 zclient->interface_down = ospf_interface_state_down;
1253 zclient->interface_address_add = ospf_interface_address_add;
1254 zclient->interface_address_delete = ospf_interface_address_delete;
1255 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1256 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1257
1258 access_list_add_hook (ospf_filter_update);
1259 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001260 prefix_list_add_hook (ospf_prefix_list_update);
1261 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001262}