26 #include <netlink-private/netlink.h> 27 #include <netlink/netlink.h> 28 #include <netlink/attr.h> 29 #include <netlink/utils.h> 30 #include <netlink/object.h> 31 #include <netlink/route/rtnl.h> 32 #include <netlink-private/route/link/api.h> 33 #include <netlink/route/link/macvlan.h> 34 #include <netlink/route/link/macvtap.h> 36 #include <linux/if_link.h> 39 #define MACVLAN_HAS_MODE (1<<0) 40 #define MACVLAN_HAS_FLAGS (1<<1) 41 #define MACVLAN_HAS_MACADDR (1<<2) 48 uint32_t mvi_maccount;
50 struct nl_addr **mvi_macaddr;
55 static struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX+1] = {
57 [IFLA_MACVLAN_FLAGS] = { .type =
NLA_U16 },
58 [IFLA_MACVLAN_MACADDR_MODE] = { .type =
NLA_U32 },
59 [IFLA_MACVLAN_MACADDR] = { .type =
NLA_UNSPEC },
60 [IFLA_MACVLAN_MACADDR_DATA] = { .type =
NLA_NESTED },
61 [IFLA_MACVLAN_MACADDR_COUNT] = { .type =
NLA_U32 },
64 static int macvlan_alloc(
struct rtnl_link *link)
66 struct macvlan_info *mvi;
71 for (i = 0; i < mvi->mvi_maccount; i++)
73 free(mvi->mvi_macaddr);
74 memset(mvi, 0,
sizeof(*mvi));
76 if ((mvi = calloc(1,
sizeof(*mvi))) == NULL)
81 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
86 static int macvlan_parse(
struct rtnl_link *link,
struct nlattr *data,
87 struct nlattr *xstats)
89 struct nlattr *tb[IFLA_MACVLAN_MAX+1];
90 struct macvlan_info *mvi;
95 NL_DBG(3,
"Parsing %s link info", link->l_info_ops->io_name);
97 if ((err =
nla_parse_nested(tb, IFLA_MACVLAN_MAX, data, macvlan_policy)) < 0)
100 if ((err = macvlan_alloc(link)) < 0)
105 if (tb[IFLA_MACVLAN_MODE]) {
106 mvi->mvi_mode =
nla_get_u32(tb[IFLA_MACVLAN_MODE]);
107 mvi->mvi_mask |= MACVLAN_HAS_MODE;
110 if (tb[IFLA_MACVLAN_FLAGS]) {
111 mvi->mvi_mode =
nla_get_u16(tb[IFLA_MACVLAN_FLAGS]);
112 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
115 if ( tb[IFLA_MACVLAN_MACADDR_COUNT]
116 && tb[IFLA_MACVLAN_MACADDR_DATA]) {
117 mvi->mvi_maccount =
nla_get_u32(tb[IFLA_MACVLAN_MACADDR_COUNT]);
118 if (mvi->mvi_maccount > 0) {
121 nla =
nla_data(tb[IFLA_MACVLAN_MACADDR_DATA]);
122 len =
nla_len(tb[IFLA_MACVLAN_MACADDR_DATA]);
124 mvi->mvi_macaddr = calloc(mvi->mvi_maccount,
125 sizeof(*(mvi->mvi_macaddr)));
129 if (i >= mvi->mvi_maccount)
131 if (
nla_type(nla) != IFLA_MACVLAN_MACADDR ||
138 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
146 static void macvlan_free(
struct rtnl_link *link)
148 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 for (i = 0; i < mvi->mvi_maccount; i++) {
181 nl_dump(p,
"macvlan-sourcemac %s",
190 struct macvlan_info *vdst, *vsrc = src->l_info;
202 memcpy(vdst, vsrc,
sizeof(
struct macvlan_info));
204 if ( vsrc->mvi_mask & MACVLAN_HAS_MACADDR
205 && vsrc->mvi_maccount > 0) {
206 vdst->mvi_macaddr = calloc(vdst->mvi_maccount,
207 sizeof(*(vdst->mvi_macaddr)));
208 for (i = 0; i < vdst->mvi_maccount; i++)
211 vdst->mvi_macaddr = NULL;
216 static int macvlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
218 struct macvlan_info *mvi = link->l_info;
219 struct nlattr *data, *datamac = NULL;
227 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
228 NLA_PUT_U32(msg, IFLA_MACVLAN_MODE, mvi->mvi_mode);
230 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS)
231 NLA_PUT_U16(msg, IFLA_MACVLAN_FLAGS, mvi->mvi_flags);
233 if (mvi->mvi_mask & MACVLAN_HAS_MACADDR) {
234 NLA_PUT_U32(msg, IFLA_MACVLAN_MACADDR_MODE, mvi->mvi_macmode);
237 goto nla_put_failure;
239 for (i = 0; i < mvi->mvi_maccount; i++) {
241 mvi->mvi_macaddr[i]);
256 static struct rtnl_link_info_ops macvlan_info_ops = {
257 .io_name =
"macvlan",
258 .io_alloc = macvlan_alloc,
259 .io_parse = macvlan_parse,
264 .io_clone = macvlan_clone,
265 .io_put_attrs = macvlan_put_attrs,
266 .io_free = macvlan_free,
269 static struct rtnl_link_info_ops macvtap_info_ops = {
270 .io_name =
"macvtap",
271 .io_alloc = macvlan_alloc,
272 .io_parse = macvlan_parse,
277 .io_clone = macvlan_clone,
278 .io_put_attrs = macvlan_put_attrs,
279 .io_free = macvlan_free,
283 #define IS_MACVLAN_LINK_ASSERT(link) \ 284 if ((link)->l_info_ops != &macvlan_info_ops) { \ 285 APPBUG("Link is not a macvlan link. set type \"macvlan\" first."); \ 286 return -NLE_OPNOTSUPP; \ 289 #define IS_MACVTAP_LINK_ASSERT(link) \ 290 if ((link)->l_info_ops != &macvtap_info_ops) { \ 291 APPBUG("Link is not a macvtap link. set type \"macvtap\" first."); \ 292 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;
676 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvtap");
688 struct macvlan_info *mvi = link->l_info;
690 IS_MACVTAP_LINK_ASSERT(link);
692 mvi->mvi_mode = mode;
693 mvi->mvi_mask |= MACVLAN_HAS_MODE;
706 struct macvlan_info *mvi = link->l_info;
708 IS_MACVTAP_LINK_ASSERT(link);
710 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
711 return mvi->mvi_mode;
725 struct macvlan_info *mvi = link->l_info;
727 IS_MACVTAP_LINK_ASSERT(link);
729 mvi->mvi_flags |= flags;
730 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
747 struct macvlan_info *mvi = link->l_info;
749 IS_MACVTAP_LINK_ASSERT(link);
751 mvi->mvi_flags &= ~flags;
752 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
765 struct macvlan_info *mvi = link->l_info;
767 IS_MACVTAP_LINK_ASSERT(link);
769 return mvi->mvi_flags;
775 static const struct trans_tbl macvlan_flags[] = {
776 __ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
779 static const struct trans_tbl macvlan_modes[] = {
780 __ADD(MACVLAN_MODE_PRIVATE,
private),
781 __ADD(MACVLAN_MODE_VEPA, vepa),
782 __ADD(MACVLAN_MODE_BRIDGE, bridge),
783 __ADD(MACVLAN_MODE_PASSTHRU, passthru),
784 __ADD(MACVLAN_MODE_SOURCE, source),
787 static const struct trans_tbl macvlan_macmodes[] = {
788 __ADD(MACVLAN_MACADDR_ADD,
"add"),
789 __ADD(MACVLAN_MACADDR_DEL,
"del"),
790 __ADD(MACVLAN_MACADDR_SET,
"set"),
791 __ADD(MACVLAN_MACADDR_FLUSH,
"flush"),
799 char *rtnl_link_macvlan_flags2str(
int flags,
char *buf,
size_t len)
801 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
804 int rtnl_link_macvlan_str2flags(
const char *name)
806 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
809 char *rtnl_link_macvtap_flags2str(
int flags,
char *buf,
size_t len)
811 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
814 int rtnl_link_macvtap_str2flags(
const char *name)
816 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
826 char *rtnl_link_macvlan_mode2str(
int mode,
char *buf,
size_t len)
828 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
831 int rtnl_link_macvlan_str2mode(
const char *name)
833 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
836 char *rtnl_link_macvlan_macmode2str(
int mode,
char *buf,
size_t len)
838 return __type2str(mode, buf, len, macvlan_macmodes,
839 ARRAY_SIZE(macvlan_macmodes));
842 int rtnl_link_macvlan_str2macmode(
const char *name)
844 return __str2type(name, macvlan_macmodes, ARRAY_SIZE(macvlan_macmodes));
847 char *rtnl_link_macvtap_mode2str(
int mode,
char *buf,
size_t len)
849 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
852 int rtnl_link_macvtap_str2mode(
const char *name)
854 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
859 static void __init macvlan_init(
void)
865 static void __exit macvlan_exit(
void)
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Dump object briefly on one line.
int rtnl_link_macvlan_get_macmode(struct rtnl_link *link, uint32_t *out_macmode)
Get MACVLAN MACMODE.
int nla_ok(const struct nlattr *nla, int remaining)
Check if the attribute header and payload can be accessed safely.
int rtnl_link_is_macvlan(struct rtnl_link *link)
Check if link is a MACVLAN link.
int rtnl_link_macvlan_unset_flags(struct rtnl_link *link, uint16_t flags)
Unset MACVLAN flags.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
uint16_t rtnl_link_macvlan_get_flags(struct rtnl_link *link)
Get MACVLAN flags.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Attribute validation policy.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
Unspecified type, binary data chunk.
uint32_t rtnl_link_macvlan_get_mode(struct rtnl_link *link)
Get MACVLAN Mode.
int rtnl_link_macvlan_del_macaddr(struct rtnl_link *link, struct nl_addr *addr)
Remove MAC-Addr from MACVLAN device in source mode.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
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.
int rtnl_link_macvlan_set_macmode(struct rtnl_link *link, uint32_t macmode)
Set MACVLAN MACMODE.
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 rtnl_link_macvtap_unset_flags(struct rtnl_link *link, uint16_t flags)
Unset MACVTAP flags.
Dump all attributes but no statistics.
int rtnl_link_is_macvtap(struct rtnl_link *link)
Check if link is a MACVTAP link.
struct rtnl_link * rtnl_link_macvtap_alloc(void)
Allocate link object of type MACVTAP.
int rtnl_link_macvlan_set_flags(struct rtnl_link *link, uint16_t flags)
Set MACVLAN flags.
int rtnl_link_macvlan_set_mode(struct rtnl_link *link, uint32_t mode)
Set MACVLAN MODE.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
struct nlattr * nla_next(const struct nlattr *nla, int *remaining)
Return next attribute in a stream of attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_type(const struct nlattr *nla)
Return type of the attribute.
uint16_t rtnl_link_macvtap_get_flags(struct rtnl_link *link)
Get MACVTAP flags.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int rtnl_link_macvtap_set_flags(struct rtnl_link *link, uint16_t flags)
Set MACVTAP flags.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
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.
uint16_t type
Type of attribute or NLA_UNSPEC.
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.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
uint32_t rtnl_link_macvtap_get_mode(struct rtnl_link *link)
Get MACVTAP Mode.
int rtnl_link_macvtap_set_mode(struct rtnl_link *link, uint32_t mode)
Set MACVTAP MODE.
struct rtnl_link * rtnl_link_macvlan_alloc(void)
Allocate link object of type MACVLAN.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
int rtnl_link_macvlan_add_macaddr(struct rtnl_link *link, struct nl_addr *addr)
Add MAC-Addr to MACVLAN device in source mode.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.