]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_zebra.c
Initial revision
[mirror_frr.git] / ospfd / ospf_zebra.c
1 /*
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. */
52 struct zclient *zclient = NULL;
53
54 /* For registering threads. */
55 extern struct thread_master *master;
56
57 /* Inteface addition message from zebra. */
58 int
59 ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length)
60 {
61 struct interface *ifp;
62
63 ifp = zebra_interface_add_read (zclient->ibuf);
64
65 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
66 zlog_info ("Zebra: interface add %s index %d flags %ld metric %d mtu %d",
67 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
68
69 if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
70 {
71 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
72 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
73
74 if (if_is_broadcast (ifp))
75 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST;
76 else if (if_is_pointopoint (ifp))
77 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_POINTOPOINT;
78 else if (if_is_loopback (ifp))
79 IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_LOOPBACK;
80 }
81
82 ospf_if_update ();
83
84 #ifdef HAVE_SNMP
85 ospf_snmp_if_update (ifp);
86 #endif /* HAVE_SNMP */
87
88 return 0;
89 }
90
91 int
92 ospf_interface_delete (int command, struct zclient *zclient,
93 zebra_size_t length)
94 {
95 struct interface *ifp;
96 struct stream *s;
97 struct route_node *rn;
98
99 s = zclient->ibuf;
100 /* zebra_interface_state_read() updates interface structure in iflist */
101 ifp = zebra_interface_state_read (s);
102
103 if (ifp == NULL)
104 return 0;
105
106 if (if_is_up (ifp))
107 zlog_warn ("Zebra: got delete of %s, but interface is still up",
108 ifp->name);
109
110 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
111 zlog_info ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d",
112 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
113
114 #ifdef HAVE_SNMP
115 ospf_snmp_if_delete (ifp);
116 #endif /* HAVE_SNMP */
117
118 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
119 if (rn->info)
120 ospf_if_free ((struct ospf_interface *) rn->info);
121
122 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
123 if (rn->info)
124 ospf_del_if_params (rn->info);
125
126 if_delete (ifp);
127
128 return 0;
129 }
130
131 struct interface *
132 zebra_interface_if_lookup (struct stream *s)
133 {
134 struct interface *ifp;
135 u_char ifname_tmp[INTERFACE_NAMSIZ];
136
137 /* Read interface name. */
138 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
139
140 /* Lookup this by interface index. */
141 ifp = if_lookup_by_name (ifname_tmp);
142
143 /* If such interface does not exist, indicate an error */
144 if (!ifp)
145 return NULL;
146
147 return ifp;
148 }
149
150 void
151 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
152 {
153 /* Read interface's index. */
154 ifp->ifindex = stream_getl (s);
155
156 /* Read interface's value. */
157 ifp->flags = stream_getl (s);
158 ifp->metric = stream_getl (s);
159 ifp->mtu = stream_getl (s);
160 ifp->bandwidth = stream_getl (s);
161 }
162
163 int
164 ospf_interface_state_up (int command, struct zclient *zclient,
165 zebra_size_t length)
166 {
167 struct interface *ifp;
168 struct interface if_tmp;
169 struct ospf_interface *oi;
170 struct route_node *rn;
171
172 ifp = zebra_interface_if_lookup (zclient->ibuf);
173
174 if (ifp == NULL)
175 return 0;
176
177 /* Interface is already up. */
178 if (if_is_up (ifp))
179 {
180 /* Temporarily keep ifp values. */
181 memcpy (&if_tmp, ifp, sizeof (struct interface));
182
183 zebra_interface_if_set_value (zclient->ibuf, ifp);
184
185 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
186 zlog_info ("Zebra: Interface[%s] state update.", ifp->name);
187
188 if (if_tmp.bandwidth != ifp->bandwidth)
189 {
190 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
191 zlog_info ("Zebra: Interface[%s] bandwidth change %d -> %d.",
192 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
193
194 ospf_if_recalculate_output_cost (ifp);
195 }
196 return 0;
197 }
198
199 zebra_interface_if_set_value (zclient->ibuf, ifp);
200
201 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
202 zlog_info ("Zebra: Interface[%s] state change to up.", ifp->name);
203
204 for (rn = route_top (IF_OIFS (ifp));rn; rn = route_next (rn))
205 {
206 if ( (oi = rn->info) == NULL)
207 continue;
208
209 ospf_if_up (oi);
210 }
211
212 return 0;
213 }
214
215 int
216 ospf_interface_state_down (int command, struct zclient *zclient,
217 zebra_size_t length)
218 {
219 struct interface *ifp;
220 struct ospf_interface *oi;
221 struct route_node *node;
222
223 ifp = zebra_interface_state_read (zclient->ibuf);
224
225 if (ifp == NULL)
226 return 0;
227
228 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
229 zlog_info ("Zebra: Interface[%s] state change to down.", ifp->name);
230
231 for (node = route_top (IF_OIFS (ifp));node; node = route_next (node))
232 {
233 if ( (oi = node->info) == NULL)
234 continue;
235 ospf_if_down (oi);
236 }
237
238 return 0;
239 }
240
241 int
242 ospf_interface_address_add (int command, struct zclient *zclient,
243 zebra_size_t length)
244 {
245 struct connected *c;
246
247 c = zebra_interface_address_add_read (zclient->ibuf);
248
249 if (c == NULL)
250 return 0;
251
252 #if 0
253 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
254 {
255 struct prefix *p;
256
257 p = c->address;
258 if (p->family == AF_INET)
259 zlog_info (" connected address %s/%d",
260 inet_atop (p->u.prefix4), p->prefixlen);
261 }
262 #endif
263
264 ospf_if_update ();
265
266 #ifdef HAVE_SNMP
267 ospf_snmp_if_update (c->ifp);
268 #endif /* HAVE_SNMP */
269
270 return 0;
271 }
272
273 int
274 ospf_interface_address_delete (int command, struct zclient *zclient,
275 zebra_size_t length)
276 {
277 struct connected *c;
278 struct interface *ifp;
279 struct ospf_interface *oi;
280 struct route_node *rn;
281 struct prefix p;
282
283 c = zebra_interface_address_delete_read (zclient->ibuf);
284
285 if (c == NULL)
286 return 0;
287
288 ifp = c->ifp;
289 p = *c->address;
290 p.prefixlen = IPV4_MAX_PREFIXLEN;
291
292 rn = route_node_lookup (IF_OIFS (ifp), &p);
293 if (! rn)
294 return 0;
295
296 assert (rn->info);
297 oi = rn->info;
298
299 /* Call interface hook functions to clean up */
300 ospf_if_free (oi);
301
302 #ifdef HAVE_SNMP
303 ospf_snmp_if_update (c->ifp);
304 #endif /* HAVE_SNMP */
305
306 connected_free (c);
307
308 ospf_if_update();
309
310 return 0;
311 }
312 \f
313 void
314 ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or)
315 {
316 u_char message;
317 u_char distance;
318 u_char flags;
319 int psize;
320 struct stream *s;
321 struct ospf_path *path;
322 listnode node;
323
324 if (zclient->redist[ZEBRA_ROUTE_OSPF])
325 {
326 message = 0;
327 flags = 0;
328
329 /* OSPF pass nexthop and metric */
330 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
331 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
332
333 /* Distance value. */
334 distance = ospf_distance_apply (p, or);
335 if (distance)
336 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
337
338 /* Make packet. */
339 s = zclient->obuf;
340 stream_reset (s);
341
342 /* Length place holder. */
343 stream_putw (s, 0);
344
345 /* Put command, type, flags, message. */
346 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
347 stream_putc (s, ZEBRA_ROUTE_OSPF);
348 stream_putc (s, flags);
349 stream_putc (s, message);
350
351 /* Put prefix information. */
352 psize = PSIZE (p->prefixlen);
353 stream_putc (s, p->prefixlen);
354 stream_write (s, (u_char *)&p->prefix, psize);
355
356 /* Nexthop count. */
357 stream_putc (s, or->path->count);
358
359 /* Nexthop, ifindex, distance and metric information. */
360 for (node = listhead (or->path); node; nextnode (node))
361 {
362 path = getdata (node);
363
364 if (path->nexthop.s_addr != INADDR_ANY)
365 {
366 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
367 stream_put_in_addr (s, &path->nexthop);
368 }
369 else
370 {
371 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
372 if (path->oi)
373 stream_putl (s, path->oi->ifp->ifindex);
374 else
375 stream_putl (s, 0);
376 }
377 }
378
379 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
380 stream_putc (s, distance);
381 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
382 {
383 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
384 stream_putl (s, or->cost + or->u.ext.type2_cost);
385 else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
386 stream_putl (s, or->u.ext.type2_cost);
387 else
388 stream_putl (s, or->cost);
389 }
390
391 stream_putw_at (s, 0, stream_get_endp (s));
392
393 writen (zclient->sock, s->data, stream_get_endp (s));
394
395 #if 0
396 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
397 {
398 char *nexthop_str;
399
400 nexthop_str = strdup (inet_ntoa (*nexthop));
401 zlog_info ("Zebra: Route add %s/%d nexthop %s metric %d",
402 inet_ntoa (p->prefix), p->prefixlen, nexthop_str,
403 metric);
404 free (nexthop_str);
405 }
406 #endif /* 0 */
407 }
408 }
409
410 void
411 ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or)
412 {
413 struct zapi_ipv4 api;
414
415 if (zclient->redist[ZEBRA_ROUTE_OSPF])
416 {
417 api.type = ZEBRA_ROUTE_OSPF;
418 api.flags = 0;
419 api.message = 0;
420 zapi_ipv4_delete (zclient, p, &api);
421
422 #if 0
423 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
424 {
425 char *nexthop_str;
426
427 nexthop_str = strdup (inet_ntoa (*nexthop));
428 zlog_info ("Zebra: Route delete %s/%d nexthop %s",
429 inet_ntoa (p->prefix), p->prefixlen, nexthop_str);
430 free (nexthop_str);
431 }
432 #endif /* 0 */
433 }
434 }
435
436 void
437 ospf_zebra_add_discard (struct prefix_ipv4 *p)
438 {
439 struct zapi_ipv4 api;
440
441 if (zclient->redist[ZEBRA_ROUTE_OSPF])
442 {
443 api.type = ZEBRA_ROUTE_OSPF;
444 api.flags = ZEBRA_FLAG_BLACKHOLE;
445 api.message = 0;
446 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
447 api.nexthop_num = 0;
448 api.ifindex_num = 0;
449
450 zapi_ipv4_add (zclient, p, &api);
451 }
452 }
453
454 void
455 ospf_zebra_delete_discard (struct prefix_ipv4 *p)
456 {
457 struct zapi_ipv4 api;
458
459 if (zclient->redist[ZEBRA_ROUTE_OSPF])
460 {
461 api.type = ZEBRA_ROUTE_OSPF;
462 api.flags = ZEBRA_FLAG_BLACKHOLE;
463 api.message = 0;
464 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
465 api.nexthop_num = 0;
466 api.ifindex_num = 0;
467
468 zapi_ipv4_delete (zclient, p, &api);
469 }
470 }
471
472 int
473 ospf_is_type_redistributed (int type)
474 {
475 return (DEFAULT_ROUTE_TYPE (type)) ?
476 zclient->default_information : zclient->redist[type];
477 }
478
479 int
480 ospf_redistribute_set (int type, int mtype, int mvalue)
481 {
482 int force = 0;
483
484 if (ospf_is_type_redistributed (type))
485 {
486 if (mtype != ospf_top->dmetric[type].type)
487 {
488 ospf_top->dmetric[type].type = mtype;
489 force = LSA_REFRESH_FORCE;
490 }
491 if (mvalue != ospf_top->dmetric[type].value)
492 {
493 ospf_top->dmetric[type].value = mvalue;
494 force = LSA_REFRESH_FORCE;
495 }
496
497 ospf_external_lsa_refresh_type (type, force);
498
499 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
500 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
501 LOOKUP (ospf_redistributed_proto, type),
502 metric_type (type), metric_value (type));
503
504 return CMD_SUCCESS;
505 }
506
507 ospf_top->dmetric[type].type = mtype;
508 ospf_top->dmetric[type].value = mvalue;
509
510 zclient_redistribute_set (zclient, type);
511
512 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
513 zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]",
514 LOOKUP (ospf_redistributed_proto, type),
515 metric_type (type), metric_value (type));
516
517 ospf_asbr_status_update (++ospf_top->redistribute);
518
519 return CMD_SUCCESS;
520 }
521
522 int
523 ospf_redistribute_unset (int type)
524 {
525 if (type == zclient->redist_default)
526 return CMD_SUCCESS;
527
528 if (! ospf_is_type_redistributed (type))
529 return CMD_SUCCESS;
530
531 zclient_redistribute_unset (zclient, type);
532
533 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
534 zlog_info ("Redistribute[%s]: Stop",
535 LOOKUP (ospf_redistributed_proto, type));
536
537 ospf_top->dmetric[type].type = -1;
538 ospf_top->dmetric[type].value = -1;
539
540 /* Remove the routes from OSPF table. */
541 ospf_redistribute_withdraw (type);
542
543 ospf_asbr_status_update (--ospf_top->redistribute);
544
545 return CMD_SUCCESS;
546 }
547
548 int
549 ospf_redistribute_default_set (int originate, int mtype, int mvalue)
550 {
551 int force = 0;
552 if (ospf_is_type_redistributed (DEFAULT_ROUTE))
553 {
554 if (mtype != ospf_top->dmetric[DEFAULT_ROUTE].type)
555 {
556 ospf_top->dmetric[DEFAULT_ROUTE].type = mtype;
557 force = 1;
558 }
559 if (mvalue != ospf_top->dmetric[DEFAULT_ROUTE].value)
560 {
561 force = 1;
562 ospf_top->dmetric[DEFAULT_ROUTE].value = mvalue;
563 }
564
565 ospf_external_lsa_refresh_default ();
566
567 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
568 zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
569 LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE),
570 metric_type (DEFAULT_ROUTE),
571 metric_value (DEFAULT_ROUTE));
572 return CMD_SUCCESS;
573 }
574
575 ospf_top->default_originate = originate;
576 ospf_top->dmetric[DEFAULT_ROUTE].type = mtype;
577 ospf_top->dmetric[DEFAULT_ROUTE].value = mvalue;
578
579 zclient_redistribute_default_set (zclient);
580
581 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
582 zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]",
583 metric_type (DEFAULT_ROUTE), metric_value (DEFAULT_ROUTE));
584
585
586 if (ospf_top->router_id.s_addr == 0)
587 ospf_top->external_origin |= (1 << DEFAULT_ROUTE);
588 else
589 thread_add_timer (master, ospf_default_originate_timer,
590 &ospf_top->default_originate, 1);
591
592 ospf_asbr_status_update (++ospf_top->redistribute);
593
594 return CMD_SUCCESS;
595 }
596
597 int
598 ospf_redistribute_default_unset ()
599 {
600 if (!ospf_is_type_redistributed (DEFAULT_ROUTE))
601 return CMD_SUCCESS;
602
603 ospf_top->default_originate = DEFAULT_ORIGINATE_NONE;
604 ospf_top->dmetric[DEFAULT_ROUTE].type = -1;
605 ospf_top->dmetric[DEFAULT_ROUTE].value = -1;
606
607 zclient_redistribute_default_unset (zclient);
608
609 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
610 zlog_info ("Redistribute[DEFAULT]: Stop");
611
612 ospf_asbr_status_update (--ospf_top->redistribute);
613
614 return CMD_SUCCESS;
615 }
616
617 int
618 ospf_external_lsa_originate_check (struct external_info *ei)
619 {
620 /* If prefix is multicast, then do not originate LSA. */
621 if (IN_MULTICAST (htonl (ei->p.prefix.s_addr)))
622 {
623 zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, "
624 "Prefix belongs multicast", inet_ntoa (ei->p.prefix));
625 return 0;
626 }
627
628 /* Take care of default-originate. */
629 if (is_prefix_default (&ei->p))
630 if (ospf_top->default_originate == DEFAULT_ORIGINATE_NONE)
631 {
632 zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
633 "for default");
634 return 0;
635 }
636
637 return 1;
638 }
639
640 /* If connected prefix is OSPF enable interface, then do not announce. */
641 int
642 ospf_distribute_check_connected (struct external_info *ei)
643 {
644 struct route_node *rn;
645
646 for (rn = route_top (ospf_top->networks); rn; rn = route_next (rn))
647 if (rn->info != NULL)
648 if (prefix_match (&rn->p, (struct prefix *)&ei->p))
649 return 0;
650
651 return 1;
652 }
653
654 /* return 1 if external LSA must be originated, 0 otherwise */
655 int
656 ospf_redistribute_check (struct external_info *ei, int *changed)
657 {
658 struct route_map_set_values save_values;
659 struct prefix_ipv4 *p = &ei->p;
660 u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
661
662 if (changed)
663 *changed = 0;
664
665 if (!ospf_external_lsa_originate_check (ei))
666 return 0;
667
668 /* Take care connected route. */
669 if (type == ZEBRA_ROUTE_CONNECT && !ospf_distribute_check_connected (ei))
670 return 0;
671
672 if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (type))
673 /* distirbute-list exists, but access-list may not? */
674 if (DISTRIBUTE_LIST (type))
675 if (access_list_apply (DISTRIBUTE_LIST (type), p) == FILTER_DENY)
676 {
677 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
678 zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.",
679 LOOKUP (ospf_redistributed_proto, type),
680 inet_ntoa (p->prefix), p->prefixlen);
681 return 0;
682 }
683
684 save_values = ei->route_map_set;
685 ospf_reset_route_map_set_values (&ei->route_map_set);
686
687 /* apply route-map if needed */
688 if (ROUTEMAP_NAME (type))
689 {
690 int ret;
691
692 ret = route_map_apply (ROUTEMAP (type), (struct prefix *)p,
693 RMAP_OSPF, ei);
694
695 if (ret == RMAP_DENYMATCH)
696 {
697 ei->route_map_set = save_values;
698 if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE))
699 zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.",
700 LOOKUP (ospf_redistributed_proto, type),
701 inet_ntoa (p->prefix), p->prefixlen);
702 return 0;
703 }
704
705 /* check if 'route-map set' changed something */
706 if (changed)
707 *changed = !ospf_route_map_set_compare (&ei->route_map_set,
708 &save_values);
709 }
710
711 return 1;
712 }
713
714 /* OSPF route-map set for redistribution */
715 void
716 ospf_routemap_set (int type, char *name)
717 {
718 if (ROUTEMAP_NAME (type))
719 free (ROUTEMAP_NAME (type));
720
721 ROUTEMAP_NAME (type) = strdup (name);
722 ROUTEMAP (type) = route_map_lookup_by_name (name);
723 }
724
725 void
726 ospf_routemap_unset (int type)
727 {
728 if (ROUTEMAP_NAME (type))
729 free (ROUTEMAP_NAME (type));
730
731 ROUTEMAP_NAME (type) = NULL;
732 ROUTEMAP (type) = NULL;
733 }
734
735 /* Zebra route add and delete treatment. */
736 int
737 ospf_zebra_read_ipv4 (int command, struct zclient *zclient,
738 zebra_size_t length)
739 {
740 struct stream *s;
741 struct zapi_ipv4 api;
742 unsigned long ifindex;
743 struct in_addr nexthop;
744 struct prefix_ipv4 p;
745 struct external_info *ei;
746
747 s = zclient->ibuf;
748 ifindex = 0;
749 nexthop.s_addr = 0;
750
751 /* Type, flags, message. */
752 api.type = stream_getc (s);
753 api.flags = stream_getc (s);
754 api.message = stream_getc (s);
755
756 /* IPv4 prefix. */
757 memset (&p, 0, sizeof (struct prefix_ipv4));
758 p.family = AF_INET;
759 p.prefixlen = stream_getc (s);
760 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
761
762 /* Nexthop, ifindex, distance, metric. */
763 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
764 {
765 api.nexthop_num = stream_getc (s);
766 nexthop.s_addr = stream_get_ipv4 (s);
767 }
768 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
769 {
770 api.ifindex_num = stream_getc (s);
771 ifindex = stream_getl (s);
772 }
773 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
774 api.distance = stream_getc (s);
775 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
776 api.metric = stream_getl (s);
777
778 if (command == ZEBRA_IPV4_ROUTE_ADD)
779 {
780 ei = ospf_external_info_add (api.type, p, ifindex, nexthop);
781
782 if (ospf_top->router_id.s_addr == 0)
783 /* Set flags to generate AS-external-LSA originate event
784 for each redistributed protocols later. */
785 ospf_top->external_origin |= (1 << api.type);
786 else
787 {
788 if (ei)
789 {
790 if (is_prefix_default (&p))
791 ospf_external_lsa_refresh_default ();
792 else
793 {
794 struct ospf_lsa *current;
795
796 current = ospf_external_info_find_lsa (&ei->p);
797 if (!current)
798 ospf_external_lsa_originate (ei);
799 else if (IS_LSA_MAXAGE (current))
800 ospf_external_lsa_refresh (current, ei, LSA_REFRESH_FORCE);
801 else
802 zlog_warn ("ospf_zebra_read_ipv4() : %s already exists",
803 inet_ntoa (p.prefix));
804 }
805 }
806 }
807 }
808 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
809 {
810 ospf_external_info_delete (api.type, p);
811 if ( !is_prefix_default (&p))
812 ospf_external_lsa_flush (api.type, &p, ifindex, nexthop);
813 else
814 ospf_external_lsa_refresh_default ();
815 }
816
817 return 0;
818 }
819
820 \f
821 int
822 ospf_distribute_list_out_set (int type, char *name)
823 {
824 /* Lookup access-list for distribute-list. */
825 DISTRIBUTE_LIST (type) = access_list_lookup (AFI_IP, name);
826
827 /* Clear previous distribute-name. */
828 if (DISTRIBUTE_NAME (type))
829 free (DISTRIBUTE_NAME (type));
830
831 /* Set distribute-name. */
832 DISTRIBUTE_NAME (type) = strdup (name);
833
834 /* If access-list have been set, schedule update timer. */
835 if (DISTRIBUTE_LIST (type))
836 ospf_distribute_list_update (type);
837
838 return CMD_SUCCESS;
839 }
840
841 int
842 ospf_distribute_list_out_unset (int type, char *name)
843 {
844 /* Schedule update timer. */
845 if (DISTRIBUTE_LIST (type))
846 ospf_distribute_list_update (type);
847
848 /* Unset distribute-list. */
849 DISTRIBUTE_LIST (type) = NULL;
850
851 /* Clear distribute-name. */
852 if (DISTRIBUTE_NAME (type))
853 free (DISTRIBUTE_NAME (type));
854
855 DISTRIBUTE_NAME (type) = NULL;
856
857 return CMD_SUCCESS;
858 }
859
860 /* distribute-list update timer. */
861 int
862 ospf_distribute_list_update_timer (struct thread *thread)
863 {
864 struct route_node *rn;
865 struct external_info *ei;
866 struct route_table *rt;
867 struct ospf_lsa *lsa;
868 u_char type;
869
870 type = (int) THREAD_ARG (thread);
871 rt = EXTERNAL_INFO (type);
872
873 ospf_top->t_distribute_update = NULL;
874
875 zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!");
876
877 /* foreach all external info. */
878 if (rt)
879 for (rn = route_top (rt); rn; rn = route_next (rn))
880 if ((ei = rn->info) != NULL)
881 {
882 if (is_prefix_default (&ei->p))
883 ospf_external_lsa_refresh_default ();
884 else if ((lsa = ospf_external_info_find_lsa (&ei->p)))
885 ospf_external_lsa_refresh (lsa, ei, LSA_REFRESH_IF_CHANGED);
886 else
887 ospf_external_lsa_originate (ei);
888 }
889 return 0;
890 }
891
892 #define OSPF_DISTRIBUTE_UPDATE_DELAY 5
893
894 /* Update distribute-list and set timer to apply access-list. */
895 void
896 ospf_distribute_list_update (int type)
897 {
898 struct route_table *rt;
899
900 zlog_info ("ospf_distribute_list_update(): start");
901
902 /* External info does not exist. */
903 if (!(rt = EXTERNAL_INFO (type)))
904 return;
905
906 /* If exists previously invoked thread, then cancel it. */
907 if (ospf_top->t_distribute_update)
908 OSPF_TIMER_OFF (ospf_top->t_distribute_update);
909
910 /* Set timer. */
911 ospf_top->t_distribute_update =
912 thread_add_timer (master, ospf_distribute_list_update_timer,
913 (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY);
914
915 zlog_info ("ospf_distribute_list_update(): stop");
916 }
917
918 /* If access-list is updated, apply some check. */
919 void
920 ospf_filter_update (struct access_list *access)
921 {
922 int type;
923 int abr_inv = 0;
924 struct ospf_area *area;
925 listnode node;
926
927 /* If OSPF instatnce does not exist, return right now. */
928 if (!ospf_top)
929 return;
930
931
932 /* Update distribute-list, and apply filter. */
933 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
934 {
935 if (ROUTEMAP (type) != NULL)
936 {
937 /* if route-map is not NULL it may be using this access list */
938 ospf_distribute_list_update (type);
939 continue;
940 }
941
942
943 if (DISTRIBUTE_NAME (type))
944 {
945 /* Keep old access-list for distribute-list. */
946 struct access_list *old = DISTRIBUTE_LIST (type);
947
948 /* Update access-list for distribute-list. */
949 DISTRIBUTE_LIST (type) =
950 access_list_lookup (AFI_IP, DISTRIBUTE_NAME (type));
951
952 /* No update for this distribute type. */
953 if (old == NULL && DISTRIBUTE_LIST (type) == NULL)
954 continue;
955
956 /* Schedule distribute-list update timer. */
957 if (DISTRIBUTE_LIST (type) == NULL ||
958 strcmp (DISTRIBUTE_NAME (type), access->name) == 0)
959 ospf_distribute_list_update (type);
960 }
961 }
962
963 /* Update Area access-list. */
964 for (node = listhead (ospf_top->areas); node; nextnode (node))
965 if ((area = getdata (node)) != NULL)
966 {
967 if (EXPORT_NAME (area))
968 {
969 EXPORT_LIST (area) = NULL;
970 abr_inv++;
971 }
972
973 if (IMPORT_NAME (area))
974 {
975 IMPORT_LIST (area) = NULL;
976 abr_inv++;
977 }
978 }
979
980 /* Schedule ABR tasks -- this will be changed -- takada. */
981 if (OSPF_IS_ABR && abr_inv)
982 ospf_schedule_abr_task ();
983 }
984
985 \f
986 struct ospf_distance *
987 ospf_distance_new ()
988 {
989 struct ospf_distance *new;
990 new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance));
991 memset (new, 0, sizeof (struct ospf_distance));
992 return new;
993 }
994
995 void
996 ospf_distance_free (struct ospf_distance *odistance)
997 {
998 XFREE (MTYPE_OSPF_DISTANCE, odistance);
999 }
1000
1001 int
1002 ospf_distance_set (struct vty *vty, char *distance_str, char *ip_str,
1003 char *access_list_str)
1004 {
1005 int ret;
1006 struct prefix_ipv4 p;
1007 u_char distance;
1008 struct route_node *rn;
1009 struct ospf_distance *odistance;
1010
1011 ret = str2prefix_ipv4 (ip_str, &p);
1012 if (ret == 0)
1013 {
1014 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1015 return CMD_WARNING;
1016 }
1017
1018 distance = atoi (distance_str);
1019
1020 /* Get OSPF distance node. */
1021 rn = route_node_get (ospf_top->distance_table, (struct prefix *) &p);
1022 if (rn->info)
1023 {
1024 odistance = rn->info;
1025 route_unlock_node (rn);
1026 }
1027 else
1028 {
1029 odistance = ospf_distance_new ();
1030 rn->info = odistance;
1031 }
1032
1033 /* Set distance value. */
1034 odistance->distance = distance;
1035
1036 /* Reset access-list configuration. */
1037 if (odistance->access_list)
1038 {
1039 free (odistance->access_list);
1040 odistance->access_list = NULL;
1041 }
1042 if (access_list_str)
1043 odistance->access_list = strdup (access_list_str);
1044
1045 return CMD_SUCCESS;
1046 }
1047
1048 int
1049 ospf_distance_unset (struct vty *vty, char *distance_str, char *ip_str,
1050 char *access_list_str)
1051 {
1052 int ret;
1053 struct prefix_ipv4 p;
1054 u_char distance;
1055 struct route_node *rn;
1056 struct ospf_distance *odistance;
1057
1058 ret = str2prefix_ipv4 (ip_str, &p);
1059 if (ret == 0)
1060 {
1061 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
1062 return CMD_WARNING;
1063 }
1064
1065 distance = atoi (distance_str);
1066
1067 rn = route_node_lookup (ospf_top->distance_table, (struct prefix *)&p);
1068 if (! rn)
1069 {
1070 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
1071 return CMD_WARNING;
1072 }
1073
1074 odistance = rn->info;
1075
1076 if (odistance->access_list)
1077 free (odistance->access_list);
1078 ospf_distance_free (odistance);
1079
1080 rn->info = NULL;
1081 route_unlock_node (rn);
1082 route_unlock_node (rn);
1083
1084 return CMD_SUCCESS;
1085 }
1086
1087 void
1088 ospf_distance_reset ()
1089 {
1090 struct route_node *rn;
1091 struct ospf_distance *odistance;
1092
1093 for (rn = route_top (ospf_top->distance_table); rn; rn = route_next (rn))
1094 if ((odistance = rn->info) != NULL)
1095 {
1096 if (odistance->access_list)
1097 free (odistance->access_list);
1098 ospf_distance_free (odistance);
1099 rn->info = NULL;
1100 route_unlock_node (rn);
1101 }
1102 }
1103
1104 u_char
1105 ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or)
1106 {
1107 #if 0
1108 struct route_node *rn;
1109 struct ospf_distance *odistance;
1110 struct access_list *alist;
1111 struct prefix_ipv4 q;
1112
1113 memset (&q, 0, sizeof (struct prefix_ipv4));
1114 q.family = AF_INET;
1115 /* q.prefix = */
1116 q.prefixlen = IPV4_MAX_BITLEN;
1117 #endif /* 0 */
1118
1119 if (! ospf_top)
1120 return 0;
1121
1122 #if 0
1123 rn = route_node_match (ospf_top->distance_table, (struct prefix *) &q);
1124 if (rn)
1125 {
1126 odistance = rn->info;
1127 route_unlock_node (rn);
1128
1129 if (odistance->access_list)
1130 {
1131 alist = access_list_lookup (AFI_IP, odistance->access_list);
1132 if (alist == NULL)
1133 return 0;
1134 if (access_list_apply (alist, (struct prefix *) p) == FILTER_DENY)
1135 return 0;
1136
1137 return odistance->distance;
1138 }
1139 else
1140 return odistance->distance;
1141 }
1142 #endif /* 0 */
1143
1144 if (ospf_top->distance_intra)
1145 if (or->path_type == OSPF_PATH_INTRA_AREA)
1146 return ospf_top->distance_intra;
1147
1148 if (ospf_top->distance_inter)
1149 if (or->path_type == OSPF_PATH_INTER_AREA)
1150 return ospf_top->distance_inter;
1151
1152 if (ospf_top->distance_external)
1153 if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL
1154 || or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1155 return ospf_top->distance_external;
1156
1157 if (ospf_top->distance_all)
1158 return ospf_top->distance_all;
1159
1160 return 0;
1161 }
1162
1163 void
1164 ospf_zebra_init ()
1165 {
1166 /* Allocate zebra structure. */
1167 zclient = zclient_new ();
1168 zclient_init (zclient, ZEBRA_ROUTE_OSPF);
1169 zclient->interface_add = ospf_interface_add;
1170 zclient->interface_delete = ospf_interface_delete;
1171 zclient->interface_up = ospf_interface_state_up;
1172 zclient->interface_down = ospf_interface_state_down;
1173 zclient->interface_address_add = ospf_interface_address_add;
1174 zclient->interface_address_delete = ospf_interface_address_delete;
1175 zclient->ipv4_route_add = ospf_zebra_read_ipv4;
1176 zclient->ipv4_route_delete = ospf_zebra_read_ipv4;
1177
1178 access_list_add_hook (ospf_filter_update);
1179 access_list_delete_hook (ospf_filter_update);
1180 }