1 /* Route map function of bgpd.
2  * Copyright (C) 1998, 1999 Kunihiro Ishiguro
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  *
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
19  */
20 
21 #include <zebra.h>
22 
23 #include "prefix.h"
24 #include "filter.h"
25 #include "routemap.h"
26 #include "command.h"
27 #include "linklist.h"
28 #include "plist.h"
29 #include "memory.h"
30 #include "log.h"
31 #include "frrlua.h"
32 #ifdef HAVE_LIBPCREPOSIX
33 #include <pcreposix.h>
34 #else
35 #include <regex.h>
36 #endif /* HAVE_LIBPCREPOSIX */
37 #include "buffer.h"
38 #include "sockunion.h"
39 #include "hash.h"
40 #include "queue.h"
41 #include "frrstr.h"
42 #include "network.h"
43 
44 #include "bgpd/bgpd.h"
45 #include "bgpd/bgp_table.h"
46 #include "bgpd/bgp_attr.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_packet.h"
49 #include "bgpd/bgp_route.h"
50 #include "bgpd/bgp_zebra.h"
51 #include "bgpd/bgp_regex.h"
52 #include "bgpd/bgp_community.h"
53 #include "bgpd/bgp_clist.h"
54 #include "bgpd/bgp_filter.h"
55 #include "bgpd/bgp_mplsvpn.h"
56 #include "bgpd/bgp_ecommunity.h"
57 #include "bgpd/bgp_lcommunity.h"
58 #include "bgpd/bgp_vty.h"
59 #include "bgpd/bgp_debug.h"
60 #include "bgpd/bgp_evpn.h"
61 #include "bgpd/bgp_evpn_private.h"
62 #include "bgpd/bgp_evpn_vty.h"
63 #include "bgpd/bgp_mplsvpn.h"
64 #include "bgpd/bgp_pbr.h"
65 #include "bgpd/bgp_flowspec_util.h"
66 #include "bgpd/bgp_encap_types.h"
67 #include "bgpd/bgp_mpath.h"
68 
69 #ifdef ENABLE_BGP_VNC
70 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
71 #endif
72 
73 #ifndef VTYSH_EXTRACT_PL
74 #include "bgpd/bgp_routemap_clippy.c"
75 #endif
76 
77 /* Memo of route-map commands.
78 
79 o Cisco route-map
80 
81  match as-path          :  Done
82        community        :  Done
83        interface        :  Done
84        ip address       :  Done
85        ip next-hop      :  Done
86        ip route-source  :  Done
87        ip prefix-list   :  Done
88        ipv6 address     :  Done
89        ipv6 next-hop    :  Done
90        ipv6 route-source:  (This will not be implemented by bgpd)
91        ipv6 prefix-list :  Done
92        length           :  (This will not be implemented by bgpd)
93        metric           :  Done
94        route-type       :  (This will not be implemented by bgpd)
95        tag              :  Done
96        local-preference :  Done
97 
98  set  as-path prepend   :  Done
99       as-path tag       :  Not yet
100       automatic-tag     :  (This will not be implemented by bgpd)
101       community         :  Done
102       large-community   :  Done
103       large-comm-list   :  Done
104       comm-list         :  Not yet
105       dampning          :  Not yet
106       default           :  (This will not be implemented by bgpd)
107       interface         :  (This will not be implemented by bgpd)
108       ip default        :  (This will not be implemented by bgpd)
109       ip next-hop       :  Done
110       ip precedence     :  (This will not be implemented by bgpd)
111       ip tos            :  (This will not be implemented by bgpd)
112       level             :  (This will not be implemented by bgpd)
113       local-preference  :  Done
114       metric            :  Done
115       metric-type       :  Not yet
116       origin            :  Done
117       tag               :  Done
118       weight            :  Done
119       table             :  Done
120 
121 o Local extensions
122 
123   set ipv6 next-hop global: Done
124   set ipv6 next-hop prefer-global: Done
125   set ipv6 next-hop local : Done
126   set as-path exclude     : Done
127 
128 */
129 
130 /* generic value manipulation to be shared in multiple rules */
131 
132 #define RMAP_VALUE_SET 0
133 #define RMAP_VALUE_ADD 1
134 #define RMAP_VALUE_SUB 2
135 
136 struct rmap_value {
137 	uint8_t action;
138 	uint8_t variable;
139 	uint32_t value;
140 };
141 
route_value_match(struct rmap_value * rv,uint32_t value)142 static int route_value_match(struct rmap_value *rv, uint32_t value)
143 {
144 	if (rv->variable == 0 && value == rv->value)
145 		return RMAP_MATCH;
146 
147 	return RMAP_NOMATCH;
148 }
149 
route_value_adjust(struct rmap_value * rv,uint32_t current,struct peer * peer)150 static uint32_t route_value_adjust(struct rmap_value *rv, uint32_t current,
151 				   struct peer *peer)
152 {
153 	uint32_t value;
154 
155 	switch (rv->variable) {
156 	case 1:
157 		value = peer->rtt;
158 		break;
159 	default:
160 		value = rv->value;
161 		break;
162 	}
163 
164 	switch (rv->action) {
165 	case RMAP_VALUE_ADD:
166 		if (current > UINT32_MAX - value)
167 			return UINT32_MAX;
168 		return current + value;
169 	case RMAP_VALUE_SUB:
170 		if (current <= value)
171 			return 0;
172 		return current - value;
173 	default:
174 		return value;
175 	}
176 }
177 
route_value_compile(const char * arg)178 static void *route_value_compile(const char *arg)
179 {
180 	uint8_t action = RMAP_VALUE_SET, var = 0;
181 	unsigned long larg = 0;
182 	char *endptr = NULL;
183 	struct rmap_value *rv;
184 
185 	if (arg[0] == '+') {
186 		action = RMAP_VALUE_ADD;
187 		arg++;
188 	} else if (arg[0] == '-') {
189 		action = RMAP_VALUE_SUB;
190 		arg++;
191 	}
192 
193 	if (all_digit(arg)) {
194 		errno = 0;
195 		larg = strtoul(arg, &endptr, 10);
196 		if (*arg == 0 || *endptr != 0 || errno || larg > UINT32_MAX)
197 			return NULL;
198 	} else {
199 		if (strcmp(arg, "rtt") == 0)
200 			var = 1;
201 		else
202 			return NULL;
203 	}
204 
205 	rv = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_value));
206 
207 	rv->action = action;
208 	rv->variable = var;
209 	rv->value = larg;
210 	return rv;
211 }
212 
route_value_free(void * rule)213 static void route_value_free(void *rule)
214 {
215 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
216 }
217 
218 /* generic as path object to be shared in multiple rules */
219 
route_aspath_compile(const char * arg)220 static void *route_aspath_compile(const char *arg)
221 {
222 	struct aspath *aspath;
223 
224 	aspath = aspath_str2aspath(arg);
225 	if (!aspath)
226 		return NULL;
227 	return aspath;
228 }
229 
route_aspath_free(void * rule)230 static void route_aspath_free(void *rule)
231 {
232 	struct aspath *aspath = rule;
233 	aspath_free(aspath);
234 }
235 
236 struct bgp_match_peer_compiled {
237 	char *interface;
238 	union sockunion su;
239 };
240 
241 /* 'match peer (A.B.C.D|X:X::X:X|WORD)' */
242 
243 /* Compares the peer specified in the 'match peer' clause with the peer
244     received in bgp_path_info->peer. If it is the same, or if the peer structure
245     received is a peer_group containing it, returns RMAP_MATCH. */
246 static enum route_map_cmd_result_t
route_match_peer(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)247 route_match_peer(void *rule, const struct prefix *prefix,
248 		 route_map_object_t type, void *object)
249 {
250 	struct bgp_match_peer_compiled *pc;
251 	union sockunion *su;
252 	union sockunion su_def = {
253 		.sin = {.sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY}};
254 	struct peer_group *group;
255 	struct peer *peer;
256 	struct listnode *node, *nnode;
257 
258 	if (type == RMAP_BGP) {
259 		pc = rule;
260 		su = &pc->su;
261 		peer = ((struct bgp_path_info *)object)->peer;
262 
263 		if (pc->interface) {
264 			if (!peer->conf_if)
265 				return RMAP_NOMATCH;
266 
267 			if (strcmp(peer->conf_if, pc->interface) == 0)
268 				return RMAP_MATCH;
269 
270 			return RMAP_NOMATCH;
271 		}
272 
273 		/* If su='0.0.0.0' (command 'match peer local'), and it's a
274 		   NETWORK,
275 		    REDISTRIBUTE, AGGREGATE-ADDRESS or DEFAULT_GENERATED route
276 		   => return RMAP_MATCH
277 		   */
278 		if (sockunion_same(su, &su_def)) {
279 			int ret;
280 			if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_NETWORK)
281 			    || CHECK_FLAG(peer->rmap_type,
282 					  PEER_RMAP_TYPE_REDISTRIBUTE)
283 			    || CHECK_FLAG(peer->rmap_type,
284 					  PEER_RMAP_TYPE_AGGREGATE)
285 			    || CHECK_FLAG(peer->rmap_type,
286 					  PEER_RMAP_TYPE_DEFAULT))
287 				ret = RMAP_MATCH;
288 			else
289 				ret = RMAP_NOMATCH;
290 			return ret;
291 		}
292 
293 		if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
294 			if (sockunion_same(su, &peer->su))
295 				return RMAP_MATCH;
296 
297 			return RMAP_NOMATCH;
298 		} else {
299 			group = peer->group;
300 			for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
301 					       peer)) {
302 				if (sockunion_same(su, &peer->su))
303 					return RMAP_MATCH;
304 			}
305 			return RMAP_NOMATCH;
306 		}
307 	}
308 	return RMAP_NOMATCH;
309 }
310 
route_match_peer_compile(const char * arg)311 static void *route_match_peer_compile(const char *arg)
312 {
313 	struct bgp_match_peer_compiled *pc;
314 	int ret;
315 
316 	pc = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
317 		     sizeof(struct bgp_match_peer_compiled));
318 
319 	ret = str2sockunion(strcmp(arg, "local") ? arg : "0.0.0.0", &pc->su);
320 	if (ret < 0) {
321 		pc->interface = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
322 		return pc;
323 	}
324 
325 	return pc;
326 }
327 
328 /* Free route map's compiled `ip address' value. */
route_match_peer_free(void * rule)329 static void route_match_peer_free(void *rule)
330 {
331 	struct bgp_match_peer_compiled *pc = rule;
332 
333 	XFREE(MTYPE_ROUTE_MAP_COMPILED, pc->interface);
334 
335 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
336 }
337 
338 /* Route map commands for ip address matching. */
339 static const struct route_map_rule_cmd route_match_peer_cmd = {
340 	"peer",
341 	route_match_peer,
342 	route_match_peer_compile,
343 	route_match_peer_free
344 };
345 
346 #if defined(HAVE_LUA)
347 static enum route_map_cmd_result_t
route_match_command(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)348 route_match_command(void *rule, const struct prefix *prefix,
349 		    route_map_object_t type, void *object)
350 {
351 	int status = RMAP_NOMATCH;
352 	u_int32_t locpref = 0;
353 	u_int32_t newlocpref = 0;
354 	enum lua_rm_status lrm_status;
355 	struct bgp_path_info *path = (struct bgp_path_info *)object;
356 	lua_State *L = lua_initialize("/etc/frr/lua.scr");
357 
358 	if (L == NULL)
359 		return status;
360 
361 	/*
362 	 * Setup the prefix information to pass in
363 	 */
364 	lua_setup_prefix_table(L, prefix);
365 
366 	zlog_debug("Set up prefix table");
367 	/*
368 	 * Setup the bgp_path_info information
369 	 */
370 	lua_newtable(L);
371 	lua_pushinteger(L, path->attr->med);
372 	lua_setfield(L, -2, "metric");
373 	lua_pushinteger(L, path->attr->nh_ifindex);
374 	lua_setfield(L, -2, "ifindex");
375 	lua_pushstring(L, path->attr->aspath->str);
376 	lua_setfield(L, -2, "aspath");
377 	lua_pushinteger(L, path->attr->local_pref);
378 	lua_setfield(L, -2, "localpref");
379 	zlog_debug("%s %d", path->attr->aspath->str, path->attr->nh_ifindex);
380 	lua_setglobal(L, "nexthop");
381 
382 	zlog_debug("Set up nexthop information");
383 	/*
384 	 * Run the rule
385 	 */
386 	lrm_status = lua_run_rm_rule(L, rule);
387 	switch (lrm_status) {
388 	case LUA_RM_FAILURE:
389 		zlog_debug("RM_FAILURE");
390 		break;
391 	case LUA_RM_NOMATCH:
392 		zlog_debug("RM_NOMATCH");
393 		break;
394 	case LUA_RM_MATCH_AND_CHANGE:
395 		zlog_debug("MATCH AND CHANGE");
396 		lua_getglobal(L, "nexthop");
397 		path->attr->med = get_integer(L, "metric");
398 		/*
399 		 * This needs to be abstraced with the set function
400 		 */
401 		if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
402 			locpref = path->attr->local_pref;
403 		newlocpref = get_integer(L, "localpref");
404 		if (newlocpref != locpref) {
405 			path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
406 			path->attr->local_pref = newlocpref;
407 		}
408 		status = RMAP_MATCH;
409 		break;
410 	case LUA_RM_MATCH:
411 		zlog_debug("MATCH ONLY");
412 		status = RMAP_MATCH;
413 		break;
414 	}
415 	lua_close(L);
416 	return status;
417 }
418 
route_match_command_compile(const char * arg)419 static void *route_match_command_compile(const char *arg)
420 {
421 	char *command;
422 
423 	command = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
424 	return command;
425 }
426 
427 static void
route_match_command_free(void * rule)428 route_match_command_free(void *rule)
429 {
430 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
431 }
432 
433 static const struct route_map_rule_cmd route_match_command_cmd = {
434 	"command",
435 	route_match_command,
436 	route_match_command_compile,
437 	route_match_command_free
438 };
439 #endif
440 
441 /* `match ip address IP_ACCESS_LIST' */
442 
443 /* Match function should return 1 if match is success else return
444    zero. */
445 static enum route_map_cmd_result_t
route_match_ip_address(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)446 route_match_ip_address(void *rule, const struct prefix *prefix,
447 		       route_map_object_t type, void *object)
448 {
449 	struct access_list *alist;
450 
451 	if (type == RMAP_BGP && prefix->family == AF_INET) {
452 		alist = access_list_lookup(AFI_IP, (char *)rule);
453 		if (alist == NULL)
454 			return RMAP_NOMATCH;
455 
456 		return (access_list_apply(alist, prefix) == FILTER_DENY
457 				? RMAP_NOMATCH
458 				: RMAP_MATCH);
459 	}
460 	return RMAP_NOMATCH;
461 }
462 
463 /* Route map `ip address' match statement.  `arg' should be
464    access-list name. */
route_match_ip_address_compile(const char * arg)465 static void *route_match_ip_address_compile(const char *arg)
466 {
467 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
468 }
469 
470 /* Free route map's compiled `ip address' value. */
route_match_ip_address_free(void * rule)471 static void route_match_ip_address_free(void *rule)
472 {
473 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
474 }
475 
476 /* Route map commands for ip address matching. */
477 static const struct route_map_rule_cmd route_match_ip_address_cmd = {
478 	"ip address",
479 	route_match_ip_address,
480 	route_match_ip_address_compile,
481 	route_match_ip_address_free
482 };
483 
484 /* `match ip next-hop IP_ADDRESS' */
485 
486 /* Match function return 1 if match is success else return zero. */
487 static enum route_map_cmd_result_t
route_match_ip_next_hop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)488 route_match_ip_next_hop(void *rule, const struct prefix *prefix,
489 			route_map_object_t type, void *object)
490 {
491 	struct access_list *alist;
492 	struct bgp_path_info *path;
493 	struct prefix_ipv4 p;
494 
495 	if (type == RMAP_BGP && prefix->family == AF_INET) {
496 		path = object;
497 		p.family = AF_INET;
498 		p.prefix = path->attr->nexthop;
499 		p.prefixlen = IPV4_MAX_BITLEN;
500 
501 		alist = access_list_lookup(AFI_IP, (char *)rule);
502 		if (alist == NULL)
503 			return RMAP_NOMATCH;
504 
505 		return (access_list_apply(alist, &p) == FILTER_DENY
506 				? RMAP_NOMATCH
507 				: RMAP_MATCH);
508 	}
509 	return RMAP_NOMATCH;
510 }
511 
512 /* Route map `ip next-hop' match statement. `arg' is
513    access-list name. */
route_match_ip_next_hop_compile(const char * arg)514 static void *route_match_ip_next_hop_compile(const char *arg)
515 {
516 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
517 }
518 
519 /* Free route map's compiled `ip address' value. */
route_match_ip_next_hop_free(void * rule)520 static void route_match_ip_next_hop_free(void *rule)
521 {
522 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
523 }
524 
525 /* Route map commands for ip next-hop matching. */
526 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd = {
527 	"ip next-hop",
528 	route_match_ip_next_hop,
529 	route_match_ip_next_hop_compile,
530 	route_match_ip_next_hop_free
531 };
532 
533 /* `match ip route-source ACCESS-LIST' */
534 
535 /* Match function return 1 if match is success else return zero. */
536 static enum route_map_cmd_result_t
route_match_ip_route_source(void * rule,const struct prefix * pfx,route_map_object_t type,void * object)537 route_match_ip_route_source(void *rule, const struct prefix *pfx,
538 			    route_map_object_t type, void *object)
539 {
540 	struct access_list *alist;
541 	struct bgp_path_info *path;
542 	struct peer *peer;
543 	struct prefix_ipv4 p;
544 
545 	if (type == RMAP_BGP && pfx->family == AF_INET) {
546 		path = object;
547 		peer = path->peer;
548 
549 		if (!peer || sockunion_family(&peer->su) != AF_INET)
550 			return RMAP_NOMATCH;
551 
552 		p.family = AF_INET;
553 		p.prefix = peer->su.sin.sin_addr;
554 		p.prefixlen = IPV4_MAX_BITLEN;
555 
556 		alist = access_list_lookup(AFI_IP, (char *)rule);
557 		if (alist == NULL)
558 			return RMAP_NOMATCH;
559 
560 		return (access_list_apply(alist, &p) == FILTER_DENY
561 				? RMAP_NOMATCH
562 				: RMAP_MATCH);
563 	}
564 	return RMAP_NOMATCH;
565 }
566 
567 /* Route map `ip route-source' match statement. `arg' is
568    access-list name. */
route_match_ip_route_source_compile(const char * arg)569 static void *route_match_ip_route_source_compile(const char *arg)
570 {
571 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
572 }
573 
574 /* Free route map's compiled `ip address' value. */
route_match_ip_route_source_free(void * rule)575 static void route_match_ip_route_source_free(void *rule)
576 {
577 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
578 }
579 
580 /* Route map commands for ip route-source matching. */
581 static const struct route_map_rule_cmd route_match_ip_route_source_cmd = {
582 	"ip route-source",
583 	route_match_ip_route_source,
584 	route_match_ip_route_source_compile,
585 	route_match_ip_route_source_free
586 };
587 
588 static enum route_map_cmd_result_t
route_match_prefix_list_flowspec(afi_t afi,struct prefix_list * plist,const struct prefix * p)589 route_match_prefix_list_flowspec(afi_t afi, struct prefix_list *plist,
590 				 const struct prefix *p)
591 {
592 	int ret;
593 	struct bgp_pbr_entry_main api;
594 
595 	memset(&api, 0, sizeof(api));
596 
597 	if (family2afi(p->u.prefix_flowspec.family) != afi)
598 		return RMAP_NOMATCH;
599 
600 	/* extract match from flowspec entries */
601 	ret = bgp_flowspec_match_rules_fill(
602 					    (uint8_t *)p->u.prefix_flowspec.ptr,
603 					    p->u.prefix_flowspec.prefixlen, &api,
604 					    afi);
605 	if (ret < 0)
606 		return RMAP_NOMATCH;
607 	if (api.match_bitmask & PREFIX_DST_PRESENT ||
608 	    api.match_bitmask_iprule & PREFIX_DST_PRESENT) {
609 		if (family2afi((&api.dst_prefix)->family) != afi)
610 			return RMAP_NOMATCH;
611 		return prefix_list_apply(plist, &api.dst_prefix) == PREFIX_DENY
612 			? RMAP_NOMATCH
613 			: RMAP_MATCH;
614 	} else if (api.match_bitmask & PREFIX_SRC_PRESENT ||
615 		   api.match_bitmask_iprule & PREFIX_SRC_PRESENT) {
616 		if (family2afi((&api.src_prefix)->family) != afi)
617 			return RMAP_NOMATCH;
618 		return (prefix_list_apply(plist, &api.src_prefix) == PREFIX_DENY
619 			? RMAP_NOMATCH
620 			: RMAP_MATCH);
621 	}
622 	return RMAP_NOMATCH;
623 }
624 
625 static enum route_map_cmd_result_t
route_match_address_prefix_list(void * rule,afi_t afi,const struct prefix * prefix,route_map_object_t type,void * object)626 route_match_address_prefix_list(void *rule, afi_t afi,
627 				const struct prefix *prefix,
628 				route_map_object_t type, void *object)
629 {
630 	struct prefix_list *plist;
631 
632 	if (type != RMAP_BGP)
633 		return RMAP_NOMATCH;
634 
635 	plist = prefix_list_lookup(afi, (char *)rule);
636 	if (plist == NULL)
637 		return RMAP_NOMATCH;
638 
639 	if (prefix->family == AF_FLOWSPEC)
640 		return route_match_prefix_list_flowspec(afi, plist,
641 							prefix);
642 	return (prefix_list_apply(plist, prefix) == PREFIX_DENY ? RMAP_NOMATCH
643 								: RMAP_MATCH);
644 }
645 
646 static enum route_map_cmd_result_t
route_match_ip_address_prefix_list(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)647 route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix,
648 				   route_map_object_t type, void *object)
649 {
650 	return route_match_address_prefix_list(rule, AFI_IP, prefix, type,
651 					       object);
652 }
653 
route_match_ip_address_prefix_list_compile(const char * arg)654 static void *route_match_ip_address_prefix_list_compile(const char *arg)
655 {
656 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
657 }
658 
route_match_ip_address_prefix_list_free(void * rule)659 static void route_match_ip_address_prefix_list_free(void *rule)
660 {
661 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
662 }
663 
664 static const struct route_map_rule_cmd
665 		route_match_ip_address_prefix_list_cmd = {
666 	"ip address prefix-list",
667 	route_match_ip_address_prefix_list,
668 	route_match_ip_address_prefix_list_compile,
669 	route_match_ip_address_prefix_list_free
670 };
671 
672 /* `match ip next-hop prefix-list PREFIX_LIST' */
673 
674 static enum route_map_cmd_result_t
route_match_ip_next_hop_prefix_list(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)675 route_match_ip_next_hop_prefix_list(void *rule, const struct prefix *prefix,
676 				    route_map_object_t type, void *object)
677 {
678 	struct prefix_list *plist;
679 	struct bgp_path_info *path;
680 	struct prefix_ipv4 p;
681 
682 	if (type == RMAP_BGP && prefix->family == AF_INET) {
683 		path = object;
684 		p.family = AF_INET;
685 		p.prefix = path->attr->nexthop;
686 		p.prefixlen = IPV4_MAX_BITLEN;
687 
688 		plist = prefix_list_lookup(AFI_IP, (char *)rule);
689 		if (plist == NULL)
690 			return RMAP_NOMATCH;
691 
692 		return (prefix_list_apply(plist, &p) == PREFIX_DENY
693 				? RMAP_NOMATCH
694 				: RMAP_MATCH);
695 	}
696 	return RMAP_NOMATCH;
697 }
698 
route_match_ip_next_hop_prefix_list_compile(const char * arg)699 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
700 {
701 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
702 }
703 
route_match_ip_next_hop_prefix_list_free(void * rule)704 static void route_match_ip_next_hop_prefix_list_free(void *rule)
705 {
706 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
707 }
708 
709 static const struct route_map_rule_cmd
710 		route_match_ip_next_hop_prefix_list_cmd = {
711 	"ip next-hop prefix-list",
712 	route_match_ip_next_hop_prefix_list,
713 	route_match_ip_next_hop_prefix_list_compile,
714 	route_match_ip_next_hop_prefix_list_free
715 };
716 
717 /* `match ip next-hop type <blackhole>' */
718 
719 static enum route_map_cmd_result_t
route_match_ip_next_hop_type(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)720 route_match_ip_next_hop_type(void *rule, const struct prefix *prefix,
721 			     route_map_object_t type, void *object)
722 {
723 	struct bgp_path_info *path;
724 
725 	if (type == RMAP_BGP && prefix->family == AF_INET) {
726 		path = (struct bgp_path_info *)object;
727 		if (!path)
728 			return RMAP_NOMATCH;
729 
730 		/* If nexthop interface's index can't be resolved and nexthop is
731 		   set to any address then mark it as type `blackhole`.
732 		   This logic works for matching kernel/static routes like:
733 		   `ip route add blackhole 10.0.0.1`. */
734 		if (path->attr->nexthop.s_addr == INADDR_ANY
735 		    && !path->attr->nh_ifindex)
736 			return RMAP_MATCH;
737 	}
738 	return RMAP_NOMATCH;
739 }
740 
route_match_ip_next_hop_type_compile(const char * arg)741 static void *route_match_ip_next_hop_type_compile(const char *arg)
742 {
743 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
744 }
745 
route_match_ip_next_hop_type_free(void * rule)746 static void route_match_ip_next_hop_type_free(void *rule)
747 {
748 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
749 }
750 
751 static const struct route_map_rule_cmd
752 		route_match_ip_next_hop_type_cmd = {
753 	"ip next-hop type",
754 	route_match_ip_next_hop_type,
755 	route_match_ip_next_hop_type_compile,
756 	route_match_ip_next_hop_type_free
757 };
758 
759 /* `match ip route-source prefix-list PREFIX_LIST' */
760 
761 static enum route_map_cmd_result_t
route_match_ip_route_source_prefix_list(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)762 route_match_ip_route_source_prefix_list(void *rule,
763 					const struct prefix *prefix,
764 					route_map_object_t type, void *object)
765 {
766 	struct prefix_list *plist;
767 	struct bgp_path_info *path;
768 	struct peer *peer;
769 	struct prefix_ipv4 p;
770 
771 	if (type == RMAP_BGP && prefix->family == AF_INET) {
772 		path = object;
773 		peer = path->peer;
774 
775 		if (!peer || sockunion_family(&peer->su) != AF_INET)
776 			return RMAP_NOMATCH;
777 
778 		p.family = AF_INET;
779 		p.prefix = peer->su.sin.sin_addr;
780 		p.prefixlen = IPV4_MAX_BITLEN;
781 
782 		plist = prefix_list_lookup(AFI_IP, (char *)rule);
783 		if (plist == NULL)
784 			return RMAP_NOMATCH;
785 
786 		return (prefix_list_apply(plist, &p) == PREFIX_DENY
787 				? RMAP_NOMATCH
788 				: RMAP_MATCH);
789 	}
790 	return RMAP_NOMATCH;
791 }
792 
route_match_ip_route_source_prefix_list_compile(const char * arg)793 static void *route_match_ip_route_source_prefix_list_compile(const char *arg)
794 {
795 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
796 }
797 
route_match_ip_route_source_prefix_list_free(void * rule)798 static void route_match_ip_route_source_prefix_list_free(void *rule)
799 {
800 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
801 }
802 
803 static const struct route_map_rule_cmd
804 		route_match_ip_route_source_prefix_list_cmd = {
805 	"ip route-source prefix-list",
806 	route_match_ip_route_source_prefix_list,
807 	route_match_ip_route_source_prefix_list_compile,
808 	route_match_ip_route_source_prefix_list_free
809 };
810 
811 /* `match evpn default-route' */
812 
813 /* Match function should return 1 if match is success else 0 */
814 static enum route_map_cmd_result_t
route_match_evpn_default_route(void * rule,const struct prefix * p,route_map_object_t type,void * object)815 route_match_evpn_default_route(void *rule, const struct prefix *p,
816 			       route_map_object_t type, void *object)
817 {
818 	if (type == RMAP_BGP && is_evpn_prefix_default(p))
819 		return RMAP_MATCH;
820 
821 	return RMAP_NOMATCH;
822 }
823 
824 /* Route map commands for default-route matching. */
825 static const struct route_map_rule_cmd
826 		route_match_evpn_default_route_cmd = {
827 	"evpn default-route",
828 	route_match_evpn_default_route,
829 	NULL,
830 	NULL
831 };
832 
833 /* `match mac address MAC_ACCESS_LIST' */
834 
835 /* Match function should return 1 if match is success else return
836    zero. */
837 static enum route_map_cmd_result_t
route_match_mac_address(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)838 route_match_mac_address(void *rule, const struct prefix *prefix,
839 			route_map_object_t type, void *object)
840 {
841 	struct access_list *alist;
842 	struct prefix p;
843 
844 	if (type == RMAP_BGP) {
845 		alist = access_list_lookup(AFI_L2VPN, (char *)rule);
846 		if (alist == NULL)
847 			return RMAP_NOMATCH;
848 
849 		if (prefix->u.prefix_evpn.route_type != BGP_EVPN_MAC_IP_ROUTE)
850 			return RMAP_NOMATCH;
851 
852 		p.family = AF_ETHERNET;
853 		p.prefixlen = ETH_ALEN * 8;
854 		p.u.prefix_eth = prefix->u.prefix_evpn.macip_addr.mac;
855 
856 		return (access_list_apply(alist, &p) == FILTER_DENY
857 				? RMAP_NOMATCH
858 				: RMAP_MATCH);
859 	}
860 
861 	return RMAP_NOMATCH;
862 }
863 
864 /* Route map `mac address' match statement.  `arg' should be
865    access-list name. */
route_match_mac_address_compile(const char * arg)866 static void *route_match_mac_address_compile(const char *arg)
867 {
868 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
869 }
870 
871 /* Free route map's compiled `ip address' value. */
route_match_mac_address_free(void * rule)872 static void route_match_mac_address_free(void *rule)
873 {
874 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
875 }
876 
877 /* Route map commands for mac address matching. */
878 static const struct route_map_rule_cmd route_match_mac_address_cmd = {
879 	"mac address",
880 	route_match_mac_address,
881 	route_match_mac_address_compile,
882 	route_match_mac_address_free
883 };
884 
885 /*
886  * Match function returns:
887  * ...RMAP_MATCH if match is found.
888  * ...RMAP_NOMATCH if match is not found.
889  * ...RMAP_NOOP to ignore this match check.
890  */
891 static enum route_map_cmd_result_t
route_match_vni(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)892 route_match_vni(void *rule, const struct prefix *prefix,
893 		route_map_object_t type, void *object)
894 {
895 	vni_t vni = 0;
896 	unsigned int label_cnt = 0;
897 	struct bgp_path_info *path = NULL;
898 	struct prefix_evpn *evp = (struct prefix_evpn *) prefix;
899 
900 	if (type == RMAP_BGP) {
901 		vni = *((vni_t *)rule);
902 		path = (struct bgp_path_info *)object;
903 
904 		/*
905 		 * This rmap filter is valid for vxlan tunnel type only.
906 		 * For any other tunnel type, return noop to ignore
907 		 * this check.
908 		 */
909 		if (path->attr->encap_tunneltype != BGP_ENCAP_TYPE_VXLAN)
910 			return RMAP_NOOP;
911 
912 		/*
913 		 * Apply filter to type 1, 2, 5 routes only.
914 		 * Other route types do not have vni label.
915 		 */
916 		if (evp && (evp->prefix.route_type != BGP_EVPN_AD_ROUTE &&
917 			evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE &&
918 			evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE))
919 			return RMAP_NOOP;
920 
921 		if (path->extra == NULL)
922 			return RMAP_NOMATCH;
923 
924 		for ( ; label_cnt < BGP_MAX_LABELS &&
925 			label_cnt < path->extra->num_labels; label_cnt++) {
926 			if (vni == label2vni(&path->extra->label[label_cnt]))
927 				return RMAP_MATCH;
928 		}
929 	}
930 
931 	return RMAP_NOMATCH;
932 }
933 
934 /* Route map `vni' match statement. */
route_match_vni_compile(const char * arg)935 static void *route_match_vni_compile(const char *arg)
936 {
937 	vni_t *vni = NULL;
938 	char *end = NULL;
939 
940 	vni = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(vni_t));
941 
942 	*vni = strtoul(arg, &end, 10);
943 	if (*end != '\0') {
944 		XFREE(MTYPE_ROUTE_MAP_COMPILED, vni);
945 		return NULL;
946 	}
947 
948 	return vni;
949 }
950 
951 /* Free route map's compiled `vni' value. */
route_match_vni_free(void * rule)952 static void route_match_vni_free(void *rule)
953 {
954 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
955 }
956 
957 /* Route map commands for vni matching. */
958 static const struct route_map_rule_cmd route_match_evpn_vni_cmd = {
959 	"evpn vni",
960 	route_match_vni,
961 	route_match_vni_compile,
962 	route_match_vni_free
963 };
964 
965 /* `match evpn route-type' */
966 
967 /* Match function should return 1 if match is success else return
968    zero. */
969 static enum route_map_cmd_result_t
route_match_evpn_route_type(void * rule,const struct prefix * pfx,route_map_object_t type,void * object)970 route_match_evpn_route_type(void *rule, const struct prefix *pfx,
971 			    route_map_object_t type, void *object)
972 {
973 	uint8_t route_type = 0;
974 
975 	if (type == RMAP_BGP) {
976 		route_type = *((uint8_t *)rule);
977 
978 		if (route_type == pfx->u.prefix_evpn.route_type)
979 			return RMAP_MATCH;
980 	}
981 
982 	return RMAP_NOMATCH;
983 }
984 
985 /* Route map `route-type' match statement. */
route_match_evpn_route_type_compile(const char * arg)986 static void *route_match_evpn_route_type_compile(const char *arg)
987 {
988 	uint8_t *route_type = NULL;
989 
990 	route_type = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
991 
992 	if (strncmp(arg, "ma", 2) == 0)
993 		*route_type = BGP_EVPN_MAC_IP_ROUTE;
994 	else if (strncmp(arg, "mu", 2) == 0)
995 		*route_type = BGP_EVPN_IMET_ROUTE;
996 	else
997 		*route_type = BGP_EVPN_IP_PREFIX_ROUTE;
998 
999 	return route_type;
1000 }
1001 
1002 /* Free route map's compiled `route-type' value. */
route_match_evpn_route_type_free(void * rule)1003 static void route_match_evpn_route_type_free(void *rule)
1004 {
1005 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1006 }
1007 
1008 /* Route map commands for evpn route-type  matching. */
1009 static const struct route_map_rule_cmd route_match_evpn_route_type_cmd = {
1010 	"evpn route-type",
1011 	route_match_evpn_route_type,
1012 	route_match_evpn_route_type_compile,
1013 	route_match_evpn_route_type_free
1014 };
1015 
1016 /* `match rd' */
1017 
1018 /* Match function should return 1 if match is success else return zero. */
1019 static enum route_map_cmd_result_t
route_match_rd(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1020 route_match_rd(void *rule, const struct prefix *prefix,
1021 	       route_map_object_t type, void *object)
1022 {
1023 	struct prefix_rd *prd_rule = NULL;
1024 	const struct prefix_rd *prd_route = NULL;
1025 	struct bgp_path_info *path = NULL;
1026 
1027 	if (type == RMAP_BGP) {
1028 		if (prefix->family != AF_EVPN)
1029 			return RMAP_NOMATCH;
1030 
1031 		prd_rule = (struct prefix_rd *)rule;
1032 		path = (struct bgp_path_info *)object;
1033 
1034 		if (path->net == NULL || path->net->pdest == NULL)
1035 			return RMAP_NOMATCH;
1036 
1037 		prd_route = (struct prefix_rd *)bgp_dest_get_prefix(
1038 			path->net->pdest);
1039 		if (memcmp(prd_route->val, prd_rule->val, ECOMMUNITY_SIZE) == 0)
1040 			return RMAP_MATCH;
1041 	}
1042 
1043 	return RMAP_NOMATCH;
1044 }
1045 
1046 /* Route map `rd' match statement. */
route_match_rd_compile(const char * arg)1047 static void *route_match_rd_compile(const char *arg)
1048 {
1049 	struct prefix_rd *prd;
1050 	int ret;
1051 
1052 	prd = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct prefix_rd));
1053 
1054 	ret = str2prefix_rd(arg, prd);
1055 	if (!ret) {
1056 		XFREE(MTYPE_ROUTE_MAP_COMPILED, prd);
1057 		return NULL;
1058 	}
1059 
1060 	return prd;
1061 }
1062 
1063 /* Free route map's compiled `rd' value. */
route_match_rd_free(void * rule)1064 static void route_match_rd_free(void *rule)
1065 {
1066 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1067 }
1068 
1069 /* Route map commands for rd matching. */
1070 static const struct route_map_rule_cmd route_match_evpn_rd_cmd = {
1071 	"evpn rd",
1072 	route_match_rd,
1073 	route_match_rd_compile,
1074 	route_match_rd_free
1075 };
1076 
1077 /* Route map commands for VRF route leak with source vrf matching */
1078 static enum route_map_cmd_result_t
route_match_vrl_source_vrf(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1079 route_match_vrl_source_vrf(void *rule, const struct prefix *prefix,
1080 			   route_map_object_t type, void *object)
1081 {
1082 	struct bgp_path_info *path;
1083 	char *vrf_name;
1084 
1085 	if (type == RMAP_BGP) {
1086 		vrf_name = rule;
1087 		path = (struct bgp_path_info *)object;
1088 
1089 		if (strncmp(vrf_name, "n/a", VRF_NAMSIZ) == 0)
1090 			return RMAP_NOMATCH;
1091 
1092 		if (path->extra == NULL)
1093 			return RMAP_NOMATCH;
1094 
1095 		if (strncmp(vrf_name, vrf_id_to_name(
1096 				path->extra->bgp_orig->vrf_id), VRF_NAMSIZ)
1097 		    == 0)
1098 			return RMAP_MATCH;
1099 	}
1100 
1101 	return RMAP_NOMATCH;
1102 }
1103 
route_match_vrl_source_vrf_compile(const char * arg)1104 static void *route_match_vrl_source_vrf_compile(const char *arg)
1105 {
1106 	uint8_t *vrf_name = NULL;
1107 
1108 	vrf_name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1109 
1110 	return vrf_name;
1111 }
1112 
1113 /* Free route map's compiled `route-type' value. */
route_match_vrl_source_vrf_free(void * rule)1114 static void route_match_vrl_source_vrf_free(void *rule)
1115 {
1116 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1117 }
1118 
1119 static const struct route_map_rule_cmd route_match_vrl_source_vrf_cmd = {
1120 	"source-vrf",
1121 	route_match_vrl_source_vrf,
1122 	route_match_vrl_source_vrf_compile,
1123 	route_match_vrl_source_vrf_free
1124 };
1125 
1126 /* `match local-preference LOCAL-PREF' */
1127 
1128 /* Match function return 1 if match is success else return zero. */
1129 static enum route_map_cmd_result_t
route_match_local_pref(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1130 route_match_local_pref(void *rule, const struct prefix *prefix,
1131 		       route_map_object_t type, void *object)
1132 {
1133 	uint32_t *local_pref;
1134 	struct bgp_path_info *path;
1135 
1136 	if (type == RMAP_BGP) {
1137 		local_pref = rule;
1138 		path = object;
1139 
1140 		if (path->attr->local_pref == *local_pref)
1141 			return RMAP_MATCH;
1142 		else
1143 			return RMAP_NOMATCH;
1144 	}
1145 	return RMAP_NOMATCH;
1146 }
1147 
1148 /*
1149  * Route map `match local-preference' match statement.
1150  * `arg' is local-pref value
1151  */
route_match_local_pref_compile(const char * arg)1152 static void *route_match_local_pref_compile(const char *arg)
1153 {
1154 	uint32_t *local_pref;
1155 	char *endptr = NULL;
1156 	unsigned long tmpval;
1157 
1158 	/* Locpref value shoud be integer. */
1159 	if (!all_digit(arg))
1160 		return NULL;
1161 
1162 	errno = 0;
1163 	tmpval = strtoul(arg, &endptr, 10);
1164 	if (*endptr != '\0' || errno || tmpval > UINT32_MAX)
1165 		return NULL;
1166 
1167 	local_pref = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
1168 
1169 	*local_pref = tmpval;
1170 	return local_pref;
1171 }
1172 
1173 /* Free route map's compiled `match local-preference' value. */
route_match_local_pref_free(void * rule)1174 static void route_match_local_pref_free(void *rule)
1175 {
1176 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1177 }
1178 
1179 /* Route map commands for metric matching. */
1180 static const struct route_map_rule_cmd route_match_local_pref_cmd = {
1181 	"local-preference",
1182 	route_match_local_pref,
1183 	route_match_local_pref_compile,
1184 	route_match_local_pref_free
1185 };
1186 
1187 /* `match metric METRIC' */
1188 
1189 /* Match function return 1 if match is success else return zero. */
1190 static enum route_map_cmd_result_t
route_match_metric(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1191 route_match_metric(void *rule, const struct prefix *prefix,
1192 		   route_map_object_t type, void *object)
1193 {
1194 	struct rmap_value *rv;
1195 	struct bgp_path_info *path;
1196 
1197 	if (type == RMAP_BGP) {
1198 		rv = rule;
1199 		path = object;
1200 		return route_value_match(rv, path->attr->med);
1201 	}
1202 	return RMAP_NOMATCH;
1203 }
1204 
1205 /* Route map commands for metric matching. */
1206 static const struct route_map_rule_cmd route_match_metric_cmd = {
1207 	"metric",
1208 	route_match_metric,
1209 	route_value_compile,
1210 	route_value_free,
1211 };
1212 
1213 /* `match as-path ASPATH' */
1214 
1215 /* Match function for as-path match.  I assume given object is */
1216 static enum route_map_cmd_result_t
route_match_aspath(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1217 route_match_aspath(void *rule, const struct prefix *prefix,
1218 		   route_map_object_t type, void *object)
1219 {
1220 
1221 	struct as_list *as_list;
1222 	struct bgp_path_info *path;
1223 
1224 	if (type == RMAP_BGP) {
1225 		as_list = as_list_lookup((char *)rule);
1226 		if (as_list == NULL)
1227 			return RMAP_NOMATCH;
1228 
1229 		path = object;
1230 
1231 		/* Perform match. */
1232 		return ((as_list_apply(as_list, path->attr->aspath)
1233 			 == AS_FILTER_DENY)
1234 				? RMAP_NOMATCH
1235 				: RMAP_MATCH);
1236 	}
1237 	return RMAP_NOMATCH;
1238 }
1239 
1240 /* Compile function for as-path match. */
route_match_aspath_compile(const char * arg)1241 static void *route_match_aspath_compile(const char *arg)
1242 {
1243 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1244 }
1245 
1246 /* Compile function for as-path match. */
route_match_aspath_free(void * rule)1247 static void route_match_aspath_free(void *rule)
1248 {
1249 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1250 }
1251 
1252 /* Route map commands for aspath matching. */
1253 static const struct route_map_rule_cmd route_match_aspath_cmd = {
1254 	"as-path",
1255 	route_match_aspath,
1256 	route_match_aspath_compile,
1257 	route_match_aspath_free
1258 };
1259 
1260 /* `match community COMMUNIY' */
1261 struct rmap_community {
1262 	char *name;
1263 	uint32_t name_hash;
1264 	int exact;
1265 };
1266 
1267 /* Match function for community match. */
1268 static enum route_map_cmd_result_t
route_match_community(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1269 route_match_community(void *rule, const struct prefix *prefix,
1270 		      route_map_object_t type, void *object)
1271 {
1272 	struct community_list *list;
1273 	struct bgp_path_info *path;
1274 	struct rmap_community *rcom = rule;
1275 
1276 	if (type == RMAP_BGP) {
1277 		path = object;
1278 		rcom = rule;
1279 
1280 		list = community_list_lookup(bgp_clist, rcom->name,
1281 					     rcom->name_hash,
1282 					     COMMUNITY_LIST_MASTER);
1283 		if (!list)
1284 			return RMAP_NOMATCH;
1285 
1286 		if (rcom->exact) {
1287 			if (community_list_exact_match(path->attr->community,
1288 						       list))
1289 				return RMAP_MATCH;
1290 		} else {
1291 			if (community_list_match(path->attr->community, list))
1292 				return RMAP_MATCH;
1293 		}
1294 	}
1295 	return RMAP_NOMATCH;
1296 }
1297 
1298 /* Compile function for community match. */
route_match_community_compile(const char * arg)1299 static void *route_match_community_compile(const char *arg)
1300 {
1301 	struct rmap_community *rcom;
1302 	int len;
1303 	char *p;
1304 
1305 	rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1306 
1307 	p = strchr(arg, ' ');
1308 	if (p) {
1309 		len = p - arg;
1310 		rcom->name = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, len + 1);
1311 		memcpy(rcom->name, arg, len);
1312 		rcom->exact = 1;
1313 	} else {
1314 		rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1315 		rcom->exact = 0;
1316 	}
1317 
1318 	rcom->name_hash = bgp_clist_hash_key(rcom->name);
1319 	return rcom;
1320 }
1321 
1322 /* Compile function for community match. */
route_match_community_free(void * rule)1323 static void route_match_community_free(void *rule)
1324 {
1325 	struct rmap_community *rcom = rule;
1326 
1327 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1328 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1329 }
1330 
1331 /*
1332  * In routemap processing there is a need to add the
1333  * name as a rule_key in the dependency table. Routemap
1334  * lib is unaware of rule_key when exact-match clause
1335  * is in use. routemap lib uses the compiled output to
1336  * get the rule_key value.
1337  */
route_match_get_community_key(void * rule)1338 static void *route_match_get_community_key(void *rule)
1339 {
1340 	struct rmap_community *rcom;
1341 
1342 	rcom = rule;
1343 	return rcom->name;
1344 }
1345 
1346 
1347 /* Route map commands for community matching. */
1348 static const struct route_map_rule_cmd route_match_community_cmd = {
1349 	"community",
1350 	route_match_community,
1351 	route_match_community_compile,
1352 	route_match_community_free,
1353 	route_match_get_community_key
1354 };
1355 
1356 /* Match function for lcommunity match. */
1357 static enum route_map_cmd_result_t
route_match_lcommunity(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1358 route_match_lcommunity(void *rule, const struct prefix *prefix,
1359 		       route_map_object_t type, void *object)
1360 {
1361 	struct community_list *list;
1362 	struct bgp_path_info *path;
1363 	struct rmap_community *rcom = rule;
1364 
1365 	if (type == RMAP_BGP) {
1366 		path = object;
1367 
1368 		list = community_list_lookup(bgp_clist, rcom->name,
1369 					     rcom->name_hash,
1370 					     LARGE_COMMUNITY_LIST_MASTER);
1371 		if (!list)
1372 			return RMAP_NOMATCH;
1373 
1374 		if (rcom->exact) {
1375 			if (lcommunity_list_exact_match(
1376 						path->attr->lcommunity,
1377 						list))
1378 				return RMAP_MATCH;
1379 		} else {
1380 			if (lcommunity_list_match(
1381 						path->attr->lcommunity,
1382 						list))
1383 				return RMAP_MATCH;
1384 		}
1385 	}
1386 	return RMAP_NOMATCH;
1387 }
1388 
1389 /* Compile function for community match. */
route_match_lcommunity_compile(const char * arg)1390 static void *route_match_lcommunity_compile(const char *arg)
1391 {
1392 	struct rmap_community *rcom;
1393 	int len;
1394 	char *p;
1395 
1396 	rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1397 
1398 	p = strchr(arg, ' ');
1399 	if (p) {
1400 		len = p - arg;
1401 		rcom->name = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, len + 1);
1402 		memcpy(rcom->name, arg, len);
1403 		rcom->exact = 1;
1404 	} else {
1405 		rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1406 		rcom->exact = 0;
1407 	}
1408 
1409 	rcom->name_hash = bgp_clist_hash_key(rcom->name);
1410 	return rcom;
1411 }
1412 
1413 /* Compile function for community match. */
route_match_lcommunity_free(void * rule)1414 static void route_match_lcommunity_free(void *rule)
1415 {
1416 	struct rmap_community *rcom = rule;
1417 
1418 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1419 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1420 }
1421 
1422 /* Route map commands for community matching. */
1423 static const struct route_map_rule_cmd route_match_lcommunity_cmd = {
1424 	"large-community",
1425 	route_match_lcommunity,
1426 	route_match_lcommunity_compile,
1427 	route_match_lcommunity_free,
1428 	route_match_get_community_key
1429 };
1430 
1431 
1432 /* Match function for extcommunity match. */
1433 static enum route_map_cmd_result_t
route_match_ecommunity(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1434 route_match_ecommunity(void *rule, const struct prefix *prefix,
1435 		       route_map_object_t type, void *object)
1436 {
1437 	struct community_list *list;
1438 	struct bgp_path_info *path;
1439 	struct rmap_community *rcom = rule;
1440 
1441 	if (type == RMAP_BGP) {
1442 		path = object;
1443 
1444 		list = community_list_lookup(bgp_clist, rcom->name,
1445 					     rcom->name_hash,
1446 					     EXTCOMMUNITY_LIST_MASTER);
1447 		if (!list)
1448 			return RMAP_NOMATCH;
1449 
1450 		if (ecommunity_list_match(path->attr->ecommunity, list))
1451 			return RMAP_MATCH;
1452 	}
1453 	return RMAP_NOMATCH;
1454 }
1455 
1456 /* Compile function for extcommunity match. */
route_match_ecommunity_compile(const char * arg)1457 static void *route_match_ecommunity_compile(const char *arg)
1458 {
1459 	struct rmap_community *rcom;
1460 
1461 	rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1462 	rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1463 	rcom->name_hash = bgp_clist_hash_key(rcom->name);
1464 
1465 	return rcom;
1466 }
1467 
1468 /* Compile function for extcommunity match. */
route_match_ecommunity_free(void * rule)1469 static void route_match_ecommunity_free(void *rule)
1470 {
1471 	struct rmap_community *rcom = rule;
1472 
1473 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1474 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1475 }
1476 
1477 /* Route map commands for community matching. */
1478 static const struct route_map_rule_cmd route_match_ecommunity_cmd = {
1479 	"extcommunity",
1480 	route_match_ecommunity,
1481 	route_match_ecommunity_compile,
1482 	route_match_ecommunity_free
1483 };
1484 
1485 /* `match nlri` and `set nlri` are replaced by `address-family ipv4`
1486    and `address-family vpnv4'.  */
1487 
1488 /* `match origin' */
1489 static enum route_map_cmd_result_t
route_match_origin(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1490 route_match_origin(void *rule, const struct prefix *prefix,
1491 		   route_map_object_t type, void *object)
1492 {
1493 	uint8_t *origin;
1494 	struct bgp_path_info *path;
1495 
1496 	if (type == RMAP_BGP) {
1497 		origin = rule;
1498 		path = object;
1499 
1500 		if (path->attr->origin == *origin)
1501 			return RMAP_MATCH;
1502 	}
1503 
1504 	return RMAP_NOMATCH;
1505 }
1506 
route_match_origin_compile(const char * arg)1507 static void *route_match_origin_compile(const char *arg)
1508 {
1509 	uint8_t *origin;
1510 
1511 	origin = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
1512 
1513 	if (strcmp(arg, "igp") == 0)
1514 		*origin = 0;
1515 	else if (strcmp(arg, "egp") == 0)
1516 		*origin = 1;
1517 	else
1518 		*origin = 2;
1519 
1520 	return origin;
1521 }
1522 
1523 /* Free route map's compiled `ip address' value. */
route_match_origin_free(void * rule)1524 static void route_match_origin_free(void *rule)
1525 {
1526 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1527 }
1528 
1529 /* Route map commands for origin matching. */
1530 static const struct route_map_rule_cmd route_match_origin_cmd = {
1531 	"origin",
1532 	route_match_origin,
1533 	route_match_origin_compile,
1534 	route_match_origin_free
1535 };
1536 
1537 /* match probability  { */
1538 
1539 static enum route_map_cmd_result_t
route_match_probability(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1540 route_match_probability(void *rule, const struct prefix *prefix,
1541 			route_map_object_t type, void *object)
1542 {
1543 	long r = frr_weak_random();
1544 
1545 	switch (*(long *)rule) {
1546 	case 0:
1547 		break;
1548 	case RAND_MAX:
1549 		return RMAP_MATCH;
1550 	default:
1551 		if (r < *(long *)rule) {
1552 			return RMAP_MATCH;
1553 		}
1554 	}
1555 
1556 	return RMAP_NOMATCH;
1557 }
1558 
route_match_probability_compile(const char * arg)1559 static void *route_match_probability_compile(const char *arg)
1560 {
1561 	long *lobule;
1562 	unsigned perc;
1563 
1564 	perc = atoi(arg);
1565 	lobule = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(long));
1566 
1567 	switch (perc) {
1568 	case 0:
1569 		*lobule = 0;
1570 		break;
1571 	case 100:
1572 		*lobule = RAND_MAX;
1573 		break;
1574 	default:
1575 		*lobule = RAND_MAX / 100 * perc;
1576 	}
1577 
1578 	return lobule;
1579 }
1580 
route_match_probability_free(void * rule)1581 static void route_match_probability_free(void *rule)
1582 {
1583 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1584 }
1585 
1586 static const struct route_map_rule_cmd route_match_probability_cmd = {
1587 	"probability",
1588 	route_match_probability,
1589 	route_match_probability_compile,
1590 	route_match_probability_free
1591 };
1592 
1593 /* `match interface IFNAME' */
1594 /* Match function should return 1 if match is success else return
1595    zero. */
1596 static enum route_map_cmd_result_t
route_match_interface(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1597 route_match_interface(void *rule, const struct prefix *prefix,
1598 		      route_map_object_t type, void *object)
1599 {
1600 	struct interface *ifp;
1601 	struct bgp_path_info *path;
1602 
1603 	if (type == RMAP_BGP) {
1604 		path = object;
1605 
1606 		if (!path)
1607 			return RMAP_NOMATCH;
1608 
1609 		ifp = if_lookup_by_name_all_vrf((char *)rule);
1610 
1611 		if (ifp == NULL || ifp->ifindex != path->attr->nh_ifindex)
1612 			return RMAP_NOMATCH;
1613 
1614 		return RMAP_MATCH;
1615 	}
1616 	return RMAP_NOMATCH;
1617 }
1618 
1619 /* Route map `interface' match statement.  `arg' should be
1620    interface name. */
route_match_interface_compile(const char * arg)1621 static void *route_match_interface_compile(const char *arg)
1622 {
1623 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1624 }
1625 
1626 /* Free route map's compiled `interface' value. */
route_match_interface_free(void * rule)1627 static void route_match_interface_free(void *rule)
1628 {
1629 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1630 }
1631 
1632 /* Route map commands for ip address matching. */
1633 static const struct route_map_rule_cmd route_match_interface_cmd = {
1634 	"interface",
1635 	route_match_interface,
1636 	route_match_interface_compile,
1637 	route_match_interface_free
1638 };
1639 
1640 /* } */
1641 
1642 /* `set ip next-hop IP_ADDRESS' */
1643 
1644 /* Match function return 1 if match is success else return zero. */
1645 static enum route_map_cmd_result_t
route_match_tag(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1646 route_match_tag(void *rule, const struct prefix *prefix,
1647 		route_map_object_t type, void *object)
1648 {
1649 	route_tag_t *tag;
1650 	struct bgp_path_info *path;
1651 
1652 	if (type == RMAP_BGP) {
1653 		tag = rule;
1654 		path = object;
1655 
1656 		return ((path->attr->tag == *tag) ? RMAP_MATCH : RMAP_NOMATCH);
1657 	}
1658 
1659 	return RMAP_NOMATCH;
1660 }
1661 
1662 
1663 /* Route map commands for tag matching. */
1664 static const struct route_map_rule_cmd route_match_tag_cmd = {
1665 	"tag",
1666 	route_match_tag,
1667 	route_map_rule_tag_compile,
1668 	route_map_rule_tag_free,
1669 };
1670 
1671 static enum route_map_cmd_result_t
route_set_srte_color(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1672 route_set_srte_color(void *rule, const struct prefix *prefix,
1673 		     route_map_object_t type, void *object)
1674 {
1675 	uint32_t *srte_color = rule;
1676 	struct bgp_path_info *path;
1677 
1678 	if (type != RMAP_BGP)
1679 		return RMAP_OKAY;
1680 
1681 	path = object;
1682 
1683 	path->attr->srte_color = *srte_color;
1684 	path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR);
1685 
1686 	return RMAP_OKAY;
1687 }
1688 
1689 /* Route map `sr-te color' compile function */
route_set_srte_color_compile(const char * arg)1690 static void *route_set_srte_color_compile(const char *arg)
1691 {
1692 	uint32_t *color;
1693 
1694 	color = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
1695 	*color = atoi(arg);
1696 
1697 	return color;
1698 }
1699 
1700 /* Free route map's compiled `sr-te color' value. */
route_set_srte_color_free(void * rule)1701 static void route_set_srte_color_free(void *rule)
1702 {
1703 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1704 }
1705 
1706 /* Route map commands for sr-te color set. */
1707 struct route_map_rule_cmd route_set_srte_color_cmd = {
1708 	"sr-te color", route_set_srte_color, route_set_srte_color_compile,
1709 	route_set_srte_color_free};
1710 
1711 /* Set nexthop to object.  ojbect must be pointer to struct attr. */
1712 struct rmap_ip_nexthop_set {
1713 	struct in_addr *address;
1714 	int peer_address;
1715 	int unchanged;
1716 };
1717 
1718 static enum route_map_cmd_result_t
route_set_ip_nexthop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1719 route_set_ip_nexthop(void *rule, const struct prefix *prefix,
1720 		     route_map_object_t type, void *object)
1721 {
1722 	struct rmap_ip_nexthop_set *rins = rule;
1723 	struct bgp_path_info *path;
1724 	struct peer *peer;
1725 
1726 	if (type != RMAP_BGP)
1727 		return RMAP_OKAY;
1728 
1729 	if (prefix->family == AF_INET6)
1730 		return RMAP_OKAY;
1731 
1732 	path = object;
1733 	peer = path->peer;
1734 
1735 	if (rins->unchanged) {
1736 		SET_FLAG(path->attr->rmap_change_flags,
1737 			 BATTR_RMAP_NEXTHOP_UNCHANGED);
1738 	} else if (rins->peer_address) {
1739 		if ((CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
1740 		     || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT))
1741 		    && peer->su_remote
1742 		    && sockunion_family(peer->su_remote) == AF_INET) {
1743 			path->attr->nexthop.s_addr =
1744 				sockunion2ip(peer->su_remote);
1745 			path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1746 		} else if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT)) {
1747 			/* The next hop value will be set as part of
1748 			 * packet rewrite.  Set the flags here to indicate
1749 			 * that rewrite needs to be done.
1750 			 * Also, clear the value.
1751 			 */
1752 			SET_FLAG(path->attr->rmap_change_flags,
1753 				 BATTR_RMAP_NEXTHOP_PEER_ADDRESS);
1754 			path->attr->nexthop.s_addr = INADDR_ANY;
1755 		}
1756 	} else {
1757 		/* Set next hop value. */
1758 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1759 		path->attr->nexthop = *rins->address;
1760 		SET_FLAG(path->attr->rmap_change_flags,
1761 			 BATTR_RMAP_IPV4_NHOP_CHANGED);
1762 		/* case for MP-BGP : MPLS VPN */
1763 		path->attr->mp_nexthop_global_in = *rins->address;
1764 		path->attr->mp_nexthop_len = sizeof(*rins->address);
1765 	}
1766 
1767 	return RMAP_OKAY;
1768 }
1769 
1770 /* Route map `ip nexthop' compile function.  Given string is converted
1771    to struct in_addr structure. */
route_set_ip_nexthop_compile(const char * arg)1772 static void *route_set_ip_nexthop_compile(const char *arg)
1773 {
1774 	struct rmap_ip_nexthop_set *rins;
1775 	struct in_addr *address = NULL;
1776 	int peer_address = 0;
1777 	int unchanged = 0;
1778 	int ret;
1779 
1780 	if (strcmp(arg, "peer-address") == 0)
1781 		peer_address = 1;
1782 	else if (strcmp(arg, "unchanged") == 0)
1783 		unchanged = 1;
1784 	else {
1785 		address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
1786 				  sizeof(struct in_addr));
1787 		ret = inet_aton(arg, address);
1788 
1789 		if (ret == 0) {
1790 			XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
1791 			return NULL;
1792 		}
1793 	}
1794 
1795 	rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
1796 		       sizeof(struct rmap_ip_nexthop_set));
1797 
1798 	rins->address = address;
1799 	rins->peer_address = peer_address;
1800 	rins->unchanged = unchanged;
1801 
1802 	return rins;
1803 }
1804 
1805 /* Free route map's compiled `ip nexthop' value. */
route_set_ip_nexthop_free(void * rule)1806 static void route_set_ip_nexthop_free(void *rule)
1807 {
1808 	struct rmap_ip_nexthop_set *rins = rule;
1809 
1810 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rins->address);
1811 
1812 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rins);
1813 }
1814 
1815 /* Route map commands for ip nexthop set. */
1816 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
1817 	"ip next-hop",
1818 	route_set_ip_nexthop,
1819 	route_set_ip_nexthop_compile,
1820 	route_set_ip_nexthop_free
1821 };
1822 
1823 /* `set local-preference LOCAL_PREF' */
1824 
1825 /* Set local preference. */
1826 static enum route_map_cmd_result_t
route_set_local_pref(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1827 route_set_local_pref(void *rule, const struct prefix *prefix,
1828 		     route_map_object_t type, void *object)
1829 {
1830 	struct rmap_value *rv;
1831 	struct bgp_path_info *path;
1832 	uint32_t locpref = 0;
1833 
1834 	if (type == RMAP_BGP) {
1835 		/* Fetch routemap's rule information. */
1836 		rv = rule;
1837 		path = object;
1838 
1839 		/* Set local preference value. */
1840 		if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
1841 			locpref = path->attr->local_pref;
1842 
1843 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1844 		path->attr->local_pref =
1845 			route_value_adjust(rv, locpref, path->peer);
1846 	}
1847 
1848 	return RMAP_OKAY;
1849 }
1850 
1851 /* Set local preference rule structure. */
1852 static const struct route_map_rule_cmd route_set_local_pref_cmd = {
1853 	"local-preference",
1854 	route_set_local_pref,
1855 	route_value_compile,
1856 	route_value_free,
1857 };
1858 
1859 /* `set weight WEIGHT' */
1860 
1861 /* Set weight. */
1862 static enum route_map_cmd_result_t
route_set_weight(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1863 route_set_weight(void *rule, const struct prefix *prefix,
1864 		 route_map_object_t type, void *object)
1865 {
1866 	struct rmap_value *rv;
1867 	struct bgp_path_info *path;
1868 
1869 	if (type == RMAP_BGP) {
1870 		/* Fetch routemap's rule information. */
1871 		rv = rule;
1872 		path = object;
1873 
1874 		/* Set weight value. */
1875 		path->attr->weight = route_value_adjust(rv, 0, path->peer);
1876 	}
1877 
1878 	return RMAP_OKAY;
1879 }
1880 
1881 /* Set local preference rule structure. */
1882 static const struct route_map_rule_cmd route_set_weight_cmd = {
1883 	"weight",
1884 	route_set_weight,
1885 	route_value_compile,
1886 	route_value_free,
1887 };
1888 
1889 /* `set distance DISTANCE */
1890 static enum route_map_cmd_result_t
route_set_distance(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1891 route_set_distance(void *rule, const struct prefix *prefix,
1892 		   route_map_object_t type, void *object)
1893 {
1894 	struct bgp_path_info *path = object;
1895 	struct rmap_value *rv = rule;
1896 
1897 	if (type != RMAP_BGP)
1898 		return RMAP_OKAY;
1899 
1900 	path->attr->distance = rv->value;
1901 
1902 	return RMAP_OKAY;
1903 }
1904 
1905 /* set distance rule structure */
1906 static const struct route_map_rule_cmd route_set_distance_cmd = {
1907 	"distance",
1908 	route_set_distance,
1909 	route_value_compile,
1910 	route_value_free,
1911 };
1912 
1913 /* `set metric METRIC' */
1914 
1915 /* Set metric to attribute. */
1916 static enum route_map_cmd_result_t
route_set_metric(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1917 route_set_metric(void *rule, const struct prefix *prefix,
1918 		 route_map_object_t type, void *object)
1919 {
1920 	struct rmap_value *rv;
1921 	struct bgp_path_info *path;
1922 	uint32_t med = 0;
1923 
1924 	if (type == RMAP_BGP) {
1925 		/* Fetch routemap's rule information. */
1926 		rv = rule;
1927 		path = object;
1928 
1929 		if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
1930 			med = path->attr->med;
1931 
1932 		path->attr->med = route_value_adjust(rv, med, path->peer);
1933 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
1934 	}
1935 	return RMAP_OKAY;
1936 }
1937 
1938 /* Set metric rule structure. */
1939 static const struct route_map_rule_cmd route_set_metric_cmd = {
1940 	"metric",
1941 	route_set_metric,
1942 	route_value_compile,
1943 	route_value_free,
1944 };
1945 
1946 /* `set table (1-4294967295)' */
1947 
route_set_table_id(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1948 static enum route_map_cmd_result_t route_set_table_id(void *rule,
1949 						      const struct prefix *prefix,
1950 						      route_map_object_t type,
1951 						      void *object)
1952 {
1953 	struct rmap_value *rv;
1954 	struct bgp_path_info *path;
1955 
1956 	if (type == RMAP_BGP) {
1957 		/* Fetch routemap's rule information. */
1958 		rv = rule;
1959 		path = object;
1960 
1961 		path->attr->rmap_table_id = rv->value;
1962 	}
1963 	return RMAP_OKAY;
1964 }
1965 
1966 /* Set table_id rule structure. */
1967 static const struct route_map_rule_cmd route_set_table_id_cmd = {
1968 	"table",
1969 	route_set_table_id,
1970 	route_value_compile,
1971 	route_value_free
1972 };
1973 
1974 /* `set as-path prepend ASPATH' */
1975 
1976 /* For AS path prepend mechanism. */
1977 static enum route_map_cmd_result_t
route_set_aspath_prepend(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)1978 route_set_aspath_prepend(void *rule, const struct prefix *prefix,
1979 			 route_map_object_t type, void *object)
1980 {
1981 	struct aspath *aspath;
1982 	struct aspath *new;
1983 	struct bgp_path_info *path;
1984 
1985 	if (type == RMAP_BGP) {
1986 		path = object;
1987 
1988 		if (path->attr->aspath->refcnt)
1989 			new = aspath_dup(path->attr->aspath);
1990 		else
1991 			new = path->attr->aspath;
1992 
1993 		if ((uintptr_t)rule > 10) {
1994 			aspath = rule;
1995 			aspath_prepend(aspath, new);
1996 		} else {
1997 			as_t as = aspath_leftmost(new);
1998 			if (!as)
1999 				as = path->peer->as;
2000 			new = aspath_add_seq_n(new, as, (uintptr_t)rule);
2001 		}
2002 
2003 		path->attr->aspath = new;
2004 	}
2005 
2006 	return RMAP_OKAY;
2007 }
2008 
route_set_aspath_prepend_compile(const char * arg)2009 static void *route_set_aspath_prepend_compile(const char *arg)
2010 {
2011 	unsigned int num;
2012 
2013 	if (sscanf(arg, "last-as %u", &num) == 1 && num > 0 && num <= 10)
2014 		return (void *)(uintptr_t)num;
2015 
2016 	return route_aspath_compile(arg);
2017 }
2018 
route_set_aspath_prepend_free(void * rule)2019 static void route_set_aspath_prepend_free(void *rule)
2020 {
2021 	if ((uintptr_t)rule > 10)
2022 		route_aspath_free(rule);
2023 }
2024 
2025 
2026 /* Set as-path prepend rule structure. */
2027 static const struct route_map_rule_cmd route_set_aspath_prepend_cmd = {
2028 	"as-path prepend",
2029 	route_set_aspath_prepend,
2030 	route_set_aspath_prepend_compile,
2031 	route_set_aspath_prepend_free,
2032 };
2033 
2034 /* `set as-path exclude ASn' */
2035 
2036 /* For ASN exclude mechanism.
2037  * Iterate over ASns requested and filter them from the given AS_PATH one by
2038  * one.
2039  * Make a deep copy of existing AS_PATH, but for the first ASn only.
2040  */
2041 static enum route_map_cmd_result_t
route_set_aspath_exclude(void * rule,const struct prefix * dummy,route_map_object_t type,void * object)2042 route_set_aspath_exclude(void *rule, const struct prefix *dummy,
2043 			 route_map_object_t type, void *object)
2044 {
2045 	struct aspath *new_path, *exclude_path;
2046 	struct bgp_path_info *path;
2047 
2048 	if (type == RMAP_BGP) {
2049 		exclude_path = rule;
2050 		path = object;
2051 		if (path->attr->aspath->refcnt)
2052 			new_path = aspath_dup(path->attr->aspath);
2053 		else
2054 			new_path = path->attr->aspath;
2055 		path->attr->aspath =
2056 			aspath_filter_exclude(new_path, exclude_path);
2057 	}
2058 	return RMAP_OKAY;
2059 }
2060 
2061 /* Set ASn exlude rule structure. */
2062 static const struct route_map_rule_cmd route_set_aspath_exclude_cmd = {
2063 	"as-path exclude",
2064 	route_set_aspath_exclude,
2065 	route_aspath_compile,
2066 	route_aspath_free,
2067 };
2068 
2069 /* `set community COMMUNITY' */
2070 struct rmap_com_set {
2071 	struct community *com;
2072 	int additive;
2073 	int none;
2074 };
2075 
2076 /* For community set mechanism. */
2077 static enum route_map_cmd_result_t
route_set_community(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2078 route_set_community(void *rule, const struct prefix *prefix,
2079 		    route_map_object_t type, void *object)
2080 {
2081 	struct rmap_com_set *rcs;
2082 	struct bgp_path_info *path;
2083 	struct attr *attr;
2084 	struct community *new = NULL;
2085 	struct community *old;
2086 	struct community *merge;
2087 
2088 	if (type == RMAP_BGP) {
2089 		rcs = rule;
2090 		path = object;
2091 		attr = path->attr;
2092 		old = attr->community;
2093 
2094 		/* "none" case.  */
2095 		if (rcs->none) {
2096 			attr->flag &= ~(ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES));
2097 			attr->community = NULL;
2098 			/* See the longer comment down below. */
2099 			if (old && old->refcnt == 0)
2100 				community_free(&old);
2101 			return RMAP_OKAY;
2102 		}
2103 
2104 		/* "additive" case.  */
2105 		if (rcs->additive && old) {
2106 			merge = community_merge(community_dup(old), rcs->com);
2107 
2108 			new = community_uniq_sort(merge);
2109 			community_free(&merge);
2110 		} else
2111 			new = community_dup(rcs->com);
2112 
2113 		/* HACK: if the old community is not intern'd,
2114 		 * we should free it here, or all reference to it may be
2115 		 * lost.
2116 		 * Really need to cleanup attribute caching sometime.
2117 		 */
2118 		if (old && old->refcnt == 0)
2119 			community_free(&old);
2120 
2121 		/* will be interned by caller if required */
2122 		attr->community = new;
2123 
2124 		attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2125 	}
2126 
2127 	return RMAP_OKAY;
2128 }
2129 
2130 /* Compile function for set community. */
route_set_community_compile(const char * arg)2131 static void *route_set_community_compile(const char *arg)
2132 {
2133 	struct rmap_com_set *rcs;
2134 	struct community *com = NULL;
2135 	char *sp;
2136 	int additive = 0;
2137 	int none = 0;
2138 
2139 	if (strcmp(arg, "none") == 0)
2140 		none = 1;
2141 	else {
2142 		sp = strstr(arg, "additive");
2143 
2144 		if (sp && sp > arg) {
2145 			/* "additive" keyword is included.  */
2146 			additive = 1;
2147 			*(sp - 1) = '\0';
2148 		}
2149 
2150 		com = community_str2com(arg);
2151 
2152 		if (additive)
2153 			*(sp - 1) = ' ';
2154 
2155 		if (!com)
2156 			return NULL;
2157 	}
2158 
2159 	rcs = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_com_set));
2160 	rcs->com = com;
2161 	rcs->additive = additive;
2162 	rcs->none = none;
2163 
2164 	return rcs;
2165 }
2166 
2167 /* Free function for set community. */
route_set_community_free(void * rule)2168 static void route_set_community_free(void *rule)
2169 {
2170 	struct rmap_com_set *rcs = rule;
2171 
2172 	if (rcs->com)
2173 		community_free(&rcs->com);
2174 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcs);
2175 }
2176 
2177 /* Set community rule structure. */
2178 static const struct route_map_rule_cmd route_set_community_cmd = {
2179 	"community",
2180 	route_set_community,
2181 	route_set_community_compile,
2182 	route_set_community_free,
2183 };
2184 
2185 /* `set community COMMUNITY' */
2186 struct rmap_lcom_set {
2187 	struct lcommunity *lcom;
2188 	int additive;
2189 	int none;
2190 };
2191 
2192 
2193 /* For lcommunity set mechanism. */
2194 static enum route_map_cmd_result_t
route_set_lcommunity(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2195 route_set_lcommunity(void *rule, const struct prefix *prefix,
2196 		     route_map_object_t type, void *object)
2197 {
2198 	struct rmap_lcom_set *rcs;
2199 	struct bgp_path_info *path;
2200 	struct attr *attr;
2201 	struct lcommunity *new = NULL;
2202 	struct lcommunity *old;
2203 	struct lcommunity *merge;
2204 
2205 	if (type == RMAP_BGP) {
2206 		rcs = rule;
2207 		path = object;
2208 		attr = path->attr;
2209 		old = attr->lcommunity;
2210 
2211 		/* "none" case.  */
2212 		if (rcs->none) {
2213 			attr->flag &=
2214 				~(ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES));
2215 			attr->lcommunity = NULL;
2216 
2217 			/* See the longer comment down below. */
2218 			if (old && old->refcnt == 0)
2219 				lcommunity_free(&old);
2220 			return RMAP_OKAY;
2221 		}
2222 
2223 		if (rcs->additive && old) {
2224 			merge = lcommunity_merge(lcommunity_dup(old),
2225 						 rcs->lcom);
2226 
2227 			new = lcommunity_uniq_sort(merge);
2228 			lcommunity_free(&merge);
2229 		} else
2230 			new = lcommunity_dup(rcs->lcom);
2231 
2232 		/* HACK: if the old large-community is not intern'd,
2233 		 * we should free it here, or all reference to it may be
2234 		 * lost.
2235 		 * Really need to cleanup attribute caching sometime.
2236 		 */
2237 		if (old && old->refcnt == 0)
2238 			lcommunity_free(&old);
2239 
2240 		/* will be intern()'d or attr_flush()'d by bgp_update_main() */
2241 		attr->lcommunity = new;
2242 
2243 		attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
2244 	}
2245 
2246 	return RMAP_OKAY;
2247 }
2248 
2249 /* Compile function for set community. */
route_set_lcommunity_compile(const char * arg)2250 static void *route_set_lcommunity_compile(const char *arg)
2251 {
2252 	struct rmap_lcom_set *rcs;
2253 	struct lcommunity *lcom = NULL;
2254 	char *sp;
2255 	int additive = 0;
2256 	int none = 0;
2257 
2258 	if (strcmp(arg, "none") == 0)
2259 		none = 1;
2260 	else {
2261 		sp = strstr(arg, "additive");
2262 
2263 		if (sp && sp > arg) {
2264 			/* "additive" keyworkd is included.  */
2265 			additive = 1;
2266 			*(sp - 1) = '\0';
2267 		}
2268 
2269 		lcom = lcommunity_str2com(arg);
2270 
2271 		if (additive)
2272 			*(sp - 1) = ' ';
2273 
2274 		if (!lcom)
2275 			return NULL;
2276 	}
2277 
2278 	rcs = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_com_set));
2279 	rcs->lcom = lcom;
2280 	rcs->additive = additive;
2281 	rcs->none = none;
2282 
2283 	return rcs;
2284 }
2285 
2286 /* Free function for set lcommunity. */
route_set_lcommunity_free(void * rule)2287 static void route_set_lcommunity_free(void *rule)
2288 {
2289 	struct rmap_lcom_set *rcs = rule;
2290 
2291 	if (rcs->lcom) {
2292 		lcommunity_free(&rcs->lcom);
2293 	}
2294 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcs);
2295 }
2296 
2297 /* Set community rule structure. */
2298 static const struct route_map_rule_cmd route_set_lcommunity_cmd = {
2299 	"large-community",
2300 	route_set_lcommunity,
2301 	route_set_lcommunity_compile,
2302 	route_set_lcommunity_free,
2303 };
2304 
2305 /* `set large-comm-list (<1-99>|<100-500>|WORD) delete' */
2306 
2307 /* For large community set mechanism. */
2308 static enum route_map_cmd_result_t
route_set_lcommunity_delete(void * rule,const struct prefix * pfx,route_map_object_t type,void * object)2309 route_set_lcommunity_delete(void *rule, const struct prefix *pfx,
2310 			    route_map_object_t type, void *object)
2311 {
2312 	struct community_list *list;
2313 	struct lcommunity *merge;
2314 	struct lcommunity *new;
2315 	struct lcommunity *old;
2316 	struct bgp_path_info *path;
2317 	struct rmap_community *rcom = rule;
2318 
2319 	if (type == RMAP_BGP) {
2320 		if (!rcom)
2321 			return RMAP_OKAY;
2322 
2323 		path = object;
2324 		list = community_list_lookup(bgp_clist, rcom->name,
2325 					     rcom->name_hash,
2326 					     LARGE_COMMUNITY_LIST_MASTER);
2327 		old = path->attr->lcommunity;
2328 
2329 		if (list && old) {
2330 			merge = lcommunity_list_match_delete(
2331 				lcommunity_dup(old), list);
2332 			new = lcommunity_uniq_sort(merge);
2333 			lcommunity_free(&merge);
2334 
2335 			/* HACK: if the old community is not intern'd,
2336 			 * we should free it here, or all reference to it may be
2337 			 * lost.
2338 			 * Really need to cleanup attribute caching sometime.
2339 			 */
2340 			if (old->refcnt == 0)
2341 				lcommunity_free(&old);
2342 
2343 			if (new->size == 0) {
2344 				path->attr->lcommunity = NULL;
2345 				path->attr->flag &= ~ATTR_FLAG_BIT(
2346 					BGP_ATTR_LARGE_COMMUNITIES);
2347 				lcommunity_free(&new);
2348 			} else {
2349 				path->attr->lcommunity = new;
2350 				path->attr->flag |= ATTR_FLAG_BIT(
2351 					BGP_ATTR_LARGE_COMMUNITIES);
2352 			}
2353 		}
2354 	}
2355 
2356 	return RMAP_OKAY;
2357 }
2358 
2359 /* Compile function for set lcommunity. */
route_set_lcommunity_delete_compile(const char * arg)2360 static void *route_set_lcommunity_delete_compile(const char *arg)
2361 {
2362 	struct rmap_community *rcom;
2363 	char **splits;
2364 	int num;
2365 
2366 	frrstr_split(arg, " ", &splits, &num);
2367 
2368 	rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
2369 	rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, splits[0]);
2370 	rcom->name_hash = bgp_clist_hash_key(rcom->name);
2371 
2372 	for (int i = 0; i < num; i++)
2373 		XFREE(MTYPE_TMP, splits[i]);
2374 	XFREE(MTYPE_TMP, splits);
2375 
2376 	return rcom;
2377 }
2378 
2379 /* Free function for set lcommunity. */
route_set_lcommunity_delete_free(void * rule)2380 static void route_set_lcommunity_delete_free(void *rule)
2381 {
2382 	struct rmap_community *rcom = rule;
2383 
2384 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
2385 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
2386 }
2387 
2388 /* Set lcommunity rule structure. */
2389 static const struct route_map_rule_cmd route_set_lcommunity_delete_cmd = {
2390 	"large-comm-list",
2391 	route_set_lcommunity_delete,
2392 	route_set_lcommunity_delete_compile,
2393 	route_set_lcommunity_delete_free,
2394 };
2395 
2396 
2397 /* `set comm-list (<1-99>|<100-500>|WORD) delete' */
2398 
2399 /* For community set mechanism. */
2400 static enum route_map_cmd_result_t
route_set_community_delete(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2401 route_set_community_delete(void *rule, const struct prefix *prefix,
2402 			   route_map_object_t type, void *object)
2403 {
2404 	struct community_list *list;
2405 	struct community *merge;
2406 	struct community *new;
2407 	struct community *old;
2408 	struct bgp_path_info *path;
2409 	struct rmap_community *rcom = rule;
2410 
2411 	if (type == RMAP_BGP) {
2412 		if (!rcom)
2413 			return RMAP_OKAY;
2414 
2415 		path = object;
2416 		list = community_list_lookup(bgp_clist, rcom->name,
2417 					     rcom->name_hash,
2418 					     COMMUNITY_LIST_MASTER);
2419 		old = path->attr->community;
2420 
2421 		if (list && old) {
2422 			merge = community_list_match_delete(community_dup(old),
2423 							    list);
2424 			new = community_uniq_sort(merge);
2425 			community_free(&merge);
2426 
2427 			/* HACK: if the old community is not intern'd,
2428 			 * we should free it here, or all reference to it may be
2429 			 * lost.
2430 			 * Really need to cleanup attribute caching sometime.
2431 			 */
2432 			if (old->refcnt == 0)
2433 				community_free(&old);
2434 
2435 			if (new->size == 0) {
2436 				path->attr->community = NULL;
2437 				path->attr->flag &=
2438 					~ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2439 				community_free(&new);
2440 			} else {
2441 				path->attr->community = new;
2442 				path->attr->flag |=
2443 					ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2444 			}
2445 		}
2446 	}
2447 
2448 	return RMAP_OKAY;
2449 }
2450 
2451 /* Compile function for set community. */
route_set_community_delete_compile(const char * arg)2452 static void *route_set_community_delete_compile(const char *arg)
2453 {
2454 	struct rmap_community *rcom;
2455 	char **splits;
2456 	int num;
2457 
2458 	frrstr_split(arg, " ", &splits, &num);
2459 
2460 	rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
2461 	rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, splits[0]);
2462 	rcom->name_hash = bgp_clist_hash_key(rcom->name);
2463 
2464 	for (int i = 0; i < num; i++)
2465 		XFREE(MTYPE_TMP, splits[i]);
2466 	XFREE(MTYPE_TMP, splits);
2467 
2468 	return rcom;
2469 }
2470 
2471 /* Free function for set community. */
route_set_community_delete_free(void * rule)2472 static void route_set_community_delete_free(void *rule)
2473 {
2474 	struct rmap_community *rcom = rule;
2475 
2476 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
2477 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
2478 }
2479 
2480 /* Set community rule structure. */
2481 static const struct route_map_rule_cmd route_set_community_delete_cmd = {
2482 	"comm-list",
2483 	route_set_community_delete,
2484 	route_set_community_delete_compile,
2485 	route_set_community_delete_free,
2486 };
2487 
2488 /* `set extcommunity rt COMMUNITY' */
2489 
2490 /* For community set mechanism.  Used by _rt and _soo. */
2491 static enum route_map_cmd_result_t
route_set_ecommunity(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2492 route_set_ecommunity(void *rule, const struct prefix *prefix,
2493 		     route_map_object_t type, void *object)
2494 {
2495 	struct ecommunity *ecom;
2496 	struct ecommunity *new_ecom;
2497 	struct ecommunity *old_ecom;
2498 	struct bgp_path_info *path;
2499 
2500 	if (type == RMAP_BGP) {
2501 		ecom = rule;
2502 		path = object;
2503 
2504 		if (!ecom)
2505 			return RMAP_OKAY;
2506 
2507 		/* We assume additive for Extended Community. */
2508 		old_ecom = path->attr->ecommunity;
2509 
2510 		if (old_ecom) {
2511 			new_ecom = ecommunity_merge(ecommunity_dup(old_ecom),
2512 						    ecom);
2513 
2514 			/* old_ecom->refcnt = 1 => owned elsewhere, e.g.
2515 			 * bgp_update_receive()
2516 			 *         ->refcnt = 0 => set by a previous route-map
2517 			 * statement */
2518 			if (!old_ecom->refcnt)
2519 				ecommunity_free(&old_ecom);
2520 		} else
2521 			new_ecom = ecommunity_dup(ecom);
2522 
2523 		/* will be intern()'d or attr_flush()'d by bgp_update_main() */
2524 		path->attr->ecommunity = new_ecom;
2525 
2526 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
2527 	}
2528 	return RMAP_OKAY;
2529 }
2530 
2531 /* Compile function for set community. */
route_set_ecommunity_rt_compile(const char * arg)2532 static void *route_set_ecommunity_rt_compile(const char *arg)
2533 {
2534 	struct ecommunity *ecom;
2535 
2536 	ecom = ecommunity_str2com(arg, ECOMMUNITY_ROUTE_TARGET, 0);
2537 	if (!ecom)
2538 		return NULL;
2539 	return ecommunity_intern(ecom);
2540 }
2541 
2542 /* Free function for set community.  Used by _rt and _soo */
route_set_ecommunity_free(void * rule)2543 static void route_set_ecommunity_free(void *rule)
2544 {
2545 	struct ecommunity *ecom = rule;
2546 	ecommunity_unintern(&ecom);
2547 }
2548 
2549 /* Set community rule structure. */
2550 static const struct route_map_rule_cmd route_set_ecommunity_rt_cmd = {
2551 	"extcommunity rt",
2552 	route_set_ecommunity,
2553 	route_set_ecommunity_rt_compile,
2554 	route_set_ecommunity_free,
2555 };
2556 
2557 /* `set extcommunity soo COMMUNITY' */
2558 
2559 /* Compile function for set community. */
route_set_ecommunity_soo_compile(const char * arg)2560 static void *route_set_ecommunity_soo_compile(const char *arg)
2561 {
2562 	struct ecommunity *ecom;
2563 
2564 	ecom = ecommunity_str2com(arg, ECOMMUNITY_SITE_ORIGIN, 0);
2565 	if (!ecom)
2566 		return NULL;
2567 
2568 	return ecommunity_intern(ecom);
2569 }
2570 
2571 /* Set community rule structure. */
2572 static const struct route_map_rule_cmd route_set_ecommunity_soo_cmd = {
2573 	"extcommunity soo",
2574 	route_set_ecommunity,
2575 	route_set_ecommunity_soo_compile,
2576 	route_set_ecommunity_free,
2577 };
2578 
2579 /* `set extcommunity bandwidth' */
2580 
2581 struct rmap_ecomm_lb_set {
2582 	uint8_t lb_type;
2583 #define RMAP_ECOMM_LB_SET_VALUE 1
2584 #define RMAP_ECOMM_LB_SET_CUMUL 2
2585 #define RMAP_ECOMM_LB_SET_NUM_MPATH 3
2586 	bool non_trans;
2587 	uint32_t bw;
2588 };
2589 
2590 static enum route_map_cmd_result_t
route_set_ecommunity_lb(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2591 route_set_ecommunity_lb(void *rule, const struct prefix *prefix,
2592 			route_map_object_t type, void *object)
2593 {
2594 	struct rmap_ecomm_lb_set *rels = rule;
2595 	struct bgp_path_info *path;
2596 	struct peer *peer;
2597 	struct ecommunity ecom_lb = {0};
2598 	struct ecommunity_val lb_eval;
2599 	uint32_t bw_bytes = 0;
2600 	uint16_t mpath_count = 0;
2601 	struct ecommunity *new_ecom;
2602 	struct ecommunity *old_ecom;
2603 	as_t as;
2604 
2605 	if (type != RMAP_BGP)
2606 		return RMAP_OKAY;
2607 
2608 	path = object;
2609 	peer = path->peer;
2610 	if (!peer || !peer->bgp)
2611 		return RMAP_ERROR;
2612 
2613 	/* Build link bandwidth extended community */
2614 	as = (peer->bgp->as > BGP_AS_MAX) ? BGP_AS_TRANS : peer->bgp->as;
2615 	if (rels->lb_type == RMAP_ECOMM_LB_SET_VALUE) {
2616 		bw_bytes = ((uint64_t)rels->bw * 1000 * 1000) / 8;
2617 	} else if (rels->lb_type == RMAP_ECOMM_LB_SET_CUMUL) {
2618 		/* process this only for the best path. */
2619 		if (!CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
2620 			return RMAP_OKAY;
2621 
2622 		bw_bytes = (uint32_t)bgp_path_info_mpath_cumbw(path);
2623 		if (!bw_bytes)
2624 			return RMAP_OKAY;
2625 
2626 	} else if (rels->lb_type == RMAP_ECOMM_LB_SET_NUM_MPATH) {
2627 
2628 		/* process this only for the best path. */
2629 		if (!CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
2630 			return RMAP_OKAY;
2631 
2632 		bw_bytes = ((uint64_t)peer->bgp->lb_ref_bw * 1000 * 1000) / 8;
2633 		mpath_count = bgp_path_info_mpath_count(path) + 1;
2634 		bw_bytes *= mpath_count;
2635 	}
2636 
2637 	encode_lb_extcomm(as, bw_bytes, rels->non_trans, &lb_eval);
2638 
2639 	/* add to route or merge with existing */
2640 	old_ecom = path->attr->ecommunity;
2641 	if (old_ecom) {
2642 		new_ecom = ecommunity_dup(old_ecom);
2643 		ecommunity_add_val(new_ecom, &lb_eval, true, true);
2644 		if (!old_ecom->refcnt)
2645 			ecommunity_free(&old_ecom);
2646 	} else {
2647 		ecom_lb.size = 1;
2648 		ecom_lb.unit_size = ECOMMUNITY_SIZE;
2649 		ecom_lb.val = (uint8_t *)lb_eval.val;
2650 		new_ecom = ecommunity_dup(&ecom_lb);
2651 	}
2652 
2653 	/* new_ecom will be intern()'d or attr_flush()'d in call stack */
2654 	path->attr->ecommunity = new_ecom;
2655 	path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
2656 
2657 	/* Mark that route-map has set link bandwidth; used in attribute
2658 	 * setting decisions.
2659 	 */
2660 	SET_FLAG(path->attr->rmap_change_flags, BATTR_RMAP_LINK_BW_SET);
2661 
2662 	return RMAP_OKAY;
2663 }
2664 
route_set_ecommunity_lb_compile(const char * arg)2665 static void *route_set_ecommunity_lb_compile(const char *arg)
2666 {
2667 	struct rmap_ecomm_lb_set *rels;
2668 	uint8_t lb_type;
2669 	uint32_t bw = 0;
2670 	char bw_str[40] = {0};
2671 	char *p, *str;
2672 	bool non_trans = false;
2673 
2674 	str = (char *)arg;
2675 	p = strchr(arg, ' ');
2676 	if (p) {
2677 		int len;
2678 
2679 		len = p - arg;
2680 		memcpy(bw_str, arg, len);
2681 		non_trans = true;
2682 		str = bw_str;
2683 	}
2684 
2685 	if (strcmp(str, "cumulative") == 0)
2686 		lb_type = RMAP_ECOMM_LB_SET_CUMUL;
2687 	else if (strcmp(str, "num-multipaths") == 0)
2688 		lb_type = RMAP_ECOMM_LB_SET_NUM_MPATH;
2689 	else {
2690 		char *end = NULL;
2691 
2692 		bw = strtoul(str, &end, 10);
2693 		if (*end != '\0')
2694 			return NULL;
2695 		lb_type = RMAP_ECOMM_LB_SET_VALUE;
2696 	}
2697 
2698 	rels = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
2699 		       sizeof(struct rmap_ecomm_lb_set));
2700 	rels->lb_type = lb_type;
2701 	rels->bw = bw;
2702 	rels->non_trans = non_trans;
2703 
2704 	return rels;
2705 }
2706 
route_set_ecommunity_lb_free(void * rule)2707 static void route_set_ecommunity_lb_free(void *rule)
2708 {
2709 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2710 }
2711 
2712 /* Set community rule structure. */
2713 struct route_map_rule_cmd route_set_ecommunity_lb_cmd = {
2714 	"extcommunity bandwidth",
2715 	route_set_ecommunity_lb,
2716 	route_set_ecommunity_lb_compile,
2717 	route_set_ecommunity_lb_free,
2718 };
2719 
2720 /* `set origin ORIGIN' */
2721 
2722 /* For origin set. */
2723 static enum route_map_cmd_result_t
route_set_origin(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2724 route_set_origin(void *rule, const struct prefix *prefix,
2725 		 route_map_object_t type, void *object)
2726 {
2727 	uint8_t *origin;
2728 	struct bgp_path_info *path;
2729 
2730 	if (type == RMAP_BGP) {
2731 		origin = rule;
2732 		path = object;
2733 
2734 		path->attr->origin = *origin;
2735 	}
2736 
2737 	return RMAP_OKAY;
2738 }
2739 
2740 /* Compile function for origin set. */
route_set_origin_compile(const char * arg)2741 static void *route_set_origin_compile(const char *arg)
2742 {
2743 	uint8_t *origin;
2744 
2745 	origin = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
2746 
2747 	if (strcmp(arg, "igp") == 0)
2748 		*origin = 0;
2749 	else if (strcmp(arg, "egp") == 0)
2750 		*origin = 1;
2751 	else
2752 		*origin = 2;
2753 
2754 	return origin;
2755 }
2756 
2757 /* Compile function for origin set. */
route_set_origin_free(void * rule)2758 static void route_set_origin_free(void *rule)
2759 {
2760 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2761 }
2762 
2763 /* Set origin rule structure. */
2764 static const struct route_map_rule_cmd route_set_origin_cmd = {
2765 	"origin",
2766 	route_set_origin,
2767 	route_set_origin_compile,
2768 	route_set_origin_free,
2769 };
2770 
2771 /* `set atomic-aggregate' */
2772 
2773 /* For atomic aggregate set. */
2774 static enum route_map_cmd_result_t
route_set_atomic_aggregate(void * rule,const struct prefix * pfx,route_map_object_t type,void * object)2775 route_set_atomic_aggregate(void *rule, const struct prefix *pfx,
2776 			   route_map_object_t type, void *object)
2777 {
2778 	struct bgp_path_info *path;
2779 
2780 	if (type == RMAP_BGP) {
2781 		path = object;
2782 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
2783 	}
2784 
2785 	return RMAP_OKAY;
2786 }
2787 
2788 /* Compile function for atomic aggregate. */
route_set_atomic_aggregate_compile(const char * arg)2789 static void *route_set_atomic_aggregate_compile(const char *arg)
2790 {
2791 	return (void *)1;
2792 }
2793 
2794 /* Compile function for atomic aggregate. */
route_set_atomic_aggregate_free(void * rule)2795 static void route_set_atomic_aggregate_free(void *rule)
2796 {
2797 	return;
2798 }
2799 
2800 /* Set atomic aggregate rule structure. */
2801 static const struct route_map_rule_cmd route_set_atomic_aggregate_cmd = {
2802 	"atomic-aggregate",
2803 	route_set_atomic_aggregate,
2804 	route_set_atomic_aggregate_compile,
2805 	route_set_atomic_aggregate_free,
2806 };
2807 
2808 /* `set aggregator as AS A.B.C.D' */
2809 struct aggregator {
2810 	as_t as;
2811 	struct in_addr address;
2812 };
2813 
2814 static enum route_map_cmd_result_t
route_set_aggregator_as(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2815 route_set_aggregator_as(void *rule, const struct prefix *prefix,
2816 			route_map_object_t type, void *object)
2817 {
2818 	struct bgp_path_info *path;
2819 	struct aggregator *aggregator;
2820 
2821 	if (type == RMAP_BGP) {
2822 		path = object;
2823 		aggregator = rule;
2824 
2825 		path->attr->aggregator_as = aggregator->as;
2826 		path->attr->aggregator_addr = aggregator->address;
2827 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR);
2828 	}
2829 
2830 	return RMAP_OKAY;
2831 }
2832 
route_set_aggregator_as_compile(const char * arg)2833 static void *route_set_aggregator_as_compile(const char *arg)
2834 {
2835 	struct aggregator *aggregator;
2836 	char as[10];
2837 	char address[20];
2838 	int ret;
2839 
2840 	aggregator =
2841 		XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct aggregator));
2842 	if (sscanf(arg, "%s %s", as, address) != 2) {
2843 		XFREE(MTYPE_ROUTE_MAP_COMPILED, aggregator);
2844 		return NULL;
2845 	}
2846 
2847 	aggregator->as = strtoul(as, NULL, 10);
2848 	ret = inet_aton(address, &aggregator->address);
2849 	if (ret == 0) {
2850 		XFREE(MTYPE_ROUTE_MAP_COMPILED, aggregator);
2851 		return NULL;
2852 	}
2853 	return aggregator;
2854 }
2855 
route_set_aggregator_as_free(void * rule)2856 static void route_set_aggregator_as_free(void *rule)
2857 {
2858 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2859 }
2860 
2861 static const struct route_map_rule_cmd route_set_aggregator_as_cmd = {
2862 	"aggregator as",
2863 	route_set_aggregator_as,
2864 	route_set_aggregator_as_compile,
2865 	route_set_aggregator_as_free,
2866 };
2867 
2868 /* Set tag to object. object must be pointer to struct bgp_path_info */
2869 static enum route_map_cmd_result_t
route_set_tag(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2870 route_set_tag(void *rule, const struct prefix *prefix,
2871 	      route_map_object_t type, void *object)
2872 {
2873 	route_tag_t *tag;
2874 	struct bgp_path_info *path;
2875 
2876 	if (type == RMAP_BGP) {
2877 		tag = rule;
2878 		path = object;
2879 
2880 		/* Set tag value */
2881 		path->attr->tag = *tag;
2882 	}
2883 
2884 	return RMAP_OKAY;
2885 }
2886 
2887 /* Route map commands for tag set. */
2888 static const struct route_map_rule_cmd route_set_tag_cmd = {
2889 	"tag",
2890 	route_set_tag,
2891 	route_map_rule_tag_compile,
2892 	route_map_rule_tag_free,
2893 };
2894 
2895 /* Set label-index to object. object must be pointer to struct bgp_path_info */
2896 static enum route_map_cmd_result_t
route_set_label_index(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2897 route_set_label_index(void *rule, const struct prefix *prefix,
2898 		      route_map_object_t type, void *object)
2899 {
2900 	struct rmap_value *rv;
2901 	struct bgp_path_info *path;
2902 	uint32_t label_index;
2903 
2904 	if (type == RMAP_BGP) {
2905 		/* Fetch routemap's rule information. */
2906 		rv = rule;
2907 		path = object;
2908 
2909 		/* Set label-index value. */
2910 		label_index = rv->value;
2911 		if (label_index) {
2912 			path->attr->label_index = label_index;
2913 			path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
2914 		}
2915 	}
2916 
2917 	return RMAP_OKAY;
2918 }
2919 
2920 /* Route map commands for label-index set. */
2921 static const struct route_map_rule_cmd route_set_label_index_cmd = {
2922 	"label-index",
2923 	route_set_label_index,
2924 	route_value_compile,
2925 	route_value_free,
2926 };
2927 
2928 /* `match ipv6 address IP_ACCESS_LIST' */
2929 
2930 static enum route_map_cmd_result_t
route_match_ipv6_address(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2931 route_match_ipv6_address(void *rule, const struct prefix *prefix,
2932 			 route_map_object_t type, void *object)
2933 {
2934 	struct access_list *alist;
2935 
2936 	if (type == RMAP_BGP && prefix->family == AF_INET6) {
2937 		alist = access_list_lookup(AFI_IP6, (char *)rule);
2938 		if (alist == NULL)
2939 			return RMAP_NOMATCH;
2940 
2941 		return (access_list_apply(alist, prefix) == FILTER_DENY
2942 				? RMAP_NOMATCH
2943 				: RMAP_MATCH);
2944 	}
2945 	return RMAP_NOMATCH;
2946 }
2947 
route_match_ipv6_address_compile(const char * arg)2948 static void *route_match_ipv6_address_compile(const char *arg)
2949 {
2950 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
2951 }
2952 
route_match_ipv6_address_free(void * rule)2953 static void route_match_ipv6_address_free(void *rule)
2954 {
2955 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2956 }
2957 
2958 /* Route map commands for ip address matching. */
2959 static const struct route_map_rule_cmd route_match_ipv6_address_cmd = {
2960 	"ipv6 address",
2961 	route_match_ipv6_address,
2962 	route_match_ipv6_address_compile,
2963 	route_match_ipv6_address_free
2964 };
2965 
2966 /* `match ipv6 next-hop IP_ADDRESS' */
2967 
2968 static enum route_map_cmd_result_t
route_match_ipv6_next_hop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)2969 route_match_ipv6_next_hop(void *rule, const struct prefix *prefix,
2970 			  route_map_object_t type, void *object)
2971 {
2972 	struct in6_addr *addr = rule;
2973 	struct bgp_path_info *path;
2974 
2975 	if (type == RMAP_BGP) {
2976 		path = object;
2977 
2978 		if (IPV6_ADDR_SAME(&path->attr->mp_nexthop_global, addr))
2979 			return RMAP_MATCH;
2980 
2981 		if (path->attr->mp_nexthop_len
2982 			    == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
2983 		    && IPV6_ADDR_SAME(&path->attr->mp_nexthop_local, rule))
2984 			return RMAP_MATCH;
2985 
2986 		return RMAP_NOMATCH;
2987 	}
2988 
2989 	return RMAP_NOMATCH;
2990 }
2991 
route_match_ipv6_next_hop_compile(const char * arg)2992 static void *route_match_ipv6_next_hop_compile(const char *arg)
2993 {
2994 	struct in6_addr *address;
2995 	int ret;
2996 
2997 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
2998 
2999 	ret = inet_pton(AF_INET6, arg, address);
3000 	if (!ret) {
3001 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3002 		return NULL;
3003 	}
3004 
3005 	return address;
3006 }
3007 
route_match_ipv6_next_hop_free(void * rule)3008 static void route_match_ipv6_next_hop_free(void *rule)
3009 {
3010 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3011 }
3012 
3013 static const struct route_map_rule_cmd route_match_ipv6_next_hop_cmd = {
3014 	"ipv6 next-hop",
3015 	route_match_ipv6_next_hop,
3016 	route_match_ipv6_next_hop_compile,
3017 	route_match_ipv6_next_hop_free
3018 };
3019 
3020 /* `match ip next-hop IP_ADDRESS' */
3021 
3022 static enum route_map_cmd_result_t
route_match_ipv4_next_hop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3023 route_match_ipv4_next_hop(void *rule, const struct prefix *prefix,
3024 			  route_map_object_t type, void *object)
3025 {
3026 	struct in_addr *addr = rule;
3027 	struct bgp_path_info *path;
3028 
3029 	if (type == RMAP_BGP) {
3030 		path = object;
3031 
3032 		if (path->attr->nexthop.s_addr == addr->s_addr ||
3033 		    (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4 &&
3034 		     IPV4_ADDR_SAME(&path->attr->mp_nexthop_global_in, addr)))
3035 			return RMAP_MATCH;
3036 
3037 		return RMAP_NOMATCH;
3038 	}
3039 
3040 	return RMAP_NOMATCH;
3041 }
3042 
route_match_ipv4_next_hop_compile(const char * arg)3043 static void *route_match_ipv4_next_hop_compile(const char *arg)
3044 {
3045 	struct in_addr *address;
3046 	int ret;
3047 
3048 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3049 
3050 	ret = inet_pton(AF_INET, arg, address);
3051 	if (!ret) {
3052 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3053 		return NULL;
3054 	}
3055 
3056 	return address;
3057 }
3058 
route_match_ipv4_next_hop_free(void * rule)3059 static void route_match_ipv4_next_hop_free(void *rule)
3060 {
3061 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3062 }
3063 
3064 static const struct route_map_rule_cmd route_match_ipv4_next_hop_cmd = {
3065 	"ip next-hop address",
3066 	route_match_ipv4_next_hop,
3067 	route_match_ipv4_next_hop_compile,
3068 	route_match_ipv4_next_hop_free
3069 };
3070 
3071 /* `match ipv6 address prefix-list PREFIX_LIST' */
3072 
3073 static enum route_map_cmd_result_t
route_match_ipv6_address_prefix_list(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3074 route_match_ipv6_address_prefix_list(void *rule, const struct prefix *prefix,
3075 				     route_map_object_t type, void *object)
3076 {
3077 	return route_match_address_prefix_list(rule, AFI_IP6, prefix, type,
3078 					       object);
3079 }
3080 
route_match_ipv6_address_prefix_list_compile(const char * arg)3081 static void *route_match_ipv6_address_prefix_list_compile(const char *arg)
3082 {
3083 	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
3084 }
3085 
route_match_ipv6_address_prefix_list_free(void * rule)3086 static void route_match_ipv6_address_prefix_list_free(void *rule)
3087 {
3088 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3089 }
3090 
3091 static const struct route_map_rule_cmd
3092 		route_match_ipv6_address_prefix_list_cmd = {
3093 	"ipv6 address prefix-list",
3094 	route_match_ipv6_address_prefix_list,
3095 	route_match_ipv6_address_prefix_list_compile,
3096 	route_match_ipv6_address_prefix_list_free
3097 };
3098 
3099 /* `match ipv6 next-hop type <TYPE>' */
3100 
3101 static enum route_map_cmd_result_t
route_match_ipv6_next_hop_type(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3102 route_match_ipv6_next_hop_type(void *rule, const struct prefix *prefix,
3103 			       route_map_object_t type, void *object)
3104 {
3105 	struct bgp_path_info *path;
3106 	struct in6_addr *addr = rule;
3107 
3108 	if (type == RMAP_BGP && prefix->family == AF_INET6) {
3109 		path = (struct bgp_path_info *)object;
3110 		if (!path)
3111 			return RMAP_NOMATCH;
3112 
3113 		if (IPV6_ADDR_SAME(&path->attr->mp_nexthop_global, addr)
3114 		    && !path->attr->nh_ifindex)
3115 			return RMAP_MATCH;
3116 	}
3117 	return RMAP_NOMATCH;
3118 }
3119 
route_match_ipv6_next_hop_type_compile(const char * arg)3120 static void *route_match_ipv6_next_hop_type_compile(const char *arg)
3121 {
3122 	struct in6_addr *address;
3123 	int ret;
3124 
3125 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3126 
3127 	ret = inet_pton(AF_INET6, "::0", address);
3128 	if (!ret) {
3129 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3130 		return NULL;
3131 	}
3132 
3133 	return address;
3134 }
3135 
route_match_ipv6_next_hop_type_free(void * rule)3136 static void route_match_ipv6_next_hop_type_free(void *rule)
3137 {
3138 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3139 }
3140 
3141 static const struct route_map_rule_cmd
3142 		route_match_ipv6_next_hop_type_cmd = {
3143 	"ipv6 next-hop type",
3144 	route_match_ipv6_next_hop_type,
3145 	route_match_ipv6_next_hop_type_compile,
3146 	route_match_ipv6_next_hop_type_free
3147 };
3148 
3149 /* `set ipv6 nexthop global IP_ADDRESS' */
3150 
3151 /* Set nexthop to object.  ojbect must be pointer to struct attr. */
3152 static enum route_map_cmd_result_t
route_set_ipv6_nexthop_global(void * rule,const struct prefix * p,route_map_object_t type,void * object)3153 route_set_ipv6_nexthop_global(void *rule, const struct prefix *p,
3154 			      route_map_object_t type, void *object)
3155 {
3156 	struct in6_addr *address;
3157 	struct bgp_path_info *path;
3158 
3159 	if (type == RMAP_BGP) {
3160 		/* Fetch routemap's rule information. */
3161 		address = rule;
3162 		path = object;
3163 
3164 		/* Set next hop value. */
3165 		path->attr->mp_nexthop_global = *address;
3166 
3167 		/* Set nexthop length. */
3168 		if (path->attr->mp_nexthop_len == 0)
3169 			path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
3170 
3171 		SET_FLAG(path->attr->rmap_change_flags,
3172 			 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED);
3173 	}
3174 
3175 	return RMAP_OKAY;
3176 }
3177 
3178 /* Route map `ip next-hop' compile function.  Given string is converted
3179    to struct in_addr structure. */
route_set_ipv6_nexthop_global_compile(const char * arg)3180 static void *route_set_ipv6_nexthop_global_compile(const char *arg)
3181 {
3182 	int ret;
3183 	struct in6_addr *address;
3184 
3185 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3186 
3187 	ret = inet_pton(AF_INET6, arg, address);
3188 
3189 	if (ret == 0) {
3190 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3191 		return NULL;
3192 	}
3193 
3194 	return address;
3195 }
3196 
3197 /* Free route map's compiled `ip next-hop' value. */
route_set_ipv6_nexthop_global_free(void * rule)3198 static void route_set_ipv6_nexthop_global_free(void *rule)
3199 {
3200 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3201 }
3202 
3203 /* Route map commands for ip nexthop set. */
3204 static const struct route_map_rule_cmd
3205 		route_set_ipv6_nexthop_global_cmd = {
3206 	"ipv6 next-hop global",
3207 	route_set_ipv6_nexthop_global,
3208 	route_set_ipv6_nexthop_global_compile,
3209 	route_set_ipv6_nexthop_global_free
3210 };
3211 
3212 /* Set next-hop preference value. */
3213 static enum route_map_cmd_result_t
route_set_ipv6_nexthop_prefer_global(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3214 route_set_ipv6_nexthop_prefer_global(void *rule, const struct prefix *prefix,
3215 				     route_map_object_t type, void *object)
3216 {
3217 	struct bgp_path_info *path;
3218 	struct peer *peer;
3219 
3220 	if (type == RMAP_BGP) {
3221 		/* Fetch routemap's rule information. */
3222 		path = object;
3223 		peer = path->peer;
3224 
3225 		if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
3226 		    || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT)) {
3227 			/* Set next hop preference to global */
3228 			path->attr->mp_nexthop_prefer_global = true;
3229 			SET_FLAG(path->attr->rmap_change_flags,
3230 				 BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED);
3231 		} else {
3232 			path->attr->mp_nexthop_prefer_global = false;
3233 			SET_FLAG(path->attr->rmap_change_flags,
3234 				 BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED);
3235 		}
3236 	}
3237 	return RMAP_OKAY;
3238 }
3239 
route_set_ipv6_nexthop_prefer_global_compile(const char * arg)3240 static void *route_set_ipv6_nexthop_prefer_global_compile(const char *arg)
3241 {
3242 	int *rins = NULL;
3243 
3244 	rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(int));
3245 	*rins = 1;
3246 
3247 	return rins;
3248 }
3249 
3250 /* Free route map's compiled `ip next-hop' value. */
route_set_ipv6_nexthop_prefer_global_free(void * rule)3251 static void route_set_ipv6_nexthop_prefer_global_free(void *rule)
3252 {
3253 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3254 }
3255 
3256 /* Route map commands for ip nexthop set preferred. */
3257 static const struct route_map_rule_cmd
3258 		route_set_ipv6_nexthop_prefer_global_cmd = {
3259 	"ipv6 next-hop prefer-global",
3260 	route_set_ipv6_nexthop_prefer_global,
3261 	route_set_ipv6_nexthop_prefer_global_compile,
3262 	route_set_ipv6_nexthop_prefer_global_free
3263 };
3264 
3265 /* `set ipv6 nexthop local IP_ADDRESS' */
3266 
3267 /* Set nexthop to object.  ojbect must be pointer to struct attr. */
3268 static enum route_map_cmd_result_t
route_set_ipv6_nexthop_local(void * rule,const struct prefix * p,route_map_object_t type,void * object)3269 route_set_ipv6_nexthop_local(void *rule, const struct prefix *p,
3270 			     route_map_object_t type, void *object)
3271 {
3272 	struct in6_addr *address;
3273 	struct bgp_path_info *path;
3274 
3275 	if (type == RMAP_BGP) {
3276 		/* Fetch routemap's rule information. */
3277 		address = rule;
3278 		path = object;
3279 
3280 		/* Set next hop value. */
3281 		path->attr->mp_nexthop_local = *address;
3282 
3283 		/* Set nexthop length. */
3284 		if (path->attr->mp_nexthop_len
3285 		    != BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3286 			path->attr->mp_nexthop_len =
3287 				BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
3288 
3289 		SET_FLAG(path->attr->rmap_change_flags,
3290 			 BATTR_RMAP_IPV6_LL_NHOP_CHANGED);
3291 	}
3292 
3293 	return RMAP_OKAY;
3294 }
3295 
3296 /* Route map `ip nexthop' compile function.  Given string is converted
3297    to struct in_addr structure. */
route_set_ipv6_nexthop_local_compile(const char * arg)3298 static void *route_set_ipv6_nexthop_local_compile(const char *arg)
3299 {
3300 	int ret;
3301 	struct in6_addr *address;
3302 
3303 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3304 
3305 	ret = inet_pton(AF_INET6, arg, address);
3306 
3307 	if (ret == 0) {
3308 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3309 		return NULL;
3310 	}
3311 
3312 	return address;
3313 }
3314 
3315 /* Free route map's compiled `ip nexthop' value. */
route_set_ipv6_nexthop_local_free(void * rule)3316 static void route_set_ipv6_nexthop_local_free(void *rule)
3317 {
3318 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3319 }
3320 
3321 /* Route map commands for ip nexthop set. */
3322 static const struct route_map_rule_cmd
3323 		route_set_ipv6_nexthop_local_cmd = {
3324 	"ipv6 next-hop local",
3325 	route_set_ipv6_nexthop_local,
3326 	route_set_ipv6_nexthop_local_compile,
3327 	route_set_ipv6_nexthop_local_free
3328 };
3329 
3330 /* `set ipv6 nexthop peer-address' */
3331 
3332 /* Set nexthop to object.  ojbect must be pointer to struct attr. */
3333 static enum route_map_cmd_result_t
route_set_ipv6_nexthop_peer(void * rule,const struct prefix * pfx,route_map_object_t type,void * object)3334 route_set_ipv6_nexthop_peer(void *rule, const struct prefix *pfx,
3335 			    route_map_object_t type, void *object)
3336 {
3337 	struct in6_addr peer_address;
3338 	struct bgp_path_info *path;
3339 	struct peer *peer;
3340 
3341 	if (type == RMAP_BGP) {
3342 		/* Fetch routemap's rule information. */
3343 		path = object;
3344 		peer = path->peer;
3345 
3346 		if ((CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
3347 		     || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT))
3348 		    && peer->su_remote
3349 		    && sockunion_family(peer->su_remote) == AF_INET6) {
3350 			peer_address = peer->su_remote->sin6.sin6_addr;
3351 			/* Set next hop value and length in attribute. */
3352 			if (IN6_IS_ADDR_LINKLOCAL(&peer_address)) {
3353 				path->attr->mp_nexthop_local = peer_address;
3354 				if (path->attr->mp_nexthop_len
3355 				    != BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3356 					path->attr->mp_nexthop_len =
3357 						BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
3358 			} else {
3359 				path->attr->mp_nexthop_global = peer_address;
3360 				if (path->attr->mp_nexthop_len == 0)
3361 					path->attr->mp_nexthop_len =
3362 						BGP_ATTR_NHLEN_IPV6_GLOBAL;
3363 			}
3364 
3365 		} else if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT)) {
3366 			/* The next hop value will be set as part of packet
3367 			 * rewrite.
3368 			 * Set the flags here to indicate that rewrite needs to
3369 			 * be done.
3370 			 * Also, clear the value - we clear both global and
3371 			 * link-local
3372 			 * nexthops, whether we send one or both is determined
3373 			 * elsewhere.
3374 			 */
3375 			SET_FLAG(path->attr->rmap_change_flags,
3376 				 BATTR_RMAP_NEXTHOP_PEER_ADDRESS);
3377 			/* clear next hop value. */
3378 			memset(&(path->attr->mp_nexthop_global), 0,
3379 			       sizeof(struct in6_addr));
3380 			memset(&(path->attr->mp_nexthop_local), 0,
3381 			       sizeof(struct in6_addr));
3382 		}
3383 	}
3384 
3385 	return RMAP_OKAY;
3386 }
3387 
3388 /* Route map `ip next-hop' compile function.  Given string is converted
3389    to struct in_addr structure. */
route_set_ipv6_nexthop_peer_compile(const char * arg)3390 static void *route_set_ipv6_nexthop_peer_compile(const char *arg)
3391 {
3392 	int *rins = NULL;
3393 
3394 	rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(int));
3395 	*rins = 1;
3396 
3397 	return rins;
3398 }
3399 
3400 /* Free route map's compiled `ip next-hop' value. */
route_set_ipv6_nexthop_peer_free(void * rule)3401 static void route_set_ipv6_nexthop_peer_free(void *rule)
3402 {
3403 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3404 }
3405 
3406 /* Route map commands for ip nexthop set. */
3407 static const struct route_map_rule_cmd route_set_ipv6_nexthop_peer_cmd = {
3408 	"ipv6 next-hop peer-address",
3409 	route_set_ipv6_nexthop_peer,
3410 	route_set_ipv6_nexthop_peer_compile,
3411 	route_set_ipv6_nexthop_peer_free
3412 };
3413 
3414 /* `set ipv4 vpn next-hop A.B.C.D' */
3415 
3416 static enum route_map_cmd_result_t
route_set_vpnv4_nexthop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3417 route_set_vpnv4_nexthop(void *rule, const struct prefix *prefix,
3418 			route_map_object_t type, void *object)
3419 {
3420 	struct in_addr *address;
3421 	struct bgp_path_info *path;
3422 
3423 	if (type == RMAP_BGP) {
3424 		/* Fetch routemap's rule information. */
3425 		address = rule;
3426 		path = object;
3427 
3428 		/* Set next hop value. */
3429 		path->attr->mp_nexthop_global_in = *address;
3430 		path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
3431 	}
3432 
3433 	return RMAP_OKAY;
3434 }
3435 
route_set_vpnv4_nexthop_compile(const char * arg)3436 static void *route_set_vpnv4_nexthop_compile(const char *arg)
3437 {
3438 	int ret;
3439 	struct in_addr *address;
3440 
3441 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3442 
3443 	ret = inet_aton(arg, address);
3444 
3445 	if (ret == 0) {
3446 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3447 		return NULL;
3448 	}
3449 
3450 	return address;
3451 }
3452 
3453 /* `set ipv6 vpn next-hop A.B.C.D' */
3454 
3455 static enum route_map_cmd_result_t
route_set_vpnv6_nexthop(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3456 route_set_vpnv6_nexthop(void *rule, const struct prefix *prefix,
3457 			route_map_object_t type, void *object)
3458 {
3459 	struct in6_addr *address;
3460 	struct bgp_path_info *path;
3461 
3462 	if (type == RMAP_BGP) {
3463 		/* Fetch routemap's rule information. */
3464 		address = rule;
3465 		path = object;
3466 
3467 		/* Set next hop value. */
3468 		memcpy(&path->attr->mp_nexthop_global, address,
3469 		       sizeof(struct in6_addr));
3470 		path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_VPNV6_GLOBAL;
3471 	}
3472 
3473 	return RMAP_OKAY;
3474 }
3475 
route_set_vpnv6_nexthop_compile(const char * arg)3476 static void *route_set_vpnv6_nexthop_compile(const char *arg)
3477 {
3478 	int ret;
3479 	struct in6_addr *address;
3480 
3481 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3482 	ret = inet_pton(AF_INET6, arg, address);
3483 
3484 	if (ret == 0) {
3485 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3486 		return NULL;
3487 	}
3488 
3489 	return address;
3490 }
3491 
route_set_vpn_nexthop_free(void * rule)3492 static void route_set_vpn_nexthop_free(void *rule)
3493 {
3494 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3495 }
3496 
3497 /* Route map commands for ipv4 next-hop set. */
3498 static const struct route_map_rule_cmd route_set_vpnv4_nexthop_cmd = {
3499 	"ipv4 vpn next-hop",
3500 	route_set_vpnv4_nexthop,
3501 	route_set_vpnv4_nexthop_compile,
3502 	route_set_vpn_nexthop_free
3503 };
3504 
3505 /* Route map commands for ipv6 next-hop set. */
3506 static const struct route_map_rule_cmd route_set_vpnv6_nexthop_cmd = {
3507 	"ipv6 vpn next-hop",
3508 	route_set_vpnv6_nexthop,
3509 	route_set_vpnv6_nexthop_compile,
3510 	route_set_vpn_nexthop_free
3511 };
3512 
3513 /* `set originator-id' */
3514 
3515 /* For origin set. */
3516 static enum route_map_cmd_result_t
route_set_originator_id(void * rule,const struct prefix * prefix,route_map_object_t type,void * object)3517 route_set_originator_id(void *rule, const struct prefix *prefix,
3518 			route_map_object_t type, void *object)
3519 {
3520 	struct in_addr *address;
3521 	struct bgp_path_info *path;
3522 
3523 	if (type == RMAP_BGP) {
3524 		address = rule;
3525 		path = object;
3526 
3527 		path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID);
3528 		path->attr->originator_id = *address;
3529 	}
3530 
3531 	return RMAP_OKAY;
3532 }
3533 
3534 /* Compile function for originator-id set. */
route_set_originator_id_compile(const char * arg)3535 static void *route_set_originator_id_compile(const char *arg)
3536 {
3537 	int ret;
3538 	struct in_addr *address;
3539 
3540 	address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3541 
3542 	ret = inet_aton(arg, address);
3543 
3544 	if (ret == 0) {
3545 		XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3546 		return NULL;
3547 	}
3548 
3549 	return address;
3550 }
3551 
3552 /* Compile function for originator_id set. */
route_set_originator_id_free(void * rule)3553 static void route_set_originator_id_free(void *rule)
3554 {
3555 	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3556 }
3557 
3558 /* Set originator-id rule structure. */
3559 static const struct route_map_rule_cmd route_set_originator_id_cmd = {
3560 	"originator-id",
3561 	route_set_originator_id,
3562 	route_set_originator_id_compile,
3563 	route_set_originator_id_free,
3564 };
3565 
3566 /* Add bgp route map rule. */
bgp_route_match_add(struct vty * vty,const char * command,const char * arg,route_map_event_t type)3567 static int bgp_route_match_add(struct vty *vty, const char *command,
3568 			       const char *arg, route_map_event_t type)
3569 {
3570 	VTY_DECLVAR_CONTEXT(route_map_index, index);
3571 	int retval = CMD_SUCCESS;
3572 	enum rmap_compile_rets ret;
3573 
3574 	ret = route_map_add_match(index, command, arg, type);
3575 	switch (ret) {
3576 	case RMAP_RULE_MISSING:
3577 		vty_out(vty, "%% BGP Can't find rule.\n");
3578 		retval = CMD_WARNING_CONFIG_FAILED;
3579 		break;
3580 	case RMAP_COMPILE_ERROR:
3581 		vty_out(vty, "%% BGP Argument is malformed.\n");
3582 		retval = CMD_WARNING_CONFIG_FAILED;
3583 		break;
3584 	case RMAP_COMPILE_SUCCESS:
3585 		/*
3586 		 * Intentionally doing nothing here.
3587 		 */
3588 		break;
3589 	}
3590 
3591 	return retval;
3592 }
3593 
3594 /* Delete bgp route map rule. */
bgp_route_match_delete(struct vty * vty,const char * command,const char * arg,route_map_event_t type)3595 static int bgp_route_match_delete(struct vty *vty, const char *command,
3596 				  const char *arg, route_map_event_t type)
3597 {
3598 	VTY_DECLVAR_CONTEXT(route_map_index, index);
3599 	enum rmap_compile_rets ret;
3600 	int retval = CMD_SUCCESS;
3601 	char *dep_name = NULL;
3602 	const char *tmpstr;
3603 	char *rmap_name = NULL;
3604 
3605 	if (type != RMAP_EVENT_MATCH_DELETED) {
3606 		/* ignore the mundane, the types without any dependency */
3607 		if (arg == NULL) {
3608 			if ((tmpstr = route_map_get_match_arg(index, command))
3609 			    != NULL)
3610 				dep_name =
3611 					XSTRDUP(MTYPE_ROUTE_MAP_RULE, tmpstr);
3612 		} else {
3613 			dep_name = XSTRDUP(MTYPE_ROUTE_MAP_RULE, arg);
3614 		}
3615 		rmap_name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, index->map->name);
3616 	}
3617 
3618 	ret = route_map_delete_match(index, command, dep_name, type);
3619 	switch (ret) {
3620 	case RMAP_RULE_MISSING:
3621 		vty_out(vty, "%% BGP Can't find rule.\n");
3622 		retval = CMD_WARNING_CONFIG_FAILED;
3623 		break;
3624 	case RMAP_COMPILE_ERROR:
3625 		vty_out(vty, "%% BGP Argument is malformed.\n");
3626 		retval = CMD_WARNING_CONFIG_FAILED;
3627 		break;
3628 	case RMAP_COMPILE_SUCCESS:
3629 		/*
3630 		 * Nothing to do here
3631 		 */
3632 		break;
3633 	}
3634 
3635 	XFREE(MTYPE_ROUTE_MAP_RULE, dep_name);
3636 	XFREE(MTYPE_ROUTE_MAP_NAME, rmap_name);
3637 
3638 	return retval;
3639 }
3640 
3641 /*
3642  * This is the workhorse routine for processing in/out routemap
3643  * modifications.
3644  */
bgp_route_map_process_peer(const char * rmap_name,struct route_map * map,struct peer * peer,int afi,int safi,int route_update)3645 static void bgp_route_map_process_peer(const char *rmap_name,
3646 				       struct route_map *map, struct peer *peer,
3647 				       int afi, int safi, int route_update)
3648 {
3649 	struct bgp_filter *filter;
3650 
3651 	if (!peer || !rmap_name)
3652 		return;
3653 
3654 	filter = &peer->filter[afi][safi];
3655 	/*
3656 	 * in is for non-route-server clients,
3657 	 * out is for all peers
3658 	 */
3659 	if (filter->map[RMAP_IN].name
3660 	    && (strcmp(rmap_name, filter->map[RMAP_IN].name) == 0)) {
3661 		filter->map[RMAP_IN].map = map;
3662 
3663 		if (route_update && peer->status == Established) {
3664 			if (CHECK_FLAG(peer->af_flags[afi][safi],
3665 				       PEER_FLAG_SOFT_RECONFIG)) {
3666 				if (bgp_debug_update(peer, NULL, NULL, 1))
3667 					zlog_debug(
3668 						"Processing route_map %s update on peer %s (inbound, soft-reconfig)",
3669 						rmap_name, peer->host);
3670 
3671 				bgp_soft_reconfig_in(peer, afi, safi);
3672 			} else if (CHECK_FLAG(peer->cap,
3673 					      PEER_CAP_REFRESH_OLD_RCV)
3674 				   || CHECK_FLAG(peer->cap,
3675 						 PEER_CAP_REFRESH_NEW_RCV)) {
3676 				if (bgp_debug_update(peer, NULL, NULL, 1))
3677 					zlog_debug(
3678 						"Processing route_map %s update on peer %s (inbound, route-refresh)",
3679 						rmap_name, peer->host);
3680 				bgp_route_refresh_send(peer, afi, safi, 0, 0,
3681 						       0);
3682 			}
3683 		}
3684 	}
3685 
3686 	/*
3687 	 * For outbound, unsuppress and default-originate map change (content or
3688 	 * map created), merely update the "config" here, the actual route
3689 	 * announcement happens at the group level.
3690 	 */
3691 	if (filter->map[RMAP_OUT].name
3692 	    && (strcmp(rmap_name, filter->map[RMAP_OUT].name) == 0))
3693 		filter->map[RMAP_OUT].map = map;
3694 
3695 	if (filter->usmap.name && (strcmp(rmap_name, filter->usmap.name) == 0))
3696 		filter->usmap.map = map;
3697 
3698 	if (peer->default_rmap[afi][safi].name
3699 	    && (strcmp(rmap_name, peer->default_rmap[afi][safi].name) == 0))
3700 		peer->default_rmap[afi][safi].map = map;
3701 }
3702 
bgp_route_map_update_peer_group(const char * rmap_name,struct route_map * map,struct bgp * bgp)3703 static void bgp_route_map_update_peer_group(const char *rmap_name,
3704 					    struct route_map *map,
3705 					    struct bgp *bgp)
3706 {
3707 	struct peer_group *group;
3708 	struct listnode *node, *nnode;
3709 	struct bgp_filter *filter;
3710 	int afi, safi;
3711 	int direct;
3712 
3713 	if (!bgp)
3714 		return;
3715 
3716 	/* All the peers have been updated correctly already. This is
3717 	 * just updating the placeholder data. No real update required.
3718 	 */
3719 	for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
3720 		FOREACH_AFI_SAFI (afi, safi) {
3721 			filter = &group->conf->filter[afi][safi];
3722 
3723 			for (direct = RMAP_IN; direct < RMAP_MAX; direct++) {
3724 				if ((filter->map[direct].name)
3725 				    && (strcmp(rmap_name,
3726 					       filter->map[direct].name)
3727 					== 0))
3728 					filter->map[direct].map = map;
3729 			}
3730 
3731 			if (filter->usmap.name
3732 			    && (strcmp(rmap_name, filter->usmap.name) == 0))
3733 				filter->usmap.map = map;
3734 		}
3735 	}
3736 }
3737 
3738 /*
3739  * Note that if an extreme number (tens of thousands) of route-maps are in use
3740  * and if bgp has an extreme number of peers, network statements, etc then this
3741  * function can consume a lot of cycles. This is due to this function being
3742  * called for each route-map and within this function we walk the list of peers,
3743  * network statements, etc looking to see if they use this route-map.
3744  */
bgp_route_map_process_update(struct bgp * bgp,const char * rmap_name,int route_update)3745 static void bgp_route_map_process_update(struct bgp *bgp, const char *rmap_name,
3746 					 int route_update)
3747 {
3748 	int i;
3749 	afi_t afi;
3750 	safi_t safi;
3751 	struct peer *peer;
3752 	struct bgp_dest *bn;
3753 	struct bgp_static *bgp_static;
3754 	struct bgp_aggregate *aggregate;
3755 	struct listnode *node, *nnode;
3756 	struct route_map *map;
3757 	char buf[INET6_ADDRSTRLEN];
3758 
3759 	map = route_map_lookup_by_name(rmap_name);
3760 
3761 	for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
3762 
3763 		/* Ignore dummy peer-group structure */
3764 		if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
3765 			continue;
3766 
3767 		FOREACH_AFI_SAFI (afi, safi) {
3768 			/* process in/out/import/export/default-orig
3769 			 * route-maps */
3770 			bgp_route_map_process_peer(rmap_name, map, peer, afi,
3771 						   safi, route_update);
3772 		}
3773 	}
3774 
3775 	/* for outbound/default-orig route-maps, process for groups */
3776 	update_group_policy_update(bgp, BGP_POLICY_ROUTE_MAP, rmap_name,
3777 				   route_update, 0);
3778 
3779 	/* update peer-group config (template) */
3780 	bgp_route_map_update_peer_group(rmap_name, map, bgp);
3781 
3782 	FOREACH_AFI_SAFI (afi, safi) {
3783 		/* For table route-map updates. */
3784 		if (!bgp_fibupd_safi(safi))
3785 			continue;
3786 
3787 		if (bgp->table_map[afi][safi].name
3788 		    && (strcmp(rmap_name, bgp->table_map[afi][safi].name)
3789 			== 0)) {
3790 
3791 			/* bgp->table_map[afi][safi].map  is NULL.
3792 			 * i.e Route map creation event.
3793 			 * So update applied_counter.
3794 			 * If it is not NULL, i.e It may be routemap updation or
3795 			 * deletion. so no need to update the counter.
3796 			 */
3797 			if (!bgp->table_map[afi][safi].map)
3798 				route_map_counter_increment(map);
3799 			bgp->table_map[afi][safi].map = map;
3800 
3801 			if (BGP_DEBUG(zebra, ZEBRA))
3802 				zlog_debug(
3803 					"Processing route_map %s update on table map",
3804 					rmap_name);
3805 			if (route_update)
3806 				bgp_zebra_announce_table(bgp, afi, safi);
3807 		}
3808 
3809 		/* For network route-map updates. */
3810 		for (bn = bgp_table_top(bgp->route[afi][safi]); bn;
3811 		     bn = bgp_route_next(bn)) {
3812 			bgp_static = bgp_dest_get_bgp_static_info(bn);
3813 			if (!bgp_static)
3814 				continue;
3815 
3816 			if (!bgp_static->rmap.name
3817 			    || (strcmp(rmap_name, bgp_static->rmap.name) != 0))
3818 				continue;
3819 
3820 			if (!bgp_static->rmap.map)
3821 				route_map_counter_increment(map);
3822 
3823 			bgp_static->rmap.map = map;
3824 
3825 			if (route_update && !bgp_static->backdoor) {
3826 				const struct prefix *bn_p =
3827 					bgp_dest_get_prefix(bn);
3828 
3829 				if (bgp_debug_zebra(bn_p))
3830 					zlog_debug(
3831 						"Processing route_map %s update on static route %s",
3832 						rmap_name,
3833 						inet_ntop(bn_p->family,
3834 							  &bn_p->u.prefix, buf,
3835 							  INET6_ADDRSTRLEN));
3836 				bgp_static_update(bgp, bn_p, bgp_static, afi,
3837 						  safi);
3838 			}
3839 		}
3840 
3841 		/* For aggregate-address route-map updates. */
3842 		for (bn = bgp_table_top(bgp->aggregate[afi][safi]); bn;
3843 		     bn = bgp_route_next(bn)) {
3844 			aggregate = bgp_dest_get_bgp_aggregate_info(bn);
3845 			if (!aggregate)
3846 				continue;
3847 
3848 			if (!aggregate->rmap.name
3849 			    || (strcmp(rmap_name, aggregate->rmap.name) != 0))
3850 				continue;
3851 
3852 			if (!aggregate->rmap.map)
3853 				route_map_counter_increment(map);
3854 
3855 			aggregate->rmap.map = map;
3856 
3857 			if (route_update) {
3858 				const struct prefix *bn_p =
3859 					bgp_dest_get_prefix(bn);
3860 
3861 				if (bgp_debug_zebra(bn_p))
3862 					zlog_debug(
3863 						"Processing route_map %s update on aggregate-address route %s",
3864 						rmap_name,
3865 						inet_ntop(bn_p->family,
3866 							  &bn_p->u.prefix, buf,
3867 							  INET6_ADDRSTRLEN));
3868 				bgp_aggregate_route(bgp, bn_p, afi, safi,
3869 						    aggregate);
3870 			}
3871 		}
3872 	}
3873 
3874 	/* For redistribute route-map updates. */
3875 	for (afi = AFI_IP; afi < AFI_MAX; afi++)
3876 		for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3877 			struct list *red_list;
3878 			struct bgp_redist *red;
3879 
3880 			red_list = bgp->redist[afi][i];
3881 			if (!red_list)
3882 				continue;
3883 
3884 			for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
3885 				if (!red->rmap.name
3886 				    || (strcmp(rmap_name, red->rmap.name) != 0))
3887 					continue;
3888 
3889 				if (!red->rmap.map)
3890 					route_map_counter_increment(map);
3891 
3892 				red->rmap.map = map;
3893 
3894 				if (!route_update)
3895 					continue;
3896 
3897 				if (BGP_DEBUG(zebra, ZEBRA))
3898 					zlog_debug(
3899 						"Processing route_map %s update on redistributed routes",
3900 						rmap_name);
3901 
3902 				bgp_redistribute_resend(bgp, afi, i,
3903 							red->instance);
3904 			}
3905 		}
3906 
3907 	/* for type5 command route-maps */
3908 	FOREACH_AFI_SAFI (afi, safi) {
3909 		if (!bgp->adv_cmd_rmap[afi][safi].name
3910 		    || strcmp(rmap_name, bgp->adv_cmd_rmap[afi][safi].name)
3911 			       != 0)
3912 			continue;
3913 
3914 		/* Make sure the route-map is populated here if not already done */
3915 		bgp->adv_cmd_rmap[afi][safi].map = map;
3916 
3917 		if (BGP_DEBUG(zebra, ZEBRA))
3918 			zlog_debug(
3919 				"Processing route_map %s update on advertise type5 route command",
3920 				rmap_name);
3921 
3922 		if (route_update && advertise_type5_routes(bgp, afi)) {
3923 			bgp_evpn_withdraw_type5_routes(bgp, afi, safi);
3924 			bgp_evpn_advertise_type5_routes(bgp, afi, safi);
3925 		}
3926 	}
3927 }
3928 
bgp_route_map_process_update_cb(char * rmap_name)3929 static void bgp_route_map_process_update_cb(char *rmap_name)
3930 {
3931 	struct listnode *node, *nnode;
3932 	struct bgp *bgp;
3933 
3934 	for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
3935 		bgp_route_map_process_update(bgp, rmap_name, 1);
3936 
3937 #ifdef ENABLE_BGP_VNC
3938 		/* zlog_debug("%s: calling vnc_routemap_update", __func__); */
3939 		vnc_routemap_update(bgp, __func__);
3940 #endif
3941 	}
3942 
3943 	vpn_policy_routemap_event(rmap_name);
3944 }
3945 
bgp_route_map_update_timer(struct thread * thread)3946 int bgp_route_map_update_timer(struct thread *thread)
3947 {
3948 	bm->t_rmap_update = NULL;
3949 
3950 	route_map_walk_update_list(bgp_route_map_process_update_cb);
3951 
3952 	return 0;
3953 }
3954 
bgp_route_map_mark_update(const char * rmap_name)3955 static void bgp_route_map_mark_update(const char *rmap_name)
3956 {
3957 	struct listnode *node, *nnode;
3958 	struct bgp *bgp;
3959 
3960 	/* If new update is received before the current timer timed out,
3961 	 * turn it off and start a new timer.
3962 	 */
3963 	if (bm->t_rmap_update != NULL)
3964 		THREAD_OFF(bm->t_rmap_update);
3965 
3966 	/* rmap_update_timer of 0 means don't do route updates */
3967 	if (bm->rmap_update_timer) {
3968 		thread_add_timer(bm->master, bgp_route_map_update_timer,
3969 				 NULL, bm->rmap_update_timer,
3970 				 &bm->t_rmap_update);
3971 
3972 		/* Signal the groups that a route-map update event has
3973 		 * started */
3974 		for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3975 			update_group_policy_update(bgp,
3976 						   BGP_POLICY_ROUTE_MAP,
3977 						   rmap_name, 1, 1);
3978 	} else {
3979 		for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3980 			bgp_route_map_process_update(bgp, rmap_name, 0);
3981 #ifdef ENABLE_BGP_VNC
3982 		zlog_debug("%s: calling vnc_routemap_update", __func__);
3983 		vnc_routemap_update(bgp, __func__);
3984 #endif
3985 	}
3986 }
3987 
bgp_route_map_add(const char * rmap_name)3988 static void bgp_route_map_add(const char *rmap_name)
3989 {
3990 	if (route_map_mark_updated(rmap_name) == 0)
3991 		bgp_route_map_mark_update(rmap_name);
3992 
3993 	route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
3994 }
3995 
bgp_route_map_delete(const char * rmap_name)3996 static void bgp_route_map_delete(const char *rmap_name)
3997 {
3998 	if (route_map_mark_updated(rmap_name) == 0)
3999 		bgp_route_map_mark_update(rmap_name);
4000 
4001 	route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_DELETED);
4002 }
4003 
bgp_route_map_event(const char * rmap_name)4004 static void bgp_route_map_event(const char *rmap_name)
4005 {
4006 	if (route_map_mark_updated(rmap_name) == 0)
4007 		bgp_route_map_mark_update(rmap_name);
4008 
4009 	route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
4010 }
4011 
4012 DEFUN (match_mac_address,
4013        match_mac_address_cmd,
4014        "match mac address WORD",
4015        MATCH_STR
4016        "mac address\n"
4017        "Match address of route\n"
4018        "MAC Access-list name\n")
4019 {
4020 	return bgp_route_match_add(vty, "mac address", argv[3]->arg,
4021 				   RMAP_EVENT_FILTER_ADDED);
4022 }
4023 
4024 DEFUN (no_match_mac_address,
4025        no_match_mac_address_cmd,
4026        "no match mac address WORD",
4027        NO_STR
4028        MATCH_STR
4029        "mac\n"
4030        "Match address of route\n"
4031        "MAC acess-list name\n")
4032 {
4033 	return bgp_route_match_delete(vty, "mac address", argv[4]->arg,
4034 				      RMAP_EVENT_FILTER_DELETED);
4035 }
4036 
4037 DEFUN (match_evpn_route_type,
4038        match_evpn_route_type_cmd,
4039        "match evpn route-type <macip | multicast | prefix>",
4040        MATCH_STR
4041        EVPN_HELP_STR
4042        "Match route-type\n"
4043        "mac-ip route\n"
4044        "IMET route\n"
4045        "prefix route\n")
4046 {
4047 	return bgp_route_match_add(vty, "evpn route-type", argv[3]->arg,
4048 				   RMAP_EVENT_MATCH_ADDED);
4049 }
4050 
4051 DEFUN (no_match_evpn_route_type,
4052        no_match_evpn_route_type_cmd,
4053        "no match evpn route-type <macip | multicast | prefix>",
4054        NO_STR
4055        MATCH_STR
4056        EVPN_HELP_STR
4057        "Match route-type\n"
4058        "mac-ip route\n"
4059        "IMET route\n"
4060        "prefix route\n")
4061 {
4062 	return bgp_route_match_delete(vty, "evpn route-type", argv[4]->arg,
4063 				      RMAP_EVENT_MATCH_DELETED);
4064 }
4065 
4066 
4067 DEFUN (match_evpn_vni,
4068        match_evpn_vni_cmd,
4069        "match evpn vni " CMD_VNI_RANGE,
4070        MATCH_STR
4071        EVPN_HELP_STR
4072        "Match VNI\n"
4073        "VNI ID\n")
4074 {
4075 	return bgp_route_match_add(vty, "evpn vni", argv[3]->arg,
4076 				   RMAP_EVENT_MATCH_ADDED);
4077 }
4078 
4079 DEFUN (no_match_evpn_vni,
4080        no_match_evpn_vni_cmd,
4081        "no match evpn vni " CMD_VNI_RANGE,
4082        NO_STR
4083        MATCH_STR
4084        EVPN_HELP_STR
4085        "Match VNI\n"
4086        "VNI ID\n")
4087 {
4088 	return bgp_route_match_delete(vty, "evpn vni", argv[4]->arg,
4089 				      RMAP_EVENT_MATCH_DELETED);
4090 }
4091 
4092 DEFUN (match_evpn_default_route,
4093        match_evpn_default_route_cmd,
4094        "match evpn default-route",
4095        MATCH_STR
4096        EVPN_HELP_STR
4097        "default EVPN type-5 route\n")
4098 {
4099 	return bgp_route_match_add(vty, "evpn default-route", NULL,
4100 				   RMAP_EVENT_MATCH_ADDED);
4101 }
4102 
4103 DEFUN (no_match_evpn_default_route,
4104        no_match_evpn_default_route_cmd,
4105        "no match evpn default-route",
4106        NO_STR
4107        MATCH_STR
4108        EVPN_HELP_STR
4109        "default EVPN type-5 route\n")
4110 {
4111 	return bgp_route_match_delete(vty, "evpn default-route", NULL,
4112 				      RMAP_EVENT_MATCH_DELETED);
4113 }
4114 
4115 DEFUN (match_evpn_rd,
4116        match_evpn_rd_cmd,
4117        "match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4118        MATCH_STR
4119        EVPN_HELP_STR
4120        "Route Distinguisher\n"
4121        "ASN:XX or A.B.C.D:XX\n")
4122 {
4123 	return bgp_route_match_add(vty, "evpn rd", argv[3]->arg,
4124 				   RMAP_EVENT_MATCH_ADDED);
4125 }
4126 
4127 DEFUN (no_match_evpn_rd,
4128        no_match_evpn_rd_cmd,
4129        "no match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4130        NO_STR
4131        MATCH_STR
4132        EVPN_HELP_STR
4133        "Route Distinguisher\n"
4134        "ASN:XX or A.B.C.D:XX\n")
4135 {
4136 	return bgp_route_match_delete(vty, "evpn rd", argv[4]->arg,
4137 				      RMAP_EVENT_MATCH_DELETED);
4138 }
4139 
4140 DEFPY(match_vrl_source_vrf,
4141       match_vrl_source_vrf_cmd,
4142       "match source-vrf NAME$vrf_name",
4143       MATCH_STR
4144       "source vrf\n"
4145       "The VRF name\n")
4146 {
4147 	return bgp_route_match_add(vty, "source-vrf", vrf_name,
4148 				   RMAP_EVENT_MATCH_ADDED);
4149 }
4150 
4151 DEFPY(no_match_vrl_source_vrf,
4152       no_match_vrl_source_vrf_cmd,
4153       "no match source-vrf NAME$vrf_name",
4154       NO_STR
4155       MATCH_STR
4156       "source vrf\n"
4157       "The VRF name\n")
4158 {
4159 	return bgp_route_match_delete(vty, "source-vrf", vrf_name,
4160 				      RMAP_EVENT_MATCH_DELETED);
4161 }
4162 
4163 DEFUN (match_peer,
4164        match_peer_cmd,
4165        "match peer <A.B.C.D|X:X::X:X|WORD>",
4166        MATCH_STR
4167        "Match peer address\n"
4168        "IP address of peer\n"
4169        "IPv6 address of peer\n"
4170        "Interface name of peer\n")
4171 {
4172 	int idx_ip = 2;
4173 	return bgp_route_match_add(vty, "peer", argv[idx_ip]->arg,
4174 				   RMAP_EVENT_MATCH_ADDED);
4175 }
4176 
4177 DEFUN (match_peer_local,
4178        match_peer_local_cmd,
4179         "match peer local",
4180         MATCH_STR
4181         "Match peer address\n"
4182         "Static or Redistributed routes\n")
4183 {
4184 	return bgp_route_match_add(vty, "peer", "local",
4185 				   RMAP_EVENT_MATCH_DELETED);
4186 }
4187 
4188 DEFUN (no_match_peer,
4189        no_match_peer_cmd,
4190        "no match peer [<local|A.B.C.D|X:X::X:X|WORD>]",
4191        NO_STR
4192        MATCH_STR
4193        "Match peer address\n"
4194        "Static or Redistributed routes\n"
4195        "IP address of peer\n"
4196        "IPv6 address of peer\n"
4197        "Interface name of peer\n")
4198 {
4199 	int idx_peer = 3;
4200 
4201 	if (argc <= idx_peer)
4202 		return bgp_route_match_delete(vty, "peer", NULL,
4203 					      RMAP_EVENT_MATCH_DELETED);
4204 	return bgp_route_match_delete(vty, "peer", argv[idx_peer]->arg,
4205 				      RMAP_EVENT_MATCH_DELETED);
4206 }
4207 
4208 #if defined(HAVE_LUA)
4209 DEFUN (match_command,
4210        match_command_cmd,
4211        "match command WORD",
4212        MATCH_STR
4213        "Run a command to match\n"
4214        "The command to run\n")
4215 {
4216 	return bgp_route_match_add(vty, "command", argv[2]->arg,
4217 				   RMAP_EVENT_FILTER_ADDED);
4218 }
4219 
4220 DEFUN (no_match_command,
4221        no_match_command_cmd,
4222        "no match command WORD",
4223        NO_STR
4224        MATCH_STR
4225        "Run a command to match\n"
4226        "The command to run\n")
4227 {
4228 	return bgp_route_match_delete(vty, "command", argv[3]->arg,
4229 				      RMAP_EVENT_FILTER_DELETED);
4230 }
4231 #endif
4232 
4233 /* match probability */
4234 DEFUN (match_probability,
4235        match_probability_cmd,
4236        "match probability (0-100)",
4237        MATCH_STR
4238        "Match portion of routes defined by percentage value\n"
4239        "Percentage of routes\n")
4240 {
4241 	int idx_number = 2;
4242 	return bgp_route_match_add(vty, "probability", argv[idx_number]->arg,
4243 				   RMAP_EVENT_MATCH_ADDED);
4244 }
4245 
4246 
4247 DEFUN (no_match_probability,
4248        no_match_probability_cmd,
4249        "no match probability [(1-99)]",
4250        NO_STR
4251        MATCH_STR
4252        "Match portion of routes defined by percentage value\n"
4253        "Percentage of routes\n")
4254 {
4255 	int idx_number = 3;
4256 	if (argc <= idx_number)
4257 		return bgp_route_match_delete(vty, "probability", NULL,
4258 					      RMAP_EVENT_MATCH_DELETED);
4259 	return bgp_route_match_delete(vty, "probability", argv[idx_number]->arg,
4260 				      RMAP_EVENT_MATCH_DELETED);
4261 }
4262 
4263 
4264 DEFUN (match_ip_route_source,
4265        match_ip_route_source_cmd,
4266        "match ip route-source <(1-199)|(1300-2699)|WORD>",
4267        MATCH_STR
4268        IP_STR
4269        "Match advertising source address of route\n"
4270        "IP access-list number\n"
4271        "IP access-list number (expanded range)\n"
4272        "IP standard access-list name\n")
4273 {
4274 	int idx_acl = 3;
4275 	return bgp_route_match_add(vty, "ip route-source", argv[idx_acl]->arg,
4276 				   RMAP_EVENT_FILTER_ADDED);
4277 }
4278 
4279 
4280 DEFUN (no_match_ip_route_source,
4281        no_match_ip_route_source_cmd,
4282        "no match ip route-source [<(1-199)|(1300-2699)|WORD>]",
4283        NO_STR
4284        MATCH_STR
4285        IP_STR
4286        "Match advertising source address of route\n"
4287        "IP access-list number\n"
4288        "IP access-list number (expanded range)\n"
4289        "IP standard access-list name\n")
4290 {
4291 	int idx_number = 4;
4292 	if (argc <= idx_number)
4293 		return bgp_route_match_delete(vty, "ip route-source", NULL,
4294 					      RMAP_EVENT_FILTER_DELETED);
4295 	return bgp_route_match_delete(vty, "ip route-source",
4296 				      argv[idx_number]->arg,
4297 				      RMAP_EVENT_FILTER_DELETED);
4298 }
4299 
4300 
4301 DEFUN (match_ip_route_source_prefix_list,
4302        match_ip_route_source_prefix_list_cmd,
4303        "match ip route-source prefix-list WORD",
4304        MATCH_STR
4305        IP_STR
4306        "Match advertising source address of route\n"
4307        "Match entries of prefix-lists\n"
4308        "IP prefix-list name\n")
4309 {
4310 	int idx_word = 4;
4311 	return bgp_route_match_add(vty, "ip route-source prefix-list",
4312 				   argv[idx_word]->arg, RMAP_EVENT_PLIST_ADDED);
4313 }
4314 
4315 
4316 DEFUN (no_match_ip_route_source_prefix_list,
4317        no_match_ip_route_source_prefix_list_cmd,
4318        "no match ip route-source prefix-list [WORD]",
4319        NO_STR
4320        MATCH_STR
4321        IP_STR
4322        "Match advertising source address of route\n"
4323        "Match entries of prefix-lists\n"
4324        "IP prefix-list name\n")
4325 {
4326 	int idx_word = 5;
4327 	if (argc <= idx_word)
4328 		return bgp_route_match_delete(vty,
4329 					      "ip route-source prefix-list",
4330 					      NULL, RMAP_EVENT_PLIST_DELETED);
4331 	return bgp_route_match_delete(vty, "ip route-source prefix-list",
4332 				      argv[idx_word]->arg,
4333 				      RMAP_EVENT_PLIST_DELETED);
4334 }
4335 
4336 
4337 DEFUN (match_local_pref,
4338        match_local_pref_cmd,
4339        "match local-preference (0-4294967295)",
4340        MATCH_STR
4341        "Match local-preference of route\n"
4342        "Metric value\n")
4343 {
4344 	int idx_number = 2;
4345 	return bgp_route_match_add(vty, "local-preference",
4346 				   argv[idx_number]->arg,
4347 				   RMAP_EVENT_MATCH_ADDED);
4348 }
4349 
4350 
4351 DEFUN (no_match_local_pref,
4352        no_match_local_pref_cmd,
4353        "no match local-preference [(0-4294967295)]",
4354        NO_STR
4355        MATCH_STR
4356        "Match local preference of route\n"
4357        "Local preference value\n")
4358 {
4359 	int idx_localpref = 3;
4360 	if (argc <= idx_localpref)
4361 		return bgp_route_match_delete(vty, "local-preference", NULL,
4362 					      RMAP_EVENT_MATCH_DELETED);
4363 	return bgp_route_match_delete(vty, "local-preference",
4364 				      argv[idx_localpref]->arg,
4365 				      RMAP_EVENT_MATCH_DELETED);
4366 }
4367 
4368 
4369 DEFUN (match_community,
4370        match_community_cmd,
4371        "match community <(1-99)|(100-500)|WORD> [exact-match]",
4372        MATCH_STR
4373        "Match BGP community list\n"
4374        "Community-list number (standard)\n"
4375        "Community-list number (expanded)\n"
4376        "Community-list name\n"
4377        "Do exact matching of communities\n")
4378 {
4379 	int idx_comm_list = 2;
4380 	int ret;
4381 	char *argstr;
4382 
4383 	if (argc == 4) {
4384 		argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
4385 				 strlen(argv[idx_comm_list]->arg)
4386 					 + strlen("exact-match") + 2);
4387 
4388 		sprintf(argstr, "%s exact-match", argv[idx_comm_list]->arg);
4389 	} else
4390 		argstr = argv[idx_comm_list]->arg;
4391 
4392 	ret = bgp_route_match_add(vty, "community", argstr,
4393 				  RMAP_EVENT_CLIST_ADDED);
4394 
4395 	if (argstr != argv[idx_comm_list]->arg)
4396 		XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
4397 
4398 	return ret;
4399 }
4400 
4401 DEFUN (no_match_community,
4402        no_match_community_cmd,
4403        "no match community [<(1-99)|(100-500)|WORD> [exact-match]]",
4404        NO_STR
4405        MATCH_STR
4406        "Match BGP community list\n"
4407        "Community-list number (standard)\n"
4408        "Community-list number (expanded)\n"
4409        "Community-list name\n"
4410        "Do exact matching of communities\n")
4411 {
4412 	return bgp_route_match_delete(vty, "community", NULL,
4413 				      RMAP_EVENT_CLIST_DELETED);
4414 }
4415 
4416 DEFUN (match_lcommunity,
4417        match_lcommunity_cmd,
4418        "match large-community <(1-99)|(100-500)|WORD> [exact-match]",
4419        MATCH_STR
4420        "Match BGP large community list\n"
4421        "Large Community-list number (standard)\n"
4422        "Large Community-list number (expanded)\n"
4423        "Large Community-list name\n"
4424        "Do exact matching of communities\n")
4425 {
4426 	int idx_lcomm_list = 2;
4427 	int ret;
4428 	char *argstr;
4429 
4430 	if (argc == 4) {
4431 		argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
4432 				strlen(argv[idx_lcomm_list]->arg)
4433 				+ strlen("exact-match") + 2);
4434 
4435 		sprintf(argstr, "%s exact-match", argv[idx_lcomm_list]->arg);
4436 	} else
4437 		argstr = argv[idx_lcomm_list]->arg;
4438 
4439 	ret = bgp_route_match_add(vty, "large-community", argstr,
4440 				   RMAP_EVENT_LLIST_ADDED);
4441 	if (argstr != argv[idx_lcomm_list]->arg)
4442 		XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
4443 
4444 	return ret;
4445 }
4446 
4447 DEFUN (no_match_lcommunity,
4448        no_match_lcommunity_cmd,
4449        "no match large-community [<(1-99)|(100-500)|WORD> [exact-match]]",
4450        NO_STR
4451        MATCH_STR
4452        "Match BGP large community list\n"
4453        "Large Community-list number (standard)\n"
4454        "Large Community-list number (expanded)\n"
4455        "Large Community-list name\n"
4456        "Do exact matching of communities\n")
4457 {
4458 	return bgp_route_match_delete(vty, "large-community", NULL,
4459 				      RMAP_EVENT_LLIST_DELETED);
4460 }
4461 
4462 DEFUN (match_ecommunity,
4463        match_ecommunity_cmd,
4464        "match extcommunity <(1-99)|(100-500)|WORD>",
4465        MATCH_STR
4466        "Match BGP/VPN extended community list\n"
4467        "Extended community-list number (standard)\n"
4468        "Extended community-list number (expanded)\n"
4469        "Extended community-list name\n")
4470 {
4471 	int idx_comm_list = 2;
4472 	return bgp_route_match_add(vty, "extcommunity",
4473 				   argv[idx_comm_list]->arg,
4474 				   RMAP_EVENT_ECLIST_ADDED);
4475 }
4476 
4477 
4478 DEFUN (no_match_ecommunity,
4479        no_match_ecommunity_cmd,
4480        "no match extcommunity [<(1-99)|(100-500)|WORD>]",
4481        NO_STR
4482        MATCH_STR
4483        "Match BGP/VPN extended community list\n"
4484        "Extended community-list number (standard)\n"
4485        "Extended community-list number (expanded)\n"
4486        "Extended community-list name\n")
4487 {
4488 	return bgp_route_match_delete(vty, "extcommunity", NULL,
4489 				      RMAP_EVENT_ECLIST_DELETED);
4490 }
4491 
4492 
4493 DEFUN (match_aspath,
4494        match_aspath_cmd,
4495        "match as-path WORD",
4496        MATCH_STR
4497        "Match BGP AS path list\n"
4498        "AS path access-list name\n")
4499 {
4500 	int idx_word = 2;
4501 	return bgp_route_match_add(vty, "as-path", argv[idx_word]->arg,
4502 				   RMAP_EVENT_ASLIST_ADDED);
4503 }
4504 
4505 
4506 DEFUN (no_match_aspath,
4507        no_match_aspath_cmd,
4508        "no match as-path [WORD]",
4509        NO_STR
4510        MATCH_STR
4511        "Match BGP AS path list\n"
4512        "AS path access-list name\n")
4513 {
4514 	return bgp_route_match_delete(vty, "as-path", NULL,
4515 				      RMAP_EVENT_ASLIST_DELETED);
4516 }
4517 
4518 
4519 DEFUN (match_origin,
4520        match_origin_cmd,
4521        "match origin <egp|igp|incomplete>",
4522        MATCH_STR
4523        "BGP origin code\n"
4524        "remote EGP\n"
4525        "local IGP\n"
4526        "unknown heritage\n")
4527 {
4528 	int idx_origin = 2;
4529 	if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
4530 		return bgp_route_match_add(vty, "origin", "igp",
4531 					   RMAP_EVENT_MATCH_ADDED);
4532 	if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
4533 		return bgp_route_match_add(vty, "origin", "egp",
4534 					   RMAP_EVENT_MATCH_ADDED);
4535 	if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
4536 		return bgp_route_match_add(vty, "origin", "incomplete",
4537 					   RMAP_EVENT_MATCH_ADDED);
4538 
4539 	vty_out(vty, "%% Invalid match origin type\n");
4540 	return CMD_WARNING_CONFIG_FAILED;
4541 }
4542 
4543 
4544 DEFUN (no_match_origin,
4545        no_match_origin_cmd,
4546        "no match origin [<egp|igp|incomplete>]",
4547        NO_STR
4548        MATCH_STR
4549        "BGP origin code\n"
4550        "remote EGP\n"
4551        "local IGP\n"
4552        "unknown heritage\n")
4553 {
4554 	return bgp_route_match_delete(vty, "origin", NULL,
4555 				      RMAP_EVENT_MATCH_DELETED);
4556 }
4557 
4558 DEFUN (set_table_id,
4559 	set_table_id_cmd,
4560 	"set table (1-4294967295)",
4561 	SET_STR
4562 	"export route to non-main kernel table\n"
4563 	"Kernel routing table id\n")
4564 {
4565 	int idx_id = 2;
4566 
4567 	VTY_DECLVAR_CONTEXT(route_map_index, index);
4568 
4569 	return generic_set_add(vty, index, "table", argv[idx_id]->arg);
4570 }
4571 
4572 DEFUN (no_set_table_id,
4573 	no_set_table_id_cmd,
4574 	"no set table",
4575 	NO_STR
4576 	SET_STR
4577        "export route to non-main kernel table\n")
4578 {
4579 	VTY_DECLVAR_CONTEXT(route_map_index, index);
4580 
4581 	return generic_set_delete(vty, index, "table", NULL);
4582 }
4583 
4584 DEFUN (set_ip_nexthop_peer,
4585        set_ip_nexthop_peer_cmd,
4586        "[no] set ip next-hop peer-address",
4587        NO_STR
4588        SET_STR
4589        IP_STR
4590        "Next hop address\n"
4591        "Use peer address (for BGP only)\n")
4592 {
4593 	int (*func)(struct vty *, struct route_map_index *, const char *,
4594 		    const char *) = strmatch(argv[0]->text, "no")
4595 					    ? generic_set_delete
4596 					    : generic_set_add;
4597 
4598 	return func(vty, VTY_GET_CONTEXT(route_map_index), "ip next-hop",
4599 		    "peer-address");
4600 }
4601 
4602 DEFUN (set_ip_nexthop_unchanged,
4603        set_ip_nexthop_unchanged_cmd,
4604        "[no] set ip next-hop unchanged",
4605        NO_STR
4606        SET_STR
4607        IP_STR
4608        "Next hop address\n"
4609        "Don't modify existing Next hop address\n")
4610 {
4611 	int (*func)(struct vty *, struct route_map_index *, const char *,
4612 		    const char *) = strmatch(argv[0]->text, "no")
4613 					    ? generic_set_delete
4614 					    : generic_set_add;
4615 
4616 	return func(vty, VTY_GET_CONTEXT(route_map_index), "ip next-hop",
4617 		    "unchanged");
4618 }
4619 
4620 DEFUN (set_distance,
4621        set_distance_cmd,
4622        "set distance (0-255)",
4623        SET_STR
4624        "BGP Administrative Distance to use\n"
4625        "Distance value\n")
4626 {
4627 	int idx_number = 2;
4628 
4629 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4630 			       "distance", argv[idx_number]->arg);
4631 }
4632 
4633 DEFUN (no_set_distance,
4634        no_set_distance_cmd,
4635        "no set distance [(0-255)]",
4636        NO_STR SET_STR
4637        "BGP Administrative Distance to use\n"
4638        "Distance value\n")
4639 {
4640 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4641 				  "distance", NULL);
4642 }
4643 
4644 DEFUN (set_local_pref,
4645        set_local_pref_cmd,
4646        "set local-preference WORD",
4647        SET_STR
4648        "BGP local preference path attribute\n"
4649        "Preference value (0-4294967295)\n")
4650 {
4651 	int idx_number = 2;
4652 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4653 			       "local-preference", argv[idx_number]->arg);
4654 }
4655 
4656 
4657 DEFUN (no_set_local_pref,
4658        no_set_local_pref_cmd,
4659        "no set local-preference [WORD]",
4660        NO_STR
4661        SET_STR
4662        "BGP local preference path attribute\n"
4663        "Preference value (0-4294967295)\n")
4664 {
4665 	int idx_localpref = 3;
4666 	if (argc <= idx_localpref)
4667 		return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4668 					  "local-preference", NULL);
4669 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4670 				  "local-preference", argv[idx_localpref]->arg);
4671 }
4672 
4673 
4674 DEFUN (set_weight,
4675        set_weight_cmd,
4676        "set weight (0-4294967295)",
4677        SET_STR
4678        "BGP weight for routing table\n"
4679        "Weight value\n")
4680 {
4681 	int idx_number = 2;
4682 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index), "weight",
4683 			       argv[idx_number]->arg);
4684 }
4685 
4686 
4687 DEFUN (no_set_weight,
4688        no_set_weight_cmd,
4689        "no set weight [(0-4294967295)]",
4690        NO_STR
4691        SET_STR
4692        "BGP weight for routing table\n"
4693        "Weight value\n")
4694 {
4695 	int idx_weight = 3;
4696 	if (argc <= idx_weight)
4697 		return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4698 					  "weight", NULL);
4699 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4700 				  "weight", argv[idx_weight]->arg);
4701 }
4702 
4703 DEFUN (set_label_index,
4704        set_label_index_cmd,
4705        "set label-index (0-1048560)",
4706        SET_STR
4707        "Label index to associate with the prefix\n"
4708        "Label index value\n")
4709 {
4710 	int idx_number = 2;
4711 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4712 			       "label-index", argv[idx_number]->arg);
4713 }
4714 
4715 DEFUN (no_set_label_index,
4716        no_set_label_index_cmd,
4717        "no set label-index [(0-1048560)]",
4718        NO_STR
4719        SET_STR
4720        "Label index to associate with the prefix\n"
4721        "Label index value\n")
4722 {
4723 	int idx_label_index = 3;
4724 	if (argc <= idx_label_index)
4725 		return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4726 					  "label-index", NULL);
4727 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4728 				  "label-index", argv[idx_label_index]->arg);
4729 }
4730 
4731 DEFUN (set_aspath_prepend_asn,
4732        set_aspath_prepend_asn_cmd,
4733        "set as-path prepend (1-4294967295)...",
4734        SET_STR
4735        "Transform BGP AS_PATH attribute\n"
4736        "Prepend to the as-path\n"
4737        "AS number\n")
4738 {
4739 	int idx_asn = 3;
4740 	int ret;
4741 	char *str;
4742 
4743 	str = argv_concat(argv, argc, idx_asn);
4744 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4745 			      "as-path prepend", str);
4746 	XFREE(MTYPE_TMP, str);
4747 
4748 	return ret;
4749 }
4750 
4751 DEFUN (set_aspath_prepend_lastas,
4752        set_aspath_prepend_lastas_cmd,
4753        "set as-path prepend last-as (1-10)",
4754        SET_STR
4755        "Transform BGP AS_PATH attribute\n"
4756        "Prepend to the as-path\n"
4757        "Use the peer's AS-number\n"
4758        "Number of times to insert\n")
4759 {
4760 	return set_aspath_prepend_asn(self, vty, argc, argv);
4761 }
4762 
4763 DEFUN (no_set_aspath_prepend,
4764        no_set_aspath_prepend_cmd,
4765        "no set as-path prepend [(1-4294967295)]",
4766        NO_STR
4767        SET_STR
4768        "Transform BGP AS_PATH attribute\n"
4769        "Prepend to the as-path\n"
4770        "AS number\n")
4771 {
4772 	int idx_asn = 4;
4773 	int ret;
4774 	char *str;
4775 
4776 	str = argv_concat(argv, argc, idx_asn);
4777 	ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4778 				 "as-path prepend", str);
4779 	XFREE(MTYPE_TMP, str);
4780 	return ret;
4781 }
4782 
4783 DEFUN (no_set_aspath_prepend_lastas,
4784        no_set_aspath_prepend_lastas_cmd,
4785        "no set as-path prepend last-as [(1-10)]",
4786        NO_STR
4787        SET_STR
4788        "Transform BGP AS_PATH attribute\n"
4789        "Prepend to the as-path\n"
4790        "Use the peers AS-number\n"
4791        "Number of times to insert\n")
4792 {
4793 	return no_set_aspath_prepend(self, vty, argc, argv);
4794 }
4795 
4796 DEFUN (set_aspath_exclude,
4797        set_aspath_exclude_cmd,
4798        "set as-path exclude (1-4294967295)...",
4799        SET_STR
4800        "Transform BGP AS-path attribute\n"
4801        "Exclude from the as-path\n"
4802        "AS number\n")
4803 {
4804 	int idx_asn = 3;
4805 	int ret;
4806 	char *str;
4807 
4808 	str = argv_concat(argv, argc, idx_asn);
4809 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4810 			      "as-path exclude", str);
4811 	XFREE(MTYPE_TMP, str);
4812 	return ret;
4813 }
4814 
4815 DEFUN (no_set_aspath_exclude,
4816        no_set_aspath_exclude_cmd,
4817        "no set as-path exclude (1-4294967295)...",
4818        NO_STR
4819        SET_STR
4820        "Transform BGP AS_PATH attribute\n"
4821        "Exclude from the as-path\n"
4822        "AS number\n")
4823 {
4824 	int idx_asn = 4;
4825 	int ret;
4826 	char *str;
4827 
4828 	str = argv_concat(argv, argc, idx_asn);
4829 	ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4830 				 "as-path exclude", str);
4831 	XFREE(MTYPE_TMP, str);
4832 	return ret;
4833 }
4834 
4835 ALIAS(no_set_aspath_exclude, no_set_aspath_exclude_all_cmd,
4836       "no set as-path exclude",
4837       NO_STR SET_STR
4838       "Transform BGP AS_PATH attribute\n"
4839       "Exclude from the as-path\n")
4840 
4841 DEFUN (set_community,
4842        set_community_cmd,
4843        "set community AA:NN...",
4844        SET_STR
4845        "BGP community attribute\n"
4846        COMMUNITY_VAL_STR)
4847 {
4848 	int idx_aa_nn = 2;
4849 	int i;
4850 	int first = 0;
4851 	int additive = 0;
4852 	struct buffer *b;
4853 	struct community *com = NULL;
4854 	char *str;
4855 	char *argstr;
4856 	int ret;
4857 
4858 	b = buffer_new(1024);
4859 
4860 	for (i = idx_aa_nn; i < argc; i++) {
4861 		if (strncmp(argv[i]->arg, "additive", strlen(argv[i]->arg))
4862 		    == 0) {
4863 			additive = 1;
4864 			continue;
4865 		}
4866 
4867 		if (first)
4868 			buffer_putc(b, ' ');
4869 		else
4870 			first = 1;
4871 
4872 		if (strncmp(argv[i]->arg, "internet", strlen(argv[i]->arg))
4873 		    == 0) {
4874 			buffer_putstr(b, "internet");
4875 			continue;
4876 		}
4877 		if (strncmp(argv[i]->arg, "local-AS", strlen(argv[i]->arg))
4878 		    == 0) {
4879 			buffer_putstr(b, "local-AS");
4880 			continue;
4881 		}
4882 		if (strncmp(argv[i]->arg, "no-a", strlen("no-a")) == 0
4883 		    && strncmp(argv[i]->arg, "no-advertise",
4884 			       strlen(argv[i]->arg))
4885 			       == 0) {
4886 			buffer_putstr(b, "no-advertise");
4887 			continue;
4888 		}
4889 		if (strncmp(argv[i]->arg, "no-e", strlen("no-e")) == 0
4890 		    && strncmp(argv[i]->arg, "no-export", strlen(argv[i]->arg))
4891 			       == 0) {
4892 			buffer_putstr(b, "no-export");
4893 			continue;
4894 		}
4895 		if (strncmp(argv[i]->arg, "graceful-shutdown",
4896 			    strlen(argv[i]->arg))
4897 		    == 0) {
4898 			buffer_putstr(b, "graceful-shutdown");
4899 			continue;
4900 		}
4901 		buffer_putstr(b, argv[i]->arg);
4902 	}
4903 	buffer_putc(b, '\0');
4904 
4905 	/* Fetch result string then compile it to communities attribute.  */
4906 	str = buffer_getstr(b);
4907 	buffer_free(b);
4908 
4909 	if (str) {
4910 		com = community_str2com(str);
4911 		XFREE(MTYPE_TMP, str);
4912 	}
4913 
4914 	/* Can't compile user input into communities attribute.  */
4915 	if (!com) {
4916 		vty_out(vty, "%% Malformed communities attribute\n");
4917 		return CMD_WARNING_CONFIG_FAILED;
4918 	}
4919 
4920 	/* Set communites attribute string.  */
4921 	str = community_str(com, false);
4922 
4923 	if (additive) {
4924 		size_t argstr_sz = strlen(str) + strlen(" additive") + 1;
4925 		argstr = XCALLOC(MTYPE_TMP, argstr_sz);
4926 		strlcpy(argstr, str, argstr_sz);
4927 		strlcat(argstr, " additive", argstr_sz);
4928 		ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4929 				      "community", argstr);
4930 		XFREE(MTYPE_TMP, argstr);
4931 	} else
4932 		ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4933 				      "community", str);
4934 
4935 	community_free(&com);
4936 
4937 	return ret;
4938 }
4939 
4940 DEFUN (set_community_none,
4941        set_community_none_cmd,
4942        "set community none",
4943        SET_STR
4944        "BGP community attribute\n"
4945        "No community attribute\n")
4946 {
4947 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4948 			       "community", "none");
4949 }
4950 
4951 DEFUN (no_set_community,
4952        no_set_community_cmd,
4953        "no set community AA:NN...",
4954        NO_STR
4955        SET_STR
4956        "BGP community attribute\n"
4957        COMMUNITY_VAL_STR)
4958 {
4959 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4960 				  "community", NULL);
4961 }
4962 
4963 ALIAS (no_set_community,
4964        no_set_community_short_cmd,
4965        "no set community",
4966        NO_STR
4967        SET_STR
4968        "BGP community attribute\n")
4969 
4970 
4971 DEFUN (set_community_delete,
4972        set_community_delete_cmd,
4973        "set comm-list <(1-99)|(100-500)|WORD> delete",
4974        SET_STR
4975        "set BGP community list (for deletion)\n"
4976        "Community-list number (standard)\n"
4977        "Community-list number (expanded)\n"
4978        "Community-list name\n"
4979        "Delete matching communities\n")
4980 {
4981 	int idx_comm_list = 2;
4982 	char *args;
4983 
4984 	args = argv_concat(argv, argc, idx_comm_list);
4985 	generic_set_add(vty, VTY_GET_CONTEXT(route_map_index), "comm-list",
4986 			args);
4987 	XFREE(MTYPE_TMP, args);
4988 
4989 	return CMD_SUCCESS;
4990 }
4991 
4992 DEFUN (no_set_community_delete,
4993        no_set_community_delete_cmd,
4994        "no set comm-list [<(1-99)|(100-500)|WORD> delete]",
4995        NO_STR
4996        SET_STR
4997        "set BGP community list (for deletion)\n"
4998        "Community-list number (standard)\n"
4999        "Community-list number (expanded)\n"
5000        "Community-list name\n"
5001        "Delete matching communities\n")
5002 {
5003 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5004 				  "comm-list", NULL);
5005 }
5006 
5007 DEFUN (set_lcommunity,
5008        set_lcommunity_cmd,
5009        "set large-community AA:BB:CC...",
5010        SET_STR
5011        "BGP large community attribute\n"
5012        "Large Community number in aa:bb:cc format or additive\n")
5013 {
5014 	int ret;
5015 	char *str;
5016 
5017 	str = argv_concat(argv, argc, 2);
5018 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5019 			      "large-community", str);
5020 	XFREE(MTYPE_TMP, str);
5021 
5022 	return ret;
5023 }
5024 
5025 DEFUN (set_lcommunity_none,
5026        set_lcommunity_none_cmd,
5027        "set large-community none",
5028        SET_STR
5029        "BGP large community attribute\n"
5030        "No large community attribute\n")
5031 {
5032 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5033 			       "large-community", "none");
5034 }
5035 
5036 DEFUN (no_set_lcommunity,
5037        no_set_lcommunity_cmd,
5038        "no set large-community none",
5039        NO_STR
5040        SET_STR
5041        "BGP large community attribute\n"
5042        "No community attribute\n")
5043 {
5044 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5045 				  "large-community", NULL);
5046 }
5047 
5048 DEFUN (no_set_lcommunity1,
5049        no_set_lcommunity1_cmd,
5050        "no set large-community AA:BB:CC...",
5051        NO_STR
5052        SET_STR
5053        "BGP large community attribute\n"
5054        "Large community in AA:BB:CC... format or additive\n")
5055 {
5056 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5057 				  "large-community", NULL);
5058 }
5059 
5060 ALIAS (no_set_lcommunity1,
5061        no_set_lcommunity1_short_cmd,
5062        "no set large-community",
5063        NO_STR
5064        SET_STR
5065        "BGP large community attribute\n")
5066 
5067 DEFUN (set_lcommunity_delete,
5068        set_lcommunity_delete_cmd,
5069        "set large-comm-list <(1-99)|(100-500)|WORD> delete",
5070        SET_STR
5071        "set BGP large community list (for deletion)\n"
5072        "Large Community-list number (standard)\n"
5073        "Large Communitly-list number (expanded)\n"
5074        "Large Community-list name\n"
5075        "Delete matching large communities\n")
5076 {
5077 	int idx_lcomm_list = 2;
5078 	char *args;
5079 
5080 	args = argv_concat(argv, argc, idx_lcomm_list);
5081 	generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5082 			"large-comm-list", args);
5083 	XFREE(MTYPE_TMP, args);
5084 
5085 	return CMD_SUCCESS;
5086 }
5087 
5088 DEFUN (no_set_lcommunity_delete,
5089        no_set_lcommunity_delete_cmd,
5090        "no set large-comm-list <(1-99)|(100-500)|WORD> [delete]",
5091        NO_STR
5092        SET_STR
5093        "set BGP large community list (for deletion)\n"
5094        "Large Community-list number (standard)\n"
5095        "Large Communitly-list number (expanded)\n"
5096        "Large Community-list name\n"
5097        "Delete matching large communities\n")
5098 {
5099 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5100 				  "large-comm-list", NULL);
5101 }
5102 
5103 ALIAS (no_set_lcommunity_delete,
5104        no_set_lcommunity_delete_short_cmd,
5105        "no set large-comm-list",
5106        NO_STR
5107        SET_STR
5108        "set BGP large community list (for deletion)\n")
5109 
5110 DEFUN (set_ecommunity_rt,
5111        set_ecommunity_rt_cmd,
5112        "set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
5113        SET_STR
5114        "BGP extended community attribute\n"
5115        "Route Target extended community\n"
5116        "VPN extended community\n")
5117 {
5118 	int idx_asn_nn = 3;
5119 	int ret;
5120 	char *str;
5121 
5122 	str = argv_concat(argv, argc, idx_asn_nn);
5123 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5124 			      "extcommunity rt", str);
5125 	XFREE(MTYPE_TMP, str);
5126 
5127 	return ret;
5128 }
5129 
5130 DEFUN (no_set_ecommunity_rt,
5131        no_set_ecommunity_rt_cmd,
5132        "no set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
5133        NO_STR
5134        SET_STR
5135        "BGP extended community attribute\n"
5136        "Route Target extended community\n"
5137        "VPN extended community\n")
5138 {
5139 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5140 				  "extcommunity rt", NULL);
5141 }
5142 
5143 ALIAS (no_set_ecommunity_rt,
5144        no_set_ecommunity_rt_short_cmd,
5145        "no set extcommunity rt",
5146        NO_STR
5147        SET_STR
5148        "BGP extended community attribute\n"
5149        "Route Target extended community\n")
5150 
5151 DEFUN (set_ecommunity_soo,
5152        set_ecommunity_soo_cmd,
5153        "set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
5154        SET_STR
5155        "BGP extended community attribute\n"
5156        "Site-of-Origin extended community\n"
5157        "VPN extended community\n")
5158 {
5159 	int idx_asn_nn = 3;
5160 	int ret;
5161 	char *str;
5162 
5163 	str = argv_concat(argv, argc, idx_asn_nn);
5164 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5165 			      "extcommunity soo", str);
5166 	XFREE(MTYPE_TMP, str);
5167 	return ret;
5168 }
5169 
5170 
5171 DEFUN (no_set_ecommunity_soo,
5172        no_set_ecommunity_soo_cmd,
5173        "no set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
5174        NO_STR
5175        SET_STR
5176        "BGP extended community attribute\n"
5177        "Site-of-Origin extended community\n"
5178        "VPN extended community\n")
5179 {
5180 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5181 				  "extcommunity soo", NULL);
5182 }
5183 
5184 ALIAS (no_set_ecommunity_soo,
5185        no_set_ecommunity_soo_short_cmd,
5186        "no set extcommunity soo",
5187        NO_STR
5188        SET_STR
5189        "GP extended community attribute\n"
5190        "Site-of-Origin extended community\n")
5191 
5192 DEFUN (set_ecommunity_lb,
5193        set_ecommunity_lb_cmd,
5194        "set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
5195        SET_STR
5196        "BGP extended community attribute\n"
5197        "Link bandwidth extended community\n"
5198        "Bandwidth value in Mbps\n"
5199        "Cumulative bandwidth of all multipaths (outbound-only)\n"
5200        "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
5201        "Attribute is set as non-transitive\n")
5202 {
5203 	int idx_lb = 3;
5204 	int ret;
5205 	char *str;
5206 
5207 	str = argv_concat(argv, argc, idx_lb);
5208 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5209 			      "extcommunity bandwidth", str);
5210 	XFREE(MTYPE_TMP, str);
5211 	return ret;
5212 }
5213 
5214 
5215 DEFUN (no_set_ecommunity_lb,
5216        no_set_ecommunity_lb_cmd,
5217        "no set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
5218        NO_STR
5219        SET_STR
5220        "BGP extended community attribute\n"
5221        "Link bandwidth extended community\n"
5222        "Bandwidth value in Mbps\n"
5223        "Cumulative bandwidth of all multipaths (outbound-only)\n"
5224        "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
5225        "Attribute is set as non-transitive\n")
5226 {
5227 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5228 				  "extcommunity bandwidth", NULL);
5229 }
5230 
5231 ALIAS (no_set_ecommunity_lb,
5232        no_set_ecommunity_lb_short_cmd,
5233        "no set extcommunity bandwidth",
5234        NO_STR
5235        SET_STR
5236        "BGP extended community attribute\n"
5237        "Link bandwidth extended community\n")
5238 
5239 DEFUN (set_origin,
5240        set_origin_cmd,
5241        "set origin <egp|igp|incomplete>",
5242        SET_STR
5243        "BGP origin code\n"
5244        "remote EGP\n"
5245        "local IGP\n"
5246        "unknown heritage\n")
5247 {
5248 	int idx_origin = 2;
5249 	if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
5250 		return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5251 				       "origin", "igp");
5252 	if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
5253 		return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5254 				       "origin", "egp");
5255 	if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
5256 		return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5257 				       "origin", "incomplete");
5258 
5259 	vty_out(vty, "%% Invalid set origin type\n");
5260 	return CMD_WARNING_CONFIG_FAILED;
5261 }
5262 
5263 
5264 DEFUN (no_set_origin,
5265        no_set_origin_cmd,
5266        "no set origin [<egp|igp|incomplete>]",
5267        NO_STR
5268        SET_STR
5269        "BGP origin code\n"
5270        "remote EGP\n"
5271        "local IGP\n"
5272        "unknown heritage\n")
5273 {
5274 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5275 				  "origin", NULL);
5276 }
5277 
5278 
5279 DEFUN (set_atomic_aggregate,
5280        set_atomic_aggregate_cmd,
5281        "set atomic-aggregate",
5282        SET_STR
5283        "BGP atomic aggregate attribute\n" )
5284 {
5285 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5286 			       "atomic-aggregate", NULL);
5287 }
5288 
5289 DEFUN (no_set_atomic_aggregate,
5290        no_set_atomic_aggregate_cmd,
5291        "no set atomic-aggregate",
5292        NO_STR
5293        SET_STR
5294        "BGP atomic aggregate attribute\n" )
5295 {
5296 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5297 				  "atomic-aggregate", NULL);
5298 }
5299 
5300 DEFUN (set_aggregator_as,
5301        set_aggregator_as_cmd,
5302        "set aggregator as (1-4294967295) A.B.C.D",
5303        SET_STR
5304        "BGP aggregator attribute\n"
5305        "AS number of aggregator\n"
5306        "AS number\n"
5307        "IP address of aggregator\n")
5308 {
5309 	int idx_number = 3;
5310 	int idx_ipv4 = 4;
5311 	int ret;
5312 	struct in_addr address;
5313 	char *argstr;
5314 
5315 	ret = inet_aton(argv[idx_ipv4]->arg, &address);
5316 	if (ret == 0) {
5317 		vty_out(vty, "Aggregator IP address is invalid\n");
5318 		return CMD_WARNING_CONFIG_FAILED;
5319 	}
5320 
5321 	argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
5322 			 strlen(argv[idx_number]->arg)
5323 				 + strlen(argv[idx_ipv4]->arg) + 2);
5324 
5325 	sprintf(argstr, "%s %s", argv[idx_number]->arg, argv[idx_ipv4]->arg);
5326 
5327 	ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5328 			      "aggregator as", argstr);
5329 
5330 	XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
5331 
5332 	return ret;
5333 }
5334 
5335 
5336 DEFUN (no_set_aggregator_as,
5337        no_set_aggregator_as_cmd,
5338        "no set aggregator as [(1-4294967295) A.B.C.D]",
5339        NO_STR
5340        SET_STR
5341        "BGP aggregator attribute\n"
5342        "AS number of aggregator\n"
5343        "AS number\n"
5344        "IP address of aggregator\n")
5345 {
5346 	int idx_asn = 4;
5347 	int idx_ip = 5;
5348 	int ret;
5349 	struct in_addr address;
5350 	char *argstr;
5351 
5352 	if (argc <= idx_asn)
5353 		return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5354 					  "aggregator as", NULL);
5355 
5356 	ret = inet_aton(argv[idx_ip]->arg, &address);
5357 	if (ret == 0) {
5358 		vty_out(vty, "Aggregator IP address is invalid\n");
5359 		return CMD_WARNING_CONFIG_FAILED;
5360 	}
5361 
5362 	argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
5363 			 strlen(argv[idx_asn]->arg) + strlen(argv[idx_ip]->arg)
5364 				 + 2);
5365 
5366 	sprintf(argstr, "%s %s", argv[idx_asn]->arg, argv[idx_ip]->arg);
5367 
5368 	ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5369 				 "aggregator as", argstr);
5370 
5371 	XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
5372 
5373 	return ret;
5374 }
5375 
5376 DEFUN (match_ipv6_next_hop,
5377        match_ipv6_next_hop_cmd,
5378        "match ipv6 next-hop X:X::X:X",
5379        MATCH_STR
5380        IPV6_STR
5381        "Match IPv6 next-hop address of route\n"
5382        "IPv6 address of next hop\n")
5383 {
5384 	int idx_ipv6 = 3;
5385 	return bgp_route_match_add(vty, "ipv6 next-hop", argv[idx_ipv6]->arg,
5386 				   RMAP_EVENT_MATCH_ADDED);
5387 }
5388 
5389 DEFUN (no_match_ipv6_next_hop,
5390        no_match_ipv6_next_hop_cmd,
5391        "no match ipv6 next-hop X:X::X:X",
5392        NO_STR
5393        MATCH_STR
5394        IPV6_STR
5395        "Match IPv6 next-hop address of route\n"
5396        "IPv6 address of next hop\n")
5397 {
5398 	int idx_ipv6 = 4;
5399 	return bgp_route_match_delete(vty, "ipv6 next-hop", argv[idx_ipv6]->arg,
5400 				      RMAP_EVENT_MATCH_DELETED);
5401 }
5402 
5403 DEFPY (match_ipv4_next_hop,
5404        match_ipv4_next_hop_cmd,
5405        "match ip next-hop address A.B.C.D",
5406        MATCH_STR
5407        IP_STR
5408        "Match IP next-hop address of route\n"
5409        "IP address\n"
5410        "IP address of next-hop\n")
5411 {
5412 	int idx_ipv4 = 4;
5413 
5414 	return bgp_route_match_add(vty, "ip next-hop address",
5415 				   argv[idx_ipv4]->arg, RMAP_EVENT_MATCH_ADDED);
5416 }
5417 
5418 DEFPY (no_match_ipv4_next_hop,
5419        no_match_ipv4_next_hop_cmd,
5420        "no match ip next-hop address [A.B.C.D]",
5421        NO_STR
5422        MATCH_STR
5423        IP_STR
5424        "Match IP next-hop address of route\n"
5425        "IP address\n"
5426        "IP address of next-hop\n")
5427 {
5428 	return bgp_route_match_delete(vty, "ip next-hop address", NULL,
5429 				      RMAP_EVENT_MATCH_DELETED);
5430 }
5431 
5432 DEFUN (set_ipv6_nexthop_peer,
5433        set_ipv6_nexthop_peer_cmd,
5434        "set ipv6 next-hop peer-address",
5435        SET_STR
5436        IPV6_STR
5437        "Next hop address\n"
5438        "Use peer address (for BGP only)\n")
5439 {
5440 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5441 			       "ipv6 next-hop peer-address", NULL);
5442 }
5443 
5444 DEFUN (no_set_ipv6_nexthop_peer,
5445        no_set_ipv6_nexthop_peer_cmd,
5446        "no set ipv6 next-hop peer-address",
5447        NO_STR
5448        SET_STR
5449        IPV6_STR
5450        "IPv6 next-hop address\n"
5451        "Use peer address (for BGP only)\n")
5452 {
5453 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5454 				  "ipv6 next-hop peer-address", NULL);
5455 }
5456 
5457 DEFUN (set_ipv6_nexthop_prefer_global,
5458        set_ipv6_nexthop_prefer_global_cmd,
5459        "set ipv6 next-hop prefer-global",
5460        SET_STR
5461        IPV6_STR
5462        "IPv6 next-hop address\n"
5463        "Prefer global over link-local if both exist\n")
5464 {
5465 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5466 			       "ipv6 next-hop prefer-global", NULL);
5467 	;
5468 }
5469 
5470 DEFUN (no_set_ipv6_nexthop_prefer_global,
5471        no_set_ipv6_nexthop_prefer_global_cmd,
5472        "no set ipv6 next-hop prefer-global",
5473        NO_STR
5474        SET_STR
5475        IPV6_STR
5476        "IPv6 next-hop address\n"
5477        "Prefer global over link-local if both exist\n")
5478 {
5479 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5480 				  "ipv6 next-hop prefer-global", NULL);
5481 }
5482 
5483 DEFUN (set_ipv6_nexthop_global,
5484        set_ipv6_nexthop_global_cmd,
5485        "set ipv6 next-hop global X:X::X:X",
5486        SET_STR
5487        IPV6_STR
5488        "IPv6 next-hop address\n"
5489        "IPv6 global address\n"
5490        "IPv6 address of next hop\n")
5491 {
5492 	int idx_ipv6 = 4;
5493 	struct in6_addr addr;
5494 	int ret;
5495 
5496 	ret = inet_pton(AF_INET6, argv[idx_ipv6]->arg, &addr);
5497 	if (!ret) {
5498 		vty_out(vty, "%% Malformed nexthop address\n");
5499 		return CMD_WARNING_CONFIG_FAILED;
5500 	}
5501 	if (IN6_IS_ADDR_UNSPECIFIED(&addr) || IN6_IS_ADDR_LOOPBACK(&addr)
5502 	    || IN6_IS_ADDR_MULTICAST(&addr) || IN6_IS_ADDR_LINKLOCAL(&addr)) {
5503 		vty_out(vty, "%% Invalid global nexthop address\n");
5504 		return CMD_WARNING_CONFIG_FAILED;
5505 	}
5506 
5507 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5508 			       "ipv6 next-hop global", argv[idx_ipv6]->arg);
5509 }
5510 
5511 
5512 DEFUN (no_set_ipv6_nexthop_global,
5513        no_set_ipv6_nexthop_global_cmd,
5514        "no set ipv6 next-hop global X:X::X:X",
5515        NO_STR
5516        SET_STR
5517        IPV6_STR
5518        "IPv6 next-hop address\n"
5519        "IPv6 global address\n"
5520        "IPv6 address of next hop\n")
5521 {
5522 	int idx_ipv6 = 5;
5523 	if (argc <= idx_ipv6)
5524 		return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5525 					  "ipv6 next-hop global", NULL);
5526 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5527 				  "ipv6 next-hop global", argv[idx_ipv6]->arg);
5528 }
5529 
5530 #ifdef KEEP_OLD_VPN_COMMANDS
5531 DEFUN (set_vpn_nexthop,
5532        set_vpn_nexthop_cmd,
5533        "set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
5534        SET_STR
5535        "VPNv4 information\n"
5536        "VPN next-hop address\n"
5537        "IP address of next hop\n"
5538        "VPNv6 information\n"
5539        "VPN next-hop address\n"
5540        "IPv6 address of next hop\n")
5541 {
5542 	int idx_ip = 3;
5543 	afi_t afi;
5544 	int idx = 0;
5545 
5546 	if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
5547 		if (afi == AFI_IP)
5548 			return generic_set_add(
5549 				vty, VTY_GET_CONTEXT(route_map_index),
5550 				"ipv4 vpn next-hop", argv[idx_ip]->arg);
5551 		else
5552 			return generic_set_add(
5553 				vty, VTY_GET_CONTEXT(route_map_index),
5554 				"ipv6 vpn next-hop", argv[idx_ip]->arg);
5555 	}
5556 	return CMD_SUCCESS;
5557 }
5558 
5559 DEFUN (no_set_vpn_nexthop,
5560        no_set_vpn_nexthop_cmd,
5561        "no set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
5562        NO_STR
5563        SET_STR
5564        "VPNv4 information\n"
5565        "VPN next-hop address\n"
5566        "IP address of next hop\n"
5567        "VPNv6 information\n"
5568        "VPN next-hop address\n"
5569        "IPv6 address of next hop\n")
5570 {
5571 	int idx_ip = 4;
5572 	char *arg;
5573 	afi_t afi;
5574 	int idx = 0;
5575 
5576 	if (argc <= idx_ip)
5577 		arg = NULL;
5578 	else
5579 		arg = argv[idx_ip]->arg;
5580 	if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
5581 		if (afi == AFI_IP)
5582 			return generic_set_delete(
5583 				vty, VTY_GET_CONTEXT(route_map_index),
5584 				"ipv4 vpn next-hop", arg);
5585 		else
5586 			return generic_set_delete(
5587 				vty, VTY_GET_CONTEXT(route_map_index),
5588 				"ipv6 vpn next-hop", argv[idx_ip]->arg);
5589 	}
5590 	return CMD_SUCCESS;
5591 }
5592 #endif /* KEEP_OLD_VPN_COMMANDS */
5593 
5594 DEFUN (set_ipx_vpn_nexthop,
5595        set_ipx_vpn_nexthop_cmd,
5596        "set <ipv4|ipv6> vpn next-hop <A.B.C.D|X:X::X:X>",
5597        SET_STR
5598        "IPv4 information\n"
5599        "IPv6 information\n"
5600        "VPN information\n"
5601        "VPN next-hop address\n"
5602        "IP address of next hop\n"
5603        "IPv6 address of next hop\n")
5604 {
5605 	int idx_ip = 4;
5606 	afi_t afi;
5607 	int idx = 0;
5608 
5609 	if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
5610 		if (afi == AFI_IP)
5611 			return generic_set_add(
5612 				vty, VTY_GET_CONTEXT(route_map_index),
5613 				"ipv4 vpn next-hop", argv[idx_ip]->arg);
5614 		else
5615 			return generic_set_add(
5616 				vty, VTY_GET_CONTEXT(route_map_index),
5617 				"ipv6 vpn next-hop", argv[idx_ip]->arg);
5618 	}
5619 	return CMD_SUCCESS;
5620 }
5621 
5622 DEFUN (no_set_ipx_vpn_nexthop,
5623        no_set_ipx_vpn_nexthop_cmd,
5624        "no set <ipv4|ipv6> vpn next-hop [<A.B.C.D|X:X::X:X>]",
5625        NO_STR
5626        SET_STR
5627        "IPv4 information\n"
5628        "IPv6 information\n"
5629        "VPN information\n"
5630        "VPN next-hop address\n"
5631        "IP address of next hop\n"
5632        "IPv6 address of next hop\n")
5633 {
5634 	int idx_ip = 5;
5635 	char *arg;
5636 	afi_t afi;
5637 	int idx = 0;
5638 
5639 	if (argc <= idx_ip)
5640 		arg = NULL;
5641 	else
5642 		arg = argv[idx_ip]->arg;
5643 	if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
5644 		if (afi == AFI_IP)
5645 			return generic_set_delete(
5646 				vty, VTY_GET_CONTEXT(route_map_index),
5647 				"ipv4 vpn next-hop", arg);
5648 		else
5649 			return generic_set_delete(
5650 				vty, VTY_GET_CONTEXT(route_map_index),
5651 				"ipv6 vpn next-hop", arg);
5652 	}
5653 	return CMD_SUCCESS;
5654 }
5655 
5656 DEFUN (set_originator_id,
5657        set_originator_id_cmd,
5658        "set originator-id A.B.C.D",
5659        SET_STR
5660        "BGP originator ID attribute\n"
5661        "IP address of originator\n")
5662 {
5663 	int idx_ipv4 = 2;
5664 	return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5665 			       "originator-id", argv[idx_ipv4]->arg);
5666 }
5667 
5668 
5669 DEFUN (no_set_originator_id,
5670        no_set_originator_id_cmd,
5671        "no set originator-id [A.B.C.D]",
5672        NO_STR
5673        SET_STR
5674        "BGP originator ID attribute\n"
5675        "IP address of originator\n")
5676 {
5677 	int idx = 0;
5678 	char *arg =
5679 		argv_find(argv, argc, "A.B.C.D", &idx) ? argv[idx]->arg : NULL;
5680 
5681 	return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5682 				  "originator-id", arg);
5683 }
5684 
5685 
5686 /* Initialization of route map. */
bgp_route_map_init(void)5687 void bgp_route_map_init(void)
5688 {
5689 	route_map_init();
5690 
5691 	route_map_add_hook(bgp_route_map_add);
5692 	route_map_delete_hook(bgp_route_map_delete);
5693 	route_map_event_hook(bgp_route_map_event);
5694 
5695 	route_map_match_interface_hook(generic_match_add);
5696 	route_map_no_match_interface_hook(generic_match_delete);
5697 
5698 	route_map_match_ip_address_hook(generic_match_add);
5699 	route_map_no_match_ip_address_hook(generic_match_delete);
5700 
5701 	route_map_match_ip_address_prefix_list_hook(generic_match_add);
5702 	route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
5703 
5704 	route_map_match_ip_next_hop_hook(generic_match_add);
5705 	route_map_no_match_ip_next_hop_hook(generic_match_delete);
5706 
5707 	route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
5708 	route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
5709 
5710 	route_map_match_ip_next_hop_type_hook(generic_match_add);
5711 	route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
5712 
5713 	route_map_match_ipv6_address_hook(generic_match_add);
5714 	route_map_no_match_ipv6_address_hook(generic_match_delete);
5715 
5716 	route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
5717 	route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);
5718 
5719 	route_map_match_ipv6_next_hop_type_hook(generic_match_add);
5720 	route_map_no_match_ipv6_next_hop_type_hook(generic_match_delete);
5721 
5722 	route_map_match_metric_hook(generic_match_add);
5723 	route_map_no_match_metric_hook(generic_match_delete);
5724 
5725 	route_map_match_tag_hook(generic_match_add);
5726 	route_map_no_match_tag_hook(generic_match_delete);
5727 
5728 	route_map_set_srte_color_hook(generic_set_add);
5729 	route_map_no_set_srte_color_hook(generic_set_delete);
5730 
5731 	route_map_set_ip_nexthop_hook(generic_set_add);
5732 	route_map_no_set_ip_nexthop_hook(generic_set_delete);
5733 
5734 	route_map_set_ipv6_nexthop_local_hook(generic_set_add);
5735 	route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
5736 
5737 	route_map_set_metric_hook(generic_set_add);
5738 	route_map_no_set_metric_hook(generic_set_delete);
5739 
5740 	route_map_set_tag_hook(generic_set_add);
5741 	route_map_no_set_tag_hook(generic_set_delete);
5742 
5743 	route_map_install_match(&route_match_peer_cmd);
5744 	route_map_install_match(&route_match_local_pref_cmd);
5745 #if defined(HAVE_LUA)
5746 	route_map_install_match(&route_match_command_cmd);
5747 #endif
5748 	route_map_install_match(&route_match_ip_address_cmd);
5749 	route_map_install_match(&route_match_ip_next_hop_cmd);
5750 	route_map_install_match(&route_match_ip_route_source_cmd);
5751 	route_map_install_match(&route_match_ip_address_prefix_list_cmd);
5752 	route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
5753 	route_map_install_match(&route_match_ip_next_hop_type_cmd);
5754 	route_map_install_match(&route_match_ip_route_source_prefix_list_cmd);
5755 	route_map_install_match(&route_match_aspath_cmd);
5756 	route_map_install_match(&route_match_community_cmd);
5757 	route_map_install_match(&route_match_lcommunity_cmd);
5758 	route_map_install_match(&route_match_ecommunity_cmd);
5759 	route_map_install_match(&route_match_local_pref_cmd);
5760 	route_map_install_match(&route_match_metric_cmd);
5761 	route_map_install_match(&route_match_origin_cmd);
5762 	route_map_install_match(&route_match_probability_cmd);
5763 	route_map_install_match(&route_match_interface_cmd);
5764 	route_map_install_match(&route_match_tag_cmd);
5765 	route_map_install_match(&route_match_mac_address_cmd);
5766 	route_map_install_match(&route_match_evpn_vni_cmd);
5767 	route_map_install_match(&route_match_evpn_route_type_cmd);
5768 	route_map_install_match(&route_match_evpn_rd_cmd);
5769 	route_map_install_match(&route_match_evpn_default_route_cmd);
5770 	route_map_install_match(&route_match_vrl_source_vrf_cmd);
5771 
5772 	route_map_install_set(&route_set_table_id_cmd);
5773 	route_map_install_set(&route_set_srte_color_cmd);
5774 	route_map_install_set(&route_set_ip_nexthop_cmd);
5775 	route_map_install_set(&route_set_local_pref_cmd);
5776 	route_map_install_set(&route_set_weight_cmd);
5777 	route_map_install_set(&route_set_label_index_cmd);
5778 	route_map_install_set(&route_set_metric_cmd);
5779 	route_map_install_set(&route_set_distance_cmd);
5780 	route_map_install_set(&route_set_aspath_prepend_cmd);
5781 	route_map_install_set(&route_set_aspath_exclude_cmd);
5782 	route_map_install_set(&route_set_origin_cmd);
5783 	route_map_install_set(&route_set_atomic_aggregate_cmd);
5784 	route_map_install_set(&route_set_aggregator_as_cmd);
5785 	route_map_install_set(&route_set_community_cmd);
5786 	route_map_install_set(&route_set_community_delete_cmd);
5787 	route_map_install_set(&route_set_lcommunity_cmd);
5788 	route_map_install_set(&route_set_lcommunity_delete_cmd);
5789 	route_map_install_set(&route_set_vpnv4_nexthop_cmd);
5790 	route_map_install_set(&route_set_vpnv6_nexthop_cmd);
5791 	route_map_install_set(&route_set_originator_id_cmd);
5792 	route_map_install_set(&route_set_ecommunity_rt_cmd);
5793 	route_map_install_set(&route_set_ecommunity_soo_cmd);
5794 	route_map_install_set(&route_set_ecommunity_lb_cmd);
5795 	route_map_install_set(&route_set_tag_cmd);
5796 	route_map_install_set(&route_set_label_index_cmd);
5797 
5798 	install_element(RMAP_NODE, &match_peer_cmd);
5799 	install_element(RMAP_NODE, &match_peer_local_cmd);
5800 	install_element(RMAP_NODE, &no_match_peer_cmd);
5801 	install_element(RMAP_NODE, &match_ip_route_source_cmd);
5802 	install_element(RMAP_NODE, &no_match_ip_route_source_cmd);
5803 	install_element(RMAP_NODE, &match_ip_route_source_prefix_list_cmd);
5804 	install_element(RMAP_NODE, &no_match_ip_route_source_prefix_list_cmd);
5805 	install_element(RMAP_NODE, &match_mac_address_cmd);
5806 	install_element(RMAP_NODE, &no_match_mac_address_cmd);
5807 	install_element(RMAP_NODE, &match_evpn_vni_cmd);
5808 	install_element(RMAP_NODE, &no_match_evpn_vni_cmd);
5809 	install_element(RMAP_NODE, &match_evpn_route_type_cmd);
5810 	install_element(RMAP_NODE, &no_match_evpn_route_type_cmd);
5811 	install_element(RMAP_NODE, &match_evpn_rd_cmd);
5812 	install_element(RMAP_NODE, &no_match_evpn_rd_cmd);
5813 	install_element(RMAP_NODE, &match_evpn_default_route_cmd);
5814 	install_element(RMAP_NODE, &no_match_evpn_default_route_cmd);
5815 	install_element(RMAP_NODE, &match_vrl_source_vrf_cmd);
5816 	install_element(RMAP_NODE, &no_match_vrl_source_vrf_cmd);
5817 
5818 	install_element(RMAP_NODE, &match_aspath_cmd);
5819 	install_element(RMAP_NODE, &no_match_aspath_cmd);
5820 	install_element(RMAP_NODE, &match_local_pref_cmd);
5821 	install_element(RMAP_NODE, &no_match_local_pref_cmd);
5822 	install_element(RMAP_NODE, &match_community_cmd);
5823 	install_element(RMAP_NODE, &no_match_community_cmd);
5824 	install_element(RMAP_NODE, &match_lcommunity_cmd);
5825 	install_element(RMAP_NODE, &no_match_lcommunity_cmd);
5826 	install_element(RMAP_NODE, &match_ecommunity_cmd);
5827 	install_element(RMAP_NODE, &no_match_ecommunity_cmd);
5828 	install_element(RMAP_NODE, &match_origin_cmd);
5829 	install_element(RMAP_NODE, &no_match_origin_cmd);
5830 	install_element(RMAP_NODE, &match_probability_cmd);
5831 	install_element(RMAP_NODE, &no_match_probability_cmd);
5832 
5833 	install_element(RMAP_NODE, &no_set_table_id_cmd);
5834 	install_element(RMAP_NODE, &set_table_id_cmd);
5835 	install_element(RMAP_NODE, &set_ip_nexthop_peer_cmd);
5836 	install_element(RMAP_NODE, &set_ip_nexthop_unchanged_cmd);
5837 	install_element(RMAP_NODE, &set_local_pref_cmd);
5838 	install_element(RMAP_NODE, &set_distance_cmd);
5839 	install_element(RMAP_NODE, &no_set_distance_cmd);
5840 	install_element(RMAP_NODE, &no_set_local_pref_cmd);
5841 	install_element(RMAP_NODE, &set_weight_cmd);
5842 	install_element(RMAP_NODE, &set_label_index_cmd);
5843 	install_element(RMAP_NODE, &no_set_weight_cmd);
5844 	install_element(RMAP_NODE, &no_set_label_index_cmd);
5845 	install_element(RMAP_NODE, &set_aspath_prepend_asn_cmd);
5846 	install_element(RMAP_NODE, &set_aspath_prepend_lastas_cmd);
5847 	install_element(RMAP_NODE, &set_aspath_exclude_cmd);
5848 	install_element(RMAP_NODE, &no_set_aspath_prepend_cmd);
5849 	install_element(RMAP_NODE, &no_set_aspath_prepend_lastas_cmd);
5850 	install_element(RMAP_NODE, &no_set_aspath_exclude_cmd);
5851 	install_element(RMAP_NODE, &no_set_aspath_exclude_all_cmd);
5852 	install_element(RMAP_NODE, &set_origin_cmd);
5853 	install_element(RMAP_NODE, &no_set_origin_cmd);
5854 	install_element(RMAP_NODE, &set_atomic_aggregate_cmd);
5855 	install_element(RMAP_NODE, &no_set_atomic_aggregate_cmd);
5856 	install_element(RMAP_NODE, &set_aggregator_as_cmd);
5857 	install_element(RMAP_NODE, &no_set_aggregator_as_cmd);
5858 	install_element(RMAP_NODE, &set_community_cmd);
5859 	install_element(RMAP_NODE, &set_community_none_cmd);
5860 	install_element(RMAP_NODE, &no_set_community_cmd);
5861 	install_element(RMAP_NODE, &no_set_community_short_cmd);
5862 	install_element(RMAP_NODE, &set_community_delete_cmd);
5863 	install_element(RMAP_NODE, &no_set_community_delete_cmd);
5864 	install_element(RMAP_NODE, &set_lcommunity_cmd);
5865 	install_element(RMAP_NODE, &set_lcommunity_none_cmd);
5866 	install_element(RMAP_NODE, &no_set_lcommunity_cmd);
5867 	install_element(RMAP_NODE, &no_set_lcommunity1_cmd);
5868 	install_element(RMAP_NODE, &no_set_lcommunity1_short_cmd);
5869 	install_element(RMAP_NODE, &set_lcommunity_delete_cmd);
5870 	install_element(RMAP_NODE, &no_set_lcommunity_delete_cmd);
5871 	install_element(RMAP_NODE, &no_set_lcommunity_delete_short_cmd);
5872 	install_element(RMAP_NODE, &set_ecommunity_rt_cmd);
5873 	install_element(RMAP_NODE, &no_set_ecommunity_rt_cmd);
5874 	install_element(RMAP_NODE, &no_set_ecommunity_rt_short_cmd);
5875 	install_element(RMAP_NODE, &set_ecommunity_soo_cmd);
5876 	install_element(RMAP_NODE, &no_set_ecommunity_soo_cmd);
5877 	install_element(RMAP_NODE, &no_set_ecommunity_soo_short_cmd);
5878 	install_element(RMAP_NODE, &set_ecommunity_lb_cmd);
5879 	install_element(RMAP_NODE, &no_set_ecommunity_lb_cmd);
5880 	install_element(RMAP_NODE, &no_set_ecommunity_lb_short_cmd);
5881 #ifdef KEEP_OLD_VPN_COMMANDS
5882 	install_element(RMAP_NODE, &set_vpn_nexthop_cmd);
5883 	install_element(RMAP_NODE, &no_set_vpn_nexthop_cmd);
5884 #endif /* KEEP_OLD_VPN_COMMANDS */
5885 	install_element(RMAP_NODE, &set_ipx_vpn_nexthop_cmd);
5886 	install_element(RMAP_NODE, &no_set_ipx_vpn_nexthop_cmd);
5887 	install_element(RMAP_NODE, &set_originator_id_cmd);
5888 	install_element(RMAP_NODE, &no_set_originator_id_cmd);
5889 
5890 	route_map_install_match(&route_match_ipv6_address_cmd);
5891 	route_map_install_match(&route_match_ipv6_next_hop_cmd);
5892 	route_map_install_match(&route_match_ipv4_next_hop_cmd);
5893 	route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
5894 	route_map_install_match(&route_match_ipv6_next_hop_type_cmd);
5895 	route_map_install_set(&route_set_ipv6_nexthop_global_cmd);
5896 	route_map_install_set(&route_set_ipv6_nexthop_prefer_global_cmd);
5897 	route_map_install_set(&route_set_ipv6_nexthop_local_cmd);
5898 	route_map_install_set(&route_set_ipv6_nexthop_peer_cmd);
5899 
5900 	install_element(RMAP_NODE, &match_ipv6_next_hop_cmd);
5901 	install_element(RMAP_NODE, &no_match_ipv6_next_hop_cmd);
5902 	install_element(RMAP_NODE, &match_ipv4_next_hop_cmd);
5903 	install_element(RMAP_NODE, &no_match_ipv4_next_hop_cmd);
5904 	install_element(RMAP_NODE, &set_ipv6_nexthop_global_cmd);
5905 	install_element(RMAP_NODE, &no_set_ipv6_nexthop_global_cmd);
5906 	install_element(RMAP_NODE, &set_ipv6_nexthop_prefer_global_cmd);
5907 	install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd);
5908 	install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd);
5909 	install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd);
5910 #if defined(HAVE_LUA)
5911 	install_element(RMAP_NODE, &match_command_cmd);
5912 	install_element(RMAP_NODE, &no_match_command_cmd);
5913 #endif
5914 }
5915 
bgp_route_map_terminate(void)5916 void bgp_route_map_terminate(void)
5917 {
5918 	/* ToDo: Cleanup all the used memory */
5919 	route_map_finish();
5920 }
5921