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 along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <zebra.h>
23 
24 #include "thread.h"
25 #include "command.h"
26 #include "network.h"
27 #include "prefix.h"
28 #include "routemap.h"
29 #include "table.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "zclient.h"
33 #include "filter.h"
34 #include "plist.h"
35 #include "log.h"
36 #include "lib/bfd.h"
37 #include "nexthop.h"
38 
39 #include "ospfd/ospfd.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_ism.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_asbr.h"
44 #include "ospfd/ospf_abr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_dump.h"
47 #include "ospfd/ospf_route.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_nsm.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_te.h"
53 #include "ospfd/ospf_sr.h"
54 
55 DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table")
56 DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute")
57 DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments")
58 
59 
60 /* Zebra structure to hold current status. */
61 struct zclient *zclient = NULL;
62 /* and for the Synchronous connection to the Label Manager */
63 static struct zclient *zclient_sync;
64 
65 /* For registering threads. */
66 extern struct thread_master *master;
67 
68 /* Router-id update message from zebra. */
ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)69 static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
70 {
71 	struct ospf *ospf = NULL;
72 	struct prefix router_id;
73 	zebra_router_id_update_read(zclient->ibuf, &router_id);
74 
75 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
76 		char buf[PREFIX2STR_BUFFER];
77 		prefix2str(&router_id, buf, sizeof(buf));
78 		zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf,
79 			   ospf_vrf_id_to_name(vrf_id), vrf_id);
80 	}
81 
82 	ospf = ospf_lookup_by_vrf_id(vrf_id);
83 
84 	if (ospf != NULL) {
85 		ospf->router_id_zebra = router_id.u.prefix4;
86 		ospf_router_id_update(ospf);
87 	} else {
88 		if (IS_DEBUG_OSPF_EVENT) {
89 			char buf[PREFIX2STR_BUFFER];
90 
91 			prefix2str(&router_id, buf, sizeof(buf));
92 			zlog_debug(
93 				"%s: ospf instance not found for vrf %s id %u router_id %s",
94 				__func__, ospf_vrf_id_to_name(vrf_id), vrf_id,
95 				buf);
96 		}
97 	}
98 	return 0;
99 }
100 
ospf_interface_address_add(ZAPI_CALLBACK_ARGS)101 static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS)
102 {
103 	struct connected *c;
104 	struct ospf *ospf = NULL;
105 
106 
107 	c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
108 
109 	if (c == NULL)
110 		return 0;
111 
112 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
113 		char buf[PREFIX2STR_BUFFER];
114 		prefix2str(c->address, buf, sizeof(buf));
115 		zlog_debug("Zebra: interface %s address add %s vrf %s id %u",
116 			   c->ifp->name, buf, ospf_vrf_id_to_name(vrf_id),
117 			   vrf_id);
118 	}
119 
120 	ospf = ospf_lookup_by_vrf_id(vrf_id);
121 	if (!ospf)
122 		return 0;
123 
124 	ospf_if_update(ospf, c->ifp);
125 
126 	ospf_if_interface(c->ifp);
127 
128 	return 0;
129 }
130 
ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)131 static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS)
132 {
133 	struct connected *c;
134 	struct interface *ifp;
135 	struct ospf_interface *oi;
136 	struct route_node *rn;
137 	struct prefix p;
138 
139 	c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
140 
141 	if (c == NULL)
142 		return 0;
143 
144 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
145 		char buf[PREFIX2STR_BUFFER];
146 		prefix2str(c->address, buf, sizeof(buf));
147 		zlog_debug("Zebra: interface %s address delete %s",
148 			   c->ifp->name, buf);
149 	}
150 
151 	ifp = c->ifp;
152 	p = *c->address;
153 	p.prefixlen = IPV4_MAX_PREFIXLEN;
154 
155 	rn = route_node_lookup(IF_OIFS(ifp), &p);
156 	if (!rn) {
157 		connected_free(&c);
158 		return 0;
159 	}
160 
161 	assert(rn->info);
162 	oi = rn->info;
163 	route_unlock_node(rn);
164 
165 	/* Call interface hook functions to clean up */
166 	ospf_if_free(oi);
167 
168 	ospf_if_interface(c->ifp);
169 
170 	connected_free(&c);
171 
172 	return 0;
173 }
174 
ospf_interface_link_params(ZAPI_CALLBACK_ARGS)175 static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS)
176 {
177 	struct interface *ifp;
178 
179 	ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id);
180 
181 	if (ifp == NULL)
182 		return 0;
183 
184 	/* Update TE TLV */
185 	ospf_mpls_te_update_if(ifp);
186 
187 	return 0;
188 }
189 
190 /* VRF update for an interface. */
ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)191 static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS)
192 {
193 	struct interface *ifp = NULL;
194 	vrf_id_t new_vrf_id;
195 
196 	ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
197 					      &new_vrf_id);
198 	if (!ifp)
199 		return 0;
200 
201 	if (IS_DEBUG_OSPF_EVENT)
202 		zlog_debug(
203 			"%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u",
204 			__func__, ifp->name, vrf_id,
205 			ospf_vrf_id_to_name(new_vrf_id), new_vrf_id);
206 
207 	/*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/
208 	if_update_to_new_vrf(ifp, new_vrf_id);
209 
210 	return 0;
211 }
212 
ospf_zebra_add(struct ospf * ospf,struct prefix_ipv4 * p,struct ospf_route * or)213 void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p,
214 		    struct ospf_route * or)
215 {
216 	struct zapi_route api;
217 	struct zapi_nexthop *api_nh;
218 	uint8_t distance;
219 	struct ospf_path *path;
220 	struct listnode *node;
221 	int count = 0;
222 
223 	memset(&api, 0, sizeof(api));
224 	api.vrf_id = ospf->vrf_id;
225 	api.type = ZEBRA_ROUTE_OSPF;
226 	api.instance = ospf->instance;
227 	api.safi = SAFI_UNICAST;
228 
229 	memcpy(&api.prefix, p, sizeof(*p));
230 	SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
231 
232 	/* Metric value. */
233 	SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
234 	if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
235 		api.metric = or->cost + or->u.ext.type2_cost;
236 	else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
237 		api.metric = or->u.ext.type2_cost;
238 	else
239 		api.metric = or->cost;
240 
241 	/* Check if path type is ASE */
242 	if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL)
243 	     || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL))
244 	    && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) {
245 		SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
246 		api.tag = or->u.ext.tag;
247 	}
248 
249 	/* Distance value. */
250 	distance = ospf_distance_apply(ospf, p, or);
251 	if (distance) {
252 		SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
253 		api.distance = distance;
254 	}
255 
256 	/* Nexthop, ifindex, distance and metric information. */
257 	for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) {
258 		if (count >= MULTIPATH_NUM)
259 			break;
260 		api_nh = &api.nexthops[count];
261 #ifdef HAVE_NETLINK
262 		if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY
263 					 && path->ifindex != 0)) {
264 #else  /* HAVE_NETLINK */
265 		if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) {
266 #endif /* HAVE_NETLINK */
267 			api_nh->gate.ipv4 = path->nexthop;
268 			api_nh->ifindex = path->ifindex;
269 			api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
270 		} else if (path->nexthop.s_addr != INADDR_ANY) {
271 			api_nh->gate.ipv4 = path->nexthop;
272 			api_nh->type = NEXTHOP_TYPE_IPV4;
273 		} else {
274 			api_nh->ifindex = path->ifindex;
275 			api_nh->type = NEXTHOP_TYPE_IFINDEX;
276 		}
277 		api_nh->vrf_id = ospf->vrf_id;
278 		count++;
279 
280 		if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
281 			char buf[2][PREFIX2STR_BUFFER];
282 			struct interface *ifp;
283 
284 			ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id);
285 
286 			zlog_debug(
287 				"Zebra: Route add %s nexthop %s, ifindex=%d %s",
288 				prefix2str(p, buf[0], sizeof(buf[0])),
289 			        inet_ntop(AF_INET, &path->nexthop,
290 					  buf[1], sizeof(buf[1])),
291 				path->ifindex, ifp ? ifp->name : " ");
292 		}
293 	}
294 	api.nexthop_num = count;
295 
296 	zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
297 }
298 
299 void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p,
300 		       struct ospf_route * or)
301 {
302 	struct zapi_route api;
303 
304 	memset(&api, 0, sizeof(api));
305 	api.vrf_id = ospf->vrf_id;
306 	api.type = ZEBRA_ROUTE_OSPF;
307 	api.instance = ospf->instance;
308 	api.safi = SAFI_UNICAST;
309 	memcpy(&api.prefix, p, sizeof(*p));
310 
311 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
312 		char buf[PREFIX2STR_BUFFER];
313 		zlog_debug("Zebra: Route delete %s",
314 			   prefix2str(p, buf, sizeof(buf)));
315 	}
316 
317 	zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
318 }
319 
320 void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p)
321 {
322 	struct zapi_route api;
323 
324 	memset(&api, 0, sizeof(api));
325 	api.vrf_id = ospf->vrf_id;
326 	api.type = ZEBRA_ROUTE_OSPF;
327 	api.instance = ospf->instance;
328 	api.safi = SAFI_UNICAST;
329 	memcpy(&api.prefix, p, sizeof(*p));
330 	zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
331 
332 	zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
333 
334 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
335 		char buf[PREFIX2STR_BUFFER];
336 		zlog_debug("Zebra: Route add discard %s",
337 			   prefix2str(p, buf, sizeof(buf)));
338 	}
339 }
340 
341 void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p)
342 {
343 	struct zapi_route api;
344 
345 	memset(&api, 0, sizeof(api));
346 	api.vrf_id = ospf->vrf_id;
347 	api.type = ZEBRA_ROUTE_OSPF;
348 	api.instance = ospf->instance;
349 	api.safi = SAFI_UNICAST;
350 	memcpy(&api.prefix, p, sizeof(*p));
351 	zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
352 
353 	zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
354 
355 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
356 		char buf[PREFIX2STR_BUFFER];
357 		zlog_debug("Zebra: Route delete discard %s",
358 			   prefix2str(p, buf, sizeof(buf)));
359 	}
360 }
361 
362 struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type,
363 					   unsigned short instance)
364 {
365 	struct list *ext_list;
366 	struct listnode *node;
367 	struct ospf_external *ext;
368 
369 	ext_list = ospf->external[type];
370 	if (!ext_list)
371 		return (NULL);
372 
373 	for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext))
374 		if (ext->instance == instance)
375 			return ext;
376 
377 	return NULL;
378 }
379 
380 struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type,
381 					unsigned short instance)
382 {
383 	struct list *ext_list;
384 	struct ospf_external *ext;
385 
386 	ext = ospf_external_lookup(ospf, type, instance);
387 	if (ext)
388 		return ext;
389 
390 	if (!ospf->external[type])
391 		ospf->external[type] = list_new();
392 
393 	ext_list = ospf->external[type];
394 	ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external));
395 	ext->instance = instance;
396 	EXTERNAL_INFO(ext) = route_table_init();
397 
398 	listnode_add(ext_list, ext);
399 
400 	return ext;
401 }
402 
403 /*
404  * Walk all the ei received from zebra for a route type and apply
405  * default route-map.
406  */
407 bool ospf_external_default_routemap_apply_walk(struct ospf *ospf,
408 					       struct list *ext_list,
409 					       struct external_info *default_ei)
410 {
411 	struct listnode *node;
412 	struct ospf_external *ext;
413 	struct route_node *rn;
414 	struct external_info *ei = NULL;
415 	int ret = 0;
416 
417 	for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
418 		if (!ext->external_info)
419 			continue;
420 
421 		for (rn = route_top(ext->external_info); rn;
422 		     rn = route_next(rn)) {
423 			ei = rn->info;
424 			if (!ei)
425 				continue;
426 			ret = ospf_external_info_apply_default_routemap(
427 				ospf, ei, default_ei);
428 			if (ret)
429 				break;
430 		}
431 	}
432 
433 	if (ret && ei) {
434 		if (IS_DEBUG_OSPF_DEFAULT_INFO)
435 			zlog_debug("Default originate routemap permit ei: %s",
436 				   inet_ntoa(ei->p.prefix));
437 		return true;
438 	}
439 
440 	return false;
441 }
442 
443 /*
444  * Function to originate or flush default after applying
445  * route-map on all ei.
446  */
447 static int ospf_external_lsa_default_routemap_timer(struct thread *thread)
448 {
449 	struct list *ext_list;
450 	struct ospf *ospf = THREAD_ARG(thread);
451 	struct prefix_ipv4 p;
452 	int type;
453 	int ret = 0;
454 	struct ospf_lsa *lsa;
455 	struct external_info *default_ei;
456 
457 	p.family = AF_INET;
458 	p.prefixlen = 0;
459 	p.prefix.s_addr = INADDR_ANY;
460 
461 	/* Get the default extenal info. */
462 	default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
463 					       ospf->instance, &p);
464 	if (!default_ei) {
465 		/* Nothing to be done here. */
466 		if (IS_DEBUG_OSPF_DEFAULT_INFO)
467 			zlog_debug("Default originate info not present");
468 		return 0;
469 	}
470 
471 	/* For all the ei apply route-map */
472 	for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
473 		ext_list = ospf->external[type];
474 		if (!ext_list || type == ZEBRA_ROUTE_OSPF)
475 			continue;
476 
477 		ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
478 								default_ei);
479 		if (ret)
480 			break;
481 	}
482 
483 	/* Get the default LSA. */
484 	lsa = ospf_external_info_find_lsa(ospf, &p);
485 
486 	/* If permit then originate default. */
487 	if (ret && !lsa)
488 		ospf_external_lsa_originate(ospf, default_ei);
489 	else if (ret && lsa && IS_LSA_MAXAGE(lsa))
490 		ospf_external_lsa_refresh(ospf, lsa, default_ei, true);
491 	else if (!ret && lsa)
492 		ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &default_ei->p, 0);
493 
494 	return 1;
495 }
496 
497 
498 void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance)
499 {
500 	struct ospf_external *ext;
501 
502 	ext = ospf_external_lookup(ospf, type, instance);
503 
504 	if (ext) {
505 		if (EXTERNAL_INFO(ext))
506 			route_table_finish(EXTERNAL_INFO(ext));
507 
508 		listnode_delete(ospf->external[type], ext);
509 
510 		if (!ospf->external[type]->count)
511 			list_delete(&ospf->external[type]);
512 
513 		XFREE(MTYPE_OSPF_EXTERNAL, ext);
514 	}
515 
516 	/*
517 	 * Check if default needs to be flushed too.
518 	 */
519 	thread_add_event(master, ospf_external_lsa_default_routemap_timer, ospf,
520 			 0, &ospf->t_default_routemap_timer);
521 }
522 
523 /* Update NHLFE for Prefix SID */
524 void ospf_zebra_update_prefix_sid(const struct sr_prefix *srp)
525 {
526 	struct zapi_labels zl;
527 	struct zapi_nexthop *znh;
528 	struct listnode *node;
529 	struct ospf_path *path;
530 
531 	osr_debug("SR (%s): Update Labels %u for Prefix %pFX", __func__,
532 		  srp->label_in, (struct prefix *)&srp->prefv4);
533 
534 	/* Prepare message. */
535 	memset(&zl, 0, sizeof(zl));
536 	zl.type = ZEBRA_LSP_OSPF_SR;
537 	zl.local_label = srp->label_in;
538 
539 	switch (srp->type) {
540 	case LOCAL_SID:
541 		/* Set Label for local Prefix */
542 		znh = &zl.nexthops[zl.nexthop_num++];
543 		znh->type = NEXTHOP_TYPE_IFINDEX;
544 		znh->ifindex = srp->nhlfe.ifindex;
545 		znh->label_num = 1;
546 		znh->labels[0] = srp->nhlfe.label_out;
547 		break;
548 
549 	case PREF_SID:
550 		/* Update route in the RIB too. */
551 		SET_FLAG(zl.message, ZAPI_LABELS_FTN);
552 		zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
553 		zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
554 		zl.route.prefix.family = srp->prefv4.family;
555 		zl.route.type = ZEBRA_ROUTE_OSPF;
556 		zl.route.instance = 0;
557 
558 		/* Check that SRP contains at least one valid path */
559 		if (srp->route == NULL) {
560 			return;
561 		}
562 		for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
563 			if (path->srni.label_out == MPLS_INVALID_LABEL)
564 				continue;
565 
566 			if (zl.nexthop_num >= MULTIPATH_NUM)
567 				break;
568 
569 			znh = &zl.nexthops[zl.nexthop_num++];
570 			znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
571 			znh->gate.ipv4 = path->nexthop;
572 			znh->ifindex = path->ifindex;
573 			znh->label_num = 1;
574 			znh->labels[0] = path->srni.label_out;
575 		}
576 		break;
577 	default:
578 		return;
579 	}
580 
581 	/* Finally, send message to zebra. */
582 	(void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_REPLACE, &zl);
583 }
584 
585 /* Remove NHLFE for Prefix-SID */
586 void ospf_zebra_delete_prefix_sid(const struct sr_prefix *srp)
587 {
588 	struct zapi_labels zl;
589 
590 	osr_debug("SR (%s): Delete Labels %u for Prefix %pFX", __func__,
591 		  srp->label_in, (struct prefix *)&srp->prefv4);
592 
593 	/* Prepare message. */
594 	memset(&zl, 0, sizeof(zl));
595 	zl.type = ZEBRA_LSP_OSPF_SR;
596 	zl.local_label = srp->label_in;
597 
598 	if (srp->type == PREF_SID) {
599 		/* Update route in the RIB too */
600 		SET_FLAG(zl.message, ZAPI_LABELS_FTN);
601 		zl.route.prefix.u.prefix4 = srp->prefv4.prefix;
602 		zl.route.prefix.prefixlen = srp->prefv4.prefixlen;
603 		zl.route.prefix.family = srp->prefv4.family;
604 		zl.route.type = ZEBRA_ROUTE_OSPF;
605 		zl.route.instance = 0;
606 	}
607 
608 	/* Send message to zebra. */
609 	(void)zebra_send_mpls_labels(zclient, ZEBRA_MPLS_LABELS_DELETE, &zl);
610 }
611 
612 /* Send MPLS Label entry to Zebra for installation or deletion */
613 void ospf_zebra_send_adjacency_sid(int cmd, struct sr_nhlfe nhlfe)
614 {
615 	struct zapi_labels zl;
616 	struct zapi_nexthop *znh;
617 
618 	osr_debug("SR (%s): %s Labels %u/%u for Adjacency via %u", __func__,
619 		  cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
620 		  nhlfe.label_in, nhlfe.label_out, nhlfe.ifindex);
621 
622 	memset(&zl, 0, sizeof(zl));
623 	zl.type = ZEBRA_LSP_OSPF_SR;
624 	zl.local_label = nhlfe.label_in;
625 	zl.nexthop_num = 1;
626 	znh = &zl.nexthops[0];
627 	znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
628 	znh->gate.ipv4 = nhlfe.nexthop;
629 	znh->ifindex = nhlfe.ifindex;
630 	znh->label_num = 1;
631 	znh->labels[0] = nhlfe.label_out;
632 
633 	(void)zebra_send_mpls_labels(zclient, cmd, &zl);
634 }
635 
636 struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type,
637 				       unsigned short instance)
638 {
639 	struct list *red_list;
640 	struct listnode *node;
641 	struct ospf_redist *red;
642 
643 	red_list = ospf->redist[type];
644 	if (!red_list)
645 		return (NULL);
646 
647 	for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
648 		if (red->instance == instance)
649 			return red;
650 
651 	return NULL;
652 }
653 
654 struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type,
655 				    unsigned short instance)
656 {
657 	struct list *red_list;
658 	struct ospf_redist *red;
659 
660 	red = ospf_redist_lookup(ospf, type, instance);
661 	if (red)
662 		return red;
663 
664 	if (!ospf->redist[type])
665 		ospf->redist[type] = list_new();
666 
667 	red_list = ospf->redist[type];
668 	red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist));
669 	red->instance = instance;
670 	red->dmetric.type = -1;
671 	red->dmetric.value = -1;
672 	ROUTEMAP_NAME(red) = NULL;
673 	ROUTEMAP(red) = NULL;
674 
675 	listnode_add(red_list, red);
676 
677 	return red;
678 }
679 
680 void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance)
681 {
682 	struct ospf_redist *red;
683 
684 	red = ospf_redist_lookup(ospf, type, instance);
685 
686 	if (red) {
687 		listnode_delete(ospf->redist[type], red);
688 		if (!ospf->redist[type]->count) {
689 			list_delete(&ospf->redist[type]);
690 		}
691 		ospf_routemap_unset(red);
692 		XFREE(MTYPE_OSPF_REDISTRIBUTE, red);
693 	}
694 }
695 
696 
697 int ospf_is_type_redistributed(struct ospf *ospf, int type,
698 			       unsigned short instance)
699 {
700 	return (DEFAULT_ROUTE_TYPE(type)
701 			? vrf_bitmap_check(zclient->default_information[AFI_IP],
702 					   ospf->vrf_id)
703 			: ((instance
704 			    && redist_check_instance(
705 				       &zclient->mi_redist[AFI_IP][type],
706 				       instance))
707 			   || (!instance
708 			       && vrf_bitmap_check(
709 					  zclient->redist[AFI_IP][type],
710 					  ospf->vrf_id))));
711 }
712 
713 int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance,
714 			  int mtype, int mvalue)
715 {
716 	int force = 0;
717 	struct ospf_redist *red;
718 
719 	red = ospf_redist_lookup(ospf, type, instance);
720 
721 	if (red == NULL) {
722 		zlog_err(
723 			 "Redistribute[%s][%d]: Lookup failed  Type[%d] , Metric[%d]",
724 			 ospf_redist_string(type), instance,
725 			 metric_type(ospf, type, instance),
726 			 metric_value(ospf, type, instance));
727 		return CMD_WARNING_CONFIG_FAILED;
728 	}
729 
730 	if (ospf_is_type_redistributed(ospf, type, instance)) {
731 		if (mtype != red->dmetric.type) {
732 			red->dmetric.type = mtype;
733 			force = LSA_REFRESH_FORCE;
734 		}
735 		if (mvalue != red->dmetric.value) {
736 			red->dmetric.value = mvalue;
737 			force = LSA_REFRESH_FORCE;
738 		}
739 
740 		ospf_external_lsa_refresh_type(ospf, type, instance, force);
741 
742 		if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
743 			zlog_debug(
744 				"Redistribute[%s][%d]: Refresh  Type[%d], Metric[%d]",
745 				ospf_redist_string(type), instance,
746 				metric_type(ospf, type, instance),
747 				metric_value(ospf, type, instance));
748 
749 		return CMD_SUCCESS;
750 	}
751 
752 	red->dmetric.type = mtype;
753 	red->dmetric.value = mvalue;
754 
755 	ospf_external_add(ospf, type, instance);
756 
757 	zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
758 			     instance, ospf->vrf_id);
759 
760 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
761 		zlog_debug(
762 			"Redistribute[%s][%d] vrf id %u: Start  Type[%d], Metric[%d]",
763 			ospf_redist_string(type), instance, ospf->vrf_id,
764 			metric_type(ospf, type, instance),
765 			metric_value(ospf, type, instance));
766 
767 	ospf_asbr_status_update(ospf, ++ospf->redistribute);
768 
769 	return CMD_SUCCESS;
770 }
771 
772 int ospf_redistribute_unset(struct ospf *ospf, int type,
773 			    unsigned short instance)
774 {
775 	if (type == zclient->redist_default && instance == zclient->instance)
776 		return CMD_SUCCESS;
777 
778 	if (!ospf_is_type_redistributed(ospf, type, instance))
779 		return CMD_SUCCESS;
780 
781 	zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type,
782 			     instance, ospf->vrf_id);
783 
784 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
785 		zlog_debug("Redistribute[%s][%d] vrf id %u: Stop",
786 			   ospf_redist_string(type), instance, ospf->vrf_id);
787 
788 	/* Remove the routes from OSPF table. */
789 	ospf_redistribute_withdraw(ospf, type, instance);
790 
791 	ospf_external_del(ospf, type, instance);
792 
793 	ospf_asbr_status_update(ospf, --ospf->redistribute);
794 
795 	return CMD_SUCCESS;
796 }
797 
798 int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype,
799 				  int mvalue)
800 {
801 	struct prefix_ipv4 p;
802 	struct in_addr nexthop;
803 	int cur_originate = ospf->default_originate;
804 	const char *type_str = NULL;
805 
806 	nexthop.s_addr = INADDR_ANY;
807 	p.family = AF_INET;
808 	p.prefix.s_addr = INADDR_ANY;
809 	p.prefixlen = 0;
810 
811 	ospf->default_originate = originate;
812 
813 	if (cur_originate == originate) {
814 		/* Refresh the lsa since metric might different */
815 		if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
816 			zlog_debug(
817 				"Redistribute[%s]: Refresh  Type[%d], Metric[%d]",
818 				ospf_redist_string(DEFAULT_ROUTE),
819 				metric_type(ospf, DEFAULT_ROUTE, 0),
820 				metric_value(ospf, DEFAULT_ROUTE, 0));
821 
822 		ospf_external_lsa_refresh_default(ospf);
823 		return CMD_SUCCESS;
824 	}
825 
826 	switch (cur_originate) {
827 	case DEFAULT_ORIGINATE_NONE:
828 		break;
829 	case DEFAULT_ORIGINATE_ZEBRA:
830 		zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
831 				 zclient, AFI_IP, ospf->vrf_id);
832 		ospf->redistribute--;
833 		break;
834 	case DEFAULT_ORIGINATE_ALWAYS:
835 		ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p);
836 		ospf_external_del(ospf, DEFAULT_ROUTE, 0);
837 		ospf->redistribute--;
838 		break;
839 	}
840 
841 	switch (originate) {
842 	case DEFAULT_ORIGINATE_NONE:
843 		type_str = "none";
844 		break;
845 	case DEFAULT_ORIGINATE_ZEBRA:
846 		type_str = "normal";
847 		ospf->redistribute++;
848 		zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
849 					     zclient, AFI_IP, ospf->vrf_id);
850 		break;
851 	case DEFAULT_ORIGINATE_ALWAYS:
852 		type_str = "always";
853 		ospf->redistribute++;
854 		ospf_external_add(ospf, DEFAULT_ROUTE, 0);
855 		ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop,
856 				       0);
857 		break;
858 	}
859 
860 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
861 		zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]",
862 		type_str,
863 		metric_type(ospf, DEFAULT_ROUTE, 0),
864 		metric_value(ospf, DEFAULT_ROUTE, 0));
865 
866 	ospf_external_lsa_refresh_default(ospf);
867 	ospf_asbr_status_update(ospf, ospf->redistribute);
868 	return CMD_SUCCESS;
869 }
870 
871 static int ospf_external_lsa_originate_check(struct ospf *ospf,
872 					     struct external_info *ei)
873 {
874 	/* If prefix is multicast, then do not originate LSA. */
875 	if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) {
876 		zlog_info(
877 			"LSA[Type5:%s]: Not originate AS-external-LSA, Prefix belongs multicast",
878 			inet_ntoa(ei->p.prefix));
879 		return 0;
880 	}
881 
882 	/* Take care of default-originate. */
883 	if (is_prefix_default(&ei->p))
884 		if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
885 			zlog_info(
886 				"LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
887 			return 0;
888 		}
889 
890 	return 1;
891 }
892 
893 /* If connected prefix is OSPF enable interface, then do not announce. */
894 int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei)
895 {
896 	struct listnode *node;
897 	struct ospf_interface *oi;
898 
899 
900 	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
901 		if (prefix_match(oi->address, (struct prefix *)&ei->p))
902 			return 0;
903 	return 1;
904 }
905 
906 
907 /* Apply default route-map on ei received. */
908 int ospf_external_info_apply_default_routemap(struct ospf *ospf,
909 					      struct external_info *ei,
910 					      struct external_info *default_ei)
911 {
912 	struct ospf_redist *red;
913 	int type = default_ei->type;
914 	struct prefix_ipv4 *p = &ei->p;
915 	struct route_map_set_values save_values;
916 
917 
918 	if (!ospf_external_lsa_originate_check(ospf, default_ei))
919 		return 0;
920 
921 	save_values = default_ei->route_map_set;
922 	ospf_reset_route_map_set_values(&default_ei->route_map_set);
923 
924 	/* apply route-map if needed */
925 	red = ospf_redist_lookup(ospf, type, ospf->instance);
926 	if (red && ROUTEMAP_NAME(red)) {
927 		route_map_result_t ret;
928 
929 		ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
930 				      RMAP_OSPF, ei);
931 
932 		if (ret == RMAP_DENYMATCH) {
933 			ei->route_map_set = save_values;
934 			return 0;
935 		}
936 	}
937 
938 	return 1;
939 }
940 
941 
942 /*
943  * Default originated is based on route-map condition then
944  * apply route-map on received external info. Originate or
945  * flush based on route-map condition.
946  */
947 static bool ospf_external_lsa_default_routemap_apply(struct ospf *ospf,
948 						     struct external_info *ei,
949 						     int cmd)
950 {
951 	struct external_info *default_ei;
952 	struct prefix_ipv4 p;
953 	struct ospf_lsa *lsa;
954 	int ret;
955 
956 	p.family = AF_INET;
957 	p.prefixlen = 0;
958 	p.prefix.s_addr = INADDR_ANY;
959 
960 
961 	/* Get the default extenal info. */
962 	default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
963 					       ospf->instance, &p);
964 	if (!default_ei) {
965 		/* Nothing to be done here. */
966 		return false;
967 	}
968 
969 	if (IS_DEBUG_OSPF_DEFAULT_INFO)
970 		zlog_debug("Apply default originate routemap on ei: %s cmd: %d",
971 			   inet_ntoa(ei->p.prefix), cmd);
972 
973 	ret = ospf_external_info_apply_default_routemap(ospf, ei, default_ei);
974 
975 	/* If deny then nothing to be done both in add and del case. */
976 	if (!ret) {
977 		if (IS_DEBUG_OSPF_DEFAULT_INFO)
978 			zlog_debug("Default originte routemap deny for ei: %s",
979 				   inet_ntoa(ei->p.prefix));
980 		return false;
981 	}
982 
983 	/* Get the default LSA. */
984 	lsa = ospf_external_info_find_lsa(ospf, &p);
985 
986 	/* If this is add route and permit then ooriginate default. */
987 	if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
988 		/* If permit and default already advertise then return. */
989 		if (lsa && !IS_LSA_MAXAGE(lsa)) {
990 			if (IS_DEBUG_OSPF_DEFAULT_INFO)
991 				zlog_debug("Default lsa already originated");
992 			return true;
993 		}
994 
995 		if (IS_DEBUG_OSPF_DEFAULT_INFO)
996 			zlog_debug("Originating/Refreshing default lsa");
997 
998 		if (lsa && IS_LSA_MAXAGE(lsa))
999 			/* Refresh lsa.*/
1000 			ospf_external_lsa_refresh(ospf, lsa, default_ei, true);
1001 		else
1002 			/* If permit and default not advertised then advertise.
1003 			 */
1004 			ospf_external_lsa_originate(ospf, default_ei);
1005 
1006 	} else if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) {
1007 		/* If deny and lsa is not originated then nothing to be done.*/
1008 		if (!lsa) {
1009 			if (IS_DEBUG_OSPF_DEFAULT_INFO)
1010 				zlog_debug(
1011 					"Default lsa not originated, not flushing");
1012 			return true;
1013 		}
1014 
1015 		if (IS_DEBUG_OSPF_DEFAULT_INFO)
1016 			zlog_debug(
1017 				"Running default route-map again as ei: %s deleted",
1018 				inet_ntoa(ei->p.prefix));
1019 		/*
1020 		 * if this route delete was permitted then we need to check
1021 		 * there are any other external info which can still trigger
1022 		 * default route origination else flush it.
1023 		 */
1024 		thread_add_event(master,
1025 				 ospf_external_lsa_default_routemap_timer, ospf,
1026 				 0, &ospf->t_default_routemap_timer);
1027 	}
1028 
1029 	return true;
1030 }
1031 
1032 /* return 1 if external LSA must be originated, 0 otherwise */
1033 int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
1034 			    int *changed)
1035 {
1036 	struct route_map_set_values save_values;
1037 	struct prefix_ipv4 *p = &ei->p;
1038 	struct ospf_redist *red;
1039 	uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
1040 	unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
1041 	route_tag_t saved_tag = 0;
1042 
1043 	/* Default is handled differently. */
1044 	if (type == DEFAULT_ROUTE)
1045 		return 1;
1046 
1047 	if (changed)
1048 		*changed = 0;
1049 
1050 	if (!ospf_external_lsa_originate_check(ospf, ei))
1051 		return 0;
1052 
1053 	/* Take care connected route. */
1054 	if (type == ZEBRA_ROUTE_CONNECT
1055 	    && !ospf_distribute_check_connected(ospf, ei))
1056 		return 0;
1057 
1058 	if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type))
1059 		/* distirbute-list exists, but access-list may not? */
1060 		if (DISTRIBUTE_LIST(ospf, type))
1061 			if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p)
1062 			    == FILTER_DENY) {
1063 				if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
1064 					char buf[PREFIX2STR_BUFFER];
1065 					zlog_debug(
1066 						"Redistribute[%s]: %s filtered by distribute-list.",
1067 						ospf_redist_string(type),
1068 						prefix2str(p, buf, sizeof(buf)));
1069 				}
1070 				return 0;
1071 			}
1072 
1073 	save_values = ei->route_map_set;
1074 	ospf_reset_route_map_set_values(&ei->route_map_set);
1075 
1076 	saved_tag = ei->tag;
1077 	/* Resetting with original route tag */
1078 	ei->tag = ei->orig_tag;
1079 
1080 	/* apply route-map if needed */
1081 	red = ospf_redist_lookup(ospf, type, instance);
1082 	if (red && ROUTEMAP_NAME(red)) {
1083 		route_map_result_t ret;
1084 
1085 		ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p,
1086 				      RMAP_OSPF, ei);
1087 
1088 		if (ret == RMAP_DENYMATCH) {
1089 			ei->route_map_set = save_values;
1090 			if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
1091 				char buf[PREFIX2STR_BUFFER];
1092 				zlog_debug(
1093 					"Redistribute[%s]: %s filtered by route-map.",
1094 					ospf_redist_string(type),
1095 					prefix2str(p, buf, sizeof(buf)));
1096 			}
1097 			return 0;
1098 		}
1099 
1100 		/* check if 'route-map set' changed something */
1101 		if (changed) {
1102 			*changed = !ospf_route_map_set_compare(
1103 				&ei->route_map_set, &save_values);
1104 
1105 			/* check if tag is modified */
1106 			*changed |= (saved_tag != ei->tag);
1107 		}
1108 	}
1109 
1110 	return 1;
1111 }
1112 
1113 /* OSPF route-map set for redistribution */
1114 void ospf_routemap_set(struct ospf_redist *red, const char *name)
1115 {
1116 	if (ROUTEMAP_NAME(red)) {
1117 		route_map_counter_decrement(ROUTEMAP(red));
1118 		free(ROUTEMAP_NAME(red));
1119 	}
1120 
1121 	ROUTEMAP_NAME(red) = strdup(name);
1122 	ROUTEMAP(red) = route_map_lookup_by_name(name);
1123 	route_map_counter_increment(ROUTEMAP(red));
1124 }
1125 
1126 void ospf_routemap_unset(struct ospf_redist *red)
1127 {
1128 	if (ROUTEMAP_NAME(red)) {
1129 		route_map_counter_decrement(ROUTEMAP(red));
1130 		free(ROUTEMAP_NAME(red));
1131 	}
1132 
1133 	ROUTEMAP_NAME(red) = NULL;
1134 	ROUTEMAP(red) = NULL;
1135 }
1136 
1137 /* Zebra route add and delete treatment. */
1138 static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
1139 {
1140 	struct zapi_route api;
1141 	struct prefix_ipv4 p;
1142 	unsigned long ifindex;
1143 	struct in_addr nexthop;
1144 	struct external_info *ei;
1145 	struct ospf *ospf;
1146 	int i;
1147 	uint8_t rt_type;
1148 
1149 	ospf = ospf_lookup_by_vrf_id(vrf_id);
1150 	if (ospf == NULL)
1151 		return 0;
1152 
1153 	if (zapi_route_decode(zclient->ibuf, &api) < 0)
1154 		return -1;
1155 
1156 	ifindex = api.nexthops[0].ifindex;
1157 	nexthop = api.nexthops[0].gate.ipv4;
1158 	rt_type = api.type;
1159 
1160 	memcpy(&p, &api.prefix, sizeof(p));
1161 	if (IPV4_NET127(ntohl(p.prefix.s_addr)))
1162 		return 0;
1163 
1164 	/* Re-destributed route is default route.
1165 	 * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for
1166 	 * updating ex-info. But in resetting (no default-info
1167 	 * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
1168 	 * Resolved this inconsistency by maintaining same route type.
1169 	 */
1170 	if (is_prefix_default(&p))
1171 		rt_type = DEFAULT_ROUTE;
1172 
1173 	if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) {
1174 		char buf_prefix[PREFIX_STRLEN];
1175 		prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1176 
1177 		zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %s",
1178 			   __func__, zserv_command_string(cmd),
1179 			   zebra_route_string(api.type), vrf_id, buf_prefix);
1180 	}
1181 
1182 	if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
1183 		/* XXX|HACK|TODO|FIXME:
1184 		 * Maybe we should ignore reject/blackhole routes? Testing
1185 		 * shows that there is no problems though and this is only way
1186 		 * to "summarize" routes in ASBR at the moment. Maybe we need
1187 		 * just a better generalised solution for these types?
1188 		 */
1189 
1190 		/* Protocol tag overwrites all other tag value sent by zebra */
1191 		if (ospf->dtag[rt_type] > 0)
1192 			api.tag = ospf->dtag[rt_type];
1193 
1194 		/*
1195 		 * Given zebra sends update for a prefix via ADD message, it
1196 		 * should
1197 		 * be considered as an implicit DEL for that prefix with other
1198 		 * source
1199 		 * types.
1200 		 */
1201 		for (i = 0; i <= ZEBRA_ROUTE_MAX; i++)
1202 			if (i != rt_type)
1203 				ospf_external_info_delete(ospf, i, api.instance,
1204 							  p);
1205 
1206 		ei = ospf_external_info_add(ospf, rt_type, api.instance, p,
1207 					    ifindex, nexthop, api.tag);
1208 		if (ei == NULL) {
1209 			/* Nothing has changed, so nothing to do; return */
1210 			return 0;
1211 		}
1212 		if (ospf->router_id.s_addr != INADDR_ANY) {
1213 			if (ei) {
1214 				if (is_prefix_default(&p))
1215 					ospf_external_lsa_refresh_default(ospf);
1216 				else {
1217 					struct ospf_lsa *current;
1218 
1219 					current = ospf_external_info_find_lsa(
1220 						ospf, &ei->p);
1221 					if (!current)
1222 						ospf_external_lsa_originate(
1223 							ospf, ei);
1224 					else {
1225 						if (IS_DEBUG_OSPF(
1226 							    zebra,
1227 							    ZEBRA_REDISTRIBUTE))
1228 							zlog_debug(
1229 								"ospf_zebra_read_route() : %s refreshing LSA",
1230 								inet_ntoa(
1231 									p.prefix));
1232 						ospf_external_lsa_refresh(
1233 							ospf, current, ei,
1234 							LSA_REFRESH_FORCE);
1235 					}
1236 				}
1237 			}
1238 		}
1239 
1240 		/*
1241 		 * Check if default-information originate is
1242 		 * with some routemap prefix/access list match.
1243 		 */
1244 		ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1245 
1246 	} else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
1247 	{
1248 		/*
1249 		 * Check if default-information originate is
1250 		 * with some routemap prefix/access list match.
1251 		 * Apply before ei is deleted.
1252 		 */
1253 		ei = ospf_external_info_lookup(ospf, rt_type, api.instance, &p);
1254 		if (ei)
1255 			ospf_external_lsa_default_routemap_apply(ospf, ei, cmd);
1256 
1257 		ospf_external_info_delete(ospf, rt_type, api.instance, p);
1258 		if (is_prefix_default(&p))
1259 			ospf_external_lsa_refresh_default(ospf);
1260 		else
1261 			ospf_external_lsa_flush(ospf, rt_type, &p,
1262 						ifindex /*, nexthop */);
1263 	}
1264 
1265 
1266 	return 0;
1267 }
1268 
1269 
1270 int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name)
1271 {
1272 	/* Lookup access-list for distribute-list. */
1273 	DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name);
1274 
1275 	/* Clear previous distribute-name. */
1276 	if (DISTRIBUTE_NAME(ospf, type))
1277 		free(DISTRIBUTE_NAME(ospf, type));
1278 
1279 	/* Set distribute-name. */
1280 	DISTRIBUTE_NAME(ospf, type) = strdup(name);
1281 
1282 	/* If access-list have been set, schedule update timer. */
1283 	if (DISTRIBUTE_LIST(ospf, type))
1284 		ospf_distribute_list_update(ospf, type, 0);
1285 
1286 	return CMD_SUCCESS;
1287 }
1288 
1289 int ospf_distribute_list_out_unset(struct ospf *ospf, int type,
1290 				   const char *name)
1291 {
1292 	/* Schedule update timer. */
1293 	if (DISTRIBUTE_LIST(ospf, type))
1294 		ospf_distribute_list_update(ospf, type, 0);
1295 
1296 	/* Unset distribute-list. */
1297 	DISTRIBUTE_LIST(ospf, type) = NULL;
1298 
1299 	/* Clear distribute-name. */
1300 	if (DISTRIBUTE_NAME(ospf, type))
1301 		free(DISTRIBUTE_NAME(ospf, type));
1302 
1303 	DISTRIBUTE_NAME(ospf, type) = NULL;
1304 
1305 	return CMD_SUCCESS;
1306 }
1307 
1308 /* distribute-list update timer. */
1309 static int ospf_distribute_list_update_timer(struct thread *thread)
1310 {
1311 	struct route_node *rn;
1312 	struct external_info *ei;
1313 	struct route_table *rt;
1314 	struct ospf_lsa *lsa;
1315 	int type, default_refresh = 0, arg_type;
1316 	struct ospf *ospf = NULL;
1317 	void **arg = THREAD_ARG(thread);
1318 
1319 	ospf = (struct ospf *)arg[0];
1320 	arg_type = (int)(intptr_t)arg[1];
1321 
1322 	if (ospf == NULL)
1323 		return 0;
1324 
1325 	ospf->t_distribute_update = NULL;
1326 
1327 	zlog_info("Zebra[Redistribute]: distribute-list update timer fired!");
1328 
1329 	if (IS_DEBUG_OSPF_EVENT) {
1330 		zlog_debug(
1331 			"%s: ospf distribute-list update arg_type %d vrf %s id %d",
1332 			__func__, arg_type, ospf_vrf_id_to_name(ospf->vrf_id),
1333 			ospf->vrf_id);
1334 	}
1335 
1336 	/* foreach all external info. */
1337 	for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1338 		struct list *ext_list;
1339 		struct listnode *node;
1340 		struct ospf_external *ext;
1341 
1342 		ext_list = ospf->external[type];
1343 		if (!ext_list)
1344 			continue;
1345 
1346 		for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
1347 			rt = ext->external_info;
1348 			if (!rt)
1349 				continue;
1350 			for (rn = route_top(rt); rn; rn = route_next(rn))
1351 				if ((ei = rn->info) != NULL) {
1352 					if (is_prefix_default(&ei->p))
1353 						default_refresh = 1;
1354 					else if (
1355 						(lsa = ospf_external_info_find_lsa(
1356 							 ospf, &ei->p))) {
1357 						int force =
1358 							LSA_REFRESH_IF_CHANGED;
1359 						/* If this is a MaxAge LSA, we
1360 						 * need to force refresh it
1361 						 * because distribute settings
1362 						 * might have changed and now,
1363 						 * this LSA needs to be
1364 						 * originated, not be removed.
1365 						 * If we don't force refresh it,
1366 						 * it will remain a MaxAge LSA
1367 						 * because it will look like it
1368 						 * hasn't changed. Neighbors
1369 						 * will not receive updates for
1370 						 * this LSA.
1371 						 */
1372 						if (IS_LSA_MAXAGE(lsa))
1373 							force = LSA_REFRESH_FORCE;
1374 
1375 						ospf_external_lsa_refresh(
1376 							ospf, lsa, ei, force);
1377 					} else
1378 						ospf_external_lsa_originate(
1379 							ospf, ei);
1380 				}
1381 		}
1382 	}
1383 	if (default_refresh)
1384 		ospf_external_lsa_refresh_default(ospf);
1385 
1386 	XFREE(MTYPE_OSPF_DIST_ARGS, arg);
1387 	return 0;
1388 }
1389 
1390 /* Update distribute-list and set timer to apply access-list. */
1391 void ospf_distribute_list_update(struct ospf *ospf, int type,
1392 				 unsigned short instance)
1393 {
1394 	struct ospf_external *ext;
1395 	void **args = XCALLOC(MTYPE_OSPF_DIST_ARGS, sizeof(void *) * 2);
1396 
1397 	args[0] = ospf;
1398 	args[1] = (void *)((ptrdiff_t)type);
1399 
1400 	/* External info does not exist. */
1401 	ext = ospf_external_lookup(ospf, type, instance);
1402 	if (!ext || !EXTERNAL_INFO(ext)) {
1403 		XFREE(MTYPE_OSPF_DIST_ARGS, args);
1404 		return;
1405 	}
1406 
1407 	/* If exists previously invoked thread, then let it continue. */
1408 	if (ospf->t_distribute_update) {
1409 		XFREE(MTYPE_OSPF_DIST_ARGS, args);
1410 		return;
1411 	}
1412 
1413 	/* Set timer. */
1414 	ospf->t_distribute_update = NULL;
1415 	thread_add_timer_msec(master, ospf_distribute_list_update_timer, args,
1416 			      ospf->min_ls_interval,
1417 			      &ospf->t_distribute_update);
1418 }
1419 
1420 /* If access-list is updated, apply some check. */
1421 static void ospf_filter_update(struct access_list *access)
1422 {
1423 	struct ospf *ospf;
1424 	int type;
1425 	int abr_inv = 0;
1426 	struct ospf_area *area;
1427 	struct listnode *node, *n1;
1428 
1429 	/* If OSPF instance does not exist, return right now. */
1430 	if (listcount(om->ospf) == 0)
1431 		return;
1432 
1433 	/* Iterate all ospf [VRF] instances */
1434 	for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1435 		/* Update distribute-list, and apply filter. */
1436 		for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1437 			struct list *red_list;
1438 			struct ospf_redist *red;
1439 
1440 			red_list = ospf->redist[type];
1441 			if (red_list)
1442 				for (ALL_LIST_ELEMENTS_RO(red_list, node,
1443 							  red)) {
1444 					if (ROUTEMAP(red)) {
1445 						/* if route-map is not NULL it
1446 						 * may be
1447 						 * using this access list */
1448 						ospf_distribute_list_update(
1449 							ospf, type,
1450 							red->instance);
1451 					}
1452 				}
1453 
1454 			/* There is place for route-map for default-information
1455 			 * (ZEBRA_ROUTE_MAX),
1456 			 * but no distribute list. */
1457 			if (type == ZEBRA_ROUTE_MAX)
1458 				break;
1459 
1460 			if (DISTRIBUTE_NAME(ospf, type)) {
1461 				/* Keep old access-list for distribute-list. */
1462 				struct access_list *old =
1463 					DISTRIBUTE_LIST(ospf, type);
1464 
1465 				/* Update access-list for distribute-list. */
1466 				DISTRIBUTE_LIST(ospf, type) =
1467 					access_list_lookup(
1468 						AFI_IP,
1469 						DISTRIBUTE_NAME(ospf, type));
1470 
1471 				/* No update for this distribute type. */
1472 				if (old == NULL
1473 				    && DISTRIBUTE_LIST(ospf, type) == NULL)
1474 					continue;
1475 
1476 				/* Schedule distribute-list update timer. */
1477 				if (DISTRIBUTE_LIST(ospf, type) == NULL
1478 				    || strcmp(DISTRIBUTE_NAME(ospf, type),
1479 					      access->name)
1480 					       == 0)
1481 					ospf_distribute_list_update(ospf, type,
1482 								    0);
1483 			}
1484 		}
1485 
1486 		/* Update Area access-list. */
1487 		for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1488 			if (EXPORT_NAME(area)) {
1489 				EXPORT_LIST(area) = NULL;
1490 				abr_inv++;
1491 			}
1492 
1493 			if (IMPORT_NAME(area)) {
1494 				IMPORT_LIST(area) = NULL;
1495 				abr_inv++;
1496 			}
1497 		}
1498 
1499 		/* Schedule ABR tasks -- this will be changed -- takada. */
1500 		if (IS_OSPF_ABR(ospf) && abr_inv)
1501 			ospf_schedule_abr_task(ospf);
1502 	}
1503 }
1504 
1505 /* If prefix-list is updated, do some updates. */
1506 void ospf_prefix_list_update(struct prefix_list *plist)
1507 {
1508 	struct ospf *ospf = NULL;
1509 	int type;
1510 	int abr_inv = 0;
1511 	struct ospf_area *area;
1512 	struct listnode *node, *n1;
1513 
1514 	/* If OSPF instatnce does not exist, return right now. */
1515 	if (listcount(om->ospf) == 0)
1516 		return;
1517 
1518 	/* Iterate all ospf [VRF] instances */
1519 	for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) {
1520 
1521 		/* Update all route-maps which are used
1522 		 * as redistribution filters.
1523 		 * They might use prefix-list.
1524 		 */
1525 		for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
1526 			struct list *red_list;
1527 			struct ospf_redist *red;
1528 
1529 			red_list = ospf->redist[type];
1530 			if (red_list) {
1531 				for (ALL_LIST_ELEMENTS_RO(red_list, node,
1532 							  red)) {
1533 					if (ROUTEMAP(red)) {
1534 						/* if route-map is not NULL
1535 						 * it may be using
1536 						 * this prefix list */
1537 						ospf_distribute_list_update(
1538 							ospf, type,
1539 							red->instance);
1540 					}
1541 				}
1542 			}
1543 		}
1544 
1545 		/* Update area filter-lists. */
1546 		for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
1547 			/* Update filter-list in. */
1548 			if (PREFIX_NAME_IN(area))
1549 				if (strcmp(PREFIX_NAME_IN(area),
1550 					   prefix_list_name(plist))
1551 				    == 0) {
1552 					PREFIX_LIST_IN(area) =
1553 						prefix_list_lookup(
1554 							AFI_IP,
1555 							PREFIX_NAME_IN(area));
1556 					abr_inv++;
1557 				}
1558 
1559 			/* Update filter-list out. */
1560 			if (PREFIX_NAME_OUT(area))
1561 				if (strcmp(PREFIX_NAME_OUT(area),
1562 					   prefix_list_name(plist))
1563 				    == 0) {
1564 					PREFIX_LIST_IN(area) =
1565 						prefix_list_lookup(
1566 							AFI_IP,
1567 							PREFIX_NAME_OUT(area));
1568 					abr_inv++;
1569 				}
1570 		}
1571 
1572 		/* Schedule ABR task. */
1573 		if (IS_OSPF_ABR(ospf) && abr_inv)
1574 			ospf_schedule_abr_task(ospf);
1575 	}
1576 }
1577 
1578 static struct ospf_distance *ospf_distance_new(void)
1579 {
1580 	return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance));
1581 }
1582 
1583 static void ospf_distance_free(struct ospf_distance *odistance)
1584 {
1585 	XFREE(MTYPE_OSPF_DISTANCE, odistance);
1586 }
1587 
1588 int ospf_distance_set(struct vty *vty, struct ospf *ospf,
1589 		      const char *distance_str, const char *ip_str,
1590 		      const char *access_list_str)
1591 {
1592 	int ret;
1593 	struct prefix_ipv4 p;
1594 	uint8_t distance;
1595 	struct route_node *rn;
1596 	struct ospf_distance *odistance;
1597 
1598 	ret = str2prefix_ipv4(ip_str, &p);
1599 	if (ret == 0) {
1600 		vty_out(vty, "Malformed prefix\n");
1601 		return CMD_WARNING_CONFIG_FAILED;
1602 	}
1603 
1604 	distance = atoi(distance_str);
1605 
1606 	/* Get OSPF distance node. */
1607 	rn = route_node_get(ospf->distance_table, (struct prefix *)&p);
1608 	if (rn->info) {
1609 		odistance = rn->info;
1610 		route_unlock_node(rn);
1611 	} else {
1612 		odistance = ospf_distance_new();
1613 		rn->info = odistance;
1614 	}
1615 
1616 	/* Set distance value. */
1617 	odistance->distance = distance;
1618 
1619 	/* Reset access-list configuration. */
1620 	if (odistance->access_list) {
1621 		free(odistance->access_list);
1622 		odistance->access_list = NULL;
1623 	}
1624 	if (access_list_str)
1625 		odistance->access_list = strdup(access_list_str);
1626 
1627 	return CMD_SUCCESS;
1628 }
1629 
1630 int ospf_distance_unset(struct vty *vty, struct ospf *ospf,
1631 			const char *distance_str, const char *ip_str,
1632 			char const *access_list_str)
1633 {
1634 	int ret;
1635 	struct prefix_ipv4 p;
1636 	struct route_node *rn;
1637 	struct ospf_distance *odistance;
1638 
1639 	ret = str2prefix_ipv4(ip_str, &p);
1640 	if (ret == 0) {
1641 		vty_out(vty, "Malformed prefix\n");
1642 		return CMD_WARNING_CONFIG_FAILED;
1643 	}
1644 
1645 	rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p);
1646 	if (!rn) {
1647 		vty_out(vty, "Can't find specified prefix\n");
1648 		return CMD_WARNING_CONFIG_FAILED;
1649 	}
1650 
1651 	odistance = rn->info;
1652 
1653 	if (odistance->access_list)
1654 		free(odistance->access_list);
1655 	ospf_distance_free(odistance);
1656 
1657 	rn->info = NULL;
1658 	route_unlock_node(rn);
1659 	route_unlock_node(rn);
1660 
1661 	return CMD_SUCCESS;
1662 }
1663 
1664 void ospf_distance_reset(struct ospf *ospf)
1665 {
1666 	struct route_node *rn;
1667 	struct ospf_distance *odistance;
1668 
1669 	for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn))
1670 		if ((odistance = rn->info) != NULL) {
1671 			if (odistance->access_list)
1672 				free(odistance->access_list);
1673 			ospf_distance_free(odistance);
1674 			rn->info = NULL;
1675 			route_unlock_node(rn);
1676 		}
1677 }
1678 
1679 uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p,
1680 			    struct ospf_route * or)
1681 {
1682 
1683 	if (ospf == NULL)
1684 		return 0;
1685 
1686 	if (ospf->distance_intra)
1687 		if (or->path_type == OSPF_PATH_INTRA_AREA)
1688 			return ospf->distance_intra;
1689 
1690 	if (ospf->distance_inter)
1691 		if (or->path_type == OSPF_PATH_INTER_AREA)
1692 			return ospf->distance_inter;
1693 
1694 	if (ospf->distance_external)
1695 		if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL ||
1696 		    or->path_type == OSPF_PATH_TYPE2_EXTERNAL)
1697 			return ospf->distance_external;
1698 
1699 	if (ospf->distance_all)
1700 		return ospf->distance_all;
1701 
1702 	return 0;
1703 }
1704 
1705 void ospf_zebra_vrf_register(struct ospf *ospf)
1706 {
1707 	if (!zclient || zclient->sock < 0 || !ospf)
1708 		return;
1709 
1710 	if (ospf->vrf_id != VRF_UNKNOWN) {
1711 		if (IS_DEBUG_OSPF_EVENT)
1712 			zlog_debug("%s: Register VRF %s id %u", __func__,
1713 				   ospf_vrf_id_to_name(ospf->vrf_id),
1714 				   ospf->vrf_id);
1715 		zclient_send_reg_requests(zclient, ospf->vrf_id);
1716 	}
1717 }
1718 
1719 void ospf_zebra_vrf_deregister(struct ospf *ospf)
1720 {
1721 	if (!zclient || zclient->sock < 0 || !ospf)
1722 		return;
1723 
1724 	if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) {
1725 		if (IS_DEBUG_OSPF_EVENT)
1726 			zlog_debug("%s: De-Register VRF %s id %u to Zebra.",
1727 				   __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1728 				   ospf->vrf_id);
1729 		/* Deregister for router-id, interfaces,
1730 		 * redistributed routes. */
1731 		zclient_send_dereg_requests(zclient, ospf->vrf_id);
1732 	}
1733 }
1734 
1735 /* Label Manager Functions */
1736 
1737 /**
1738  * Check if Label Manager is Ready or not.
1739  *
1740  * @return	True if Label Manager is ready, False otherwise
1741  */
1742 bool ospf_zebra_label_manager_ready(void)
1743 {
1744 	return (zclient_sync->sock > 0);
1745 }
1746 
1747 /**
1748  * Request Label Range to the Label Manager.
1749  *
1750  * @param base		base label of the label range to request
1751  * @param chunk_size	size of the label range to request
1752  *
1753  * @return 	0 on success, -1 on failure
1754  */
1755 int ospf_zebra_request_label_range(uint32_t base, uint32_t chunk_size)
1756 {
1757 	int ret;
1758 	uint32_t start, end;
1759 
1760 	if (zclient_sync->sock < 0)
1761 		return -1;
1762 
1763 	ret = lm_get_label_chunk(zclient_sync, 0, base, chunk_size, &start,
1764 				 &end);
1765 	if (ret < 0) {
1766 		zlog_warn("%s: error getting label range!", __func__);
1767 		return -1;
1768 	}
1769 
1770 	return 0;
1771 }
1772 
1773 /**
1774  * Release Label Range to the Label Manager.
1775  *
1776  * @param start		start of label range to release
1777  * @param end		end of label range to release
1778  *
1779  * @return		0 on success, -1 otherwise
1780  */
1781 int ospf_zebra_release_label_range(uint32_t start, uint32_t end)
1782 {
1783 	int ret;
1784 
1785 	if (zclient_sync->sock < 0)
1786 		return -1;
1787 
1788 	ret = lm_release_label_chunk(zclient_sync, start, end);
1789 	if (ret < 0) {
1790 		zlog_warn("%s: error releasing label range!", __func__);
1791 		return -1;
1792 	}
1793 
1794 	return 0;
1795 }
1796 
1797 /**
1798  * Connect to the Label Manager.
1799  *
1800  * @return	0 on success, -1 otherwise
1801  */
1802 int ospf_zebra_label_manager_connect(void)
1803 {
1804 	/* Connect to label manager. */
1805 	if (zclient_socket_connect(zclient_sync) < 0) {
1806 		zlog_warn("%s: failed connecting synchronous zclient!",
1807 			  __func__);
1808 		return -1;
1809 	}
1810 	/* make socket non-blocking */
1811 	set_nonblocking(zclient_sync->sock);
1812 
1813 	/* Send hello to notify zebra this is a synchronous client */
1814 	if (zclient_send_hello(zclient_sync) < 0) {
1815 		zlog_warn("%s: failed sending hello for synchronous zclient!",
1816 			  __func__);
1817 		close(zclient_sync->sock);
1818 		zclient_sync->sock = -1;
1819 		return -1;
1820 	}
1821 
1822 	/* Connect to label manager */
1823 	if (lm_label_manager_connect(zclient_sync, 0) != 0) {
1824 		zlog_warn("%s: failed connecting to label manager!", __func__);
1825 		if (zclient_sync->sock > 0) {
1826 			close(zclient_sync->sock);
1827 			zclient_sync->sock = -1;
1828 		}
1829 		return -1;
1830 	}
1831 
1832 	osr_debug("SR (%s): Successfully connected to the Label Manager",
1833 		  __func__);
1834 
1835 	return 0;
1836 }
1837 
1838 static void ospf_zebra_connected(struct zclient *zclient)
1839 {
1840 	/* Send the client registration */
1841 	bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
1842 
1843 	zclient_send_reg_requests(zclient, VRF_DEFAULT);
1844 }
1845 
1846 void ospf_zebra_init(struct thread_master *master, unsigned short instance)
1847 {
1848 	/* Allocate zebra structure. */
1849 	zclient = zclient_new(master, &zclient_options_default);
1850 	zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs);
1851 	zclient->zebra_connected = ospf_zebra_connected;
1852 	zclient->router_id_update = ospf_router_id_update_zebra;
1853 	zclient->interface_address_add = ospf_interface_address_add;
1854 	zclient->interface_address_delete = ospf_interface_address_delete;
1855 	zclient->interface_link_params = ospf_interface_link_params;
1856 	zclient->interface_vrf_update = ospf_interface_vrf_update;
1857 
1858 	zclient->redistribute_route_add = ospf_zebra_read_route;
1859 	zclient->redistribute_route_del = ospf_zebra_read_route;
1860 
1861 	/* Initialize special zclient for synchronous message exchanges. */
1862 	struct zclient_options options = zclient_options_default;
1863 	options.synchronous = true;
1864 	zclient_sync = zclient_new(master, &options);
1865 	zclient_sync->sock = -1;
1866 	zclient_sync->redist_default = ZEBRA_ROUTE_OSPF;
1867 	zclient_sync->instance = instance;
1868 	/*
1869 	 * session_id must be different from default value (0) to distinguish
1870 	 * the asynchronous socket from the synchronous one
1871 	 */
1872 	zclient_sync->session_id = 1;
1873 	zclient_sync->privs = &ospfd_privs;
1874 
1875 	access_list_add_hook(ospf_filter_update);
1876 	access_list_delete_hook(ospf_filter_update);
1877 	prefix_list_add_hook(ospf_prefix_list_update);
1878 	prefix_list_delete_hook(ospf_prefix_list_update);
1879 }
1880 
1881 void ospf_zebra_send_arp(const struct interface *ifp, const struct prefix *p)
1882 {
1883 	zclient_send_neigh_discovery_req(zclient, ifp, p);
1884 }
1885