vlan_netlink.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. };
  24. static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
  25. [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
  26. };
  27. static inline int vlan_validate_qos_map(struct nlattr *attr)
  28. {
  29. if (!attr)
  30. return 0;
  31. return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy);
  32. }
  33. static int vlan_validate(struct nlattr *tb[], struct nlattr *data[])
  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. return -EINVAL;
  41. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  42. return -EADDRNOTAVAIL;
  43. }
  44. if (!data)
  45. return -EINVAL;
  46. if (data[IFLA_VLAN_ID]) {
  47. id = nla_get_u16(data[IFLA_VLAN_ID]);
  48. if (id >= VLAN_VID_MASK)
  49. return -ERANGE;
  50. }
  51. if (data[IFLA_VLAN_FLAGS]) {
  52. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  53. if ((flags->flags & flags->mask) &
  54. ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
  55. VLAN_FLAG_LOOSE_BINDING))
  56. return -EINVAL;
  57. }
  58. err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
  59. if (err < 0)
  60. return err;
  61. err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
  62. if (err < 0)
  63. return err;
  64. return 0;
  65. }
  66. static int vlan_changelink(struct net_device *dev,
  67. struct nlattr *tb[], struct nlattr *data[])
  68. {
  69. struct ifla_vlan_flags *flags;
  70. struct ifla_vlan_qos_mapping *m;
  71. struct nlattr *attr;
  72. int rem;
  73. if (data[IFLA_VLAN_FLAGS]) {
  74. flags = nla_data(data[IFLA_VLAN_FLAGS]);
  75. vlan_dev_change_flags(dev, flags->flags, flags->mask);
  76. }
  77. if (data[IFLA_VLAN_INGRESS_QOS]) {
  78. nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
  79. m = nla_data(attr);
  80. vlan_dev_set_ingress_priority(dev, m->to, m->from);
  81. }
  82. }
  83. if (data[IFLA_VLAN_EGRESS_QOS]) {
  84. nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
  85. m = nla_data(attr);
  86. vlan_dev_set_egress_priority(dev, m->from, m->to);
  87. }
  88. }
  89. return 0;
  90. }
  91. static int vlan_newlink(struct net *src_net, struct net_device *dev,
  92. struct nlattr *tb[], struct nlattr *data[])
  93. {
  94. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  95. struct net_device *real_dev;
  96. int err;
  97. if (!data[IFLA_VLAN_ID])
  98. return -EINVAL;
  99. if (!tb[IFLA_LINK])
  100. return -EINVAL;
  101. real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  102. if (!real_dev)
  103. return -ENODEV;
  104. vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
  105. vlan->real_dev = real_dev;
  106. vlan->flags = VLAN_FLAG_REORDER_HDR;
  107. err = vlan_check_real_dev(real_dev, vlan->vlan_id);
  108. if (err < 0)
  109. return err;
  110. if (!tb[IFLA_MTU])
  111. dev->mtu = real_dev->mtu;
  112. else if (dev->mtu > real_dev->mtu)
  113. return -EINVAL;
  114. err = vlan_changelink(dev, tb, data);
  115. if (err < 0)
  116. return err;
  117. return register_vlan_dev(dev);
  118. }
  119. static inline size_t vlan_qos_map_size(unsigned int n)
  120. {
  121. if (n == 0)
  122. return 0;
  123. /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
  124. return nla_total_size(sizeof(struct nlattr)) +
  125. nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
  126. }
  127. static size_t vlan_get_size(const struct net_device *dev)
  128. {
  129. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  130. return nla_total_size(2) + /* IFLA_VLAN_ID */
  131. nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
  132. vlan_qos_map_size(vlan->nr_ingress_mappings) +
  133. vlan_qos_map_size(vlan->nr_egress_mappings);
  134. }
  135. static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  136. {
  137. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  138. struct vlan_priority_tci_mapping *pm;
  139. struct ifla_vlan_flags f;
  140. struct ifla_vlan_qos_mapping m;
  141. struct nlattr *nest;
  142. unsigned int i;
  143. NLA_PUT_U16(skb, IFLA_VLAN_ID, vlan_dev_priv(dev)->vlan_id);
  144. if (vlan->flags) {
  145. f.flags = vlan->flags;
  146. f.mask = ~0;
  147. NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f);
  148. }
  149. if (vlan->nr_ingress_mappings) {
  150. nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
  151. if (nest == NULL)
  152. goto nla_put_failure;
  153. for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
  154. if (!vlan->ingress_priority_map[i])
  155. continue;
  156. m.from = i;
  157. m.to = vlan->ingress_priority_map[i];
  158. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  159. sizeof(m), &m);
  160. }
  161. nla_nest_end(skb, nest);
  162. }
  163. if (vlan->nr_egress_mappings) {
  164. nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
  165. if (nest == NULL)
  166. goto nla_put_failure;
  167. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  168. for (pm = vlan->egress_priority_map[i]; pm;
  169. pm = pm->next) {
  170. if (!pm->vlan_qos)
  171. continue;
  172. m.from = pm->priority;
  173. m.to = (pm->vlan_qos >> 13) & 0x7;
  174. NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
  175. sizeof(m), &m);
  176. }
  177. }
  178. nla_nest_end(skb, nest);
  179. }
  180. return 0;
  181. nla_put_failure:
  182. return -EMSGSIZE;
  183. }
  184. struct rtnl_link_ops vlan_link_ops __read_mostly = {
  185. .kind = "vlan",
  186. .maxtype = IFLA_VLAN_MAX,
  187. .policy = vlan_policy,
  188. .priv_size = sizeof(struct vlan_dev_priv),
  189. .setup = vlan_setup,
  190. .validate = vlan_validate,
  191. .newlink = vlan_newlink,
  192. .changelink = vlan_changelink,
  193. .dellink = unregister_vlan_dev,
  194. .get_size = vlan_get_size,
  195. .fill_info = vlan_fill_info,
  196. };
  197. int __init vlan_netlink_init(void)
  198. {
  199. return rtnl_link_register(&vlan_link_ops);
  200. }
  201. void __exit vlan_netlink_fini(void)
  202. {
  203. rtnl_link_unregister(&vlan_link_ops);
  204. }
  205. MODULE_ALIAS_RTNL_LINK("vlan");