vlan_netlink.c 5.8 KB

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