19#include <netlink-private/netlink.h>
20#include <netlink/netlink.h>
21#include <netlink/attr.h>
22#include <netlink/utils.h>
23#include <netlink/object.h>
24#include <netlink/route/rtnl.h>
25#include <netlink-private/route/link/api.h>
26#include <netlink/route/link/vlan.h>
28#include <linux/if_vlan.h>
31#define VLAN_HAS_ID (1<<0)
32#define VLAN_HAS_FLAGS (1<<1)
33#define VLAN_HAS_INGRESS_QOS (1<<2)
34#define VLAN_HAS_EGRESS_QOS (1<<3)
35#define VLAN_HAS_PROTOCOL (1<<4)
41 unsigned int vi_ingress_qos_mask:(VLAN_PRIO_MAX+1);
43 uint32_t vi_flags_mask;
44 uint32_t vi_ingress_qos[VLAN_PRIO_MAX+1];
46 uint32_t vi_egress_size;
53static struct nla_policy vlan_policy[IFLA_VLAN_MAX+1] = {
55 [IFLA_VLAN_FLAGS] = { .minlen =
sizeof(
struct ifla_vlan_flags) },
56 [IFLA_VLAN_INGRESS_QOS] = { .type =
NLA_NESTED },
57 [IFLA_VLAN_EGRESS_QOS] = { .type =
NLA_NESTED },
58 [IFLA_VLAN_PROTOCOL] = { .type =
NLA_U16 },
61static int vlan_alloc(
struct rtnl_link *link)
67 free(vi->vi_egress_qos);
68 memset(link->l_info, 0,
sizeof(*vi));
70 if ((vi = calloc(1,
sizeof(*vi))) == NULL)
79static int vlan_parse(
struct rtnl_link *link,
struct nlattr *data,
80 struct nlattr *xstats)
82 struct nlattr *tb[IFLA_VLAN_MAX+1];
86 NL_DBG(3,
"Parsing VLAN link info\n");
91 if ((err = vlan_alloc(link)) < 0)
96 if (tb[IFLA_VLAN_ID]) {
98 vi->vi_mask |= VLAN_HAS_ID;
101 if (tb[IFLA_VLAN_PROTOCOL]) {
102 vi->vi_protocol =
nla_get_u16(tb[IFLA_VLAN_PROTOCOL]);
103 vi->vi_mask |= VLAN_HAS_PROTOCOL;
106 if (tb[IFLA_VLAN_FLAGS]) {
107 struct ifla_vlan_flags flags;
108 nla_memcpy(&flags, tb[IFLA_VLAN_FLAGS],
sizeof(flags));
110 vi->vi_flags = flags.flags;
111 vi->vi_mask |= VLAN_HAS_FLAGS;
114 if (tb[IFLA_VLAN_INGRESS_QOS]) {
115 struct ifla_vlan_qos_mapping *map;
119 vi->vi_ingress_qos_mask = 0;
120 memset(vi->vi_ingress_qos, 0,
sizeof(vi->vi_ingress_qos));
123 if (
nla_len(nla) <
sizeof(*map))
127 if (map->from > VLAN_PRIO_MAX) {
141 vi->vi_ingress_qos_mask |= (1 << map->from);
142 vi->vi_ingress_qos[map->from] = map->to;
145 vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
148 if (tb[IFLA_VLAN_EGRESS_QOS]) {
149 struct ifla_vlan_qos_mapping *map;
151 int remaining, i = 0;
154 if (
nla_len(nla) <
sizeof(*map))
160 vi->vi_egress_size = (i + 32) & ~31;
161 vi->vi_egress_qos = calloc(vi->vi_egress_size,
sizeof(*vi->vi_egress_qos));
162 if (vi->vi_egress_qos == NULL)
168 NL_DBG(4,
"Assigning egress qos mapping %d\n", i);
169 vi->vi_egress_qos[i].vm_from = map->from;
170 vi->vi_egress_qos[i++].vm_to = map->to;
174 vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
182static void vlan_free(
struct rtnl_link *link)
184 struct vlan_info *vi = link->l_info;
187 free(vi->vi_egress_qos);
188 vi->vi_egress_qos = NULL;
197 struct vlan_info *vi = link->l_info;
199 nl_dump(p,
"vlan-id %d", vi->vi_vlan_id);
204 struct vlan_info *vi = link->l_info;
209 rtnl_link_vlan_flags2str(vi->vi_flags, buf,
sizeof(buf));
210 nl_dump_line(p,
" vlan-info id %d <%s>", vi->vi_vlan_id, buf);
212 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
213 nl_dump_line(p,
" vlan protocol <%d>", ntohs(vi->vi_protocol));
217 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
219 " ingress vlan prio -> qos/socket prio mapping:\n");
220 for (i = 0, printed = 0; i <= VLAN_PRIO_MAX; i++) {
221 if (vi->vi_ingress_qos_mask & (1 << i)) {
223 nl_dump_line(p,
" ");
225 i, vi->vi_ingress_qos[i]);
226 if (printed++ == 3) {
233 if (printed > 0 && printed != 4)
237 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
239 " egress qos/socket prio -> vlan prio mapping:\n");
240 for (i = 0, printed = 0; i < vi->vi_negress; i++) {
242 nl_dump_line(p,
" ");
244 vi->vi_egress_qos[i].vm_from,
245 vi->vi_egress_qos[i].vm_to);
246 if (printed++ == 3) {
252 if (printed > 0 && printed != 4)
259 struct vlan_info *vdst, *vsrc = src->l_info;
268 if (vsrc->vi_negress) {
269 p = calloc(vsrc->vi_negress,
277 if (vsrc->vi_negress) {
278 vdst->vi_egress_size = vsrc->vi_negress;
279 vdst->vi_egress_qos = p;
280 memcpy(vdst->vi_egress_qos, vsrc->vi_egress_qos,
281 vsrc->vi_negress *
sizeof(
struct vlan_map));
287static int vlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
289 struct vlan_info *vi = link->l_info;
294 goto nla_put_failure;
298 if (vi->vi_mask & VLAN_HAS_ID)
301 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
302 NLA_PUT_U16(msg, IFLA_VLAN_PROTOCOL, vi->vi_protocol);
304 if (vi->vi_mask & VLAN_HAS_FLAGS) {
305 struct ifla_vlan_flags flags = {
306 .flags = vi->vi_flags,
307 .mask = vi->vi_flags_mask,
310 NLA_PUT(msg, IFLA_VLAN_FLAGS,
sizeof(flags), &flags);
313 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
314 struct ifla_vlan_qos_mapping map;
319 goto nla_put_failure;
323 for (i = 0; i <= VLAN_PRIO_MAX; i++) {
324 if (vi->vi_ingress_qos_mask & (1 << i)) {
326 map.to = vi->vi_ingress_qos[i];
328 NLA_PUT(msg, i,
sizeof(map), &map);
336 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
337 struct ifla_vlan_qos_mapping map;
342 goto nla_put_failure;
346 for (i = 0; i < vi->vi_negress; i++) {
347 map.from = vi->vi_egress_qos[i].vm_from;
348 map.to = vi->vi_egress_qos[i].vm_to;
350 NLA_PUT(msg, i,
sizeof(map), &map);
361 for (; nest_count > 0; nest_count--)
366static struct rtnl_link_info_ops vlan_info_ops = {
368 .io_alloc = vlan_alloc,
369 .io_parse = vlan_parse,
374 .io_clone = vlan_clone,
375 .io_put_attrs = vlan_put_attrs,
376 .io_free = vlan_free,
380#define IS_VLAN_LINK_ASSERT(link) \
381 if ((link)->l_info_ops != &vlan_info_ops) { \
382 APPBUG("Link is not a vlan link. set type \"vlan\" first."); \
383 return -NLE_OPNOTSUPP; \
420 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"vlan");
432 struct vlan_info *vi = link->l_info;
434 IS_VLAN_LINK_ASSERT(link);
437 vi->vi_mask |= VLAN_HAS_ID;
450 struct vlan_info *vi = link->l_info;
452 IS_VLAN_LINK_ASSERT(link);
454 if (vi->vi_mask & VLAN_HAS_ID)
455 return vi->vi_vlan_id;
470 struct vlan_info *vi = link->l_info;
472 IS_VLAN_LINK_ASSERT(link);
474 vi->vi_protocol = protocol;
475 vi->vi_mask |= VLAN_HAS_PROTOCOL;
489 struct vlan_info *vi = link->l_info;
491 IS_VLAN_LINK_ASSERT(link);
493 if (vi->vi_mask & VLAN_HAS_PROTOCOL)
494 return vi->vi_protocol;
508 struct vlan_info *vi = link->l_info;
510 IS_VLAN_LINK_ASSERT(link);
512 vi->vi_flags_mask |= flags;
513 vi->vi_flags |= flags;
514 vi->vi_mask |= VLAN_HAS_FLAGS;
528 struct vlan_info *vi = link->l_info;
530 IS_VLAN_LINK_ASSERT(link);
532 vi->vi_flags_mask |= flags;
533 vi->vi_flags &= ~flags;
534 vi->vi_mask |= VLAN_HAS_FLAGS;
547 struct vlan_info *vi = link->l_info;
549 IS_VLAN_LINK_ASSERT(link);
561int rtnl_link_vlan_set_ingress_map(
struct rtnl_link *link,
int from,
564 struct vlan_info *vi = link->l_info;
566 IS_VLAN_LINK_ASSERT(link);
568 if (from < 0 || from > VLAN_PRIO_MAX)
571 vi->vi_ingress_qos_mask |= (1 << from);
572 vi->vi_ingress_qos[from] = to;
573 vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
578uint32_t *rtnl_link_vlan_get_ingress_map(
struct rtnl_link *link)
580 struct vlan_info *vi = link->l_info;
582 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
585 if (vi->vi_mask & VLAN_HAS_INGRESS_QOS)
586 return vi->vi_ingress_qos;
591int rtnl_link_vlan_set_egress_map(
struct rtnl_link *link, uint32_t from,
int to)
593 struct vlan_info *vi = link->l_info;
595 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
596 return -NLE_OPNOTSUPP;
598 if (to < 0 || to > VLAN_PRIO_MAX)
601 if (vi->vi_negress >= vi->vi_egress_size) {
602 uint32_t new_size = vi->vi_egress_size + 1 + vi->vi_egress_size / 2;
606 if (new_size < vi->vi_egress_size)
608 bytes = (size_t) new_size *
sizeof(
struct vlan_map);
609 if (bytes /
sizeof (
struct vlan_map) != new_size)
611 ptr = realloc(vi->vi_egress_qos, bytes);
615 vi->vi_egress_qos = ptr;
616 vi->vi_egress_size = new_size;
619 vi->vi_egress_qos[vi->vi_negress].vm_from = from;
620 vi->vi_egress_qos[vi->vi_negress].vm_to = to;
622 vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
630 struct vlan_info *vi = link->l_info;
632 if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
638 if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
639 *negress = vi->vi_negress;
640 return vi->vi_egress_qos;
649static const struct trans_tbl vlan_flags[] = {
650 __ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr),
651 __ADD(VLAN_FLAG_GVRP, gvrp),
652 __ADD(VLAN_FLAG_LOOSE_BINDING, loose_binding),
653 __ADD(VLAN_FLAG_MVRP, mvrp),
654 __ADD(VLAN_FLAG_BRIDGE_BINDING, bridge_binding),
662char *rtnl_link_vlan_flags2str(
int flags,
char *buf,
size_t len)
664 return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
667int rtnl_link_vlan_str2flags(
const char *name)
669 return __str2flags(name, vlan_flags, ARRAY_SIZE(vlan_flags));
675static void __init vlan_init(
void)
680static void __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_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_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.
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.