blob: b903689a8fc5e7cc9032ab598231139628ef084e [file] [log] [blame]
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001#include <linux/kernel.h>
2#include <linux/netdevice.h>
3#include <linux/rtnetlink.h>
4#include <linux/slab.h>
Scott Feldman7f109532015-06-12 17:39:50 -07005#include <net/switchdev.h>
Vlad Yasevich243a2e62013-02-13 12:00:09 +00006
7#include "br_private.h"
Roopa Prabhuefa53562017-01-31 22:59:54 -08008#include "br_private_tunnel.h"
Vlad Yasevich243a2e62013-02-13 12:00:09 +00009
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020010static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
11 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000012{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020013 const struct net_bridge_vlan *vle = ptr;
14 u16 vid = *(u16 *)arg->key;
15
16 return vle->vid != vid;
17}
18
19static const struct rhashtable_params br_vlan_rht_params = {
20 .head_offset = offsetof(struct net_bridge_vlan, vnode),
21 .key_offset = offsetof(struct net_bridge_vlan, vid),
22 .key_len = sizeof(u16),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020023 .nelem_hint = 3,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020024 .max_size = VLAN_N_VID,
25 .obj_cmpfn = br_vlan_cmp,
26 .automatic_shrinking = true,
27};
28
29static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
30{
31 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
32}
33
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030034static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020035{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020036 if (vg->pvid == vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030037 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000038
39 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020040 vg->pvid = vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030041
42 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000043}
44
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030045static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000046{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020047 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030048 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000049
50 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020051 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030052
53 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000054}
55
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030056/* return true if anything changed, false otherwise */
57static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000058{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020059 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030060 u16 old_flags = v->flags;
61 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020062
63 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020064 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020065 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020066 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020067
68 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030069 ret = __vlan_add_pvid(vg, v->vid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020070 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030071 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000072
73 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020074 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040075 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020076 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030077
78 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000079}
80
Scott Feldman7f109532015-06-12 17:39:50 -070081static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +000082 struct net_bridge_vlan *v, u16 flags,
83 struct netlink_ext_ack *extack)
Scott Feldman7f109532015-06-12 17:39:50 -070084{
Scott Feldman7f109532015-06-12 17:39:50 -070085 int err;
86
Jiri Pirko0944d6b2015-10-09 13:54:11 +020087 /* Try switchdev op first. In case it is not supported, fallback to
88 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070089 */
Ido Schimmel27973792019-01-08 16:48:11 +000090 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020091 if (err == -EOPNOTSUPP)
Ido Schimmel27973792019-01-08 16:48:11 +000092 return vlan_vid_add(dev, br->vlan_proto, v->vid);
93 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
Scott Feldman7f109532015-06-12 17:39:50 -070094 return err;
95}
96
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020097static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000098{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020099 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200100 struct list_head *headp, *hpos;
101 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000102
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200103 if (br_vlan_is_master(v))
104 vg = br_vlan_group(v->br);
105 else
106 vg = nbp_vlan_group(v->port);
107
108 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200109 list_for_each_prev(hpos, headp) {
110 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
111 if (v->vid < vent->vid)
112 continue;
113 else
114 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000115 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200116 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200117}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000118
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200119static void __vlan_del_list(struct net_bridge_vlan *v)
120{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200121 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000122}
123
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400124static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +0000125 const struct net_bridge_vlan *v)
Scott Feldman7f109532015-06-12 17:39:50 -0700126{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200127 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700128
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200129 /* Try switchdev op first. In case it is not supported, fallback to
130 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700131 */
Ido Schimmel27973792019-01-08 16:48:11 +0000132 err = br_switchdev_port_vlan_del(dev, v->vid);
133 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
134 vlan_vid_del(dev, br->vlan_proto, v->vid);
135 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman7f109532015-06-12 17:39:50 -0700136}
137
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200138/* Returns a master vlan, if it didn't exist it gets created. In all cases a
139 * a reference is taken to the master vlan before returning.
140 */
Petr Machata169327d2018-12-12 17:02:50 +0000141static struct net_bridge_vlan *
142br_vlan_get_master(struct net_bridge *br, u16 vid,
143 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200144{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200145 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200146 struct net_bridge_vlan *masterv;
147
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200148 vg = br_vlan_group(br);
149 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200150 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300151 bool changed;
152
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200153 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000154 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200155 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200156 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200157 if (WARN_ON(!masterv))
158 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200159 refcount_set(&masterv->refcnt, 1);
160 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200161 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300162 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200163
164 return masterv;
165}
166
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200167static void br_master_vlan_rcu_free(struct rcu_head *rcu)
168{
169 struct net_bridge_vlan *v;
170
171 v = container_of(rcu, struct net_bridge_vlan, rcu);
172 WARN_ON(!br_vlan_is_master(v));
173 free_percpu(v->stats);
174 v->stats = NULL;
175 kfree(v);
176}
177
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200178static void br_vlan_put_master(struct net_bridge_vlan *masterv)
179{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200180 struct net_bridge_vlan_group *vg;
181
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200182 if (!br_vlan_is_master(masterv))
183 return;
184
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200185 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300186 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200187 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200188 &masterv->vnode, br_vlan_rht_params);
189 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200190 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200191 }
192}
193
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300194static void nbp_vlan_rcu_free(struct rcu_head *rcu)
195{
196 struct net_bridge_vlan *v;
197
198 v = container_of(rcu, struct net_bridge_vlan, rcu);
199 WARN_ON(br_vlan_is_master(v));
200 /* if we had per-port stats configured then free them here */
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200201 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300202 free_percpu(v->stats);
203 v->stats = NULL;
204 kfree(v);
205}
206
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200207/* This is the shared VLAN add function which works for both ports and bridge
208 * devices. There are four possible calls to this function in terms of the
209 * vlan entry type:
210 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100211 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200212 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100213 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200214 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100215 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200216 * will be used for filtering in both the port and the bridge
217 */
Petr Machata169327d2018-12-12 17:02:50 +0000218static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
219 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000220{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200221 struct net_bridge_vlan *masterv = NULL;
222 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200223 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200224 struct net_device *dev;
225 struct net_bridge *br;
226 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000227
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200228 if (br_vlan_is_master(v)) {
229 br = v->br;
230 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200231 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200232 } else {
233 p = v->port;
234 br = p->br;
235 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200236 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200237 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000238
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200239 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200240 /* Add VLAN to the device filter if it is supported.
241 * This ensures tagged traffic enters the bridge when
242 * promiscuous mode is disabled by br_manage_promisc().
243 */
Ido Schimmel27973792019-01-08 16:48:11 +0000244 err = __vlan_vid_add(dev, br, v, flags, extack);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400245 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200246 goto out;
247
248 /* need to work on the master vlan too */
249 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300250 bool changed;
251
252 err = br_vlan_add(br, v->vid,
253 flags | BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000254 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200255 if (err)
256 goto out_filt;
257 }
258
Petr Machata169327d2018-12-12 17:02:50 +0000259 masterv = br_vlan_get_master(br, v->vid, extack);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200260 if (!masterv)
261 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200262 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300263 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
264 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
265 if (!v->stats) {
266 err = -ENOMEM;
267 goto out_filt;
268 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200269 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300270 } else {
271 v->stats = masterv->stats;
272 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200273 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000274 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200275 if (err && err != -EOPNOTSUPP)
276 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900277 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000278
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200279 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200280 if (br_vlan_should_use(v)) {
281 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
282 if (err) {
283 br_err(br, "failed insert local address into bridge forwarding table\n");
284 goto out_filt;
285 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200286 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000287 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200289 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
290 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200291 if (err)
292 goto out_fdb_insert;
293
294 __vlan_add_list(v);
295 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200296out:
297 return err;
298
299out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200300 if (br_vlan_should_use(v)) {
301 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
302 vg->num_vlans--;
303 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200304
305out_filt:
306 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000307 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200308 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800309 if (v->stats && masterv->stats != v->stats)
310 free_percpu(v->stats);
311 v->stats = NULL;
312
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200313 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200314 v->brvlan = NULL;
315 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200316 } else {
317 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200318 }
319
320 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000321}
322
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200323static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000324{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200326 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200327 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200328 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329
330 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200331 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 } else {
333 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200334 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200335 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 }
337
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200338 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200339 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000340 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200341 if (err)
342 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200343 } else {
344 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
345 if (err && err != -EOPNOTSUPP)
346 goto out;
347 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200348 }
349
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200350 if (br_vlan_should_use(v)) {
351 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
352 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200353 }
354
355 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800356 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200357 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
358 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 __vlan_del_list(v);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300360 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200361 }
362
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200363 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200364out:
365 return err;
366}
367
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200368static void __vlan_group_free(struct net_bridge_vlan_group *vg)
369{
370 WARN_ON(!list_empty(&vg->vlan_list));
371 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800372 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200373 kfree(vg);
374}
375
376static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200377{
378 struct net_bridge_vlan *vlan, *tmp;
379
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200380 __vlan_delete_pvid(vg, vg->pvid);
381 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200382 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000383}
384
Vlad Yasevich78851982013-02-13 12:00:14 +0000385struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800386 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200387 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000388 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000389{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200390 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200391 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000392 u16 vid;
393
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400394 /* If this packet was not filtered at input, let it pass */
395 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000396 goto out;
397
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200398 /* At this point, we know that the frame was filtered and contains
399 * a valid vlan id. If the vlan id has untagged flag set,
400 * send untagged; otherwise, send tagged.
401 */
402 br_vlan_get_tag(skb, &vid);
403 v = br_vlan_find(vg, vid);
404 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400405 * only exception is the bridge is set in promisc mode and the
406 * packet is destined for the bridge device. In this case
407 * pass the packet as is.
408 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200409 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400410 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
411 goto out;
412 } else {
413 kfree_skb(skb);
414 return NULL;
415 }
416 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300417 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200418 stats = this_cpu_ptr(v->stats);
419 u64_stats_update_begin(&stats->syncp);
420 stats->tx_bytes += skb->len;
421 stats->tx_packets++;
422 u64_stats_update_end(&stats->syncp);
423 }
424
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200425 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100426 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800427
428 if (p && (p->flags & BR_VLAN_TUNNEL) &&
429 br_handle_egress_vlan_tunnel(skb, v)) {
430 kfree_skb(skb);
431 return NULL;
432 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000433out:
434 return skb;
435}
436
437/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200438static bool __allowed_ingress(const struct net_bridge *br,
439 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200440 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000441{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200442 struct br_vlan_stats *stats;
443 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900444 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000445
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400446 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900447 /* If vlan tx offload is disabled on bridge device and frame was
448 * sent from vlan device on the bridge device, it does not have
449 * HW accelerated vlan tag.
450 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100451 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200452 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400453 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900454 if (unlikely(!skb))
455 return false;
456 }
457
Toshiaki Makita8580e212014-06-10 20:59:23 +0900458 if (!br_vlan_get_tag(skb, vid)) {
459 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200460 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900461 /* Protocol-mismatch, empty out vlan_tci for new tag */
462 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100463 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100464 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900465 if (unlikely(!skb))
466 return false;
467
468 skb_pull(skb, ETH_HLEN);
469 skb_reset_mac_len(skb);
470 *vid = 0;
471 tagged = false;
472 } else {
473 tagged = true;
474 }
475 } else {
476 /* Untagged frame */
477 tagged = false;
478 }
479
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900480 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200481 u16 pvid = br_get_pvid(vg);
482
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900483 /* Frame had a tag with VID 0 or did not have a tag.
484 * See if pvid is set on this port. That tells us which
485 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000486 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400487 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900488 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000489
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900490 /* PVID is set on this port. Any untagged or priority-tagged
491 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000492 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900493 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900494 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900495 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200496 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900497 else
498 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100499 * At this point, we know that skb->vlan_tci VID
500 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900501 * We update only VID field and preserve PCP field.
502 */
503 skb->vlan_tci |= pvid;
504
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200505 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300506 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200507 return true;
508 }
509 v = br_vlan_find(vg, *vid);
510 if (!v || !br_vlan_should_use(v))
511 goto drop;
512
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300513 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200514 stats = this_cpu_ptr(v->stats);
515 u64_stats_update_begin(&stats->syncp);
516 stats->rx_bytes += skb->len;
517 stats->rx_packets++;
518 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000519 }
520
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200521 return true;
522
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900523drop:
524 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000525 return false;
526}
527
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200528bool br_allowed_ingress(const struct net_bridge *br,
529 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
530 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200531{
532 /* If VLAN filtering is disabled on the bridge, all packets are
533 * permitted.
534 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300535 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
537 return true;
538 }
539
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200540 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200541}
542
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000543/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200544bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000545 const struct sk_buff *skb)
546{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200547 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000548 u16 vid;
549
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400550 /* If this packet was not filtered at input, let it pass */
551 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000552 return true;
553
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000554 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200555 v = br_vlan_find(vg, vid);
556 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000557 return true;
558
559 return false;
560}
561
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900562/* Called under RCU */
563bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
564{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200565 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900566 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900567
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400568 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300569 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900570 return true;
571
Ido Schimmeleca1e002015-11-01 18:31:45 +0200572 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200573 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900574 return false;
575
Toshiaki Makita8580e212014-06-10 20:59:23 +0900576 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
577 *vid = 0;
578
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900579 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200580 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400581 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900582 return false;
583
584 return true;
585 }
586
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200587 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900588 return true;
589
590 return false;
591}
592
Petr Machatadbd6dc72018-05-30 02:56:55 +0200593static int br_vlan_add_existing(struct net_bridge *br,
594 struct net_bridge_vlan_group *vg,
595 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000596 u16 flags, bool *changed,
597 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200598{
599 int err;
600
Petr Machata169327d2018-12-12 17:02:50 +0000601 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200602 if (err && err != -EOPNOTSUPP)
603 return err;
604
Petr Machatadbd6dc72018-05-30 02:56:55 +0200605 if (!br_vlan_is_brentry(vlan)) {
606 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200607 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
608 err = -EINVAL;
609 goto err_flags;
610 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200611 /* It was only kept for port vlans, now make it real */
612 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
613 vlan->vid);
614 if (err) {
615 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200616 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200617 }
618
619 refcount_inc(&vlan->refcnt);
620 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
621 vg->num_vlans++;
622 *changed = true;
623 }
624
625 if (__vlan_add_flags(vlan, flags))
626 *changed = true;
627
628 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200629
630err_fdb_insert:
631err_flags:
632 br_switchdev_port_vlan_del(br->dev, vlan->vid);
633 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200634}
635
Toshiaki Makita8adff412013-10-16 17:07:13 +0900636/* Must be protected by RTNL.
637 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300638 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900639 */
Petr Machata169327d2018-12-12 17:02:50 +0000640int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
641 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000642{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200643 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200644 struct net_bridge_vlan *vlan;
645 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000646
647 ASSERT_RTNL();
648
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300649 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200650 vg = br_vlan_group(br);
651 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200652 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000653 return br_vlan_add_existing(br, vg, vlan, flags, changed,
654 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000655
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200656 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
657 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000658 return -ENOMEM;
659
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200660 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
661 if (!vlan->stats) {
662 kfree(vlan);
663 return -ENOMEM;
664 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200665 vlan->vid = vid;
666 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
667 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
668 vlan->br = br;
669 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300670 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000671 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200672 if (ret) {
673 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200674 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300675 } else {
676 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200677 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000678
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200679 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000680}
681
Toshiaki Makita8adff412013-10-16 17:07:13 +0900682/* Must be protected by RTNL.
683 * Must be called with vid in range from 1 to 4094 inclusive.
684 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000685int br_vlan_delete(struct net_bridge *br, u16 vid)
686{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200687 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200688 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000689
690 ASSERT_RTNL();
691
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200692 vg = br_vlan_group(br);
693 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200694 if (!v || !br_vlan_is_brentry(v))
695 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000696
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900697 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700698 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000699
Roopa Prabhuefa53562017-01-31 22:59:54 -0800700 vlan_tunnel_info_del(vg, v);
701
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200702 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000703}
704
705void br_vlan_flush(struct net_bridge *br)
706{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200707 struct net_bridge_vlan_group *vg;
708
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000709 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000710
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200711 vg = br_vlan_group(br);
712 __vlan_flush(vg);
713 RCU_INIT_POINTER(br->vlgrp, NULL);
714 synchronize_rcu();
715 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000716}
717
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200718struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900719{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200720 if (!vg)
721 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900722
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200723 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900724}
725
Toshiaki Makita204177f2014-06-10 20:59:25 +0900726/* Must be protected by RTNL. */
727static void recalculate_group_addr(struct net_bridge *br)
728{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300729 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900730 return;
731
732 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300733 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
734 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900735 /* Bridge Group Address */
736 br->group_addr[5] = 0x00;
737 } else { /* vlan_enabled && ETH_P_8021AD */
738 /* Provider Bridge Group Address */
739 br->group_addr[5] = 0x08;
740 }
741 spin_unlock_bh(&br->lock);
742}
743
744/* Must be protected by RTNL. */
745void br_recalculate_fwd_mask(struct net_bridge *br)
746{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300747 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
748 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900749 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
750 else /* vlan_enabled && ETH_P_8021AD */
751 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
752 ~(1u << br->group_addr[5]);
753}
754
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300755int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000756{
Elad Raz6b72a772016-01-06 13:01:06 +0100757 struct switchdev_attr attr = {
758 .orig_dev = br->dev,
759 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
760 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
761 .u.vlan_filtering = val,
762 };
763 int err;
764
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300765 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300766 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000767
Elad Raz6b72a772016-01-06 13:01:06 +0100768 err = switchdev_port_attr_set(br->dev, &attr);
769 if (err && err != -EOPNOTSUPP)
770 return err;
771
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300772 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400773 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900774 recalculate_group_addr(br);
775 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000776
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300777 return 0;
778}
779
780int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
781{
Xin Long047831a2016-04-09 00:03:31 +0800782 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000783}
784
Ido Schimmel1f514452017-05-26 08:37:23 +0200785bool br_vlan_enabled(const struct net_device *dev)
786{
787 struct net_bridge *br = netdev_priv(dev);
788
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300789 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200790}
791EXPORT_SYMBOL_GPL(br_vlan_enabled);
792
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900793int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900794{
795 int err = 0;
796 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200797 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200798 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900799 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900800
Toshiaki Makita204177f2014-06-10 20:59:25 +0900801 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900802 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900803
804 /* Add VLANs for the new proto to the device filter. */
805 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200806 vg = nbp_vlan_group(p);
807 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200808 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900809 if (err)
810 goto err_filt;
811 }
812 }
813
814 oldproto = br->vlan_proto;
815 br->vlan_proto = proto;
816
817 recalculate_group_addr(br);
818 br_recalculate_fwd_mask(br);
819
820 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200821 list_for_each_entry(p, &br->port_list, list) {
822 vg = nbp_vlan_group(p);
823 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200824 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200825 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900826
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900827 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900828
829err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200830 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200831 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900832
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200833 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
834 vg = nbp_vlan_group(p);
835 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200836 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200837 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900838
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900839 return err;
840}
841
842int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
843{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900844 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
845 return -EPROTONOSUPPORT;
846
Xin Long047831a2016-04-09 00:03:31 +0800847 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900848}
849
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200850int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
851{
852 switch (val) {
853 case 0:
854 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300855 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200856 break;
857 default:
858 return -EINVAL;
859 }
860
861 return 0;
862}
863
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300864int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
865{
866 struct net_bridge_port *p;
867
868 /* allow to change the option if there are no port vlans configured */
869 list_for_each_entry(p, &br->port_list, list) {
870 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
871
872 if (vg->num_vlans)
873 return -EBUSY;
874 }
875
876 switch (val) {
877 case 0:
878 case 1:
879 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
880 break;
881 default:
882 return -EINVAL;
883 }
884
885 return 0;
886}
887
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200888static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400889{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200890 struct net_bridge_vlan *v;
891
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200892 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200893 return false;
894
895 v = br_vlan_lookup(&vg->vlan_hash, vid);
896 if (v && br_vlan_should_use(v) &&
897 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
898 return true;
899
900 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400901}
902
903static void br_vlan_disable_default_pvid(struct net_bridge *br)
904{
905 struct net_bridge_port *p;
906 u16 pvid = br->default_pvid;
907
908 /* Disable default_pvid on all ports where it is still
909 * configured.
910 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200911 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400912 br_vlan_delete(br, pvid);
913
914 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200915 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400916 nbp_vlan_delete(p, pvid);
917 }
918
919 br->default_pvid = 0;
920}
921
Petr Machata169327d2018-12-12 17:02:50 +0000922int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
923 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400924{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200925 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200926 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400927 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300928 unsigned long *changed;
929 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400930 u16 old_pvid;
931 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400932
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200933 if (!pvid) {
934 br_vlan_disable_default_pvid(br);
935 return 0;
936 }
937
Andy Shevchenko459479d2018-08-30 13:33:18 +0300938 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400939 if (!changed)
940 return -ENOMEM;
941
942 old_pvid = br->default_pvid;
943
944 /* Update default_pvid config only if we do not conflict with
945 * user configuration.
946 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200947 vg = br_vlan_group(br);
948 pvent = br_vlan_find(vg, pvid);
949 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200950 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400951 err = br_vlan_add(br, pvid,
952 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200953 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300954 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000955 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400956 if (err)
957 goto out;
958 br_vlan_delete(br, old_pvid);
959 set_bit(0, changed);
960 }
961
962 list_for_each_entry(p, &br->port_list, list) {
963 /* Update default_pvid config only if we do not conflict with
964 * user configuration.
965 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200966 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400967 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200968 !vlan_default_pvid(vg, old_pvid)) ||
969 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400970 continue;
971
972 err = nbp_vlan_add(p, pvid,
973 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300974 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000975 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400976 if (err)
977 goto err_port;
978 nbp_vlan_delete(p, old_pvid);
979 set_bit(p->port_no, changed);
980 }
981
982 br->default_pvid = pvid;
983
984out:
Andy Shevchenko459479d2018-08-30 13:33:18 +0300985 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400986 return err;
987
988err_port:
989 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
990 if (!test_bit(p->port_no, changed))
991 continue;
992
993 if (old_pvid)
994 nbp_vlan_add(p, old_pvid,
995 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300996 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000997 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400998 nbp_vlan_delete(p, pvid);
999 }
1000
1001 if (test_bit(0, changed)) {
1002 if (old_pvid)
1003 br_vlan_add(br, old_pvid,
1004 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001005 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001006 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001007 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001008 br_vlan_delete(br, pvid);
1009 }
1010 goto out;
1011}
1012
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001013int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1014{
1015 u16 pvid = val;
1016 int err = 0;
1017
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001018 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001019 return -EINVAL;
1020
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001021 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001022 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001023
1024 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001025 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001026 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1027 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001028 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001029 }
Petr Machata169327d2018-12-12 17:02:50 +00001030 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001031out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001032 return err;
1033}
1034
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001035int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001036{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001037 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001038 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001039 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001040
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001041 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1042 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001043 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001044 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001045 if (ret)
1046 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001047 ret = vlan_tunnel_init(vg);
1048 if (ret)
1049 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001050 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001051 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001052 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001053 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001054 ret = br_vlan_add(br, 1,
1055 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Petr Machata169327d2018-12-12 17:02:50 +00001056 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001057 if (ret)
1058 goto err_vlan_add;
1059
1060out:
1061 return ret;
1062
1063err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -08001064 vlan_tunnel_deinit(vg);
1065err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001066 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001067err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001068 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001069
1070 goto out;
1071}
1072
Petr Machata169327d2018-12-12 17:02:50 +00001073int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001074{
Elad Raz404cdbf2016-01-06 13:01:07 +01001075 struct switchdev_attr attr = {
1076 .orig_dev = p->br->dev,
1077 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1078 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001079 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001080 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001081 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001082 int ret = -ENOMEM;
1083
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001084 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1085 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001086 goto out;
1087
Elad Raz404cdbf2016-01-06 13:01:07 +01001088 ret = switchdev_port_attr_set(p->dev, &attr);
1089 if (ret && ret != -EOPNOTSUPP)
1090 goto err_vlan_enabled;
1091
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001092 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001093 if (ret)
1094 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001095 ret = vlan_tunnel_init(vg);
1096 if (ret)
1097 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001098 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001099 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001100 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001101 bool changed;
1102
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001103 ret = nbp_vlan_add(p, p->br->default_pvid,
1104 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001105 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001106 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001107 if (ret)
1108 goto err_vlan_add;
1109 }
1110out:
1111 return ret;
1112
1113err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001114 RCU_INIT_POINTER(p->vlgrp, NULL);
1115 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001116 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001117err_tunnel_init:
1118 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001119err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001120err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001121 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001122
1123 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001124}
1125
Toshiaki Makita8adff412013-10-16 17:07:13 +09001126/* Must be protected by RTNL.
1127 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001128 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001129 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001130int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001131 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001132{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001133 struct net_bridge_vlan *vlan;
1134 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001135
1136 ASSERT_RTNL();
1137
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001138 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001139 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001140 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001141 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001142 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001143 if (ret && ret != -EOPNOTSUPP)
1144 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001145 *changed = __vlan_add_flags(vlan, flags);
1146
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001147 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001148 }
1149
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001150 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1151 if (!vlan)
1152 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001153
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001154 vlan->vid = vid;
1155 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001156 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001157 if (ret)
1158 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001159 else
1160 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001161
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001162 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001163}
1164
Toshiaki Makita8adff412013-10-16 17:07:13 +09001165/* Must be protected by RTNL.
1166 * Must be called with vid in range from 1 to 4094 inclusive.
1167 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001168int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1169{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001170 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001171
1172 ASSERT_RTNL();
1173
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001174 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001175 if (!v)
1176 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001177 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001178 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001179
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001180 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001181}
1182
1183void nbp_vlan_flush(struct net_bridge_port *port)
1184{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001185 struct net_bridge_vlan_group *vg;
1186
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001187 ASSERT_RTNL();
1188
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001189 vg = nbp_vlan_group(port);
1190 __vlan_flush(vg);
1191 RCU_INIT_POINTER(port->vlgrp, NULL);
1192 synchronize_rcu();
1193 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001194}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001195
1196void br_vlan_get_stats(const struct net_bridge_vlan *v,
1197 struct br_vlan_stats *stats)
1198{
1199 int i;
1200
1201 memset(stats, 0, sizeof(*stats));
1202 for_each_possible_cpu(i) {
1203 u64 rxpackets, rxbytes, txpackets, txbytes;
1204 struct br_vlan_stats *cpu_stats;
1205 unsigned int start;
1206
1207 cpu_stats = per_cpu_ptr(v->stats, i);
1208 do {
1209 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1210 rxpackets = cpu_stats->rx_packets;
1211 rxbytes = cpu_stats->rx_bytes;
1212 txbytes = cpu_stats->tx_bytes;
1213 txpackets = cpu_stats->tx_packets;
1214 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1215
1216 stats->rx_packets += rxpackets;
1217 stats->rx_bytes += rxbytes;
1218 stats->tx_bytes += txbytes;
1219 stats->tx_packets += txpackets;
1220 }
1221}
Petr Machata4d4fd362018-04-29 10:56:08 +03001222
1223int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1224{
1225 struct net_bridge_vlan_group *vg;
Ido Schimmel5a6db042018-11-28 20:06:58 +00001226 struct net_bridge_port *p;
Petr Machata4d4fd362018-04-29 10:56:08 +03001227
1228 ASSERT_RTNL();
Ido Schimmel5a6db042018-11-28 20:06:58 +00001229 p = br_port_get_check_rtnl(dev);
1230 if (p)
1231 vg = nbp_vlan_group(p);
1232 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001233 vg = br_vlan_group(netdev_priv(dev));
1234 else
1235 return -EINVAL;
1236
1237 *p_pvid = br_get_pvid(vg);
1238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1241
1242int br_vlan_get_info(const struct net_device *dev, u16 vid,
1243 struct bridge_vlan_info *p_vinfo)
1244{
1245 struct net_bridge_vlan_group *vg;
1246 struct net_bridge_vlan *v;
1247 struct net_bridge_port *p;
1248
1249 ASSERT_RTNL();
1250 p = br_port_get_check_rtnl(dev);
1251 if (p)
1252 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001253 else if (netif_is_bridge_master(dev))
1254 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001255 else
1256 return -EINVAL;
1257
1258 v = br_vlan_find(vg, vid);
1259 if (!v)
1260 return -ENOENT;
1261
1262 p_vinfo->vid = vid;
1263 p_vinfo->flags = v->flags;
1264 return 0;
1265}
1266EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001267
1268static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1269{
1270 return is_vlan_dev(dev) &&
1271 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1272}
1273
1274static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1275 __always_unused void *data)
1276{
1277 return br_vlan_is_bind_vlan_dev(dev);
1278}
1279
1280static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1281{
1282 int found;
1283
1284 rcu_read_lock();
1285 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1286 NULL);
1287 rcu_read_unlock();
1288
1289 return !!found;
1290}
1291
1292struct br_vlan_bind_walk_data {
1293 u16 vid;
1294 struct net_device *result;
1295};
1296
1297static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1298 void *data_in)
1299{
1300 struct br_vlan_bind_walk_data *data = data_in;
1301 int found = 0;
1302
1303 if (br_vlan_is_bind_vlan_dev(dev) &&
1304 vlan_dev_priv(dev)->vlan_id == data->vid) {
1305 data->result = dev;
1306 found = 1;
1307 }
1308
1309 return found;
1310}
1311
1312static struct net_device *
1313br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1314{
1315 struct br_vlan_bind_walk_data data = {
1316 .vid = vid,
1317 };
1318
1319 rcu_read_lock();
1320 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1321 &data);
1322 rcu_read_unlock();
1323
1324 return data.result;
1325}
1326
1327static bool br_vlan_is_dev_up(const struct net_device *dev)
1328{
1329 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1330}
1331
1332static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1333 struct net_device *vlan_dev)
1334{
1335 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1336 struct net_bridge_vlan_group *vg;
1337 struct net_bridge_port *p;
1338 bool has_carrier = false;
1339
1340 list_for_each_entry(p, &br->port_list, list) {
1341 vg = nbp_vlan_group(p);
1342 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1343 has_carrier = true;
1344 break;
1345 }
1346 }
1347
1348 if (has_carrier)
1349 netif_carrier_on(vlan_dev);
1350 else
1351 netif_carrier_off(vlan_dev);
1352}
1353
1354static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1355{
1356 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1357 struct net_bridge_vlan *vlan;
1358 struct net_device *vlan_dev;
1359
1360 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1361 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1362 vlan->vid);
1363 if (vlan_dev) {
1364 if (br_vlan_is_dev_up(p->dev))
1365 netif_carrier_on(vlan_dev);
1366 else
1367 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1368 }
1369 }
1370}
1371
1372static void br_vlan_upper_change(struct net_device *dev,
1373 struct net_device *upper_dev,
1374 bool linking)
1375{
1376 struct net_bridge *br = netdev_priv(dev);
1377
1378 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1379 return;
1380
1381 if (linking) {
1382 br_vlan_set_vlan_dev_state(br, upper_dev);
1383 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1384 } else {
1385 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1386 br_vlan_has_upper_bind_vlan_dev(dev));
1387 }
1388}
1389
1390/* Must be protected by RTNL. */
1391void br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1392 void *ptr)
1393{
1394 struct netdev_notifier_changeupper_info *info;
1395
1396 switch (event) {
1397 case NETDEV_CHANGEUPPER:
1398 info = ptr;
1399 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1400 break;
1401 }
1402}
1403
1404/* Must be protected by RTNL. */
1405void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1406{
1407 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1408 return;
1409
1410 switch (event) {
1411 case NETDEV_CHANGE:
1412 case NETDEV_DOWN:
1413 case NETDEV_UP:
1414 br_vlan_set_all_vlan_dev_state(p);
1415 break;
1416 }
1417}