vlan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. * @ingroup link
  13. * @defgroup vlan VLAN
  14. * Virtual LAN link module
  15. *
  16. * @details
  17. * \b Link Type Name: "vlan"
  18. *
  19. * @route_doc{link_vlan, VLAN Documentation}
  20. *
  21. * @{
  22. */
  23. #include <netlink-private/netlink.h>
  24. #include <netlink/netlink.h>
  25. #include <netlink/attr.h>
  26. #include <netlink/utils.h>
  27. #include <netlink/object.h>
  28. #include <netlink/route/rtnl.h>
  29. #include <netlink-private/route/link/api.h>
  30. #include <netlink/route/link/vlan.h>
  31. #include <linux/if_vlan.h>
  32. /** @cond SKIP */
  33. #define VLAN_HAS_ID (1<<0)
  34. #define VLAN_HAS_FLAGS (1<<1)
  35. #define VLAN_HAS_INGRESS_QOS (1<<2)
  36. #define VLAN_HAS_EGRESS_QOS (1<<3)
  37. #define VLAN_HAS_PROTOCOL (1<<4)
  38. struct vlan_info
  39. {
  40. uint16_t vi_vlan_id;
  41. uint16_t vi_protocol;
  42. uint32_t vi_flags;
  43. uint32_t vi_flags_mask;
  44. uint32_t vi_ingress_qos[VLAN_PRIO_MAX+1];
  45. uint32_t vi_negress;
  46. uint32_t vi_egress_size;
  47. struct vlan_map * vi_egress_qos;
  48. uint32_t vi_mask;
  49. };
  50. /** @endcond */
  51. static struct nla_policy vlan_policy[IFLA_VLAN_MAX+1] = {
  52. [IFLA_VLAN_ID] = { .type = NLA_U16 },
  53. [IFLA_VLAN_FLAGS] = { .minlen = sizeof(struct ifla_vlan_flags) },
  54. [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
  55. [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
  56. [IFLA_VLAN_PROTOCOL] = { .type = NLA_U16 },
  57. };
  58. static int vlan_alloc(struct rtnl_link *link)
  59. {
  60. struct vlan_info *vi;
  61. if ((vi = calloc(1, sizeof(*vi))) == NULL)
  62. return -NLE_NOMEM;
  63. link->l_info = vi;
  64. return 0;
  65. }
  66. static int vlan_parse(struct rtnl_link *link, struct nlattr *data,
  67. struct nlattr *xstats)
  68. {
  69. struct nlattr *tb[IFLA_VLAN_MAX+1];
  70. struct vlan_info *vi;
  71. int err;
  72. NL_DBG(3, "Parsing VLAN link info");
  73. if ((err = nla_parse_nested(tb, IFLA_VLAN_MAX, data, vlan_policy)) < 0)
  74. goto errout;
  75. if ((err = vlan_alloc(link)) < 0)
  76. goto errout;
  77. vi = link->l_info;
  78. if (tb[IFLA_VLAN_ID]) {
  79. vi->vi_vlan_id = nla_get_u16(tb[IFLA_VLAN_ID]);
  80. vi->vi_mask |= VLAN_HAS_ID;
  81. }
  82. if (tb[IFLA_VLAN_PROTOCOL]) {
  83. vi->vi_protocol = nla_get_u16(tb[IFLA_VLAN_PROTOCOL]);
  84. vi->vi_mask |= VLAN_HAS_PROTOCOL;
  85. }
  86. if (tb[IFLA_VLAN_FLAGS]) {
  87. struct ifla_vlan_flags flags;
  88. nla_memcpy(&flags, tb[IFLA_VLAN_FLAGS], sizeof(flags));
  89. vi->vi_flags = flags.flags;
  90. vi->vi_mask |= VLAN_HAS_FLAGS;
  91. }
  92. if (tb[IFLA_VLAN_INGRESS_QOS]) {
  93. struct ifla_vlan_qos_mapping *map;
  94. struct nlattr *nla;
  95. int remaining;
  96. memset(vi->vi_ingress_qos, 0, sizeof(vi->vi_ingress_qos));
  97. nla_for_each_nested(nla, tb[IFLA_VLAN_INGRESS_QOS], remaining) {
  98. if (nla_len(nla) < sizeof(*map))
  99. return -NLE_INVAL;
  100. map = nla_data(nla);
  101. if (map->from > VLAN_PRIO_MAX) {
  102. return -NLE_INVAL;
  103. }
  104. vi->vi_ingress_qos[map->from] = map->to;
  105. }
  106. vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
  107. }
  108. if (tb[IFLA_VLAN_EGRESS_QOS]) {
  109. struct ifla_vlan_qos_mapping *map;
  110. struct nlattr *nla;
  111. int remaining, i = 0;
  112. nla_for_each_nested(nla, tb[IFLA_VLAN_EGRESS_QOS], remaining) {
  113. if (nla_len(nla) < sizeof(*map))
  114. return -NLE_INVAL;
  115. i++;
  116. }
  117. /* align to have a little reserve */
  118. vi->vi_egress_size = (i + 32) & ~31;
  119. vi->vi_egress_qos = calloc(vi->vi_egress_size, sizeof(*vi->vi_egress_qos));
  120. if (vi->vi_egress_qos == NULL)
  121. return -NLE_NOMEM;
  122. i = 0;
  123. nla_for_each_nested(nla, tb[IFLA_VLAN_EGRESS_QOS], remaining) {
  124. map = nla_data(nla);
  125. NL_DBG(4, "Assigning egress qos mapping %d\n", i);
  126. vi->vi_egress_qos[i].vm_from = map->from;
  127. vi->vi_egress_qos[i++].vm_to = map->to;
  128. }
  129. vi->vi_negress = i;
  130. vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
  131. }
  132. err = 0;
  133. errout:
  134. return err;
  135. }
  136. static void vlan_free(struct rtnl_link *link)
  137. {
  138. struct vlan_info *vi = link->l_info;
  139. if (vi) {
  140. free(vi->vi_egress_qos);
  141. vi->vi_egress_qos = NULL;
  142. }
  143. free(vi);
  144. link->l_info = NULL;
  145. }
  146. static void vlan_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
  147. {
  148. struct vlan_info *vi = link->l_info;
  149. nl_dump(p, "vlan-id %d", vi->vi_vlan_id);
  150. }
  151. static void vlan_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
  152. {
  153. struct vlan_info *vi = link->l_info;
  154. int printed;
  155. uint32_t i;
  156. char buf[64];
  157. rtnl_link_vlan_flags2str(vi->vi_flags, buf, sizeof(buf));
  158. nl_dump_line(p, " vlan-info id %d <%s>", vi->vi_vlan_id, buf);
  159. if (vi->vi_mask & VLAN_HAS_PROTOCOL)
  160. nl_dump_line(p, " vlan protocol <%d>", vi->vi_protocol);
  161. nl_dump(p, "\n");
  162. if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
  163. nl_dump_line(p,
  164. " ingress vlan prio -> qos/socket prio mapping:\n");
  165. for (i = 0, printed = 0; i <= VLAN_PRIO_MAX; i++) {
  166. if (vi->vi_ingress_qos[i]) {
  167. if (printed == 0)
  168. nl_dump_line(p, " ");
  169. nl_dump(p, "%x -> %#08x, ",
  170. i, vi->vi_ingress_qos[i]);
  171. if (printed++ == 3) {
  172. nl_dump(p, "\n");
  173. printed = 0;
  174. }
  175. }
  176. }
  177. if (printed > 0 && printed != 4)
  178. nl_dump(p, "\n");
  179. }
  180. if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
  181. nl_dump_line(p,
  182. " egress qos/socket prio -> vlan prio mapping:\n");
  183. for (i = 0, printed = 0; i < vi->vi_negress; i++) {
  184. if (printed == 0)
  185. nl_dump_line(p, " ");
  186. nl_dump(p, "%#08x -> %x, ",
  187. vi->vi_egress_qos[i].vm_from,
  188. vi->vi_egress_qos[i].vm_to);
  189. if (printed++ == 3) {
  190. nl_dump(p, "\n");
  191. printed = 0;
  192. }
  193. }
  194. if (printed > 0 && printed != 4)
  195. nl_dump(p, "\n");
  196. }
  197. }
  198. static int vlan_clone(struct rtnl_link *dst, struct rtnl_link *src)
  199. {
  200. struct vlan_info *vdst, *vsrc = src->l_info;
  201. int err;
  202. dst->l_info = NULL;
  203. if ((err = rtnl_link_set_type(dst, "vlan")) < 0)
  204. return err;
  205. vdst = dst->l_info;
  206. vdst->vi_egress_qos = calloc(vsrc->vi_egress_size,
  207. sizeof(struct vlan_map));
  208. if (!vdst->vi_egress_qos)
  209. return -NLE_NOMEM;
  210. memcpy(vdst->vi_egress_qos, vsrc->vi_egress_qos,
  211. vsrc->vi_egress_size * sizeof(struct vlan_map));
  212. return 0;
  213. }
  214. static int vlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
  215. {
  216. struct vlan_info *vi = link->l_info;
  217. struct nlattr *data;
  218. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  219. return -NLE_MSGSIZE;
  220. if (vi->vi_mask & VLAN_HAS_ID)
  221. NLA_PUT_U16(msg, IFLA_VLAN_ID, vi->vi_vlan_id);
  222. if (vi->vi_mask & VLAN_HAS_FLAGS) {
  223. struct ifla_vlan_flags flags = {
  224. .flags = vi->vi_flags,
  225. .mask = vi->vi_flags_mask,
  226. };
  227. NLA_PUT(msg, IFLA_VLAN_FLAGS, sizeof(flags), &flags);
  228. }
  229. if (vi->vi_mask & VLAN_HAS_INGRESS_QOS) {
  230. struct ifla_vlan_qos_mapping map;
  231. struct nlattr *qos;
  232. int i;
  233. if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
  234. goto nla_put_failure;
  235. for (i = 0; i <= VLAN_PRIO_MAX; i++) {
  236. if (vi->vi_ingress_qos[i]) {
  237. map.from = i;
  238. map.to = vi->vi_ingress_qos[i];
  239. NLA_PUT(msg, i, sizeof(map), &map);
  240. }
  241. }
  242. nla_nest_end(msg, qos);
  243. }
  244. if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
  245. struct ifla_vlan_qos_mapping map;
  246. struct nlattr *qos;
  247. uint32_t i;
  248. if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
  249. goto nla_put_failure;
  250. for (i = 0; i < vi->vi_negress; i++) {
  251. map.from = vi->vi_egress_qos[i].vm_from;
  252. map.to = vi->vi_egress_qos[i].vm_to;
  253. NLA_PUT(msg, i, sizeof(map), &map);
  254. }
  255. nla_nest_end(msg, qos);
  256. }
  257. nla_nest_end(msg, data);
  258. nla_put_failure:
  259. return 0;
  260. }
  261. static struct rtnl_link_info_ops vlan_info_ops = {
  262. .io_name = "vlan",
  263. .io_alloc = vlan_alloc,
  264. .io_parse = vlan_parse,
  265. .io_dump = {
  266. [NL_DUMP_LINE] = vlan_dump_line,
  267. [NL_DUMP_DETAILS] = vlan_dump_details,
  268. },
  269. .io_clone = vlan_clone,
  270. .io_put_attrs = vlan_put_attrs,
  271. .io_free = vlan_free,
  272. };
  273. /** @cond SKIP */
  274. #define IS_VLAN_LINK_ASSERT(link) \
  275. if ((link)->l_info_ops != &vlan_info_ops) { \
  276. APPBUG("Link is not a vlan link. set type \"vlan\" first."); \
  277. return -NLE_OPNOTSUPP; \
  278. }
  279. /** @endcond */
  280. /**
  281. * @name VLAN Object
  282. * @{
  283. */
  284. /**
  285. * Allocate link object of type VLAN
  286. *
  287. * @return Allocated link object or NULL.
  288. */
  289. struct rtnl_link *rtnl_link_vlan_alloc(void)
  290. {
  291. struct rtnl_link *link;
  292. int err;
  293. if (!(link = rtnl_link_alloc()))
  294. return NULL;
  295. if ((err = rtnl_link_set_type(link, "vlan")) < 0) {
  296. rtnl_link_put(link);
  297. return NULL;
  298. }
  299. return link;
  300. }
  301. /**
  302. * Check if link is a VLAN link
  303. * @arg link Link object
  304. *
  305. * @return True if link is a VLAN link, otherwise false is returned.
  306. */
  307. int rtnl_link_is_vlan(struct rtnl_link *link)
  308. {
  309. return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vlan");
  310. }
  311. /**
  312. * Set VLAN ID
  313. * @arg link Link object
  314. * @arg id VLAN identifier
  315. *
  316. * @return 0 on success or a negative error code
  317. */
  318. int rtnl_link_vlan_set_id(struct rtnl_link *link, uint16_t id)
  319. {
  320. struct vlan_info *vi = link->l_info;
  321. IS_VLAN_LINK_ASSERT(link);
  322. vi->vi_vlan_id = id;
  323. vi->vi_mask |= VLAN_HAS_ID;
  324. return 0;
  325. }
  326. /**
  327. * Get VLAN Id
  328. * @arg link Link object
  329. *
  330. * @return VLAN id, 0 if not set or a negative error code.
  331. */
  332. int rtnl_link_vlan_get_id(struct rtnl_link *link)
  333. {
  334. struct vlan_info *vi = link->l_info;
  335. IS_VLAN_LINK_ASSERT(link);
  336. if (vi->vi_mask & VLAN_HAS_ID)
  337. return vi->vi_vlan_id;
  338. else
  339. return 0;
  340. }
  341. /**
  342. * Set VLAN protocol
  343. * @arg link Link object
  344. * @arg protocol VLAN protocol
  345. *
  346. * @return 0 on success or a negative error code
  347. */
  348. int rtnl_link_vlan_set_protocol(struct rtnl_link *link, uint16_t protocol)
  349. {
  350. struct vlan_info *vi = link->l_info;
  351. IS_VLAN_LINK_ASSERT(link);
  352. vi->vi_protocol = protocol;
  353. vi->vi_mask |= VLAN_HAS_PROTOCOL;
  354. return 0;
  355. }
  356. /**
  357. * Get VLAN protocol
  358. * @arg link Link object
  359. *
  360. * @return VLAN protocol, 0 if not set or a negative error code.
  361. */
  362. int rtnl_link_vlan_get_protocol(struct rtnl_link *link)
  363. {
  364. struct vlan_info *vi = link->l_info;
  365. IS_VLAN_LINK_ASSERT(link);
  366. if (vi->vi_mask & VLAN_HAS_PROTOCOL)
  367. return vi->vi_protocol;
  368. else
  369. return 0;
  370. }
  371. /**
  372. * Set VLAN flags
  373. * @arg link Link object
  374. * @arg flags VLAN flags
  375. *
  376. * @return 0 on success or a negative error code.
  377. */
  378. int rtnl_link_vlan_set_flags(struct rtnl_link *link, unsigned int flags)
  379. {
  380. struct vlan_info *vi = link->l_info;
  381. IS_VLAN_LINK_ASSERT(link);
  382. vi->vi_flags_mask |= flags;
  383. vi->vi_flags |= flags;
  384. vi->vi_mask |= VLAN_HAS_FLAGS;
  385. return 0;
  386. }
  387. /**
  388. * Unset VLAN flags
  389. * @arg link Link object
  390. * @arg flags VLAN flags
  391. *
  392. * @return 0 on success or a negative error code.
  393. */
  394. int rtnl_link_vlan_unset_flags(struct rtnl_link *link, unsigned int flags)
  395. {
  396. struct vlan_info *vi = link->l_info;
  397. IS_VLAN_LINK_ASSERT(link);
  398. vi->vi_flags_mask |= flags;
  399. vi->vi_flags &= ~flags;
  400. vi->vi_mask |= VLAN_HAS_FLAGS;
  401. return 0;
  402. }
  403. /**
  404. * Get VLAN flags
  405. * @arg link Link object
  406. *
  407. * @return VLAN flags, 0 if none set, or a negative error code.
  408. */
  409. int rtnl_link_vlan_get_flags(struct rtnl_link *link)
  410. {
  411. struct vlan_info *vi = link->l_info;
  412. IS_VLAN_LINK_ASSERT(link);
  413. return vi->vi_flags;
  414. }
  415. /** @} */
  416. /**
  417. * @name Quality of Service
  418. * @{
  419. */
  420. int rtnl_link_vlan_set_ingress_map(struct rtnl_link *link, int from,
  421. uint32_t to)
  422. {
  423. struct vlan_info *vi = link->l_info;
  424. IS_VLAN_LINK_ASSERT(link);
  425. if (from < 0 || from > VLAN_PRIO_MAX)
  426. return -NLE_INVAL;
  427. vi->vi_ingress_qos[from] = to;
  428. vi->vi_mask |= VLAN_HAS_INGRESS_QOS;
  429. return 0;
  430. }
  431. uint32_t *rtnl_link_vlan_get_ingress_map(struct rtnl_link *link)
  432. {
  433. struct vlan_info *vi = link->l_info;
  434. if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
  435. return NULL;
  436. if (vi->vi_mask & VLAN_HAS_INGRESS_QOS)
  437. return vi->vi_ingress_qos;
  438. else
  439. return NULL;
  440. }
  441. int rtnl_link_vlan_set_egress_map(struct rtnl_link *link, uint32_t from, int to)
  442. {
  443. struct vlan_info *vi = link->l_info;
  444. if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
  445. return -NLE_OPNOTSUPP;
  446. if (to < 0 || to > VLAN_PRIO_MAX)
  447. return -NLE_INVAL;
  448. if (vi->vi_negress >= vi->vi_egress_size) {
  449. int new_size = vi->vi_egress_size + 32;
  450. void *ptr;
  451. ptr = realloc(vi->vi_egress_qos, new_size);
  452. if (!ptr)
  453. return -NLE_NOMEM;
  454. vi->vi_egress_qos = ptr;
  455. vi->vi_egress_size = new_size;
  456. }
  457. vi->vi_egress_qos[vi->vi_negress].vm_from = from;
  458. vi->vi_egress_qos[vi->vi_negress].vm_to = to;
  459. vi->vi_negress++;
  460. vi->vi_mask |= VLAN_HAS_EGRESS_QOS;
  461. return 0;
  462. }
  463. struct vlan_map *rtnl_link_vlan_get_egress_map(struct rtnl_link *link,
  464. int *negress)
  465. {
  466. struct vlan_info *vi = link->l_info;
  467. if (link->l_info_ops != &vlan_info_ops || !link->l_info_ops)
  468. return NULL;
  469. if (negress == NULL)
  470. return NULL;
  471. if (vi->vi_mask & VLAN_HAS_EGRESS_QOS) {
  472. *negress = vi->vi_negress;
  473. return vi->vi_egress_qos;
  474. } else {
  475. *negress = 0;
  476. return NULL;
  477. }
  478. }
  479. /** @} */
  480. static const struct trans_tbl vlan_flags[] = {
  481. __ADD(VLAN_FLAG_REORDER_HDR, reorder_hdr)
  482. };
  483. /**
  484. * @name Flag Translation
  485. * @{
  486. */
  487. char *rtnl_link_vlan_flags2str(int flags, char *buf, size_t len)
  488. {
  489. return __flags2str(flags, buf, len, vlan_flags, ARRAY_SIZE(vlan_flags));
  490. }
  491. int rtnl_link_vlan_str2flags(const char *name)
  492. {
  493. return __str2flags(name, vlan_flags, ARRAY_SIZE(vlan_flags));
  494. }
  495. /** @} */
  496. static void __init vlan_init(void)
  497. {
  498. rtnl_link_register_info(&vlan_info_ops);
  499. }
  500. static void __exit vlan_exit(void)
  501. {
  502. rtnl_link_unregister_info(&vlan_info_ops);
  503. }
  504. /** @} */