vlan_netlink.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <linux/module.h>
  14. #include <net/net_namespace.h>
  15. #include <net/netlink.h>
  16. #include <net/rtnetlink.h>
  17. #include "vlan.h"
  18. static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
  19. [IFLA_VLAN_ID] = { .type = NLA_U16 },
  20. [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) },
  21. [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
  22. [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
  23. [IFLA_VLAN_PROTOCOL] = { .type = NLA_U16 },
  24. };
  25. static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
  26. [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
  27. };
  28. static inline int vlan_validate_qos_map(struct nlattr *attr)
  29. {
  30. if (!attr)
  31. return 0;
  32. return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy,
  33. NULL);
  34. }
  35. static int vlan_validate(struct nlattr *tb[], struct nlattr *data[],
  36. struct netlink_ext_ack *extack)
  37. {
  38. struct ifla_vlan_flags *flags;
  39. u16 id;
  40. int err;
  41. if (tb[IFLA_ADDRESS]) {
  42. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  43. return -EINVAL;
  44. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  45. return -EADDRNOTAVAIL;
  46. }
  47. if (!data)
  48. return -EINVAL;
  49. if (data[IFLA_VLAN_PROTOCOL]) {
  50. switch (nla_get_be16(data[IFLA_VLAN_PROTOCOL])) {
  51. case htons(ETH_P_8021Q):
  52. case htons(ETH_P_8021AD):
  53. break;
  54. default:
  55. return -EPROTONOSUPPORT;
  56. }
  57. }
  58. if (data[IFLA_VLAN_ID]) {
  59. id = nla_get_u16(data[IFLA_VLAN_ID]);
  60. if (id >= VLAN_VID_MASK)
  61. return -ERANGE;
  62. }
  63. if (data[IFLA_VLAN_FLAGS]) {
  64. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  65. if ((flags->flags & flags->mask) &
  66. ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
  67. VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP))
  68. return -EINVAL;
  69. }
  70. err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
  71. if (err < 0)
  72. return err;
  73. err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
  74. if (err < 0)
  75. return err;
  76. return 0;
  77. }
  78. static int vlan_changelink(struct net_device *dev, struct nlattr *tb[],
  79. struct nlattr *data[],
  80. struct netlink_ext_ack *extack)
  81. {
  82. struct ifla_vlan_flags *flags;
  83. struct ifla_vlan_qos_mapping *m;
  84. struct nlattr *attr;
  85. int rem, err;
  86. if (data[IFLA_VLAN_FLAGS]) {
  87. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  88. err = vlan_dev_change_flags(dev, flags->flags, flags->mask);
  89. if (err)
  90. return err;
  91. }
  92. if (data[IFLA_VLAN_INGRESS_QOS]) {
  93. nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
  94. m = nla_data(attr);
  95. vlan_dev_set_ingress_priority(dev, m->to, m->from);
  96. }
  97. }
  98. if (data[IFLA_VLAN_EGRESS_QOS]) {
  99. nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
  100. m = nla_data(attr);
  101. err = vlan_dev_set_egress_priority(dev, m->from, m->to);
  102. if (err)
  103. return err;
  104. }
  105. }
  106. return 0;
  107. }
  108. static int vlan_newlink(struct net *src_net, struct net_device *dev,
  109. struct nlattr *tb[], struct nlattr *data[],
  110. struct netlink_ext_ack *extack)
  111. {
  112. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  113. struct net_device *real_dev;
  114. unsigned int max_mtu;
  115. __be16 proto;
  116. int err;
  117. if (!data[IFLA_VLAN_ID])
  118. return -EINVAL;
  119. if (!tb[IFLA_LINK])
  120. return -EINVAL;
  121. real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  122. if (!real_dev)
  123. return -ENODEV;
  124. if (data[IFLA_VLAN_PROTOCOL])
  125. proto = nla_get_be16(data[IFLA_VLAN_PROTOCOL]);
  126. else
  127. proto = htons(ETH_P_8021Q);
  128. vlan->vlan_proto = proto;
  129. vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
  130. vlan->real_dev = real_dev;
  131. vlan->flags = VLAN_FLAG_REORDER_HDR;
  132. err = vlan_check_real_dev(real_dev, vlan->vlan_proto, vlan->vlan_id);
  133. if (err < 0)
  134. return err;
  135. max_mtu = netif_reduces_vlan_mtu(real_dev) ? real_dev->mtu - VLAN_HLEN :
  136. real_dev->mtu;
  137. if (!tb[IFLA_MTU])
  138. dev->mtu = max_mtu;
  139. else if (dev->mtu > max_mtu)
  140. return -EINVAL;
  141. err = vlan_changelink(dev, tb, data, extack);
  142. if (!err)
  143. err = register_vlan_dev(dev);
  144. if (err)
  145. vlan_dev_uninit(dev);
  146. return err;
  147. }
  148. static inline size_t vlan_qos_map_size(unsigned int n)
  149. {
  150. if (n == 0)
  151. return 0;
  152. /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
  153. return nla_total_size(sizeof(struct nlattr)) +
  154. nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
  155. }
  156. static size_t vlan_get_size(const struct net_device *dev)
  157. {
  158. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  159. return nla_total_size(2) + /* IFLA_VLAN_PROTOCOL */
  160. nla_total_size(2) + /* IFLA_VLAN_ID */
  161. nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
  162. vlan_qos_map_size(vlan->nr_ingress_mappings) +
  163. vlan_qos_map_size(vlan->nr_egress_mappings);
  164. }
  165. static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  166. {
  167. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  168. struct vlan_priority_tci_mapping *pm;
  169. struct ifla_vlan_flags f;
  170. struct ifla_vlan_qos_mapping m;
  171. struct nlattr *nest;
  172. unsigned int i;
  173. if (nla_put_be16(skb, IFLA_VLAN_PROTOCOL, vlan->vlan_proto) ||
  174. nla_put_u16(skb, IFLA_VLAN_ID, vlan->vlan_id))
  175. goto nla_put_failure;
  176. if (vlan->flags) {
  177. f.flags = vlan->flags;
  178. f.mask = ~0;
  179. if (nla_put(skb, IFLA_VLAN_FLAGS, sizeof(f), &f))
  180. goto nla_put_failure;
  181. }
  182. if (vlan->nr_ingress_mappings) {
  183. nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
  184. if (nest == NULL)
  185. goto nla_put_failure;
  186. for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
  187. if (!vlan->ingress_priority_map[i])
  188. continue;
  189. m.from = i;
  190. m.to = vlan->ingress_priority_map[i];
  191. if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
  192. sizeof(m), &m))
  193. goto nla_put_failure;
  194. }
  195. nla_nest_end(skb, nest);
  196. }
  197. if (vlan->nr_egress_mappings) {
  198. nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
  199. if (nest == NULL)
  200. goto nla_put_failure;
  201. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  202. for (pm = vlan->egress_priority_map[i]; pm;
  203. pm = pm->next) {
  204. if (!pm->vlan_qos)
  205. continue;
  206. m.from = pm->priority;
  207. m.to = (pm->vlan_qos >> 13) & 0x7;
  208. if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
  209. sizeof(m), &m))
  210. goto nla_put_failure;
  211. }
  212. }
  213. nla_nest_end(skb, nest);
  214. }
  215. return 0;
  216. nla_put_failure:
  217. return -EMSGSIZE;
  218. }
  219. static struct net *vlan_get_link_net(const struct net_device *dev)
  220. {
  221. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  222. return dev_net(real_dev);
  223. }
  224. struct rtnl_link_ops vlan_link_ops __read_mostly = {
  225. .kind = "vlan",
  226. .maxtype = IFLA_VLAN_MAX,
  227. .policy = vlan_policy,
  228. .priv_size = sizeof(struct vlan_dev_priv),
  229. .setup = vlan_setup,
  230. .validate = vlan_validate,
  231. .newlink = vlan_newlink,
  232. .changelink = vlan_changelink,
  233. .dellink = unregister_vlan_dev,
  234. .get_size = vlan_get_size,
  235. .fill_info = vlan_fill_info,
  236. .get_link_net = vlan_get_link_net,
  237. };
  238. int __init vlan_netlink_init(void)
  239. {
  240. return rtnl_link_register(&vlan_link_ops);
  241. }
  242. void __exit vlan_netlink_fini(void)
  243. {
  244. rtnl_link_unregister(&vlan_link_ops);
  245. }
  246. MODULE_ALIAS_RTNL_LINK("vlan");