blob: 3594b17cd587788690dcc59a9ffeaf08eb3bb963 [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. */
paul96735ee2003-08-10 02:51:22 +0000358 stream_putc (s, or->paths->count);
paul718e3742002-12-13 20:15:29 +0000359
360 /* Nexthop, ifindex, distance and metric information. */
paul96735ee2003-08-10 02:51:22 +0000361 for (node = listhead (or->paths); 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
paul96735ee2003-08-10 02:51:22 +0000422 for (node = listhead (or->paths); 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 {
hasso622149a2003-08-07 09:29:13 +0000434 /* Commented out by Hasso because it introduces segfault.
435 * See bug #29 in bugzilla for details. */
436 /* api.ifindex_num = 1;
437 api.ifindex = &path->oi->ifp->ifindex; */
paulcf795c52003-06-19 02:13:25 +0000438 }
paul72357f22003-06-19 02:11:23 +0000439
440 zapi_ipv4_delete (zclient, p, &api);
441
442 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num)
paulcf795c52003-06-19 02:13:25 +0000443 {
444 zlog_info ("Zebra: Route delete %s/%d nexthop %s",
445 inet_ntoa (p->prefix),
446 p->prefixlen, inet_ntoa (**api.nexthop));
447 }
448 }
paul718e3742002-12-13 20:15:29 +0000449 }
450}
451
452void
453ospf_zebra_add_discard (struct prefix_ipv4 *p)
454{
455 struct zapi_ipv4 api;
456
457 if (zclient->redist[ZEBRA_ROUTE_OSPF])
458 {
459 api.type = ZEBRA_ROUTE_OSPF;
460 api.flags = ZEBRA_FLAG_BLACKHOLE;
461 api.message = 0;
462 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
463 api.nexthop_num = 0;
464 api.ifindex_num = 0;
465
466 zapi_ipv4_add (zclient, p, &api);
467 }
468}
469
470void
471ospf_zebra_delete_discard (struct prefix_ipv4 *p)
472{
473 struct zapi_ipv4 api;
474
475 if (zclient->redist[ZEBRA_ROUTE_OSPF])
476 {
477 api.type = ZEBRA_ROUTE_OSPF;
478 api.flags = ZEBRA_FLAG_BLACKHOLE;
479 api.message = 0;
480 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
481 api.nexthop_num = 0;
482 api.ifindex_num = 0;
483
484 zapi_ipv4_delete (zclient, p, &api);
paul72357f22003-06-19 02:11:23 +0000485
486 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000487 zlog_info ("Zebra: Route delete discard %s/%d",
488 inet_ntoa (p->prefix), p->prefixlen);
paul72357f22003-06-19 02:11:23 +0000489
paul718e3742002-12-13 20:15:29 +0000490 }
491}
492
493int
494ospf_is_type_redistributed (int type)
495{
496 return (DEFAULT_ROUTE_TYPE (type)) ?
497 zclient->default_information : zclient->redist[type];
498}
499
500int
paul020709f2003-04-04 02:44:16 +0000501ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000502{
503 int force = 0;
paulcf795c52003-06-19 02:13:25 +0000504
paul718e3742002-12-13 20:15:29 +0000505 if (ospf_is_type_redistributed (type))
506 {
paul68980082003-03-25 05:07:42 +0000507 if (mtype != ospf->dmetric[type].type)
paulcf795c52003-06-19 02:13:25 +0000508 {
509 ospf->dmetric[type].type = mtype;
510 force = LSA_REFRESH_FORCE;
511 }
paul68980082003-03-25 05:07:42 +0000512 if (mvalue != ospf->dmetric[type].value)
paulcf795c52003-06-19 02:13:25 +0000513 {
514 ospf->dmetric[type].value = mvalue;
515 force = LSA_REFRESH_FORCE;
516 }
517
paul68980082003-03-25 05:07:42 +0000518 ospf_external_lsa_refresh_type (ospf, type, force);
paulcf795c52003-06-19 02:13:25 +0000519
paul718e3742002-12-13 20:15:29 +0000520 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000521 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
522 LOOKUP (ospf_redistributed_proto, type),
523 metric_type (ospf, type), metric_value (ospf, type));
524
paul718e3742002-12-13 20:15:29 +0000525 return CMD_SUCCESS;
526 }
527
paul68980082003-03-25 05:07:42 +0000528 ospf->dmetric[type].type = mtype;
529 ospf->dmetric[type].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000530
531 zclient_redistribute_set (zclient, type);
532
533 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
534 zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000535 LOOKUP (ospf_redistributed_proto, type),
536 metric_type (ospf, type), metric_value (ospf, type));
537
paul68980082003-03-25 05:07:42 +0000538 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000539
540 return CMD_SUCCESS;
541}
542
543int
paul020709f2003-04-04 02:44:16 +0000544ospf_redistribute_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000545{
546 if (type == zclient->redist_default)
547 return CMD_SUCCESS;
548
paulcf795c52003-06-19 02:13:25 +0000549 if (!ospf_is_type_redistributed (type))
paul718e3742002-12-13 20:15:29 +0000550 return CMD_SUCCESS;
551
552 zclient_redistribute_unset (zclient, type);
paulcf795c52003-06-19 02:13:25 +0000553
paul718e3742002-12-13 20:15:29 +0000554 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
555 zlog_info ("Redistribute[%s]: Stop",
paulcf795c52003-06-19 02:13:25 +0000556 LOOKUP (ospf_redistributed_proto, type));
paul718e3742002-12-13 20:15:29 +0000557
paul68980082003-03-25 05:07:42 +0000558 ospf->dmetric[type].type = -1;
559 ospf->dmetric[type].value = -1;
paul718e3742002-12-13 20:15:29 +0000560
561 /* Remove the routes from OSPF table. */
562 ospf_redistribute_withdraw (type);
563
paul68980082003-03-25 05:07:42 +0000564 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000565
566 return CMD_SUCCESS;
567}
568
569int
paul020709f2003-04-04 02:44:16 +0000570ospf_redistribute_default_set (struct ospf *ospf, int originate,
paulcf795c52003-06-19 02:13:25 +0000571 int mtype, int mvalue)
paul718e3742002-12-13 20:15:29 +0000572{
573 int force = 0;
paul020709f2003-04-04 02:44:16 +0000574
paul718e3742002-12-13 20:15:29 +0000575 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
576 {
paul68980082003-03-25 05:07:42 +0000577 if (mtype != ospf->dmetric[DEFAULT_ROUTE].type)
paulcf795c52003-06-19 02:13:25 +0000578 {
579 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
580 force = 1;
581 }
paul68980082003-03-25 05:07:42 +0000582 if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value)
paulcf795c52003-06-19 02:13:25 +0000583 {
584 force = 1;
585 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
586 }
587
paul68980082003-03-25 05:07:42 +0000588 ospf_external_lsa_refresh_default (ospf);
paulcf795c52003-06-19 02:13:25 +0000589
paul718e3742002-12-13 20:15:29 +0000590 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
paulcf795c52003-06-19 02:13:25 +0000591 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
592 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
593 metric_type (ospf, DEFAULT_ROUTE),
594 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000595 return CMD_SUCCESS;
596 }
597
paul68980082003-03-25 05:07:42 +0000598 ospf->default_originate = originate;
599 ospf->dmetric[DEFAULT_ROUTE].type = mtype;
600 ospf->dmetric[DEFAULT_ROUTE].value = mvalue;
paul718e3742002-12-13 20:15:29 +0000601
602 zclient_redistribute_default_set (zclient);
paulcf795c52003-06-19 02:13:25 +0000603
paul718e3742002-12-13 20:15:29 +0000604 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
605 zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
paulcf795c52003-06-19 02:13:25 +0000606 metric_type (ospf, DEFAULT_ROUTE),
607 metric_value (ospf, DEFAULT_ROUTE));
paul718e3742002-12-13 20:15:29 +0000608
paul68980082003-03-25 05:07:42 +0000609 if (ospf->router_id.s_addr == 0)
610 ospf->external_origin |= (1 << DEFAULT_ROUTE);
paul718e3742002-12-13 20:15:29 +0000611 else
612 thread_add_timer (master, ospf_default_originate_timer,
paulcf795c52003-06-19 02:13:25 +0000613 &ospf->default_originate, 1);
paul718e3742002-12-13 20:15:29 +0000614
paul68980082003-03-25 05:07:42 +0000615 ospf_asbr_status_update (ospf, ++ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000616
617 return CMD_SUCCESS;
618}
619
620int
paul020709f2003-04-04 02:44:16 +0000621ospf_redistribute_default_unset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +0000622{
623 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
624 return CMD_SUCCESS;
625
paul68980082003-03-25 05:07:42 +0000626 ospf->default_originate = DEFAULT_ORIGINATE_NONE;
627 ospf->dmetric[DEFAULT_ROUTE].type = -1;
628 ospf->dmetric[DEFAULT_ROUTE].value = -1;
paul718e3742002-12-13 20:15:29 +0000629
630 zclient_redistribute_default_unset (zclient);
631
632 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
633 zlog_info ("Redistribute[DEFAULT]: Stop");
paulcf795c52003-06-19 02:13:25 +0000634
paul68980082003-03-25 05:07:42 +0000635 ospf_asbr_status_update (ospf, --ospf->redistribute);
paul718e3742002-12-13 20:15:29 +0000636
637 return CMD_SUCCESS;
638}
639
640int
paul020709f2003-04-04 02:44:16 +0000641ospf_external_lsa_originate_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000642 struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000643{
644 /* If prefix is multicast, then do not originate LSA. */
645 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
646 {
647 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
paulcf795c52003-06-19 02:13:25 +0000648 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
paul718e3742002-12-13 20:15:29 +0000649 return 0;
650 }
651
652 /* Take care of default-originate. */
653 if (is_prefix_default (&ei->p))
paul68980082003-03-25 05:07:42 +0000654 if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
paul718e3742002-12-13 20:15:29 +0000655 {
paulcf795c52003-06-19 02:13:25 +0000656 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
657 "for default");
658 return 0;
paul718e3742002-12-13 20:15:29 +0000659 }
660
661 return 1;
662}
663
664/* If connected prefix is OSPF enable interface, then do not announce. */
665int
paulcf795c52003-06-19 02:13:25 +0000666ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei)
paul718e3742002-12-13 20:15:29 +0000667{
668 struct route_node *rn;
669
paul68980082003-03-25 05:07:42 +0000670 for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000671 if (rn->info != NULL)
paulcf795c52003-06-19 02:13:25 +0000672 if (prefix_match (&rn->p, (struct prefix *) &ei->p))
673 {
674 route_unlock_node (rn);
675 return 0;
676 }
paul718e3742002-12-13 20:15:29 +0000677
678 return 1;
679}
680
681/* return 1 if external LSA must be originated, 0 otherwise */
682int
paul68980082003-03-25 05:07:42 +0000683ospf_redistribute_check (struct ospf *ospf,
paulcf795c52003-06-19 02:13:25 +0000684 struct external_info *ei, int *changed)
paul718e3742002-12-13 20:15:29 +0000685{
686 struct route_map_set_values save_values;
687 struct prefix_ipv4 *p = &ei->p;
688 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
paulcf795c52003-06-19 02:13:25 +0000689
paul718e3742002-12-13 20:15:29 +0000690 if (changed)
691 *changed = 0;
692
paul020709f2003-04-04 02:44:16 +0000693 if (!ospf_external_lsa_originate_check (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000694 return 0;
695
696 /* Take care connected route. */
paul68980082003-03-25 05:07:42 +0000697 if (type == ZEBRA_ROUTE_CONNECT &&
698 !ospf_distribute_check_connected (ospf, ei))
paul718e3742002-12-13 20:15:29 +0000699 return 0;
700
paul020709f2003-04-04 02:44:16 +0000701 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000702 /* distirbute-list exists, but access-list may not? */
paul020709f2003-04-04 02:44:16 +0000703 if (DISTRIBUTE_LIST (ospf, type))
704 if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY)
paulcf795c52003-06-19 02:13:25 +0000705 {
706 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
707 zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
708 LOOKUP (ospf_redistributed_proto, type),
709 inet_ntoa (p->prefix), p->prefixlen);
710 return 0;
711 }
paul718e3742002-12-13 20:15:29 +0000712
713 save_values = ei->route_map_set;
714 ospf_reset_route_map_set_values (&ei->route_map_set);
paulcf795c52003-06-19 02:13:25 +0000715
paul718e3742002-12-13 20:15:29 +0000716 /* apply route-map if needed */
paul020709f2003-04-04 02:44:16 +0000717 if (ROUTEMAP_NAME (ospf, type))
paul718e3742002-12-13 20:15:29 +0000718 {
719 int ret;
720
paulcf795c52003-06-19 02:13:25 +0000721 ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p,
722 RMAP_OSPF, ei);
paul718e3742002-12-13 20:15:29 +0000723
724 if (ret == RMAP_DENYMATCH)
paulcf795c52003-06-19 02:13:25 +0000725 {
726 ei->route_map_set = save_values;
727 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
728 zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.",
729 LOOKUP (ospf_redistributed_proto, type),
730 inet_ntoa (p->prefix), p->prefixlen);
731 return 0;
732 }
733
paul718e3742002-12-13 20:15:29 +0000734 /* check if 'route-map set' changed something */
735 if (changed)
paulcf795c52003-06-19 02:13:25 +0000736 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
737 &save_values);
paul718e3742002-12-13 20:15:29 +0000738 }
739
740 return 1;
741}
742
743/* OSPF route-map set for redistribution */
744void
paul020709f2003-04-04 02:44:16 +0000745ospf_routemap_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000746{
paul020709f2003-04-04 02:44:16 +0000747 if (ROUTEMAP_NAME (ospf, type))
748 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000749
paul020709f2003-04-04 02:44:16 +0000750 ROUTEMAP_NAME (ospf, type) = strdup (name);
751 ROUTEMAP (ospf, type) = route_map_lookup_by_name (name);
paul718e3742002-12-13 20:15:29 +0000752}
753
754void
paul020709f2003-04-04 02:44:16 +0000755ospf_routemap_unset (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000756{
paul020709f2003-04-04 02:44:16 +0000757 if (ROUTEMAP_NAME (ospf, type))
758 free (ROUTEMAP_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000759
paul020709f2003-04-04 02:44:16 +0000760 ROUTEMAP_NAME (ospf, type) = NULL;
761 ROUTEMAP (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000762}
763
764/* Zebra route add and delete treatment. */
765int
766ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
paulcf795c52003-06-19 02:13:25 +0000767 zebra_size_t length)
paul718e3742002-12-13 20:15:29 +0000768{
769 struct stream *s;
770 struct zapi_ipv4 api;
771 unsigned long ifindex;
772 struct in_addr nexthop;
773 struct prefix_ipv4 p;
774 struct external_info *ei;
paul020709f2003-04-04 02:44:16 +0000775 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000776
777 s = zclient->ibuf;
778 ifindex = 0;
779 nexthop.s_addr = 0;
780
781 /* Type, flags, message. */
782 api.type = stream_getc (s);
783 api.flags = stream_getc (s);
784 api.message = stream_getc (s);
785
786 /* IPv4 prefix. */
787 memset (&p, 0, sizeof (struct prefix_ipv4));
788 p.family = AF_INET;
789 p.prefixlen = stream_getc (s);
790 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
791
792 /* Nexthop, ifindex, distance, metric. */
793 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
794 {
795 api.nexthop_num = stream_getc (s);
796 nexthop.s_addr = stream_get_ipv4 (s);
797 }
798 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
799 {
800 api.ifindex_num = stream_getc (s);
801 ifindex = stream_getl (s);
802 }
803 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
804 api.distance = stream_getc (s);
805 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
806 api.metric = stream_getl (s);
807
paul020709f2003-04-04 02:44:16 +0000808 ospf = ospf_lookup ();
809 if (ospf == NULL)
810 return 0;
811
paul718e3742002-12-13 20:15:29 +0000812 if (command == ZEBRA_IPV4_ROUTE_ADD)
813 {
paul7021c422003-07-15 12:52:22 +0000814 /* XXX|HACK|TODO|FIXME:
815 * ignore reject/blackhole routes
816 * need a better generalised solution for these types
817 * really.
818 */
819 if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE)
820 || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT))
821 return 0;
822
paul718e3742002-12-13 20:15:29 +0000823 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
824
paul68980082003-03-25 05:07:42 +0000825 if (ospf->router_id.s_addr == 0)
paulcf795c52003-06-19 02:13:25 +0000826 /* Set flags to generate AS-external-LSA originate event
827 for each redistributed protocols later. */
828 ospf->external_origin |= (1 << api.type);
paul718e3742002-12-13 20:15:29 +0000829 else
paulcf795c52003-06-19 02:13:25 +0000830 {
831 if (ei)
832 {
833 if (is_prefix_default (&p))
834 ospf_external_lsa_refresh_default (ospf);
835 else
836 {
837 struct ospf_lsa *current;
paul718e3742002-12-13 20:15:29 +0000838
paulcf795c52003-06-19 02:13:25 +0000839 current = ospf_external_info_find_lsa (ospf, &ei->p);
840 if (!current)
841 ospf_external_lsa_originate (ospf, ei);
842 else if (IS_LSA_MAXAGE (current))
843 ospf_external_lsa_refresh (ospf, current,
844 ei, LSA_REFRESH_FORCE);
845 else
846 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
847 inet_ntoa (p.prefix));
848 }
849 }
850 }
paul718e3742002-12-13 20:15:29 +0000851 }
paulcf795c52003-06-19 02:13:25 +0000852 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
paul718e3742002-12-13 20:15:29 +0000853 {
854 ospf_external_info_delete (api.type, p);
paul7021c422003-07-15 12:52:22 +0000855 if (is_prefix_default (&p))
paulcf795c52003-06-19 02:13:25 +0000856 ospf_external_lsa_refresh_default (ospf);
paul7021c422003-07-15 12:52:22 +0000857 else
858 ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop);
paul718e3742002-12-13 20:15:29 +0000859 }
860
861 return 0;
862}
paul718e3742002-12-13 20:15:29 +0000863
paulcf795c52003-06-19 02:13:25 +0000864
paul718e3742002-12-13 20:15:29 +0000865int
paul68980082003-03-25 05:07:42 +0000866ospf_distribute_list_out_set (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000867{
868 /* Lookup access-list for distribute-list. */
paul020709f2003-04-04 02:44:16 +0000869 DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name);
paul718e3742002-12-13 20:15:29 +0000870
871 /* Clear previous distribute-name. */
paul020709f2003-04-04 02:44:16 +0000872 if (DISTRIBUTE_NAME (ospf, type))
873 free (DISTRIBUTE_NAME (ospf, type));
paul718e3742002-12-13 20:15:29 +0000874
875 /* Set distribute-name. */
paul020709f2003-04-04 02:44:16 +0000876 DISTRIBUTE_NAME (ospf, type) = strdup (name);
paul718e3742002-12-13 20:15:29 +0000877
878 /* If access-list have been set, schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000879 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000880 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000881
882 return CMD_SUCCESS;
883}
884
885int
paul68980082003-03-25 05:07:42 +0000886ospf_distribute_list_out_unset (struct ospf *ospf, int type, char *name)
paul718e3742002-12-13 20:15:29 +0000887{
888 /* Schedule update timer. */
paul020709f2003-04-04 02:44:16 +0000889 if (DISTRIBUTE_LIST (ospf, type))
paul68980082003-03-25 05:07:42 +0000890 ospf_distribute_list_update (ospf, type);
paul718e3742002-12-13 20:15:29 +0000891
892 /* Unset distribute-list. */
paul020709f2003-04-04 02:44:16 +0000893 DISTRIBUTE_LIST (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000894
895 /* Clear distribute-name. */
paul020709f2003-04-04 02:44:16 +0000896 if (DISTRIBUTE_NAME (ospf, type))
897 free (DISTRIBUTE_NAME (ospf, type));
paulcf795c52003-06-19 02:13:25 +0000898
paul020709f2003-04-04 02:44:16 +0000899 DISTRIBUTE_NAME (ospf, type) = NULL;
paul718e3742002-12-13 20:15:29 +0000900
901 return CMD_SUCCESS;
902}
903
904/* distribute-list update timer. */
905int
906ospf_distribute_list_update_timer (struct thread *thread)
907{
908 struct route_node *rn;
909 struct external_info *ei;
910 struct route_table *rt;
911 struct ospf_lsa *lsa;
912 u_char type;
paul020709f2003-04-04 02:44:16 +0000913 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000914
915 type = (int) THREAD_ARG (thread);
916 rt = EXTERNAL_INFO (type);
917
paul020709f2003-04-04 02:44:16 +0000918 ospf = ospf_lookup ();
919 if (ospf == NULL)
920 return 0;
921
paul68980082003-03-25 05:07:42 +0000922 ospf->t_distribute_update = NULL;
paul718e3742002-12-13 20:15:29 +0000923
924 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
925
926 /* foreach all external info. */
927 if (rt)
928 for (rn = route_top (rt); rn; rn = route_next (rn))
929 if ((ei = rn->info) != NULL)
paulcf795c52003-06-19 02:13:25 +0000930 {
931 if (is_prefix_default (&ei->p))
932 ospf_external_lsa_refresh_default (ospf);
933 else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
934 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED);
935 else
936 ospf_external_lsa_originate (ospf, ei);
937 }
paul718e3742002-12-13 20:15:29 +0000938 return 0;
939}
940
941#define OSPF_DISTRIBUTE_UPDATE_DELAY 5
942
943/* Update distribute-list and set timer to apply access-list. */
944void
paul68980082003-03-25 05:07:42 +0000945ospf_distribute_list_update (struct ospf *ospf, int type)
paul718e3742002-12-13 20:15:29 +0000946{
947 struct route_table *rt;
paulcf795c52003-06-19 02:13:25 +0000948
paul718e3742002-12-13 20:15:29 +0000949 /* External info does not exist. */
950 if (!(rt = EXTERNAL_INFO (type)))
951 return;
952
953 /* If exists previously invoked thread, then cancel it. */
paul68980082003-03-25 05:07:42 +0000954 if (ospf->t_distribute_update)
955 OSPF_TIMER_OFF (ospf->t_distribute_update);
paul718e3742002-12-13 20:15:29 +0000956
957 /* Set timer. */
paul68980082003-03-25 05:07:42 +0000958 ospf->t_distribute_update =
paul718e3742002-12-13 20:15:29 +0000959 thread_add_timer (master, ospf_distribute_list_update_timer,
paulcf795c52003-06-19 02:13:25 +0000960 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
paul718e3742002-12-13 20:15:29 +0000961}
962
963/* If access-list is updated, apply some check. */
964void
965ospf_filter_update (struct access_list *access)
966{
paul020709f2003-04-04 02:44:16 +0000967 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +0000968 int type;
969 int abr_inv = 0;
970 struct ospf_area *area;
971 listnode node;
972
973 /* If OSPF instatnce does not exist, return right now. */
paul020709f2003-04-04 02:44:16 +0000974 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +0000975 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +0000976 return;
977
paul718e3742002-12-13 20:15:29 +0000978 /* Update distribute-list, and apply filter. */
979 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
980 {
paul020709f2003-04-04 02:44:16 +0000981 if (ROUTEMAP (ospf, type) != NULL)
paulcf795c52003-06-19 02:13:25 +0000982 {
983 /* if route-map is not NULL it may be using this access list */
984 ospf_distribute_list_update (ospf, type);
985 continue;
986 }
987
paul718e3742002-12-13 20:15:29 +0000988
paul020709f2003-04-04 02:44:16 +0000989 if (DISTRIBUTE_NAME (ospf, type))
paulcf795c52003-06-19 02:13:25 +0000990 {
991 /* Keep old access-list for distribute-list. */
992 struct access_list *old = DISTRIBUTE_LIST (ospf, type);
993
994 /* Update access-list for distribute-list. */
995 DISTRIBUTE_LIST (ospf, type) =
996 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type));
997
998 /* No update for this distribute type. */
999 if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL)
1000 continue;
1001
1002 /* Schedule distribute-list update timer. */
1003 if (DISTRIBUTE_LIST (ospf, type) == NULL ||
1004 strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0)
1005 ospf_distribute_list_update (ospf, type);
1006 }
paul718e3742002-12-13 20:15:29 +00001007 }
1008
1009 /* Update Area access-list. */
paul68980082003-03-25 05:07:42 +00001010 for (node = listhead (ospf->areas); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001011 if ((area = getdata (node)) != NULL)
1012 {
paulcf795c52003-06-19 02:13:25 +00001013 if (EXPORT_NAME (area))
1014 {
1015 EXPORT_LIST (area) = NULL;
1016 abr_inv++;
1017 }
paul718e3742002-12-13 20:15:29 +00001018
paulcf795c52003-06-19 02:13:25 +00001019 if (IMPORT_NAME (area))
1020 {
1021 IMPORT_LIST (area) = NULL;
1022 abr_inv++;
1023 }
paul718e3742002-12-13 20:15:29 +00001024 }
1025
1026 /* Schedule ABR tasks -- this will be changed -- takada. */
paul020709f2003-04-04 02:44:16 +00001027 if (IS_OSPF_ABR (ospf) && abr_inv)
paul68980082003-03-25 05:07:42 +00001028 ospf_schedule_abr_task (ospf);
paul718e3742002-12-13 20:15:29 +00001029}
paul718e3742002-12-13 20:15:29 +00001030
paulcf795c52003-06-19 02:13:25 +00001031
paul718e3742002-12-13 20:15:29 +00001032struct ospf_distance *
1033ospf_distance_new ()
1034{
1035 struct ospf_distance *new;
1036 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
1037 memset (new, 0, sizeof (struct ospf_distance));
1038 return new;
1039}
1040
1041void
1042ospf_distance_free (struct ospf_distance *odistance)
1043{
1044 XFREE (MTYPE_OSPF_DISTANCE, odistance);
1045}
1046
1047int
paul020709f2003-04-04 02:44:16 +00001048ospf_distance_set (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001049 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001050{
1051 int ret;
1052 struct prefix_ipv4 p;
1053 u_char distance;
1054 struct route_node *rn;
1055 struct ospf_distance *odistance;
1056
1057 ret = str2prefix_ipv4 (ip_str, &p);
1058 if (ret == 0)
1059 {
1060 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1061 return CMD_WARNING;
1062 }
1063
1064 distance = atoi (distance_str);
1065
1066 /* Get OSPF distance node. */
paul68980082003-03-25 05:07:42 +00001067 rn = route_node_get (ospf->distance_table, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +00001068 if (rn->info)
1069 {
1070 odistance = rn->info;
1071 route_unlock_node (rn);
1072 }
1073 else
1074 {
1075 odistance = ospf_distance_new ();
1076 rn->info = odistance;
1077 }
1078
1079 /* Set distance value. */
1080 odistance->distance = distance;
1081
1082 /* Reset access-list configuration. */
1083 if (odistance->access_list)
1084 {
1085 free (odistance->access_list);
1086 odistance->access_list = NULL;
1087 }
1088 if (access_list_str)
1089 odistance->access_list = strdup (access_list_str);
1090
1091 return CMD_SUCCESS;
1092}
1093
1094int
paul020709f2003-04-04 02:44:16 +00001095ospf_distance_unset (struct vty *vty, struct ospf *ospf, char *distance_str,
paulcf795c52003-06-19 02:13:25 +00001096 char *ip_str, char *access_list_str)
paul718e3742002-12-13 20:15:29 +00001097{
1098 int ret;
1099 struct prefix_ipv4 p;
1100 u_char distance;
1101 struct route_node *rn;
1102 struct ospf_distance *odistance;
1103
1104 ret = str2prefix_ipv4 (ip_str, &p);
1105 if (ret == 0)
1106 {
1107 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1108 return CMD_WARNING;
1109 }
1110
1111 distance = atoi (distance_str);
1112
paulcf795c52003-06-19 02:13:25 +00001113 rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p);
1114 if (!rn)
paul718e3742002-12-13 20:15:29 +00001115 {
1116 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1117 return CMD_WARNING;
1118 }
1119
1120 odistance = rn->info;
1121
1122 if (odistance->access_list)
1123 free (odistance->access_list);
1124 ospf_distance_free (odistance);
1125
1126 rn->info = NULL;
1127 route_unlock_node (rn);
1128 route_unlock_node (rn);
1129
1130 return CMD_SUCCESS;
1131}
1132
1133void
paul68980082003-03-25 05:07:42 +00001134ospf_distance_reset (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001135{
1136 struct route_node *rn;
1137 struct ospf_distance *odistance;
1138
paul68980082003-03-25 05:07:42 +00001139 for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +00001140 if ((odistance = rn->info) != NULL)
1141 {
paulcf795c52003-06-19 02:13:25 +00001142 if (odistance->access_list)
1143 free (odistance->access_list);
1144 ospf_distance_free (odistance);
1145 rn->info = NULL;
1146 route_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +00001147 }
1148}
1149
1150u_char
1151ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1152{
paul020709f2003-04-04 02:44:16 +00001153 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00001154
paul020709f2003-04-04 02:44:16 +00001155 ospf = ospf_lookup ();
paul68980082003-03-25 05:07:42 +00001156 if (ospf == NULL)
paul718e3742002-12-13 20:15:29 +00001157 return 0;
1158
paul68980082003-03-25 05:07:42 +00001159 if (ospf->distance_intra)
paul718e3742002-12-13 20:15:29 +00001160 if (or->path_type == OSPF_PATH_INTRA_AREA)
paul68980082003-03-25 05:07:42 +00001161 return ospf->distance_intra;
paul718e3742002-12-13 20:15:29 +00001162
paul68980082003-03-25 05:07:42 +00001163 if (ospf->distance_inter)
paul718e3742002-12-13 20:15:29 +00001164 if (or->path_type == OSPF_PATH_INTER_AREA)
paul68980082003-03-25 05:07:42 +00001165 return ospf->distance_inter;
paul718e3742002-12-13 20:15:29 +00001166
paul68980082003-03-25 05:07:42 +00001167 if (ospf->distance_external)
paul718e3742002-12-13 20:15:29 +00001168 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
paulcf795c52003-06-19 02:13:25 +00001169 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
paul68980082003-03-25 05:07:42 +00001170 return ospf->distance_external;
paulcf795c52003-06-19 02:13:25 +00001171
paul68980082003-03-25 05:07:42 +00001172 if (ospf->distance_all)
1173 return ospf->distance_all;
paul718e3742002-12-13 20:15:29 +00001174
1175 return 0;
1176}
1177
1178void
1179ospf_zebra_init ()
1180{
1181 /* Allocate zebra structure. */
1182 zclient = zclient_new ();
1183 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
1184 zclient->interface_add = ospf_interface_add;
1185 zclient->interface_delete = ospf_interface_delete;
1186 zclient->interface_up = ospf_interface_state_up;
1187 zclient->interface_down = ospf_interface_state_down;
1188 zclient->interface_address_add = ospf_interface_address_add;
1189 zclient->interface_address_delete = ospf_interface_address_delete;
1190 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1191 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1192
1193 access_list_add_hook (ospf_filter_update);
1194 access_list_delete_hook (ospf_filter_update);
1195}