vlan_netlink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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;
  101. if (data[IFLA_VLAN_FLAGS]) {
  102. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  103. vlan_dev_change_flags(dev, flags->flags, flags->mask);
  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. vlan_dev_set_egress_priority(dev, m->from, m->to);
  115. }
  116. }
  117. return 0;
  118. }
  119. static int vlan_newlink(struct net *src_net, struct net_device *dev,
  120. struct nlattr *tb[], struct nlattr *data[],
  121. struct netlink_ext_ack *extack)
  122. {
  123. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  124. struct net_device *real_dev;
  125. unsigned int max_mtu;
  126. __be16 proto;
  127. int err;
  128. if (!data[IFLA_VLAN_ID]) {
  129. NL_SET_ERR_MSG_MOD(extack, "VLAN id not specified");
  130. return -EINVAL;
  131. }
  132. if (!tb[IFLA_LINK]) {
  133. NL_SET_ERR_MSG_MOD(extack, "link not specified");
  134. return -EINVAL;
  135. }
  136. real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  137. if (!real_dev) {
  138. NL_SET_ERR_MSG_MOD(extack, "link does not exist");
  139. return -ENODEV;
  140. }
  141. if (data[IFLA_VLAN_PROTOCOL])
  142. proto = nla_get_be16(data[IFLA_VLAN_PROTOCOL]);
  143. else
  144. proto = htons(ETH_P_8021Q);
  145. vlan->vlan_proto = proto;
  146. vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
  147. vlan->real_dev = real_dev;
  148. dev->priv_flags |= (real_dev->priv_flags & IFF_XMIT_DST_RELEASE);
  149. vlan->flags = VLAN_FLAG_REORDER_HDR;
  150. err = vlan_check_real_dev(real_dev, vlan->vlan_proto, vlan->vlan_id,
  151. extack);
  152. if (err < 0)
  153. return err;
  154. max_mtu = netif_reduces_vlan_mtu(real_dev) ? real_dev->mtu - VLAN_HLEN :
  155. real_dev->mtu;
  156. if (!tb[IFLA_MTU])
  157. dev->mtu = max_mtu;
  158. else if (dev->mtu > max_mtu)
  159. return -EINVAL;
  160. err = vlan_changelink(dev, tb, data, extack);
  161. if (err < 0)
  162. return err;
  163. return register_vlan_dev(dev, extack);
  164. }
  165. static inline size_t vlan_qos_map_size(unsigned int n)
  166. {
  167. if (n == 0)
  168. return 0;
  169. /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
  170. return nla_total_size(sizeof(struct nlattr)) +
  171. nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
  172. }
  173. static size_t vlan_get_size(const struct net_device *dev)
  174. {
  175. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  176. return nla_total_size(2) + /* IFLA_VLAN_PROTOCOL */
  177. nla_total_size(2) + /* IFLA_VLAN_ID */
  178. nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
  179. vlan_qos_map_size(vlan->nr_ingress_mappings) +
  180. vlan_qos_map_size(vlan->nr_egress_mappings);
  181. }
  182. static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  183. {
  184. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  185. struct vlan_priority_tci_mapping *pm;
  186. struct ifla_vlan_flags f;
  187. struct ifla_vlan_qos_mapping m;
  188. struct nlattr *nest;
  189. unsigned int i;
  190. if (nla_put_be16(skb, IFLA_VLAN_PROTOCOL, vlan->vlan_proto) ||
  191. nla_put_u16(skb, IFLA_VLAN_ID, vlan->vlan_id))
  192. goto nla_put_failure;
  193. if (vlan->flags) {
  194. f.flags = vlan->flags;
  195. f.mask = ~0;
  196. if (nla_put(skb, IFLA_VLAN_FLAGS, sizeof(f), &f))
  197. goto nla_put_failure;
  198. }
  199. if (vlan->nr_ingress_mappings) {
  200. nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
  201. if (nest == NULL)
  202. goto nla_put_failure;
  203. for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
  204. if (!vlan->ingress_priority_map[i])
  205. continue;
  206. m.from = i;
  207. m.to = vlan->ingress_priority_map[i];
  208. if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
  209. sizeof(m), &m))
  210. goto nla_put_failure;
  211. }
  212. nla_nest_end(skb, nest);
  213. }
  214. if (vlan->nr_egress_mappings) {
  215. nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
  216. if (nest == NULL)
  217. goto nla_put_failure;
  218. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  219. for (pm = vlan->egress_priority_map[i]; pm;
  220. pm = pm->next) {
  221. if (!pm->vlan_qos)
  222. continue;
  223. m.from = pm->priority;
  224. m.to = (pm->vlan_qos >> 13) & 0x7;
  225. if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
  226. sizeof(m), &m))
  227. goto nla_put_failure;
  228. }
  229. }
  230. nla_nest_end(skb, nest);
  231. }
  232. return 0;
  233. nla_put_failure:
  234. return -EMSGSIZE;
  235. }
  236. static struct net *vlan_get_link_net(const struct net_device *dev)
  237. {
  238. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  239. return dev_net(real_dev);
  240. }
  241. struct rtnl_link_ops vlan_link_ops __read_mostly = {
  242. .kind = "vlan",
  243. .maxtype = IFLA_VLAN_MAX,
  244. .policy = vlan_policy,
  245. .priv_size = sizeof(struct vlan_dev_priv),
  246. .setup = vlan_setup,
  247. .validate = vlan_validate,
  248. .newlink = vlan_newlink,
  249. .changelink = vlan_changelink,
  250. .dellink = unregister_vlan_dev,
  251. .get_size = vlan_get_size,
  252. .fill_info = vlan_fill_info,
  253. .get_link_net = vlan_get_link_net,
  254. };
  255. int __init vlan_netlink_init(void)
  256. {
  257. return rtnl_link_register(&vlan_link_ops);
  258. }
  259. void __exit vlan_netlink_fini(void)
  260. {
  261. rtnl_link_unregister(&vlan_link_ops);
  262. }
  263. MODULE_ALIAS_RTNL_LINK("vlan");