19#include "nl-default.h"
21#include <linux/if_vlan.h>
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/vlan.h>
34#define VLAN_HAS_ID (1<<0)
35#define VLAN_HAS_FLAGS (1<<1)
36#define VLAN_HAS_INGRESS_QOS (1<<2)
37#define VLAN_HAS_EGRESS_QOS (1<<3)
38#define VLAN_HAS_PROTOCOL (1<<4)
44 unsigned int vi_ingress_qos_mask:(VLAN_PRIO_MAX+1);
46 uint32_t vi_flags_mask;
47 uint32_t vi_ingress_qos[VLAN_PRIO_MAX+1];
49 uint32_t vi_egress_size;
59 [IFLA_VLAN_INGRESS_QOS] = { .type =
NLA_NESTED },
60 [IFLA_VLAN_EGRESS_QOS] = { .type =
NLA_NESTED },
61 [IFLA_VLAN_PROTOCOL] = { .type =
NLA_U16 },
64static int vlan_alloc(
struct rtnl_link *link)
70 free(vi->vi_egress_qos);
71 memset(link->l_info, 0,
sizeof(*vi));
73 if ((vi = calloc(1,
sizeof(*vi))) == NULL)
82static int vlan_parse(
struct rtnl_link *link,
struct nlattr *data,
83 struct nlattr *xstats)
85 struct nlattr *tb[IFLA_VLAN_MAX+1];
89 NL_DBG(3,
"Parsing VLAN link info\n");
94 if ((err = vlan_alloc(link)) < 0)
99 if (tb[IFLA_VLAN_ID]) {
101 vi->vi_mask |= VLAN_HAS_ID;
104 if (tb[IFLA_VLAN_PROTOCOL]) {
105 vi->vi_protocol =
nla_get_u16(tb[IFLA_VLAN_PROTOCOL]);
106 vi->vi_mask |= VLAN_HAS_PROTOCOL;
109 if (tb[IFLA_VLAN_FLAGS]) {
110 struct ifla_vlan_flags flags;
111 nla_memcpy(&flags, tb[IFLA_VLAN_FLAGS],
sizeof(flags));
113 vi->vi_flags = flags.flags;
114 vi->vi_mask |= VLAN_HAS_FLAGS;
117 if (tb[IFLA_VLAN_INGRESS_QOS]) {
118 struct ifla_vlan_qos_mapping *map;
122 vi->vi_ingress_qos_mask = 0;
123 memset(vi->vi_ingress_qos, 0,
sizeof(vi->vi_ingress_qos));
126 if (_nla_len(nla) <
sizeof(*map))
130 if (map->from > VLAN_PRIO_MAX) {
144 vi->vi_ingress_qos_mask |= (1 << map->from);
145 vi->vi_ingress_qos[map->from] = map->to;
148 vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
151 if (tb[IFLA_VLAN_EGRESS_QOS]) {
152 struct ifla_vlan_qos_mapping *map;
154 int remaining, i = 0;
157 if (_nla_len(nla) <
sizeof(*map))
163 vi->vi_egress_size = (i + 32) & ~31;
164 vi->vi_egress_qos = calloc(vi->vi_egress_size,
sizeof(*vi->vi_egress_qos));
165 if (vi->vi_egress_qos == NULL)
171 NL_DBG(4,
"Assigning egress qos mapping %d\n", i);
172 vi->vi_egress_qos[i].vm_from = map->from;
173 vi->vi_egress_qos[i++].vm_to = map->to;
177 vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
185static void vlan_free(
struct rtnl_link *link)
187 struct vlan_info *vi = link->l_info;
190 free(vi->vi_egress_qos);
191 vi->vi_egress_qos = NULL;
200 struct vlan_info *vi = link->l_info;
202 nl_dump(p,
"vlan-id %d", vi->vi_vlan_id);
207 struct vlan_info *vi = link->l_info;
212 rtnl_link_vlan_flags2str(vi->vi_flags, buf,
sizeof(buf));
213 nl_dump_line(p,
" vlan-info id %d <%s>", vi->vi_vlan_id, buf);
215 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
216 nl_dump_line(p,
" vlan protocol <%d>", ntohs(vi->vi_protocol));
220 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
222 " ingress vlan prio -> qos/socket prio mapping:\n");
223 for (i = 0, printed = 0; i <= VLAN_PRIO_MAX; i++) {
224 if (vi->vi_ingress_qos_mask & (1 << i)) {
226 nl_dump_line(p,
" ");
228 i, vi->vi_ingress_qos[i]);
229 if (printed++ == 3) {
236 if (printed > 0 && printed != 4)
240 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
242 " egress qos/socket prio -> vlan prio mapping:\n");
243 for (i = 0, printed = 0; i < vi->vi_negress; i++) {
245 nl_dump_line(p,
" ");
247 vi->vi_egress_qos[i].vm_from,
248 vi->vi_egress_qos[i].vm_to);
249 if (printed++ == 3) {
255 if (printed > 0 && printed != 4)
262 struct vlan_info *vdst, *vsrc = src->l_info;
271 if (vsrc->vi_negress) {
272 p = calloc(vsrc->vi_negress,
280 if (vsrc->vi_negress) {
281 vdst->vi_egress_size = vsrc->vi_negress;
282 vdst->vi_egress_qos = p;
283 memcpy(vdst->vi_egress_qos, vsrc->vi_egress_qos,
284 vsrc->vi_negress *
sizeof(
struct vlan_map));
290static int vlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
292 struct vlan_info *vi = link->l_info;
297 goto nla_put_failure;
301 if (vi->vi_mask & VLAN_HAS_ID)
304 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
305 NLA_PUT_U16(msg, IFLA_VLAN_PROTOCOL, vi->vi_protocol);
307 if (vi->vi_mask & VLAN_HAS_FLAGS) {
308 struct ifla_vlan_flags flags = {
309 .flags = vi->vi_flags,
310 .mask = vi->vi_flags_mask,
313 NLA_PUT(msg, IFLA_VLAN_FLAGS,
sizeof(flags), &flags);
316 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
317 struct ifla_vlan_qos_mapping map;
322 goto nla_put_failure;
326 for (i = 0; i <= VLAN_PRIO_MAX; i++) {
327 if (vi->vi_ingress_qos_mask & (1 << i)) {
329 map.to = vi->vi_ingress_qos[i];
331 NLA_PUT(msg, i,
sizeof(map), &map);
339 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
340 struct ifla_vlan_qos_mapping map;
345 goto nla_put_failure;
349 for (i = 0; i < vi->vi_negress; i++) {
350 map.from = vi->vi_egress_qos[i].vm_from;
351 map.to = vi->vi_egress_qos[i].vm_to;
353 NLA_PUT(msg, i,
sizeof(map), &map);
364 for (; nest_count > 0; nest_count--)
371 .io_alloc = vlan_alloc,
372 .io_parse = vlan_parse,
377 .io_clone = vlan_clone,
378 .io_put_attrs = vlan_put_attrs,
379 .io_free = vlan_free,
383#define IS_VLAN_LINK_ASSERT(link) \
384 if ((link)->l_info_ops != &vlan_info_ops) { \
385 APPBUG("Link is not a vlan link. set type \"vlan\" first."); \
386 return -NLE_OPNOTSUPP; \
423 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"vlan");
435 struct vlan_info *vi = link->l_info;
437 IS_VLAN_LINK_ASSERT(link);
440 vi->vi_mask |= VLAN_HAS_ID;
453 struct vlan_info *vi = link->l_info;
455 IS_VLAN_LINK_ASSERT(link);
457 if (vi->vi_mask & VLAN_HAS_ID)
458 return vi->vi_vlan_id;
473 struct vlan_info *vi = link->l_info;
475 IS_VLAN_LINK_ASSERT(link);
477 vi->vi_protocol = protocol;
478 vi->vi_mask |= VLAN_HAS_PROTOCOL;
492 struct vlan_info *vi = link->l_info;
494 IS_VLAN_LINK_ASSERT(link);
496 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
497 return vi->vi_protocol;
511 struct vlan_info *vi = link->l_info;
513 IS_VLAN_LINK_ASSERT(link);
515 vi->vi_flags_mask |= flags;
516 vi->vi_flags |= flags;
517 vi->vi_mask |= VLAN_HAS_FLAGS;
531 struct vlan_info *vi = link->l_info;
533 IS_VLAN_LINK_ASSERT(link);
535 vi->vi_flags_mask |= flags;
536 vi->vi_flags &= ~flags;
537 vi->vi_mask |= VLAN_HAS_FLAGS;
550 struct vlan_info *vi = link->l_info;
552 IS_VLAN_LINK_ASSERT(link);
564int rtnl_link_vlan_set_ingress_map(
struct rtnl_link *link,
int from,
567 struct vlan_info *vi = link->l_info;
569 IS_VLAN_LINK_ASSERT(link);
571 if (from < 0 || from > VLAN_PRIO_MAX)
574 vi->vi_ingress_qos_mask |= (1 << from);
575 vi->vi_ingress_qos[from] = to;
576 vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
581uint32_t *rtnl_link_vlan_get_ingress_map(
struct rtnl_link *link)
583 struct vlan_info *vi = link->l_info;
585 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
588 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS)
589 return vi->vi_ingress_qos;
594int rtnl_link_vlan_set_egress_map(
struct rtnl_link *link, uint32_t from,
int to)
596 struct vlan_info *vi = link->l_info;
598 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
599 return -NLE_OPNOTSUPP;
601 if (to < 0 || to > VLAN_PRIO_MAX)
604 if (vi->vi_negress >= vi->vi_egress_size) {
605 uint32_t new_size = vi->vi_egress_size + 1 + vi->vi_egress_size / 2;
609 if (new_size < vi->vi_egress_size)
611 bytes = (size_t) new_size *
sizeof(
struct vlan_map);
612 if (bytes /
sizeof (
struct vlan_map) != new_size)
614 ptr = realloc(vi->vi_egress_qos, bytes);
618 vi->vi_egress_qos = ptr;
619 vi->vi_egress_size = new_size;
622 vi->vi_egress_qos[vi->vi_negress].vm_from = from;
623 vi->vi_egress_qos[vi->vi_negress].vm_to = to;
625 vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
633 struct vlan_info *vi = link->l_info;
635 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
641 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
642 *negress = vi->vi_negress;
643 return vi->vi_egress_qos;
652static const struct trans_tbl vlan_flags[] = {
653 __ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr),
654 __ADD(VLAN_FLAG_GVRP, gvrp),
655 __ADD(VLAN_FLAG_LOOSE_BINDING, loose_binding),
656 __ADD(VLAN_FLAG_MVRP, mvrp),
657 __ADD(VLAN_FLAG_BRIDGE_BINDING, bridge_binding),
665char *rtnl_link_vlan_flags2str(
int flags,
char *buf,
size_t len)
667 return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
670int rtnl_link_vlan_str2flags(
const char *name)
672 return __str2flags(name, vlan_flags, ARRAY_SIZE(vlan_flags));
678static void _nl_init vlan_init(
void)
683static void _nl_exit vlan_exit(
void)
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 * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
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.
void nla_nest_cancel(struct nl_msg *msg, const struct nlattr *attr)
Cancel the addition of a nested attribute.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
@ 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.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
int rtnl_link_vlan_get_protocol(struct rtnl_link *link)
Get VLAN protocol.
int rtnl_link_vlan_unset_flags(struct rtnl_link *link, unsigned int flags)
Unset VLAN flags.
int rtnl_link_vlan_set_id(struct rtnl_link *link, uint16_t id)
Set VLAN ID.
int rtnl_link_vlan_set_protocol(struct rtnl_link *link, uint16_t protocol)
Set VLAN protocol.
int rtnl_link_is_vlan(struct rtnl_link *link)
Check if link is a VLAN link.
int rtnl_link_vlan_get_id(struct rtnl_link *link)
Get VLAN Id.
struct rtnl_link * rtnl_link_vlan_alloc(void)
Allocate link object of type VLAN.
int rtnl_link_vlan_set_flags(struct rtnl_link *link, unsigned int flags)
Set VLAN flags.
int rtnl_link_vlan_get_flags(struct rtnl_link *link)
Get VLAN flags.
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.