libnl  3.2.27
vlan.c
1 /*
2  * lib/route/link/vlan.c VLAN Link Info
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup link
14  * @defgroup vlan VLAN
15  * Virtual LAN link module
16  *
17  * @details
18  * \b Link Type Name: "vlan"
19  *
20  * @route_doc{link_vlan, VLAN Documentation}
21  *
22  * @{
23  */
24 
25 #include <netlink-private/netlink.h>
26 #include <netlink/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/utils.h>
29 #include <netlink/object.h>
30 #include <netlink/route/rtnl.h>
31 #include <netlink-private/route/link/api.h>
32 #include <netlink/route/link/vlan.h>
33 
34 #include <linux/if_vlan.h>
35 
36 /** @cond SKIP */
37 #define VLAN_HAS_ID (1<<0)
38 #define VLAN_HAS_FLAGS (1<<1)
39 #define VLAN_HAS_INGRESS_QOS (1<<2)
40 #define VLAN_HAS_EGRESS_QOS (1<<3)
41 #define VLAN_HAS_PROTOCOL (1<<4)
42 
43 struct vlan_info
44 {
45  uint16_t vi_vlan_id;
46  uint16_t vi_protocol;
47  uint32_t vi_flags;
48  uint32_t vi_flags_mask;
49  uint32_t vi_ingress_qos[VLAN_PRIO_MAX+1];
50  uint32_t vi_negress;
51  uint32_t vi_egress_size;
52  struct vlan_map * vi_egress_qos;
53  uint32_t vi_mask;
54 };
55 
56 /** @endcond */
57 
58 static struct nla_policy vlan_policy[IFLA_VLAN_MAX+1] = {
59  [IFLA_VLAN_ID] = { .type = NLA_U16 },
60  [IFLA_VLAN_FLAGS] = { .minlen = sizeof(struct ifla_vlan_flags) },
61  [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
62  [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
63  [IFLA_VLAN_PROTOCOL] = { .type = NLA_U16 },
64 };
65 
66 static int vlan_alloc(struct rtnl_link *link)
67 {
68  struct vlan_info *vi;
69 
70  if (link->l_info) {
71  vi = link->l_info;
72  free(vi->vi_egress_qos);
73  memset(link->l_info, 0, sizeof(*vi));
74  } else {
75  if ((vi = calloc(1, sizeof(*vi))) == NULL)
76  return -NLE_NOMEM;
77 
78  link->l_info = vi;
79  }
80 
81  return 0;
82 }
83 
84 static int vlan_parse(struct rtnl_link *link, struct nlattr *data,
85  struct nlattr *xstats)
86 {
87  struct nlattr *tb[IFLA_VLAN_MAX+1];
88  struct vlan_info *vi;
89  int err;
90 
91  NL_DBG(3, "Parsing VLAN link info\n");
92 
93  if ((err = nla_parse_nested(tb, IFLA_VLAN_MAX, data, vlan_policy)) < 0)
94  goto errout;
95 
96  if ((err = vlan_alloc(link)) < 0)
97  goto errout;
98 
99  vi = link->l_info;
100 
101  if (tb[IFLA_VLAN_ID]) {
102  vi->vi_vlan_id = nla_get_u16(tb[IFLA_VLAN_ID]);
103  vi->vi_mask |= VLAN_HAS_ID;
104  }
105 
106  if (tb[IFLA_VLAN_PROTOCOL]) {
107  vi->vi_protocol = nla_get_u16(tb[IFLA_VLAN_PROTOCOL]);
108  vi->vi_mask |= VLAN_HAS_PROTOCOL;
109  }
110 
111  if (tb[IFLA_VLAN_FLAGS]) {
112  struct ifla_vlan_flags flags;
113  nla_memcpy(&flags, tb[IFLA_VLAN_FLAGS], sizeof(flags));
114 
115  vi->vi_flags = flags.flags;
116  vi->vi_mask |= VLAN_HAS_FLAGS;
117  }
118 
119  if (tb[IFLA_VLAN_INGRESS_QOS]) {
120  struct ifla_vlan_qos_mapping *map;
121  struct nlattr *nla;
122  int remaining;
123 
124  memset(vi->vi_ingress_qos, 0, sizeof(vi->vi_ingress_qos));
125 
126  nla_for_each_nested(nla, tb[IFLA_VLAN_INGRESS_QOS], remaining) {
127  if (nla_len(nla) < sizeof(*map))
128  return -NLE_INVAL;
129 
130  map = nla_data(nla);
131  if (map->from > VLAN_PRIO_MAX) {
132  return -NLE_INVAL;
133  }
134 
135  vi->vi_ingress_qos[map->from] = map->to;
136  }
137 
138  vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
139  }
140 
141  if (tb[IFLA_VLAN_EGRESS_QOS]) {
142  struct ifla_vlan_qos_mapping *map;
143  struct nlattr *nla;
144  int remaining, i = 0;
145 
146  nla_for_each_nested(nla, tb[IFLA_VLAN_EGRESS_QOS], remaining) {
147  if (nla_len(nla) < sizeof(*map))
148  return -NLE_INVAL;
149  i++;
150  }
151 
152  /* align to have a little reserve */
153  vi->vi_egress_size = (i + 32) & ~31;
154  vi->vi_egress_qos = calloc(vi->vi_egress_size, sizeof(*vi->vi_egress_qos));
155  if (vi->vi_egress_qos == NULL)
156  return -NLE_NOMEM;
157 
158  i = 0;
159  nla_for_each_nested(nla, tb[IFLA_VLAN_EGRESS_QOS], remaining) {
160  map = nla_data(nla);
161  NL_DBG(4, "Assigning egress qos mapping %d\n", i);
162  vi->vi_egress_qos[i].vm_from = map->from;
163  vi->vi_egress_qos[i++].vm_to = map->to;
164  }
165 
166  vi->vi_negress = i;
167  vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
168  }
169 
170  err = 0;
171 errout:
172  return err;
173 }
174 
175 static void vlan_free(struct rtnl_link *link)
176 {
177  struct vlan_info *vi = link->l_info;
178 
179  if (vi) {
180  free(vi->vi_egress_qos);
181  vi->vi_egress_qos = NULL;
182  }
183 
184  free(vi);
185  link->l_info = NULL;
186 }
187 
188 static void vlan_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
189 {
190  struct vlan_info *vi = link->l_info;
191 
192  nl_dump(p, "vlan-id %d", vi->vi_vlan_id);
193 }
194 
195 static void vlan_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
196 {
197  struct vlan_info *vi = link->l_info;
198  int printed;
199  uint32_t i;
200  char buf[64];
201 
202  rtnl_link_vlan_flags2str(vi->vi_flags, buf, sizeof(buf));
203  nl_dump_line(p, " vlan-info id %d <%s>", vi->vi_vlan_id, buf);
204 
205  if (vi->vi_mask & VLAN_HAS_PROTOCOL)
206  nl_dump_line(p, " vlan protocol <%d>", vi->vi_protocol);
207 
208  nl_dump(p, "\n");
209 
210  if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
211  nl_dump_line(p,
212  " ingress vlan prio -> qos/socket prio mapping:\n");
213  for (i = 0, printed = 0; i <= VLAN_PRIO_MAX; i++) {
214  if (vi->vi_ingress_qos[i]) {
215  if (printed == 0)
216  nl_dump_line(p, " ");
217  nl_dump(p, "%x -> %#08x, ",
218  i, vi->vi_ingress_qos[i]);
219  if (printed++ == 3) {
220  nl_dump(p, "\n");
221  printed = 0;
222  }
223  }
224  }
225 
226  if (printed > 0 && printed != 4)
227  nl_dump(p, "\n");
228  }
229 
230  if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
231  nl_dump_line(p,
232  " egress qos/socket prio -> vlan prio mapping:\n");
233  for (i = 0, printed = 0; i < vi->vi_negress; i++) {
234  if (printed == 0)
235  nl_dump_line(p, " ");
236  nl_dump(p, "%#08x -> %x, ",
237  vi->vi_egress_qos[i].vm_from,
238  vi->vi_egress_qos[i].vm_to);
239  if (printed++ == 3) {
240  nl_dump(p, "\n");
241  printed = 0;
242  }
243  }
244 
245  if (printed > 0 && printed != 4)
246  nl_dump(p, "\n");
247  }
248 }
249 
250 static int vlan_clone(struct rtnl_link *dst, struct rtnl_link *src)
251 {
252  struct vlan_info *vdst, *vsrc = src->l_info;
253  int err;
254 
255  dst->l_info = NULL;
256  if ((err = rtnl_link_set_type(dst, "vlan")) < 0)
257  return err;
258  vdst = dst->l_info;
259 
260  vdst->vi_egress_qos = calloc(vsrc->vi_egress_size,
261  sizeof(struct vlan_map));
262  if (!vdst->vi_egress_qos)
263  return -NLE_NOMEM;
264 
265  memcpy(vdst->vi_egress_qos, vsrc->vi_egress_qos,
266  vsrc->vi_egress_size * sizeof(struct vlan_map));
267 
268  return 0;
269 }
270 
271 static int vlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
272 {
273  struct vlan_info *vi = link->l_info;
274  struct nlattr *data;
275 
276  if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
277  return -NLE_MSGSIZE;
278 
279  if (vi->vi_mask & VLAN_HAS_ID)
280  NLA_PUT_U16(msg, IFLA_VLAN_ID, vi->vi_vlan_id);
281 
282  if (vi->vi_mask & VLAN_HAS_FLAGS) {
283  struct ifla_vlan_flags flags = {
284  .flags = vi->vi_flags,
285  .mask = vi->vi_flags_mask,
286  };
287 
288  NLA_PUT(msg, IFLA_VLAN_FLAGS, sizeof(flags), &flags);
289  }
290 
291  if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
292  struct ifla_vlan_qos_mapping map;
293  struct nlattr *qos;
294  int i;
295 
296  if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
297  goto nla_put_failure;
298 
299  for (i = 0; i <= VLAN_PRIO_MAX; i++) {
300  if (vi->vi_ingress_qos[i]) {
301  map.from = i;
302  map.to = vi->vi_ingress_qos[i];
303 
304  NLA_PUT(msg, i, sizeof(map), &map);
305  }
306  }
307 
308  nla_nest_end(msg, qos);
309  }
310 
311  if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
312  struct ifla_vlan_qos_mapping map;
313  struct nlattr *qos;
314  uint32_t i;
315 
316  if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
317  goto nla_put_failure;
318 
319  for (i = 0; i < vi->vi_negress; i++) {
320  map.from = vi->vi_egress_qos[i].vm_from;
321  map.to = vi->vi_egress_qos[i].vm_to;
322 
323  NLA_PUT(msg, i, sizeof(map), &map);
324  }
325 
326  nla_nest_end(msg, qos);
327  }
328 
329  nla_nest_end(msg, data);
330 
331 nla_put_failure:
332 
333  return 0;
334 }
335 
336 static struct rtnl_link_info_ops vlan_info_ops = {
337  .io_name = "vlan",
338  .io_alloc = vlan_alloc,
339  .io_parse = vlan_parse,
340  .io_dump = {
341  [NL_DUMP_LINE] = vlan_dump_line,
342  [NL_DUMP_DETAILS] = vlan_dump_details,
343  },
344  .io_clone = vlan_clone,
345  .io_put_attrs = vlan_put_attrs,
346  .io_free = vlan_free,
347 };
348 
349 /** @cond SKIP */
350 #define IS_VLAN_LINK_ASSERT(link) \
351  if ((link)->l_info_ops != &vlan_info_ops) { \
352  APPBUG("Link is not a vlan link. set type \"vlan\" first."); \
353  return -NLE_OPNOTSUPP; \
354  }
355 /** @endcond */
356 
357 /**
358  * @name VLAN Object
359  * @{
360  */
361 
362 /**
363  * Allocate link object of type VLAN
364  *
365  * @return Allocated link object or NULL.
366  */
368 {
369  struct rtnl_link *link;
370  int err;
371 
372  if (!(link = rtnl_link_alloc()))
373  return NULL;
374 
375  if ((err = rtnl_link_set_type(link, "vlan")) < 0) {
376  rtnl_link_put(link);
377  return NULL;
378  }
379 
380  return link;
381 }
382 
383 /**
384  * Check if link is a VLAN link
385  * @arg link Link object
386  *
387  * @return True if link is a VLAN link, otherwise false is returned.
388  */
389 int rtnl_link_is_vlan(struct rtnl_link *link)
390 {
391  return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vlan");
392 }
393 
394 /**
395  * Set VLAN ID
396  * @arg link Link object
397  * @arg id VLAN identifier
398  *
399  * @return 0 on success or a negative error code
400  */
401 int rtnl_link_vlan_set_id(struct rtnl_link *link, uint16_t id)
402 {
403  struct vlan_info *vi = link->l_info;
404 
405  IS_VLAN_LINK_ASSERT(link);
406 
407  vi->vi_vlan_id = id;
408  vi->vi_mask |= VLAN_HAS_ID;
409 
410  return 0;
411 }
412 
413 /**
414  * Get VLAN Id
415  * @arg link Link object
416  *
417  * @return VLAN id, 0 if not set or a negative error code.
418  */
420 {
421  struct vlan_info *vi = link->l_info;
422 
423  IS_VLAN_LINK_ASSERT(link);
424 
425  if (vi->vi_mask & VLAN_HAS_ID)
426  return vi->vi_vlan_id;
427  else
428  return 0;
429 }
430 
431 /**
432  * Set VLAN protocol
433  * @arg link Link object
434  * @arg protocol VLAN protocol
435  *
436  * @return 0 on success or a negative error code
437  */
438 int rtnl_link_vlan_set_protocol(struct rtnl_link *link, uint16_t protocol)
439 {
440  struct vlan_info *vi = link->l_info;
441 
442  IS_VLAN_LINK_ASSERT(link);
443 
444  vi->vi_protocol = protocol;
445  vi->vi_mask |= VLAN_HAS_PROTOCOL;
446 
447  return 0;
448 }
449 
450 /**
451  * Get VLAN protocol
452  * @arg link Link object
453  *
454  * @return VLAN protocol, 0 if not set or a negative error code.
455  */
457 {
458  struct vlan_info *vi = link->l_info;
459 
460  IS_VLAN_LINK_ASSERT(link);
461 
462  if (vi->vi_mask & VLAN_HAS_PROTOCOL)
463  return vi->vi_protocol;
464  else
465  return 0;
466 }
467 
468 /**
469  * Set VLAN flags
470  * @arg link Link object
471  * @arg flags VLAN flags
472  *
473  * @return 0 on success or a negative error code.
474  */
475 int rtnl_link_vlan_set_flags(struct rtnl_link *link, unsigned int flags)
476 {
477  struct vlan_info *vi = link->l_info;
478 
479  IS_VLAN_LINK_ASSERT(link);
480 
481  vi->vi_flags_mask |= flags;
482  vi->vi_flags |= flags;
483  vi->vi_mask |= VLAN_HAS_FLAGS;
484 
485  return 0;
486 }
487 
488 /**
489  * Unset VLAN flags
490  * @arg link Link object
491  * @arg flags VLAN flags
492  *
493  * @return 0 on success or a negative error code.
494  */
495 int rtnl_link_vlan_unset_flags(struct rtnl_link *link, unsigned int flags)
496 {
497  struct vlan_info *vi = link->l_info;
498 
499  IS_VLAN_LINK_ASSERT(link);
500 
501  vi->vi_flags_mask |= flags;
502  vi->vi_flags &= ~flags;
503  vi->vi_mask |= VLAN_HAS_FLAGS;
504 
505  return 0;
506 }
507 
508 /**
509  * Get VLAN flags
510  * @arg link Link object
511  *
512  * @return VLAN flags, 0 if none set, or a negative error code.
513  */
515 {
516  struct vlan_info *vi = link->l_info;
517 
518  IS_VLAN_LINK_ASSERT(link);
519 
520  return vi->vi_flags;
521 }
522 
523 /** @} */
524 
525 /**
526  * @name Quality of Service
527  * @{
528  */
529 
530 int rtnl_link_vlan_set_ingress_map(struct rtnl_link *link, int from,
531  uint32_t to)
532 {
533  struct vlan_info *vi = link->l_info;
534 
535  IS_VLAN_LINK_ASSERT(link);
536 
537  if (from < 0 || from > VLAN_PRIO_MAX)
538  return -NLE_INVAL;
539 
540  vi->vi_ingress_qos[from] = to;
541  vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
542 
543  return 0;
544 }
545 
546 uint32_t *rtnl_link_vlan_get_ingress_map(struct rtnl_link *link)
547 {
548  struct vlan_info *vi = link->l_info;
549 
550  if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
551  return NULL;
552 
553  if (vi->vi_mask & VLAN_HAS_INGRESS_QOS)
554  return vi->vi_ingress_qos;
555  else
556  return NULL;
557 }
558 
559 int rtnl_link_vlan_set_egress_map(struct rtnl_link *link, uint32_t from, int to)
560 {
561  struct vlan_info *vi = link->l_info;
562 
563  if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
564  return -NLE_OPNOTSUPP;
565 
566  if (to < 0 || to > VLAN_PRIO_MAX)
567  return -NLE_INVAL;
568 
569  if (vi->vi_negress >= vi->vi_egress_size) {
570  int new_size = vi->vi_egress_size + 32;
571  void *ptr;
572 
573  ptr = realloc(vi->vi_egress_qos, new_size);
574  if (!ptr)
575  return -NLE_NOMEM;
576 
577  vi->vi_egress_qos = ptr;
578  vi->vi_egress_size = new_size;
579  }
580 
581  vi->vi_egress_qos[vi->vi_negress].vm_from = from;
582  vi->vi_egress_qos[vi->vi_negress].vm_to = to;
583  vi->vi_negress++;
584  vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
585 
586  return 0;
587 }
588 
589 struct vlan_map *rtnl_link_vlan_get_egress_map(struct rtnl_link *link,
590  int *negress)
591 {
592  struct vlan_info *vi = link->l_info;
593 
594  if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
595  return NULL;
596 
597  if (negress == NULL)
598  return NULL;
599 
600  if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
601  *negress = vi->vi_negress;
602  return vi->vi_egress_qos;
603  } else {
604  *negress = 0;
605  return NULL;
606  }
607 }
608 
609 /** @} */
610 
611 static const struct trans_tbl vlan_flags[] = {
612  __ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr),
613  __ADD(VLAN_FLAG_GVRP, gvrp),
614  __ADD(VLAN_FLAG_LOOSE_BINDING, loose_binding),
615  __ADD(VLAN_FLAG_MVRP, mvrp),
616 };
617 
618 /**
619  * @name Flag Translation
620  * @{
621  */
622 
623 char *rtnl_link_vlan_flags2str(int flags, char *buf, size_t len)
624 {
625  return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
626 }
627 
628 int rtnl_link_vlan_str2flags(const char *name)
629 {
630  return __str2flags(name, vlan_flags, ARRAY_SIZE(vlan_flags));
631 }
632 
633 /** @} */
634 
635 
636 static void __init vlan_init(void)
637 {
638  rtnl_link_register_info(&vlan_info_ops);
639 }
640 
641 static void __exit vlan_exit(void)
642 {
643  rtnl_link_unregister_info(&vlan_info_ops);
644 }
645 
646 /** @} */
Dump object briefly on one line.
Definition: types.h:22
int rtnl_link_vlan_set_flags(struct rtnl_link *link, unsigned int flags)
Set VLAN flags.
Definition: vlan.c:475
struct rtnl_link * rtnl_link_vlan_alloc(void)
Allocate link object of type VLAN.
Definition: vlan.c:367
int rtnl_link_vlan_get_flags(struct rtnl_link *link)
Get VLAN flags.
Definition: vlan.c:514
Attribute validation policy.
Definition: attr.h:67
Dump all attributes but no statistics.
Definition: types.h:23
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:917
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:353
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:162
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:991
16 bit integer
Definition: attr.h:40
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
int rtnl_link_vlan_get_id(struct rtnl_link *link)
Get VLAN Id.
Definition: vlan.c:419
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition: attr.c:131
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
Definition: attr.h:327
int rtnl_link_vlan_get_protocol(struct rtnl_link *link)
Get VLAN protocol.
Definition: vlan.c:456
int rtnl_link_vlan_unset_flags(struct rtnl_link *link, unsigned int flags)
Unset VLAN flags.
Definition: vlan.c:495
Nested attributes.
Definition: attr.h:46
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:69
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
Definition: attr.c:649
Dumping parameters.
Definition: types.h:33
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
Definition: attr.h:215
Definition: vlan.h:22
int rtnl_link_vlan_set_protocol(struct rtnl_link *link, uint16_t protocol)
Set VLAN protocol.
Definition: vlan.c:438
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:914
int rtnl_link_is_vlan(struct rtnl_link *link)
Check if link is a VLAN link.
Definition: vlan.c:389
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:895
int rtnl_link_vlan_set_id(struct rtnl_link *link, uint16_t id)
Set VLAN ID.
Definition: vlan.c:401