2 * Bridge per vlan tunnel port dst_metadata netlink control interface
5 * Roopa Prabhu <roopa@cumulusnetworks.com>
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.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/etherdevice.h>
16 #include <net/rtnetlink.h>
17 #include <net/net_namespace.h>
19 #include <uapi/linux/if_bridge.h>
20 #include <net/dst_metadata.h>
22 #include "br_private.h"
23 #include "br_private_tunnel.h"
25 static size_t __get_vlan_tinfo_size(void)
27 return nla_total_size(0) + /* nest IFLA_BRIDGE_VLAN_TUNNEL_INFO */
28 nla_total_size(sizeof(u32)) + /* IFLA_BRIDGE_VLAN_TUNNEL_ID */
29 nla_total_size(sizeof(u16)) + /* IFLA_BRIDGE_VLAN_TUNNEL_VID */
30 nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_VLAN_TUNNEL_FLAGS */
33 static bool vlan_tunnel_id_isrange(struct net_bridge_vlan *v,
34 struct net_bridge_vlan *v_end)
36 __be32 tunid_curr = tunnel_id_to_key32(v->tinfo.tunnel_id);
37 __be32 tunid_end = tunnel_id_to_key32(v_end->tinfo.tunnel_id);
39 return (be32_to_cpu(tunid_curr) - be32_to_cpu(tunid_end)) == 1;
42 static int __get_num_vlan_tunnel_infos(struct net_bridge_vlan_group *vg)
44 struct net_bridge_vlan *v, *v_start = NULL, *v_end = NULL;
47 /* Count number of vlan infos */
48 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
49 /* only a context, bridge vlan not activated */
50 if (!br_vlan_should_use(v) || !v->tinfo.tunnel_id)
55 } else if ((v->vid - v_end->vid) == 1 &&
56 vlan_tunnel_id_isrange(v_end, v) == 1) {
60 if ((v_end->vid - v->vid) > 0 &&
61 vlan_tunnel_id_isrange(v_end, v) > 0)
72 if ((v_end->vid - v->vid) > 0 &&
73 vlan_tunnel_id_isrange(v_end, v) > 0)
82 int br_get_vlan_tunnel_info_size(struct net_bridge_vlan_group *vg)
90 num_tinfos = __get_num_vlan_tunnel_infos(vg);
93 return num_tinfos * __get_vlan_tinfo_size();
96 static int br_fill_vlan_tinfo(struct sk_buff *skb, u16 vid,
97 __be64 tunnel_id, u16 flags)
99 __be32 tid = tunnel_id_to_key32(tunnel_id);
102 tmap = nla_nest_start(skb, IFLA_BRIDGE_VLAN_TUNNEL_INFO);
105 if (nla_put_u32(skb, IFLA_BRIDGE_VLAN_TUNNEL_ID,
107 goto nla_put_failure;
108 if (nla_put_u16(skb, IFLA_BRIDGE_VLAN_TUNNEL_VID,
110 goto nla_put_failure;
111 if (nla_put_u16(skb, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS,
113 goto nla_put_failure;
114 nla_nest_end(skb, tmap);
119 nla_nest_cancel(skb, tmap);
124 static int br_fill_vlan_tinfo_range(struct sk_buff *skb,
125 struct net_bridge_vlan *vtbegin,
126 struct net_bridge_vlan *vtend)
130 if (vtend && (vtend->vid - vtbegin->vid) > 0) {
131 /* add range to skb */
132 err = br_fill_vlan_tinfo(skb, vtbegin->vid,
133 vtbegin->tinfo.tunnel_id,
134 BRIDGE_VLAN_INFO_RANGE_BEGIN);
138 err = br_fill_vlan_tinfo(skb, vtend->vid,
139 vtend->tinfo.tunnel_id,
140 BRIDGE_VLAN_INFO_RANGE_END);
144 err = br_fill_vlan_tinfo(skb, vtbegin->vid,
145 vtbegin->tinfo.tunnel_id,
154 int br_fill_vlan_tunnel_info(struct sk_buff *skb,
155 struct net_bridge_vlan_group *vg)
157 struct net_bridge_vlan *vtbegin = NULL;
158 struct net_bridge_vlan *vtend = NULL;
159 struct net_bridge_vlan *v;
162 /* Count number of vlan infos */
163 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
164 /* only a context, bridge vlan not activated */
165 if (!br_vlan_should_use(v))
168 if (!v->tinfo.tunnel_dst)
173 } else if ((v->vid - vtend->vid) == 1 &&
174 vlan_tunnel_id_isrange(v, vtend)) {
178 err = br_fill_vlan_tinfo_range(skb, vtbegin, vtend);
188 err = br_fill_vlan_tinfo_range(skb, vtbegin, vtend);
196 static const struct nla_policy vlan_tunnel_policy[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1] = {
197 [IFLA_BRIDGE_VLAN_TUNNEL_ID] = { .type = NLA_U32 },
198 [IFLA_BRIDGE_VLAN_TUNNEL_VID] = { .type = NLA_U16 },
199 [IFLA_BRIDGE_VLAN_TUNNEL_FLAGS] = { .type = NLA_U16 },
202 static int br_vlan_tunnel_info(struct net_bridge_port *p, int cmd,
212 err = nbp_vlan_tunnel_info_add(p, vid, tun_id);
215 nbp_vlan_tunnel_info_delete(p, vid);
222 int br_parse_vlan_tunnel_info(struct nlattr *attr,
223 struct vtunnel_info *tinfo)
225 struct nlattr *tb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
230 memset(tinfo, 0, sizeof(*tinfo));
232 err = nla_parse_nested(tb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
233 attr, vlan_tunnel_policy);
237 if (!tb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
238 !tb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
241 tun_id = nla_get_u32(tb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
242 vid = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
243 if (vid >= VLAN_VID_MASK)
246 if (tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
247 flags = nla_get_u16(tb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
249 tinfo->tunid = tun_id;
251 tinfo->flags = flags;
256 int br_process_vlan_tunnel_info(struct net_bridge *br,
257 struct net_bridge_port *p, int cmd,
258 struct vtunnel_info *tinfo_curr,
259 struct vtunnel_info *tinfo_last)
263 if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
264 if (tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN)
266 memcpy(tinfo_last, tinfo_curr, sizeof(struct vtunnel_info));
267 } else if (tinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END) {
270 if (!(tinfo_last->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN))
272 if ((tinfo_curr->vid - tinfo_last->vid) !=
273 (tinfo_curr->tunid - tinfo_last->tunid))
275 t = tinfo_last->tunid;
276 for (v = tinfo_last->vid; v <= tinfo_curr->vid; v++) {
277 err = br_vlan_tunnel_info(p, cmd, v, t);
282 memset(tinfo_last, 0, sizeof(struct vtunnel_info));
283 memset(tinfo_curr, 0, sizeof(struct vtunnel_info));
285 if (tinfo_last->flags)
287 err = br_vlan_tunnel_info(p, cmd, tinfo_curr->vid,
291 memset(tinfo_last, 0, sizeof(struct vtunnel_info));
292 memset(tinfo_curr, 0, sizeof(struct vtunnel_info));