vlan_netlink.c 7.8 KB

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