br_netlink_tunnel.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_UNSPEC] = {
  162. .strict_start_type = IFLA_BRIDGE_VLAN_TUNNEL_FLAGS + 1
  163. },
  164. [IFLA_BRIDGE_VLAN_TUNNEL_ID] = { .type = NLA_U32 },
  165. [IFLA_BRIDGE_VLAN_TUNNEL_VID] = { .type = NLA_U16 },
  166. [IFLA_BRIDGE_VLAN_TUNNEL_FLAGS] = { .type = NLA_U16 },
  167. };
  168. int br_vlan_tunnel_info(const struct net_bridge_port *p, int cmd,
  169. u16 vid, u32 tun_id, bool *changed)
  170. {
  171. int err = 0;
  172. if (!p)
  173. return -EINVAL;
  174. switch (cmd) {
  175. case RTM_SETLINK:
  176. err = nbp_vlan_tunnel_info_add(p, vid, tun_id);
  177. if (!err)
  178. *changed = true;
  179. break;
  180. case RTM_DELLINK:
  181. if (!nbp_vlan_tunnel_info_delete(p, vid))
  182. *changed = true;
  183. break;
  184. }
  185. return err;
  186. }
  187. int br_parse_vlan_tunnel_info(struct nlattr *attr,
  188. struct vtunnel_info *tinfo)
  189. {
  190. struct nlattr *tb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
  191. u32 tun_id;
  192. u16 vid, flags = 0;
  193. int err;
  194. memset(tinfo, 0, sizeof(*tinfo));
  195. err = nla_parse_nested_deprecated(tb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
  196. attr, vlan_tunnel_policy, NULL);
  197. if (err < 0)
  198. return err;
  199. if (!tb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
  200. !tb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
  201. return -EINVAL;
  202. tun_id = nla_get_u32(tb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
  203. vid = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
  204. if (vid >= VLAN_VID_MASK)
  205. return -ERANGE;
  206. if (tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
  207. flags = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
  208. tinfo->tunid = tun_id;
  209. tinfo->vid = vid;
  210. tinfo->flags = flags;
  211. return 0;
  212. }
  213. /* send a notification if v_curr can't enter the range and start a new one */
  214. static void __vlan_tunnel_handle_range(const struct net_bridge_port *p,
  215. struct net_bridge_vlan **v_start,
  216. struct net_bridge_vlan **v_end,
  217. int v_curr, bool curr_change)
  218. {
  219. struct net_bridge_vlan_group *vg;
  220. struct net_bridge_vlan *v;
  221. vg = nbp_vlan_group(p);
  222. if (!vg)
  223. return;
  224. v = br_vlan_find(vg, v_curr);
  225. if (!*v_start)
  226. goto out_init;
  227. if (v && curr_change && br_vlan_can_enter_range(v, *v_end)) {
  228. *v_end = v;
  229. return;
  230. }
  231. br_vlan_notify(p->br, p, (*v_start)->vid, (*v_end)->vid, RTM_NEWVLAN);
  232. out_init:
  233. /* we start a range only if there are any changes to notify about */
  234. *v_start = curr_change ? v : NULL;
  235. *v_end = *v_start;
  236. }
  237. int br_process_vlan_tunnel_info(const struct net_bridge *br,
  238. const struct net_bridge_port *p, int cmd,
  239. struct vtunnel_info *tinfo_curr,
  240. struct vtunnel_info *tinfo_last,
  241. bool *changed)
  242. {
  243. int err;
  244. if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  245. if (tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN)
  246. return -EINVAL;
  247. memcpy(tinfo_last, tinfo_curr, sizeof(struct vtunnel_info));
  248. } else if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END) {
  249. struct net_bridge_vlan *v_start = NULL, *v_end = NULL;
  250. int t, v;
  251. if (!(tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN))
  252. return -EINVAL;
  253. if ((tinfo_curr->vid - tinfo_last->vid) !=
  254. (tinfo_curr->tunid - tinfo_last->tunid))
  255. return -EINVAL;
  256. t = tinfo_last->tunid;
  257. for (v = tinfo_last->vid; v <= tinfo_curr->vid; v++) {
  258. bool curr_change = false;
  259. err = br_vlan_tunnel_info(p, cmd, v, t, &curr_change);
  260. if (err)
  261. break;
  262. t++;
  263. if (curr_change)
  264. *changed = curr_change;
  265. __vlan_tunnel_handle_range(p, &v_start, &v_end, v,
  266. curr_change);
  267. }
  268. if (v_start && v_end)
  269. br_vlan_notify(br, p, v_start->vid, v_end->vid,
  270. RTM_NEWVLAN);
  271. if (err)
  272. return err;
  273. memset(tinfo_last, 0, sizeof(struct vtunnel_info));
  274. memset(tinfo_curr, 0, sizeof(struct vtunnel_info));
  275. } else {
  276. if (tinfo_last->flags)
  277. return -EINVAL;
  278. err = br_vlan_tunnel_info(p, cmd, tinfo_curr->vid,
  279. tinfo_curr->tunid, changed);
  280. if (err)
  281. return err;
  282. br_vlan_notify(br, p, tinfo_curr->vid, 0, RTM_NEWVLAN);
  283. memset(tinfo_last, 0, sizeof(struct vtunnel_info));
  284. memset(tinfo_curr, 0, sizeof(struct vtunnel_info));
  285. }
  286. return 0;
  287. }