20#include "nl-default.h"
22#include <linux/if_link.h>
24#include <linux/ethtool.h>
26#include <netlink/netlink.h>
27#include <netlink/attr.h>
28#include <netlink/utils.h>
29#include <netlink/object.h>
30#include <netlink/route/rtnl.h>
31#include <netlink/route/link/macvlan.h>
32#include <netlink/route/link/macvtap.h>
38#define MACVLAN_HAS_MODE (1<<0)
39#define MACVLAN_HAS_FLAGS (1<<1)
40#define MACVLAN_HAS_MACADDR (1<<2)
47 uint32_t mvi_maccount;
49 struct nl_addr **mvi_macaddr;
63static int macvlan_alloc(
struct rtnl_link *link)
65 struct macvlan_info *mvi;
70 for (i = 0; i < mvi->mvi_maccount; i++)
72 free(mvi->mvi_macaddr);
73 memset(mvi, 0,
sizeof(*mvi));
75 if ((mvi = calloc(1,
sizeof(*mvi))) == NULL)
80 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
85static int macvlan_parse(
struct rtnl_link *link,
struct nlattr *data,
86 struct nlattr *xstats)
88 struct nlattr *tb[IFLA_MACVLAN_MAX+1];
89 struct macvlan_info *mvi;
94 NL_DBG(3,
"Parsing %s link info", link->l_info_ops->
io_name);
96 if ((err =
nla_parse_nested(tb, IFLA_MACVLAN_MAX, data, macvlan_policy)) < 0)
99 if ((err = macvlan_alloc(link)) < 0)
104 if (tb[IFLA_MACVLAN_MODE]) {
105 mvi->mvi_mode =
nla_get_u32(tb[IFLA_MACVLAN_MODE]);
106 mvi->mvi_mask |= MACVLAN_HAS_MODE;
109 if (tb[IFLA_MACVLAN_FLAGS]) {
110 mvi->mvi_flags =
nla_get_u16(tb[IFLA_MACVLAN_FLAGS]);
111 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
114 if ( tb[IFLA_MACVLAN_MACADDR_COUNT]
115 && tb[IFLA_MACVLAN_MACADDR_DATA]) {
116 mvi->mvi_maccount =
nla_get_u32(tb[IFLA_MACVLAN_MACADDR_COUNT]);
117 if (mvi->mvi_maccount > 0) {
120 nla =
nla_data(tb[IFLA_MACVLAN_MACADDR_DATA]);
121 len =
nla_len(tb[IFLA_MACVLAN_MACADDR_DATA]);
123 mvi->mvi_macaddr = calloc(mvi->mvi_maccount,
124 sizeof(*(mvi->mvi_macaddr)));
125 if (mvi->mvi_macaddr == NULL) {
132 if (i >= mvi->mvi_maccount)
134 if (
nla_type(nla) != IFLA_MACVLAN_MACADDR ||
141 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
149static void macvlan_free(
struct rtnl_link *link)
151 struct macvlan_info *mvi;
158 for (i = 0; i < mvi->mvi_maccount; i++)
160 free(mvi->mvi_macaddr);
170 struct macvlan_info *mvi = link->l_info;
172 if (mvi->mvi_mask & MACVLAN_HAS_MODE) {
173 rtnl_link_macvlan_mode2str(mvi->mvi_mode, buf,
sizeof(buf));
177 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS) {
178 rtnl_link_macvlan_flags2str(mvi->mvi_flags, buf,
sizeof(buf));
182 if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
183 nl_dump(p,
" macvlan-count %u", (
unsigned) mvi->mvi_maccount);
185 if (mvi->mvi_maccount)
186 nl_dump(p,
" macvlan-sourcemac");
188 for (i = 0; i < mvi->mvi_maccount; i++) {
198 struct macvlan_info *vdst, *vsrc = src->l_info;
210 memcpy(vdst, vsrc,
sizeof(
struct macvlan_info));
212 if ( vsrc->mvi_mask & MACVLAN_HAS_MACADDR
213 && vsrc->mvi_maccount > 0) {
214 vdst->mvi_macaddr = calloc(vdst->mvi_maccount,
215 sizeof(*(vdst->mvi_macaddr)));
216 for (i = 0; i < vdst->mvi_maccount; i++)
219 vdst->mvi_macaddr = NULL;
224static int macvlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
226 struct macvlan_info *mvi = link->l_info;
227 struct nlattr *data, *datamac = NULL;
235 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
236 NLA_PUT_U32(msg, IFLA_MACVLAN_MODE, mvi->mvi_mode);
238 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS)
239 NLA_PUT_U16(msg, IFLA_MACVLAN_FLAGS, mvi->mvi_flags);
241 if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
244 NLA_PUT_U32(msg, IFLA_MACVLAN_MACADDR_MODE, mvi->mvi_macmode);
247 goto nla_put_failure;
249 for (i = 0; i < mvi->mvi_maccount; i++) {
251 mvi->mvi_macaddr[i]);
268 .io_alloc = macvlan_alloc,
269 .io_parse = macvlan_parse,
273 .io_clone = macvlan_clone,
274 .io_put_attrs = macvlan_put_attrs,
275 .io_free = macvlan_free,
280 .io_alloc = macvlan_alloc,
281 .io_parse = macvlan_parse,
285 .io_clone = macvlan_clone,
286 .io_put_attrs = macvlan_put_attrs,
287 .io_free = macvlan_free,
291#define IS_MACVLAN_LINK_ASSERT(link) \
292 if ((link)->l_info_ops != &macvlan_info_ops) { \
293 APPBUG("Link is not a macvlan link. set type \"macvlan\" first."); \
294 return -NLE_OPNOTSUPP; \
297#define IS_MACVTAP_LINK_ASSERT(link) \
298 if ((link)->l_info_ops != &macvtap_info_ops) { \
299 APPBUG("Link is not a macvtap link. set type \"macvtap\" first."); \
300 return -NLE_OPNOTSUPP; \
337 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"macvlan");
349 struct macvlan_info *mvi = link->l_info;
351 IS_MACVLAN_LINK_ASSERT(link);
353 mvi->mvi_mode = mode;
354 mvi->mvi_mask |= MACVLAN_HAS_MODE;
356 if (mode != MACVLAN_MODE_SOURCE) {
359 for (i = 0; i < mvi->mvi_maccount; i++)
361 free(mvi->mvi_macaddr);
362 mvi->mvi_maccount = 0;
363 mvi->mvi_macaddr = NULL;
364 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
365 mvi->mvi_mask &= ~MACVLAN_HAS_MACADDR;
379 struct macvlan_info *mvi = link->l_info;
381 IS_MACVLAN_LINK_ASSERT(link);
383 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
384 return mvi->mvi_mode;
400 struct macvlan_info *mvi = link->l_info;
402 IS_MACVLAN_LINK_ASSERT(link);
404 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
405 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
408 mvi->mvi_macmode = macmode;
409 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
425 struct macvlan_info *mvi = link->l_info;
427 IS_MACVLAN_LINK_ASSERT(link);
429 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
430 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
433 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
436 *out_macmode = mvi->mvi_macmode;
450 struct macvlan_info *mvi = link->l_info;
452 IS_MACVLAN_LINK_ASSERT(link);
454 mvi->mvi_flags |= flags;
455 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
472 struct macvlan_info *mvi = link->l_info;
474 IS_MACVLAN_LINK_ASSERT(link);
476 mvi->mvi_flags &= ~flags;
477 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
490 struct macvlan_info *mvi = link->l_info;
492 IS_MACVLAN_LINK_ASSERT(link);
494 return mvi->mvi_flags;
506 struct macvlan_info *mvi = link->l_info;
508 IS_MACVLAN_LINK_ASSERT(link);
510 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
511 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
514 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
517 *out_count = mvi->mvi_maccount;
534 const struct nl_addr **out_addr)
536 struct macvlan_info *mvi = link->l_info;
538 IS_MACVLAN_LINK_ASSERT(link);
540 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
541 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
544 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
547 if (idx >= mvi->mvi_maccount)
550 *out_addr = mvi->mvi_macaddr[idx];
565 struct macvlan_info *mvi = link->l_info;
566 struct nl_addr **mvi_macaddr;
569 IS_MACVLAN_LINK_ASSERT(link);
574 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
575 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
578 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
581 if (mvi->mvi_maccount >= UINT32_MAX)
584 newsize = (mvi->mvi_maccount + 1) *
sizeof(*(mvi->mvi_macaddr));
585 mvi_macaddr = realloc(mvi->mvi_macaddr, newsize);
589 mvi->mvi_macaddr = mvi_macaddr;
593 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
610 struct macvlan_info *mvi = link->l_info;
613 IS_MACVLAN_LINK_ASSERT(link);
618 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
619 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
622 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
628 while (i + found < mvi->mvi_maccount) {
629 mvi->mvi_macaddr[i] = mvi->mvi_macaddr[i + found];
631 mvi->mvi_macaddr[i + found] = NULL;
634 mvi->mvi_macaddr[i] = NULL;
642 mvi->mvi_maccount -= found;
644 return found > INT_MAX ? INT_MAX : (int) found;
683 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"macvtap");
695 struct macvlan_info *mvi = link->l_info;
697 IS_MACVTAP_LINK_ASSERT(link);
699 mvi->mvi_mode = mode;
700 mvi->mvi_mask |= MACVLAN_HAS_MODE;
713 struct macvlan_info *mvi = link->l_info;
715 IS_MACVTAP_LINK_ASSERT(link);
717 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
718 return mvi->mvi_mode;
732 struct macvlan_info *mvi = link->l_info;
734 IS_MACVTAP_LINK_ASSERT(link);
736 mvi->mvi_flags |= flags;
737 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
754 struct macvlan_info *mvi = link->l_info;
756 IS_MACVTAP_LINK_ASSERT(link);
758 mvi->mvi_flags &= ~flags;
759 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
772 struct macvlan_info *mvi = link->l_info;
774 IS_MACVTAP_LINK_ASSERT(link);
776 return mvi->mvi_flags;
782static const struct trans_tbl macvlan_flags[] = {
783 __ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
786static const struct trans_tbl macvlan_modes[] = {
787 __ADD(MACVLAN_MODE_PRIVATE,
private),
788 __ADD(MACVLAN_MODE_VEPA, vepa),
789 __ADD(MACVLAN_MODE_BRIDGE, bridge),
790 __ADD(MACVLAN_MODE_PASSTHRU, passthru),
791 __ADD(MACVLAN_MODE_SOURCE, source),
794static const struct trans_tbl macvlan_macmodes[] = {
795 __ADD(MACVLAN_MACADDR_ADD,
"add"),
796 __ADD(MACVLAN_MACADDR_DEL,
"del"),
797 __ADD(MACVLAN_MACADDR_SET,
"set"),
798 __ADD(MACVLAN_MACADDR_FLUSH,
"flush"),
806char *rtnl_link_macvlan_flags2str(
int flags,
char *buf,
size_t len)
808 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
811int rtnl_link_macvlan_str2flags(
const char *name)
813 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
816char *rtnl_link_macvtap_flags2str(
int flags,
char *buf,
size_t len)
818 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
821int rtnl_link_macvtap_str2flags(
const char *name)
823 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
833char *rtnl_link_macvlan_mode2str(
int mode,
char *buf,
size_t len)
835 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
838int rtnl_link_macvlan_str2mode(
const char *name)
840 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
843char *rtnl_link_macvlan_macmode2str(
int mode,
char *buf,
size_t len)
845 return __type2str(mode, buf, len, macvlan_macmodes,
846 ARRAY_SIZE(macvlan_macmodes));
849int rtnl_link_macvlan_str2macmode(
const char *name)
851 return __str2type(name, macvlan_macmodes, ARRAY_SIZE(macvlan_macmodes));
854char *rtnl_link_macvtap_mode2str(
int mode,
char *buf,
size_t len)
856 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
859int rtnl_link_macvtap_str2mode(
const char *name)
861 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
866static void _nl_init macvlan_init(
void)
872static void _nl_exit macvlan_exit(
void)
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
struct nlattr * nla_next(const struct nlattr *nla, int *remaining)
Return next attribute in a stream of attributes.
int nla_type(const struct nlattr *nla)
Return type of the attribute.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
int nla_ok(const struct nlattr *nla, int remaining)
Check if the attribute header and payload can be accessed safely.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
@ NLA_UNSPEC
Unspecified type, binary data chunk.
@ NLA_NESTED
Nested attributes.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
int rtnl_link_macvlan_get_macmode(struct rtnl_link *link, uint32_t *out_macmode)
Get MACVLAN MACMODE.
int rtnl_link_is_macvtap(struct rtnl_link *link)
Check if link is a MACVTAP link.
int rtnl_link_macvtap_set_flags(struct rtnl_link *link, uint16_t flags)
Set MACVTAP flags.
int rtnl_link_macvlan_unset_flags(struct rtnl_link *link, uint16_t flags)
Unset MACVLAN flags.
int rtnl_link_macvlan_set_mode(struct rtnl_link *link, uint32_t mode)
Set MACVLAN MODE.
uint32_t rtnl_link_macvtap_get_mode(struct rtnl_link *link)
Get MACVTAP Mode.
struct rtnl_link * rtnl_link_macvlan_alloc(void)
Allocate link object of type MACVLAN.
int rtnl_link_macvtap_set_mode(struct rtnl_link *link, uint32_t mode)
Set MACVTAP MODE.
uint16_t rtnl_link_macvtap_get_flags(struct rtnl_link *link)
Get MACVTAP flags.
uint32_t rtnl_link_macvlan_get_mode(struct rtnl_link *link)
Get MACVLAN Mode.
int rtnl_link_macvlan_set_macmode(struct rtnl_link *link, uint32_t macmode)
Set MACVLAN MACMODE.
int rtnl_link_macvtap_unset_flags(struct rtnl_link *link, uint16_t flags)
Unset MACVTAP flags.
int rtnl_link_macvlan_get_macaddr(struct rtnl_link *link, uint32_t idx, const struct nl_addr **out_addr)
Get configured remote MAC-Addr from MACVLAN device in source mode.
int rtnl_link_macvlan_set_flags(struct rtnl_link *link, uint16_t flags)
Set MACVLAN flags.
int rtnl_link_macvlan_del_macaddr(struct rtnl_link *link, struct nl_addr *addr)
Remove MAC-Addr from MACVLAN device in source mode.
uint16_t rtnl_link_macvlan_get_flags(struct rtnl_link *link)
Get MACVLAN flags.
int rtnl_link_is_macvlan(struct rtnl_link *link)
Check if link is a MACVLAN link.
int rtnl_link_macvlan_add_macaddr(struct rtnl_link *link, struct nl_addr *addr)
Add MAC-Addr to MACVLAN device in source mode.
struct rtnl_link * rtnl_link_macvtap_alloc(void)
Allocate link object of type MACVTAP.
int rtnl_link_macvlan_count_macaddr(struct rtnl_link *link, uint32_t *out_count)
Get number of MAC-Addr for MACVLAN device in source mode.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.
Available operations to modules implementing a link info type.
char * io_name
Name of link info type, must match name on kernel side.