blob: 3172ebc5e5bfc81b9e601be7eaa2366c8786577d [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))
90 zlog_info ("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);
98 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
paulcf795c52003-06-19 02:13:25 +000099
paul718e3742002-12-13 20:15:29 +0000100 if (if_is_broadcast (ifp))
paulcf795c52003-06-19 02:13:25 +0000101 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
paul718e3742002-12-13 20:15:29 +0000102 else if (if_is_pointopoint (ifp))
paulcf795c52003-06-19 02:13:25 +0000103 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_POINTOPOINT;
paul718e3742002-12-13 20:15:29 +0000104 else if (if_is_loopback (ifp))
paulcf795c52003-06-19 02:13:25 +0000105 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_LOOPBACK;
paul718e3742002-12-13 20:15:29 +0000106 }
107
paul020709f2003-04-04 02:44:16 +0000108 ospf = ospf_lookup ();
109 if (ospf != NULL)
110 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000111
112#ifdef HAVE_SNMP
113 ospf_snmp_if_update (ifp);
114#endif /* HAVE_SNMP */
115
116 return 0;
117}
118
119int
120ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000121 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000122{
123 struct interface *ifp;
124 struct stream *s;
125 struct route_node *rn;
126
paulcf795c52003-06-19 02:13:25 +0000127 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000128 /* zebra_interface_state_read() updates interface structure in iflist */
129 ifp = zebra_interface_state_read (s);
130
131 if (ifp == NULL)
132 return 0;
133
134 if (if_is_up (ifp))
135 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000136 ifp->name);
137
paul718e3742002-12-13 20:15:29 +0000138 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
paulcf795c52003-06-19 02:13:25 +0000139 zlog_info
140 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
141 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000142
143#ifdef HAVE_SNMP
144 ospf_snmp_if_delete (ifp);
145#endif /* HAVE_SNMP */
146
147 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
148 if (rn->info)
149 ospf_if_free ((struct ospf_interface *) rn->info);
150
paul718e3742002-12-13 20:15:29 +0000151 return 0;
152}
153
154struct interface *
155zebra_interface_if_lookup (struct stream *s)
156{
157 struct interface *ifp;
158 u_char ifname_tmp[INTERFACE_NAMSIZ];
159
160 /* Read interface name. */
161 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
162
163 /* Lookup this by interface index. */
hassoc9e52be2004-09-26 16:09:34 +0000164 ifp = if_lookup_by_name ((char *) ifname_tmp);
paul718e3742002-12-13 20:15:29 +0000165
166 /* If such interface does not exist, indicate an error */
167 if (!ifp)
168 return NULL;
169
170 return ifp;
171}
172
paul718e3742002-12-13 20:15:29 +0000173int
174ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000175 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000176{
177 struct interface *ifp;
178 struct interface if_tmp;
179 struct ospf_interface *oi;
180 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000181
paul718e3742002-12-13 20:15:29 +0000182 ifp = zebra_interface_if_lookup (zclient->ibuf);
183
184 if (ifp == NULL)
185 return 0;
186
187 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000188 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000189 {
190 /* Temporarily keep ifp values. */
191 memcpy (&if_tmp, ifp, sizeof (struct interface));
192
193 zebra_interface_if_set_value (zclient->ibuf, ifp);
194
195 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
paulcf795c52003-06-19 02:13:25 +0000196 zlog_info ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000197
198 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000199 {
200 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
201 zlog_info ("Zebra: Interface[%s] bandwidth change %d -> %d.",
202 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000203
paulcf795c52003-06-19 02:13:25 +0000204 ospf_if_recalculate_output_cost (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))
212 zlog_info ("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
225int
226ospf_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))
239 zlog_info ("Zebra: Interface[%s] state change to down.", ifp->name);
240
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
251int
252ospf_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{
paul020709f2003-04-04 02:44:16 +0000255 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000256 struct connected *c;
257
paul0a589352004-05-08 11:48:26 +0000258 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000259
260 if (c == NULL)
261 return 0;
262
paul020709f2003-04-04 02:44:16 +0000263 ospf = ospf_lookup ();
264 if (ospf != NULL)
265 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000266
267#ifdef HAVE_SNMP
268 ospf_snmp_if_update (c->ifp);
269#endif /* HAVE_SNMP */
270
271 return 0;
272}
273
274int
275ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000276 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000277{
paul020709f2003-04-04 02:44:16 +0000278 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000279 struct connected *c;
280 struct interface *ifp;
281 struct ospf_interface *oi;
282 struct route_node *rn;
283 struct prefix p;
284
paul0a589352004-05-08 11:48:26 +0000285 c = zebra_interface_address_read (command, zclient->ibuf);
paul718e3742002-12-13 20:15:29 +0000286
287 if (c == NULL)
288 return 0;
289
290 ifp = c->ifp;
291 p = *c->address;
292 p.prefixlen = IPV4_MAX_PREFIXLEN;
293
294 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000295 if (!rn)
paul718e3742002-12-13 20:15:29 +0000296 return 0;
297
298 assert (rn->info);
299 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000300
paul718e3742002-12-13 20:15:29 +0000301 /* Call interface hook functions to clean up */
302 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000303
paul718e3742002-12-13 20:15:29 +0000304#ifdef HAVE_SNMP
305 ospf_snmp_if_update (c->ifp);
306#endif /* HAVE_SNMP */
307
308 connected_free (c);
309
paul020709f2003-04-04 02:44:16 +0000310 ospf = ospf_lookup ();
311 if (ospf != NULL)
312 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000313
314 return 0;
315}
paul72357f22003-06-19 02:11:23 +0000316
paul718e3742002-12-13 20:15:29 +0000317void
318ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
319{
320 u_char message;
321 u_char distance;
322 u_char flags;
323 int psize;
324 struct stream *s;
325 struct ospf_path *path;
hasso52dc7ee2004-09-23 19:18:23 +0000326 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000327
328 if (zclient->redist[ZEBRA_ROUTE_OSPF])
329 {
330 message = 0;
331 flags = 0;
332
333 /* OSPF pass nexthop and metric */
334 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
335 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
336
337 /* Distance value. */
338 distance = ospf_distance_apply (p, or);
339 if (distance)
paul72357f22003-06-19 02:11:23 +0000340 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000341
342 /* Make packet. */
343 s = zclient->obuf;
344 stream_reset (s);
345
346 /* Length place holder. */
347 stream_putw (s, 0);
348
349 /* Put command, type, flags, message. */
350 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
351 stream_putc (s, ZEBRA_ROUTE_OSPF);
352 stream_putc (s, flags);
353 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000354
paul718e3742002-12-13 20:15:29 +0000355 /* Put prefix information. */
356 psize = PSIZE (p->prefixlen);
357 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000358 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000359
360 /* Nexthop count. */
paul96735ee2003-08-10 02:51:22 +0000361 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000362
363 /* Nexthop, ifindex, distance and metric information. */
paul96735ee2003-08-10 02:51:22 +0000364 for (node = listhead (or->paths); node; nextnode (node))
paul72357f22003-06-19 02:11:23 +0000365 {
paulcf795c52003-06-19 02:13:25 +0000366 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000367
paulcf795c52003-06-19 02:13:25 +0000368 if (path->nexthop.s_addr != INADDR_ANY)
369 {
370 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
371 stream_put_in_addr (s, &path->nexthop);
372 }
373 else
374 {
375 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
376 if (path->oi)
377 stream_putl (s, path->oi->ifp->ifindex);
378 else
379 stream_putl (s, 0);
380 }
paul72357f22003-06-19 02:11:23 +0000381
382 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
383 {
384 zlog_info ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000385 inet_ntoa (p->prefix),
386 p->prefixlen, inet_ntoa (path->nexthop));
387 }
388 }
paul718e3742002-12-13 20:15:29 +0000389
390 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000391 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000392 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000393 {
394 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
395 stream_putl (s, or->cost + or->u.ext.type2_cost);
396 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
397 stream_putl (s, or->u.ext.type2_cost);
398 else
399 stream_putl (s, or->cost);
400 }
paul718e3742002-12-13 20:15:29 +0000401
402 stream_putw_at (s, 0, stream_get_endp (s));
403
404 writen (zclient->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000405 }
406}
407
408void
409ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
410{
411 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000412 struct ospf_path *path;
413 struct in_addr *nexthop;
hasso52dc7ee2004-09-23 19:18:23 +0000414 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000415
416 if (zclient->redist[ZEBRA_ROUTE_OSPF])
417 {
418 api.type = ZEBRA_ROUTE_OSPF;
419 api.flags = 0;
420 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000421 api.ifindex_num = 0;
422 api.nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +0000423
paul96735ee2003-08-10 02:51:22 +0000424 for (node = listhead (or->paths); node; nextnode (node))
paulcf795c52003-06-19 02:13:25 +0000425 {
426 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000427
paulcf795c52003-06-19 02:13:25 +0000428 if (path->nexthop.s_addr != INADDR_ANY)
429 {
pauld8e1d6b2003-08-10 04:04:41 +0000430 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paulcf795c52003-06-19 02:13:25 +0000431 api.nexthop_num = 1;
432 nexthop = &path->nexthop;
433 api.nexthop = &nexthop;
434 }
hasso2db3d052004-02-11 21:52:13 +0000435 else if (ospf_if_exists(path->oi) && (path->oi->ifp))
paulbb8ff1e2003-08-12 06:00:30 +0000436 {
437 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
438 api.ifindex_num = 1;
439 api.ifindex = &path->oi->ifp->ifindex;
440 }
441 else if ( IS_DEBUG_OSPF(zebra,ZEBRA_REDISTRIBUTE) )
442 {
443 zlog_info("Zebra: no ifp %s %d",
444 inet_ntoa(p->prefix),
445 p->prefixlen);
446 }
paul72357f22003-06-19 02:11:23 +0000447
paul0a589352004-05-08 11:48:26 +0000448 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000449
450 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000451 {
452 zlog_info ("Zebra: Route delete %s/%d nexthop %s",
453 inet_ntoa (p->prefix),
454 p->prefixlen, inet_ntoa (**api.nexthop));
455 }
paulbb8ff1e2003-08-12 06:00:30 +0000456 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.ifindex_num)
457 {
458 zlog_info ("Zebra: Route delete %s/%d ifindex %d",
459 inet_ntoa (p->prefix),
460 p->prefixlen, *api.ifindex);
461 }
paulcf795c52003-06-19 02:13:25 +0000462 }
paul718e3742002-12-13 20:15:29 +0000463 }
464}
465
466void
467ospf_zebra_add_discard (struct prefix_ipv4 *p)
468{
469 struct zapi_ipv4 api;
470
471 if (zclient->redist[ZEBRA_ROUTE_OSPF])
472 {
473 api.type = ZEBRA_ROUTE_OSPF;
474 api.flags = ZEBRA_FLAG_BLACKHOLE;
475 api.message = 0;
476 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
477 api.nexthop_num = 0;
478 api.ifindex_num = 0;
479
paul0a589352004-05-08 11:48:26 +0000480 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, p, &api);
paul718e3742002-12-13 20:15:29 +0000481 }
482}
483
484void
485ospf_zebra_delete_discard (struct prefix_ipv4 *p)
486{
487 struct zapi_ipv4 api;
488
489 if (zclient->redist[ZEBRA_ROUTE_OSPF])
490 {
491 api.type = ZEBRA_ROUTE_OSPF;
492 api.flags = ZEBRA_FLAG_BLACKHOLE;
493 api.message = 0;
494 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
495 api.nexthop_num = 0;
496 api.ifindex_num = 0;
497
paul0a589352004-05-08 11:48:26 +0000498 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000499
500 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000501 zlog_info ("Zebra: Route delete discard %s/%d",
502 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000503
paul718e3742002-12-13 20:15:29 +0000504 }
505}
506
507int
508ospf_is_type_redistributed (int type)
509{
510 return (DEFAULT_ROUTE_TYPE (type)) ?
511 zclient->default_information : zclient->redist[type];
512}
513
514int
paul020709f2003-04-04 02:44:16 +0000515ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000516{
517 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000518
paul718e3742002-12-13 20:15:29 +0000519 if (ospf_is_type_redistributed (type))
520 {
paul68980082003-03-25 05:07:42 +0000521 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000522 {
523 ospf->dmetric[type].type = mtype;
524 force = LSA_REFRESH_FORCE;
525 }
paul68980082003-03-25 05:07:42 +0000526 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000527 {
528 ospf->dmetric[type].value = mvalue;
529 force = LSA_REFRESH_FORCE;
530 }
531
paul68980082003-03-25 05:07:42 +0000532 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000533
paul718e3742002-12-13 20:15:29 +0000534 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000535 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
536 LOOKUP (ospf_redistributed_proto, type),
537 metric_type (ospf, type), metric_value (ospf, type));
538
paul718e3742002-12-13 20:15:29 +0000539 return CMD_SUCCESS;
540 }
541
paul68980082003-03-25 05:07:42 +0000542 ospf->dmetric[type].type = mtype;
543 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000544
paul0a589352004-05-08 11:48:26 +0000545 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
paul718e3742002-12-13 20:15:29 +0000546
547 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
548 zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000549 LOOKUP (ospf_redistributed_proto, type),
550 metric_type (ospf, type), metric_value (ospf, type));
551
paul68980082003-03-25 05:07:42 +0000552 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000553
554 return CMD_SUCCESS;
555}
556
557int
paul020709f2003-04-04 02:44:16 +0000558ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000559{
560 if (type == zclient->redist_default)
561 return CMD_SUCCESS;
562
paulcf795c52003-06-19 02:13:25 +0000563 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000564 return CMD_SUCCESS;
565
paul0a589352004-05-08 11:48:26 +0000566 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
paulcf795c52003-06-19 02:13:25 +0000567
paul718e3742002-12-13 20:15:29 +0000568 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
569 zlog_info ("Redistribute[%s]: Stop",
paulcf795c52003-06-19 02:13:25 +0000570 LOOKUP (ospf_redistributed_proto, type));
paul718e3742002-12-13 20:15:29 +0000571
paul68980082003-03-25 05:07:42 +0000572 ospf->dmetric[type].type = -1;
573 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000574
575 /* Remove the routes from OSPF table. */
576 ospf_redistribute_withdraw (type);
577
paul68980082003-03-25 05:07:42 +0000578 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000579
580 return CMD_SUCCESS;
581}
582
583int
paul020709f2003-04-04 02:44:16 +0000584ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000585 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000586{
587 int force = 0;
paul020709f2003-04-04 02:44:16 +0000588
paul718e3742002-12-13 20:15:29 +0000589 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
590 {
paul68980082003-03-25 05:07:42 +0000591 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000592 {
593 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
594 force = 1;
595 }
paul68980082003-03-25 05:07:42 +0000596 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000597 {
598 force = 1;
599 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
600 }
601
paul68980082003-03-25 05:07:42 +0000602 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000603
paul718e3742002-12-13 20:15:29 +0000604 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000605 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
606 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
607 metric_type (ospf, DEFAULT_ROUTE),
608 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000609 return CMD_SUCCESS;
610 }
611
paul68980082003-03-25 05:07:42 +0000612 ospf->default_originate = originate;
613 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
614 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000615
paul0a589352004-05-08 11:48:26 +0000616 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient);
paulcf795c52003-06-19 02:13:25 +0000617
paul718e3742002-12-13 20:15:29 +0000618 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
619 zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000620 metric_type (ospf, DEFAULT_ROUTE),
621 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000622
paul68980082003-03-25 05:07:42 +0000623 if (ospf->router_id.s_addr == 0)
624 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000625 else
626 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000627 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000628
paul68980082003-03-25 05:07:42 +0000629 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000630
631 return CMD_SUCCESS;
632}
633
634int
paul020709f2003-04-04 02:44:16 +0000635ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000636{
637 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
638 return CMD_SUCCESS;
639
paul68980082003-03-25 05:07:42 +0000640 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
641 ospf->dmetric[DEFAULT_ROUTE].type = -1;
642 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000643
paul0a589352004-05-08 11:48:26 +0000644 zclient_redistribute_default (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient);
paul718e3742002-12-13 20:15:29 +0000645
646 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
647 zlog_info ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000648
paul68980082003-03-25 05:07:42 +0000649 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000650
651 return CMD_SUCCESS;
652}
653
654int
paul020709f2003-04-04 02:44:16 +0000655ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000656 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000657{
658 /* If prefix is multicast, then do not originate LSA. */
659 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
660 {
661 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000662 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000663 return 0;
664 }
665
666 /* Take care of default-originate. */
667 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000668 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000669 {
paulcf795c52003-06-19 02:13:25 +0000670 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
671 "for default");
672 return 0;
paul718e3742002-12-13 20:15:29 +0000673 }
674
675 return 1;
676}
677
678/* If connected prefix is OSPF enable interface, then do not announce. */
679int
paulcf795c52003-06-19 02:13:25 +0000680ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000681{
682 struct route_node *rn;
683
paul68980082003-03-25 05:07:42 +0000684 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000685 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000686 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
687 {
688 route_unlock_node (rn);
689 return 0;
690 }
paul718e3742002-12-13 20:15:29 +0000691
692 return 1;
693}
694
695/* return 1 if external LSA must be originated, 0 otherwise */
696int
paul68980082003-03-25 05:07:42 +0000697ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000698 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000699{
700 struct route_map_set_values save_values;
701 struct prefix_ipv4 *p = &ei->p;
702 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000703
paul718e3742002-12-13 20:15:29 +0000704 if (changed)
705 *changed = 0;
706
paul020709f2003-04-04 02:44:16 +0000707 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000708 return 0;
709
710 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000711 if (type == ZEBRA_ROUTE_CONNECT &&
712 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000713 return 0;
714
paul020709f2003-04-04 02:44:16 +0000715 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000716 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000717 if (DISTRIBUTE_LIST (ospf, type))
718 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000719 {
720 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
721 zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
722 LOOKUP (ospf_redistributed_proto, type),
723 inet_ntoa (p->prefix), p->prefixlen);
724 return 0;
725 }
paul718e3742002-12-13 20:15:29 +0000726
727 save_values = ei->route_map_set;
728 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000729
paul718e3742002-12-13 20:15:29 +0000730 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000731 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000732 {
733 int ret;
734
paulcf795c52003-06-19 02:13:25 +0000735 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
736 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000737
738 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000739 {
740 ei->route_map_set = save_values;
741 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
742 zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.",
743 LOOKUP (ospf_redistributed_proto, type),
744 inet_ntoa (p->prefix), p->prefixlen);
745 return 0;
746 }
747
paul718e3742002-12-13 20:15:29 +0000748 /* check if 'route-map set' changed something */
749 if (changed)
paulcf795c52003-06-19 02:13:25 +0000750 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
751 &save_values);
paul718e3742002-12-13 20:15:29 +0000752 }
753
754 return 1;
755}
756
757/* OSPF route-map set for redistribution */
758void
paul020709f2003-04-04 02:44:16 +0000759ospf_routemap_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000760{
paul020709f2003-04-04 02:44:16 +0000761 if (ROUTEMAP_NAME (ospf, type))
762 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000763
paul020709f2003-04-04 02:44:16 +0000764 ROUTEMAP_NAME (ospf, type) = strdup (name);
765 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000766}
767
768void
paul020709f2003-04-04 02:44:16 +0000769ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000770{
paul020709f2003-04-04 02:44:16 +0000771 if (ROUTEMAP_NAME (ospf, type))
772 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000773
paul020709f2003-04-04 02:44:16 +0000774 ROUTEMAP_NAME (ospf, type) = NULL;
775 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000776}
777
778/* Zebra route add and delete treatment. */
779int
780ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000781 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000782{
783 struct stream *s;
784 struct zapi_ipv4 api;
785 unsigned long ifindex;
786 struct in_addr nexthop;
787 struct prefix_ipv4 p;
788 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000789 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000790
791 s = zclient->ibuf;
792 ifindex = 0;
793 nexthop.s_addr = 0;
794
795 /* Type, flags, message. */
796 api.type = stream_getc (s);
797 api.flags = stream_getc (s);
798 api.message = stream_getc (s);
799
800 /* IPv4 prefix. */
801 memset (&p, 0, sizeof (struct prefix_ipv4));
802 p.family = AF_INET;
803 p.prefixlen = stream_getc (s);
804 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
805
hasso8585d4e2004-04-20 17:25:12 +0000806 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
807 return 0;
808
paul718e3742002-12-13 20:15:29 +0000809 /* Nexthop, ifindex, distance, metric. */
810 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
811 {
812 api.nexthop_num = stream_getc (s);
813 nexthop.s_addr = stream_get_ipv4 (s);
814 }
815 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
816 {
817 api.ifindex_num = stream_getc (s);
gdt6a8da852004-02-13 17:40:51 +0000818 /* XXX assert(api.ifindex_num == 1); */
paul718e3742002-12-13 20:15:29 +0000819 ifindex = stream_getl (s);
820 }
821 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
822 api.distance = stream_getc (s);
823 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
824 api.metric = stream_getl (s);
825
paul020709f2003-04-04 02:44:16 +0000826 ospf = ospf_lookup ();
827 if (ospf == NULL)
828 return 0;
829
paul718e3742002-12-13 20:15:29 +0000830 if (command == ZEBRA_IPV4_ROUTE_ADD)
831 {
paul7021c422003-07-15 12:52:22 +0000832 /* XXX|HACK|TODO|FIXME:
hassoa0a39762004-04-23 08:51:10 +0000833 * Maybe we should ignore reject/blackhole routes? Testing shows that
834 * there is no problems though and this is only way to "summarize"
835 * routes in ASBR at the moment. Maybe we need just a better generalised
836 * solution for these types?
837 *
838 * if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
839 * || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
840 * return 0;
paul7021c422003-07-15 12:52:22 +0000841 */
paul7021c422003-07-15 12:52:22 +0000842
paul718e3742002-12-13 20:15:29 +0000843 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
844
paul68980082003-03-25 05:07:42 +0000845 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000846 /* Set flags to generate AS-external-LSA originate event
847 for each redistributed protocols later. */
848 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000849 else
paulcf795c52003-06-19 02:13:25 +0000850 {
851 if (ei)
852 {
853 if (is_prefix_default (&p))
854 ospf_external_lsa_refresh_default (ospf);
855 else
856 {
857 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000858
paulcf795c52003-06-19 02:13:25 +0000859 current = ospf_external_info_find_lsa (ospf, &ei->p);
860 if (!current)
861 ospf_external_lsa_originate (ospf, ei);
862 else if (IS_LSA_MAXAGE (current))
863 ospf_external_lsa_refresh (ospf, current,
864 ei, LSA_REFRESH_FORCE);
865 else
866 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
867 inet_ntoa (p.prefix));
868 }
869 }
870 }
paul718e3742002-12-13 20:15:29 +0000871 }
paulcf795c52003-06-19 02:13:25 +0000872 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000873 {
874 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000875 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000876 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000877 else
878 ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop);
paul718e3742002-12-13 20:15:29 +0000879 }
880
881 return 0;
882}
paul718e3742002-12-13 20:15:29 +0000883
paulcf795c52003-06-19 02:13:25 +0000884
paul718e3742002-12-13 20:15:29 +0000885int
paul68980082003-03-25 05:07:42 +0000886ospf_distribute_list_out_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000887{
888 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000889 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000890
891 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000892 if (DISTRIBUTE_NAME (ospf, type))
893 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000894
895 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000896 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000897
898 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000899 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000900 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000901
902 return CMD_SUCCESS;
903}
904
905int
paul68980082003-03-25 05:07:42 +0000906ospf_distribute_list_out_unset (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000907{
908 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000909 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000910 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000911
912 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000913 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000914
915 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000916 if (DISTRIBUTE_NAME (ospf, type))
917 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000918
paul020709f2003-04-04 02:44:16 +0000919 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000920
921 return CMD_SUCCESS;
922}
923
924/* distribute-list update timer. */
925int
926ospf_distribute_list_update_timer (struct thread *thread)
927{
928 struct route_node *rn;
929 struct external_info *ei;
930 struct route_table *rt;
931 struct ospf_lsa *lsa;
932 u_char type;
paul020709f2003-04-04 02:44:16 +0000933 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000934
935 type = (int) THREAD_ARG (thread);
936 rt = EXTERNAL_INFO (type);
937
paul020709f2003-04-04 02:44:16 +0000938 ospf = ospf_lookup ();
939 if (ospf == NULL)
940 return 0;
941
paul68980082003-03-25 05:07:42 +0000942 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000943
944 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
945
946 /* foreach all external info. */
947 if (rt)
948 for (rn = route_top (rt); rn; rn = route_next (rn))
949 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000950 {
951 if (is_prefix_default (&ei->p))
952 ospf_external_lsa_refresh_default (ospf);
953 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
954 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
955 else
956 ospf_external_lsa_originate (ospf, ei);
957 }
paul718e3742002-12-13 20:15:29 +0000958 return 0;
959}
960
961#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
962
963/* Update distribute-list and set timer to apply access-list. */
964void
paul68980082003-03-25 05:07:42 +0000965ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000966{
967 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000968
paul718e3742002-12-13 20:15:29 +0000969 /* External info does not exist. */
970 if (!(rt = EXTERNAL_INFO (type)))
971 return;
972
973 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000974 if (ospf->t_distribute_update)
975 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000976
977 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000978 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000979 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000980 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000981}
982
983/* If access-list is updated, apply some check. */
984void
985ospf_filter_update (struct access_list *access)
986{
paul020709f2003-04-04 02:44:16 +0000987 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000988 int type;
989 int abr_inv = 0;
990 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +0000991 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000992
993 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000994 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000995 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000996 return;
997
paul718e3742002-12-13 20:15:29 +0000998 /* Update distribute-list, and apply filter. */
999 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1000 {
paul020709f2003-04-04 02:44:16 +00001001 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +00001002 {
1003 /* if route-map is not NULL it may be using this access list */
1004 ospf_distribute_list_update (ospf, type);
1005 continue;
1006 }
1007
paul718e3742002-12-13 20:15:29 +00001008
paul020709f2003-04-04 02:44:16 +00001009 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +00001010 {
1011 /* Keep old access-list for distribute-list. */
1012 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
1013
1014 /* Update access-list for distribute-list. */
1015 DISTRIBUTE_LIST (ospf, type) =
1016 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
1017
1018 /* No update for this distribute type. */
1019 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1020 continue;
1021
1022 /* Schedule distribute-list update timer. */
1023 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1024 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1025 ospf_distribute_list_update (ospf, type);
1026 }
paul718e3742002-12-13 20:15:29 +00001027 }
1028
1029 /* Update Area access-list. */
paul68980082003-03-25 05:07:42 +00001030 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001031 if ((area = getdata (node)) != NULL)
1032 {
paulcf795c52003-06-19 02:13:25 +00001033 if (EXPORT_NAME (area))
1034 {
1035 EXPORT_LIST (area) = NULL;
1036 abr_inv++;
1037 }
paul718e3742002-12-13 20:15:29 +00001038
paulcf795c52003-06-19 02:13:25 +00001039 if (IMPORT_NAME (area))
1040 {
1041 IMPORT_LIST (area) = NULL;
1042 abr_inv++;
1043 }
paul718e3742002-12-13 20:15:29 +00001044 }
1045
1046 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001047 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001048 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001049}
hassodd669bb2004-05-10 07:43:59 +00001050
1051/* If prefix-list is updated, do some updates. */
1052void
1053ospf_prefix_list_update (struct prefix_list *plist)
1054{
1055 struct ospf *ospf;
1056 int type;
1057 int abr_inv = 0;
1058 struct ospf_area *area;
hasso52dc7ee2004-09-23 19:18:23 +00001059 struct listnode *node;
hassodd669bb2004-05-10 07:43:59 +00001060
1061 /* If OSPF instatnce does not exist, return right now. */
1062 ospf = ospf_lookup ();
1063 if (ospf == NULL)
1064 return;
1065
1066 /* Update all route-maps which are used as redistribution filters.
1067 * They might use prefix-list.
1068 */
1069 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1070 {
1071 if (ROUTEMAP (ospf, type) != NULL)
1072 {
1073 /* If route-map is not NULL it may be using this prefix list */
1074 ospf_distribute_list_update (ospf, type);
1075 continue;
1076 }
1077 }
1078
1079 /* Update area filter-lists. */
1080 for (node = listhead (ospf->areas); node; nextnode (node))
1081 if ((area = getdata (node)) != NULL)
1082 {
1083 /* Update filter-list in. */
1084 if (PREFIX_NAME_IN (area))
1085 if (strcmp (PREFIX_NAME_IN (area), plist->name) == 0)
1086 {
1087 PREFIX_LIST_IN (area) =
1088 prefix_list_lookup (AFI_IP, PREFIX_NAME_IN (area));
1089 abr_inv++;
1090 }
1091
1092 /* Update filter-list out. */
1093 if (PREFIX_NAME_OUT (area))
1094 if (strcmp (PREFIX_NAME_OUT (area), plist->name) == 0)
1095 {
1096 PREFIX_LIST_IN (area) =
1097 prefix_list_lookup (AFI_IP, PREFIX_NAME_OUT (area));
1098 abr_inv++;
1099 }
1100 }
1101
1102 /* Schedule ABR task. */
1103 if (IS_OSPF_ABR (ospf) && abr_inv)
1104 ospf_schedule_abr_task (ospf);
1105}
paulcf795c52003-06-19 02:13:25 +00001106
paul718e3742002-12-13 20:15:29 +00001107struct ospf_distance *
1108ospf_distance_new ()
1109{
1110 struct ospf_distance *new;
1111 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1112 memset (new, 0, sizeof (struct ospf_distance));
1113 return new;
1114}
1115
1116void
1117ospf_distance_free (struct ospf_distance *odistance)
1118{
1119 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1120}
1121
1122int
paul020709f2003-04-04 02:44:16 +00001123ospf_distance_set (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001124 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001125{
1126 int ret;
1127 struct prefix_ipv4 p;
1128 u_char distance;
1129 struct route_node *rn;
1130 struct ospf_distance *odistance;
1131
1132 ret = str2prefix_ipv4 (ip_str, &p);
1133 if (ret == 0)
1134 {
1135 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1136 return CMD_WARNING;
1137 }
1138
1139 distance = atoi (distance_str);
1140
1141 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001142 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001143 if (rn->info)
1144 {
1145 odistance = rn->info;
1146 route_unlock_node (rn);
1147 }
1148 else
1149 {
1150 odistance = ospf_distance_new ();
1151 rn->info = odistance;
1152 }
1153
1154 /* Set distance value. */
1155 odistance->distance = distance;
1156
1157 /* Reset access-list configuration. */
1158 if (odistance->access_list)
1159 {
1160 free (odistance->access_list);
1161 odistance->access_list = NULL;
1162 }
1163 if (access_list_str)
1164 odistance->access_list = strdup (access_list_str);
1165
1166 return CMD_SUCCESS;
1167}
1168
1169int
paul020709f2003-04-04 02:44:16 +00001170ospf_distance_unset (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001171 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001172{
1173 int ret;
1174 struct prefix_ipv4 p;
1175 u_char distance;
1176 struct route_node *rn;
1177 struct ospf_distance *odistance;
1178
1179 ret = str2prefix_ipv4 (ip_str, &p);
1180 if (ret == 0)
1181 {
1182 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1183 return CMD_WARNING;
1184 }
1185
1186 distance = atoi (distance_str);
1187
paulcf795c52003-06-19 02:13:25 +00001188 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1189 if (!rn)
paul718e3742002-12-13 20:15:29 +00001190 {
1191 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1192 return CMD_WARNING;
1193 }
1194
1195 odistance = rn->info;
1196
1197 if (odistance->access_list)
1198 free (odistance->access_list);
1199 ospf_distance_free (odistance);
1200
1201 rn->info = NULL;
1202 route_unlock_node (rn);
1203 route_unlock_node (rn);
1204
1205 return CMD_SUCCESS;
1206}
1207
1208void
paul68980082003-03-25 05:07:42 +00001209ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001210{
1211 struct route_node *rn;
1212 struct ospf_distance *odistance;
1213
paul68980082003-03-25 05:07:42 +00001214 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001215 if ((odistance = rn->info) != NULL)
1216 {
paulcf795c52003-06-19 02:13:25 +00001217 if (odistance->access_list)
1218 free (odistance->access_list);
1219 ospf_distance_free (odistance);
1220 rn->info = NULL;
1221 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001222 }
1223}
1224
1225u_char
1226ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1227{
paul020709f2003-04-04 02:44:16 +00001228 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001229
paul020709f2003-04-04 02:44:16 +00001230 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001231 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001232 return 0;
1233
paul68980082003-03-25 05:07:42 +00001234 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001235 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001236 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001237
paul68980082003-03-25 05:07:42 +00001238 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001239 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001240 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001241
paul68980082003-03-25 05:07:42 +00001242 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001243 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001244 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001245 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001246
paul68980082003-03-25 05:07:42 +00001247 if (ospf->distance_all)
1248 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001249
1250 return 0;
1251}
1252
1253void
1254ospf_zebra_init ()
1255{
1256 /* Allocate zebra structure. */
1257 zclient = zclient_new ();
1258 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
hasso18a6dce2004-10-03 18:18:34 +00001259 zclient->router_id_update = ospf_router_id_update_zebra;
paul718e3742002-12-13 20:15:29 +00001260 zclient->interface_add = ospf_interface_add;
1261 zclient->interface_delete = ospf_interface_delete;
1262 zclient->interface_up = ospf_interface_state_up;
1263 zclient->interface_down = ospf_interface_state_down;
1264 zclient->interface_address_add = ospf_interface_address_add;
1265 zclient->interface_address_delete = ospf_interface_address_delete;
1266 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1267 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1268
1269 access_list_add_hook (ospf_filter_update);
1270 access_list_delete_hook (ospf_filter_update);
hassodd669bb2004-05-10 07:43:59 +00001271 prefix_list_add_hook (ospf_prefix_list_update);
1272 prefix_list_delete_hook (ospf_prefix_list_update);
paul718e3742002-12-13 20:15:29 +00001273}