br_netlink_tunnel.c 7.9 KB

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