br_netlink_tunnel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Bridge per vlan tunnel port dst_metadata netlink control interface
  3. *
  4. * Authors:
  5. * Roopa Prabhu <roopa@cumulusnetworks.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/net_namespace.h>
  17. #include <net/sock.h>
  18. #include <uapi/linux/if_bridge.h>
  19. #include <net/dst_metadata.h>
  20. #include "br_private.h"
  21. #include "br_private_tunnel.h"
  22. static size_t __get_vlan_tinfo_size(void)
  23. {
  24. return nla_total_size(0) + /* nest IFLA_BRIDGE_VLAN_TUNNEL_INFO */
  25. nla_total_size(sizeof(u32)) + /* IFLA_BRIDGE_VLAN_TUNNEL_ID */
  26. nla_total_size(sizeof(u16)) + /* IFLA_BRIDGE_VLAN_TUNNEL_VID */
  27. nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_VLAN_TUNNEL_FLAGS */
  28. }
  29. static bool vlan_tunid_inrange(struct net_bridge_vlan *v_curr,
  30. struct net_bridge_vlan *v_last)
  31. {
  32. __be32 tunid_curr = tunnel_id_to_key32(v_curr->tinfo.tunnel_id);
  33. __be32 tunid_last = tunnel_id_to_key32(v_last->tinfo.tunnel_id);
  34. return (be32_to_cpu(tunid_curr) - be32_to_cpu(tunid_last)) == 1;
  35. }
  36. static int __get_num_vlan_tunnel_infos(struct net_bridge_vlan_group *vg)
  37. {
  38. struct net_bridge_vlan *v, *vtbegin = NULL, *vtend = NULL;
  39. int num_tinfos = 0;
  40. /* Count number of vlan infos */
  41. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  42. /* only a context, bridge vlan not activated */
  43. if (!br_vlan_should_use(v) || !v->tinfo.tunnel_id)
  44. continue;
  45. if (!vtbegin) {
  46. goto initvars;
  47. } else if ((v->vid - vtend->vid) == 1 &&
  48. vlan_tunid_inrange(v, vtend)) {
  49. vtend = v;
  50. continue;
  51. } else {
  52. if ((vtend->vid - vtbegin->vid) > 0)
  53. num_tinfos += 2;
  54. else
  55. num_tinfos += 1;
  56. }
  57. initvars:
  58. vtbegin = v;
  59. vtend = v;
  60. }
  61. if (vtbegin && vtend) {
  62. if ((vtend->vid - vtbegin->vid) > 0)
  63. num_tinfos += 2;
  64. else
  65. num_tinfos += 1;
  66. }
  67. return num_tinfos;
  68. }
  69. int br_get_vlan_tunnel_info_size(struct net_bridge_vlan_group *vg)
  70. {
  71. int num_tinfos;
  72. if (!vg)
  73. return 0;
  74. rcu_read_lock();
  75. num_tinfos = __get_num_vlan_tunnel_infos(vg);
  76. rcu_read_unlock();
  77. return num_tinfos * __get_vlan_tinfo_size();
  78. }
  79. static int br_fill_vlan_tinfo(struct sk_buff *skb, u16 vid,
  80. __be64 tunnel_id, u16 flags)
  81. {
  82. __be32 tid = tunnel_id_to_key32(tunnel_id);
  83. struct nlattr *tmap;
  84. tmap = nla_nest_start(skb, IFLA_BRIDGE_VLAN_TUNNEL_INFO);
  85. if (!tmap)
  86. return -EMSGSIZE;
  87. if (nla_put_u32(skb, IFLA_BRIDGE_VLAN_TUNNEL_ID,
  88. be32_to_cpu(tid)))
  89. goto nla_put_failure;
  90. if (nla_put_u16(skb, IFLA_BRIDGE_VLAN_TUNNEL_VID,
  91. vid))
  92. goto nla_put_failure;
  93. if (nla_put_u16(skb, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS,
  94. flags))
  95. goto nla_put_failure;
  96. nla_nest_end(skb, tmap);
  97. return 0;
  98. nla_put_failure:
  99. nla_nest_cancel(skb, tmap);
  100. return -EMSGSIZE;
  101. }
  102. static int br_fill_vlan_tinfo_range(struct sk_buff *skb,
  103. struct net_bridge_vlan *vtbegin,
  104. struct net_bridge_vlan *vtend)
  105. {
  106. int err;
  107. if (vtend && (vtend->vid - vtbegin->vid) > 0) {
  108. /* add range to skb */
  109. err = br_fill_vlan_tinfo(skb, vtbegin->vid,
  110. vtbegin->tinfo.tunnel_id,
  111. BRIDGE_VLAN_INFO_RANGE_BEGIN);
  112. if (err)
  113. return err;
  114. err = br_fill_vlan_tinfo(skb, vtend->vid,
  115. vtend->tinfo.tunnel_id,
  116. BRIDGE_VLAN_INFO_RANGE_END);
  117. if (err)
  118. return err;
  119. } else {
  120. err = br_fill_vlan_tinfo(skb, vtbegin->vid,
  121. vtbegin->tinfo.tunnel_id,
  122. 0);
  123. if (err)
  124. return err;
  125. }
  126. return 0;
  127. }
  128. int br_fill_vlan_tunnel_info(struct sk_buff *skb,
  129. struct net_bridge_vlan_group *vg)
  130. {
  131. struct net_bridge_vlan *vtbegin = NULL;
  132. struct net_bridge_vlan *vtend = NULL;
  133. struct net_bridge_vlan *v;
  134. int err;
  135. /* Count number of vlan infos */
  136. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  137. /* only a context, bridge vlan not activated */
  138. if (!br_vlan_should_use(v))
  139. continue;
  140. if (!v->tinfo.tunnel_dst)
  141. continue;
  142. if (!vtbegin) {
  143. goto initvars;
  144. } else if ((v->vid - vtend->vid) == 1 &&
  145. vlan_tunid_inrange(v, vtend)) {
  146. vtend = v;
  147. continue;
  148. } else {
  149. err = br_fill_vlan_tinfo_range(skb, vtbegin, vtend);
  150. if (err)
  151. return err;
  152. }
  153. initvars:
  154. vtbegin = v;
  155. vtend = v;
  156. }
  157. if (vtbegin) {
  158. err = br_fill_vlan_tinfo_range(skb, vtbegin, vtend);
  159. if (err)
  160. return err;
  161. }
  162. return 0;
  163. }
  164. static const struct nla_policy vlan_tunnel_policy[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1] = {
  165. [IFLA_BRIDGE_VLAN_TUNNEL_ID] = { .type = NLA_U32 },
  166. [IFLA_BRIDGE_VLAN_TUNNEL_VID] = { .type = NLA_U16 },
  167. [IFLA_BRIDGE_VLAN_TUNNEL_FLAGS] = { .type = NLA_U16 },
  168. };
  169. static int br_vlan_tunnel_info(struct net_bridge_port *p, int cmd,
  170. u16 vid, u32 tun_id, bool *changed)
  171. {
  172. int err = 0;
  173. if (!p)
  174. return -EINVAL;
  175. switch (cmd) {
  176. case RTM_SETLINK:
  177. err = nbp_vlan_tunnel_info_add(p, vid, tun_id);
  178. if (!err)
  179. *changed = true;
  180. break;
  181. case RTM_DELLINK:
  182. if (!nbp_vlan_tunnel_info_delete(p, vid))
  183. *changed = true;
  184. break;
  185. }
  186. return err;
  187. }
  188. int br_parse_vlan_tunnel_info(struct nlattr *attr,
  189. struct vtunnel_info *tinfo)
  190. {
  191. struct nlattr *tb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
  192. u32 tun_id;
  193. u16 vid, flags = 0;
  194. int err;
  195. memset(tinfo, 0, sizeof(*tinfo));
  196. err = nla_parse_nested(tb, IFLA_BRIDGE_VLAN_TUNNEL_MAX, attr,
  197. vlan_tunnel_policy, NULL);
  198. if (err < 0)
  199. return err;
  200. if (!tb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
  201. !tb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
  202. return -EINVAL;
  203. tun_id = nla_get_u32(tb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
  204. vid = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
  205. if (vid >= VLAN_VID_MASK)
  206. return -ERANGE;
  207. if (tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
  208. flags = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
  209. tinfo->tunid = tun_id;
  210. tinfo->vid = vid;
  211. tinfo->flags = flags;
  212. return 0;
  213. }
  214. int br_process_vlan_tunnel_info(struct net_bridge *br,
  215. struct net_bridge_port *p, int cmd,
  216. struct vtunnel_info *tinfo_curr,
  217. struct vtunnel_info *tinfo_last,
  218. bool *changed)
  219. {
  220. int err;
  221. if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  222. if (tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN)
  223. return -EINVAL;
  224. memcpy(tinfo_last, tinfo_curr, sizeof(struct vtunnel_info));
  225. } else if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  226. int t, v;
  227. if (!(tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN))
  228. return -EINVAL;
  229. if ((tinfo_curr->vid - tinfo_last->vid) !=
  230. (tinfo_curr->tunid - tinfo_last->tunid))
  231. return -EINVAL;
  232. t = tinfo_last->tunid;
  233. for (v = tinfo_last->vid; v <= tinfo_curr->vid; v++) {
  234. err = br_vlan_tunnel_info(p, cmd, v, t, changed);
  235. if (err)
  236. return err;
  237. t++;
  238. }
  239. memset(tinfo_last, 0, sizeof(struct vtunnel_info));
  240. memset(tinfo_curr, 0, sizeof(struct vtunnel_info));
  241. } else {
  242. if (tinfo_last->flags)
  243. return -EINVAL;
  244. err = br_vlan_tunnel_info(p, cmd, tinfo_curr->vid,
  245. tinfo_curr->tunid, changed);
  246. if (err)
  247. return err;
  248. memset(tinfo_last, 0, sizeof(struct vtunnel_info));
  249. memset(tinfo_curr, 0, sizeof(struct vtunnel_info));
  250. }
  251. return 0;
  252. }