vlan_netlink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. VLAN_FLAG_LOOSE_BINDING))
  55. return -EINVAL;
  56. }
  57. err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
  58. if (err < 0)
  59. return err;
  60. err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
  61. if (err < 0)
  62. return err;
  63. return 0;
  64. }
  65. static int vlan_changelink(struct net_device *dev,
  66. struct nlattr *tb[], struct nlattr *data[])
  67. {
  68. struct ifla_vlan_flags *flags;
  69. struct ifla_vlan_qos_mapping *m;
  70. struct nlattr *attr;
  71. int rem;
  72. if (data[IFLA_VLAN_FLAGS]) {
  73. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  74. vlan_dev_change_flags(dev, flags->flags, flags->mask);
  75. }
  76. if (data[IFLA_VLAN_INGRESS_QOS]) {
  77. nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
  78. m = nla_data(attr);
  79. vlan_dev_set_ingress_priority(dev, m->to, m->from);
  80. }
  81. }
  82. if (data[IFLA_VLAN_EGRESS_QOS]) {
  83. nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
  84. m = nla_data(attr);
  85. vlan_dev_set_egress_priority(dev, m->from, m->to);
  86. }
  87. }
  88. return 0;
  89. }
  90. static int vlan_get_tx_queues(struct net *net,
  91. struct nlattr *tb[],
  92. unsigned int *num_tx_queues,
  93. unsigned int *real_num_tx_queues)
  94. {
  95. struct net_device *real_dev;
  96. if (!tb[IFLA_LINK])
  97. return -EINVAL;
  98. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  99. if (!real_dev)
  100. return -ENODEV;
  101. *num_tx_queues = real_dev->num_tx_queues;
  102. *real_num_tx_queues = real_dev->real_num_tx_queues;
  103. return 0;
  104. }
  105. static int vlan_newlink(struct net *src_net, struct net_device *dev,
  106. struct nlattr *tb[], struct nlattr *data[])
  107. {
  108. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  109. struct net_device *real_dev;
  110. int err;
  111. if (!data[IFLA_VLAN_ID])
  112. return -EINVAL;
  113. if (!tb[IFLA_LINK])
  114. return -EINVAL;
  115. real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  116. if (!real_dev)
  117. return -ENODEV;
  118. vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
  119. vlan->real_dev = real_dev;
  120. vlan->flags = VLAN_FLAG_REORDER_HDR;
  121. err = vlan_check_real_dev(real_dev, vlan->vlan_id);
  122. if (err < 0)
  123. return err;
  124. if (!tb[IFLA_MTU])
  125. dev->mtu = real_dev->mtu;
  126. else if (dev->mtu > real_dev->mtu)
  127. return -EINVAL;
  128. err = vlan_changelink(dev, tb, data);
  129. if (err < 0)
  130. return err;
  131. return register_vlan_dev(dev);
  132. }
  133. static inline size_t vlan_qos_map_size(unsigned int n)
  134. {
  135. if (n == 0)
  136. return 0;
  137. /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
  138. return nla_total_size(sizeof(struct nlattr)) +
  139. nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
  140. }
  141. static size_t vlan_get_size(const struct net_device *dev)
  142. {
  143. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  144. return nla_total_size(2) + /* IFLA_VLAN_ID */
  145. sizeof(struct ifla_vlan_flags) + /* IFLA_VLAN_FLAGS */
  146. vlan_qos_map_size(vlan->nr_ingress_mappings) +
  147. vlan_qos_map_size(vlan->nr_egress_mappings);
  148. }
  149. static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  150. {
  151. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  152. struct vlan_priority_tci_mapping *pm;
  153. struct ifla_vlan_flags f;
  154. struct ifla_vlan_qos_mapping m;
  155. struct nlattr *nest;
  156. unsigned int i;
  157. NLA_PUT_U16(skb, IFLA_VLAN_ID, vlan_dev_info(dev)->vlan_id);
  158. if (vlan->flags) {
  159. f.flags = vlan->flags;
  160. f.mask = ~0;
  161. NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f);
  162. }
  163. if (vlan->nr_ingress_mappings) {
  164. nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
  165. if (nest == NULL)
  166. goto nla_put_failure;
  167. for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
  168. if (!vlan->ingress_priority_map[i])
  169. continue;
  170. m.from = i;
  171. m.to = vlan->ingress_priority_map[i];
  172. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  173. sizeof(m), &m);
  174. }
  175. nla_nest_end(skb, nest);
  176. }
  177. if (vlan->nr_egress_mappings) {
  178. nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
  179. if (nest == NULL)
  180. goto nla_put_failure;
  181. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  182. for (pm = vlan->egress_priority_map[i]; pm;
  183. pm = pm->next) {
  184. if (!pm->vlan_qos)
  185. continue;
  186. m.from = pm->priority;
  187. m.to = (pm->vlan_qos >> 13) & 0x7;
  188. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  189. sizeof(m), &m);
  190. }
  191. }
  192. nla_nest_end(skb, nest);
  193. }
  194. return 0;
  195. nla_put_failure:
  196. return -EMSGSIZE;
  197. }
  198. struct rtnl_link_ops vlan_link_ops __read_mostly = {
  199. .kind = "vlan",
  200. .maxtype = IFLA_VLAN_MAX,
  201. .policy = vlan_policy,
  202. .priv_size = sizeof(struct vlan_dev_info),
  203. .get_tx_queues = vlan_get_tx_queues,
  204. .setup = vlan_setup,
  205. .validate = vlan_validate,
  206. .newlink = vlan_newlink,
  207. .changelink = vlan_changelink,
  208. .dellink = unregister_vlan_dev,
  209. .get_size = vlan_get_size,
  210. .fill_info = vlan_fill_info,
  211. };
  212. int __init vlan_netlink_init(void)
  213. {
  214. return rtnl_link_register(&vlan_link_ops);
  215. }
  216. void __exit vlan_netlink_fini(void)
  217. {
  218. rtnl_link_unregister(&vlan_link_ops);
  219. }
  220. MODULE_ALIAS_RTNL_LINK("vlan");