blob: 7101d56f2193d1e940a671ebbc1b0dc91ec053b8 [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"
35#include "log.h"
36
37#include "ospfd/ospfd.h"
38#include "ospfd/ospf_interface.h"
39#include "ospfd/ospf_ism.h"
40#include "ospfd/ospf_asbr.h"
41#include "ospfd/ospf_asbr.h"
42#include "ospfd/ospf_abr.h"
43#include "ospfd/ospf_lsa.h"
44#include "ospfd/ospf_dump.h"
45#include "ospfd/ospf_route.h"
46#include "ospfd/ospf_zebra.h"
47#ifdef HAVE_SNMP
48#include "ospfd/ospf_snmp.h"
49#endif /* HAVE_SNMP */
50
51/* Zebra structure to hold current status. */
52struct zclient *zclient = NULL;
53
54/* For registering threads. */
55extern struct thread_master *master;
56
57/* Inteface addition message from zebra. */
58int
59ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
60{
61 struct interface *ifp;
paul020709f2003-04-04 02:44:16 +000062 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +000063
64 ifp = zebra_interface_add_read (zclient->ibuf);
65
66 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
67 zlog_info ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
paulcf795c52003-06-19 02:13:25 +000068 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +000069
paulcf795c52003-06-19 02:13:25 +000070 assert (ifp->info);
paulf2c80652002-12-13 21:44:27 +000071
paul718e3742002-12-13 20:15:29 +000072 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
73 {
74 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
75 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
paulcf795c52003-06-19 02:13:25 +000076
paul718e3742002-12-13 20:15:29 +000077 if (if_is_broadcast (ifp))
paulcf795c52003-06-19 02:13:25 +000078 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
paul718e3742002-12-13 20:15:29 +000079 else if (if_is_pointopoint (ifp))
paulcf795c52003-06-19 02:13:25 +000080 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_POINTOPOINT;
paul718e3742002-12-13 20:15:29 +000081 else if (if_is_loopback (ifp))
paulcf795c52003-06-19 02:13:25 +000082 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_LOOPBACK;
paul718e3742002-12-13 20:15:29 +000083 }
84
paul020709f2003-04-04 02:44:16 +000085 ospf = ospf_lookup ();
86 if (ospf != NULL)
87 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +000088
89#ifdef HAVE_SNMP
90 ospf_snmp_if_update (ifp);
91#endif /* HAVE_SNMP */
92
93 return 0;
94}
95
96int
97ospf_interface_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +000098 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +000099{
100 struct interface *ifp;
101 struct stream *s;
102 struct route_node *rn;
103
paulcf795c52003-06-19 02:13:25 +0000104 s = zclient->ibuf;
paul718e3742002-12-13 20:15:29 +0000105 /* zebra_interface_state_read() updates interface structure in iflist */
106 ifp = zebra_interface_state_read (s);
107
108 if (ifp == NULL)
109 return 0;
110
111 if (if_is_up (ifp))
112 zlog_warn ("Zebra: got delete of %s, but interface is still up",
paulcf795c52003-06-19 02:13:25 +0000113 ifp->name);
114
paul718e3742002-12-13 20:15:29 +0000115 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
paulcf795c52003-06-19 02:13:25 +0000116 zlog_info
117 ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
118 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
paul718e3742002-12-13 20:15:29 +0000119
120#ifdef HAVE_SNMP
121 ospf_snmp_if_delete (ifp);
122#endif /* HAVE_SNMP */
123
124 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
125 if (rn->info)
126 ospf_if_free ((struct ospf_interface *) rn->info);
127
128 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
129 if (rn->info)
130 ospf_del_if_params (rn->info);
paulcf795c52003-06-19 02:13:25 +0000131
paul718e3742002-12-13 20:15:29 +0000132 if_delete (ifp);
133
134 return 0;
135}
136
137struct interface *
138zebra_interface_if_lookup (struct stream *s)
139{
140 struct interface *ifp;
141 u_char ifname_tmp[INTERFACE_NAMSIZ];
142
143 /* Read interface name. */
144 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
145
146 /* Lookup this by interface index. */
147 ifp = if_lookup_by_name (ifname_tmp);
148
149 /* If such interface does not exist, indicate an error */
150 if (!ifp)
151 return NULL;
152
153 return ifp;
154}
155
156void
157zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
158{
159 /* Read interface's index. */
160 ifp->ifindex = stream_getl (s);
161
162 /* Read interface's value. */
paul2e3b2e42002-12-13 21:03:13 +0000163 ifp->status = stream_getc (s);
paul718e3742002-12-13 20:15:29 +0000164 ifp->flags = stream_getl (s);
165 ifp->metric = stream_getl (s);
166 ifp->mtu = stream_getl (s);
167 ifp->bandwidth = stream_getl (s);
168}
169
170int
171ospf_interface_state_up (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000172 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000173{
174 struct interface *ifp;
175 struct interface if_tmp;
176 struct ospf_interface *oi;
177 struct route_node *rn;
paulcf795c52003-06-19 02:13:25 +0000178
paul718e3742002-12-13 20:15:29 +0000179 ifp = zebra_interface_if_lookup (zclient->ibuf);
180
181 if (ifp == NULL)
182 return 0;
183
184 /* Interface is already up. */
paul2e3b2e42002-12-13 21:03:13 +0000185 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000186 {
187 /* Temporarily keep ifp values. */
188 memcpy (&if_tmp, ifp, sizeof (struct interface));
189
190 zebra_interface_if_set_value (zclient->ibuf, ifp);
191
192 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
paulcf795c52003-06-19 02:13:25 +0000193 zlog_info ("Zebra: Interface[%s] state update.", ifp->name);
paul718e3742002-12-13 20:15:29 +0000194
195 if (if_tmp.bandwidth != ifp->bandwidth)
paulcf795c52003-06-19 02:13:25 +0000196 {
197 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
198 zlog_info ("Zebra: Interface[%s] bandwidth change %d -> %d.",
199 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
paul718e3742002-12-13 20:15:29 +0000200
paulcf795c52003-06-19 02:13:25 +0000201 ospf_if_recalculate_output_cost (ifp);
202 }
paul718e3742002-12-13 20:15:29 +0000203 return 0;
204 }
paulcf795c52003-06-19 02:13:25 +0000205
paul718e3742002-12-13 20:15:29 +0000206 zebra_interface_if_set_value (zclient->ibuf, ifp);
paulcf795c52003-06-19 02:13:25 +0000207
paul718e3742002-12-13 20:15:29 +0000208 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
209 zlog_info ("Zebra: Interface[%s] state change to up.", ifp->name);
paulcf795c52003-06-19 02:13:25 +0000210
211 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000212 {
paulcf795c52003-06-19 02:13:25 +0000213 if ((oi = rn->info) == NULL)
214 continue;
215
paul718e3742002-12-13 20:15:29 +0000216 ospf_if_up (oi);
217 }
paulcf795c52003-06-19 02:13:25 +0000218
paul718e3742002-12-13 20:15:29 +0000219 return 0;
220}
221
222int
223ospf_interface_state_down (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000224 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000225{
226 struct interface *ifp;
227 struct ospf_interface *oi;
228 struct route_node *node;
229
230 ifp = zebra_interface_state_read (zclient->ibuf);
231
232 if (ifp == NULL)
233 return 0;
234
235 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
236 zlog_info ("Zebra: Interface[%s] state change to down.", ifp->name);
237
paulcf795c52003-06-19 02:13:25 +0000238 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
paul718e3742002-12-13 20:15:29 +0000239 {
paulcf795c52003-06-19 02:13:25 +0000240 if ((oi = node->info) == NULL)
241 continue;
paul718e3742002-12-13 20:15:29 +0000242 ospf_if_down (oi);
243 }
244
245 return 0;
246}
247
248int
249ospf_interface_address_add (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000250 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000251{
paul020709f2003-04-04 02:44:16 +0000252 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000253 struct connected *c;
254
255 c = zebra_interface_address_add_read (zclient->ibuf);
256
257 if (c == NULL)
258 return 0;
259
paul020709f2003-04-04 02:44:16 +0000260 ospf = ospf_lookup ();
261 if (ospf != NULL)
262 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000263
264#ifdef HAVE_SNMP
265 ospf_snmp_if_update (c->ifp);
266#endif /* HAVE_SNMP */
267
268 return 0;
269}
270
271int
272ospf_interface_address_delete (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000273 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000274{
paul020709f2003-04-04 02:44:16 +0000275 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000276 struct connected *c;
277 struct interface *ifp;
278 struct ospf_interface *oi;
279 struct route_node *rn;
280 struct prefix p;
281
282 c = zebra_interface_address_delete_read (zclient->ibuf);
283
284 if (c == NULL)
285 return 0;
286
287 ifp = c->ifp;
288 p = *c->address;
289 p.prefixlen = IPV4_MAX_PREFIXLEN;
290
291 rn = route_node_lookup (IF_OIFS (ifp), &p);
paulcf795c52003-06-19 02:13:25 +0000292 if (!rn)
paul718e3742002-12-13 20:15:29 +0000293 return 0;
294
295 assert (rn->info);
296 oi = rn->info;
paulcf795c52003-06-19 02:13:25 +0000297
paul718e3742002-12-13 20:15:29 +0000298 /* Call interface hook functions to clean up */
299 ospf_if_free (oi);
paulcf795c52003-06-19 02:13:25 +0000300
paul718e3742002-12-13 20:15:29 +0000301#ifdef HAVE_SNMP
302 ospf_snmp_if_update (c->ifp);
303#endif /* HAVE_SNMP */
304
305 connected_free (c);
306
paul020709f2003-04-04 02:44:16 +0000307 ospf = ospf_lookup ();
308 if (ospf != NULL)
309 ospf_if_update (ospf);
paul718e3742002-12-13 20:15:29 +0000310
311 return 0;
312}
paul72357f22003-06-19 02:11:23 +0000313
paul718e3742002-12-13 20:15:29 +0000314void
315ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
316{
317 u_char message;
318 u_char distance;
319 u_char flags;
320 int psize;
321 struct stream *s;
322 struct ospf_path *path;
323 listnode node;
324
325 if (zclient->redist[ZEBRA_ROUTE_OSPF])
326 {
327 message = 0;
328 flags = 0;
329
330 /* OSPF pass nexthop and metric */
331 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
332 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
333
334 /* Distance value. */
335 distance = ospf_distance_apply (p, or);
336 if (distance)
paul72357f22003-06-19 02:11:23 +0000337 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
paul718e3742002-12-13 20:15:29 +0000338
339 /* Make packet. */
340 s = zclient->obuf;
341 stream_reset (s);
342
343 /* Length place holder. */
344 stream_putw (s, 0);
345
346 /* Put command, type, flags, message. */
347 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
348 stream_putc (s, ZEBRA_ROUTE_OSPF);
349 stream_putc (s, flags);
350 stream_putc (s, message);
paulcf795c52003-06-19 02:13:25 +0000351
paul718e3742002-12-13 20:15:29 +0000352 /* Put prefix information. */
353 psize = PSIZE (p->prefixlen);
354 stream_putc (s, p->prefixlen);
paulcf795c52003-06-19 02:13:25 +0000355 stream_write (s, (u_char *) & p->prefix, psize);
paul718e3742002-12-13 20:15:29 +0000356
357 /* Nexthop count. */
358 stream_putc (s, or->path->count);
359
360 /* Nexthop, ifindex, distance and metric information. */
361 for (node = listhead (or->path); node; nextnode (node))
paul72357f22003-06-19 02:11:23 +0000362 {
paulcf795c52003-06-19 02:13:25 +0000363 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000364
paulcf795c52003-06-19 02:13:25 +0000365 if (path->nexthop.s_addr != INADDR_ANY)
366 {
367 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
368 stream_put_in_addr (s, &path->nexthop);
369 }
370 else
371 {
372 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
373 if (path->oi)
374 stream_putl (s, path->oi->ifp->ifindex);
375 else
376 stream_putl (s, 0);
377 }
paul72357f22003-06-19 02:11:23 +0000378
379 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
380 {
381 zlog_info ("Zebra: Route add %s/%d nexthop %s",
paulcf795c52003-06-19 02:13:25 +0000382 inet_ntoa (p->prefix),
383 p->prefixlen, inet_ntoa (path->nexthop));
384 }
385 }
paul718e3742002-12-13 20:15:29 +0000386
387 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
paulcf795c52003-06-19 02:13:25 +0000388 stream_putc (s, distance);
paul718e3742002-12-13 20:15:29 +0000389 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
paulcf795c52003-06-19 02:13:25 +0000390 {
391 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
392 stream_putl (s, or->cost + or->u.ext.type2_cost);
393 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
394 stream_putl (s, or->u.ext.type2_cost);
395 else
396 stream_putl (s, or->cost);
397 }
paul718e3742002-12-13 20:15:29 +0000398
399 stream_putw_at (s, 0, stream_get_endp (s));
400
401 writen (zclient->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000402 }
403}
404
405void
406ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
407{
408 struct zapi_ipv4 api;
paul72357f22003-06-19 02:11:23 +0000409 struct ospf_path *path;
410 struct in_addr *nexthop;
411 listnode node;
paul718e3742002-12-13 20:15:29 +0000412
413 if (zclient->redist[ZEBRA_ROUTE_OSPF])
414 {
415 api.type = ZEBRA_ROUTE_OSPF;
416 api.flags = 0;
417 api.message = 0;
paul72357f22003-06-19 02:11:23 +0000418 api.ifindex_num = 0;
419 api.nexthop_num = 0;
420 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
paul718e3742002-12-13 20:15:29 +0000421
paul72357f22003-06-19 02:11:23 +0000422 for (node = listhead (or->path); node; nextnode (node))
paulcf795c52003-06-19 02:13:25 +0000423 {
424 path = getdata (node);
paul718e3742002-12-13 20:15:29 +0000425
paulcf795c52003-06-19 02:13:25 +0000426 if (path->nexthop.s_addr != INADDR_ANY)
427 {
428 api.nexthop_num = 1;
429 nexthop = &path->nexthop;
430 api.nexthop = &nexthop;
431 }
432 else
433 {
434 api.ifindex_num = 1;
435 api.ifindex = &path->oi->ifp->ifindex;
436 }
paul72357f22003-06-19 02:11:23 +0000437
438 zapi_ipv4_delete (zclient, p, &api);
439
440 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000441 {
442 zlog_info ("Zebra: Route delete %s/%d nexthop %s",
443 inet_ntoa (p->prefix),
444 p->prefixlen, inet_ntoa (**api.nexthop));
445 }
446 }
paul718e3742002-12-13 20:15:29 +0000447 }
448}
449
450void
451ospf_zebra_add_discard (struct prefix_ipv4 *p)
452{
453 struct zapi_ipv4 api;
454
455 if (zclient->redist[ZEBRA_ROUTE_OSPF])
456 {
457 api.type = ZEBRA_ROUTE_OSPF;
458 api.flags = ZEBRA_FLAG_BLACKHOLE;
459 api.message = 0;
460 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
461 api.nexthop_num = 0;
462 api.ifindex_num = 0;
463
464 zapi_ipv4_add (zclient, p, &api);
465 }
466}
467
468void
469ospf_zebra_delete_discard (struct prefix_ipv4 *p)
470{
471 struct zapi_ipv4 api;
472
473 if (zclient->redist[ZEBRA_ROUTE_OSPF])
474 {
475 api.type = ZEBRA_ROUTE_OSPF;
476 api.flags = ZEBRA_FLAG_BLACKHOLE;
477 api.message = 0;
478 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
479 api.nexthop_num = 0;
480 api.ifindex_num = 0;
481
482 zapi_ipv4_delete (zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000483
484 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000485 zlog_info ("Zebra: Route delete discard %s/%d",
486 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000487
paul718e3742002-12-13 20:15:29 +0000488 }
489}
490
491int
492ospf_is_type_redistributed (int type)
493{
494 return (DEFAULT_ROUTE_TYPE (type)) ?
495 zclient->default_information : zclient->redist[type];
496}
497
498int
paul020709f2003-04-04 02:44:16 +0000499ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000500{
501 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000502
paul718e3742002-12-13 20:15:29 +0000503 if (ospf_is_type_redistributed (type))
504 {
paul68980082003-03-25 05:07:42 +0000505 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000506 {
507 ospf->dmetric[type].type = mtype;
508 force = LSA_REFRESH_FORCE;
509 }
paul68980082003-03-25 05:07:42 +0000510 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000511 {
512 ospf->dmetric[type].value = mvalue;
513 force = LSA_REFRESH_FORCE;
514 }
515
paul68980082003-03-25 05:07:42 +0000516 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000517
paul718e3742002-12-13 20:15:29 +0000518 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000519 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
520 LOOKUP (ospf_redistributed_proto, type),
521 metric_type (ospf, type), metric_value (ospf, type));
522
paul718e3742002-12-13 20:15:29 +0000523 return CMD_SUCCESS;
524 }
525
paul68980082003-03-25 05:07:42 +0000526 ospf->dmetric[type].type = mtype;
527 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000528
529 zclient_redistribute_set (zclient, type);
530
531 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
532 zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000533 LOOKUP (ospf_redistributed_proto, type),
534 metric_type (ospf, type), metric_value (ospf, type));
535
paul68980082003-03-25 05:07:42 +0000536 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000537
538 return CMD_SUCCESS;
539}
540
541int
paul020709f2003-04-04 02:44:16 +0000542ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000543{
544 if (type == zclient->redist_default)
545 return CMD_SUCCESS;
546
paulcf795c52003-06-19 02:13:25 +0000547 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000548 return CMD_SUCCESS;
549
550 zclient_redistribute_unset (zclient, type);
paulcf795c52003-06-19 02:13:25 +0000551
paul718e3742002-12-13 20:15:29 +0000552 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
553 zlog_info ("Redistribute[%s]: Stop",
paulcf795c52003-06-19 02:13:25 +0000554 LOOKUP (ospf_redistributed_proto, type));
paul718e3742002-12-13 20:15:29 +0000555
paul68980082003-03-25 05:07:42 +0000556 ospf->dmetric[type].type = -1;
557 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000558
559 /* Remove the routes from OSPF table. */
560 ospf_redistribute_withdraw (type);
561
paul68980082003-03-25 05:07:42 +0000562 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000563
564 return CMD_SUCCESS;
565}
566
567int
paul020709f2003-04-04 02:44:16 +0000568ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000569 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000570{
571 int force = 0;
paul020709f2003-04-04 02:44:16 +0000572
paul718e3742002-12-13 20:15:29 +0000573 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
574 {
paul68980082003-03-25 05:07:42 +0000575 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000576 {
577 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
578 force = 1;
579 }
paul68980082003-03-25 05:07:42 +0000580 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000581 {
582 force = 1;
583 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
584 }
585
paul68980082003-03-25 05:07:42 +0000586 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000587
paul718e3742002-12-13 20:15:29 +0000588 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000589 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
590 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
591 metric_type (ospf, DEFAULT_ROUTE),
592 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000593 return CMD_SUCCESS;
594 }
595
paul68980082003-03-25 05:07:42 +0000596 ospf->default_originate = originate;
597 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
598 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000599
600 zclient_redistribute_default_set (zclient);
paulcf795c52003-06-19 02:13:25 +0000601
paul718e3742002-12-13 20:15:29 +0000602 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
603 zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000604 metric_type (ospf, DEFAULT_ROUTE),
605 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000606
paul68980082003-03-25 05:07:42 +0000607 if (ospf->router_id.s_addr == 0)
608 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000609 else
610 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000611 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000612
paul68980082003-03-25 05:07:42 +0000613 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000614
615 return CMD_SUCCESS;
616}
617
618int
paul020709f2003-04-04 02:44:16 +0000619ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000620{
621 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
622 return CMD_SUCCESS;
623
paul68980082003-03-25 05:07:42 +0000624 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
625 ospf->dmetric[DEFAULT_ROUTE].type = -1;
626 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000627
628 zclient_redistribute_default_unset (zclient);
629
630 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
631 zlog_info ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000632
paul68980082003-03-25 05:07:42 +0000633 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000634
635 return CMD_SUCCESS;
636}
637
638int
paul020709f2003-04-04 02:44:16 +0000639ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000640 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000641{
642 /* If prefix is multicast, then do not originate LSA. */
643 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
644 {
645 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000646 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000647 return 0;
648 }
649
650 /* Take care of default-originate. */
651 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000652 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000653 {
paulcf795c52003-06-19 02:13:25 +0000654 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
655 "for default");
656 return 0;
paul718e3742002-12-13 20:15:29 +0000657 }
658
659 return 1;
660}
661
662/* If connected prefix is OSPF enable interface, then do not announce. */
663int
paulcf795c52003-06-19 02:13:25 +0000664ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000665{
666 struct route_node *rn;
667
paul68980082003-03-25 05:07:42 +0000668 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000669 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000670 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
671 {
672 route_unlock_node (rn);
673 return 0;
674 }
paul718e3742002-12-13 20:15:29 +0000675
676 return 1;
677}
678
679/* return 1 if external LSA must be originated, 0 otherwise */
680int
paul68980082003-03-25 05:07:42 +0000681ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000682 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000683{
684 struct route_map_set_values save_values;
685 struct prefix_ipv4 *p = &ei->p;
686 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000687
paul718e3742002-12-13 20:15:29 +0000688 if (changed)
689 *changed = 0;
690
paul020709f2003-04-04 02:44:16 +0000691 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000692 return 0;
693
694 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000695 if (type == ZEBRA_ROUTE_CONNECT &&
696 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000697 return 0;
698
paul020709f2003-04-04 02:44:16 +0000699 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000700 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000701 if (DISTRIBUTE_LIST (ospf, type))
702 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000703 {
704 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
705 zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
706 LOOKUP (ospf_redistributed_proto, type),
707 inet_ntoa (p->prefix), p->prefixlen);
708 return 0;
709 }
paul718e3742002-12-13 20:15:29 +0000710
711 save_values = ei->route_map_set;
712 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000713
paul718e3742002-12-13 20:15:29 +0000714 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000715 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000716 {
717 int ret;
718
paulcf795c52003-06-19 02:13:25 +0000719 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
720 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000721
722 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000723 {
724 ei->route_map_set = save_values;
725 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
726 zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.",
727 LOOKUP (ospf_redistributed_proto, type),
728 inet_ntoa (p->prefix), p->prefixlen);
729 return 0;
730 }
731
paul718e3742002-12-13 20:15:29 +0000732 /* check if 'route-map set' changed something */
733 if (changed)
paulcf795c52003-06-19 02:13:25 +0000734 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
735 &save_values);
paul718e3742002-12-13 20:15:29 +0000736 }
737
738 return 1;
739}
740
741/* OSPF route-map set for redistribution */
742void
paul020709f2003-04-04 02:44:16 +0000743ospf_routemap_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000744{
paul020709f2003-04-04 02:44:16 +0000745 if (ROUTEMAP_NAME (ospf, type))
746 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000747
paul020709f2003-04-04 02:44:16 +0000748 ROUTEMAP_NAME (ospf, type) = strdup (name);
749 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000750}
751
752void
paul020709f2003-04-04 02:44:16 +0000753ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000754{
paul020709f2003-04-04 02:44:16 +0000755 if (ROUTEMAP_NAME (ospf, type))
756 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000757
paul020709f2003-04-04 02:44:16 +0000758 ROUTEMAP_NAME (ospf, type) = NULL;
759 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000760}
761
762/* Zebra route add and delete treatment. */
763int
764ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000765 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000766{
767 struct stream *s;
768 struct zapi_ipv4 api;
769 unsigned long ifindex;
770 struct in_addr nexthop;
771 struct prefix_ipv4 p;
772 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000773 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000774
775 s = zclient->ibuf;
776 ifindex = 0;
777 nexthop.s_addr = 0;
778
779 /* Type, flags, message. */
780 api.type = stream_getc (s);
781 api.flags = stream_getc (s);
782 api.message = stream_getc (s);
783
784 /* IPv4 prefix. */
785 memset (&p, 0, sizeof (struct prefix_ipv4));
786 p.family = AF_INET;
787 p.prefixlen = stream_getc (s);
788 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
789
790 /* Nexthop, ifindex, distance, metric. */
791 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
792 {
793 api.nexthop_num = stream_getc (s);
794 nexthop.s_addr = stream_get_ipv4 (s);
795 }
796 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
797 {
798 api.ifindex_num = stream_getc (s);
799 ifindex = stream_getl (s);
800 }
801 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
802 api.distance = stream_getc (s);
803 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
804 api.metric = stream_getl (s);
805
paul020709f2003-04-04 02:44:16 +0000806 ospf = ospf_lookup ();
807 if (ospf == NULL)
808 return 0;
809
paul718e3742002-12-13 20:15:29 +0000810 if (command == ZEBRA_IPV4_ROUTE_ADD)
811 {
812 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
813
paul68980082003-03-25 05:07:42 +0000814 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000815 /* Set flags to generate AS-external-LSA originate event
816 for each redistributed protocols later. */
817 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000818 else
paulcf795c52003-06-19 02:13:25 +0000819 {
820 if (ei)
821 {
822 if (is_prefix_default (&p))
823 ospf_external_lsa_refresh_default (ospf);
824 else
825 {
826 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000827
paulcf795c52003-06-19 02:13:25 +0000828 current = ospf_external_info_find_lsa (ospf, &ei->p);
829 if (!current)
830 ospf_external_lsa_originate (ospf, ei);
831 else if (IS_LSA_MAXAGE (current))
832 ospf_external_lsa_refresh (ospf, current,
833 ei, LSA_REFRESH_FORCE);
834 else
835 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
836 inet_ntoa (p.prefix));
837 }
838 }
839 }
paul718e3742002-12-13 20:15:29 +0000840 }
paulcf795c52003-06-19 02:13:25 +0000841 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000842 {
843 ospf_external_info_delete (api.type, p);
paulcf795c52003-06-19 02:13:25 +0000844 if (!is_prefix_default (&p))
845 ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop);
paul718e3742002-12-13 20:15:29 +0000846 else
paulcf795c52003-06-19 02:13:25 +0000847 ospf_external_lsa_refresh_default (ospf);
paul718e3742002-12-13 20:15:29 +0000848 }
849
850 return 0;
851}
paul718e3742002-12-13 20:15:29 +0000852
paulcf795c52003-06-19 02:13:25 +0000853
paul718e3742002-12-13 20:15:29 +0000854int
paul68980082003-03-25 05:07:42 +0000855ospf_distribute_list_out_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000856{
857 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000858 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000859
860 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000861 if (DISTRIBUTE_NAME (ospf, type))
862 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000863
864 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000865 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000866
867 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000868 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000869 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000870
871 return CMD_SUCCESS;
872}
873
874int
paul68980082003-03-25 05:07:42 +0000875ospf_distribute_list_out_unset (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000876{
877 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000878 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000879 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000880
881 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000882 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000883
884 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000885 if (DISTRIBUTE_NAME (ospf, type))
886 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000887
paul020709f2003-04-04 02:44:16 +0000888 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000889
890 return CMD_SUCCESS;
891}
892
893/* distribute-list update timer. */
894int
895ospf_distribute_list_update_timer (struct thread *thread)
896{
897 struct route_node *rn;
898 struct external_info *ei;
899 struct route_table *rt;
900 struct ospf_lsa *lsa;
901 u_char type;
paul020709f2003-04-04 02:44:16 +0000902 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000903
904 type = (int) THREAD_ARG (thread);
905 rt = EXTERNAL_INFO (type);
906
paul020709f2003-04-04 02:44:16 +0000907 ospf = ospf_lookup ();
908 if (ospf == NULL)
909 return 0;
910
paul68980082003-03-25 05:07:42 +0000911 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000912
913 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
914
915 /* foreach all external info. */
916 if (rt)
917 for (rn = route_top (rt); rn; rn = route_next (rn))
918 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000919 {
920 if (is_prefix_default (&ei->p))
921 ospf_external_lsa_refresh_default (ospf);
922 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
923 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
924 else
925 ospf_external_lsa_originate (ospf, ei);
926 }
paul718e3742002-12-13 20:15:29 +0000927 return 0;
928}
929
930#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
931
932/* Update distribute-list and set timer to apply access-list. */
933void
paul68980082003-03-25 05:07:42 +0000934ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000935{
936 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000937
paul718e3742002-12-13 20:15:29 +0000938 /* External info does not exist. */
939 if (!(rt = EXTERNAL_INFO (type)))
940 return;
941
942 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000943 if (ospf->t_distribute_update)
944 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000945
946 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000947 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000948 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000949 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000950}
951
952/* If access-list is updated, apply some check. */
953void
954ospf_filter_update (struct access_list *access)
955{
paul020709f2003-04-04 02:44:16 +0000956 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000957 int type;
958 int abr_inv = 0;
959 struct ospf_area *area;
960 listnode node;
961
962 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000963 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000964 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000965 return;
966
paul718e3742002-12-13 20:15:29 +0000967 /* Update distribute-list, and apply filter. */
968 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
969 {
paul020709f2003-04-04 02:44:16 +0000970 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +0000971 {
972 /* if route-map is not NULL it may be using this access list */
973 ospf_distribute_list_update (ospf, type);
974 continue;
975 }
976
paul718e3742002-12-13 20:15:29 +0000977
paul020709f2003-04-04 02:44:16 +0000978 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +0000979 {
980 /* Keep old access-list for distribute-list. */
981 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
982
983 /* Update access-list for distribute-list. */
984 DISTRIBUTE_LIST (ospf, type) =
985 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
986
987 /* No update for this distribute type. */
988 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
989 continue;
990
991 /* Schedule distribute-list update timer. */
992 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
993 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
994 ospf_distribute_list_update (ospf, type);
995 }
paul718e3742002-12-13 20:15:29 +0000996 }
997
998 /* Update Area access-list. */
paul68980082003-03-25 05:07:42 +0000999 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001000 if ((area = getdata (node)) != NULL)
1001 {
paulcf795c52003-06-19 02:13:25 +00001002 if (EXPORT_NAME (area))
1003 {
1004 EXPORT_LIST (area) = NULL;
1005 abr_inv++;
1006 }
paul718e3742002-12-13 20:15:29 +00001007
paulcf795c52003-06-19 02:13:25 +00001008 if (IMPORT_NAME (area))
1009 {
1010 IMPORT_LIST (area) = NULL;
1011 abr_inv++;
1012 }
paul718e3742002-12-13 20:15:29 +00001013 }
1014
1015 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001016 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001017 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001018}
paul718e3742002-12-13 20:15:29 +00001019
paulcf795c52003-06-19 02:13:25 +00001020
paul718e3742002-12-13 20:15:29 +00001021struct ospf_distance *
1022ospf_distance_new ()
1023{
1024 struct ospf_distance *new;
1025 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1026 memset (new, 0, sizeof (struct ospf_distance));
1027 return new;
1028}
1029
1030void
1031ospf_distance_free (struct ospf_distance *odistance)
1032{
1033 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1034}
1035
1036int
paul020709f2003-04-04 02:44:16 +00001037ospf_distance_set (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001038 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001039{
1040 int ret;
1041 struct prefix_ipv4 p;
1042 u_char distance;
1043 struct route_node *rn;
1044 struct ospf_distance *odistance;
1045
1046 ret = str2prefix_ipv4 (ip_str, &p);
1047 if (ret == 0)
1048 {
1049 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1050 return CMD_WARNING;
1051 }
1052
1053 distance = atoi (distance_str);
1054
1055 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001056 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001057 if (rn->info)
1058 {
1059 odistance = rn->info;
1060 route_unlock_node (rn);
1061 }
1062 else
1063 {
1064 odistance = ospf_distance_new ();
1065 rn->info = odistance;
1066 }
1067
1068 /* Set distance value. */
1069 odistance->distance = distance;
1070
1071 /* Reset access-list configuration. */
1072 if (odistance->access_list)
1073 {
1074 free (odistance->access_list);
1075 odistance->access_list = NULL;
1076 }
1077 if (access_list_str)
1078 odistance->access_list = strdup (access_list_str);
1079
1080 return CMD_SUCCESS;
1081}
1082
1083int
paul020709f2003-04-04 02:44:16 +00001084ospf_distance_unset (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001085 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001086{
1087 int ret;
1088 struct prefix_ipv4 p;
1089 u_char distance;
1090 struct route_node *rn;
1091 struct ospf_distance *odistance;
1092
1093 ret = str2prefix_ipv4 (ip_str, &p);
1094 if (ret == 0)
1095 {
1096 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1097 return CMD_WARNING;
1098 }
1099
1100 distance = atoi (distance_str);
1101
paulcf795c52003-06-19 02:13:25 +00001102 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1103 if (!rn)
paul718e3742002-12-13 20:15:29 +00001104 {
1105 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1106 return CMD_WARNING;
1107 }
1108
1109 odistance = rn->info;
1110
1111 if (odistance->access_list)
1112 free (odistance->access_list);
1113 ospf_distance_free (odistance);
1114
1115 rn->info = NULL;
1116 route_unlock_node (rn);
1117 route_unlock_node (rn);
1118
1119 return CMD_SUCCESS;
1120}
1121
1122void
paul68980082003-03-25 05:07:42 +00001123ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001124{
1125 struct route_node *rn;
1126 struct ospf_distance *odistance;
1127
paul68980082003-03-25 05:07:42 +00001128 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001129 if ((odistance = rn->info) != NULL)
1130 {
paulcf795c52003-06-19 02:13:25 +00001131 if (odistance->access_list)
1132 free (odistance->access_list);
1133 ospf_distance_free (odistance);
1134 rn->info = NULL;
1135 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001136 }
1137}
1138
1139u_char
1140ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1141{
paul020709f2003-04-04 02:44:16 +00001142 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001143
paul020709f2003-04-04 02:44:16 +00001144 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001145 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001146 return 0;
1147
paul68980082003-03-25 05:07:42 +00001148 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001149 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001150 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001151
paul68980082003-03-25 05:07:42 +00001152 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001153 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001154 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001155
paul68980082003-03-25 05:07:42 +00001156 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001157 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001158 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001159 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001160
paul68980082003-03-25 05:07:42 +00001161 if (ospf->distance_all)
1162 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001163
1164 return 0;
1165}
1166
1167void
1168ospf_zebra_init ()
1169{
1170 /* Allocate zebra structure. */
1171 zclient = zclient_new ();
1172 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
1173 zclient->interface_add = ospf_interface_add;
1174 zclient->interface_delete = ospf_interface_delete;
1175 zclient->interface_up = ospf_interface_state_up;
1176 zclient->interface_down = ospf_interface_state_down;
1177 zclient->interface_address_add = ospf_interface_address_add;
1178 zclient->interface_address_delete = ospf_interface_address_delete;
1179 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1180 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1181
1182 access_list_add_hook (ospf_filter_update);
1183 access_list_delete_hook (ospf_filter_update);
1184}