vlan_netlink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * VLAN netlink control interface
  3. *
  4. * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_vlan.h>
  13. #include <net/net_namespace.h>
  14. #include <net/netlink.h>
  15. #include <net/rtnetlink.h>
  16. #include "vlan.h"
  17. static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
  18. [IFLA_VLAN_ID] = { .type = NLA_U16 },
  19. [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) },
  20. [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
  21. [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
  22. };
  23. static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
  24. [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
  25. };
  26. static inline int vlan_validate_qos_map(struct nlattr *attr)
  27. {
  28. if (!attr)
  29. return 0;
  30. return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy);
  31. }
  32. static int vlan_validate(struct nlattr *tb[], struct nlattr *data[])
  33. {
  34. struct ifla_vlan_flags *flags;
  35. u16 id;
  36. int err;
  37. if (tb[IFLA_ADDRESS]) {
  38. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  39. return -EINVAL;
  40. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  41. return -EADDRNOTAVAIL;
  42. }
  43. if (!data)
  44. return -EINVAL;
  45. if (data[IFLA_VLAN_ID]) {
  46. id = nla_get_u16(data[IFLA_VLAN_ID]);
  47. if (id >= VLAN_VID_MASK)
  48. return -ERANGE;
  49. }
  50. if (data[IFLA_VLAN_FLAGS]) {
  51. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  52. if ((flags->flags & flags->mask) &
  53. ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
  54. return -EINVAL;
  55. }
  56. err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
  57. if (err < 0)
  58. return err;
  59. err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
  60. if (err < 0)
  61. return err;
  62. return 0;
  63. }
  64. static int vlan_changelink(struct net_device *dev,
  65. struct nlattr *tb[], struct nlattr *data[])
  66. {
  67. struct ifla_vlan_flags *flags;
  68. struct ifla_vlan_qos_mapping *m;
  69. struct nlattr *attr;
  70. int rem;
  71. if (data[IFLA_VLAN_FLAGS]) {
  72. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  73. vlan_dev_change_flags(dev, flags->flags, flags->mask);
  74. }
  75. if (data[IFLA_VLAN_INGRESS_QOS]) {
  76. nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
  77. m = nla_data(attr);
  78. vlan_dev_set_ingress_priority(dev, m->to, m->from);
  79. }
  80. }
  81. if (data[IFLA_VLAN_EGRESS_QOS]) {
  82. nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
  83. m = nla_data(attr);
  84. vlan_dev_set_egress_priority(dev, m->from, m->to);
  85. }
  86. }
  87. return 0;
  88. }
  89. static int vlan_get_tx_queues(struct net *net,
  90. struct nlattr *tb[],
  91. unsigned int *num_tx_queues,
  92. unsigned int *real_num_tx_queues)
  93. {
  94. struct net_device *real_dev;
  95. if (!tb[IFLA_LINK])
  96. return -EINVAL;
  97. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  98. if (!real_dev)
  99. return -ENODEV;
  100. *num_tx_queues = real_dev->num_tx_queues;
  101. *real_num_tx_queues = real_dev->real_num_tx_queues;
  102. return 0;
  103. }
  104. static int vlan_newlink(struct net_device *dev,
  105. struct nlattr *tb[], struct nlattr *data[])
  106. {
  107. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  108. struct net_device *real_dev;
  109. int err;
  110. if (!data[IFLA_VLAN_ID])
  111. return -EINVAL;
  112. if (!tb[IFLA_LINK])
  113. return -EINVAL;
  114. real_dev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
  115. if (!real_dev)
  116. return -ENODEV;
  117. vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
  118. vlan->real_dev = real_dev;
  119. vlan->flags = VLAN_FLAG_REORDER_HDR;
  120. err = vlan_check_real_dev(real_dev, vlan->vlan_id);
  121. if (err < 0)
  122. return err;
  123. if (!tb[IFLA_MTU])
  124. dev->mtu = real_dev->mtu;
  125. else if (dev->mtu > real_dev->mtu)
  126. return -EINVAL;
  127. err = vlan_changelink(dev, tb, data);
  128. if (err < 0)
  129. return err;
  130. return register_vlan_dev(dev);
  131. }
  132. static inline size_t vlan_qos_map_size(unsigned int n)
  133. {
  134. if (n == 0)
  135. return 0;
  136. /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
  137. return nla_total_size(sizeof(struct nlattr)) +
  138. nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
  139. }
  140. static size_t vlan_get_size(const struct net_device *dev)
  141. {
  142. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  143. return nla_total_size(2) + /* IFLA_VLAN_ID */
  144. sizeof(struct ifla_vlan_flags) + /* IFLA_VLAN_FLAGS */
  145. vlan_qos_map_size(vlan->nr_ingress_mappings) +
  146. vlan_qos_map_size(vlan->nr_egress_mappings);
  147. }
  148. static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  149. {
  150. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  151. struct vlan_priority_tci_mapping *pm;
  152. struct ifla_vlan_flags f;
  153. struct ifla_vlan_qos_mapping m;
  154. struct nlattr *nest;
  155. unsigned int i;
  156. NLA_PUT_U16(skb, IFLA_VLAN_ID, vlan_dev_info(dev)->vlan_id);
  157. if (vlan->flags) {
  158. f.flags = vlan->flags;
  159. f.mask = ~0;
  160. NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f);
  161. }
  162. if (vlan->nr_ingress_mappings) {
  163. nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
  164. if (nest == NULL)
  165. goto nla_put_failure;
  166. for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
  167. if (!vlan->ingress_priority_map[i])
  168. continue;
  169. m.from = i;
  170. m.to = vlan->ingress_priority_map[i];
  171. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  172. sizeof(m), &m);
  173. }
  174. nla_nest_end(skb, nest);
  175. }
  176. if (vlan->nr_egress_mappings) {
  177. nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
  178. if (nest == NULL)
  179. goto nla_put_failure;
  180. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  181. for (pm = vlan->egress_priority_map[i]; pm;
  182. pm = pm->next) {
  183. if (!pm->vlan_qos)
  184. continue;
  185. m.from = pm->priority;
  186. m.to = (pm->vlan_qos >> 13) & 0x7;
  187. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  188. sizeof(m), &m);
  189. }
  190. }
  191. nla_nest_end(skb, nest);
  192. }
  193. return 0;
  194. nla_put_failure:
  195. return -EMSGSIZE;
  196. }
  197. struct rtnl_link_ops vlan_link_ops __read_mostly = {
  198. .kind = "vlan",
  199. .maxtype = IFLA_VLAN_MAX,
  200. .policy = vlan_policy,
  201. .priv_size = sizeof(struct vlan_dev_info),
  202. .get_tx_queues = vlan_get_tx_queues,
  203. .setup = vlan_setup,
  204. .validate = vlan_validate,
  205. .newlink = vlan_newlink,
  206. .changelink = vlan_changelink,
  207. .dellink = unregister_vlan_dev,
  208. .get_size = vlan_get_size,
  209. .fill_info = vlan_fill_info,
  210. };
  211. int __init vlan_netlink_init(void)
  212. {
  213. return rtnl_link_register(&vlan_link_ops);
  214. }
  215. void __exit vlan_netlink_fini(void)
  216. {
  217. rtnl_link_unregister(&vlan_link_ops);
  218. }
  219. MODULE_ALIAS_RTNL_LINK("vlan");