| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2015 Cong Wang <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @ingroup link |
| 8 |
* @defgroup ipvlan IPVLAN |
| 9 |
* IP-based Virtual LAN link module |
| 10 |
* |
| 11 |
* @details |
| 12 |
* \b Link Type Name: "ipvlan" |
| 13 |
* |
| 14 |
* @route_doc{link_ipvlan, IPVLAN Documentation} |
| 15 |
* |
| 16 |
* @{ |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "nl-default.h" |
| 20 |
|
| 21 |
#include <linux/if_link.h> |
| 22 |
|
| 23 |
#include <netlink/netlink.h> |
| 24 |
#include <netlink/attr.h> |
| 25 |
#include <netlink/utils.h> |
| 26 |
#include <netlink/object.h> |
| 27 |
#include <netlink/route/rtnl.h> |
| 28 |
#include <netlink/route/link/ipvlan.h> |
| 29 |
|
| 30 |
#include "nl-route.h" |
| 31 |
#include "link-api.h" |
| 32 |
|
| 33 |
/** @cond SKIP */ |
| 34 |
#define IPVLAN_HAS_MODE (1<<0) |
| 35 |
|
| 36 |
struct ipvlan_info |
| 37 |
{ |
| 38 |
uint16_t ipi_mode; |
| 39 |
uint32_t ipi_mask; |
| 40 |
}; |
| 41 |
|
| 42 |
/** @endcond */ |
| 43 |
|
| 44 |
static struct nla_policy ipvlan_policy[IFLA_IPVLAN_MAX+1] = { |
| 45 |
[IFLA_IPVLAN_MODE] = { .type = NLA_U16 }, |
| 46 |
}; |
| 47 |
|
| 48 |
static int ipvlan_alloc(struct rtnl_link *link) |
| 49 |
{ |
| 50 |
struct ipvlan_info *ipi; |
| 51 |
|
| 52 |
if (link->l_info) |
| 53 |
memset(link->l_info, 0, sizeof(*ipi)); |
| 54 |
else { |
| 55 |
if ((ipi = calloc(1, sizeof(*ipi))) == NULL) |
| 56 |
return -NLE_NOMEM; |
| 57 |
|
| 58 |
link->l_info = ipi; |
| 59 |
} |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
static int ipvlan_parse(struct rtnl_link *link, struct nlattr *data, |
| 65 |
struct nlattr *xstats) |
| 66 |
{ |
| 67 |
struct nlattr *tb[IFLA_IPVLAN_MAX+1]; |
| 68 |
struct ipvlan_info *ipi; |
| 69 |
int err; |
| 70 |
|
| 71 |
NL_DBG(3, "Parsing IPVLAN link info\n"); |
| 72 |
|
| 73 |
if ((err = nla_parse_nested(tb, IFLA_IPVLAN_MAX, data, ipvlan_policy)) < 0) |
| 74 |
goto errout; |
| 75 |
|
| 76 |
if ((err = ipvlan_alloc(link)) < 0) |
| 77 |
goto errout; |
| 78 |
|
| 79 |
ipi = link->l_info; |
| 80 |
|
| 81 |
if (tb[IFLA_IPVLAN_MODE]) { |
| 82 |
ipi->ipi_mode = nla_get_u16(tb[IFLA_IPVLAN_MODE]); |
| 83 |
ipi->ipi_mask |= IPVLAN_HAS_MODE; |
| 84 |
} |
| 85 |
|
| 86 |
err = 0; |
| 87 |
errout: |
| 88 |
return err; |
| 89 |
} |
| 90 |
|
| 91 |
static void ipvlan_free(struct rtnl_link *link) |
| 92 |
{ |
| 93 |
free(link->l_info); |
| 94 |
link->l_info = NULL; |
| 95 |
} |
| 96 |
|
| 97 |
static void ipvlan_dump(struct rtnl_link *link, struct nl_dump_params *p) |
| 98 |
{ |
| 99 |
char buf[64]; |
| 100 |
struct ipvlan_info *ipi = link->l_info; |
| 101 |
|
| 102 |
if (ipi->ipi_mask & IPVLAN_HAS_MODE) { |
| 103 |
rtnl_link_ipvlan_mode2str(ipi->ipi_mode, buf, sizeof(buf)); |
| 104 |
nl_dump(p, "ipvlan-mode %s", buf); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static int ipvlan_clone(struct rtnl_link *dst, struct rtnl_link *src) |
| 109 |
{ |
| 110 |
struct ipvlan_info *vdst, *vsrc = src->l_info; |
| 111 |
int err; |
| 112 |
|
| 113 |
dst->l_info = NULL; |
| 114 |
if ((err = rtnl_link_set_type(dst, "ipvlan")) < 0) |
| 115 |
return err; |
| 116 |
vdst = dst->l_info; |
| 117 |
|
| 118 |
if (!vdst || !vsrc) |
| 119 |
return -NLE_NOMEM; |
| 120 |
|
| 121 |
memcpy(vdst, vsrc, sizeof(struct ipvlan_info)); |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int ipvlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link) |
| 127 |
{ |
| 128 |
struct ipvlan_info *ipi = link->l_info; |
| 129 |
struct nlattr *data; |
| 130 |
|
| 131 |
if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) |
| 132 |
return -NLE_MSGSIZE; |
| 133 |
|
| 134 |
if (ipi->ipi_mask & IPVLAN_HAS_MODE) |
| 135 |
NLA_PUT_U16(msg, IFLA_IPVLAN_MODE, ipi->ipi_mode); |
| 136 |
|
| 137 |
nla_nest_end(msg, data); |
| 138 |
|
| 139 |
nla_put_failure: |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
static struct rtnl_link_info_ops ipvlan_info_ops = { |
| 145 |
.io_name = "ipvlan", |
| 146 |
.io_alloc = ipvlan_alloc, |
| 147 |
.io_parse = ipvlan_parse, |
| 148 |
.io_dump = { |
| 149 |
[NL_DUMP_LINE] = ipvlan_dump, |
| 150 |
[NL_DUMP_DETAILS] = ipvlan_dump, |
| 151 |
}, |
| 152 |
.io_clone = ipvlan_clone, |
| 153 |
.io_put_attrs = ipvlan_put_attrs, |
| 154 |
.io_free = ipvlan_free, |
| 155 |
}; |
| 156 |
|
| 157 |
/** @cond SKIP */ |
| 158 |
#define IS_IPVLAN_LINK_ASSERT(link) \ |
| 159 |
if ((link)->l_info_ops != &ipvlan_info_ops) { \ |
| 160 |
APPBUG("Link is not a ipvlan link. set type \"ipvlan\" first."); \ |
| 161 |
return -NLE_OPNOTSUPP; \ |
| 162 |
} |
| 163 |
/** @endcond */ |
| 164 |
|
| 165 |
/** |
| 166 |
* @name IPVLAN Object |
| 167 |
* @{ |
| 168 |
*/ |
| 169 |
|
| 170 |
/** |
| 171 |
* Allocate link object of type IPVLAN |
| 172 |
* |
| 173 |
* @return Allocated link object or NULL. |
| 174 |
*/ |
| 175 |
struct rtnl_link *rtnl_link_ipvlan_alloc(void) |
| 176 |
{ |
| 177 |
struct rtnl_link *link; |
| 178 |
|
| 179 |
if (!(link = rtnl_link_alloc())) |
| 180 |
return NULL; |
| 181 |
|
| 182 |
if (rtnl_link_set_type(link, "ipvlan") < 0) { |
| 183 |
rtnl_link_put(link); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
return link; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Check if link is a IPVLAN link |
| 192 |
* @arg link Link object |
| 193 |
* |
| 194 |
* @return True if link is a IPVLAN link, otherwise false is returned. |
| 195 |
*/ |
| 196 |
int rtnl_link_is_ipvlan(struct rtnl_link *link) |
| 197 |
{ |
| 198 |
return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "ipvlan"); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set IPVLAN MODE |
| 203 |
* @arg link Link object |
| 204 |
* @arg mode IPVLAN mode |
| 205 |
* |
| 206 |
* @return 0 on success or a negative error code |
| 207 |
*/ |
| 208 |
int rtnl_link_ipvlan_set_mode(struct rtnl_link *link, uint16_t mode) |
| 209 |
{ |
| 210 |
struct ipvlan_info *ipi = link->l_info; |
| 211 |
|
| 212 |
IS_IPVLAN_LINK_ASSERT(link); |
| 213 |
|
| 214 |
ipi->ipi_mode = mode; |
| 215 |
ipi->ipi_mask |= IPVLAN_HAS_MODE; |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get IPVLAN Mode |
| 222 |
* @arg link Link object |
| 223 |
* @arg out_mode on success, return the mode |
| 224 |
* |
| 225 |
* @return 0 on success or a negative error code. |
| 226 |
*/ |
| 227 |
int rtnl_link_ipvlan_get_mode(struct rtnl_link *link, uint16_t *out_mode) |
| 228 |
{ |
| 229 |
struct ipvlan_info *ipi = link->l_info; |
| 230 |
|
| 231 |
IS_IPVLAN_LINK_ASSERT(link); |
| 232 |
|
| 233 |
if (!(ipi->ipi_mask & IPVLAN_HAS_MODE)) |
| 234 |
return -NLE_INVAL; |
| 235 |
*out_mode = ipi->ipi_mode; |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
/** @} */ |
| 240 |
|
| 241 |
static const struct trans_tbl ipvlan_modes[] = { |
| 242 |
__ADD(IPVLAN_MODE_L2, l2), |
| 243 |
__ADD(IPVLAN_MODE_L3, l3), |
| 244 |
}; |
| 245 |
|
| 246 |
/** |
| 247 |
* @name Mode Translation |
| 248 |
* @{ |
| 249 |
*/ |
| 250 |
|
| 251 |
char *rtnl_link_ipvlan_mode2str(int mode, char *buf, size_t len) |
| 252 |
{ |
| 253 |
return __type2str(mode, buf, len, ipvlan_modes, ARRAY_SIZE(ipvlan_modes)); |
| 254 |
} |
| 255 |
|
| 256 |
int rtnl_link_ipvlan_str2mode(const char *name) |
| 257 |
{ |
| 258 |
return __str2type(name, ipvlan_modes, ARRAY_SIZE(ipvlan_modes)); |
| 259 |
} |
| 260 |
|
| 261 |
/** @} */ |
| 262 |
|
| 263 |
static void _nl_init ipvlan_init(void) |
| 264 |
{ |
| 265 |
rtnl_link_register_info(&ipvlan_info_ops); |
| 266 |
} |
| 267 |
|
| 268 |
static void _nl_exit ipvlan_exit(void) |
| 269 |
{ |
| 270 |
rtnl_link_unregister_info(&ipvlan_info_ops); |
| 271 |
} |
| 272 |
|
| 273 |
/** @} */ |