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_flags =
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 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;
219 static 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]);
259 static 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,
271 static 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; \ 331 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvlan");
343 struct macvlan_info *mvi = link->l_info;
346 IS_MACVLAN_LINK_ASSERT(link);
348 mvi->mvi_mode = mode;
349 mvi->mvi_mask |= MACVLAN_HAS_MODE;
351 if (mode != MACVLAN_MODE_SOURCE) {
352 for (i = 0; i < mvi->mvi_maccount; i++)
354 free(mvi->mvi_macaddr);
355 mvi->mvi_maccount = 0;
356 mvi->mvi_macaddr = NULL;
357 mvi->mvi_macmode = MACVLAN_MACADDR_SET;
358 mvi->mvi_mask &= ~MACVLAN_HAS_MACADDR;
372 struct macvlan_info *mvi = link->l_info;
374 IS_MACVLAN_LINK_ASSERT(link);
376 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
377 return mvi->mvi_mode;
393 struct macvlan_info *mvi = link->l_info;
395 IS_MACVLAN_LINK_ASSERT(link);
397 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
398 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
401 mvi->mvi_macmode = macmode;
402 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
418 struct macvlan_info *mvi = link->l_info;
420 IS_MACVLAN_LINK_ASSERT(link);
422 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
423 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
426 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
429 *out_macmode = mvi->mvi_macmode;
443 struct macvlan_info *mvi = link->l_info;
445 IS_MACVLAN_LINK_ASSERT(link);
447 mvi->mvi_flags |= flags;
448 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
465 struct macvlan_info *mvi = link->l_info;
467 IS_MACVLAN_LINK_ASSERT(link);
469 mvi->mvi_flags &= ~flags;
470 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
483 struct macvlan_info *mvi = link->l_info;
485 IS_MACVLAN_LINK_ASSERT(link);
487 return mvi->mvi_flags;
499 struct macvlan_info *mvi = link->l_info;
501 IS_MACVLAN_LINK_ASSERT(link);
503 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
504 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
507 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
510 *out_count = mvi->mvi_maccount;
527 const struct nl_addr **out_addr)
529 struct macvlan_info *mvi = link->l_info;
531 IS_MACVLAN_LINK_ASSERT(link);
533 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
534 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
537 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
540 if (idx >= mvi->mvi_maccount)
543 *out_addr = mvi->mvi_macaddr[idx];
558 struct macvlan_info *mvi = link->l_info;
559 struct nl_addr **mvi_macaddr;
562 IS_MACVLAN_LINK_ASSERT(link);
567 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
568 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
571 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
574 if (mvi->mvi_maccount >= UINT32_MAX)
577 newsize = (mvi->mvi_maccount + 1) *
sizeof(*(mvi->mvi_macaddr));
578 mvi_macaddr = realloc(mvi->mvi_macaddr, newsize);
582 mvi->mvi_macaddr = mvi_macaddr;
586 mvi->mvi_mask |= MACVLAN_HAS_MACADDR;
603 struct macvlan_info *mvi = link->l_info;
606 IS_MACVLAN_LINK_ASSERT(link);
611 if (!(mvi->mvi_mask & MACVLAN_HAS_MODE) ||
612 (mvi->mvi_mode != MACVLAN_MODE_SOURCE))
615 if (!(mvi->mvi_mask & MACVLAN_HAS_MACADDR))
621 while (i + found < mvi->mvi_maccount) {
622 mvi->mvi_macaddr[i] = mvi->mvi_macaddr[i + found];
624 mvi->mvi_macaddr[i + found] = NULL;
627 mvi->mvi_macaddr[i] = NULL;
635 mvi->mvi_maccount -= found;
637 return found > INT_MAX ? INT_MAX : (int) found;
677 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvtap");
689 struct macvlan_info *mvi = link->l_info;
691 IS_MACVTAP_LINK_ASSERT(link);
693 mvi->mvi_mode = mode;
694 mvi->mvi_mask |= MACVLAN_HAS_MODE;
707 struct macvlan_info *mvi = link->l_info;
709 IS_MACVTAP_LINK_ASSERT(link);
711 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
712 return mvi->mvi_mode;
726 struct macvlan_info *mvi = link->l_info;
728 IS_MACVTAP_LINK_ASSERT(link);
730 mvi->mvi_flags |= flags;
731 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
748 struct macvlan_info *mvi = link->l_info;
750 IS_MACVTAP_LINK_ASSERT(link);
752 mvi->mvi_flags &= ~flags;
753 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
766 struct macvlan_info *mvi = link->l_info;
768 IS_MACVTAP_LINK_ASSERT(link);
770 return mvi->mvi_flags;
776 static const struct trans_tbl macvlan_flags[] = {
777 __ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
780 static const struct trans_tbl macvlan_modes[] = {
781 __ADD(MACVLAN_MODE_PRIVATE,
private),
782 __ADD(MACVLAN_MODE_VEPA, vepa),
783 __ADD(MACVLAN_MODE_BRIDGE, bridge),
784 __ADD(MACVLAN_MODE_PASSTHRU, passthru),
785 __ADD(MACVLAN_MODE_SOURCE, source),
788 static const struct trans_tbl macvlan_macmodes[] = {
789 __ADD(MACVLAN_MACADDR_ADD,
"add"),
790 __ADD(MACVLAN_MACADDR_DEL,
"del"),
791 __ADD(MACVLAN_MACADDR_SET,
"set"),
792 __ADD(MACVLAN_MACADDR_FLUSH,
"flush"),
800 char *rtnl_link_macvlan_flags2str(
int flags,
char *buf,
size_t len)
802 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
805 int rtnl_link_macvlan_str2flags(
const char *name)
807 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
810 char *rtnl_link_macvtap_flags2str(
int flags,
char *buf,
size_t len)
812 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
815 int rtnl_link_macvtap_str2flags(
const char *name)
817 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
827 char *rtnl_link_macvlan_mode2str(
int mode,
char *buf,
size_t len)
829 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
832 int rtnl_link_macvlan_str2mode(
const char *name)
834 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
837 char *rtnl_link_macvlan_macmode2str(
int mode,
char *buf,
size_t len)
839 return __type2str(mode, buf, len, macvlan_macmodes,
840 ARRAY_SIZE(macvlan_macmodes));
843 int rtnl_link_macvlan_str2macmode(
const char *name)
845 return __str2type(name, macvlan_macmodes, ARRAY_SIZE(macvlan_macmodes));
848 char *rtnl_link_macvtap_mode2str(
int mode,
char *buf,
size_t len)
850 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
853 int rtnl_link_macvtap_str2mode(
const char *name)
855 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
860 static void __init macvlan_init(
void)
866 static void __exit macvlan_exit(
void)
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
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, const 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.