]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_asbr.c
bgpd: peer->bgp must be non NULL
[mirror_frr.git] / ospf6d / ospf6_asbr.c
CommitLineData
718e3744 1/*
508e53e2 2 * Copyright (C) 2003 Yasuhiro Ohara
718e3744 3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
22
23#include "log.h"
24#include "memory.h"
25#include "prefix.h"
26#include "command.h"
27#include "vty.h"
28#include "routemap.h"
29#include "table.h"
30#include "plist.h"
31#include "thread.h"
508e53e2 32#include "linklist.h"
718e3744 33
508e53e2 34#include "ospf6_proto.h"
35#include "ospf6_lsa.h"
36#include "ospf6_lsdb.h"
37#include "ospf6_route.h"
718e3744 38#include "ospf6_zebra.h"
6452df09 39#include "ospf6_message.h"
40
718e3744 41#include "ospf6_top.h"
508e53e2 42#include "ospf6_area.h"
6452df09 43#include "ospf6_interface.h"
44#include "ospf6_neighbor.h"
508e53e2 45#include "ospf6_asbr.h"
46#include "ospf6_intra.h"
6452df09 47#include "ospf6_flood.h"
049207c3 48#include "ospf6d.h"
718e3744 49
508e53e2 50unsigned char conf_debug_ospf6_asbr = 0;
718e3744 51
f52d13cb 52#define ZROUTE_NAME(x) zebra_route_string(x)
508e53e2 53
54/* AS External LSA origination */
d62a17ae 55static void ospf6_as_external_lsa_originate(struct ospf6_route *route)
718e3744 56{
d62a17ae 57 char buffer[OSPF6_MAX_LSASIZE];
58 struct ospf6_lsa_header *lsa_header;
59 struct ospf6_lsa *lsa;
60 struct ospf6_external_info *info = route->route_option;
61
62 struct ospf6_as_external_lsa *as_external_lsa;
63 char buf[PREFIX2STR_BUFFER];
64 caddr_t p;
65
66 if (IS_OSPF6_DEBUG_ASBR || IS_OSPF6_DEBUG_ORIGINATE(AS_EXTERNAL)) {
67 prefix2str(&route->prefix, buf, sizeof(buf));
68 zlog_debug("Originate AS-External-LSA for %s", buf);
69 }
70
71 /* prepare buffer */
72 memset(buffer, 0, sizeof(buffer));
73 lsa_header = (struct ospf6_lsa_header *)buffer;
74 as_external_lsa = (struct ospf6_as_external_lsa
75 *)((caddr_t)lsa_header
76 + sizeof(struct ospf6_lsa_header));
77 p = (caddr_t)((caddr_t)as_external_lsa
78 + sizeof(struct ospf6_as_external_lsa));
79
80 /* Fill AS-External-LSA */
81 /* Metric type */
82 if (route->path.metric_type == 2)
83 SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_E);
84 else
85 UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_E);
86
87 /* forwarding address */
88 if (!IN6_IS_ADDR_UNSPECIFIED(&info->forwarding))
89 SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F);
90 else
91 UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F);
92
93 /* external route tag */
94 if (info->tag)
95 SET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T);
96 else
97 UNSET_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T);
98
99 /* Set metric */
100 OSPF6_ASBR_METRIC_SET(as_external_lsa, route->path.cost);
101
102 /* prefixlen */
103 as_external_lsa->prefix.prefix_length = route->prefix.prefixlen;
104
105 /* PrefixOptions */
106 as_external_lsa->prefix.prefix_options = route->path.prefix_options;
107
108 /* don't use refer LS-type */
109 as_external_lsa->prefix.prefix_refer_lstype = htons(0);
110
111 /* set Prefix */
112 memcpy(p, &route->prefix.u.prefix6,
113 OSPF6_PREFIX_SPACE(route->prefix.prefixlen));
114 ospf6_prefix_apply_mask(&as_external_lsa->prefix);
115 p += OSPF6_PREFIX_SPACE(route->prefix.prefixlen);
116
117 /* Forwarding address */
118 if (CHECK_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F)) {
119 memcpy(p, &info->forwarding, sizeof(struct in6_addr));
120 p += sizeof(struct in6_addr);
121 }
122
123 /* External Route Tag */
124 if (CHECK_FLAG(as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T)) {
125 route_tag_t network_order = htonl(info->tag);
126
127 memcpy(p, &network_order, sizeof(network_order));
128 p += sizeof(network_order);
129 }
130
131 /* Fill LSA Header */
132 lsa_header->age = 0;
133 lsa_header->type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
134 lsa_header->id = route->path.origin.id;
135 lsa_header->adv_router = ospf6->router_id;
136 lsa_header->seqnum =
137 ospf6_new_ls_seqnum(lsa_header->type, lsa_header->id,
138 lsa_header->adv_router, ospf6->lsdb);
139 lsa_header->length = htons((caddr_t)p - (caddr_t)lsa_header);
140
141 /* LSA checksum */
142 ospf6_lsa_checksum(lsa_header);
143
144 /* create LSA */
145 lsa = ospf6_lsa_create(lsa_header);
146
147 /* Originate */
148 ospf6_lsa_originate_process(lsa, ospf6);
718e3744 149}
150
76249532
CS
151int ospf6_orig_as_external_lsa(struct thread *thread)
152{
153 struct ospf6_interface *oi;
154 struct ospf6_lsa *lsa;
155 uint32_t type, adv_router;
156
157 oi = (struct ospf6_interface *)THREAD_ARG(thread);
158 oi->thread_as_extern_lsa = NULL;
159
160 if (oi->state == OSPF6_INTERFACE_DOWN)
161 return 0;
162
163 type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
164 adv_router = oi->area->ospf6->router_id;
165 for (ALL_LSDB_TYPED_ADVRTR(ospf6->lsdb, type, adv_router, lsa)) {
166 if (IS_OSPF6_DEBUG_ASBR)
996c9314
LB
167 zlog_debug(
168 "%s: Send update of AS-External LSA %s seq 0x%x",
169 __PRETTY_FUNCTION__, lsa->name,
170 ntohl(lsa->header->seqnum));
76249532
CS
171
172 ospf6_flood_interface(NULL, lsa, oi);
173 }
174
175 return 0;
176}
177
d62a17ae 178static route_tag_t ospf6_as_external_lsa_get_tag(struct ospf6_lsa *lsa)
464015fa 179{
d62a17ae 180 struct ospf6_as_external_lsa *external;
181 ptrdiff_t tag_offset;
182 route_tag_t network_order;
464015fa 183
d62a17ae 184 if (!lsa)
185 return 0;
464015fa 186
d62a17ae 187 external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
188 lsa->header);
464015fa 189
d62a17ae 190 if (!CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T))
191 return 0;
464015fa 192
d62a17ae 193 tag_offset = sizeof(*external)
194 + OSPF6_PREFIX_SPACE(external->prefix.prefix_length);
195 if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F))
196 tag_offset += sizeof(struct in6_addr);
464015fa 197
d62a17ae 198 memcpy(&network_order, (caddr_t)external + tag_offset,
199 sizeof(network_order));
200 return ntohl(network_order);
464015fa 201}
6b0655a2 202
064d4355
CS
203void ospf6_asbr_update_route_ecmp_path(struct ospf6_route *old,
204 struct ospf6_route *route)
205{
206 struct ospf6_route *old_route;
207 struct ospf6_path *ecmp_path, *o_path = NULL;
07b37f93 208 struct listnode *anode, *anext;
064d4355
CS
209 struct listnode *nnode, *rnode, *rnext;
210 struct ospf6_nexthop *nh, *rnh;
211 char buf[PREFIX2STR_BUFFER];
212 bool route_found = false;
213
07b37f93
CS
214 /* check for old entry match with new route origin,
215 * delete old entry.
216 */
064d4355 217 for (old_route = old; old_route; old_route = old_route->next) {
07b37f93
CS
218 bool route_updated = false;
219
996c9314
LB
220 if (!ospf6_route_is_same(old_route, route)
221 || (old_route->path.type != route->path.type))
07b37f93
CS
222 continue;
223
224 /* Current and New route has same origin,
225 * delete old entry.
226 */
227 for (ALL_LIST_ELEMENTS(old_route->paths, anode, anext,
996c9314 228 o_path)) {
07b37f93
CS
229 /* Check old route path and route has same
230 * origin.
231 */
996c9314
LB
232 if (o_path->area_id != route->path.area_id
233 || (memcmp(&(o_path)->origin, &(route)->path.origin,
234 sizeof(struct ospf6_ls_origin))
235 != 0))
07b37f93
CS
236 continue;
237
238 /* Cost is not same then delete current path */
996c9314
LB
239 if ((o_path->cost == route->path.cost)
240 && (o_path->u.cost_e2 == route->path.u.cost_e2))
07b37f93 241 continue;
064d4355
CS
242
243 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
244 prefix2str(&old_route->prefix, buf,
245 sizeof(buf));
996c9314
LB
246 zlog_debug(
247 "%s: route %s cost old %u new %u is not same, replace route",
248 __PRETTY_FUNCTION__, buf, o_path->cost,
249 route->path.cost);
07b37f93
CS
250 }
251
252 /* Remove selected current rout path's nh from
253 * effective nh list.
254 */
255 for (ALL_LIST_ELEMENTS_RO(o_path->nh_list, nnode, nh)) {
256 for (ALL_LIST_ELEMENTS(old_route->nh_list,
996c9314 257 rnode, rnext, rnh)) {
07b37f93
CS
258 if (!ospf6_nexthop_is_same(rnh, nh))
259 continue;
260 listnode_delete(old_route->nh_list,
996c9314 261 rnh);
07b37f93
CS
262 ospf6_nexthop_delete(rnh);
263 route_updated = true;
264 }
265 }
266
267 listnode_delete(old_route->paths, o_path);
268 ospf6_path_free(o_path);
269
270 /* Current route's path (adv_router info) is similar
271 * to route being added.
272 * Replace current route's path with paths list head.
273 * Update FIB with effective NHs.
274 */
275 if (listcount(old_route->paths)) {
996c9314
LB
276 if (old_route->path.origin.id
277 == route->path.origin.id
278 && old_route->path.origin.adv_router
279 == route->path.origin
280 .adv_router) {
07b37f93
CS
281 struct ospf6_path *h_path;
282
283 h_path = (struct ospf6_path *)
996c9314
LB
284 listgetdata(listhead(
285 old_route->paths));
07b37f93
CS
286 old_route->path.origin.type =
287 h_path->origin.type;
288 old_route->path.origin.id =
289 h_path->origin.id;
290 old_route->path.origin.adv_router =
291 h_path->origin.adv_router;
292 }
293
294 if (route_updated) {
295 for (ALL_LIST_ELEMENTS(old_route->paths,
996c9314
LB
296 anode, anext,
297 o_path)) {
07b37f93
CS
298 ospf6_merge_nexthops(
299 old_route->nh_list,
300 o_path->nh_list);
301 }
302 /* Update RIB/FIB with effective
303 * nh_list
304 */
305 if (ospf6->route_table->hook_add)
996c9314
LB
306 (*ospf6->route_table->hook_add)(
307 old_route);
07b37f93
CS
308 break;
309 }
310 } else {
311 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
312 prefix2str(&old_route->prefix, buf,
313 sizeof(buf));
996c9314
LB
314 zlog_debug(
315 "%s: route %s old cost %u new cost %u, delete old entry.",
316 __PRETTY_FUNCTION__, buf,
317 old_route->path.cost,
318 route->path.cost);
07b37f93
CS
319 }
320 ospf6_route_remove(old_route,
321 ospf6->route_table);
322 break;
323 }
324 }
325 if (route_updated)
326 break;
327 }
328
329 /* Add new route */
330 for (old_route = old; old_route; old_route = old_route->next) {
331
332 /* Current and New Route prefix or route type
333 * is not same skip this current node.
334 */
996c9314
LB
335 if (!ospf6_route_is_same(old_route, route)
336 || (old_route->path.type != route->path.type))
07b37f93
CS
337 continue;
338
339 /* Old Route and New Route have Equal Cost, Merge NHs */
996c9314
LB
340 if ((old_route->path.cost == route->path.cost)
341 && (old_route->path.u.cost_e2 == route->path.u.cost_e2)) {
07b37f93
CS
342
343 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
344 prefix2str(&old_route->prefix, buf,
345 sizeof(buf));
996c9314
LB
346 zlog_debug(
347 "%s: old route %s path cost %u e2 %u",
348 __PRETTY_FUNCTION__, buf,
349 old_route->path.cost,
350 old_route->path.u.cost_e2);
064d4355
CS
351 }
352 route_found = true;
353 /* check if this path exists already in
354 * route->paths list, if so, replace nh_list
355 * from asbr_entry.
356 */
357 for (ALL_LIST_ELEMENTS_RO(old_route->paths, anode,
358 o_path)) {
996c9314
LB
359 if (o_path->area_id == route->path.area_id
360 && (memcmp(&(o_path)->origin,
361 &(route)->path.origin,
362 sizeof(struct ospf6_ls_origin))
363 == 0))
064d4355
CS
364 break;
365 }
366 /* If path is not found in old_route paths's list,
367 * add a new path to route paths list and merge
368 * nexthops in route->path->nh_list.
369 * Otherwise replace existing path's nh_list.
370 */
371 if (o_path == NULL) {
372 ecmp_path = ospf6_path_dup(&route->path);
373
374 /* Add a nh_list to new ecmp path */
375 ospf6_copy_nexthops(ecmp_path->nh_list,
376 route->nh_list);
377 /* Merge nexthop to existing route's nh_list */
378 ospf6_route_merge_nexthops(old_route, route);
379
380 /* Update RIB/FIB */
381 if (ospf6->route_table->hook_add)
996c9314
LB
382 (*ospf6->route_table->hook_add)(
383 old_route);
064d4355
CS
384
385 /* Add the new path to route's path list */
386 listnode_add_sort(old_route->paths, ecmp_path);
387
388 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
389 prefix2str(&route->prefix, buf,
390 sizeof(buf));
996c9314
LB
391 zlog_debug(
392 "%s: route %s another path added with nh %u, effective paths %u nh %u",
064d4355
CS
393 __PRETTY_FUNCTION__, buf,
394 listcount(ecmp_path->nh_list),
996c9314
LB
395 old_route->paths
396 ? listcount(
397 old_route
398 ->paths)
399 : 0,
07b37f93 400 listcount(old_route->nh_list));
064d4355
CS
401 }
402 } else {
403 for (ALL_LIST_ELEMENTS_RO(o_path->nh_list,
404 nnode, nh)) {
405 for (ALL_LIST_ELEMENTS(
996c9314
LB
406 old_route->nh_list, rnode,
407 rnext, rnh)) {
064d4355
CS
408 if (!ospf6_nexthop_is_same(rnh,
409 nh))
410 continue;
411
412 listnode_delete(
413 old_route->nh_list,
414 rnh);
415 ospf6_nexthop_delete(rnh);
416 }
417 }
418 list_delete_all_node(o_path->nh_list);
419 ospf6_copy_nexthops(o_path->nh_list,
996c9314 420 route->nh_list);
064d4355
CS
421
422 /* Merge nexthop to existing route's nh_list */
996c9314 423 ospf6_route_merge_nexthops(old_route, route);
064d4355
CS
424
425 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
996c9314
LB
426 prefix2str(&route->prefix, buf,
427 sizeof(buf));
428 zlog_debug(
429 "%s: existing route %s with effective nh count %u",
430 __PRETTY_FUNCTION__, buf,
431 old_route->nh_list
432 ? listcount(
433 old_route
434 ->nh_list)
435 : 0);
064d4355
CS
436 }
437
438 /* Update RIB/FIB */
439 if (ospf6->route_table->hook_add)
996c9314
LB
440 (*ospf6->route_table->hook_add)(
441 old_route);
064d4355
CS
442 }
443 /* Delete the new route its info added to existing
444 * route.
445 */
446 ospf6_route_delete(route);
07b37f93 447
064d4355
CS
448 break;
449 }
450 }
451
452 if (!route_found) {
453 /* Add new route to existing node in ospf6 route table. */
454 ospf6_route_add(route, ospf6->route_table);
455 }
456}
457
d62a17ae 458void ospf6_asbr_lsa_add(struct ospf6_lsa *lsa)
718e3744 459{
d62a17ae 460 struct ospf6_as_external_lsa *external;
461 struct prefix asbr_id;
064d4355
CS
462 struct ospf6_route *asbr_entry, *route, *old;
463 struct ospf6_path *path;
d62a17ae 464 char buf[PREFIX2STR_BUFFER];
465
466 external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
467 lsa->header);
468
469 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
470 zlog_debug("Calculate AS-External route for %s", lsa->name);
471
472 if (lsa->header->adv_router == ospf6->router_id) {
473 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
474 zlog_debug("Ignore self-originated AS-External-LSA");
475 return;
476 }
477
478 if (OSPF6_ASBR_METRIC(external) == OSPF_LS_INFINITY) {
479 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
480 zlog_debug("Ignore LSA with LSInfinity Metric");
481 return;
482 }
483
484 if (CHECK_FLAG(external->prefix.prefix_options,
485 OSPF6_PREFIX_OPTION_NU)) {
486 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
487 zlog_debug("Ignore LSA with NU bit set Metric");
488 return;
489 }
490
491 ospf6_linkstate_prefix(lsa->header->adv_router, htonl(0), &asbr_id);
492 asbr_entry = ospf6_route_lookup(&asbr_id, ospf6->brouter_table);
493 if (asbr_entry == NULL
494 || !CHECK_FLAG(asbr_entry->path.router_bits, OSPF6_ROUTER_BIT_E)) {
495 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
496 prefix2str(&asbr_id, buf, sizeof(buf));
497 zlog_debug("ASBR entry not found: %s", buf);
498 }
499 return;
500 }
501
502 route = ospf6_route_create();
503 route->type = OSPF6_DEST_TYPE_NETWORK;
504 route->prefix.family = AF_INET6;
505 route->prefix.prefixlen = external->prefix.prefix_length;
506 ospf6_prefix_in6_addr(&route->prefix.u.prefix6, &external->prefix);
507
508 route->path.area_id = asbr_entry->path.area_id;
509 route->path.origin.type = lsa->header->type;
510 route->path.origin.id = lsa->header->id;
511 route->path.origin.adv_router = lsa->header->adv_router;
512
513 route->path.prefix_options = external->prefix.prefix_options;
514 if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E)) {
515 route->path.type = OSPF6_PATH_TYPE_EXTERNAL2;
516 route->path.metric_type = 2;
517 route->path.cost = asbr_entry->path.cost;
518 route->path.u.cost_e2 = OSPF6_ASBR_METRIC(external);
519 } else {
520 route->path.type = OSPF6_PATH_TYPE_EXTERNAL1;
521 route->path.metric_type = 1;
522 route->path.cost =
523 asbr_entry->path.cost + OSPF6_ASBR_METRIC(external);
524 route->path.u.cost_e2 = 0;
525 }
526
527 route->path.tag = ospf6_as_external_lsa_get_tag(lsa);
528
529 ospf6_route_copy_nexthops(route, asbr_entry);
530
064d4355
CS
531 path = ospf6_path_dup(&route->path);
532 ospf6_copy_nexthops(path->nh_list, asbr_entry->nh_list);
533 listnode_add_sort(route->paths, path);
534
535
d62a17ae 536 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
537 prefix2str(&route->prefix, buf, sizeof(buf));
064d4355
CS
538 zlog_debug("%s: AS-External %u route add %s cost %u(%u) nh %u",
539 __PRETTY_FUNCTION__,
996c9314
LB
540 (route->path.type == OSPF6_PATH_TYPE_EXTERNAL1) ? 1
541 : 2,
542 buf, route->path.cost, route->path.u.cost_e2,
064d4355
CS
543 listcount(route->nh_list));
544 }
545
546 old = ospf6_route_lookup(&route->prefix, ospf6->route_table);
547 if (!old) {
548 /* Add the new route to ospf6 instance route table. */
549 ospf6_route_add(route, ospf6->route_table);
550 } else {
551 /* RFC 2328 16.4 (6)
552 * ECMP: Keep new equal preference path in current
553 * route's path list, update zebra with new effective
554 * list along with addition of ECMP path.
555 */
556 ospf6_asbr_update_route_ecmp_path(old, route);
d62a17ae 557 }
718e3744 558}
559
07b37f93
CS
560void ospf6_asbr_lsa_remove(struct ospf6_lsa *lsa,
561 struct ospf6_route *asbr_entry)
718e3744 562{
d62a17ae 563 struct ospf6_as_external_lsa *external;
564 struct prefix prefix;
07b37f93 565 struct ospf6_route *route, *nroute, *route_to_del;
d62a17ae 566 char buf[PREFIX2STR_BUFFER];
567
568 external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
569 lsa->header);
570
571 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
572 zlog_debug("Withdraw AS-External route for %s", lsa->name);
573
574 if (lsa->header->adv_router == ospf6->router_id) {
575 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL))
576 zlog_debug("Ignore self-originated AS-External-LSA");
577 return;
578 }
579
07b37f93
CS
580 route_to_del = ospf6_route_create();
581 route_to_del->type = OSPF6_DEST_TYPE_NETWORK;
582 route_to_del->prefix.family = AF_INET6;
583 route_to_del->prefix.prefixlen = external->prefix.prefix_length;
584 ospf6_prefix_in6_addr(&route_to_del->prefix.u.prefix6,
585 &external->prefix);
586
587 route_to_del->path.origin.type = lsa->header->type;
588 route_to_del->path.origin.id = lsa->header->id;
589 route_to_del->path.origin.adv_router = lsa->header->adv_router;
590
591 if (asbr_entry) {
592 route_to_del->path.area_id = asbr_entry->path.area_id;
593 if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E)) {
594 route_to_del->path.type = OSPF6_PATH_TYPE_EXTERNAL2;
595 route_to_del->path.metric_type = 2;
596 route_to_del->path.cost = asbr_entry->path.cost;
597 route_to_del->path.u.cost_e2 =
598 OSPF6_ASBR_METRIC(external);
599 } else {
600 route_to_del->path.type = OSPF6_PATH_TYPE_EXTERNAL1;
601 route_to_del->path.metric_type = 1;
996c9314
LB
602 route_to_del->path.cost = asbr_entry->path.cost
603 + OSPF6_ASBR_METRIC(external);
07b37f93
CS
604 route_to_del->path.u.cost_e2 = 0;
605 }
606 }
607
d62a17ae 608 memset(&prefix, 0, sizeof(struct prefix));
609 prefix.family = AF_INET6;
610 prefix.prefixlen = external->prefix.prefix_length;
611 ospf6_prefix_in6_addr(&prefix.u.prefix6, &external->prefix);
612
613 route = ospf6_route_lookup(&prefix, ospf6->route_table);
614 if (route == NULL) {
615 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
616 prefix2str(&prefix, buf, sizeof(buf));
617 zlog_debug("AS-External route %s not found", buf);
618 }
619 return;
620 }
621
07b37f93
CS
622 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
623 prefix2str(&prefix, buf, sizeof(buf));
996c9314
LB
624 zlog_debug(
625 "%s: Current route %s cost %u e2 %u, route to del cost %u e2 %u",
626 __PRETTY_FUNCTION__, buf, route->path.cost,
627 route->path.u.cost_e2, route_to_del->path.cost,
628 route_to_del->path.u.cost_e2);
07b37f93
CS
629 }
630
996c9314
LB
631 for (ospf6_route_lock(route);
632 route && ospf6_route_is_prefix(&prefix, route); route = nroute) {
d62a17ae 633 nroute = ospf6_route_next(route);
07b37f93 634
d62a17ae 635 if (route->type != OSPF6_DEST_TYPE_NETWORK)
636 continue;
064d4355 637
07b37f93
CS
638 /* Route has multiple ECMP paths, remove matching
639 * path. Update current route's effective nh list
640 * after removal of one of the path.
064d4355
CS
641 */
642 if (listcount(route->paths) > 1) {
643 struct listnode *anode, *anext;
644 struct listnode *nnode, *rnode, *rnext;
645 struct ospf6_nexthop *nh, *rnh;
646 struct ospf6_path *o_path;
647 bool nh_updated = false;
648
649 /* Iterate all paths of route to find maching with LSA
650 * remove from route path list. If route->path is same,
651 * replace from paths list.
652 */
653 for (ALL_LIST_ELEMENTS(route->paths, anode, anext,
996c9314 654 o_path)) {
07b37f93 655 if ((o_path->origin.type != lsa->header->type)
996c9314
LB
656 || (o_path->origin.adv_router
657 != lsa->header->adv_router)
658 || (o_path->origin.id != lsa->header->id))
064d4355 659 continue;
07b37f93
CS
660
661 /* Compare LSA cost with current
662 * route info.
663 */
996c9314
LB
664 if (!asbr_entry
665 && (o_path->cost != route_to_del->path.cost
666 || o_path->u.cost_e2
667 != route_to_del->path.u
668 .cost_e2)) {
07b37f93 669 if (IS_OSPF6_DEBUG_EXAMIN(
996c9314 670 AS_EXTERNAL)) {
07b37f93
CS
671 prefix2str(&prefix, buf,
672 sizeof(buf));
673 zlog_debug(
996c9314
LB
674 "%s: route %s to delete is not same, cost %u del cost %u. skip",
675 __PRETTY_FUNCTION__,
676 buf, route->path.cost,
677 route_to_del->path
678 .cost);
07b37f93 679 }
064d4355 680 continue;
07b37f93 681 }
064d4355 682
996c9314 683 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
064d4355
CS
684 prefix2str(&prefix, buf, sizeof(buf));
685 zlog_debug(
07b37f93 686 "%s: route %s path found with nh %u to remove.",
064d4355
CS
687 __PRETTY_FUNCTION__, buf,
688 listcount(o_path->nh_list));
689 }
690
691 /* Remove found path's nh_list from
692 * the route's nh_list.
693 */
694 for (ALL_LIST_ELEMENTS_RO(o_path->nh_list,
695 nnode, nh)) {
696 for (ALL_LIST_ELEMENTS(route->nh_list,
996c9314
LB
697 rnode, rnext,
698 rnh)) {
064d4355 699 if (!ospf6_nexthop_is_same(rnh,
996c9314 700 nh))
064d4355
CS
701 continue;
702 listnode_delete(route->nh_list,
703 rnh);
704 ospf6_nexthop_delete(rnh);
705 }
706 }
707 /* Delete the path from route's path list */
708 listnode_delete(route->paths, o_path);
709 ospf6_path_free(o_path);
710 nh_updated = true;
711 }
712
713 if (nh_updated) {
714 /* Iterate all paths and merge nexthop,
715 * unlesss any of the nexthop similar to
716 * ones deleted as part of path deletion.
717 */
718
719 for (ALL_LIST_ELEMENTS(route->paths, anode,
720 anext, o_path)) {
721 ospf6_merge_nexthops(route->nh_list,
722 o_path->nh_list);
723 }
724
725 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
726 prefix2str(&route->prefix, buf,
727 sizeof(buf));
996c9314
LB
728 zlog_debug(
729 "%s: AS-External %u route %s update paths %u nh %u",
730 __PRETTY_FUNCTION__,
731 (route->path.type
732 == OSPF6_PATH_TYPE_EXTERNAL1)
733 ? 1
734 : 2,
735 buf, listcount(route->paths),
736 listcount(route->nh_list));
064d4355
CS
737 }
738
07b37f93 739 /* Update RIB/FIB with effective nh_list */
064d4355
CS
740 if (ospf6->route_table->hook_add)
741 (*ospf6->route_table->hook_add)(route);
742
07b37f93
CS
743 /* route's primary path is similar to LSA,
744 * replace route's primary path with
745 * route's paths list head.
064d4355 746 */
996c9314
LB
747 if (route->path.origin.id == lsa->header->id
748 && route->path.origin.adv_router
749 == lsa->header->adv_router) {
064d4355
CS
750 struct ospf6_path *h_path;
751
752 h_path = (struct ospf6_path *)
996c9314
LB
753 listgetdata(
754 listhead(route->paths));
064d4355
CS
755 route->path.origin.type =
756 h_path->origin.type;
757 route->path.origin.id =
758 h_path->origin.id;
759 route->path.origin.adv_router =
760 h_path->origin.adv_router;
761 }
762 }
d62a17ae 763 continue;
764
064d4355 765 } else {
07b37f93
CS
766 /* Compare LSA origin and cost with current route info.
767 * if any check fails skip del this route node.
768 */
996c9314
LB
769 if (asbr_entry
770 && (!ospf6_route_is_same_origin(route, route_to_del)
771 || (route->path.type != route_to_del->path.type)
772 || (route->path.cost != route_to_del->path.cost)
773 || (route->path.u.cost_e2
774 != route_to_del->path.u.cost_e2))) {
07b37f93
CS
775 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
776 prefix2str(&prefix, buf, sizeof(buf));
996c9314
LB
777 zlog_debug(
778 "%s: route %s to delete is not same, cost %u del cost %u. skip",
779 __PRETTY_FUNCTION__, buf,
780 route->path.cost,
781 route_to_del->path.cost);
07b37f93 782 }
064d4355 783 continue;
07b37f93
CS
784 }
785
996c9314
LB
786 if ((route->path.origin.type != lsa->header->type)
787 || (route->path.origin.adv_router
788 != lsa->header->adv_router)
789 || (route->path.origin.id != lsa->header->id))
064d4355
CS
790 continue;
791 }
d62a17ae 792 if (IS_OSPF6_DEBUG_EXAMIN(AS_EXTERNAL)) {
793 prefix2str(&route->prefix, buf, sizeof(buf));
996c9314
LB
794 zlog_debug(
795 "%s: AS-External %u route remove %s cost %u(%u) nh %u",
796 __PRETTY_FUNCTION__,
797 route->path.type == OSPF6_PATH_TYPE_EXTERNAL1
798 ? 1
799 : 2,
800 buf, route->path.cost, route->path.u.cost_e2,
801 listcount(route->nh_list));
d62a17ae 802 }
803 ospf6_route_remove(route, ospf6->route_table);
804 }
805 if (route != NULL)
806 ospf6_route_unlock(route);
07b37f93
CS
807
808 ospf6_route_delete(route_to_del);
718e3744 809}
810
d62a17ae 811void ospf6_asbr_lsentry_add(struct ospf6_route *asbr_entry)
508e53e2 812{
d62a17ae 813 struct ospf6_lsa *lsa;
814 u_int16_t type;
815 u_int32_t router;
816
817 if (!CHECK_FLAG(asbr_entry->flag, OSPF6_ROUTE_BEST)) {
818 char buf[16];
819 inet_ntop(AF_INET, &ADV_ROUTER_IN_PREFIX(&asbr_entry->prefix),
820 buf, sizeof(buf));
821 zlog_info("ignore non-best path: lsentry %s add", buf);
822 return;
823 }
824
825 type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
826 router = ospf6_linkstate_prefix_adv_router(&asbr_entry->prefix);
827 for (ALL_LSDB_TYPED_ADVRTR(ospf6->lsdb, type, router, lsa)) {
828 if (!OSPF6_LSA_IS_MAXAGE(lsa))
829 ospf6_asbr_lsa_add(lsa);
830 }
508e53e2 831}
832
d62a17ae 833void ospf6_asbr_lsentry_remove(struct ospf6_route *asbr_entry)
718e3744 834{
d62a17ae 835 struct ospf6_lsa *lsa;
836 u_int16_t type;
837 u_int32_t router;
838
839 type = htons(OSPF6_LSTYPE_AS_EXTERNAL);
840 router = ospf6_linkstate_prefix_adv_router(&asbr_entry->prefix);
841 for (ALL_LSDB_TYPED_ADVRTR(ospf6->lsdb, type, router, lsa))
07b37f93 842 ospf6_asbr_lsa_remove(lsa, asbr_entry);
508e53e2 843}
718e3744 844
508e53e2 845
508e53e2 846/* redistribute function */
847
d62a17ae 848static void ospf6_asbr_routemap_set(int type, const char *mapname)
508e53e2 849{
d62a17ae 850 if (ospf6->rmap[type].name)
851 free(ospf6->rmap[type].name);
852 ospf6->rmap[type].name = strdup(mapname);
853 ospf6->rmap[type].map = route_map_lookup_by_name(mapname);
718e3744 854}
855
d62a17ae 856static void ospf6_asbr_routemap_unset(int type)
718e3744 857{
d62a17ae 858 if (ospf6->rmap[type].name)
859 free(ospf6->rmap[type].name);
860 ospf6->rmap[type].name = NULL;
861 ospf6->rmap[type].map = NULL;
508e53e2 862}
863
856ae1eb
CS
864static int ospf6_asbr_routemap_update_timer(struct thread *thread)
865{
866 void **arg;
867 int arg_type;
868
869 arg = THREAD_ARG(thread);
870 arg_type = (int)(intptr_t)arg[1];
871
872 ospf6->t_distribute_update = NULL;
873
874 if (ospf6->rmap[arg_type].name)
996c9314
LB
875 ospf6->rmap[arg_type].map =
876 route_map_lookup_by_name(ospf6->rmap[arg_type].name);
856ae1eb
CS
877 if (ospf6->rmap[arg_type].map) {
878 if (IS_OSPF6_DEBUG_ASBR)
879 zlog_debug("%s: route-map %s update, reset redist %s",
880 __PRETTY_FUNCTION__,
881 ospf6->rmap[arg_type].name,
882 ZROUTE_NAME(arg_type));
883
884 ospf6_zebra_no_redistribute(arg_type);
885 ospf6_zebra_redistribute(arg_type);
886 }
887
888 XFREE(MTYPE_OSPF6_DIST_ARGS, arg);
889 return 0;
890}
891
892void ospf6_asbr_distribute_list_update(int type)
893{
894 void **args = NULL;
895
896 if (ospf6->t_distribute_update)
897 return;
898
996c9314 899 args = XCALLOC(MTYPE_OSPF6_DIST_ARGS, sizeof(void *) * 2);
856ae1eb
CS
900
901 args[0] = ospf6;
902 args[1] = (void *)((ptrdiff_t)type);
903
904 if (IS_OSPF6_DEBUG_ASBR)
905 zlog_debug("%s: trigger redistribute %s reset thread",
906 __PRETTY_FUNCTION__, ZROUTE_NAME(type));
907
908 ospf6->t_distribute_update = NULL;
909 thread_add_timer_msec(master, ospf6_asbr_routemap_update_timer,
910 (void **)args, OSPF_MIN_LS_INTERVAL,
911 &ospf6->t_distribute_update);
912}
913
d62a17ae 914static void ospf6_asbr_routemap_update(const char *mapname)
508e53e2 915{
d62a17ae 916 int type;
917
918 if (ospf6 == NULL)
919 return;
920
921 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
c600ce45 922 if (ospf6->rmap[type].name) {
d62a17ae 923 ospf6->rmap[type].map = route_map_lookup_by_name(
924 ospf6->rmap[type].name);
c600ce45 925
996c9314
LB
926 if (mapname && ospf6->rmap[type].map
927 && (strcmp(ospf6->rmap[type].name, mapname) == 0)) {
c600ce45 928 if (IS_OSPF6_DEBUG_ASBR)
996c9314
LB
929 zlog_debug(
930 "%s: route-map %s update, reset redist %s",
931 __PRETTY_FUNCTION__, mapname,
932 ZROUTE_NAME(type));
856ae1eb 933 ospf6_asbr_distribute_list_update(type);
c600ce45
CS
934 }
935 } else
d62a17ae 936 ospf6->rmap[type].map = NULL;
937 }
718e3744 938}
939
856ae1eb
CS
940static void ospf6_asbr_routemap_event(route_map_event_t event, const char *name)
941{
942 int type;
943
944 if (ospf6 == NULL)
945 return;
946 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
996c9314
LB
947 if ((ospf6->rmap[type].name)
948 && (strcmp(ospf6->rmap[type].name, name) == 0)) {
856ae1eb
CS
949 ospf6_asbr_distribute_list_update(type);
950 }
951 }
952}
953
d62a17ae 954int ospf6_asbr_is_asbr(struct ospf6 *o)
508e53e2 955{
d62a17ae 956 return o->external_table->count;
508e53e2 957}
958
d62a17ae 959static void ospf6_asbr_redistribute_set(int type)
718e3744 960{
d62a17ae 961 ospf6_zebra_redistribute(type);
508e53e2 962}
963
d62a17ae 964static void ospf6_asbr_redistribute_unset(int type)
508e53e2 965{
d62a17ae 966 struct ospf6_route *route;
967 struct ospf6_external_info *info;
718e3744 968
d62a17ae 969 ospf6_zebra_no_redistribute(type);
508e53e2 970
d62a17ae 971 for (route = ospf6_route_head(ospf6->external_table); route;
972 route = ospf6_route_next(route)) {
973 info = route->route_option;
974 if (info->type != type)
975 continue;
718e3744 976
d62a17ae 977 ospf6_asbr_redistribute_remove(info->type, 0, &route->prefix);
978 }
d9628728 979
d62a17ae 980 ospf6_asbr_routemap_unset(type);
508e53e2 981}
718e3744 982
ca1f4309 983/* When an area is unstubified, flood all the external LSAs in the area */
d62a17ae 984void ospf6_asbr_send_externals_to_area(struct ospf6_area *oa)
ca1f4309 985{
d62a17ae 986 struct ospf6_lsa *lsa;
987
988 for (ALL_LSDB(oa->ospf6->lsdb, lsa)) {
989 if (ntohs(lsa->header->type) == OSPF6_LSTYPE_AS_EXTERNAL) {
990 zlog_debug("%s: Flooding AS-External LSA %s\n",
991 __func__, lsa->name);
992 ospf6_flood_area(NULL, lsa, oa);
993 }
ca1f4309 994 }
ca1f4309
DS
995}
996
d62a17ae 997void ospf6_asbr_redistribute_add(int type, ifindex_t ifindex,
998 struct prefix *prefix, u_int nexthop_num,
999 struct in6_addr *nexthop, route_tag_t tag)
508e53e2 1000{
d62a17ae 1001 int ret;
1002 struct ospf6_route troute;
1003 struct ospf6_external_info tinfo;
1004 struct ospf6_route *route, *match;
1005 struct ospf6_external_info *info;
1006 struct prefix prefix_id;
1007 struct route_node *node;
1008 char pbuf[PREFIX2STR_BUFFER], ibuf[16];
1009 struct listnode *lnode, *lnnode;
1010 struct ospf6_area *oa;
1011
1012 if (!ospf6_zebra_is_redistribute(type))
1013 return;
1014
6e38a9ec
VJ
1015 memset(&troute, 0, sizeof(troute));
1016 memset(&tinfo, 0, sizeof(tinfo));
1017
d62a17ae 1018 if (IS_OSPF6_DEBUG_ASBR) {
1019 prefix2str(prefix, pbuf, sizeof(pbuf));
1020 zlog_debug("Redistribute %s (%s)", pbuf, ZROUTE_NAME(type));
1021 }
1022
1023 /* if route-map was specified but not found, do not advertise */
1024 if (ospf6->rmap[type].name) {
1025 if (ospf6->rmap[type].map == NULL)
1026 ospf6_asbr_routemap_update(NULL);
1027 if (ospf6->rmap[type].map == NULL) {
1028 zlog_warn(
1029 "route-map \"%s\" not found, suppress redistributing",
1030 ospf6->rmap[type].name);
1031 return;
1032 }
1033 }
1034
1035 /* apply route-map */
1036 if (ospf6->rmap[type].map) {
d62a17ae 1037 troute.route_option = &tinfo;
1038 tinfo.ifindex = ifindex;
1039 tinfo.tag = tag;
1040
1041 ret = route_map_apply(ospf6->rmap[type].map, prefix, RMAP_OSPF6,
1042 &troute);
1043 if (ret == RMAP_DENYMATCH) {
1044 if (IS_OSPF6_DEBUG_ASBR)
1045 zlog_debug("Denied by route-map \"%s\"",
1046 ospf6->rmap[type].name);
1047 return;
1048 }
1049 }
1050
1051 match = ospf6_route_lookup(prefix, ospf6->external_table);
1052 if (match) {
1053 info = match->route_option;
d62a17ae 1054 /* copy result of route-map */
1055 if (ospf6->rmap[type].map) {
1056 if (troute.path.metric_type)
1057 match->path.metric_type =
1058 troute.path.metric_type;
1059 if (troute.path.cost)
1060 match->path.cost = troute.path.cost;
1061 if (!IN6_IS_ADDR_UNSPECIFIED(&tinfo.forwarding))
1062 memcpy(&info->forwarding, &tinfo.forwarding,
1063 sizeof(struct in6_addr));
1064 info->tag = tinfo.tag;
1065 } else {
1066 /* If there is no route-map, simply update the tag */
1067 info->tag = tag;
1068 }
1069
1070 info->type = type;
1071
1072 if (nexthop_num && nexthop)
1073 ospf6_route_add_nexthop(match, ifindex, nexthop);
1074 else
1075 ospf6_route_add_nexthop(match, ifindex, NULL);
1076
1077 /* create/update binding in external_id_table */
1078 prefix_id.family = AF_INET;
1079 prefix_id.prefixlen = 32;
1080 prefix_id.u.prefix4.s_addr = htonl(info->id);
1081 node = route_node_get(ospf6->external_id_table, &prefix_id);
1082 node->info = match;
1083
1084 if (IS_OSPF6_DEBUG_ASBR) {
1085 inet_ntop(AF_INET, &prefix_id.u.prefix4, ibuf,
1086 sizeof(ibuf));
856ae1eb 1087 prefix2str(prefix, pbuf, sizeof(pbuf));
996c9314
LB
1088 zlog_debug(
1089 "Advertise as AS-External Id:%s prefix %s metric %u",
1090 ibuf, pbuf, match->path.metric_type);
d62a17ae 1091 }
1092
1093 match->path.origin.id = htonl(info->id);
1094 ospf6_as_external_lsa_originate(match);
1095 return;
1096 }
1097
1098 /* create new entry */
1099 route = ospf6_route_create();
1100 route->type = OSPF6_DEST_TYPE_NETWORK;
1101 memcpy(&route->prefix, prefix, sizeof(struct prefix));
1102
1103 info = (struct ospf6_external_info *)XCALLOC(
1104 MTYPE_OSPF6_EXTERNAL_INFO, sizeof(struct ospf6_external_info));
1105 route->route_option = info;
1106 info->id = ospf6->external_id++;
1107
1108 /* copy result of route-map */
1109 if (ospf6->rmap[type].map) {
1110 if (troute.path.metric_type)
1111 route->path.metric_type = troute.path.metric_type;
1112 if (troute.path.cost)
1113 route->path.cost = troute.path.cost;
1114 if (!IN6_IS_ADDR_UNSPECIFIED(&tinfo.forwarding))
1115 memcpy(&info->forwarding, &tinfo.forwarding,
1116 sizeof(struct in6_addr));
1117 info->tag = tinfo.tag;
1118 } else {
1119 /* If there is no route-map, simply set the tag */
1120 info->tag = tag;
1121 }
1122
1123 info->type = type;
1124 if (nexthop_num && nexthop)
1125 ospf6_route_add_nexthop(route, ifindex, nexthop);
1126 else
1127 ospf6_route_add_nexthop(route, ifindex, NULL);
1128
1129 /* create/update binding in external_id_table */
1130 prefix_id.family = AF_INET;
1131 prefix_id.prefixlen = 32;
1132 prefix_id.u.prefix4.s_addr = htonl(info->id);
1133 node = route_node_get(ospf6->external_id_table, &prefix_id);
1134 node->info = route;
1135
1136 route = ospf6_route_add(route, ospf6->external_table);
1137 route->route_option = info;
1138
1139 if (IS_OSPF6_DEBUG_ASBR) {
1140 inet_ntop(AF_INET, &prefix_id.u.prefix4, ibuf, sizeof(ibuf));
856ae1eb
CS
1141 prefix2str(prefix, pbuf, sizeof(pbuf));
1142 zlog_debug("Advertise as AS-External Id:%s prefix %s metric %u",
1143 ibuf, pbuf, route->path.metric_type);
d62a17ae 1144 }
1145
1146 route->path.origin.id = htonl(info->id);
1147 ospf6_as_external_lsa_originate(route);
1148
1149 /* Router-Bit (ASBR Flag) may have to be updated */
1150 for (ALL_LIST_ELEMENTS(ospf6->area_list, lnode, lnnode, oa))
1151 OSPF6_ROUTER_LSA_SCHEDULE(oa);
718e3744 1152}
1153
d62a17ae 1154void ospf6_asbr_redistribute_remove(int type, ifindex_t ifindex,
1155 struct prefix *prefix)
718e3744 1156{
d62a17ae 1157 struct ospf6_route *match;
1158 struct ospf6_external_info *info = NULL;
1159 struct route_node *node;
1160 struct ospf6_lsa *lsa;
1161 struct prefix prefix_id;
1162 char pbuf[PREFIX2STR_BUFFER], ibuf[16];
1163 struct listnode *lnode, *lnnode;
1164 struct ospf6_area *oa;
1165
1166 match = ospf6_route_lookup(prefix, ospf6->external_table);
1167 if (match == NULL) {
1168 if (IS_OSPF6_DEBUG_ASBR) {
1169 prefix2str(prefix, pbuf, sizeof(pbuf));
1170 zlog_debug("No such route %s to withdraw", pbuf);
1171 }
1172 return;
1173 }
1174
1175 info = match->route_option;
1176 assert(info);
1177
1178 if (info->type != type) {
1179 if (IS_OSPF6_DEBUG_ASBR) {
1180 prefix2str(prefix, pbuf, sizeof(pbuf));
1181 zlog_debug("Original protocol mismatch: %s", pbuf);
1182 }
1183 return;
1184 }
1185
1186 if (IS_OSPF6_DEBUG_ASBR) {
1187 prefix2str(prefix, pbuf, sizeof(pbuf));
1188 inet_ntop(AF_INET, &prefix_id.u.prefix4, ibuf, sizeof(ibuf));
1189 zlog_debug("Withdraw %s (AS-External Id:%s)", pbuf, ibuf);
1190 }
1191
1192 lsa = ospf6_lsdb_lookup(htons(OSPF6_LSTYPE_AS_EXTERNAL),
1193 htonl(info->id), ospf6->router_id, ospf6->lsdb);
1194 if (lsa)
1195 ospf6_lsa_purge(lsa);
1196
1197 /* remove binding in external_id_table */
1198 prefix_id.family = AF_INET;
1199 prefix_id.prefixlen = 32;
1200 prefix_id.u.prefix4.s_addr = htonl(info->id);
1201 node = route_node_lookup(ospf6->external_id_table, &prefix_id);
1202 assert(node);
1203 node->info = NULL;
d107621d
CS
1204 route_unlock_node(node); /* to free the lookup lock */
1205 route_unlock_node(node); /* to free the original lock */
d62a17ae 1206
1207 ospf6_route_remove(match, ospf6->external_table);
1208 XFREE(MTYPE_OSPF6_EXTERNAL_INFO, info);
1209
1210 /* Router-Bit (ASBR Flag) may have to be updated */
1211 for (ALL_LIST_ELEMENTS(ospf6->area_list, lnode, lnnode, oa))
1212 OSPF6_ROUTER_LSA_SCHEDULE(oa);
718e3744 1213}
1214
508e53e2 1215DEFUN (ospf6_redistribute,
1216 ospf6_redistribute_cmd,
40d1cbfb 1217 "redistribute " FRR_REDIST_STR_OSPF6D,
508e53e2 1218 "Redistribute\n"
3b14d86e 1219 FRR_REDIST_HELP_STR_OSPF6D)
718e3744 1220{
d62a17ae 1221 int type;
e0ca5fde 1222
d62a17ae 1223 char *proto = argv[argc - 1]->text;
1224 type = proto_redistnum(AFI_IP6, proto);
1225 if (type < 0)
1226 return CMD_WARNING_CONFIG_FAILED;
718e3744 1227
d62a17ae 1228 ospf6_asbr_redistribute_unset(type);
1229 ospf6_asbr_redistribute_set(type);
1230 return CMD_SUCCESS;
508e53e2 1231}
718e3744 1232
508e53e2 1233DEFUN (ospf6_redistribute_routemap,
1234 ospf6_redistribute_routemap_cmd,
40d1cbfb 1235 "redistribute " FRR_REDIST_STR_OSPF6D " route-map WORD",
508e53e2 1236 "Redistribute\n"
ab0181ee 1237 FRR_REDIST_HELP_STR_OSPF6D
508e53e2 1238 "Route map reference\n"
e52702f2 1239 "Route map name\n")
508e53e2 1240{
d62a17ae 1241 int idx_protocol = 1;
1242 int idx_word = 3;
1243 int type;
1244
1245 char *proto = argv[idx_protocol]->text;
1246 type = proto_redistnum(AFI_IP6, proto);
1247 if (type < 0)
1248 return CMD_WARNING_CONFIG_FAILED;
1249
1250 ospf6_asbr_redistribute_unset(type);
1251 ospf6_asbr_routemap_set(type, argv[idx_word]->arg);
1252 ospf6_asbr_redistribute_set(type);
1253 return CMD_SUCCESS;
508e53e2 1254}
1255
1256DEFUN (no_ospf6_redistribute,
1257 no_ospf6_redistribute_cmd,
40d1cbfb 1258 "no redistribute " FRR_REDIST_STR_OSPF6D " [route-map WORD]",
508e53e2 1259 NO_STR
1260 "Redistribute\n"
ab0181ee 1261 FRR_REDIST_HELP_STR_OSPF6D
1d68dbfe
DW
1262 "Route map reference\n"
1263 "Route map name\n")
508e53e2 1264{
d62a17ae 1265 int idx_protocol = 2;
1266 int type;
e0ca5fde 1267
d62a17ae 1268 char *proto = argv[idx_protocol]->text;
1269 type = proto_redistnum(AFI_IP6, proto);
1270 if (type < 0)
1271 return CMD_WARNING_CONFIG_FAILED;
e26bbeba 1272
d62a17ae 1273 ospf6_asbr_redistribute_unset(type);
e26bbeba 1274
d62a17ae 1275 return CMD_SUCCESS;
508e53e2 1276}
718e3744 1277
d62a17ae 1278int ospf6_redistribute_config_write(struct vty *vty)
508e53e2 1279{
d62a17ae 1280 int type;
1281
1282 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
1283 if (type == ZEBRA_ROUTE_OSPF6)
1284 continue;
1285 if (!ospf6_zebra_is_redistribute(type))
1286 continue;
1287
1288 if (ospf6->rmap[type].name)
1289 vty_out(vty, " redistribute %s route-map %s\n",
1290 ZROUTE_NAME(type), ospf6->rmap[type].name);
1291 else
1292 vty_out(vty, " redistribute %s\n", ZROUTE_NAME(type));
1293 }
1294
1295 return 0;
718e3744 1296}
1297
d62a17ae 1298static void ospf6_redistribute_show_config(struct vty *vty)
718e3744 1299{
d62a17ae 1300 int type;
1301 int nroute[ZEBRA_ROUTE_MAX];
1302 int total;
1303 struct ospf6_route *route;
1304 struct ospf6_external_info *info;
1305
1306 total = 0;
1307 for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
1308 nroute[type] = 0;
1309 for (route = ospf6_route_head(ospf6->external_table); route;
1310 route = ospf6_route_next(route)) {
1311 info = route->route_option;
1312 nroute[info->type]++;
1313 total++;
1314 }
718e3744 1315
d62a17ae 1316 vty_out(vty, "Redistributing External Routes from:\n");
1317 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
1318 if (type == ZEBRA_ROUTE_OSPF6)
1319 continue;
1320 if (!ospf6_zebra_is_redistribute(type))
1321 continue;
1322
1323 if (ospf6->rmap[type].name)
1324 vty_out(vty, " %d: %s with route-map \"%s\"%s\n",
1325 nroute[type], ZROUTE_NAME(type),
1326 ospf6->rmap[type].name,
1327 (ospf6->rmap[type].map ? ""
1328 : " (not found !)"));
1329 else
1330 vty_out(vty, " %d: %s\n", nroute[type],
1331 ZROUTE_NAME(type));
1332 }
1333 vty_out(vty, "Total %d routes\n", total);
1334}
718e3744 1335
6b0655a2 1336
508e53e2 1337/* Routemap Functions */
6ac29a51 1338static route_map_result_t
d62a17ae 1339ospf6_routemap_rule_match_address_prefixlist(void *rule, struct prefix *prefix,
1340 route_map_object_t type,
1341 void *object)
508e53e2 1342{
d62a17ae 1343 struct prefix_list *plist;
718e3744 1344
d62a17ae 1345 if (type != RMAP_OSPF6)
1346 return RMAP_NOMATCH;
718e3744 1347
d62a17ae 1348 plist = prefix_list_lookup(AFI_IP6, (char *)rule);
1349 if (plist == NULL)
1350 return RMAP_NOMATCH;
718e3744 1351
d62a17ae 1352 return (prefix_list_apply(plist, prefix) == PREFIX_DENY ? RMAP_NOMATCH
1353 : RMAP_MATCH);
508e53e2 1354}
718e3744 1355
6ac29a51 1356static void *
d62a17ae 1357ospf6_routemap_rule_match_address_prefixlist_compile(const char *arg)
508e53e2 1358{
d62a17ae 1359 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 1360}
1361
d62a17ae 1362static void ospf6_routemap_rule_match_address_prefixlist_free(void *rule)
718e3744 1363{
d62a17ae 1364 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
508e53e2 1365}
718e3744 1366
d62a17ae 1367struct route_map_rule_cmd ospf6_routemap_rule_match_address_prefixlist_cmd = {
1368 "ipv6 address prefix-list",
1369 ospf6_routemap_rule_match_address_prefixlist,
1370 ospf6_routemap_rule_match_address_prefixlist_compile,
1371 ospf6_routemap_rule_match_address_prefixlist_free,
508e53e2 1372};
718e3744 1373
42a7debf
VT
1374/* `match interface IFNAME' */
1375/* Match function should return 1 if match is success else return
1376 zero. */
1377static route_map_result_t
d62a17ae 1378ospf6_routemap_rule_match_interface(void *rule, struct prefix *prefix,
1379 route_map_object_t type, void *object)
42a7debf 1380{
d62a17ae 1381 struct interface *ifp;
1382 struct ospf6_external_info *ei;
42a7debf 1383
d62a17ae 1384 if (type == RMAP_OSPF6) {
1385 ei = ((struct ospf6_route *)object)->route_option;
1386 ifp = if_lookup_by_name((char *)rule, VRF_DEFAULT);
42a7debf 1387
d62a17ae 1388 if (ifp != NULL && ei->ifindex == ifp->ifindex)
1389 return RMAP_MATCH;
1390 }
42a7debf 1391
d62a17ae 1392 return RMAP_NOMATCH;
42a7debf
VT
1393}
1394
1395/* Route map `interface' match statement. `arg' should be
1396 interface name. */
d62a17ae 1397static void *ospf6_routemap_rule_match_interface_compile(const char *arg)
42a7debf 1398{
d62a17ae 1399 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
42a7debf
VT
1400}
1401
1402/* Free route map's compiled `interface' value. */
d62a17ae 1403static void ospf6_routemap_rule_match_interface_free(void *rule)
42a7debf 1404{
d62a17ae 1405 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
42a7debf
VT
1406}
1407
1408/* Route map commands for interface matching. */
d62a17ae 1409struct route_map_rule_cmd ospf6_routemap_rule_match_interface_cmd = {
1410 "interface", ospf6_routemap_rule_match_interface,
1411 ospf6_routemap_rule_match_interface_compile,
1412 ospf6_routemap_rule_match_interface_free};
42a7debf 1413
464015fa 1414/* Match function for matching route tags */
d62a17ae 1415static route_map_result_t ospf6_routemap_rule_match_tag(void *rule,
1416 struct prefix *prefix,
1417 route_map_object_t type,
1418 void *object)
464015fa 1419{
d62a17ae 1420 route_tag_t *tag = rule;
1421 struct ospf6_route *route = object;
1422 struct ospf6_external_info *info = route->route_option;
464015fa 1423
d62a17ae 1424 if (type == RMAP_OSPF6 && info->tag == *tag)
1425 return RMAP_MATCH;
464015fa 1426
d62a17ae 1427 return RMAP_NOMATCH;
464015fa
CF
1428}
1429
d62a17ae 1430static struct route_map_rule_cmd ospf6_routemap_rule_match_tag_cmd = {
9d303b37 1431 "tag", ospf6_routemap_rule_match_tag, route_map_rule_tag_compile,
d62a17ae 1432 route_map_rule_tag_free,
464015fa
CF
1433};
1434
6ac29a51 1435static route_map_result_t
d62a17ae 1436ospf6_routemap_rule_set_metric_type(void *rule, struct prefix *prefix,
1437 route_map_object_t type, void *object)
508e53e2 1438{
d62a17ae 1439 char *metric_type = rule;
1440 struct ospf6_route *route = object;
718e3744 1441
d62a17ae 1442 if (type != RMAP_OSPF6)
1443 return RMAP_OKAY;
718e3744 1444
d62a17ae 1445 if (strcmp(metric_type, "type-2") == 0)
1446 route->path.metric_type = 2;
1447 else
1448 route->path.metric_type = 1;
718e3744 1449
d62a17ae 1450 return RMAP_OKAY;
508e53e2 1451}
718e3744 1452
d62a17ae 1453static void *ospf6_routemap_rule_set_metric_type_compile(const char *arg)
508e53e2 1454{
d62a17ae 1455 if (strcmp(arg, "type-2") && strcmp(arg, "type-1"))
1456 return NULL;
1457 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 1458}
1459
d62a17ae 1460static void ospf6_routemap_rule_set_metric_type_free(void *rule)
718e3744 1461{
d62a17ae 1462 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
508e53e2 1463}
718e3744 1464
d62a17ae 1465struct route_map_rule_cmd ospf6_routemap_rule_set_metric_type_cmd = {
9d303b37 1466 "metric-type", ospf6_routemap_rule_set_metric_type,
d62a17ae 1467 ospf6_routemap_rule_set_metric_type_compile,
1468 ospf6_routemap_rule_set_metric_type_free,
508e53e2 1469};
718e3744 1470
6ac29a51 1471static route_map_result_t
d62a17ae 1472ospf6_routemap_rule_set_metric(void *rule, struct prefix *prefix,
1473 route_map_object_t type, void *object)
508e53e2 1474{
d62a17ae 1475 char *metric = rule;
1476 struct ospf6_route *route = object;
718e3744 1477
d62a17ae 1478 if (type != RMAP_OSPF6)
1479 return RMAP_OKAY;
718e3744 1480
d62a17ae 1481 route->path.cost = atoi(metric);
1482 return RMAP_OKAY;
508e53e2 1483}
718e3744 1484
d62a17ae 1485static void *ospf6_routemap_rule_set_metric_compile(const char *arg)
508e53e2 1486{
d62a17ae 1487 u_int32_t metric;
1488 char *endp;
1489 metric = strtoul(arg, &endp, 0);
1490 if (metric > OSPF_LS_INFINITY || *endp != '\0')
1491 return NULL;
1492 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 1493}
1494
d62a17ae 1495static void ospf6_routemap_rule_set_metric_free(void *rule)
718e3744 1496{
d62a17ae 1497 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
508e53e2 1498}
718e3744 1499
d62a17ae 1500struct route_map_rule_cmd ospf6_routemap_rule_set_metric_cmd = {
9d303b37 1501 "metric", ospf6_routemap_rule_set_metric,
d62a17ae 1502 ospf6_routemap_rule_set_metric_compile,
1503 ospf6_routemap_rule_set_metric_free,
508e53e2 1504};
718e3744 1505
6ac29a51 1506static route_map_result_t
d62a17ae 1507ospf6_routemap_rule_set_forwarding(void *rule, struct prefix *prefix,
1508 route_map_object_t type, void *object)
508e53e2 1509{
d62a17ae 1510 char *forwarding = rule;
1511 struct ospf6_route *route = object;
1512 struct ospf6_external_info *info = route->route_option;
718e3744 1513
d62a17ae 1514 if (type != RMAP_OSPF6)
1515 return RMAP_OKAY;
718e3744 1516
d62a17ae 1517 if (inet_pton(AF_INET6, forwarding, &info->forwarding) != 1) {
1518 memset(&info->forwarding, 0, sizeof(struct in6_addr));
1519 return RMAP_ERROR;
1520 }
718e3744 1521
d62a17ae 1522 return RMAP_OKAY;
718e3744 1523}
1524
d62a17ae 1525static void *ospf6_routemap_rule_set_forwarding_compile(const char *arg)
718e3744 1526{
d62a17ae 1527 struct in6_addr a;
1528 if (inet_pton(AF_INET6, arg, &a) != 1)
1529 return NULL;
1530 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 1531}
1532
d62a17ae 1533static void ospf6_routemap_rule_set_forwarding_free(void *rule)
718e3744 1534{
d62a17ae 1535 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
508e53e2 1536}
718e3744 1537
d62a17ae 1538struct route_map_rule_cmd ospf6_routemap_rule_set_forwarding_cmd = {
9d303b37 1539 "forwarding-address", ospf6_routemap_rule_set_forwarding,
d62a17ae 1540 ospf6_routemap_rule_set_forwarding_compile,
1541 ospf6_routemap_rule_set_forwarding_free,
508e53e2 1542};
718e3744 1543
d62a17ae 1544static route_map_result_t ospf6_routemap_rule_set_tag(void *rule,
1545 struct prefix *prefix,
1546 route_map_object_t type,
1547 void *object)
464015fa 1548{
d62a17ae 1549 route_tag_t *tag = rule;
1550 struct ospf6_route *route = object;
1551 struct ospf6_external_info *info = route->route_option;
464015fa 1552
d62a17ae 1553 if (type != RMAP_OSPF6)
1554 return RMAP_OKAY;
464015fa 1555
d62a17ae 1556 info->tag = *tag;
1557 return RMAP_OKAY;
464015fa
CF
1558}
1559
d62a17ae 1560static struct route_map_rule_cmd ospf6_routemap_rule_set_tag_cmd = {
9d303b37 1561 "tag", ospf6_routemap_rule_set_tag, route_map_rule_tag_compile,
d62a17ae 1562 route_map_rule_tag_free,
464015fa
CF
1563};
1564
d62a17ae 1565static int route_map_command_status(struct vty *vty, int ret)
508e53e2 1566{
d62a17ae 1567 switch (ret) {
1568 case RMAP_RULE_MISSING:
1569 vty_out(vty, "OSPF6 Can't find rule.\n");
9ca25fed 1570 return CMD_WARNING_CONFIG_FAILED;
d62a17ae 1571 break;
1572 case RMAP_COMPILE_ERROR:
1573 vty_out(vty, "OSPF6 Argument is malformed.\n");
9ca25fed 1574 return CMD_WARNING_CONFIG_FAILED;
d62a17ae 1575 break;
9ca25fed 1576 case RMAP_COMPILE_SUCCESS:
d62a17ae 1577 break;
1578 }
9ca25fed
DS
1579
1580 return CMD_SUCCESS;
508e53e2 1581}
718e3744 1582
508e53e2 1583/* add "set metric-type" */
1584DEFUN (ospf6_routemap_set_metric_type,
1585 ospf6_routemap_set_metric_type_cmd,
6147e2c6 1586 "set metric-type <type-1|type-2>",
508e53e2 1587 "Set value\n"
1588 "Type of metric\n"
1589 "OSPF6 external type 1 metric\n"
1590 "OSPF6 external type 2 metric\n")
1591{
d62a17ae 1592 VTY_DECLVAR_CONTEXT(route_map_index, route_map_index);
1593 int idx_external = 2;
1594 int ret = route_map_add_set(route_map_index, "metric-type",
1595 argv[idx_external]->arg);
1596 return route_map_command_status(vty, ret);
718e3744 1597}
1598
508e53e2 1599/* delete "set metric-type" */
1600DEFUN (ospf6_routemap_no_set_metric_type,
1601 ospf6_routemap_no_set_metric_type_cmd,
dab8cd00 1602 "no set metric-type [<type-1|type-2>]",
508e53e2 1603 NO_STR
1604 "Set value\n"
1605 "Type of metric\n"
1606 "OSPF6 external type 1 metric\n"
1607 "OSPF6 external type 2 metric\n")
718e3744 1608{
d62a17ae 1609 VTY_DECLVAR_CONTEXT(route_map_index, route_map_index);
1610 char *ext = (argc == 4) ? argv[3]->text : NULL;
1611 int ret = route_map_delete_set(route_map_index, "metric-type", ext);
1612 return route_map_command_status(vty, ret);
508e53e2 1613}
718e3744 1614
508e53e2 1615/* add "set forwarding-address" */
1616DEFUN (ospf6_routemap_set_forwarding,
1617 ospf6_routemap_set_forwarding_cmd,
1618 "set forwarding-address X:X::X:X",
1619 "Set value\n"
1620 "Forwarding Address\n"
1621 "IPv6 Address\n")
1622{
d62a17ae 1623 VTY_DECLVAR_CONTEXT(route_map_index, route_map_index);
1624 int idx_ipv6 = 2;
1625 int ret = route_map_add_set(route_map_index, "forwarding-address",
1626 argv[idx_ipv6]->arg);
1627 return route_map_command_status(vty, ret);
508e53e2 1628}
718e3744 1629
508e53e2 1630/* delete "set forwarding-address" */
1631DEFUN (ospf6_routemap_no_set_forwarding,
1632 ospf6_routemap_no_set_forwarding_cmd,
1633 "no set forwarding-address X:X::X:X",
1634 NO_STR
1635 "Set value\n"
1636 "Forwarding Address\n"
1637 "IPv6 Address\n")
1638{
d62a17ae 1639 VTY_DECLVAR_CONTEXT(route_map_index, route_map_index);
1640 int idx_ipv6 = 3;
1641 int ret = route_map_delete_set(route_map_index, "forwarding-address",
1642 argv[idx_ipv6]->arg);
1643 return route_map_command_status(vty, ret);
508e53e2 1644}
718e3744 1645
d62a17ae 1646static void ospf6_routemap_init(void)
508e53e2 1647{
d62a17ae 1648 route_map_init();
b2575bc0 1649
d62a17ae 1650 route_map_add_hook(ospf6_asbr_routemap_update);
1651 route_map_delete_hook(ospf6_asbr_routemap_update);
856ae1eb 1652 route_map_event_hook(ospf6_asbr_routemap_event);
508e53e2 1653
d62a17ae 1654 route_map_set_metric_hook(generic_set_add);
1655 route_map_no_set_metric_hook(generic_set_delete);
82f97584 1656
d62a17ae 1657 route_map_match_tag_hook(generic_match_add);
1658 route_map_no_match_tag_hook(generic_match_delete);
e1a1b2ed 1659
d62a17ae 1660 route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
1661 route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);
e1a1b2ed 1662
d62a17ae 1663 route_map_match_interface_hook(generic_match_add);
1664 route_map_no_match_interface_hook(generic_match_delete);
e1a1b2ed 1665
d62a17ae 1666 route_map_install_match(
1667 &ospf6_routemap_rule_match_address_prefixlist_cmd);
1668 route_map_install_match(&ospf6_routemap_rule_match_interface_cmd);
1669 route_map_install_match(&ospf6_routemap_rule_match_tag_cmd);
42a7debf 1670
d62a17ae 1671 route_map_install_set(&ospf6_routemap_rule_set_metric_type_cmd);
1672 route_map_install_set(&ospf6_routemap_rule_set_metric_cmd);
1673 route_map_install_set(&ospf6_routemap_rule_set_forwarding_cmd);
1674 route_map_install_set(&ospf6_routemap_rule_set_tag_cmd);
508e53e2 1675
d62a17ae 1676 /* ASE Metric Type (e.g. Type-1/Type-2) */
1677 install_element(RMAP_NODE, &ospf6_routemap_set_metric_type_cmd);
1678 install_element(RMAP_NODE, &ospf6_routemap_no_set_metric_type_cmd);
508e53e2 1679
d62a17ae 1680 /* ASE Metric */
1681 install_element(RMAP_NODE, &ospf6_routemap_set_forwarding_cmd);
1682 install_element(RMAP_NODE, &ospf6_routemap_no_set_forwarding_cmd);
718e3744 1683}
1684
6b0655a2 1685
508e53e2 1686/* Display functions */
d62a17ae 1687static char *ospf6_as_external_lsa_get_prefix_str(struct ospf6_lsa *lsa,
1688 char *buf, int buflen,
1689 int pos)
e68a6767 1690{
d62a17ae 1691 struct ospf6_as_external_lsa *external;
1692 struct in6_addr in6;
1693 int prefix_length = 0;
1694
1695 if (lsa) {
1696 external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
1697 lsa->header);
1698
1699 if (pos == 0) {
1700 ospf6_prefix_in6_addr(&in6, &external->prefix);
1701 prefix_length = external->prefix.prefix_length;
1702 } else {
1703 in6 = *((struct in6_addr
1704 *)((caddr_t)external
1705 + sizeof(struct
1706 ospf6_as_external_lsa)
1707 + OSPF6_PREFIX_SPACE(
1708 external->prefix
1709 .prefix_length)));
1710 }
1711 if (buf) {
1712 inet_ntop(AF_INET6, &in6, buf, buflen);
1713 if (prefix_length)
1714 sprintf(&buf[strlen(buf)], "/%d",
1715 prefix_length);
1716 }
e68a6767 1717 }
d62a17ae 1718 return (buf);
e68a6767
DD
1719}
1720
d62a17ae 1721static int ospf6_as_external_lsa_show(struct vty *vty, struct ospf6_lsa *lsa)
718e3744 1722{
d62a17ae 1723 struct ospf6_as_external_lsa *external;
1724 char buf[64];
1725
1726 assert(lsa->header);
1727 external = (struct ospf6_as_external_lsa *)OSPF6_LSA_HEADER_END(
1728 lsa->header);
1729
1730 /* bits */
1731 snprintf(buf, sizeof(buf), "%c%c%c",
1732 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_E) ? 'E'
1733 : '-'),
1734 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F) ? 'F'
1735 : '-'),
1736 (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T) ? 'T'
1737 : '-'));
1738
1739 vty_out(vty, " Bits: %s\n", buf);
1740 vty_out(vty, " Metric: %5lu\n",
1741 (u_long)OSPF6_ASBR_METRIC(external));
1742
1743 ospf6_prefix_options_printbuf(external->prefix.prefix_options, buf,
1744 sizeof(buf));
1745 vty_out(vty, " Prefix Options: %s\n", buf);
1746
1747 vty_out(vty, " Referenced LSType: %d\n",
1748 ntohs(external->prefix.prefix_refer_lstype));
1749
1750 vty_out(vty, " Prefix: %s\n",
1751 ospf6_as_external_lsa_get_prefix_str(lsa, buf, sizeof(buf), 0));
1752
1753 /* Forwarding-Address */
1754 if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_F)) {
1755 vty_out(vty, " Forwarding-Address: %s\n",
1756 ospf6_as_external_lsa_get_prefix_str(lsa, buf,
1757 sizeof(buf), 1));
1758 }
1759
1760 /* Tag */
1761 if (CHECK_FLAG(external->bits_metric, OSPF6_ASBR_BIT_T)) {
1762 vty_out(vty, " Tag: %" ROUTE_TAG_PRI "\n",
1763 ospf6_as_external_lsa_get_tag(lsa));
1764 }
1765
1766 return 0;
718e3744 1767}
1768
d62a17ae 1769static void ospf6_asbr_external_route_show(struct vty *vty,
1770 struct ospf6_route *route)
718e3744 1771{
d62a17ae 1772 struct ospf6_external_info *info = route->route_option;
1773 char prefix[PREFIX2STR_BUFFER], id[16], forwarding[64];
1774 u_int32_t tmp_id;
1775
1776 prefix2str(&route->prefix, prefix, sizeof(prefix));
1777 tmp_id = ntohl(info->id);
1778 inet_ntop(AF_INET, &tmp_id, id, sizeof(id));
1779 if (!IN6_IS_ADDR_UNSPECIFIED(&info->forwarding))
1780 inet_ntop(AF_INET6, &info->forwarding, forwarding,
1781 sizeof(forwarding));
1782 else
1783 snprintf(forwarding, sizeof(forwarding), ":: (ifindex %d)",
1784 ospf6_route_get_first_nh_index(route));
1785
1786 vty_out(vty, "%c %-32s %-15s type-%d %5lu %s\n",
1787 zebra_route_char(info->type), prefix, id,
1788 route->path.metric_type,
1789 (u_long)(route->path.metric_type == 2 ? route->path.u.cost_e2
1790 : route->path.cost),
1791 forwarding);
718e3744 1792}
1793
508e53e2 1794DEFUN (show_ipv6_ospf6_redistribute,
1795 show_ipv6_ospf6_redistribute_cmd,
1796 "show ipv6 ospf6 redistribute",
1797 SHOW_STR
1798 IP6_STR
1799 OSPF6_STR
1800 "redistributing External information\n"
1801 )
718e3744 1802{
d62a17ae 1803 struct ospf6_route *route;
718e3744 1804
d62a17ae 1805 OSPF6_CMD_CHECK_RUNNING();
b52a8a52 1806
d62a17ae 1807 ospf6_redistribute_show_config(vty);
718e3744 1808
d62a17ae 1809 for (route = ospf6_route_head(ospf6->external_table); route;
1810 route = ospf6_route_next(route))
1811 ospf6_asbr_external_route_show(vty, route);
508e53e2 1812
d62a17ae 1813 return CMD_SUCCESS;
718e3744 1814}
1815
d62a17ae 1816struct ospf6_lsa_handler as_external_handler = {
3981b5c7
VJ
1817 .lh_type = OSPF6_LSTYPE_AS_EXTERNAL,
1818 .lh_name = "AS-External",
1819 .lh_short_name = "ASE",
1820 .lh_show = ospf6_as_external_lsa_show,
1821 .lh_get_prefix_str = ospf6_as_external_lsa_get_prefix_str,
996c9314 1822 .lh_debug = 0};
508e53e2 1823
d62a17ae 1824void ospf6_asbr_init(void)
718e3744 1825{
d62a17ae 1826 ospf6_routemap_init();
508e53e2 1827
d62a17ae 1828 ospf6_install_lsa_handler(&as_external_handler);
718e3744 1829
d62a17ae 1830 install_element(VIEW_NODE, &show_ipv6_ospf6_redistribute_cmd);
508e53e2 1831
d62a17ae 1832 install_element(OSPF6_NODE, &ospf6_redistribute_cmd);
1833 install_element(OSPF6_NODE, &ospf6_redistribute_routemap_cmd);
1834 install_element(OSPF6_NODE, &no_ospf6_redistribute_cmd);
718e3744 1835}
1836
d62a17ae 1837void ospf6_asbr_redistribute_reset(void)
d9628728 1838{
d62a17ae 1839 int type;
1840
1841 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
1842 if (type == ZEBRA_ROUTE_OSPF6)
1843 continue;
1844 if (ospf6_zebra_is_redistribute(type))
1845 ospf6_asbr_redistribute_unset(type);
1846 }
d9628728
CF
1847}
1848
d62a17ae 1849void ospf6_asbr_terminate(void)
ae2254aa 1850{
856ae1eb
CS
1851 /* Cleanup route maps */
1852 route_map_add_hook(NULL);
1853 route_map_delete_hook(NULL);
1854 route_map_event_hook(NULL);
d62a17ae 1855 route_map_finish();
ae2254aa 1856}
718e3744 1857
508e53e2 1858DEFUN (debug_ospf6_asbr,
1859 debug_ospf6_asbr_cmd,
1860 "debug ospf6 asbr",
1861 DEBUG_STR
1862 OSPF6_STR
1863 "Debug OSPFv3 ASBR function\n"
1864 )
1865{
d62a17ae 1866 OSPF6_DEBUG_ASBR_ON();
1867 return CMD_SUCCESS;
508e53e2 1868}
1869
1870DEFUN (no_debug_ospf6_asbr,
1871 no_debug_ospf6_asbr_cmd,
1872 "no debug ospf6 asbr",
1873 NO_STR
1874 DEBUG_STR
1875 OSPF6_STR
1876 "Debug OSPFv3 ASBR function\n"
1877 )
1878{
d62a17ae 1879 OSPF6_DEBUG_ASBR_OFF();
1880 return CMD_SUCCESS;
508e53e2 1881}
1882
d62a17ae 1883int config_write_ospf6_debug_asbr(struct vty *vty)
508e53e2 1884{
d62a17ae 1885 if (IS_OSPF6_DEBUG_ASBR)
1886 vty_out(vty, "debug ospf6 asbr\n");
1887 return 0;
508e53e2 1888}
1889
d62a17ae 1890void install_element_ospf6_debug_asbr()
92300491 1891{
d62a17ae 1892 install_element(ENABLE_NODE, &debug_ospf6_asbr_cmd);
1893 install_element(ENABLE_NODE, &no_debug_ospf6_asbr_cmd);
1894 install_element(CONFIG_NODE, &debug_ospf6_asbr_cmd);
1895 install_element(CONFIG_NODE, &no_debug_ospf6_asbr_cmd);
92300491 1896}