blob: 2db63997f313533662f5280f3afca5a2bd3ae22a [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
Mike Manning80900ac2019-04-18 18:35:34 +010010static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);
11
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020012static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
13 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000014{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020015 const struct net_bridge_vlan *vle = ptr;
16 u16 vid = *(u16 *)arg->key;
17
18 return vle->vid != vid;
19}
20
21static const struct rhashtable_params br_vlan_rht_params = {
22 .head_offset = offsetof(struct net_bridge_vlan, vnode),
23 .key_offset = offsetof(struct net_bridge_vlan, vid),
24 .key_len = sizeof(u16),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020025 .nelem_hint = 3,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020026 .max_size = VLAN_N_VID,
27 .obj_cmpfn = br_vlan_cmp,
28 .automatic_shrinking = true,
29};
30
31static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
32{
33 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
34}
35
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030036static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020037{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020038 if (vg->pvid == vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030039 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000040
41 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020042 vg->pvid = vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030043
44 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000045}
46
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030047static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000048{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020049 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030050 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000051
52 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020053 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030054
55 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000056}
57
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030058/* return true if anything changed, false otherwise */
59static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000060{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020061 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030062 u16 old_flags = v->flags;
63 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020064
65 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020066 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020067 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020068 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020069
70 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030071 ret = __vlan_add_pvid(vg, v->vid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020072 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030073 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000074
75 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020076 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040077 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020078 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030079
80 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000081}
82
Scott Feldman7f109532015-06-12 17:39:50 -070083static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +000084 struct net_bridge_vlan *v, u16 flags,
85 struct netlink_ext_ack *extack)
Scott Feldman7f109532015-06-12 17:39:50 -070086{
Scott Feldman7f109532015-06-12 17:39:50 -070087 int err;
88
Jiri Pirko0944d6b2015-10-09 13:54:11 +020089 /* Try switchdev op first. In case it is not supported, fallback to
90 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070091 */
Ido Schimmel27973792019-01-08 16:48:11 +000092 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020093 if (err == -EOPNOTSUPP)
Ido Schimmel27973792019-01-08 16:48:11 +000094 return vlan_vid_add(dev, br->vlan_proto, v->vid);
95 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
Scott Feldman7f109532015-06-12 17:39:50 -070096 return err;
97}
98
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020099static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000100{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200101 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200102 struct list_head *headp, *hpos;
103 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000104
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200105 if (br_vlan_is_master(v))
106 vg = br_vlan_group(v->br);
107 else
108 vg = nbp_vlan_group(v->port);
109
110 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200111 list_for_each_prev(hpos, headp) {
112 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
113 if (v->vid < vent->vid)
114 continue;
115 else
116 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000117 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200118 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200119}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000120
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200121static void __vlan_del_list(struct net_bridge_vlan *v)
122{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200123 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000124}
125
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400126static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
Ido Schimmel27973792019-01-08 16:48:11 +0000127 const struct net_bridge_vlan *v)
Scott Feldman7f109532015-06-12 17:39:50 -0700128{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200129 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700130
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200131 /* Try switchdev op first. In case it is not supported, fallback to
132 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700133 */
Ido Schimmel27973792019-01-08 16:48:11 +0000134 err = br_switchdev_port_vlan_del(dev, v->vid);
135 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
136 vlan_vid_del(dev, br->vlan_proto, v->vid);
137 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman7f109532015-06-12 17:39:50 -0700138}
139
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200140/* Returns a master vlan, if it didn't exist it gets created. In all cases a
141 * a reference is taken to the master vlan before returning.
142 */
Petr Machata169327d2018-12-12 17:02:50 +0000143static struct net_bridge_vlan *
144br_vlan_get_master(struct net_bridge *br, u16 vid,
145 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200146{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200147 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200148 struct net_bridge_vlan *masterv;
149
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200150 vg = br_vlan_group(br);
151 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200152 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300153 bool changed;
154
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200155 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000156 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200157 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200158 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200159 if (WARN_ON(!masterv))
160 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200161 refcount_set(&masterv->refcnt, 1);
162 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200163 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300164 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200165
166 return masterv;
167}
168
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200169static void br_master_vlan_rcu_free(struct rcu_head *rcu)
170{
171 struct net_bridge_vlan *v;
172
173 v = container_of(rcu, struct net_bridge_vlan, rcu);
174 WARN_ON(!br_vlan_is_master(v));
175 free_percpu(v->stats);
176 v->stats = NULL;
177 kfree(v);
178}
179
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200180static void br_vlan_put_master(struct net_bridge_vlan *masterv)
181{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200182 struct net_bridge_vlan_group *vg;
183
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200184 if (!br_vlan_is_master(masterv))
185 return;
186
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200187 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300188 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200189 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200190 &masterv->vnode, br_vlan_rht_params);
191 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200192 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200193 }
194}
195
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300196static void nbp_vlan_rcu_free(struct rcu_head *rcu)
197{
198 struct net_bridge_vlan *v;
199
200 v = container_of(rcu, struct net_bridge_vlan, rcu);
201 WARN_ON(br_vlan_is_master(v));
202 /* if we had per-port stats configured then free them here */
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200203 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300204 free_percpu(v->stats);
205 v->stats = NULL;
206 kfree(v);
207}
208
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200209/* This is the shared VLAN add function which works for both ports and bridge
210 * devices. There are four possible calls to this function in terms of the
211 * vlan entry type:
212 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100213 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200214 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100215 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200216 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100217 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200218 * will be used for filtering in both the port and the bridge
219 */
Petr Machata169327d2018-12-12 17:02:50 +0000220static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
221 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000222{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200223 struct net_bridge_vlan *masterv = NULL;
224 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200225 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200226 struct net_device *dev;
227 struct net_bridge *br;
228 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000229
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200230 if (br_vlan_is_master(v)) {
231 br = v->br;
232 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200233 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200234 } else {
235 p = v->port;
236 br = p->br;
237 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200238 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200239 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000240
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200241 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 /* Add VLAN to the device filter if it is supported.
243 * This ensures tagged traffic enters the bridge when
244 * promiscuous mode is disabled by br_manage_promisc().
245 */
Ido Schimmel27973792019-01-08 16:48:11 +0000246 err = __vlan_vid_add(dev, br, v, flags, extack);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400247 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200248 goto out;
249
250 /* need to work on the master vlan too */
251 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300252 bool changed;
253
254 err = br_vlan_add(br, v->vid,
255 flags | BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000256 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200257 if (err)
258 goto out_filt;
259 }
260
Petr Machata169327d2018-12-12 17:02:50 +0000261 masterv = br_vlan_get_master(br, v->vid, extack);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200262 if (!masterv)
263 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200264 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300265 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
266 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
267 if (!v->stats) {
268 err = -ENOMEM;
269 goto out_filt;
270 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200271 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300272 } else {
273 v->stats = masterv->stats;
274 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200275 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000276 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200277 if (err && err != -EOPNOTSUPP)
278 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900279 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000280
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200281 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200282 if (br_vlan_should_use(v)) {
283 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
284 if (err) {
285 br_err(br, "failed insert local address into bridge forwarding table\n");
286 goto out_filt;
287 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200288 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000289 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200290
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200291 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
292 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200293 if (err)
294 goto out_fdb_insert;
295
296 __vlan_add_list(v);
297 __vlan_add_flags(v, flags);
Mike Manning80900ac2019-04-18 18:35:34 +0100298
299 if (p)
300 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200301out:
302 return err;
303
304out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200305 if (br_vlan_should_use(v)) {
306 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
307 vg->num_vlans--;
308 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200309
310out_filt:
311 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000312 __vlan_vid_del(dev, br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200313 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800314 if (v->stats && masterv->stats != v->stats)
315 free_percpu(v->stats);
316 v->stats = NULL;
317
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200318 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200319 v->brvlan = NULL;
320 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200321 } else {
322 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200323 }
324
325 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000326}
327
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200328static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000329{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200330 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200331 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200334
335 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200336 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200337 } else {
338 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200339 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200340 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200341 }
342
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200343 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344 if (p) {
Ido Schimmel27973792019-01-08 16:48:11 +0000345 err = __vlan_vid_del(p->dev, p->br, v);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 if (err)
347 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200348 } else {
349 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
350 if (err && err != -EOPNOTSUPP)
351 goto out;
352 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200353 }
354
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200355 if (br_vlan_should_use(v)) {
356 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
357 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200358 }
359
360 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800361 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200362 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
363 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200364 __vlan_del_list(v);
Mike Manning80900ac2019-04-18 18:35:34 +0100365 nbp_vlan_set_vlan_dev_state(p, v->vid);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300366 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200367 }
368
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200369 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200370out:
371 return err;
372}
373
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200374static void __vlan_group_free(struct net_bridge_vlan_group *vg)
375{
376 WARN_ON(!list_empty(&vg->vlan_list));
377 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800378 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200379 kfree(vg);
380}
381
382static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200383{
384 struct net_bridge_vlan *vlan, *tmp;
385
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200386 __vlan_delete_pvid(vg, vg->pvid);
387 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200388 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000389}
390
Vlad Yasevich78851982013-02-13 12:00:14 +0000391struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800392 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200393 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000394 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000395{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200396 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200397 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000398 u16 vid;
399
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400400 /* If this packet was not filtered at input, let it pass */
401 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000402 goto out;
403
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200404 /* At this point, we know that the frame was filtered and contains
405 * a valid vlan id. If the vlan id has untagged flag set,
406 * send untagged; otherwise, send tagged.
407 */
408 br_vlan_get_tag(skb, &vid);
409 v = br_vlan_find(vg, vid);
410 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400411 * only exception is the bridge is set in promisc mode and the
412 * packet is destined for the bridge device. In this case
413 * pass the packet as is.
414 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200415 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400416 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
417 goto out;
418 } else {
419 kfree_skb(skb);
420 return NULL;
421 }
422 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300423 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200424 stats = this_cpu_ptr(v->stats);
425 u64_stats_update_begin(&stats->syncp);
426 stats->tx_bytes += skb->len;
427 stats->tx_packets++;
428 u64_stats_update_end(&stats->syncp);
429 }
430
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200431 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100432 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800433
434 if (p && (p->flags & BR_VLAN_TUNNEL) &&
435 br_handle_egress_vlan_tunnel(skb, v)) {
436 kfree_skb(skb);
437 return NULL;
438 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000439out:
440 return skb;
441}
442
443/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200444static bool __allowed_ingress(const struct net_bridge *br,
445 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200446 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000447{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200448 struct br_vlan_stats *stats;
449 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900450 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000451
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400452 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900453 /* If vlan tx offload is disabled on bridge device and frame was
454 * sent from vlan device on the bridge device, it does not have
455 * HW accelerated vlan tag.
456 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100457 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200458 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400459 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900460 if (unlikely(!skb))
461 return false;
462 }
463
Toshiaki Makita8580e212014-06-10 20:59:23 +0900464 if (!br_vlan_get_tag(skb, vid)) {
465 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200466 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900467 /* Protocol-mismatch, empty out vlan_tci for new tag */
468 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100469 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100470 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900471 if (unlikely(!skb))
472 return false;
473
474 skb_pull(skb, ETH_HLEN);
475 skb_reset_mac_len(skb);
476 *vid = 0;
477 tagged = false;
478 } else {
479 tagged = true;
480 }
481 } else {
482 /* Untagged frame */
483 tagged = false;
484 }
485
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900486 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200487 u16 pvid = br_get_pvid(vg);
488
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900489 /* Frame had a tag with VID 0 or did not have a tag.
490 * See if pvid is set on this port. That tells us which
491 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000492 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400493 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900494 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000495
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900496 /* PVID is set on this port. Any untagged or priority-tagged
497 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000498 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900499 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900500 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900501 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200502 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900503 else
504 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100505 * At this point, we know that skb->vlan_tci VID
506 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900507 * We update only VID field and preserve PCP field.
508 */
509 skb->vlan_tci |= pvid;
510
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200511 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300512 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200513 return true;
514 }
515 v = br_vlan_find(vg, *vid);
516 if (!v || !br_vlan_should_use(v))
517 goto drop;
518
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300519 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200520 stats = this_cpu_ptr(v->stats);
521 u64_stats_update_begin(&stats->syncp);
522 stats->rx_bytes += skb->len;
523 stats->rx_packets++;
524 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000525 }
526
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200527 return true;
528
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900529drop:
530 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000531 return false;
532}
533
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200534bool br_allowed_ingress(const struct net_bridge *br,
535 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
536 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200537{
538 /* If VLAN filtering is disabled on the bridge, all packets are
539 * permitted.
540 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300541 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200542 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
543 return true;
544 }
545
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200546 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200547}
548
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000549/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200550bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000551 const struct sk_buff *skb)
552{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200553 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000554 u16 vid;
555
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400556 /* If this packet was not filtered at input, let it pass */
557 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000558 return true;
559
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000560 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200561 v = br_vlan_find(vg, vid);
562 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000563 return true;
564
565 return false;
566}
567
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900568/* Called under RCU */
569bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
570{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200571 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900572 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900573
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400574 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300575 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900576 return true;
577
Ido Schimmeleca1e002015-11-01 18:31:45 +0200578 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200579 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900580 return false;
581
Toshiaki Makita8580e212014-06-10 20:59:23 +0900582 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
583 *vid = 0;
584
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900585 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200586 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400587 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900588 return false;
589
590 return true;
591 }
592
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200593 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900594 return true;
595
596 return false;
597}
598
Petr Machatadbd6dc72018-05-30 02:56:55 +0200599static int br_vlan_add_existing(struct net_bridge *br,
600 struct net_bridge_vlan_group *vg,
601 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000602 u16 flags, bool *changed,
603 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200604{
605 int err;
606
Petr Machata169327d2018-12-12 17:02:50 +0000607 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200608 if (err && err != -EOPNOTSUPP)
609 return err;
610
Petr Machatadbd6dc72018-05-30 02:56:55 +0200611 if (!br_vlan_is_brentry(vlan)) {
612 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200613 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
614 err = -EINVAL;
615 goto err_flags;
616 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200617 /* It was only kept for port vlans, now make it real */
618 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
619 vlan->vid);
620 if (err) {
621 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200622 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200623 }
624
625 refcount_inc(&vlan->refcnt);
626 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
627 vg->num_vlans++;
628 *changed = true;
629 }
630
631 if (__vlan_add_flags(vlan, flags))
632 *changed = true;
633
634 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200635
636err_fdb_insert:
637err_flags:
638 br_switchdev_port_vlan_del(br->dev, vlan->vid);
639 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200640}
641
Toshiaki Makita8adff412013-10-16 17:07:13 +0900642/* Must be protected by RTNL.
643 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300644 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900645 */
Petr Machata169327d2018-12-12 17:02:50 +0000646int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
647 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000648{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200649 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200650 struct net_bridge_vlan *vlan;
651 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000652
653 ASSERT_RTNL();
654
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300655 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200656 vg = br_vlan_group(br);
657 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200658 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000659 return br_vlan_add_existing(br, vg, vlan, flags, changed,
660 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000661
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200662 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
663 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000664 return -ENOMEM;
665
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200666 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
667 if (!vlan->stats) {
668 kfree(vlan);
669 return -ENOMEM;
670 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200671 vlan->vid = vid;
672 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
673 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
674 vlan->br = br;
675 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300676 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000677 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200678 if (ret) {
679 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200680 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300681 } else {
682 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200683 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000684
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200685 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000686}
687
Toshiaki Makita8adff412013-10-16 17:07:13 +0900688/* Must be protected by RTNL.
689 * Must be called with vid in range from 1 to 4094 inclusive.
690 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000691int br_vlan_delete(struct net_bridge *br, u16 vid)
692{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200693 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200694 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000695
696 ASSERT_RTNL();
697
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200698 vg = br_vlan_group(br);
699 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200700 if (!v || !br_vlan_is_brentry(v))
701 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000702
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900703 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700704 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000705
Roopa Prabhuefa53562017-01-31 22:59:54 -0800706 vlan_tunnel_info_del(vg, v);
707
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200708 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000709}
710
711void br_vlan_flush(struct net_bridge *br)
712{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200713 struct net_bridge_vlan_group *vg;
714
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000715 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000716
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200717 vg = br_vlan_group(br);
718 __vlan_flush(vg);
719 RCU_INIT_POINTER(br->vlgrp, NULL);
720 synchronize_rcu();
721 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000722}
723
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200724struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900725{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200726 if (!vg)
727 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900728
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200729 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900730}
731
Toshiaki Makita204177f2014-06-10 20:59:25 +0900732/* Must be protected by RTNL. */
733static void recalculate_group_addr(struct net_bridge *br)
734{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300735 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900736 return;
737
738 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300739 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
740 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900741 /* Bridge Group Address */
742 br->group_addr[5] = 0x00;
743 } else { /* vlan_enabled && ETH_P_8021AD */
744 /* Provider Bridge Group Address */
745 br->group_addr[5] = 0x08;
746 }
747 spin_unlock_bh(&br->lock);
748}
749
750/* Must be protected by RTNL. */
751void br_recalculate_fwd_mask(struct net_bridge *br)
752{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300753 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
754 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900755 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
756 else /* vlan_enabled && ETH_P_8021AD */
757 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
758 ~(1u << br->group_addr[5]);
759}
760
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300761int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000762{
Elad Raz6b72a772016-01-06 13:01:06 +0100763 struct switchdev_attr attr = {
764 .orig_dev = br->dev,
765 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
766 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
767 .u.vlan_filtering = val,
768 };
769 int err;
770
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300771 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300772 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000773
Elad Raz6b72a772016-01-06 13:01:06 +0100774 err = switchdev_port_attr_set(br->dev, &attr);
775 if (err && err != -EOPNOTSUPP)
776 return err;
777
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300778 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400779 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900780 recalculate_group_addr(br);
781 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000782
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300783 return 0;
784}
785
786int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
787{
Xin Long047831a2016-04-09 00:03:31 +0800788 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000789}
790
Ido Schimmel1f514452017-05-26 08:37:23 +0200791bool br_vlan_enabled(const struct net_device *dev)
792{
793 struct net_bridge *br = netdev_priv(dev);
794
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300795 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200796}
797EXPORT_SYMBOL_GPL(br_vlan_enabled);
798
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900799int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900800{
801 int err = 0;
802 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200803 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200804 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900805 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900806
Toshiaki Makita204177f2014-06-10 20:59:25 +0900807 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900808 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900809
810 /* Add VLANs for the new proto to the device filter. */
811 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200812 vg = nbp_vlan_group(p);
813 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200814 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900815 if (err)
816 goto err_filt;
817 }
818 }
819
820 oldproto = br->vlan_proto;
821 br->vlan_proto = proto;
822
823 recalculate_group_addr(br);
824 br_recalculate_fwd_mask(br);
825
826 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200827 list_for_each_entry(p, &br->port_list, list) {
828 vg = nbp_vlan_group(p);
829 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200830 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200831 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900832
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900833 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900834
835err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200836 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200837 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900838
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200839 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
840 vg = nbp_vlan_group(p);
841 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200842 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200843 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900844
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900845 return err;
846}
847
848int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
849{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900850 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
851 return -EPROTONOSUPPORT;
852
Xin Long047831a2016-04-09 00:03:31 +0800853 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900854}
855
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200856int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
857{
858 switch (val) {
859 case 0:
860 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300861 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200862 break;
863 default:
864 return -EINVAL;
865 }
866
867 return 0;
868}
869
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300870int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
871{
872 struct net_bridge_port *p;
873
874 /* allow to change the option if there are no port vlans configured */
875 list_for_each_entry(p, &br->port_list, list) {
876 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
877
878 if (vg->num_vlans)
879 return -EBUSY;
880 }
881
882 switch (val) {
883 case 0:
884 case 1:
885 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
886 break;
887 default:
888 return -EINVAL;
889 }
890
891 return 0;
892}
893
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200894static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400895{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200896 struct net_bridge_vlan *v;
897
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200898 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200899 return false;
900
901 v = br_vlan_lookup(&vg->vlan_hash, vid);
902 if (v && br_vlan_should_use(v) &&
903 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
904 return true;
905
906 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400907}
908
909static void br_vlan_disable_default_pvid(struct net_bridge *br)
910{
911 struct net_bridge_port *p;
912 u16 pvid = br->default_pvid;
913
914 /* Disable default_pvid on all ports where it is still
915 * configured.
916 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200917 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400918 br_vlan_delete(br, pvid);
919
920 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200921 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400922 nbp_vlan_delete(p, pvid);
923 }
924
925 br->default_pvid = 0;
926}
927
Petr Machata169327d2018-12-12 17:02:50 +0000928int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
929 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400930{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200931 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200932 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400933 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300934 unsigned long *changed;
935 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400936 u16 old_pvid;
937 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400938
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200939 if (!pvid) {
940 br_vlan_disable_default_pvid(br);
941 return 0;
942 }
943
Andy Shevchenko459479d2018-08-30 13:33:18 +0300944 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400945 if (!changed)
946 return -ENOMEM;
947
948 old_pvid = br->default_pvid;
949
950 /* Update default_pvid config only if we do not conflict with
951 * user configuration.
952 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200953 vg = br_vlan_group(br);
954 pvent = br_vlan_find(vg, pvid);
955 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200956 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400957 err = br_vlan_add(br, pvid,
958 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200959 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300960 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000961 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400962 if (err)
963 goto out;
964 br_vlan_delete(br, old_pvid);
965 set_bit(0, changed);
966 }
967
968 list_for_each_entry(p, &br->port_list, list) {
969 /* Update default_pvid config only if we do not conflict with
970 * user configuration.
971 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200972 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400973 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200974 !vlan_default_pvid(vg, old_pvid)) ||
975 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400976 continue;
977
978 err = nbp_vlan_add(p, pvid,
979 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300980 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000981 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400982 if (err)
983 goto err_port;
984 nbp_vlan_delete(p, old_pvid);
985 set_bit(p->port_no, changed);
986 }
987
988 br->default_pvid = pvid;
989
990out:
Andy Shevchenko459479d2018-08-30 13:33:18 +0300991 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400992 return err;
993
994err_port:
995 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
996 if (!test_bit(p->port_no, changed))
997 continue;
998
999 if (old_pvid)
1000 nbp_vlan_add(p, old_pvid,
1001 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001002 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001003 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001004 nbp_vlan_delete(p, pvid);
1005 }
1006
1007 if (test_bit(0, changed)) {
1008 if (old_pvid)
1009 br_vlan_add(br, old_pvid,
1010 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001011 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001012 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001013 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001014 br_vlan_delete(br, pvid);
1015 }
1016 goto out;
1017}
1018
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001019int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1020{
1021 u16 pvid = val;
1022 int err = 0;
1023
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001024 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001025 return -EINVAL;
1026
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001027 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001028 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001029
1030 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001031 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001032 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1033 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001034 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001035 }
Petr Machata169327d2018-12-12 17:02:50 +00001036 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001037out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001038 return err;
1039}
1040
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001041int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001042{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001043 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001044 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001045 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001046
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001047 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1048 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001049 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001050 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001051 if (ret)
1052 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001053 ret = vlan_tunnel_init(vg);
1054 if (ret)
1055 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001056 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001057 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001058 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001059 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001060 ret = br_vlan_add(br, 1,
1061 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Petr Machata169327d2018-12-12 17:02:50 +00001062 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001063 if (ret)
1064 goto err_vlan_add;
1065
1066out:
1067 return ret;
1068
1069err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -08001070 vlan_tunnel_deinit(vg);
1071err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001072 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001073err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001074 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001075
1076 goto out;
1077}
1078
Petr Machata169327d2018-12-12 17:02:50 +00001079int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001080{
Elad Raz404cdbf2016-01-06 13:01:07 +01001081 struct switchdev_attr attr = {
1082 .orig_dev = p->br->dev,
1083 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1084 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001085 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001086 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001087 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001088 int ret = -ENOMEM;
1089
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001090 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1091 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001092 goto out;
1093
Elad Raz404cdbf2016-01-06 13:01:07 +01001094 ret = switchdev_port_attr_set(p->dev, &attr);
1095 if (ret && ret != -EOPNOTSUPP)
1096 goto err_vlan_enabled;
1097
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001098 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001099 if (ret)
1100 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001101 ret = vlan_tunnel_init(vg);
1102 if (ret)
1103 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001104 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001105 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001106 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001107 bool changed;
1108
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001109 ret = nbp_vlan_add(p, p->br->default_pvid,
1110 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001111 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001112 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001113 if (ret)
1114 goto err_vlan_add;
1115 }
1116out:
1117 return ret;
1118
1119err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001120 RCU_INIT_POINTER(p->vlgrp, NULL);
1121 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001122 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001123err_tunnel_init:
1124 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001125err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001126err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001127 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001128
1129 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001130}
1131
Toshiaki Makita8adff412013-10-16 17:07:13 +09001132/* Must be protected by RTNL.
1133 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001134 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001135 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001136int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001137 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001138{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001139 struct net_bridge_vlan *vlan;
1140 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001141
1142 ASSERT_RTNL();
1143
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001144 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001145 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001146 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001147 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001148 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001149 if (ret && ret != -EOPNOTSUPP)
1150 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001151 *changed = __vlan_add_flags(vlan, flags);
1152
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001153 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001154 }
1155
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001156 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1157 if (!vlan)
1158 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001159
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001160 vlan->vid = vid;
1161 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001162 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001163 if (ret)
1164 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001165 else
1166 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001167
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001168 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001169}
1170
Toshiaki Makita8adff412013-10-16 17:07:13 +09001171/* Must be protected by RTNL.
1172 * Must be called with vid in range from 1 to 4094 inclusive.
1173 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001174int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1175{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001176 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001177
1178 ASSERT_RTNL();
1179
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001180 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001181 if (!v)
1182 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001183 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001184 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001185
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001186 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001187}
1188
1189void nbp_vlan_flush(struct net_bridge_port *port)
1190{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001191 struct net_bridge_vlan_group *vg;
1192
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001193 ASSERT_RTNL();
1194
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001195 vg = nbp_vlan_group(port);
1196 __vlan_flush(vg);
1197 RCU_INIT_POINTER(port->vlgrp, NULL);
1198 synchronize_rcu();
1199 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001200}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001201
1202void br_vlan_get_stats(const struct net_bridge_vlan *v,
1203 struct br_vlan_stats *stats)
1204{
1205 int i;
1206
1207 memset(stats, 0, sizeof(*stats));
1208 for_each_possible_cpu(i) {
1209 u64 rxpackets, rxbytes, txpackets, txbytes;
1210 struct br_vlan_stats *cpu_stats;
1211 unsigned int start;
1212
1213 cpu_stats = per_cpu_ptr(v->stats, i);
1214 do {
1215 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1216 rxpackets = cpu_stats->rx_packets;
1217 rxbytes = cpu_stats->rx_bytes;
1218 txbytes = cpu_stats->tx_bytes;
1219 txpackets = cpu_stats->tx_packets;
1220 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1221
1222 stats->rx_packets += rxpackets;
1223 stats->rx_bytes += rxbytes;
1224 stats->tx_bytes += txbytes;
1225 stats->tx_packets += txpackets;
1226 }
1227}
Petr Machata4d4fd362018-04-29 10:56:08 +03001228
1229int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1230{
1231 struct net_bridge_vlan_group *vg;
Ido Schimmel5a6db042018-11-28 20:06:58 +00001232 struct net_bridge_port *p;
Petr Machata4d4fd362018-04-29 10:56:08 +03001233
1234 ASSERT_RTNL();
Ido Schimmel5a6db042018-11-28 20:06:58 +00001235 p = br_port_get_check_rtnl(dev);
1236 if (p)
1237 vg = nbp_vlan_group(p);
1238 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001239 vg = br_vlan_group(netdev_priv(dev));
1240 else
1241 return -EINVAL;
1242
1243 *p_pvid = br_get_pvid(vg);
1244 return 0;
1245}
1246EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1247
1248int br_vlan_get_info(const struct net_device *dev, u16 vid,
1249 struct bridge_vlan_info *p_vinfo)
1250{
1251 struct net_bridge_vlan_group *vg;
1252 struct net_bridge_vlan *v;
1253 struct net_bridge_port *p;
1254
1255 ASSERT_RTNL();
1256 p = br_port_get_check_rtnl(dev);
1257 if (p)
1258 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001259 else if (netif_is_bridge_master(dev))
1260 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001261 else
1262 return -EINVAL;
1263
1264 v = br_vlan_find(vg, vid);
1265 if (!v)
1266 return -ENOENT;
1267
1268 p_vinfo->vid = vid;
1269 p_vinfo->flags = v->flags;
1270 return 0;
1271}
1272EXPORT_SYMBOL_GPL(br_vlan_get_info);
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001273
1274static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1275{
1276 return is_vlan_dev(dev) &&
1277 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1278}
1279
1280static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1281 __always_unused void *data)
1282{
1283 return br_vlan_is_bind_vlan_dev(dev);
1284}
1285
1286static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1287{
1288 int found;
1289
1290 rcu_read_lock();
1291 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1292 NULL);
1293 rcu_read_unlock();
1294
1295 return !!found;
1296}
1297
1298struct br_vlan_bind_walk_data {
1299 u16 vid;
1300 struct net_device *result;
1301};
1302
1303static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1304 void *data_in)
1305{
1306 struct br_vlan_bind_walk_data *data = data_in;
1307 int found = 0;
1308
1309 if (br_vlan_is_bind_vlan_dev(dev) &&
1310 vlan_dev_priv(dev)->vlan_id == data->vid) {
1311 data->result = dev;
1312 found = 1;
1313 }
1314
1315 return found;
1316}
1317
1318static struct net_device *
1319br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1320{
1321 struct br_vlan_bind_walk_data data = {
1322 .vid = vid,
1323 };
1324
1325 rcu_read_lock();
1326 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1327 &data);
1328 rcu_read_unlock();
1329
1330 return data.result;
1331}
1332
1333static bool br_vlan_is_dev_up(const struct net_device *dev)
1334{
1335 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1336}
1337
1338static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1339 struct net_device *vlan_dev)
1340{
1341 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1342 struct net_bridge_vlan_group *vg;
1343 struct net_bridge_port *p;
1344 bool has_carrier = false;
1345
Mike Manning8e1acd42019-04-18 18:35:35 +01001346 if (!netif_carrier_ok(br->dev)) {
1347 netif_carrier_off(vlan_dev);
1348 return;
1349 }
1350
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001351 list_for_each_entry(p, &br->port_list, list) {
1352 vg = nbp_vlan_group(p);
1353 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1354 has_carrier = true;
1355 break;
1356 }
1357 }
1358
1359 if (has_carrier)
1360 netif_carrier_on(vlan_dev);
1361 else
1362 netif_carrier_off(vlan_dev);
1363}
1364
1365static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1366{
1367 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1368 struct net_bridge_vlan *vlan;
1369 struct net_device *vlan_dev;
1370
1371 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1372 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1373 vlan->vid);
1374 if (vlan_dev) {
Mike Manning8e1acd42019-04-18 18:35:35 +01001375 if (br_vlan_is_dev_up(p->dev)) {
1376 if (netif_carrier_ok(p->br->dev))
1377 netif_carrier_on(vlan_dev);
1378 } else {
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001379 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
Mike Manning8e1acd42019-04-18 18:35:35 +01001380 }
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001381 }
1382 }
1383}
1384
1385static void br_vlan_upper_change(struct net_device *dev,
1386 struct net_device *upper_dev,
1387 bool linking)
1388{
1389 struct net_bridge *br = netdev_priv(dev);
1390
1391 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1392 return;
1393
1394 if (linking) {
1395 br_vlan_set_vlan_dev_state(br, upper_dev);
1396 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1397 } else {
1398 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1399 br_vlan_has_upper_bind_vlan_dev(dev));
1400 }
1401}
1402
Mike Manning8e1acd42019-04-18 18:35:35 +01001403struct br_vlan_link_state_walk_data {
1404 struct net_bridge *br;
1405};
1406
1407static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1408 void *data_in)
1409{
1410 struct br_vlan_link_state_walk_data *data = data_in;
1411
1412 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1413 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1414
1415 return 0;
1416}
1417
1418static void br_vlan_link_state_change(struct net_device *dev,
1419 struct net_bridge *br)
1420{
1421 struct br_vlan_link_state_walk_data data = {
1422 .br = br
1423 };
1424
1425 rcu_read_lock();
1426 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1427 &data);
1428 rcu_read_unlock();
1429}
1430
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001431/* Must be protected by RTNL. */
Mike Manning80900ac2019-04-18 18:35:34 +01001432static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1433{
1434 struct net_device *vlan_dev;
1435
1436 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1437 return;
1438
1439 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1440 if (vlan_dev)
1441 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1442}
1443
1444/* Must be protected by RTNL. */
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001445void br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1446 void *ptr)
1447{
1448 struct netdev_notifier_changeupper_info *info;
Mike Manning8e1acd42019-04-18 18:35:35 +01001449 struct net_bridge *br;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001450
1451 switch (event) {
1452 case NETDEV_CHANGEUPPER:
1453 info = ptr;
1454 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1455 break;
Mike Manning8e1acd42019-04-18 18:35:35 +01001456
1457 case NETDEV_CHANGE:
1458 case NETDEV_UP:
1459 br = netdev_priv(dev);
1460 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1461 return;
1462 br_vlan_link_state_change(dev, br);
1463 break;
Mike Manning9c0ec2e2019-04-18 18:35:33 +01001464 }
1465}
1466
1467/* Must be protected by RTNL. */
1468void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1469{
1470 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1471 return;
1472
1473 switch (event) {
1474 case NETDEV_CHANGE:
1475 case NETDEV_DOWN:
1476 case NETDEV_UP:
1477 br_vlan_set_all_vlan_dev_state(p);
1478 break;
1479 }
1480}