1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bridge per vlan tunnel port dst_metadata netlink control interface
6 * Roopa Prabhu <roopa@cumulusnetworks.com>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/etherdevice.h>
12 #include <net/rtnetlink.h>
13 #include <net/net_namespace.h>
15 #include <uapi/linux/if_bridge.h>
16 #include <net/dst_metadata.h>
18 #include "br_private.h"
19 #include "br_private_tunnel.h"
21 static size_t __get_vlan_tinfo_size(void)
23 return nla_total_size(0) + /* nest IFLA_BRIDGE_VLAN_TUNNEL_INFO */
24 nla_total_size(sizeof(u32
)) + /* IFLA_BRIDGE_VLAN_TUNNEL_ID */
25 nla_total_size(sizeof(u16
)) + /* IFLA_BRIDGE_VLAN_TUNNEL_VID */
26 nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_VLAN_TUNNEL_FLAGS */
29 static bool vlan_tunid_inrange(struct net_bridge_vlan
*v_curr
,
30 struct net_bridge_vlan
*v_last
)
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
);
35 return (be32_to_cpu(tunid_curr
) - be32_to_cpu(tunid_last
)) == 1;
38 static int __get_num_vlan_tunnel_infos(struct net_bridge_vlan_group
*vg
)
40 struct net_bridge_vlan
*v
, *vtbegin
= NULL
, *vtend
= NULL
;
43 /* Count number of vlan infos */
44 list_for_each_entry_rcu(v
, &vg
->vlan_list
, vlist
) {
45 /* only a context, bridge vlan not activated */
46 if (!br_vlan_should_use(v
) || !v
->tinfo
.tunnel_id
)
51 } else if ((v
->vid
- vtend
->vid
) == 1 &&
52 vlan_tunid_inrange(v
, vtend
)) {
56 if ((vtend
->vid
- vtbegin
->vid
) > 0)
66 if (vtbegin
&& vtend
) {
67 if ((vtend
->vid
- vtbegin
->vid
) > 0)
76 int br_get_vlan_tunnel_info_size(struct net_bridge_vlan_group
*vg
)
84 num_tinfos
= __get_num_vlan_tunnel_infos(vg
);
87 return num_tinfos
* __get_vlan_tinfo_size();
90 static int br_fill_vlan_tinfo(struct sk_buff
*skb
, u16 vid
,
91 __be64 tunnel_id
, u16 flags
)
93 __be32 tid
= tunnel_id_to_key32(tunnel_id
);
96 tmap
= nla_nest_start_noflag(skb
, IFLA_BRIDGE_VLAN_TUNNEL_INFO
);
99 if (nla_put_u32(skb
, IFLA_BRIDGE_VLAN_TUNNEL_ID
,
101 goto nla_put_failure
;
102 if (nla_put_u16(skb
, IFLA_BRIDGE_VLAN_TUNNEL_VID
,
104 goto nla_put_failure
;
105 if (nla_put_u16(skb
, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
,
107 goto nla_put_failure
;
108 nla_nest_end(skb
, tmap
);
113 nla_nest_cancel(skb
, tmap
);
118 static int br_fill_vlan_tinfo_range(struct sk_buff
*skb
,
119 struct net_bridge_vlan
*vtbegin
,
120 struct net_bridge_vlan
*vtend
)
124 if (vtend
&& (vtend
->vid
- vtbegin
->vid
) > 0) {
125 /* add range to skb */
126 err
= br_fill_vlan_tinfo(skb
, vtbegin
->vid
,
127 vtbegin
->tinfo
.tunnel_id
,
128 BRIDGE_VLAN_INFO_RANGE_BEGIN
);
132 err
= br_fill_vlan_tinfo(skb
, vtend
->vid
,
133 vtend
->tinfo
.tunnel_id
,
134 BRIDGE_VLAN_INFO_RANGE_END
);
138 err
= br_fill_vlan_tinfo(skb
, vtbegin
->vid
,
139 vtbegin
->tinfo
.tunnel_id
,
148 int br_fill_vlan_tunnel_info(struct sk_buff
*skb
,
149 struct net_bridge_vlan_group
*vg
)
151 struct net_bridge_vlan
*vtbegin
= NULL
;
152 struct net_bridge_vlan
*vtend
= NULL
;
153 struct net_bridge_vlan
*v
;
156 /* Count number of vlan infos */
157 list_for_each_entry_rcu(v
, &vg
->vlan_list
, vlist
) {
158 /* only a context, bridge vlan not activated */
159 if (!br_vlan_should_use(v
))
162 if (!v
->tinfo
.tunnel_dst
)
167 } else if ((v
->vid
- vtend
->vid
) == 1 &&
168 vlan_tunid_inrange(v
, vtend
)) {
172 err
= br_fill_vlan_tinfo_range(skb
, vtbegin
, vtend
);
182 err
= br_fill_vlan_tinfo_range(skb
, vtbegin
, vtend
);
190 static const struct nla_policy vlan_tunnel_policy
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+ 1] = {
191 [IFLA_BRIDGE_VLAN_TUNNEL_ID
] = { .type
= NLA_U32
},
192 [IFLA_BRIDGE_VLAN_TUNNEL_VID
] = { .type
= NLA_U16
},
193 [IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
] = { .type
= NLA_U16
},
196 static int br_vlan_tunnel_info(struct net_bridge_port
*p
, int cmd
,
197 u16 vid
, u32 tun_id
, bool *changed
)
206 err
= nbp_vlan_tunnel_info_add(p
, vid
, tun_id
);
211 if (!nbp_vlan_tunnel_info_delete(p
, vid
))
219 int br_parse_vlan_tunnel_info(struct nlattr
*attr
,
220 struct vtunnel_info
*tinfo
)
222 struct nlattr
*tb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+ 1];
227 memset(tinfo
, 0, sizeof(*tinfo
));
229 err
= nla_parse_nested_deprecated(tb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
,
230 attr
, vlan_tunnel_policy
, NULL
);
234 if (!tb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
] ||
235 !tb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
238 tun_id
= nla_get_u32(tb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
239 vid
= nla_get_u16(tb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
240 if (vid
>= VLAN_VID_MASK
)
243 if (tb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
244 flags
= nla_get_u16(tb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
246 tinfo
->tunid
= tun_id
;
248 tinfo
->flags
= flags
;
253 int br_process_vlan_tunnel_info(struct net_bridge
*br
,
254 struct net_bridge_port
*p
, int cmd
,
255 struct vtunnel_info
*tinfo_curr
,
256 struct vtunnel_info
*tinfo_last
,
261 if (tinfo_curr
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
262 if (tinfo_last
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
)
264 memcpy(tinfo_last
, tinfo_curr
, sizeof(struct vtunnel_info
));
265 } else if (tinfo_curr
->flags
& BRIDGE_VLAN_INFO_RANGE_END
) {
268 if (!(tinfo_last
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
))
270 if ((tinfo_curr
->vid
- tinfo_last
->vid
) !=
271 (tinfo_curr
->tunid
- tinfo_last
->tunid
))
273 t
= tinfo_last
->tunid
;
274 for (v
= tinfo_last
->vid
; v
<= tinfo_curr
->vid
; v
++) {
275 err
= br_vlan_tunnel_info(p
, cmd
, v
, t
, changed
);
280 memset(tinfo_last
, 0, sizeof(struct vtunnel_info
));
281 memset(tinfo_curr
, 0, sizeof(struct vtunnel_info
));
283 if (tinfo_last
->flags
)
285 err
= br_vlan_tunnel_info(p
, cmd
, tinfo_curr
->vid
,
286 tinfo_curr
->tunid
, changed
);
289 memset(tinfo_last
, 0, sizeof(struct vtunnel_info
));
290 memset(tinfo_curr
, 0, sizeof(struct vtunnel_info
));