vlan_netlink.c 6.8 KB

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