20#include <netlink-private/netlink.h>
21#include <netlink/netlink.h>
22#include <netlink/attr.h>
23#include <netlink/utils.h>
24#include <netlink/object.h>
25#include <netlink/route/rtnl.h>
26#include <netlink-private/route/link/api.h>
27#include <netlink/route/link/macvlan.h>
28#include <netlink/route/link/macvtap.h>
30#include <linux/if_link.h>
33#define MACVLAN_HAS_MODE (1<<0)
34#define MACVLAN_HAS_FLAGS (1<<1)
35#define MACVLAN_HAS_MACADDR (1<<2)
42 uint32_t mvi_maccount;
44 struct nl_addr **mvi_macaddr;
49static struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX+1] = {
51 [IFLA_MACVLAN_FLAGS] = { .type =
NLA_U16 },
52 [IFLA_MACVLAN_MACADDR_MODE] = { .type =
NLA_U32 },
53 [IFLA_MACVLAN_MACADDR] = { .type =
NLA_UNSPEC },
54 [IFLA_MACVLAN_MACADDR_DATA] = { .type =
NLA_NESTED },
55 [IFLA_MACVLAN_MACADDR_COUNT] = { .type =
NLA_U32 },
58static int macvlan_alloc(
struct rtnl_link *link)
60 struct macvlan_info *mvi;
65 for (i = 0; i < mvi->mvi_maccount; i++)
67 free(mvi->mvi_macaddr);
68 memset(mvi, 0,
sizeof(*mvi));
70 if ((mvi = calloc(1,
sizeof(*mvi))) == NULL)
75 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
80static int macvlan_parse(
struct rtnl_link *link,
struct nlattr *data,
81 struct nlattr *xstats)
83 struct nlattr *tb[IFLA_MACVLAN_MAX+1];
84 struct macvlan_info *mvi;
89 NL_DBG(3,
"Parsing %s link info", link->l_info_ops->io_name);
91 if ((err =
nla_parse_nested(tb, IFLA_MACVLAN_MAX, data, macvlan_policy)) < 0)
94 if ((err = macvlan_alloc(link)) < 0)
99 if (tb[IFLA_MACVLAN_MODE]) {
100 mvi->mvi_mode =
nla_get_u32(tb[IFLA_MACVLAN_MODE]);
101 mvi->mvi_mask |= MACVLAN_HAS_MODE;
104 if (tb[IFLA_MACVLAN_FLAGS]) {
105 mvi->mvi_flags =
nla_get_u16(tb[IFLA_MACVLAN_FLAGS]);
106 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
109 if ( tb[IFLA_MACVLAN_MACADDR_COUNT]
110 && tb[IFLA_MACVLAN_MACADDR_DATA]) {
111 mvi->mvi_maccount =
nla_get_u32(tb[IFLA_MACVLAN_MACADDR_COUNT]);
112 if (mvi->mvi_maccount > 0) {
115 nla =
nla_data(tb[IFLA_MACVLAN_MACADDR_DATA]);
116 len =
nla_len(tb[IFLA_MACVLAN_MACADDR_DATA]);
118 mvi->mvi_macaddr = calloc(mvi->mvi_maccount,
119 sizeof(*(mvi->mvi_macaddr)));
120 if (mvi->mvi_macaddr == NULL) {
127 if (i >= mvi->mvi_maccount)
129 if (
nla_type(nla) != IFLA_MACVLAN_MACADDR ||
136 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
144static void macvlan_free(
struct rtnl_link *link)
146 struct macvlan_info *mvi;
153 for (i = 0; i < mvi->mvi_maccount; i++)
155 free(mvi->mvi_macaddr);
165 struct macvlan_info *mvi = link->l_info;
167 if (mvi->mvi_mask & MACVLAN_HAS_MODE) {
168 rtnl_link_macvlan_mode2str(mvi->mvi_mode, buf,
sizeof(buf));
169 nl_dump(p,
" %s-mode %s", link->l_info_ops->io_name, buf);
172 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS) {
173 rtnl_link_macvlan_flags2str(mvi->mvi_flags, buf,
sizeof(buf));
174 nl_dump(p,
" %s-flags %s", link->l_info_ops->io_name, buf);
177 if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
178 nl_dump(p,
" macvlan-count %u", (
unsigned) mvi->mvi_maccount);
180 if (mvi->mvi_maccount)
181 nl_dump(p,
" macvlan-sourcemac");
183 for (i = 0; i < mvi->mvi_maccount; i++) {
193 struct macvlan_info *vdst, *vsrc = src->l_info;
205 memcpy(vdst, vsrc,
sizeof(
struct macvlan_info));
207 if ( vsrc->mvi_mask & MACVLAN_HAS_MACADDR
208 && vsrc->mvi_maccount > 0) {
209 vdst->mvi_macaddr = calloc(vdst->mvi_maccount,
210 sizeof(*(vdst->mvi_macaddr)));
211 for (i = 0; i < vdst->mvi_maccount; i++)
214 vdst->mvi_macaddr = NULL;
219static int macvlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
221 struct macvlan_info *mvi = link->l_info;
222 struct nlattr *data, *datamac = NULL;
230 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
231 NLA_PUT_U32(msg, IFLA_MACVLAN_MODE, mvi->mvi_mode);
233 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS)
234 NLA_PUT_U16(msg, IFLA_MACVLAN_FLAGS, mvi->mvi_flags);
236 if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
237 NLA_PUT_U32(msg, IFLA_MACVLAN_MACADDR_MODE, mvi->mvi_macmode);
240 goto nla_put_failure;
242 for (i = 0; i < mvi->mvi_maccount; i++) {
244 mvi->mvi_macaddr[i]);
259static struct rtnl_link_info_ops macvlan_info_ops = {
260 .io_name =
"macvlan",
261 .io_alloc = macvlan_alloc,
262 .io_parse = macvlan_parse,
266 .io_clone = macvlan_clone,
267 .io_put_attrs = macvlan_put_attrs,
268 .io_free = macvlan_free,
271static struct rtnl_link_info_ops macvtap_info_ops = {
272 .io_name =
"macvtap",
273 .io_alloc = macvlan_alloc,
274 .io_parse = macvlan_parse,
278 .io_clone = macvlan_clone,
279 .io_put_attrs = macvlan_put_attrs,
280 .io_free = macvlan_free,
284#define IS_MACVLAN_LINK_ASSERT(link) \
285 if ((link)->l_info_ops != &macvlan_info_ops) { \
286 APPBUG("Link is not a macvlan link. set type \"macvlan\" first."); \
287 return -NLE_OPNOTSUPP; \
290#define IS_MACVTAP_LINK_ASSERT(link) \
291 if ((link)->l_info_ops != &macvtap_info_ops) { \
292 APPBUG("Link is not a macvtap link. set type \"macvtap\" first."); \
293 return -NLE_OPNOTSUPP; \
330 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvlan");
342 struct macvlan_info *mvi = link->l_info;
345 IS_MACVLAN_LINK_ASSERT(link);
347 mvi->mvi_mode = mode;
348 mvi->mvi_mask |= MACVLAN_HAS_MODE;
350 if (mode != MACVLAN_MODE_SOURCE) {
351 for (i = 0; i < mvi->mvi_maccount; i++)
353 free(mvi->mvi_macaddr);
354 mvi->mvi_maccount = 0;
355 mvi->mvi_macaddr = NULL;
356 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
357 mvi->mvi_mask &= ~MACVLAN_HAS_MACADDR;
371 struct macvlan_info *mvi = link->l_info;
373 IS_MACVLAN_LINK_ASSERT(link);
375 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
376 return mvi->mvi_mode;
392 struct macvlan_info *mvi = link->l_info;
394 IS_MACVLAN_LINK_ASSERT(link);
396 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
397 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
400 mvi->mvi_macmode = macmode;
401 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
417 struct macvlan_info *mvi = link->l_info;
419 IS_MACVLAN_LINK_ASSERT(link);
421 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
422 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
425 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
428 *out_macmode = mvi->mvi_macmode;
442 struct macvlan_info *mvi = link->l_info;
444 IS_MACVLAN_LINK_ASSERT(link);
446 mvi->mvi_flags |= flags;
447 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
464 struct macvlan_info *mvi = link->l_info;
466 IS_MACVLAN_LINK_ASSERT(link);
468 mvi->mvi_flags &= ~flags;
469 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
482 struct macvlan_info *mvi = link->l_info;
484 IS_MACVLAN_LINK_ASSERT(link);
486 return mvi->mvi_flags;
498 struct macvlan_info *mvi = link->l_info;
500 IS_MACVLAN_LINK_ASSERT(link);
502 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
503 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
506 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
509 *out_count = mvi->mvi_maccount;
526 const struct nl_addr **out_addr)
528 struct macvlan_info *mvi = link->l_info;
530 IS_MACVLAN_LINK_ASSERT(link);
532 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
533 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
536 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
539 if (idx >= mvi->mvi_maccount)
542 *out_addr = mvi->mvi_macaddr[idx];
557 struct macvlan_info *mvi = link->l_info;
558 struct nl_addr **mvi_macaddr;
561 IS_MACVLAN_LINK_ASSERT(link);
566 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
567 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
570 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
573 if (mvi->mvi_maccount >= UINT32_MAX)
576 newsize = (mvi->mvi_maccount + 1) *
sizeof(*(mvi->mvi_macaddr));
577 mvi_macaddr = realloc(mvi->mvi_macaddr, newsize);
581 mvi->mvi_macaddr = mvi_macaddr;
585 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
602 struct macvlan_info *mvi = link->l_info;
605 IS_MACVLAN_LINK_ASSERT(link);
610 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
611 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
614 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
620 while (i + found < mvi->mvi_maccount) {
621 mvi->mvi_macaddr[i] = mvi->mvi_macaddr[i + found];
623 mvi->mvi_macaddr[i + found] = NULL;
626 mvi->mvi_macaddr[i] = NULL;
634 mvi->mvi_maccount -= found;
636 return found > INT_MAX ? INT_MAX : (int) found;
675 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvtap");
687 struct macvlan_info *mvi = link->l_info;
689 IS_MACVTAP_LINK_ASSERT(link);
691 mvi->mvi_mode = mode;
692 mvi->mvi_mask |= MACVLAN_HAS_MODE;
705 struct macvlan_info *mvi = link->l_info;
707 IS_MACVTAP_LINK_ASSERT(link);
709 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
710 return mvi->mvi_mode;
724 struct macvlan_info *mvi = link->l_info;
726 IS_MACVTAP_LINK_ASSERT(link);
728 mvi->mvi_flags |= flags;
729 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
746 struct macvlan_info *mvi = link->l_info;
748 IS_MACVTAP_LINK_ASSERT(link);
750 mvi->mvi_flags &= ~flags;
751 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
764 struct macvlan_info *mvi = link->l_info;
766 IS_MACVTAP_LINK_ASSERT(link);
768 return mvi->mvi_flags;
774static const struct trans_tbl macvlan_flags[] = {
775 __ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
778static const struct trans_tbl macvlan_modes[] = {
779 __ADD(MACVLAN_MODE_PRIVATE,
private),
780 __ADD(MACVLAN_MODE_VEPA, vepa),
781 __ADD(MACVLAN_MODE_BRIDGE, bridge),
782 __ADD(MACVLAN_MODE_PASSTHRU, passthru),
783 __ADD(MACVLAN_MODE_SOURCE, source),
786static const struct trans_tbl macvlan_macmodes[] = {
787 __ADD(MACVLAN_MACADDR_ADD,
"add"),
788 __ADD(MACVLAN_MACADDR_DEL,
"del"),
789 __ADD(MACVLAN_MACADDR_SET,
"set"),
790 __ADD(MACVLAN_MACADDR_FLUSH,
"flush"),
798char *rtnl_link_macvlan_flags2str(
int flags,
char *buf,
size_t len)
800 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
803int rtnl_link_macvlan_str2flags(
const char *name)
805 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
808char *rtnl_link_macvtap_flags2str(
int flags,
char *buf,
size_t len)
810 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
813int rtnl_link_macvtap_str2flags(
const char *name)
815 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
825char *rtnl_link_macvlan_mode2str(
int mode,
char *buf,
size_t len)
827 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
830int rtnl_link_macvlan_str2mode(
const char *name)
832 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
835char *rtnl_link_macvlan_macmode2str(
int mode,
char *buf,
size_t len)
837 return __type2str(mode, buf, len, macvlan_macmodes,
838 ARRAY_SIZE(macvlan_macmodes));
841int rtnl_link_macvlan_str2macmode(
const char *name)
843 return __str2type(name, macvlan_macmodes, ARRAY_SIZE(macvlan_macmodes));
846char *rtnl_link_macvtap_mode2str(
int mode,
char *buf,
size_t len)
848 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
851int rtnl_link_macvtap_str2mode(
const char *name)
853 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
858static void __init macvlan_init(
void)
864static void __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)
Return 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.