2023-02-08 13:17:09 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2002-12-13 21:15:29 +01:00
|
|
|
/* route-map for interface.
|
|
|
|
* Copyright (C) 1999 Kunihiro Ishiguro
|
2023-04-09 11:02:51 +02:00
|
|
|
* Copyright (C) 2023 LabN Consulting, L.L.C.
|
2002-12-13 21:15:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zebra.h>
|
|
|
|
|
|
|
|
#include "hash.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "if.h"
|
2003-05-24 23:41:49 +02:00
|
|
|
#include "if_rmap.h"
|
2023-04-09 11:02:51 +02:00
|
|
|
#include "northbound_cli.h"
|
|
|
|
|
|
|
|
#include "lib/if_rmap_clippy.c"
|
2002-12-13 21:15:29 +01:00
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX, "Interface route map container");
|
2019-02-19 21:08:43 +01:00
|
|
|
DEFINE_MTYPE_STATIC(LIB, IF_RMAP_CTX_NAME,
|
|
|
|
"Interface route map container name");
|
2015-05-29 05:48:31 +02:00
|
|
|
DEFINE_MTYPE_STATIC(LIB, IF_RMAP, "Interface route map");
|
|
|
|
DEFINE_MTYPE_STATIC(LIB, IF_RMAP_NAME, "I.f. route map name");
|
|
|
|
|
2005-05-06 Paul Jakma <paul@dishone.st>
* (general) extern and static'ification of functions in code and
header.
Cleanup any definitions with unspecified arguments.
Add casts for callback assignments where the callback is defined,
typically, as passing void *, but the function being assigned has
some other pointer type defined as its argument, as gcc complains
about casts from void * to X* via function arguments.
Fix some old K&R style function argument definitions.
Add noreturn gcc attribute to some functions, as appropriate.
Add unused gcc attribute to some functions (eg ones meant to help
while debugging)
Add guard defines to headers which were missing them.
* command.c: (install_node) add const qualifier, still doesnt shut
up the warning though, because of the double pointer.
(cmp_node) ditto
* keychain.c: (key_str2time) Add GET_LONG_RANGE() macro, derived
fromn vty.h ones to fix some of the (long) < 0 warnings.
* thread.c: (various) use thread_empty
(cpu_record_hash_key) should cast to uintptr_t, a stdint.h type
* vty.h: Add VTY_GET_IPV4_ADDRESS and VTY_GET_IPV4_PREFIX so they
removed from ospfd/ospf_vty.h
* zebra.h: Move definition of ZEBRA_PORT to here, to remove
dependence of lib on zebra/zserv.h
2005-05-06 23:25:49 +02:00
|
|
|
static struct if_rmap *if_rmap_new(void)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
|
|
|
struct if_rmap *new;
|
|
|
|
|
|
|
|
new = XCALLOC(MTYPE_IF_RMAP, sizeof(struct if_rmap));
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void if_rmap_free(struct if_rmap *if_rmap)
|
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
char *no_const_ifname = (char *)if_rmap->ifname;
|
|
|
|
|
|
|
|
XFREE(MTYPE_IF_RMAP_NAME, no_const_ifname);
|
2002-12-13 21:15:29 +01:00
|
|
|
|
2019-02-25 21:18:13 +01:00
|
|
|
XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_IN]);
|
|
|
|
XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[IF_RMAP_OUT]);
|
2002-12-13 21:15:29 +01:00
|
|
|
|
|
|
|
XFREE(MTYPE_IF_RMAP, if_rmap);
|
|
|
|
}
|
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
struct if_rmap *if_rmap_lookup(struct if_rmap_ctx *ctx, const char *ifname)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
struct if_rmap key = {.ifname = ifname};
|
2002-12-13 21:15:29 +01:00
|
|
|
struct if_rmap *if_rmap;
|
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
if_rmap = hash_lookup(ctx->ifrmaphash, &key);
|
2015-11-22 01:47:32 +01:00
|
|
|
|
2002-12-13 21:15:29 +01:00
|
|
|
return if_rmap;
|
|
|
|
}
|
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
void if_rmap_hook_add(struct if_rmap_ctx *ctx,
|
2023-04-11 08:57:48 +02:00
|
|
|
void (*func)(struct if_rmap_ctx *ctx, struct if_rmap *))
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2019-01-14 08:58:36 +01:00
|
|
|
ctx->if_rmap_add_hook = func;
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
void if_rmap_hook_delete(struct if_rmap_ctx *ctx,
|
|
|
|
void (*func)(struct if_rmap_ctx *ctx,
|
|
|
|
struct if_rmap *))
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2019-01-14 08:58:36 +01:00
|
|
|
ctx->if_rmap_delete_hook = func;
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2005-05-06 Paul Jakma <paul@dishone.st>
* (general) extern and static'ification of functions in code and
header.
Cleanup any definitions with unspecified arguments.
Add casts for callback assignments where the callback is defined,
typically, as passing void *, but the function being assigned has
some other pointer type defined as its argument, as gcc complains
about casts from void * to X* via function arguments.
Fix some old K&R style function argument definitions.
Add noreturn gcc attribute to some functions, as appropriate.
Add unused gcc attribute to some functions (eg ones meant to help
while debugging)
Add guard defines to headers which were missing them.
* command.c: (install_node) add const qualifier, still doesnt shut
up the warning though, because of the double pointer.
(cmp_node) ditto
* keychain.c: (key_str2time) Add GET_LONG_RANGE() macro, derived
fromn vty.h ones to fix some of the (long) < 0 warnings.
* thread.c: (various) use thread_empty
(cpu_record_hash_key) should cast to uintptr_t, a stdint.h type
* vty.h: Add VTY_GET_IPV4_ADDRESS and VTY_GET_IPV4_PREFIX so they
removed from ospfd/ospf_vty.h
* zebra.h: Move definition of ZEBRA_PORT to here, to remove
dependence of lib on zebra/zserv.h
2005-05-06 23:25:49 +02:00
|
|
|
static void *if_rmap_hash_alloc(void *arg)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2015-05-20 03:04:26 +02:00
|
|
|
struct if_rmap *ifarg = (struct if_rmap *)arg;
|
2002-12-13 21:15:29 +01:00
|
|
|
struct if_rmap *if_rmap;
|
|
|
|
|
|
|
|
if_rmap = if_rmap_new();
|
2005-10-26 07:05:16 +02:00
|
|
|
if_rmap->ifname = XSTRDUP(MTYPE_IF_RMAP_NAME, ifarg->ifname);
|
2002-12-13 21:15:29 +01:00
|
|
|
|
|
|
|
return if_rmap;
|
|
|
|
}
|
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
static struct if_rmap *if_rmap_get(struct if_rmap_ctx *ctx, const char *ifname)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
struct if_rmap key = {.ifname = ifname};
|
2015-05-20 03:04:26 +02:00
|
|
|
struct if_rmap *ret;
|
2002-12-13 21:15:29 +01:00
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
ret = hash_get(ctx->ifrmaphash, &key, if_rmap_hash_alloc);
|
2015-11-22 01:47:32 +01:00
|
|
|
|
2015-05-20 03:04:26 +02:00
|
|
|
return ret;
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2019-05-14 22:19:07 +02:00
|
|
|
static unsigned int if_rmap_hash_make(const void *data)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2010-08-27 23:11:14 +02:00
|
|
|
const struct if_rmap *if_rmap = data;
|
2002-12-13 21:15:29 +01:00
|
|
|
|
2010-08-27 23:11:14 +02:00
|
|
|
return string_hash_make(if_rmap->ifname);
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:27:12 +02:00
|
|
|
static bool if_rmap_hash_cmp(const void *arg1, const void *arg2)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2008-08-14 17:25:25 +02:00
|
|
|
const struct if_rmap *if_rmap1 = arg1;
|
|
|
|
const struct if_rmap *if_rmap2 = arg2;
|
|
|
|
|
|
|
|
return strcmp(if_rmap1->ifname, if_rmap2->ifname) == 0;
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
2014-06-04 06:53:35 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
static void if_rmap_set(struct if_rmap_ctx *ctx, const char *ifname,
|
|
|
|
enum if_rmap_type type, const char *routemap_name)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
struct if_rmap *if_rmap = if_rmap_get(ctx, ifname);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
assert(type == IF_RMAP_IN || type == IF_RMAP_OUT);
|
|
|
|
XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[type]);
|
|
|
|
if_rmap->routemap[type] = XSTRDUP(MTYPE_IF_RMAP_NAME, routemap_name);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
if (ctx->if_rmap_add_hook)
|
|
|
|
(ctx->if_rmap_add_hook)(ctx, if_rmap);
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
static void if_rmap_unset(struct if_rmap_ctx *ctx, const char *ifname,
|
|
|
|
enum if_rmap_type type)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
struct if_rmap *if_rmap = if_rmap_lookup(ctx, ifname);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2002-12-13 21:15:29 +01:00
|
|
|
if (!if_rmap)
|
2023-04-09 11:02:51 +02:00
|
|
|
return;
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
assert(type == IF_RMAP_IN || type == IF_RMAP_OUT);
|
|
|
|
if (!if_rmap->routemap[type])
|
|
|
|
return;
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
XFREE(MTYPE_IF_RMAP_NAME, if_rmap->routemap[type]);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
if (ctx->if_rmap_delete_hook)
|
|
|
|
ctx->if_rmap_delete_hook(ctx, if_rmap);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-11 08:57:48 +02:00
|
|
|
if (if_rmap->routemap[IF_RMAP_IN] == NULL &&
|
|
|
|
if_rmap->routemap[IF_RMAP_OUT] == NULL) {
|
2019-01-14 08:58:36 +01:00
|
|
|
hash_release(ctx->ifrmaphash, if_rmap);
|
2002-12-13 21:15:29 +01:00
|
|
|
if_rmap_free(if_rmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
static int if_route_map_handler(struct vty *vty, bool no, const char *dir,
|
|
|
|
const char *other_dir, const char *ifname,
|
|
|
|
const char *route_map)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
enum nb_operation op = no ? NB_OP_DESTROY : NB_OP_MODIFY;
|
2023-04-02 02:33:25 +02:00
|
|
|
const struct lyd_node *dnode;
|
2023-04-09 11:02:51 +02:00
|
|
|
char xpath[XPATH_MAXLEN];
|
|
|
|
|
|
|
|
if (!no) {
|
|
|
|
snprintf(
|
|
|
|
xpath, sizeof(xpath),
|
|
|
|
"./if-route-maps/if-route-map[interface='%s']/%s-route-map",
|
|
|
|
ifname, dir);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we are deleting the last policy for this interface,
|
|
|
|
* (i.e., no `in` or `out` policy). delete the interface list
|
|
|
|
* node instead.
|
|
|
|
*/
|
|
|
|
dnode = yang_dnode_get(vty->candidate_config->dnode,
|
|
|
|
VTY_CURR_XPATH);
|
|
|
|
if (yang_dnode_existsf(
|
|
|
|
dnode,
|
|
|
|
"./if-route-maps/if-route-map[interface='%s']/%s-route-map",
|
|
|
|
ifname, other_dir)) {
|
|
|
|
snprintf(
|
|
|
|
xpath, sizeof(xpath),
|
|
|
|
"./if-route-maps/if-route-map[interface='%s']/%s-route-map",
|
|
|
|
ifname, dir);
|
|
|
|
} else {
|
|
|
|
/* both dir will be empty so delete the list node */
|
|
|
|
snprintf(xpath, sizeof(xpath),
|
|
|
|
"./if-route-maps/if-route-map[interface='%s']",
|
|
|
|
ifname);
|
|
|
|
}
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
2023-04-09 11:02:51 +02:00
|
|
|
nb_cli_enqueue_change(vty, xpath, op, route_map);
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
return nb_cli_apply_changes(vty, NULL);
|
|
|
|
}
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
DEFPY_YANG(if_ipv4_route_map, if_ipv4_route_map_cmd,
|
|
|
|
"route-map ROUTE-MAP <in$in|out> IFNAME",
|
|
|
|
"Route map set\n"
|
|
|
|
"Route map name\n"
|
|
|
|
"Route map set for input filtering\n"
|
|
|
|
"Route map set for output filtering\n" INTERFACE_STR)
|
|
|
|
{
|
|
|
|
const char *dir = in ? "in" : "out";
|
|
|
|
const char *other_dir = in ? "out" : "in";
|
|
|
|
|
|
|
|
return if_route_map_handler(vty, false, dir, other_dir, ifname,
|
|
|
|
route_map);
|
2016-09-23 22:17:29 +02:00
|
|
|
}
|
2003-05-25 17:13:49 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
DEFPY_YANG(no_if_ipv4_route_map, no_if_ipv4_route_map_cmd,
|
|
|
|
"no route-map [ROUTE-MAP] <in$in|out> IFNAME",
|
|
|
|
NO_STR
|
|
|
|
"Route map set\n"
|
|
|
|
"Route map name\n"
|
|
|
|
"Route map set for input filtering\n"
|
|
|
|
"Route map set for output filtering\n" INTERFACE_STR)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
const char *dir = in ? "in" : "out";
|
|
|
|
const char *other_dir = in ? "out" : "in";
|
2017-07-17 14:03:14 +02:00
|
|
|
|
2023-04-09 11:02:51 +02:00
|
|
|
return if_route_map_handler(vty, true, dir, other_dir, ifname,
|
|
|
|
route_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CLI infra requires new handlers for ripngd
|
|
|
|
*/
|
|
|
|
DEFPY_YANG(if_ipv6_route_map, if_ipv6_route_map_cmd,
|
|
|
|
"route-map ROUTE-MAP <in$in|out> IFNAME",
|
|
|
|
"Route map set\n"
|
|
|
|
"Route map name\n"
|
|
|
|
"Route map set for input filtering\n"
|
|
|
|
"Route map set for output filtering\n" INTERFACE_STR)
|
|
|
|
{
|
|
|
|
const char *dir = in ? "in" : "out";
|
|
|
|
const char *other_dir = in ? "out" : "in";
|
|
|
|
|
|
|
|
return if_route_map_handler(vty, false, dir, other_dir, ifname,
|
|
|
|
route_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFPY_YANG(no_if_ipv6_route_map, no_if_ipv6_route_map_cmd,
|
|
|
|
"no route-map [ROUTE-MAP] <in$in|out> IFNAME",
|
|
|
|
NO_STR
|
|
|
|
"Route map set\n"
|
|
|
|
"Route map name\n"
|
|
|
|
"Route map set for input filtering\n"
|
|
|
|
"Route map set for output filtering\n" INTERFACE_STR)
|
|
|
|
{
|
|
|
|
const char *dir = in ? "in" : "out";
|
|
|
|
const char *other_dir = in ? "out" : "in";
|
|
|
|
|
|
|
|
return if_route_map_handler(vty, true, dir, other_dir, ifname,
|
|
|
|
route_map);
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:17:03 +02:00
|
|
|
void cli_show_if_route_map(struct vty *vty, const struct lyd_node *dnode,
|
|
|
|
bool show_defaults)
|
|
|
|
{
|
|
|
|
if (yang_dnode_exists(dnode, "./in-route-map"))
|
|
|
|
vty_out(vty, " route-map %s in %s\n",
|
|
|
|
yang_dnode_get_string(dnode, "./in-route-map"),
|
|
|
|
yang_dnode_get_string(dnode, "./interface"));
|
|
|
|
if (yang_dnode_exists(dnode, "./out-route-map"))
|
|
|
|
vty_out(vty, " route-map %s out %s\n",
|
|
|
|
yang_dnode_get_string(dnode, "./out-route-map"),
|
|
|
|
yang_dnode_get_string(dnode, "./interface"));
|
|
|
|
}
|
2023-04-09 11:02:51 +02:00
|
|
|
|
|
|
|
void if_rmap_yang_modify_cb(struct if_rmap_ctx *ctx,
|
|
|
|
const struct lyd_node *dnode,
|
|
|
|
enum if_rmap_type type, bool del)
|
|
|
|
{
|
|
|
|
|
|
|
|
const char *mapname = yang_dnode_get_string(dnode, NULL);
|
|
|
|
const char *ifname = yang_dnode_get_string(dnode, "../interface");
|
|
|
|
|
|
|
|
if (del)
|
|
|
|
if_rmap_unset(ctx, ifname, type);
|
|
|
|
else
|
|
|
|
if_rmap_set(ctx, ifname, type, mapname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void if_rmap_yang_destroy_cb(struct if_rmap_ctx *ctx,
|
|
|
|
const struct lyd_node *dnode)
|
|
|
|
{
|
|
|
|
const char *ifname = yang_dnode_get_string(dnode, "interface");
|
|
|
|
if_rmap_unset(ctx, ifname, IF_RMAP_IN);
|
|
|
|
if_rmap_unset(ctx, ifname, IF_RMAP_OUT);
|
2016-09-23 22:17:29 +02:00
|
|
|
}
|
2003-05-25 17:13:49 +02:00
|
|
|
|
2019-01-14 08:58:36 +01:00
|
|
|
void if_rmap_ctx_delete(struct if_rmap_ctx *ctx)
|
|
|
|
{
|
2023-03-21 13:54:21 +01:00
|
|
|
hash_clean_and_free(&ctx->ifrmaphash, (void (*)(void *))if_rmap_free);
|
2023-04-09 11:02:51 +02:00
|
|
|
XFREE(MTYPE_IF_RMAP_CTX_NAME, ctx->name);
|
2019-01-14 08:58:36 +01:00
|
|
|
XFREE(MTYPE_IF_RMAP_CTX, ctx);
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:08:43 +01:00
|
|
|
/* name is optional: either vrf name, or other */
|
|
|
|
struct if_rmap_ctx *if_rmap_ctx_create(const char *name)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2019-01-14 08:58:36 +01:00
|
|
|
struct if_rmap_ctx *ctx;
|
|
|
|
|
|
|
|
ctx = XCALLOC(MTYPE_IF_RMAP_CTX, sizeof(struct if_rmap_ctx));
|
2019-02-19 21:08:43 +01:00
|
|
|
|
2023-04-02 02:33:25 +02:00
|
|
|
ctx->name = XSTRDUP(MTYPE_IF_RMAP_CTX_NAME, name);
|
2023-04-11 08:57:48 +02:00
|
|
|
ctx->ifrmaphash =
|
|
|
|
hash_create_size(4, if_rmap_hash_make, if_rmap_hash_cmp,
|
|
|
|
"Interface Route-Map Hash");
|
2019-01-14 08:58:36 +01:00
|
|
|
return ctx;
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|
|
|
|
|
2003-05-24 23:41:49 +02:00
|
|
|
void if_rmap_init(int node)
|
2002-12-13 21:15:29 +01:00
|
|
|
{
|
2023-04-09 11:02:51 +02:00
|
|
|
if (node == RIP_NODE) {
|
|
|
|
install_element(RIP_NODE, &if_ipv4_route_map_cmd);
|
|
|
|
install_element(RIP_NODE, &no_if_ipv4_route_map_cmd);
|
|
|
|
} else if (node == RIPNG_NODE) {
|
|
|
|
install_element(RIPNG_NODE, &if_ipv6_route_map_cmd);
|
|
|
|
install_element(RIPNG_NODE, &no_if_ipv6_route_map_cmd);
|
2003-05-24 23:41:49 +02:00
|
|
|
}
|
2019-01-14 08:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if_rmap_terminate(void)
|
|
|
|
{
|
2002-12-13 21:15:29 +01:00
|
|
|
}
|