vlan_netlink.c 7.8 KB

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