blob: 300103e12c0191b7135e2b8cf7384bb67ffd5ae1 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Zebra connect library for OSPFd
3 * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "thread.h"
26#include "command.h"
27#include "network.h"
28#include "prefix.h"
29#include "routemap.h"
30#include "table.h"
31#include "stream.h"
32#include "memory.h"
33#include "zclient.h"
34#include "filter.h"
hassodd669bb2004-05-10 07:43:59 +000035#include "plist.h"
paul718e3742002-12-13 20:15:29 +000036#include "log.h"
37
38#include "ospfd/ospfd.h"
39#include "ospfd/ospf_interface.h"
40#include "ospfd/ospf_ism.h"
41#include "ospfd/ospf_asbr.h"
42#include "ospfd/ospf_asbr.h"
43#include "ospfd/ospf_abr.h"
44#include "ospfd/ospf_lsa.h"
45#include "ospfd/ospf_dump.h"
46#include "ospfd/ospf_route.h"
47#include "ospfd/ospf_zebra.h"
48#ifdef HAVE_SNMP
49#include "ospfd/ospf_snmp.h"
50#endif /* HAVE_SNMP */
51
52/* Zebra structure to hold current status. */
53struct zclient *zclient = NULL;
54
55/* For registering threads. */
56extern struct thread_master *master;
hasso18a6dce2004-10-03 18:18:34 +000057struct in_addr router_id_zebra;
58
59/* Router-id update message from zebra. */
60int
61ospf_router_id_update_zebra (int command, struct zclient *zclient,
62 zebra_size_t length)
63{
64 struct ospf *ospf;
65 struct prefix router_id;
66 zebra_router_id_update_read(zclient->ibuf,&router_id);
67
68 router_id_zebra = router_id.u.prefix4;
69
70 ospf = ospf_lookup ();
71 if (ospf != NULL)
72 {
73 if (ospf->t_router_id_update == NULL)
74 OSPF_TIMER_ON (ospf->t_router_id_update, ospf_router_id_update_timer,
75 OSPF_ROUTER_ID_UPDATE_DELAY);
76 }
77 return 0;
78}
paul718e3742002-12-13 20:15:29 +000079
80/* Inteface addition message from zebra. */
81int
82ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
83{
84 struct interface *ifp;
paul020709f2003-04-04 02:44:16 +000085 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +000086
87 ifp = zebra_interface_add_read (zclient->ibuf);
88
89 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +000090 zlog_debug ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +000091 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000092
paulcf795c52003-06-19 02:13:25 +000093 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000094
paul718e3742002-12-13 20:15:29 +000095 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
96 {
97 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +000098 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +000099 }
100
paul020709f2003-04-04 02:44:16 +0000101 ospf = ospf_lookup ();
102 if (ospf != NULL)
103 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000104
105#ifdef HAVE_SNMP
106 ospf_snmp_if_update (ifp);
107#endif /* HAVE_SNMP */
108
109 return 0;
110}
111
112int
113ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000114 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000115{
116 struct interface *ifp;
117 struct stream *s;
118 struct route_node *rn;
119
paulcf795c52003-06-19 02:13:25 +0000120 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000121 /* zebra_interface_state_read() updates interface structure in iflist */
122 ifp = zebra_interface_state_read (s);
123
124 if (ifp == NULL)
125 return 0;
126
127 if (if_is_up (ifp))
128 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000129 ifp->name);
130
paul718e3742002-12-13 20:15:29 +0000131 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000132 zlog_debug
paulcf795c52003-06-19 02:13:25 +0000133 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
134 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000135
136#ifdef HAVE_SNMP
137 ospf_snmp_if_delete (ifp);
138#endif /* HAVE_SNMP */
139
140 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
141 if (rn->info)
142 ospf_if_free ((struct ospf_interface *) rn->info);
143
paul718e3742002-12-13 20:15:29 +0000144 return 0;
145}
146
147struct interface *
148zebra_interface_if_lookup (struct stream *s)
149{
150 struct interface *ifp;
151 u_char ifname_tmp[INTERFACE_NAMSIZ];
152
153 /* Read interface name. */
154 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
155
156 /* Lookup this by interface index. */
hassoc9e52be2004-09-26 16:09:34 +0000157 ifp = if_lookup_by_name ((char *) ifname_tmp);
paul718e3742002-12-13 20:15:29 +0000158
159 /* If such interface does not exist, indicate an error */
160 if (!ifp)
161 return NULL;
162
163 return ifp;
164}
165
paul718e3742002-12-13 20:15:29 +0000166int
167ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000168 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000169{
170 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000171 struct ospf_interface *oi;
172 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000173
paul718e3742002-12-13 20:15:29 +0000174 ifp = zebra_interface_if_lookup (zclient->ibuf);
175
176 if (ifp == NULL)
177 return 0;
178
179 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000180 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000181 {
182 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000183 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000184 memcpy (&if_tmp, ifp, sizeof (struct interface));
185
186 zebra_interface_if_set_value (zclient->ibuf, ifp);
187
188 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000189 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000190
191 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000192 {
193 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000194 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000195 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000196
paulcf795c52003-06-19 02:13:25 +0000197 ospf_if_recalculate_output_cost (ifp);
198 }
ajsa608bbf2005-03-29 17:03:49 +0000199
200 if (if_tmp.mtu != ifp->mtu)
201 {
202 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
203 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
204 ifp->name, if_tmp.mtu, ifp->mtu);
205
206 /* Must reset the interface (simulate down/up) when MTU changes. */
207 ospf_if_reset(ifp);
208 }
paul718e3742002-12-13 20:15:29 +0000209 return 0;
210 }
paulcf795c52003-06-19 02:13:25 +0000211
paul718e3742002-12-13 20:15:29 +0000212 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000213
paul718e3742002-12-13 20:15:29 +0000214 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000215 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000216
217 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000218 {
paulcf795c52003-06-19 02:13:25 +0000219 if ((oi = rn->info) == NULL)
220 continue;
221
paul718e3742002-12-13 20:15:29 +0000222 ospf_if_up (oi);
223 }
paulcf795c52003-06-19 02:13:25 +0000224
paul718e3742002-12-13 20:15:29 +0000225 return 0;
226}
227
228int
229ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000230 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000231{
232 struct interface *ifp;
233 struct ospf_interface *oi;
234 struct route_node *node;
235
236 ifp = zebra_interface_state_read (zclient->ibuf);
237
238 if (ifp == NULL)
239 return 0;
240
241 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000242 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000243
paulcf795c52003-06-19 02:13:25 +0000244 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000245 {
paulcf795c52003-06-19 02:13:25 +0000246 if ((oi = node->info) == NULL)
247 continue;
paul718e3742002-12-13 20:15:29 +0000248 ospf_if_down (oi);
249 }
250
251 return 0;
252}
253
254int
255ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000256 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000257{
paul020709f2003-04-04 02:44:16 +0000258 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000259 struct connected *c;
260
paul0a589352004-05-08 11:48:26 +0000261 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000262
263 if (c == NULL)
264 return 0;
265
paul020709f2003-04-04 02:44:16 +0000266 ospf = ospf_lookup ();
267 if (ospf != NULL)
268 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000269
270#ifdef HAVE_SNMP
271 ospf_snmp_if_update (c->ifp);
272#endif /* HAVE_SNMP */
273
274 return 0;
275}
276
277int
278ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000279 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000280{
paul020709f2003-04-04 02:44:16 +0000281 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000282 struct connected *c;
283 struct interface *ifp;
284 struct ospf_interface *oi;
285 struct route_node *rn;
286 struct prefix p;
287
paul0a589352004-05-08 11:48:26 +0000288 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000289
290 if (c == NULL)
291 return 0;
292
293 ifp = c->ifp;
294 p = *c->address;
295 p.prefixlen = IPV4_MAX_PREFIXLEN;
296
297 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000298 if (!rn)
paul718e3742002-12-13 20:15:29 +0000299 return 0;
300
301 assert (rn->info);
302 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000303
paul718e3742002-12-13 20:15:29 +0000304 /* Call interface hook functions to clean up */
305 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000306
paul718e3742002-12-13 20:15:29 +0000307#ifdef HAVE_SNMP
308 ospf_snmp_if_update (c->ifp);
309#endif /* HAVE_SNMP */
310
311 connected_free (c);
312
paul020709f2003-04-04 02:44:16 +0000313 ospf = ospf_lookup ();
314 if (ospf != NULL)
315 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000316
317 return 0;
318}
paul72357f22003-06-19 02:11:23 +0000319
paul718e3742002-12-13 20:15:29 +0000320void
321ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
322{
323 u_char message;
324 u_char distance;
325 u_char flags;
326 int psize;
327 struct stream *s;
328 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000329 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000330
331 if (zclient->redist[ZEBRA_ROUTE_OSPF])
332 {
333 message = 0;
334 flags = 0;
335
336 /* OSPF pass nexthop and metric */
337 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
338 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
339
340 /* Distance value. */
341 distance = ospf_distance_apply (p, or);
342 if (distance)
paul72357f22003-06-19 02:11:23 +0000343 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000344
345 /* Make packet. */
346 s = zclient->obuf;
347 stream_reset (s);
348
349 /* Length place holder. */
350 stream_putw (s, 0);
351
352 /* Put command, type, flags, message. */
353 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
354 stream_putc (s, ZEBRA_ROUTE_OSPF);
355 stream_putc (s, flags);
356 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000357
paul718e3742002-12-13 20:15:29 +0000358 /* Put prefix information. */
359 psize = PSIZE (p->prefixlen);
360 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000361 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000362
363 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000364 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000365
366 /* Nexthop, ifindex, distance and metric information. */
paul96735ee2003-08-10 02:51:22 +0000367 for (node = listhead (or->paths); node; nextnode (node))
paul72357f22003-06-19 02:11:23 +0000368 {
paulcf795c52003-06-19 02:13:25 +0000369 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000370
paulcf795c52003-06-19 02:13:25 +0000371 if (path->nexthop.s_addr != INADDR_ANY)
372 {
373 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
374 stream_put_in_addr (s, &path->nexthop);
375 }
376 else
377 {
378 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
379 if (path->oi)
380 stream_putl (s, path->oi->ifp->ifindex);
381 else
382 stream_putl (s, 0);
383 }
paul72357f22003-06-19 02:11:23 +0000384
385 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
386 {
ajs9b0e25c2004-12-08 19:06:51 +0000387 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000388 inet_ntoa (p->prefix),
389 p->prefixlen, inet_ntoa (path->nexthop));
390 }
391 }
paul718e3742002-12-13 20:15:29 +0000392
393 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000394 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000395 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000396 {
397 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
398 stream_putl (s, or->cost + or->u.ext.type2_cost);
399 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
400 stream_putl (s, or->u.ext.type2_cost);
401 else
402 stream_putl (s, or->cost);
403 }
paul718e3742002-12-13 20:15:29 +0000404
405 stream_putw_at (s, 0, stream_get_endp (s));
406
407 writen (zclient->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000408 }
409}
410
411void
412ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
413{
414 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000415 struct ospf_path *path;
416 struct in_addr *nexthop;
hasso52dc7ee2004-09-23 19:18:23 +0000417 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000418
419 if (zclient->redist[ZEBRA_ROUTE_OSPF])
420 {
421 api.type = ZEBRA_ROUTE_OSPF;
422 api.flags = 0;
423 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000424 api.ifindex_num = 0;
425 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000426
paul96735ee2003-08-10 02:51:22 +0000427 for (node = listhead (or->paths); node; nextnode (node))
paulcf795c52003-06-19 02:13:25 +0000428 {
429 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000430
paulcf795c52003-06-19 02:13:25 +0000431 if (path->nexthop.s_addr != INADDR_ANY)
432 {
pauld8e1d6b2003-08-10 04:04:41 +0000433 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000434 api.nexthop_num = 1;
435 nexthop = &path->nexthop;
436 api.nexthop = &nexthop;
437 }
hasso2db3d052004-02-11 21:52:13 +0000438 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000439 {
440 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
441 api.ifindex_num = 1;
442 api.ifindex = &path->oi->ifp->ifindex;
443 }
444 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
445 {
ajs9b0e25c2004-12-08 19:06:51 +0000446 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000447 inet_ntoa(p->prefix),
448 p->prefixlen);
449 }
paul72357f22003-06-19 02:11:23 +0000450
paul0a589352004-05-08 11:48:26 +0000451 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000452
453 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000454 {
ajs9b0e25c2004-12-08 19:06:51 +0000455 zlog_debug ("Zebra: Route delete %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000456 inet_ntoa (p->prefix),
457 p->prefixlen, inet_ntoa (**api.nexthop));
458 }
paulbb8ff1e2003-08-12 06:00:30 +0000459 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
460 {
ajs9b0e25c2004-12-08 19:06:51 +0000461 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000462 inet_ntoa (p->prefix),
463 p->prefixlen, *api.ifindex);
464 }
paulcf795c52003-06-19 02:13:25 +0000465 }
paul718e3742002-12-13 20:15:29 +0000466 }
467}
468
469void
470ospf_zebra_add_discard (struct prefix_ipv4 *p)
471{
472 struct zapi_ipv4 api;
473
474 if (zclient->redist[ZEBRA_ROUTE_OSPF])
475 {
476 api.type = ZEBRA_ROUTE_OSPF;
477 api.flags = ZEBRA_FLAG_BLACKHOLE;
478 api.message = 0;
479 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
480 api.nexthop_num = 0;
481 api.ifindex_num = 0;
482
paul0a589352004-05-08 11:48:26 +0000483 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000484 }
485}
486
487void
488ospf_zebra_delete_discard (struct prefix_ipv4 *p)
489{
490 struct zapi_ipv4 api;
491
492 if (zclient->redist[ZEBRA_ROUTE_OSPF])
493 {
494 api.type = ZEBRA_ROUTE_OSPF;
495 api.flags = ZEBRA_FLAG_BLACKHOLE;
496 api.message = 0;
497 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
498 api.nexthop_num = 0;
499 api.ifindex_num = 0;
500
paul0a589352004-05-08 11:48:26 +0000501 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000502
503 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000504 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000505 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000506
paul718e3742002-12-13 20:15:29 +0000507 }
508}
509
510int
511ospf_is_type_redistributed (int type)
512{
513 return (DEFAULT_ROUTE_TYPE (type)) ?
514 zclient->default_information : zclient->redist[type];
515}
516
517int
paul020709f2003-04-04 02:44:16 +0000518ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000519{
520 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000521
paul718e3742002-12-13 20:15:29 +0000522 if (ospf_is_type_redistributed (type))
523 {
paul68980082003-03-25 05:07:42 +0000524 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000525 {
526 ospf->dmetric[type].type = mtype;
527 force = LSA_REFRESH_FORCE;
528 }
paul68980082003-03-25 05:07:42 +0000529 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000530 {
531 ospf->dmetric[type].value = mvalue;
532 force = LSA_REFRESH_FORCE;
533 }
534
paul68980082003-03-25 05:07:42 +0000535 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000536
paul718e3742002-12-13 20:15:29 +0000537 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000538 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000539 LOOKUP (ospf_redistributed_proto, type),
540 metric_type (ospf, type), metric_value (ospf, type));
541
paul718e3742002-12-13 20:15:29 +0000542 return CMD_SUCCESS;
543 }
544
paul68980082003-03-25 05:07:42 +0000545 ospf->dmetric[type].type = mtype;
546 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000547
paul0a589352004-05-08 11:48:26 +0000548 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000549
550 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000551 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000552 LOOKUP (ospf_redistributed_proto, type),
553 metric_type (ospf, type), metric_value (ospf, type));
554
paul68980082003-03-25 05:07:42 +0000555 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000556
557 return CMD_SUCCESS;
558}
559
560int
paul020709f2003-04-04 02:44:16 +0000561ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000562{
563 if (type == zclient->redist_default)
564 return CMD_SUCCESS;
565
paulcf795c52003-06-19 02:13:25 +0000566 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000567 return CMD_SUCCESS;
568
paul0a589352004-05-08 11:48:26 +0000569 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000570
paul718e3742002-12-13 20:15:29 +0000571 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000572 zlog_debug ("Redistribute[%s]: Stop",
paulcf795c52003-06-19 02:13:25 +0000573 LOOKUP (ospf_redistributed_proto, type));
paul718e3742002-12-13 20:15:29 +0000574
paul68980082003-03-25 05:07:42 +0000575 ospf->dmetric[type].type = -1;
576 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000577
578 /* Remove the routes from OSPF table. */
579 ospf_redistribute_withdraw (type);
580
paul68980082003-03-25 05:07:42 +0000581 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000582
583 return CMD_SUCCESS;
584}
585
586int
paul020709f2003-04-04 02:44:16 +0000587ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000588 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000589{
590 int force = 0;
paul020709f2003-04-04 02:44:16 +0000591
paul718e3742002-12-13 20:15:29 +0000592 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
593 {
paul68980082003-03-25 05:07:42 +0000594 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000595 {
596 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
597 force = 1;
598 }
paul68980082003-03-25 05:07:42 +0000599 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000600 {
601 force = 1;
602 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
603 }
604
paul68980082003-03-25 05:07:42 +0000605 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000606
paul718e3742002-12-13 20:15:29 +0000607 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000608 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000609 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
610 metric_type (ospf, DEFAULT_ROUTE),
611 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000612 return CMD_SUCCESS;
613 }
614
paul68980082003-03-25 05:07:42 +0000615 ospf->default_originate = originate;
616 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
617 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000618
paul0a589352004-05-08 11:48:26 +0000619 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000620
paul718e3742002-12-13 20:15:29 +0000621 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000622 zlog_debug ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000623 metric_type (ospf, DEFAULT_ROUTE),
624 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000625
paul68980082003-03-25 05:07:42 +0000626 if (ospf->router_id.s_addr == 0)
627 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000628 else
629 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000630 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000631
paul68980082003-03-25 05:07:42 +0000632 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000633
634 return CMD_SUCCESS;
635}
636
637int
paul020709f2003-04-04 02:44:16 +0000638ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000639{
640 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
641 return CMD_SUCCESS;
642
paul68980082003-03-25 05:07:42 +0000643 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
644 ospf->dmetric[DEFAULT_ROUTE].type = -1;
645 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000646
paul0a589352004-05-08 11:48:26 +0000647 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000648
649 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000650 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000651
paul68980082003-03-25 05:07:42 +0000652 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000653
654 return CMD_SUCCESS;
655}
656
657int
paul020709f2003-04-04 02:44:16 +0000658ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000659 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000660{
661 /* If prefix is multicast, then do not originate LSA. */
662 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
663 {
664 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000665 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000666 return 0;
667 }
668
669 /* Take care of default-originate. */
670 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000671 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000672 {
paulcf795c52003-06-19 02:13:25 +0000673 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
674 "for default");
675 return 0;
paul718e3742002-12-13 20:15:29 +0000676 }
677
678 return 1;
679}
680
681/* If connected prefix is OSPF enable interface, then do not announce. */
682int
paulcf795c52003-06-19 02:13:25 +0000683ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000684{
685 struct route_node *rn;
686
paul68980082003-03-25 05:07:42 +0000687 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000688 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000689 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
690 {
691 route_unlock_node (rn);
692 return 0;
693 }
paul718e3742002-12-13 20:15:29 +0000694
695 return 1;
696}
697
698/* return 1 if external LSA must be originated, 0 otherwise */
699int
paul68980082003-03-25 05:07:42 +0000700ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000701 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000702{
703 struct route_map_set_values save_values;
704 struct prefix_ipv4 *p = &ei->p;
705 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000706
paul718e3742002-12-13 20:15:29 +0000707 if (changed)
708 *changed = 0;
709
paul020709f2003-04-04 02:44:16 +0000710 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000711 return 0;
712
713 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000714 if (type == ZEBRA_ROUTE_CONNECT &&
715 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000716 return 0;
717
paul020709f2003-04-04 02:44:16 +0000718 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000719 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000720 if (DISTRIBUTE_LIST (ospf, type))
721 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000722 {
723 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000724 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
paulcf795c52003-06-19 02:13:25 +0000725 LOOKUP (ospf_redistributed_proto, type),
726 inet_ntoa (p->prefix), p->prefixlen);
727 return 0;
728 }
paul718e3742002-12-13 20:15:29 +0000729
730 save_values = ei->route_map_set;
731 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000732
paul718e3742002-12-13 20:15:29 +0000733 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000734 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000735 {
736 int ret;
737
paulcf795c52003-06-19 02:13:25 +0000738 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
739 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000740
741 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000742 {
743 ei->route_map_set = save_values;
744 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000745 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
paulcf795c52003-06-19 02:13:25 +0000746 LOOKUP (ospf_redistributed_proto, type),
747 inet_ntoa (p->prefix), p->prefixlen);
748 return 0;
749 }
750
paul718e3742002-12-13 20:15:29 +0000751 /* check if 'route-map set' changed something */
752 if (changed)
paulcf795c52003-06-19 02:13:25 +0000753 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
754 &save_values);
paul718e3742002-12-13 20:15:29 +0000755 }
756
757 return 1;
758}
759
760/* OSPF route-map set for redistribution */
761void
paul6c835672004-10-11 11:00:30 +0000762ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000763{
paul020709f2003-04-04 02:44:16 +0000764 if (ROUTEMAP_NAME (ospf, type))
765 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000766
paul020709f2003-04-04 02:44:16 +0000767 ROUTEMAP_NAME (ospf, type) = strdup (name);
768 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000769}
770
771void
paul020709f2003-04-04 02:44:16 +0000772ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000773{
paul020709f2003-04-04 02:44:16 +0000774 if (ROUTEMAP_NAME (ospf, type))
775 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000776
paul020709f2003-04-04 02:44:16 +0000777 ROUTEMAP_NAME (ospf, type) = NULL;
778 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000779}
780
781/* Zebra route add and delete treatment. */
782int
783ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000784 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000785{
786 struct stream *s;
787 struct zapi_ipv4 api;
788 unsigned long ifindex;
789 struct in_addr nexthop;
790 struct prefix_ipv4 p;
791 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000792 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000793
794 s = zclient->ibuf;
795 ifindex = 0;
796 nexthop.s_addr = 0;
797
798 /* Type, flags, message. */
799 api.type = stream_getc (s);
800 api.flags = stream_getc (s);
801 api.message = stream_getc (s);
802
803 /* IPv4 prefix. */
804 memset (&p, 0, sizeof (struct prefix_ipv4));
805 p.family = AF_INET;
806 p.prefixlen = stream_getc (s);
807 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
808
hasso8585d4e2004-04-20 17:25:12 +0000809 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
810 return 0;
811
paul718e3742002-12-13 20:15:29 +0000812 /* Nexthop, ifindex, distance, metric. */
813 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
814 {
815 api.nexthop_num = stream_getc (s);
816 nexthop.s_addr = stream_get_ipv4 (s);
817 }
818 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
819 {
820 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000821 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000822 ifindex = stream_getl (s);
823 }
824 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
825 api.distance = stream_getc (s);
826 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
827 api.metric = stream_getl (s);
828
paul020709f2003-04-04 02:44:16 +0000829 ospf = ospf_lookup ();
830 if (ospf == NULL)
831 return 0;
832
paul718e3742002-12-13 20:15:29 +0000833 if (command == ZEBRA_IPV4_ROUTE_ADD)
834 {
paul7021c422003-07-15 12:52:22 +0000835 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000836 * Maybe we should ignore reject/blackhole routes? Testing shows that
837 * there is no problems though and this is only way to "summarize"
838 * routes in ASBR at the moment. Maybe we need just a better generalised
839 * solution for these types?
840 *
841 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
842 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
843 * return 0;
paul7021c422003-07-15 12:52:22 +0000844 */
paul7021c422003-07-15 12:52:22 +0000845
paul718e3742002-12-13 20:15:29 +0000846 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
847
paul68980082003-03-25 05:07:42 +0000848 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000849 /* Set flags to generate AS-external-LSA originate event
850 for each redistributed protocols later. */
851 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000852 else
paulcf795c52003-06-19 02:13:25 +0000853 {
854 if (ei)
855 {
856 if (is_prefix_default (&p))
857 ospf_external_lsa_refresh_default (ospf);
858 else
859 {
860 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000861
paulcf795c52003-06-19 02:13:25 +0000862 current = ospf_external_info_find_lsa (ospf, &ei->p);
863 if (!current)
864 ospf_external_lsa_originate (ospf, ei);
865 else if (IS_LSA_MAXAGE (current))
866 ospf_external_lsa_refresh (ospf, current,
867 ei, LSA_REFRESH_FORCE);
868 else
869 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
870 inet_ntoa (p.prefix));
871 }
872 }
873 }
paul718e3742002-12-13 20:15:29 +0000874 }
paulcf795c52003-06-19 02:13:25 +0000875 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000876 {
877 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000878 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000879 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000880 else
881 ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop);
paul718e3742002-12-13 20:15:29 +0000882 }
883
884 return 0;
885}
paul718e3742002-12-13 20:15:29 +0000886
paulcf795c52003-06-19 02:13:25 +0000887
paul718e3742002-12-13 20:15:29 +0000888int
paul6c835672004-10-11 11:00:30 +0000889ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000890{
891 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000892 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000893
894 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000895 if (DISTRIBUTE_NAME (ospf, type))
896 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000897
898 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000899 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000900
901 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000902 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000903 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000904
905 return CMD_SUCCESS;
906}
907
908int
paul6c835672004-10-11 11:00:30 +0000909ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000910{
911 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000912 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000913 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000914
915 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000916 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000917
918 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000919 if (DISTRIBUTE_NAME (ospf, type))
920 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000921
paul020709f2003-04-04 02:44:16 +0000922 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000923
924 return CMD_SUCCESS;
925}
926
927/* distribute-list update timer. */
928int
929ospf_distribute_list_update_timer (struct thread *thread)
930{
931 struct route_node *rn;
932 struct external_info *ei;
933 struct route_table *rt;
934 struct ospf_lsa *lsa;
paul64511f32004-10-31 18:01:13 +0000935 int type;
paul020709f2003-04-04 02:44:16 +0000936 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000937
938 type = (int) THREAD_ARG (thread);
paul64511f32004-10-31 18:01:13 +0000939 assert (type < ZEBRA_ROUTE_MAX);
940
paul718e3742002-12-13 20:15:29 +0000941 rt = EXTERNAL_INFO (type);
942
paul020709f2003-04-04 02:44:16 +0000943 ospf = ospf_lookup ();
944 if (ospf == NULL)
945 return 0;
946
paul68980082003-03-25 05:07:42 +0000947 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000948
949 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
950
951 /* foreach all external info. */
952 if (rt)
953 for (rn = route_top (rt); rn; rn = route_next (rn))
954 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000955 {
956 if (is_prefix_default (&ei->p))
957 ospf_external_lsa_refresh_default (ospf);
958 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
959 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
960 else
961 ospf_external_lsa_originate (ospf, ei);
962 }
paul718e3742002-12-13 20:15:29 +0000963 return 0;
964}
965
966#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
967
968/* Update distribute-list and set timer to apply access-list. */
969void
paul68980082003-03-25 05:07:42 +0000970ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000971{
972 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000973
paul718e3742002-12-13 20:15:29 +0000974 /* External info does not exist. */
975 if (!(rt = EXTERNAL_INFO (type)))
976 return;
977
978 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000979 if (ospf->t_distribute_update)
980 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000981
982 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000983 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000984 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000985 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000986}
987
988/* If access-list is updated, apply some check. */
989void
990ospf_filter_update (struct access_list *access)
991{
paul020709f2003-04-04 02:44:16 +0000992 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000993 int type;
994 int abr_inv = 0;
995 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000996 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000997
998 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000999 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001000 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001001 return;
1002
paul718e3742002-12-13 20:15:29 +00001003 /* Update distribute-list, and apply filter. */
1004 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1005 {
paul020709f2003-04-04 02:44:16 +00001006 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001007 {
1008 /* if route-map is not NULL it may be using this access list */
1009 ospf_distribute_list_update (ospf, type);
1010 continue;
1011 }
1012
paul718e3742002-12-13 20:15:29 +00001013
paul020709f2003-04-04 02:44:16 +00001014 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001015 {
1016 /* Keep old access-list for distribute-list. */
1017 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1018
1019 /* Update access-list for distribute-list. */
1020 DISTRIBUTE_LIST (ospf, type) =
1021 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1022
1023 /* No update for this distribute type. */
1024 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1025 continue;
1026
1027 /* Schedule distribute-list update timer. */
1028 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1029 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1030 ospf_distribute_list_update (ospf, type);
1031 }
paul718e3742002-12-13 20:15:29 +00001032 }
1033
1034 /* Update Area access-list. */
paul68980082003-03-25 05:07:42 +00001035 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001036 if ((area = getdata (node)) != NULL)
1037 {
paulcf795c52003-06-19 02:13:25 +00001038 if (EXPORT_NAME (area))
1039 {
1040 EXPORT_LIST (area) = NULL;
1041 abr_inv++;
1042 }
paul718e3742002-12-13 20:15:29 +00001043
paulcf795c52003-06-19 02:13:25 +00001044 if (IMPORT_NAME (area))
1045 {
1046 IMPORT_LIST (area) = NULL;
1047 abr_inv++;
1048 }
paul718e3742002-12-13 20:15:29 +00001049 }
1050
1051 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001052 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001053 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001054}
hassodd669bb2004-05-10 07:43:59 +00001055
1056/* If prefix-list is updated, do some updates. */
1057void
1058ospf_prefix_list_update (struct prefix_list *plist)
1059{
1060 struct ospf *ospf;
1061 int type;
1062 int abr_inv = 0;
1063 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001064 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001065
1066 /* If OSPF instatnce does not exist, return right now. */
1067 ospf = ospf_lookup ();
1068 if (ospf == NULL)
1069 return;
1070
1071 /* Update all route-maps which are used as redistribution filters.
1072 * They might use prefix-list.
1073 */
1074 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1075 {
1076 if (ROUTEMAP (ospf, type) != NULL)
1077 {
1078 /* If route-map is not NULL it may be using this prefix list */
1079 ospf_distribute_list_update (ospf, type);
1080 continue;
1081 }
1082 }
1083
1084 /* Update area filter-lists. */
1085 for (node = listhead (ospf->areas); node; nextnode (node))
1086 if ((area = getdata (node)) != NULL)
1087 {
1088 /* Update filter-list in. */
1089 if (PREFIX_NAME_IN (area))
1090 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1091 {
1092 PREFIX_LIST_IN (area) =
1093 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1094 abr_inv++;
1095 }
1096
1097 /* Update filter-list out. */
1098 if (PREFIX_NAME_OUT (area))
1099 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1100 {
1101 PREFIX_LIST_IN (area) =
1102 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1103 abr_inv++;
1104 }
1105 }
1106
1107 /* Schedule ABR task. */
1108 if (IS_OSPF_ABR (ospf) && abr_inv)
1109 ospf_schedule_abr_task (ospf);
1110}
paulcf795c52003-06-19 02:13:25 +00001111
paul718e3742002-12-13 20:15:29 +00001112struct ospf_distance *
1113ospf_distance_new ()
1114{
1115 struct ospf_distance *new;
1116 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1117 memset (new, 0, sizeof (struct ospf_distance));
1118 return new;
1119}
1120
1121void
1122ospf_distance_free (struct ospf_distance *odistance)
1123{
1124 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1125}
1126
1127int
paul6c835672004-10-11 11:00:30 +00001128ospf_distance_set (struct vty *vty, struct ospf *ospf,
1129 const char *distance_str,
1130 const char *ip_str,
1131 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001132{
1133 int ret;
1134 struct prefix_ipv4 p;
1135 u_char distance;
1136 struct route_node *rn;
1137 struct ospf_distance *odistance;
1138
1139 ret = str2prefix_ipv4 (ip_str, &p);
1140 if (ret == 0)
1141 {
1142 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1143 return CMD_WARNING;
1144 }
1145
1146 distance = atoi (distance_str);
1147
1148 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001149 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001150 if (rn->info)
1151 {
1152 odistance = rn->info;
1153 route_unlock_node (rn);
1154 }
1155 else
1156 {
1157 odistance = ospf_distance_new ();
1158 rn->info = odistance;
1159 }
1160
1161 /* Set distance value. */
1162 odistance->distance = distance;
1163
1164 /* Reset access-list configuration. */
1165 if (odistance->access_list)
1166 {
1167 free (odistance->access_list);
1168 odistance->access_list = NULL;
1169 }
1170 if (access_list_str)
1171 odistance->access_list = strdup (access_list_str);
1172
1173 return CMD_SUCCESS;
1174}
1175
1176int
paul6c835672004-10-11 11:00:30 +00001177ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1178 const char *distance_str,
1179 const char *ip_str, char
1180 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001181{
1182 int ret;
1183 struct prefix_ipv4 p;
1184 u_char distance;
1185 struct route_node *rn;
1186 struct ospf_distance *odistance;
1187
1188 ret = str2prefix_ipv4 (ip_str, &p);
1189 if (ret == 0)
1190 {
1191 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1192 return CMD_WARNING;
1193 }
1194
1195 distance = atoi (distance_str);
1196
paulcf795c52003-06-19 02:13:25 +00001197 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1198 if (!rn)
paul718e3742002-12-13 20:15:29 +00001199 {
1200 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1201 return CMD_WARNING;
1202 }
1203
1204 odistance = rn->info;
1205
1206 if (odistance->access_list)
1207 free (odistance->access_list);
1208 ospf_distance_free (odistance);
1209
1210 rn->info = NULL;
1211 route_unlock_node (rn);
1212 route_unlock_node (rn);
1213
1214 return CMD_SUCCESS;
1215}
1216
1217void
paul68980082003-03-25 05:07:42 +00001218ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001219{
1220 struct route_node *rn;
1221 struct ospf_distance *odistance;
1222
paul68980082003-03-25 05:07:42 +00001223 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001224 if ((odistance = rn->info) != NULL)
1225 {
paulcf795c52003-06-19 02:13:25 +00001226 if (odistance->access_list)
1227 free (odistance->access_list);
1228 ospf_distance_free (odistance);
1229 rn->info = NULL;
1230 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001231 }
1232}
1233
1234u_char
1235ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1236{
paul020709f2003-04-04 02:44:16 +00001237 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001238
paul020709f2003-04-04 02:44:16 +00001239 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001240 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001241 return 0;
1242
paul68980082003-03-25 05:07:42 +00001243 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001244 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001245 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001246
paul68980082003-03-25 05:07:42 +00001247 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001248 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001249 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001250
paul68980082003-03-25 05:07:42 +00001251 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001252 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001253 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001254 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001255
paul68980082003-03-25 05:07:42 +00001256 if (ospf->distance_all)
1257 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001258
1259 return 0;
1260}
1261
1262void
1263ospf_zebra_init ()
1264{
1265 /* Allocate zebra structure. */
1266 zclient = zclient_new ();
1267 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001268 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001269 zclient->interface_add = ospf_interface_add;
1270 zclient->interface_delete = ospf_interface_delete;
1271 zclient->interface_up = ospf_interface_state_up;
1272 zclient->interface_down = ospf_interface_state_down;
1273 zclient->interface_address_add = ospf_interface_address_add;
1274 zclient->interface_address_delete = ospf_interface_address_delete;
1275 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1276 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1277
1278 access_list_add_hook (ospf_filter_update);
1279 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001280 prefix_list_add_hook (ospf_prefix_list_update);
1281 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001282}