blob: 150ffac3acea7c8f245fbd1405a3e165776f3617 [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
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +000068 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
69 {
70 char buf[128];
71 prefix2str(&router_id, buf, sizeof(buf));
72 zlog_debug("Zebra rcvd: router id update %s", buf);
73 }
74
hasso18a6dce2004-10-03 18:18:34 +000075 router_id_zebra = router_id.u.prefix4;
76
77 ospf = ospf_lookup ();
paulb29800a2005-11-20 14:50:45 +000078
hasso18a6dce2004-10-03 18:18:34 +000079 if (ospf != NULL)
paulb29800a2005-11-20 14:50:45 +000080 ospf_router_id_update (ospf);
81
hasso18a6dce2004-10-03 18:18:34 +000082 return 0;
83}
paul718e3742002-12-13 20:15:29 +000084
85/* Inteface addition message from zebra. */
paul4dadc292005-05-06 21:37:42 +000086static int
paul718e3742002-12-13 20:15:29 +000087ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
88{
89 struct interface *ifp;
90
91 ifp = zebra_interface_add_read (zclient->ibuf);
92
93 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +000094 zlog_debug ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +000095 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000096
paulcf795c52003-06-19 02:13:25 +000097 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000098
paul718e3742002-12-13 20:15:29 +000099 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
100 {
101 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
ajsbc18d612004-12-15 15:07:19 +0000102 IF_DEF_PARAMS (ifp)->type = ospf_default_iftype(ifp);
paul718e3742002-12-13 20:15:29 +0000103 }
104
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100105 ospf_if_update (NULL, ifp);
paul718e3742002-12-13 20:15:29 +0000106
107#ifdef HAVE_SNMP
108 ospf_snmp_if_update (ifp);
109#endif /* HAVE_SNMP */
110
111 return 0;
112}
113
paul4dadc292005-05-06 21:37:42 +0000114static int
paul718e3742002-12-13 20:15:29 +0000115ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000116 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000117{
118 struct interface *ifp;
119 struct stream *s;
120 struct route_node *rn;
121
paulcf795c52003-06-19 02:13:25 +0000122 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000123 /* zebra_interface_state_read() updates interface structure in iflist */
124 ifp = zebra_interface_state_read (s);
125
126 if (ifp == NULL)
127 return 0;
128
129 if (if_is_up (ifp))
130 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000131 ifp->name);
132
paul718e3742002-12-13 20:15:29 +0000133 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000134 zlog_debug
paulcf795c52003-06-19 02:13:25 +0000135 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
136 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000137
138#ifdef HAVE_SNMP
139 ospf_snmp_if_delete (ifp);
140#endif /* HAVE_SNMP */
141
142 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
143 if (rn->info)
144 ospf_if_free ((struct ospf_interface *) rn->info);
145
ajsd2fc8892005-04-02 18:38:43 +0000146 ifp->ifindex = IFINDEX_INTERNAL;
paul718e3742002-12-13 20:15:29 +0000147 return 0;
148}
149
ajsd2fc8892005-04-02 18:38:43 +0000150static struct interface *
paul718e3742002-12-13 20:15:29 +0000151zebra_interface_if_lookup (struct stream *s)
152{
ajs21fefa92005-04-02 23:16:41 +0000153 char ifname_tmp[INTERFACE_NAMSIZ];
paul718e3742002-12-13 20:15:29 +0000154
155 /* Read interface name. */
156 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
157
ajsd2fc8892005-04-02 18:38:43 +0000158 /* And look it up. */
ajs21fefa92005-04-02 23:16:41 +0000159 return if_lookup_by_name_len(ifname_tmp,
160 strnlen(ifname_tmp, INTERFACE_NAMSIZ));
paul718e3742002-12-13 20:15:29 +0000161}
162
paul4dadc292005-05-06 21:37:42 +0000163static int
paul718e3742002-12-13 20:15:29 +0000164ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000165 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000166{
167 struct interface *ifp;
paul718e3742002-12-13 20:15:29 +0000168 struct ospf_interface *oi;
169 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000170
paul718e3742002-12-13 20:15:29 +0000171 ifp = zebra_interface_if_lookup (zclient->ibuf);
172
173 if (ifp == NULL)
174 return 0;
175
176 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000177 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000178 {
179 /* Temporarily keep ifp values. */
ajsa608bbf2005-03-29 17:03:49 +0000180 struct interface if_tmp;
paul718e3742002-12-13 20:15:29 +0000181 memcpy (&if_tmp, ifp, sizeof (struct interface));
182
183 zebra_interface_if_set_value (zclient->ibuf, ifp);
184
185 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000186 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000187
188 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000189 {
190 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000191 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
paulcf795c52003-06-19 02:13:25 +0000192 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000193
paulcf795c52003-06-19 02:13:25 +0000194 ospf_if_recalculate_output_cost (ifp);
195 }
ajsa608bbf2005-03-29 17:03:49 +0000196
197 if (if_tmp.mtu != ifp->mtu)
198 {
199 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
200 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
201 ifp->name, if_tmp.mtu, ifp->mtu);
202
203 /* Must reset the interface (simulate down/up) when MTU changes. */
204 ospf_if_reset(ifp);
205 }
paul718e3742002-12-13 20:15:29 +0000206 return 0;
207 }
paulcf795c52003-06-19 02:13:25 +0000208
paul718e3742002-12-13 20:15:29 +0000209 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000210
paul718e3742002-12-13 20:15:29 +0000211 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000212 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000213
214 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000215 {
paulcf795c52003-06-19 02:13:25 +0000216 if ((oi = rn->info) == NULL)
217 continue;
218
paul718e3742002-12-13 20:15:29 +0000219 ospf_if_up (oi);
220 }
paulcf795c52003-06-19 02:13:25 +0000221
paul718e3742002-12-13 20:15:29 +0000222 return 0;
223}
224
paul4dadc292005-05-06 21:37:42 +0000225static int
paul718e3742002-12-13 20:15:29 +0000226ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000227 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000228{
229 struct interface *ifp;
230 struct ospf_interface *oi;
231 struct route_node *node;
232
233 ifp = zebra_interface_state_read (zclient->ibuf);
234
235 if (ifp == NULL)
236 return 0;
237
238 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
ajs9b0e25c2004-12-08 19:06:51 +0000239 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000240
paulcf795c52003-06-19 02:13:25 +0000241 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000242 {
paulcf795c52003-06-19 02:13:25 +0000243 if ((oi = node->info) == NULL)
244 continue;
paul718e3742002-12-13 20:15:29 +0000245 ospf_if_down (oi);
246 }
247
248 return 0;
249}
250
paul4dadc292005-05-06 21:37:42 +0000251static int
paul718e3742002-12-13 20:15:29 +0000252ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000253 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000254{
255 struct connected *c;
256
paul0a589352004-05-08 11:48:26 +0000257 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000258
259 if (c == NULL)
260 return 0;
261
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000262 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
263 {
264 char buf[128];
265 prefix2str(c->address, buf, sizeof(buf));
266 zlog_debug("Zebra: interface %s address add %s", c->ifp->name, buf);
267 }
268
Joakim Tjernlunda49eb302008-09-02 19:06:31 +0100269 ospf_if_update (NULL, c->ifp);
paul718e3742002-12-13 20:15:29 +0000270
271#ifdef HAVE_SNMP
272 ospf_snmp_if_update (c->ifp);
273#endif /* HAVE_SNMP */
274
275 return 0;
276}
277
paul4dadc292005-05-06 21:37:42 +0000278static int
paul718e3742002-12-13 20:15:29 +0000279ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000280 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000281{
282 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
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000293 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
294 {
295 char buf[128];
296 prefix2str(c->address, buf, sizeof(buf));
297 zlog_debug("Zebra: interface %s address delete %s", c->ifp->name, buf);
298 }
299
paul718e3742002-12-13 20:15:29 +0000300 ifp = c->ifp;
301 p = *c->address;
302 p.prefixlen = IPV4_MAX_PREFIXLEN;
303
304 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000305 if (!rn)
paul98429f62006-01-10 22:11:54 +0000306 {
307 connected_free (c);
308 return 0;
309 }
paul718e3742002-12-13 20:15:29 +0000310
311 assert (rn->info);
312 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000313
paul718e3742002-12-13 20:15:29 +0000314 /* Call interface hook functions to clean up */
315 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000316
paul718e3742002-12-13 20:15:29 +0000317#ifdef HAVE_SNMP
318 ospf_snmp_if_update (c->ifp);
319#endif /* HAVE_SNMP */
320
321 connected_free (c);
322
paul718e3742002-12-13 20:15:29 +0000323 return 0;
324}
paul72357f22003-06-19 02:11:23 +0000325
paul718e3742002-12-13 20:15:29 +0000326void
327ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
328{
329 u_char message;
330 u_char distance;
331 u_char flags;
332 int psize;
333 struct stream *s;
334 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000335 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000336
337 if (zclient->redist[ZEBRA_ROUTE_OSPF])
338 {
339 message = 0;
340 flags = 0;
341
342 /* OSPF pass nexthop and metric */
343 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
344 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
345
346 /* Distance value. */
347 distance = ospf_distance_apply (p, or);
348 if (distance)
paul72357f22003-06-19 02:11:23 +0000349 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000350
351 /* Make packet. */
352 s = zclient->obuf;
353 stream_reset (s);
354
paul718e3742002-12-13 20:15:29 +0000355 /* Put command, type, flags, message. */
paulc6371712006-01-17 17:49:53 +0000356 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD);
paul718e3742002-12-13 20:15:29 +0000357 stream_putc (s, ZEBRA_ROUTE_OSPF);
358 stream_putc (s, flags);
359 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000360
paul718e3742002-12-13 20:15:29 +0000361 /* Put prefix information. */
362 psize = PSIZE (p->prefixlen);
363 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000364 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000365
366 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000367 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000368
369 /* Nexthop, ifindex, distance and metric information. */
paul1eb8ef22005-04-07 07:30:20 +0000370 for (ALL_LIST_ELEMENTS_RO (or->paths, node, path))
paul72357f22003-06-19 02:11:23 +0000371 {
paulcf795c52003-06-19 02:13:25 +0000372 if (path->nexthop.s_addr != INADDR_ANY)
373 {
374 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
375 stream_put_in_addr (s, &path->nexthop);
376 }
377 else
378 {
379 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
380 if (path->oi)
381 stream_putl (s, path->oi->ifp->ifindex);
382 else
383 stream_putl (s, 0);
384 }
paul72357f22003-06-19 02:11:23 +0000385
386 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
387 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000388 char buf[2][INET_ADDRSTRLEN];
389 zlog_debug("Zebra: Route add %s/%d nexthop %s",
390 inet_ntop(AF_INET, &p->prefix,
391 buf[0], sizeof(buf[0])),
392 p->prefixlen,
393 inet_ntop(AF_INET, &path->nexthop,
394 buf[1], sizeof(buf[1])));
paulcf795c52003-06-19 02:13:25 +0000395 }
396 }
paul718e3742002-12-13 20:15:29 +0000397
398 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000399 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000400 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000401 {
402 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
403 stream_putl (s, or->cost + or->u.ext.type2_cost);
404 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
405 stream_putl (s, or->u.ext.type2_cost);
406 else
407 stream_putl (s, or->cost);
408 }
paul718e3742002-12-13 20:15:29 +0000409
410 stream_putw_at (s, 0, stream_get_endp (s));
411
ajs634f9ea2005-04-11 15:51:40 +0000412 zclient_send_message(zclient);
paul718e3742002-12-13 20:15:29 +0000413 }
414}
415
416void
417ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
418{
419 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000420 struct ospf_path *path;
421 struct in_addr *nexthop;
paul1eb8ef22005-04-07 07:30:20 +0000422 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000423
424 if (zclient->redist[ZEBRA_ROUTE_OSPF])
425 {
426 api.type = ZEBRA_ROUTE_OSPF;
427 api.flags = 0;
428 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000429 api.ifindex_num = 0;
430 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000431
paul1eb8ef22005-04-07 07:30:20 +0000432 for (ALL_LIST_ELEMENTS (or->paths, node, nnode, path))
paulcf795c52003-06-19 02:13:25 +0000433 {
paulcf795c52003-06-19 02:13:25 +0000434 if (path->nexthop.s_addr != INADDR_ANY)
435 {
pauld8e1d6b2003-08-10 04:04:41 +0000436 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000437 api.nexthop_num = 1;
438 nexthop = &path->nexthop;
439 api.nexthop = &nexthop;
440 }
hasso2db3d052004-02-11 21:52:13 +0000441 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000442 {
443 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
444 api.ifindex_num = 1;
445 api.ifindex = &path->oi->ifp->ifindex;
446 }
447 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
448 {
ajs9b0e25c2004-12-08 19:06:51 +0000449 zlog_debug("Zebra: no ifp %s %d",
paulbb8ff1e2003-08-12 06:00:30 +0000450 inet_ntoa(p->prefix),
451 p->prefixlen);
452 }
paul72357f22003-06-19 02:11:23 +0000453
paul0a589352004-05-08 11:48:26 +0000454 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000455
456 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000457 {
Andrew J. Schorr56b3ea02007-03-14 20:21:43 +0000458 char buf[2][INET_ADDRSTRLEN];
459 zlog_debug("Zebra: Route delete %s/%d nexthop %s",
460 inet_ntop(AF_INET, &p->prefix, buf[0], sizeof(buf[0])),
461 p->prefixlen,
462 inet_ntop(AF_INET, *api.nexthop,
463 buf[1], sizeof(buf[1])));
paulcf795c52003-06-19 02:13:25 +0000464 }
paulbb8ff1e2003-08-12 06:00:30 +0000465 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
466 {
ajs9b0e25c2004-12-08 19:06:51 +0000467 zlog_debug ("Zebra: Route delete %s/%d ifindex %d",
paulbb8ff1e2003-08-12 06:00:30 +0000468 inet_ntoa (p->prefix),
469 p->prefixlen, *api.ifindex);
470 }
paulcf795c52003-06-19 02:13:25 +0000471 }
paul718e3742002-12-13 20:15:29 +0000472 }
473}
474
475void
476ospf_zebra_add_discard (struct prefix_ipv4 *p)
477{
478 struct zapi_ipv4 api;
479
480 if (zclient->redist[ZEBRA_ROUTE_OSPF])
481 {
482 api.type = ZEBRA_ROUTE_OSPF;
483 api.flags = ZEBRA_FLAG_BLACKHOLE;
484 api.message = 0;
485 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
486 api.nexthop_num = 0;
487 api.ifindex_num = 0;
488
paul0a589352004-05-08 11:48:26 +0000489 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
Andrew J. Schorr7f643eb2006-11-30 16:17:02 +0000490
491 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
492 zlog_debug ("Zebra: Route add discard %s/%d",
493 inet_ntoa (p->prefix), p->prefixlen);
paul718e3742002-12-13 20:15:29 +0000494 }
495}
496
497void
498ospf_zebra_delete_discard (struct prefix_ipv4 *p)
499{
500 struct zapi_ipv4 api;
501
502 if (zclient->redist[ZEBRA_ROUTE_OSPF])
503 {
504 api.type = ZEBRA_ROUTE_OSPF;
505 api.flags = ZEBRA_FLAG_BLACKHOLE;
506 api.message = 0;
507 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
508 api.nexthop_num = 0;
509 api.ifindex_num = 0;
510
paul0a589352004-05-08 11:48:26 +0000511 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000512
513 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000514 zlog_debug ("Zebra: Route delete discard %s/%d",
paulcf795c52003-06-19 02:13:25 +0000515 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000516
paul718e3742002-12-13 20:15:29 +0000517 }
518}
519
520int
521ospf_is_type_redistributed (int type)
522{
523 return (DEFAULT_ROUTE_TYPE (type)) ?
524 zclient->default_information : zclient->redist[type];
525}
526
527int
paul020709f2003-04-04 02:44:16 +0000528ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000529{
530 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000531
paul718e3742002-12-13 20:15:29 +0000532 if (ospf_is_type_redistributed (type))
533 {
paul68980082003-03-25 05:07:42 +0000534 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000535 {
536 ospf->dmetric[type].type = mtype;
537 force = LSA_REFRESH_FORCE;
538 }
paul68980082003-03-25 05:07:42 +0000539 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000540 {
541 ospf->dmetric[type].value = mvalue;
542 force = LSA_REFRESH_FORCE;
543 }
544
paul68980082003-03-25 05:07:42 +0000545 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000546
paul718e3742002-12-13 20:15:29 +0000547 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000548 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000549 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000550 metric_type (ospf, type), metric_value (ospf, type));
551
paul718e3742002-12-13 20:15:29 +0000552 return CMD_SUCCESS;
553 }
554
paul68980082003-03-25 05:07:42 +0000555 ospf->dmetric[type].type = mtype;
556 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000557
paul0a589352004-05-08 11:48:26 +0000558 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000559
560 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000561 zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000562 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000563 metric_type (ospf, type), metric_value (ospf, type));
564
paul68980082003-03-25 05:07:42 +0000565 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000566
567 return CMD_SUCCESS;
568}
569
570int
paul020709f2003-04-04 02:44:16 +0000571ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000572{
573 if (type == zclient->redist_default)
574 return CMD_SUCCESS;
575
paulcf795c52003-06-19 02:13:25 +0000576 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000577 return CMD_SUCCESS;
578
paul0a589352004-05-08 11:48:26 +0000579 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000580
paul718e3742002-12-13 20:15:29 +0000581 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000582 zlog_debug ("Redistribute[%s]: Stop",
ajsf52d13c2005-10-01 17:38:06 +0000583 ospf_redist_string(type));
paul718e3742002-12-13 20:15:29 +0000584
paul68980082003-03-25 05:07:42 +0000585 ospf->dmetric[type].type = -1;
586 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000587
588 /* Remove the routes from OSPF table. */
Paul Jakma6db3a6f2006-05-12 23:02:46 +0000589 ospf_redistribute_withdraw (ospf, type);
paul718e3742002-12-13 20:15:29 +0000590
paul68980082003-03-25 05:07:42 +0000591 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000592
593 return CMD_SUCCESS;
594}
595
596int
paul020709f2003-04-04 02:44:16 +0000597ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000598 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000599{
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000600 ospf->default_originate = originate;
601 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
602 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul020709f2003-04-04 02:44:16 +0000603
paul718e3742002-12-13 20:15:29 +0000604 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
605 {
Andrew J. Schorr8fb8a502006-10-24 19:04:26 +0000606 /* if ospf->default_originate changes value, is calling
607 ospf_external_lsa_refresh_default sufficient to implement
608 the change? */
paul68980082003-03-25 05:07:42 +0000609 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000610
paul718e3742002-12-13 20:15:29 +0000611 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000612 zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
ajsf52d13c2005-10-01 17:38:06 +0000613 ospf_redist_string(DEFAULT_ROUTE),
paulcf795c52003-06-19 02:13:25 +0000614 metric_type (ospf, DEFAULT_ROUTE),
615 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000616 return CMD_SUCCESS;
617 }
618
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
Paul Jakma4021b602006-05-12 22:55:41 +0000629 thread_add_timer (master, ospf_default_originate_timer, ospf, 1);
paul718e3742002-12-13 20:15:29 +0000630
paul68980082003-03-25 05:07:42 +0000631 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000632
633 return CMD_SUCCESS;
634}
635
636int
paul020709f2003-04-04 02:44:16 +0000637ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000638{
639 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
640 return CMD_SUCCESS;
641
paul68980082003-03-25 05:07:42 +0000642 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
643 ospf->dmetric[DEFAULT_ROUTE].type = -1;
644 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000645
paul0a589352004-05-08 11:48:26 +0000646 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000647
648 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000649 zlog_debug ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000650
paul68980082003-03-25 05:07:42 +0000651 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000652
653 return CMD_SUCCESS;
654}
655
paul4dadc292005-05-06 21:37:42 +0000656static int
paul020709f2003-04-04 02:44:16 +0000657ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000658 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000659{
660 /* If prefix is multicast, then do not originate LSA. */
661 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
662 {
663 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000664 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000665 return 0;
666 }
667
668 /* Take care of default-originate. */
669 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000670 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000671 {
paulcf795c52003-06-19 02:13:25 +0000672 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
673 "for default");
674 return 0;
paul718e3742002-12-13 20:15:29 +0000675 }
676
677 return 1;
678}
679
680/* If connected prefix is OSPF enable interface, then do not announce. */
681int
paulcf795c52003-06-19 02:13:25 +0000682ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000683{
684 struct route_node *rn;
685
paul68980082003-03-25 05:07:42 +0000686 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000687 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000688 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
689 {
690 route_unlock_node (rn);
691 return 0;
692 }
paul718e3742002-12-13 20:15:29 +0000693
694 return 1;
695}
696
697/* return 1 if external LSA must be originated, 0 otherwise */
698int
paul68980082003-03-25 05:07:42 +0000699ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000700 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000701{
702 struct route_map_set_values save_values;
703 struct prefix_ipv4 *p = &ei->p;
704 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000705
paul718e3742002-12-13 20:15:29 +0000706 if (changed)
707 *changed = 0;
708
paul020709f2003-04-04 02:44:16 +0000709 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000710 return 0;
711
712 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000713 if (type == ZEBRA_ROUTE_CONNECT &&
714 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000715 return 0;
716
paul020709f2003-04-04 02:44:16 +0000717 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000718 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000719 if (DISTRIBUTE_LIST (ospf, type))
720 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000721 {
722 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000723 zlog_debug ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
ajsf52d13c2005-10-01 17:38:06 +0000724 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000725 inet_ntoa (p->prefix), p->prefixlen);
726 return 0;
727 }
paul718e3742002-12-13 20:15:29 +0000728
729 save_values = ei->route_map_set;
730 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000731
paul718e3742002-12-13 20:15:29 +0000732 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000733 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000734 {
735 int ret;
736
paulcf795c52003-06-19 02:13:25 +0000737 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
738 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000739
740 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000741 {
742 ei->route_map_set = save_values;
743 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
ajs9b0e25c2004-12-08 19:06:51 +0000744 zlog_debug ("Redistribute[%s]: %s/%d filtered by route-map.",
ajsf52d13c2005-10-01 17:38:06 +0000745 ospf_redist_string(type),
paulcf795c52003-06-19 02:13:25 +0000746 inet_ntoa (p->prefix), p->prefixlen);
747 return 0;
748 }
749
paul718e3742002-12-13 20:15:29 +0000750 /* check if 'route-map set' changed something */
751 if (changed)
paulcf795c52003-06-19 02:13:25 +0000752 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
753 &save_values);
paul718e3742002-12-13 20:15:29 +0000754 }
755
756 return 1;
757}
758
759/* OSPF route-map set for redistribution */
760void
paul6c835672004-10-11 11:00:30 +0000761ospf_routemap_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000762{
paul020709f2003-04-04 02:44:16 +0000763 if (ROUTEMAP_NAME (ospf, type))
764 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000765
paul020709f2003-04-04 02:44:16 +0000766 ROUTEMAP_NAME (ospf, type) = strdup (name);
767 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000768}
769
770void
paul020709f2003-04-04 02:44:16 +0000771ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000772{
paul020709f2003-04-04 02:44:16 +0000773 if (ROUTEMAP_NAME (ospf, type))
774 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000775
paul020709f2003-04-04 02:44:16 +0000776 ROUTEMAP_NAME (ospf, type) = NULL;
777 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000778}
779
780/* Zebra route add and delete treatment. */
paul4dadc292005-05-06 21:37:42 +0000781static int
paul718e3742002-12-13 20:15:29 +0000782ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000783 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000784{
785 struct stream *s;
786 struct zapi_ipv4 api;
787 unsigned long ifindex;
788 struct in_addr nexthop;
789 struct prefix_ipv4 p;
790 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000791 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000792
793 s = zclient->ibuf;
794 ifindex = 0;
795 nexthop.s_addr = 0;
796
797 /* Type, flags, message. */
798 api.type = stream_getc (s);
799 api.flags = stream_getc (s);
800 api.message = stream_getc (s);
801
802 /* IPv4 prefix. */
803 memset (&p, 0, sizeof (struct prefix_ipv4));
804 p.family = AF_INET;
805 p.prefixlen = stream_getc (s);
806 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
807
hasso8585d4e2004-04-20 17:25:12 +0000808 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
809 return 0;
810
paul718e3742002-12-13 20:15:29 +0000811 /* Nexthop, ifindex, distance, metric. */
812 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
813 {
814 api.nexthop_num = stream_getc (s);
815 nexthop.s_addr = stream_get_ipv4 (s);
816 }
817 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
818 {
819 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000820 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000821 ifindex = stream_getl (s);
822 }
823 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
824 api.distance = stream_getc (s);
825 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
826 api.metric = stream_getl (s);
827
paul020709f2003-04-04 02:44:16 +0000828 ospf = ospf_lookup ();
829 if (ospf == NULL)
830 return 0;
831
paul718e3742002-12-13 20:15:29 +0000832 if (command == ZEBRA_IPV4_ROUTE_ADD)
833 {
paul7021c422003-07-15 12:52:22 +0000834 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000835 * Maybe we should ignore reject/blackhole routes? Testing shows that
836 * there is no problems though and this is only way to "summarize"
837 * routes in ASBR at the moment. Maybe we need just a better generalised
838 * solution for these types?
839 *
840 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
841 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
842 * return 0;
paul7021c422003-07-15 12:52:22 +0000843 */
paul7021c422003-07-15 12:52:22 +0000844
paul718e3742002-12-13 20:15:29 +0000845 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
846
paul68980082003-03-25 05:07:42 +0000847 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000848 /* Set flags to generate AS-external-LSA originate event
849 for each redistributed protocols later. */
850 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000851 else
paulcf795c52003-06-19 02:13:25 +0000852 {
853 if (ei)
854 {
855 if (is_prefix_default (&p))
856 ospf_external_lsa_refresh_default (ospf);
857 else
858 {
859 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000860
paulcf795c52003-06-19 02:13:25 +0000861 current = ospf_external_info_find_lsa (ospf, &ei->p);
862 if (!current)
863 ospf_external_lsa_originate (ospf, ei);
864 else if (IS_LSA_MAXAGE (current))
865 ospf_external_lsa_refresh (ospf, current,
866 ei, LSA_REFRESH_FORCE);
867 else
868 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
869 inet_ntoa (p.prefix));
870 }
871 }
872 }
paul718e3742002-12-13 20:15:29 +0000873 }
paulcf795c52003-06-19 02:13:25 +0000874 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000875 {
876 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000877 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000878 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000879 else
ajs5339cfd2005-09-19 13:28:05 +0000880 ospf_external_lsa_flush (ospf, api.type, &p, ifindex /*, nexthop */);
paul718e3742002-12-13 20:15:29 +0000881 }
882
883 return 0;
884}
paul718e3742002-12-13 20:15:29 +0000885
paulcf795c52003-06-19 02:13:25 +0000886
paul718e3742002-12-13 20:15:29 +0000887int
paul6c835672004-10-11 11:00:30 +0000888ospf_distribute_list_out_set (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000889{
890 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000891 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000892
893 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000894 if (DISTRIBUTE_NAME (ospf, type))
895 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000896
897 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000898 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000899
900 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000901 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000902 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000903
904 return CMD_SUCCESS;
905}
906
907int
paul6c835672004-10-11 11:00:30 +0000908ospf_distribute_list_out_unset (struct ospf *ospf, int type, const char *name)
paul718e3742002-12-13 20:15:29 +0000909{
910 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000911 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000912 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000913
914 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000915 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000916
917 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000918 if (DISTRIBUTE_NAME (ospf, type))
919 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000920
paul020709f2003-04-04 02:44:16 +0000921 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000922
923 return CMD_SUCCESS;
924}
925
926/* distribute-list update timer. */
paul4dadc292005-05-06 21:37:42 +0000927static int
paul718e3742002-12-13 20:15:29 +0000928ospf_distribute_list_update_timer (struct thread *thread)
929{
930 struct route_node *rn;
931 struct external_info *ei;
932 struct route_table *rt;
933 struct ospf_lsa *lsa;
paul4dadc292005-05-06 21:37:42 +0000934 intptr_t type;
paul020709f2003-04-04 02:44:16 +0000935 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000936
paul4dadc292005-05-06 21:37:42 +0000937 type = (intptr_t)THREAD_ARG (thread);
hasso01018ce2005-08-05 07:40:15 +0000938 assert (type <= ZEBRA_ROUTE_MAX);
paul64511f32004-10-31 18:01:13 +0000939
paul718e3742002-12-13 20:15:29 +0000940 rt = EXTERNAL_INFO (type);
941
paul020709f2003-04-04 02:44:16 +0000942 ospf = ospf_lookup ();
943 if (ospf == NULL)
944 return 0;
945
paul68980082003-03-25 05:07:42 +0000946 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000947
948 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
949
950 /* foreach all external info. */
951 if (rt)
952 for (rn = route_top (rt); rn; rn = route_next (rn))
953 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000954 {
955 if (is_prefix_default (&ei->p))
956 ospf_external_lsa_refresh_default (ospf);
957 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
958 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
959 else
960 ospf_external_lsa_originate (ospf, ei);
961 }
paul718e3742002-12-13 20:15:29 +0000962 return 0;
963}
964
965#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
966
967/* Update distribute-list and set timer to apply access-list. */
968void
paul68980082003-03-25 05:07:42 +0000969ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000970{
971 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000972
paul718e3742002-12-13 20:15:29 +0000973 /* External info does not exist. */
974 if (!(rt = EXTERNAL_INFO (type)))
975 return;
976
977 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000978 if (ospf->t_distribute_update)
979 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000980
981 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000982 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000983 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000984 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000985}
986
987/* If access-list is updated, apply some check. */
paul4dadc292005-05-06 21:37:42 +0000988static void
paul718e3742002-12-13 20:15:29 +0000989ospf_filter_update (struct access_list *access)
990{
paul020709f2003-04-04 02:44:16 +0000991 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000992 int type;
993 int abr_inv = 0;
994 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000995 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000996
997 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000998 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000999 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001000 return;
1001
paul718e3742002-12-13 20:15:29 +00001002 /* Update distribute-list, and apply filter. */
hasso01018ce2005-08-05 07:40:15 +00001003 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
paul718e3742002-12-13 20:15:29 +00001004 {
paul020709f2003-04-04 02:44:16 +00001005 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001006 {
1007 /* if route-map is not NULL it may be using this access list */
1008 ospf_distribute_list_update (ospf, type);
1009 continue;
1010 }
1011
hasso01018ce2005-08-05 07:40:15 +00001012 /* There is place for route-map for default-information (ZEBRA_ROUTE_MAX),
1013 * but no distribute list. */
1014 if (type == ZEBRA_ROUTE_MAX)
1015 break;
paul718e3742002-12-13 20:15:29 +00001016
paul020709f2003-04-04 02:44:16 +00001017 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001018 {
1019 /* Keep old access-list for distribute-list. */
1020 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1021
1022 /* Update access-list for distribute-list. */
1023 DISTRIBUTE_LIST (ospf, type) =
1024 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1025
1026 /* No update for this distribute type. */
1027 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1028 continue;
1029
1030 /* Schedule distribute-list update timer. */
1031 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1032 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1033 ospf_distribute_list_update (ospf, type);
1034 }
paul718e3742002-12-13 20:15:29 +00001035 }
1036
1037 /* Update Area access-list. */
paul1eb8ef22005-04-07 07:30:20 +00001038 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1039 {
1040 if (EXPORT_NAME (area))
1041 {
1042 EXPORT_LIST (area) = NULL;
1043 abr_inv++;
1044 }
paul718e3742002-12-13 20:15:29 +00001045
paul1eb8ef22005-04-07 07:30:20 +00001046 if (IMPORT_NAME (area))
1047 {
1048 IMPORT_LIST (area) = NULL;
1049 abr_inv++;
1050 }
1051 }
paul718e3742002-12-13 20:15:29 +00001052
1053 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001054 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001055 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001056}
hassodd669bb2004-05-10 07:43:59 +00001057
1058/* If prefix-list is updated, do some updates. */
1059void
1060ospf_prefix_list_update (struct prefix_list *plist)
1061{
1062 struct ospf *ospf;
1063 int type;
1064 int abr_inv = 0;
1065 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001066 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001067
1068 /* If OSPF instatnce does not exist, return right now. */
1069 ospf = ospf_lookup ();
1070 if (ospf == NULL)
1071 return;
1072
1073 /* Update all route-maps which are used as redistribution filters.
1074 * They might use prefix-list.
1075 */
hasso01018ce2005-08-05 07:40:15 +00001076 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
hassodd669bb2004-05-10 07:43:59 +00001077 {
1078 if (ROUTEMAP (ospf, type) != NULL)
1079 {
1080 /* If route-map is not NULL it may be using this prefix list */
1081 ospf_distribute_list_update (ospf, type);
1082 continue;
1083 }
1084 }
1085
1086 /* Update area filter-lists. */
paul1eb8ef22005-04-07 07:30:20 +00001087 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
1088 {
1089 /* Update filter-list in. */
1090 if (PREFIX_NAME_IN (area))
1091 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1092 {
1093 PREFIX_LIST_IN (area) =
1094 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1095 abr_inv++;
1096 }
hassodd669bb2004-05-10 07:43:59 +00001097
paul1eb8ef22005-04-07 07:30:20 +00001098 /* Update filter-list out. */
1099 if (PREFIX_NAME_OUT (area))
1100 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1101 {
1102 PREFIX_LIST_IN (area) =
1103 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1104 abr_inv++;
1105 }
1106 }
hassodd669bb2004-05-10 07:43:59 +00001107
1108 /* Schedule ABR task. */
1109 if (IS_OSPF_ABR (ospf) && abr_inv)
1110 ospf_schedule_abr_task (ospf);
1111}
paulcf795c52003-06-19 02:13:25 +00001112
paul4dadc292005-05-06 21:37:42 +00001113static struct ospf_distance *
1114ospf_distance_new (void)
paul718e3742002-12-13 20:15:29 +00001115{
Stephen Hemminger393deb92008-08-18 14:13:29 -07001116 return XCALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
paul718e3742002-12-13 20:15:29 +00001117}
1118
paul4dadc292005-05-06 21:37:42 +00001119static void
paul718e3742002-12-13 20:15:29 +00001120ospf_distance_free (struct ospf_distance *odistance)
1121{
1122 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1123}
1124
1125int
paul6c835672004-10-11 11:00:30 +00001126ospf_distance_set (struct vty *vty, struct ospf *ospf,
1127 const char *distance_str,
1128 const char *ip_str,
1129 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001130{
1131 int ret;
1132 struct prefix_ipv4 p;
1133 u_char distance;
1134 struct route_node *rn;
1135 struct ospf_distance *odistance;
1136
1137 ret = str2prefix_ipv4 (ip_str, &p);
1138 if (ret == 0)
1139 {
1140 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1141 return CMD_WARNING;
1142 }
1143
1144 distance = atoi (distance_str);
1145
1146 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001147 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001148 if (rn->info)
1149 {
1150 odistance = rn->info;
1151 route_unlock_node (rn);
1152 }
1153 else
1154 {
1155 odistance = ospf_distance_new ();
1156 rn->info = odistance;
1157 }
1158
1159 /* Set distance value. */
1160 odistance->distance = distance;
1161
1162 /* Reset access-list configuration. */
1163 if (odistance->access_list)
1164 {
1165 free (odistance->access_list);
1166 odistance->access_list = NULL;
1167 }
1168 if (access_list_str)
1169 odistance->access_list = strdup (access_list_str);
1170
1171 return CMD_SUCCESS;
1172}
1173
1174int
paul6c835672004-10-11 11:00:30 +00001175ospf_distance_unset (struct vty *vty, struct ospf *ospf,
1176 const char *distance_str,
1177 const char *ip_str, char
1178 const *access_list_str)
paul718e3742002-12-13 20:15:29 +00001179{
1180 int ret;
1181 struct prefix_ipv4 p;
1182 u_char distance;
1183 struct route_node *rn;
1184 struct ospf_distance *odistance;
1185
1186 ret = str2prefix_ipv4 (ip_str, &p);
1187 if (ret == 0)
1188 {
1189 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1190 return CMD_WARNING;
1191 }
1192
1193 distance = atoi (distance_str);
1194
paulcf795c52003-06-19 02:13:25 +00001195 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1196 if (!rn)
paul718e3742002-12-13 20:15:29 +00001197 {
1198 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1199 return CMD_WARNING;
1200 }
1201
1202 odistance = rn->info;
1203
1204 if (odistance->access_list)
1205 free (odistance->access_list);
1206 ospf_distance_free (odistance);
1207
1208 rn->info = NULL;
1209 route_unlock_node (rn);
1210 route_unlock_node (rn);
1211
1212 return CMD_SUCCESS;
1213}
1214
1215void
paul68980082003-03-25 05:07:42 +00001216ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001217{
1218 struct route_node *rn;
1219 struct ospf_distance *odistance;
1220
paul68980082003-03-25 05:07:42 +00001221 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001222 if ((odistance = rn->info) != NULL)
1223 {
paulcf795c52003-06-19 02:13:25 +00001224 if (odistance->access_list)
1225 free (odistance->access_list);
1226 ospf_distance_free (odistance);
1227 rn->info = NULL;
1228 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001229 }
1230}
1231
1232u_char
1233ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1234{
paul020709f2003-04-04 02:44:16 +00001235 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001236
paul020709f2003-04-04 02:44:16 +00001237 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001238 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001239 return 0;
1240
paul68980082003-03-25 05:07:42 +00001241 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001242 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001243 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001244
paul68980082003-03-25 05:07:42 +00001245 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001246 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001247 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001248
paul68980082003-03-25 05:07:42 +00001249 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001250 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001251 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001252 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001253
paul68980082003-03-25 05:07:42 +00001254 if (ospf->distance_all)
1255 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001256
1257 return 0;
1258}
1259
1260void
1261ospf_zebra_init ()
1262{
1263 /* Allocate zebra structure. */
1264 zclient = zclient_new ();
1265 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001266 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001267 zclient->interface_add = ospf_interface_add;
1268 zclient->interface_delete = ospf_interface_delete;
1269 zclient->interface_up = ospf_interface_state_up;
1270 zclient->interface_down = ospf_interface_state_down;
1271 zclient->interface_address_add = ospf_interface_address_add;
1272 zclient->interface_address_delete = ospf_interface_address_delete;
1273 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1274 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1275
1276 access_list_add_hook (ospf_filter_update);
1277 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001278 prefix_list_add_hook (ospf_prefix_list_update);
1279 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001280}