blob: 544a0d614812762efcb35795e55a1684095900fa [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
345 /* Length place holder. */
346 stream_putw (s, 0);
347
348 /* Put command, type, flags, message. */
349 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
350 stream_putc (s, ZEBRA_ROUTE_OSPF);
351 stream_putc (s, flags);
352 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000353
paul718e3742002-12-13 20:15:29 +0000354 /* Put prefix information. */
355 psize = PSIZE (p->prefixlen);
356 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000357 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000358
359 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000360 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000361
362 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000363 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000364 {
paulcf795c52003-06-19 02:13:25 +0000365 if (path->nexthop.s_addr != INADDR_ANY)
366 {
367 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
368 stream_put_in_addr (s, &path->nexthop);
369 }
370 else
371 {
372 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
373 if (path->oi)
374 stream_putl (s, path->oi->ifp->ifindex);
375 else
376 stream_putl (s, 0);
377 }
paul72357f22003-06-19 02:11:23 +0000378
379 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
380 {
ajs9b0e25c2004-12-08 19:06:51 +0000381 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000382 inet_ntoa (p->prefix),
383 p->prefixlen, inet_ntoa (path->nexthop));
384 }
385 }
paul718e3742002-12-13 20:15:29 +0000386
387 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000388 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000389 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000390 {
391 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
392 stream_putl (s, or->cost + or->u.ext.type2_cost);
393 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
394 stream_putl (s, or->u.ext.type2_cost);
395 else
396 stream_putl (s, or->cost);
397 }
paul718e3742002-12-13 20:15:29 +0000398
399 stream_putw_at (s, 0, stream_get_endp (s));
400
ajs634f9ea2005-04-11 15:51:40 +0000401 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000402 }
403}
404
405void
406ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
407{
408 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000409 struct ospf_path *path;
410 struct in_addr *nexthop;
paul1eb8ef22005-04-07 07:30:20 +0000411 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000412
413 if (zclient->redist[ZEBRA_ROUTE_OSPF])
414 {
415 api.type = ZEBRA_ROUTE_OSPF;
416 api.flags = 0;
417 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000418 api.ifindex_num = 0;
419 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000420
paul1eb8ef22005-04-07 07:30:20 +0000421 for (ALL_LIST_ELEMENTS (or->paths, node, nnode, path))
paulcf795c52003-06-19 02:13:25 +0000422 {
paulcf795c52003-06-19 02:13:25 +0000423 if (path->nexthop.s_addr != INADDR_ANY)
424 {
pauld8e1d6b2003-08-10 04:04:41 +0000425 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000426 api.nexthop_num = 1;
427 nexthop = &path->nexthop;
428 api.nexthop = &nexthop;
429 }
hasso2db3d052004-02-11 21:52:13 +0000430 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000431 {
432 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
433 api.ifindex_num = 1;
434 api.ifindex = &path->oi->ifp->ifindex;
435 }
436 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
437 {
ajs9b0e25c2004-12-08 19:06:51 +0000438 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000439 inet_ntoa(p->prefix),
440 p->prefixlen);
441 }
paul72357f22003-06-19 02:11:23 +0000442
paul0a589352004-05-08 11:48:26 +0000443 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000444
445 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000446 {
ajs9b0e25c2004-12-08 19:06:51 +0000447 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000448 inet_ntoa (p->prefix),
449 p->prefixlen, inet_ntoa (**api.nexthop));
450 }
paulbb8ff1e2003-08-12 06:00:30 +0000451 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
452 {
ajs9b0e25c2004-12-08 19:06:51 +0000453 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000454 inet_ntoa (p->prefix),
455 p->prefixlen, *api.ifindex);
456 }
paulcf795c52003-06-19 02:13:25 +0000457 }
paul718e3742002-12-13 20:15:29 +0000458 }
459}
460
461void
462ospf_zebra_add_discard (struct prefix_ipv4 *p)
463{
464 struct zapi_ipv4 api;
465
466 if (zclient->redist[ZEBRA_ROUTE_OSPF])
467 {
468 api.type = ZEBRA_ROUTE_OSPF;
469 api.flags = ZEBRA_FLAG_BLACKHOLE;
470 api.message = 0;
471 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
472 api.nexthop_num = 0;
473 api.ifindex_num = 0;
474
paul0a589352004-05-08 11:48:26 +0000475 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000476 }
477}
478
479void
480ospf_zebra_delete_discard (struct prefix_ipv4 *p)
481{
482 struct zapi_ipv4 api;
483
484 if (zclient->redist[ZEBRA_ROUTE_OSPF])
485 {
486 api.type = ZEBRA_ROUTE_OSPF;
487 api.flags = ZEBRA_FLAG_BLACKHOLE;
488 api.message = 0;
489 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
490 api.nexthop_num = 0;
491 api.ifindex_num = 0;
492
paul0a589352004-05-08 11:48:26 +0000493 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000494
495 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000496 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000497 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000498
paul718e3742002-12-13 20:15:29 +0000499 }
500}
501
502int
503ospf_is_type_redistributed (int type)
504{
505 return (DEFAULT_ROUTE_TYPE (type)) ?
506 zclient->default_information : zclient->redist[type];
507}
508
509int
paul020709f2003-04-04 02:44:16 +0000510ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000511{
512 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000513
paul718e3742002-12-13 20:15:29 +0000514 if (ospf_is_type_redistributed (type))
515 {
paul68980082003-03-25 05:07:42 +0000516 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000517 {
518 ospf->dmetric[type].type = mtype;
519 force = LSA_REFRESH_FORCE;
520 }
paul68980082003-03-25 05:07:42 +0000521 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000522 {
523 ospf->dmetric[type].value = mvalue;
524 force = LSA_REFRESH_FORCE;
525 }
526
paul68980082003-03-25 05:07:42 +0000527 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000528
paul718e3742002-12-13 20:15:29 +0000529 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000530 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000531 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000532 metric_type (ospf, type), metric_value (ospf, type));
533
paul718e3742002-12-13 20:15:29 +0000534 return CMD_SUCCESS;
535 }
536
paul68980082003-03-25 05:07:42 +0000537 ospf->dmetric[type].type = mtype;
538 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000539
paul0a589352004-05-08 11:48:26 +0000540 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000541
542 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000543 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000544 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000545 metric_type (ospf, type), metric_value (ospf, type));
546
paul68980082003-03-25 05:07:42 +0000547 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000548
549 return CMD_SUCCESS;
550}
551
552int
paul020709f2003-04-04 02:44:16 +0000553ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000554{
555 if (type == zclient->redist_default)
556 return CMD_SUCCESS;
557
paulcf795c52003-06-19 02:13:25 +0000558 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000559 return CMD_SUCCESS;
560
paul0a589352004-05-08 11:48:26 +0000561 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000562
paul718e3742002-12-13 20:15:29 +0000563 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000564 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000565 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000566
paul68980082003-03-25 05:07:42 +0000567 ospf->dmetric[type].type = -1;
568 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000569
570 /* Remove the routes from OSPF table. */
571 ospf_redistribute_withdraw (type);
572
paul68980082003-03-25 05:07:42 +0000573 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000574
575 return CMD_SUCCESS;
576}
577
578int
paul020709f2003-04-04 02:44:16 +0000579ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000580 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000581{
582 int force = 0;
paul020709f2003-04-04 02:44:16 +0000583
paul718e3742002-12-13 20:15:29 +0000584 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
585 {
paul68980082003-03-25 05:07:42 +0000586 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000587 {
588 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
589 force = 1;
590 }
paul68980082003-03-25 05:07:42 +0000591 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000592 {
593 force = 1;
594 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
595 }
596
paul68980082003-03-25 05:07:42 +0000597 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000598
paul718e3742002-12-13 20:15:29 +0000599 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000600 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000601 ospf_redist_string(DEFAULT_ROUTE),
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 return CMD_SUCCESS;
605 }
606
paul68980082003-03-25 05:07:42 +0000607 ospf->default_originate = originate;
608 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
609 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000610
paul0a589352004-05-08 11:48:26 +0000611 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000612
paul718e3742002-12-13 20:15:29 +0000613 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000614 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000615 metric_type (ospf, DEFAULT_ROUTE),
616 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000617
paul68980082003-03-25 05:07:42 +0000618 if (ospf->router_id.s_addr == 0)
619 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000620 else
621 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000622 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000623
paul68980082003-03-25 05:07:42 +0000624 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000625
626 return CMD_SUCCESS;
627}
628
629int
paul020709f2003-04-04 02:44:16 +0000630ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000631{
632 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
633 return CMD_SUCCESS;
634
paul68980082003-03-25 05:07:42 +0000635 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
636 ospf->dmetric[DEFAULT_ROUTE].type = -1;
637 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000638
paul0a589352004-05-08 11:48:26 +0000639 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000640
641 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000642 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000643
paul68980082003-03-25 05:07:42 +0000644 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000645
646 return CMD_SUCCESS;
647}
648
paul4dadc292005-05-06 21:37:42 +0000649static int
paul020709f2003-04-04 02:44:16 +0000650ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000651 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000652{
653 /* If prefix is multicast, then do not originate LSA. */
654 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
655 {
656 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000657 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000658 return 0;
659 }
660
661 /* Take care of default-originate. */
662 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000663 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000664 {
paulcf795c52003-06-19 02:13:25 +0000665 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
666 "for default");
667 return 0;
paul718e3742002-12-13 20:15:29 +0000668 }
669
670 return 1;
671}
672
673/* If connected prefix is OSPF enable interface, then do not announce. */
674int
paulcf795c52003-06-19 02:13:25 +0000675ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000676{
677 struct route_node *rn;
678
paul68980082003-03-25 05:07:42 +0000679 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000680 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000681 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
682 {
683 route_unlock_node (rn);
684 return 0;
685 }
paul718e3742002-12-13 20:15:29 +0000686
687 return 1;
688}
689
690/* return 1 if external LSA must be originated, 0 otherwise */
691int
paul68980082003-03-25 05:07:42 +0000692ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000693 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000694{
695 struct route_map_set_values save_values;
696 struct prefix_ipv4 *p = &ei->p;
697 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000698
paul718e3742002-12-13 20:15:29 +0000699 if (changed)
700 *changed = 0;
701
paul020709f2003-04-04 02:44:16 +0000702 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000703 return 0;
704
705 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000706 if (type == ZEBRA_ROUTE_CONNECT &&
707 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000708 return 0;
709
paul020709f2003-04-04 02:44:16 +0000710 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000711 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000712 if (DISTRIBUTE_LIST (ospf, type))
713 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000714 {
715 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000716 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000717 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000718 inet_ntoa (p->prefix), p->prefixlen);
719 return 0;
720 }
paul718e3742002-12-13 20:15:29 +0000721
722 save_values = ei->route_map_set;
723 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000724
paul718e3742002-12-13 20:15:29 +0000725 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000726 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000727 {
728 int ret;
729
paulcf795c52003-06-19 02:13:25 +0000730 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
731 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000732
733 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000734 {
735 ei->route_map_set = save_values;
736 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000737 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000738 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000739 inet_ntoa (p->prefix), p->prefixlen);
740 return 0;
741 }
742
paul718e3742002-12-13 20:15:29 +0000743 /* check if 'route-map set' changed something */
744 if (changed)
paulcf795c52003-06-19 02:13:25 +0000745 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
746 &save_values);
paul718e3742002-12-13 20:15:29 +0000747 }
748
749 return 1;
750}
751
752/* OSPF route-map set for redistribution */
753void
paul6c835672004-10-11 11:00:30 +0000754ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000755{
paul020709f2003-04-04 02:44:16 +0000756 if (ROUTEMAP_NAME (ospf, type))
757 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000758
paul020709f2003-04-04 02:44:16 +0000759 ROUTEMAP_NAME (ospf, type) = strdup (name);
760 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000761}
762
763void
paul020709f2003-04-04 02:44:16 +0000764ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000765{
paul020709f2003-04-04 02:44:16 +0000766 if (ROUTEMAP_NAME (ospf, type))
767 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000768
paul020709f2003-04-04 02:44:16 +0000769 ROUTEMAP_NAME (ospf, type) = NULL;
770 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000771}
772
773/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000774static int
paul718e3742002-12-13 20:15:29 +0000775ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000776 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000777{
778 struct stream *s;
779 struct zapi_ipv4 api;
780 unsigned long ifindex;
781 struct in_addr nexthop;
782 struct prefix_ipv4 p;
783 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000784 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000785
786 s = zclient->ibuf;
787 ifindex = 0;
788 nexthop.s_addr = 0;
789
790 /* Type, flags, message. */
791 api.type = stream_getc (s);
792 api.flags = stream_getc (s);
793 api.message = stream_getc (s);
794
795 /* IPv4 prefix. */
796 memset (&p, 0, sizeof (struct prefix_ipv4));
797 p.family = AF_INET;
798 p.prefixlen = stream_getc (s);
799 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
800
hasso8585d4e2004-04-20 17:25:12 +0000801 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
802 return 0;
803
paul718e3742002-12-13 20:15:29 +0000804 /* Nexthop, ifindex, distance, metric. */
805 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
806 {
807 api.nexthop_num = stream_getc (s);
808 nexthop.s_addr = stream_get_ipv4 (s);
809 }
810 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
811 {
812 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000813 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000814 ifindex = stream_getl (s);
815 }
816 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
817 api.distance = stream_getc (s);
818 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
819 api.metric = stream_getl (s);
820
paul020709f2003-04-04 02:44:16 +0000821 ospf = ospf_lookup ();
822 if (ospf == NULL)
823 return 0;
824
paul718e3742002-12-13 20:15:29 +0000825 if (command == ZEBRA_IPV4_ROUTE_ADD)
826 {
paul7021c422003-07-15 12:52:22 +0000827 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000828 * Maybe we should ignore reject/blackhole routes? Testing shows that
829 * there is no problems though and this is only way to "summarize"
830 * routes in ASBR at the moment. Maybe we need just a better generalised
831 * solution for these types?
832 *
833 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
834 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
835 * return 0;
paul7021c422003-07-15 12:52:22 +0000836 */
paul7021c422003-07-15 12:52:22 +0000837
paul718e3742002-12-13 20:15:29 +0000838 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
839
paul68980082003-03-25 05:07:42 +0000840 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000841 /* Set flags to generate AS-external-LSA originate event
842 for each redistributed protocols later. */
843 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000844 else
paulcf795c52003-06-19 02:13:25 +0000845 {
846 if (ei)
847 {
848 if (is_prefix_default (&p))
849 ospf_external_lsa_refresh_default (ospf);
850 else
851 {
852 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000853
paulcf795c52003-06-19 02:13:25 +0000854 current = ospf_external_info_find_lsa (ospf, &ei->p);
855 if (!current)
856 ospf_external_lsa_originate (ospf, ei);
857 else if (IS_LSA_MAXAGE (current))
858 ospf_external_lsa_refresh (ospf, current,
859 ei, LSA_REFRESH_FORCE);
860 else
861 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
862 inet_ntoa (p.prefix));
863 }
864 }
865 }
paul718e3742002-12-13 20:15:29 +0000866 }
paulcf795c52003-06-19 02:13:25 +0000867 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000868 {
869 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000870 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000871 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000872 else
ajs5339cfd2005-09-19 13:28:05 +0000873 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000874 }
875
876 return 0;
877}
paul718e3742002-12-13 20:15:29 +0000878
paulcf795c52003-06-19 02:13:25 +0000879
paul718e3742002-12-13 20:15:29 +0000880int
paul6c835672004-10-11 11:00:30 +0000881ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000882{
883 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000884 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000885
886 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000887 if (DISTRIBUTE_NAME (ospf, type))
888 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000889
890 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000891 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000892
893 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000894 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000895 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000896
897 return CMD_SUCCESS;
898}
899
900int
paul6c835672004-10-11 11:00:30 +0000901ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000902{
903 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000904 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000905 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000906
907 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000908 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000909
910 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000911 if (DISTRIBUTE_NAME (ospf, type))
912 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000913
paul020709f2003-04-04 02:44:16 +0000914 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000915
916 return CMD_SUCCESS;
917}
918
919/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +0000920static int
paul718e3742002-12-13 20:15:29 +0000921ospf_distribute_list_update_timer (struct thread *thread)
922{
923 struct route_node *rn;
924 struct external_info *ei;
925 struct route_table *rt;
926 struct ospf_lsa *lsa;
paul4dadc292005-05-06 21:37:42 +0000927 intptr_t type;
paul020709f2003-04-04 02:44:16 +0000928 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000929
paul4dadc292005-05-06 21:37:42 +0000930 type = (intptr_t)THREAD_ARG (thread);
hasso01018ce2005-08-05 07:40:15 +0000931 assert (type <= ZEBRA_ROUTE_MAX);
paul64511f32004-10-31 18:01:13 +0000932
paul718e3742002-12-13 20:15:29 +0000933 rt = EXTERNAL_INFO (type);
934
paul020709f2003-04-04 02:44:16 +0000935 ospf = ospf_lookup ();
936 if (ospf == NULL)
937 return 0;
938
paul68980082003-03-25 05:07:42 +0000939 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000940
941 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
942
943 /* foreach all external info. */
944 if (rt)
945 for (rn = route_top (rt); rn; rn = route_next (rn))
946 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000947 {
948 if (is_prefix_default (&ei->p))
949 ospf_external_lsa_refresh_default (ospf);
950 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
951 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
952 else
953 ospf_external_lsa_originate (ospf, ei);
954 }
paul718e3742002-12-13 20:15:29 +0000955 return 0;
956}
957
958#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
959
960/* Update distribute-list and set timer to apply access-list. */
961void
paul68980082003-03-25 05:07:42 +0000962ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000963{
964 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000965
paul718e3742002-12-13 20:15:29 +0000966 /* External info does not exist. */
967 if (!(rt = EXTERNAL_INFO (type)))
968 return;
969
970 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000971 if (ospf->t_distribute_update)
972 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000973
974 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000975 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000976 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000977 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000978}
979
980/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +0000981static void
paul718e3742002-12-13 20:15:29 +0000982ospf_filter_update (struct access_list *access)
983{
paul020709f2003-04-04 02:44:16 +0000984 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000985 int type;
986 int abr_inv = 0;
987 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000988 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000989
990 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000991 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000992 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000993 return;
994
paul718e3742002-12-13 20:15:29 +0000995 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +0000996 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +0000997 {
paul020709f2003-04-04 02:44:16 +0000998 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +0000999 {
1000 /* if route-map is not NULL it may be using this access list */
1001 ospf_distribute_list_update (ospf, type);
1002 continue;
1003 }
1004
hasso01018ce2005-08-05 07:40:15 +00001005 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1006 * but no distribute list. */
1007 if (type == ZEBRA_ROUTE_MAX)
1008 break;
paul718e3742002-12-13 20:15:29 +00001009
paul020709f2003-04-04 02:44:16 +00001010 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001011 {
1012 /* Keep old access-list for distribute-list. */
1013 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1014
1015 /* Update access-list for distribute-list. */
1016 DISTRIBUTE_LIST (ospf, type) =
1017 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1018
1019 /* No update for this distribute type. */
1020 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1021 continue;
1022
1023 /* Schedule distribute-list update timer. */
1024 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1025 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1026 ospf_distribute_list_update (ospf, type);
1027 }
paul718e3742002-12-13 20:15:29 +00001028 }
1029
1030 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001031 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1032 {
1033 if (EXPORT_NAME (area))
1034 {
1035 EXPORT_LIST (area) = NULL;
1036 abr_inv++;
1037 }
paul718e3742002-12-13 20:15:29 +00001038
paul1eb8ef22005-04-07 07:30:20 +00001039 if (IMPORT_NAME (area))
1040 {
1041 IMPORT_LIST (area) = NULL;
1042 abr_inv++;
1043 }
1044 }
paul718e3742002-12-13 20:15:29 +00001045
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 */
hasso01018ce2005-08-05 07:40:15 +00001069 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001070 {
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. */
paul1eb8ef22005-04-07 07:30:20 +00001080 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1081 {
1082 /* Update filter-list in. */
1083 if (PREFIX_NAME_IN (area))
1084 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1085 {
1086 PREFIX_LIST_IN (area) =
1087 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1088 abr_inv++;
1089 }
hassodd669bb2004-05-10 07:43:59 +00001090
paul1eb8ef22005-04-07 07:30:20 +00001091 /* Update filter-list out. */
1092 if (PREFIX_NAME_OUT (area))
1093 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1094 {
1095 PREFIX_LIST_IN (area) =
1096 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1097 abr_inv++;
1098 }
1099 }
hassodd669bb2004-05-10 07:43:59 +00001100
1101 /* Schedule ABR task. */
1102 if (IS_OSPF_ABR (ospf) && abr_inv)
1103 ospf_schedule_abr_task (ospf);
1104}
paulcf795c52003-06-19 02:13:25 +00001105
paul4dadc292005-05-06 21:37:42 +00001106static struct ospf_distance *
1107ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001108{
1109 struct ospf_distance *new;
1110 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1111 memset (new, 0, sizeof (struct ospf_distance));
1112 return new;
1113}
1114
paul4dadc292005-05-06 21:37:42 +00001115static void
paul718e3742002-12-13 20:15:29 +00001116ospf_distance_free (struct ospf_distance *odistance)
1117{
1118 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1119}
1120
1121int
paul6c835672004-10-11 11:00:30 +00001122ospf_distance_set (struct vty *vty, struct ospf *ospf,
1123 const char *distance_str,
1124 const char *ip_str,
1125 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001126{
1127 int ret;
1128 struct prefix_ipv4 p;
1129 u_char distance;
1130 struct route_node *rn;
1131 struct ospf_distance *odistance;
1132
1133 ret = str2prefix_ipv4 (ip_str, &p);
1134 if (ret == 0)
1135 {
1136 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1137 return CMD_WARNING;
1138 }
1139
1140 distance = atoi (distance_str);
1141
1142 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001143 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001144 if (rn->info)
1145 {
1146 odistance = rn->info;
1147 route_unlock_node (rn);
1148 }
1149 else
1150 {
1151 odistance = ospf_distance_new ();
1152 rn->info = odistance;
1153 }
1154
1155 /* Set distance value. */
1156 odistance->distance = distance;
1157
1158 /* Reset access-list configuration. */
1159 if (odistance->access_list)
1160 {
1161 free (odistance->access_list);
1162 odistance->access_list = NULL;
1163 }
1164 if (access_list_str)
1165 odistance->access_list = strdup (access_list_str);
1166
1167 return CMD_SUCCESS;
1168}
1169
1170int
paul6c835672004-10-11 11:00:30 +00001171ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1172 const char *distance_str,
1173 const char *ip_str, char
1174 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001175{
1176 int ret;
1177 struct prefix_ipv4 p;
1178 u_char distance;
1179 struct route_node *rn;
1180 struct ospf_distance *odistance;
1181
1182 ret = str2prefix_ipv4 (ip_str, &p);
1183 if (ret == 0)
1184 {
1185 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1186 return CMD_WARNING;
1187 }
1188
1189 distance = atoi (distance_str);
1190
paulcf795c52003-06-19 02:13:25 +00001191 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1192 if (!rn)
paul718e3742002-12-13 20:15:29 +00001193 {
1194 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1195 return CMD_WARNING;
1196 }
1197
1198 odistance = rn->info;
1199
1200 if (odistance->access_list)
1201 free (odistance->access_list);
1202 ospf_distance_free (odistance);
1203
1204 rn->info = NULL;
1205 route_unlock_node (rn);
1206 route_unlock_node (rn);
1207
1208 return CMD_SUCCESS;
1209}
1210
1211void
paul68980082003-03-25 05:07:42 +00001212ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001213{
1214 struct route_node *rn;
1215 struct ospf_distance *odistance;
1216
paul68980082003-03-25 05:07:42 +00001217 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001218 if ((odistance = rn->info) != NULL)
1219 {
paulcf795c52003-06-19 02:13:25 +00001220 if (odistance->access_list)
1221 free (odistance->access_list);
1222 ospf_distance_free (odistance);
1223 rn->info = NULL;
1224 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001225 }
1226}
1227
1228u_char
1229ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1230{
paul020709f2003-04-04 02:44:16 +00001231 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001232
paul020709f2003-04-04 02:44:16 +00001233 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001234 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001235 return 0;
1236
paul68980082003-03-25 05:07:42 +00001237 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001238 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001239 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001240
paul68980082003-03-25 05:07:42 +00001241 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001242 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001243 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001244
paul68980082003-03-25 05:07:42 +00001245 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001246 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001247 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001248 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001249
paul68980082003-03-25 05:07:42 +00001250 if (ospf->distance_all)
1251 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001252
1253 return 0;
1254}
1255
1256void
1257ospf_zebra_init ()
1258{
1259 /* Allocate zebra structure. */
1260 zclient = zclient_new ();
1261 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001262 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001263 zclient->interface_add = ospf_interface_add;
1264 zclient->interface_delete = ospf_interface_delete;
1265 zclient->interface_up = ospf_interface_state_up;
1266 zclient->interface_down = ospf_interface_state_down;
1267 zclient->interface_address_add = ospf_interface_address_add;
1268 zclient->interface_address_delete = ospf_interface_address_delete;
1269 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1270 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1271
1272 access_list_add_hook (ospf_filter_update);
1273 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001274 prefix_list_add_hook (ospf_prefix_list_update);
1275 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001276}