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_tunid_inrange(struct net_bridge_vlan
*v_curr
,
34 struct net_bridge_vlan
*v_last
)
36 __be32 tunid_curr
= tunnel_id_to_key32(v_curr
->tinfo
.tunnel_id
);
37 __be32 tunid_last
= tunnel_id_to_key32(v_last
->tinfo
.tunnel_id
);
39 return (be32_to_cpu(tunid_curr
) - be32_to_cpu(tunid_last
)) == 1;
42 static int __get_num_vlan_tunnel_infos(struct net_bridge_vlan_group
*vg
)
44 struct net_bridge_vlan
*v
, *vtbegin
= NULL
, *vtend
= 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
- vtend
->vid
) == 1 &&
56 vlan_tunid_inrange(v
, vtend
)) {
60 if ((vtend
->vid
- vtbegin
->vid
) > 0)
70 if (vtbegin
&& vtend
) {
71 if ((vtend
->vid
- vtbegin
->vid
) > 0)
80 int br_get_vlan_tunnel_info_size(struct net_bridge_vlan_group
*vg
)
88 num_tinfos
= __get_num_vlan_tunnel_infos(vg
);
91 return num_tinfos
* __get_vlan_tinfo_size();
94 static int br_fill_vlan_tinfo(struct sk_buff
*skb
, u16 vid
,
95 __be64 tunnel_id
, u16 flags
)
97 __be32 tid
= tunnel_id_to_key32(tunnel_id
);
100 tmap
= nla_nest_start(skb
, IFLA_BRIDGE_VLAN_TUNNEL_INFO
);
103 if (nla_put_u32(skb
, IFLA_BRIDGE_VLAN_TUNNEL_ID
,
105 goto nla_put_failure
;
106 if (nla_put_u16(skb
, IFLA_BRIDGE_VLAN_TUNNEL_VID
,
108 goto nla_put_failure
;
109 if (nla_put_u16(skb
, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
,
111 goto nla_put_failure
;
112 nla_nest_end(skb
, tmap
);
117 nla_nest_cancel(skb
, tmap
);
122 static int br_fill_vlan_tinfo_range(struct sk_buff
*skb
,
123 struct net_bridge_vlan
*vtbegin
,
124 struct net_bridge_vlan
*vtend
)
128 if (vtend
&& (vtend
->vid
- vtbegin
->vid
) > 0) {
129 /* add range to skb */
130 err
= br_fill_vlan_tinfo(skb
, vtbegin
->vid
,
131 vtbegin
->tinfo
.tunnel_id
,
132 BRIDGE_VLAN_INFO_RANGE_BEGIN
);
136 err
= br_fill_vlan_tinfo(skb
, vtend
->vid
,
137 vtend
->tinfo
.tunnel_id
,
138 BRIDGE_VLAN_INFO_RANGE_END
);
142 err
= br_fill_vlan_tinfo(skb
, vtbegin
->vid
,
143 vtbegin
->tinfo
.tunnel_id
,
152 int br_fill_vlan_tunnel_info(struct sk_buff
*skb
,
153 struct net_bridge_vlan_group
*vg
)
155 struct net_bridge_vlan
*vtbegin
= NULL
;
156 struct net_bridge_vlan
*vtend
= NULL
;
157 struct net_bridge_vlan
*v
;
160 /* Count number of vlan infos */
161 list_for_each_entry_rcu(v
, &vg
->vlan_list
, vlist
) {
162 /* only a context, bridge vlan not activated */
163 if (!br_vlan_should_use(v
))
166 if (!v
->tinfo
.tunnel_dst
)
171 } else if ((v
->vid
- vtend
->vid
) == 1 &&
172 vlan_tunid_inrange(v
, vtend
)) {
176 err
= br_fill_vlan_tinfo_range(skb
, vtbegin
, vtend
);
186 err
= br_fill_vlan_tinfo_range(skb
, vtbegin
, vtend
);
194 static const struct nla_policy vlan_tunnel_policy
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+ 1] = {
195 [IFLA_BRIDGE_VLAN_TUNNEL_ID
] = { .type
= NLA_U32
},
196 [IFLA_BRIDGE_VLAN_TUNNEL_VID
] = { .type
= NLA_U16
},
197 [IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
] = { .type
= NLA_U16
},
200 static int br_vlan_tunnel_info(struct net_bridge_port
*p
, int cmd
,
210 err
= nbp_vlan_tunnel_info_add(p
, vid
, tun_id
);
213 nbp_vlan_tunnel_info_delete(p
, vid
);
220 int br_parse_vlan_tunnel_info(struct nlattr
*attr
,
221 struct vtunnel_info
*tinfo
)
223 struct nlattr
*tb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+ 1];
228 memset(tinfo
, 0, sizeof(*tinfo
));
230 err
= nla_parse_nested(tb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
, attr
,
231 vlan_tunnel_policy
, NULL
);
235 if (!tb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
] ||
236 !tb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
239 tun_id
= nla_get_u32(tb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
240 vid
= nla_get_u16(tb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
241 if (vid
>= VLAN_VID_MASK
)
244 if (tb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
245 flags
= nla_get_u16(tb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
247 tinfo
->tunid
= tun_id
;
249 tinfo
->flags
= flags
;
254 int br_process_vlan_tunnel_info(struct net_bridge
*br
,
255 struct net_bridge_port
*p
, int cmd
,
256 struct vtunnel_info
*tinfo_curr
,
257 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
);
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
,
289 memset(tinfo_last
, 0, sizeof(struct vtunnel_info
));
290 memset(tinfo_curr
, 0, sizeof(struct vtunnel_info
));