| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2015 Cumulus Networks. All rights reserved. |
| 4 |
* Copyright (c) 2015 David Ahern <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* @ingroup link |
| 9 |
* @defgroup vrf VRF |
| 10 |
* Virtual Routing and Forwarding link module |
| 11 |
* |
| 12 |
* @details |
| 13 |
* \b Link Type Name: "vrf" |
| 14 |
* |
| 15 |
* @route_doc{link_vrf, VRF Documentation} |
| 16 |
* |
| 17 |
* @{ |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "nl-default.h" |
| 21 |
|
| 22 |
#include <linux/if_link.h> |
| 23 |
#include <linux/rtnetlink.h> |
| 24 |
|
| 25 |
#include <netlink/netlink.h> |
| 26 |
#include <netlink/attr.h> |
| 27 |
#include <netlink/utils.h> |
| 28 |
#include <netlink/object.h> |
| 29 |
#include <netlink/route/rtnl.h> |
| 30 |
#include <netlink/route/link/vrf.h> |
| 31 |
|
| 32 |
#include "nl-route.h" |
| 33 |
#include "link-api.h" |
| 34 |
|
| 35 |
#define VRF_TABLE_ID_MAX RT_TABLE_MAX |
| 36 |
|
| 37 |
/** @cond SKIP */ |
| 38 |
#define VRF_HAS_TABLE_ID (1<<0) |
| 39 |
|
| 40 |
struct vrf_info { |
| 41 |
uint32_t table_id; |
| 42 |
uint32_t vi_mask; |
| 43 |
}; |
| 44 |
|
| 45 |
/** @endcond */ |
| 46 |
|
| 47 |
static struct nla_policy vrf_policy[IFLA_VRF_MAX + 1] = { |
| 48 |
[IFLA_VRF_TABLE] = { .type = NLA_U32 }, |
| 49 |
}; |
| 50 |
|
| 51 |
static int vrf_alloc(struct rtnl_link *link) |
| 52 |
{ |
| 53 |
struct vrf_info *vi; |
| 54 |
|
| 55 |
if (link->l_info) { |
| 56 |
memset(link->l_info, 0, sizeof (*vi)); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
if ((vi = calloc(1, sizeof(*vi))) == NULL) |
| 61 |
return -NLE_NOMEM; |
| 62 |
|
| 63 |
link->l_info = vi; |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int vrf_parse(struct rtnl_link *link, struct nlattr *data, |
| 69 |
struct nlattr *xstats) |
| 70 |
{ |
| 71 |
struct nlattr *tb[IFLA_VRF_MAX+1]; |
| 72 |
struct vrf_info *vi; |
| 73 |
int err; |
| 74 |
|
| 75 |
NL_DBG(3, "Parsing VRF link info"); |
| 76 |
|
| 77 |
if ((err = nla_parse_nested(tb, IFLA_VRF_MAX, data, vrf_policy)) < 0) |
| 78 |
goto errout; |
| 79 |
|
| 80 |
if ((err = vrf_alloc(link)) < 0) |
| 81 |
goto errout; |
| 82 |
|
| 83 |
vi = link->l_info; |
| 84 |
|
| 85 |
if (tb[IFLA_VRF_TABLE]) { |
| 86 |
vi->table_id = nla_get_u32(tb[IFLA_VRF_TABLE]); |
| 87 |
vi->vi_mask |= VRF_HAS_TABLE_ID; |
| 88 |
} |
| 89 |
|
| 90 |
err = 0; |
| 91 |
|
| 92 |
errout: |
| 93 |
return err; |
| 94 |
} |
| 95 |
|
| 96 |
static void vrf_free(struct rtnl_link *link) |
| 97 |
{ |
| 98 |
free(link->l_info); |
| 99 |
link->l_info = NULL; |
| 100 |
} |
| 101 |
|
| 102 |
static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src) |
| 103 |
{ |
| 104 |
struct vrf_info *vdst, *vsrc = src->l_info; |
| 105 |
int err; |
| 106 |
|
| 107 |
dst->l_info = NULL; |
| 108 |
if ((err = rtnl_link_set_type(dst, "vrf")) < 0) |
| 109 |
return err; |
| 110 |
vdst = dst->l_info; |
| 111 |
|
| 112 |
BUG_ON(!vdst || !vsrc); |
| 113 |
|
| 114 |
memcpy(vdst, vsrc, sizeof(struct vrf_info)); |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
static int vrf_put_attrs(struct nl_msg *msg, struct rtnl_link *link) |
| 120 |
{ |
| 121 |
struct vrf_info *vi = link->l_info; |
| 122 |
struct nlattr *data; |
| 123 |
|
| 124 |
if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) |
| 125 |
return -NLE_NOMEM; |
| 126 |
|
| 127 |
if (vi->vi_mask & VRF_HAS_TABLE_ID) { |
| 128 |
NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id); |
| 129 |
} |
| 130 |
|
| 131 |
nla_nest_end(msg, data); |
| 132 |
|
| 133 |
nla_put_failure: |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static void vrf_dump(struct rtnl_link *link, struct nl_dump_params *p) |
| 139 |
{ |
| 140 |
struct vrf_info *vi = link->l_info; |
| 141 |
|
| 142 |
if (vi->vi_mask & VRF_HAS_TABLE_ID) { |
| 143 |
nl_dump(p, "table-id %u", vi->table_id); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static struct rtnl_link_info_ops vrf_info_ops = { |
| 148 |
.io_name = "vrf", |
| 149 |
.io_alloc = vrf_alloc, |
| 150 |
.io_parse = vrf_parse, |
| 151 |
.io_dump = { |
| 152 |
[NL_DUMP_LINE] = vrf_dump, |
| 153 |
[NL_DUMP_DETAILS] = vrf_dump, |
| 154 |
}, |
| 155 |
.io_clone = vrf_clone, |
| 156 |
.io_put_attrs = vrf_put_attrs, |
| 157 |
.io_free = vrf_free, |
| 158 |
}; |
| 159 |
|
| 160 |
/** @cond SKIP */ |
| 161 |
#define IS_VRF_LINK_ASSERT(link) \ |
| 162 |
if ((link)->l_info_ops != &vrf_info_ops) { \ |
| 163 |
APPBUG("Link is not a VRF link. set type \"vrf\" first."); \ |
| 164 |
return -NLE_OPNOTSUPP; \ |
| 165 |
} |
| 166 |
/** @endcond */ |
| 167 |
|
| 168 |
/** |
| 169 |
* @name VRF Object |
| 170 |
* @{ |
| 171 |
*/ |
| 172 |
|
| 173 |
/** |
| 174 |
* Allocate link object of type VRF |
| 175 |
* |
| 176 |
* @return Allocated link object or NULL. |
| 177 |
*/ |
| 178 |
struct rtnl_link *rtnl_link_vrf_alloc(void) |
| 179 |
{ |
| 180 |
struct rtnl_link *link; |
| 181 |
|
| 182 |
if (!(link = rtnl_link_alloc())) |
| 183 |
return NULL; |
| 184 |
|
| 185 |
if (rtnl_link_set_type(link, "vrf") < 0) { |
| 186 |
rtnl_link_put(link); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
return link; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Check if link is a VRF link |
| 195 |
* @arg link Link object |
| 196 |
* |
| 197 |
* @return True if link is a VRF link, otherwise false is returned. |
| 198 |
*/ |
| 199 |
int rtnl_link_is_vrf(struct rtnl_link *link) |
| 200 |
{ |
| 201 |
return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vrf"); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get VRF table id |
| 206 |
* @arg link Link object |
| 207 |
* @arg id Pointer to store table identifier |
| 208 |
* |
| 209 |
* @return 0 on success or a negative error code |
| 210 |
*/ |
| 211 |
int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id) |
| 212 |
{ |
| 213 |
struct vrf_info *vi = link->l_info; |
| 214 |
|
| 215 |
IS_VRF_LINK_ASSERT(link); |
| 216 |
if(!id) |
| 217 |
return -NLE_INVAL; |
| 218 |
|
| 219 |
if (vi->vi_mask & VRF_HAS_TABLE_ID) |
| 220 |
*id = vi->table_id; |
| 221 |
else |
| 222 |
return -NLE_AGAIN; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Set VRF table id |
| 229 |
* @arg link Link object |
| 230 |
* @arg id Table identifier associated with VRF link |
| 231 |
* |
| 232 |
* @return 0 on success or a negative error code |
| 233 |
*/ |
| 234 |
int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id) |
| 235 |
{ |
| 236 |
struct vrf_info *vi = link->l_info; |
| 237 |
|
| 238 |
IS_VRF_LINK_ASSERT(link); |
| 239 |
|
| 240 |
_NL_STATIC_ASSERT(VRF_TABLE_ID_MAX == UINT32_MAX); |
| 241 |
|
| 242 |
vi->table_id = id; |
| 243 |
vi->vi_mask |= VRF_HAS_TABLE_ID; |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
/** @} */ |
| 249 |
|
| 250 |
static void _nl_init vrf_init(void) |
| 251 |
{ |
| 252 |
rtnl_link_register_info(&vrf_info_ops); |
| 253 |
} |
| 254 |
|
| 255 |
static void _nl_exit vrf_exit(void) |
| 256 |
{ |
| 257 |
rtnl_link_unregister_info(&vrf_info_ops); |
| 258 |
} |
| 259 |
|
| 260 |
/** @} */ |