vlan_netlink.c 6.7 KB

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