blob: 4a2f31157ef5efaa4f380be7d6bd50d805d1d27b [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,
24 .locks_mul = 1,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020025 .max_size = VLAN_N_VID,
26 .obj_cmpfn = br_vlan_cmp,
27 .automatic_shrinking = true,
28};
29
30static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
31{
32 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
33}
34
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030035static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020036{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020037 if (vg->pvid == vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030038 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000039
40 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020041 vg->pvid = vid;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030042
43 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000044}
45
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030046static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000047{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020048 if (vg->pvid != vid)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030049 return false;
Vlad Yasevich552406c2013-02-13 12:00:15 +000050
51 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020052 vg->pvid = 0;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030053
54 return true;
Vlad Yasevich552406c2013-02-13 12:00:15 +000055}
56
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030057/* return true if anything changed, false otherwise */
58static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000059{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020060 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030061 u16 old_flags = v->flags;
62 bool ret;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020063
64 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020065 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020066 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020067 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020068
69 if (flags & BRIDGE_VLAN_INFO_PVID)
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030070 ret = __vlan_add_pvid(vg, v->vid);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020071 else
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030072 ret = __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000073
74 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020075 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040076 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020077 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +030078
79 return ret || !!(old_flags ^ v->flags);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000080}
81
Scott Feldman7f109532015-06-12 17:39:50 -070082static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
Petr Machata169327d2018-12-12 17:02:50 +000083 u16 vid, u16 flags, 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 */
Petr Machata169327d2018-12-12 17:02:50 +000090 err = br_switchdev_port_vlan_add(dev, vid, flags, extack);
Jiri Pirko0944d6b2015-10-09 13:54:11 +020091 if (err == -EOPNOTSUPP)
92 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070093 return err;
94}
95
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020096static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000097{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020098 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020099 struct list_head *headp, *hpos;
100 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000101
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200102 if (br_vlan_is_master(v))
103 vg = br_vlan_group(v->br);
104 else
105 vg = nbp_vlan_group(v->port);
106
107 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200108 list_for_each_prev(hpos, headp) {
109 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
110 if (v->vid < vent->vid)
111 continue;
112 else
113 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000114 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200115 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200116}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000117
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200118static void __vlan_del_list(struct net_bridge_vlan *v)
119{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200120 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000121}
122
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400123static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
124 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700125{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200126 int err;
Scott Feldman7f109532015-06-12 17:39:50 -0700127
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200128 /* Try switchdev op first. In case it is not supported, fallback to
129 * 8021q del.
Scott Feldman7f109532015-06-12 17:39:50 -0700130 */
Petr Machatad66e4342018-05-30 02:56:03 +0200131 err = br_switchdev_port_vlan_del(dev, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200132 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700133 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200134 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700135 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400136 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700137}
138
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200139/* Returns a master vlan, if it didn't exist it gets created. In all cases a
140 * a reference is taken to the master vlan before returning.
141 */
Petr Machata169327d2018-12-12 17:02:50 +0000142static struct net_bridge_vlan *
143br_vlan_get_master(struct net_bridge *br, u16 vid,
144 struct netlink_ext_ack *extack)
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200145{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200146 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200147 struct net_bridge_vlan *masterv;
148
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200149 vg = br_vlan_group(br);
150 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200151 if (!masterv) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300152 bool changed;
153
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200154 /* missing global ctx, create it now */
Petr Machata169327d2018-12-12 17:02:50 +0000155 if (br_vlan_add(br, vid, 0, &changed, extack))
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200156 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200157 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200158 if (WARN_ON(!masterv))
159 return NULL;
Ido Schimmel0e5a82e2018-02-25 21:59:06 +0200160 refcount_set(&masterv->refcnt, 1);
161 return masterv;
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200162 }
Reshetova, Elena25127752017-07-04 15:53:05 +0300163 refcount_inc(&masterv->refcnt);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200164
165 return masterv;
166}
167
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200168static void br_master_vlan_rcu_free(struct rcu_head *rcu)
169{
170 struct net_bridge_vlan *v;
171
172 v = container_of(rcu, struct net_bridge_vlan, rcu);
173 WARN_ON(!br_vlan_is_master(v));
174 free_percpu(v->stats);
175 v->stats = NULL;
176 kfree(v);
177}
178
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200179static void br_vlan_put_master(struct net_bridge_vlan *masterv)
180{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200181 struct net_bridge_vlan_group *vg;
182
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200183 if (!br_vlan_is_master(masterv))
184 return;
185
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200186 vg = br_vlan_group(masterv->br);
Reshetova, Elena25127752017-07-04 15:53:05 +0300187 if (refcount_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200188 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200189 &masterv->vnode, br_vlan_rht_params);
190 __vlan_del_list(masterv);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200191 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200192 }
193}
194
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300195static void nbp_vlan_rcu_free(struct rcu_head *rcu)
196{
197 struct net_bridge_vlan *v;
198
199 v = container_of(rcu, struct net_bridge_vlan, rcu);
200 WARN_ON(br_vlan_is_master(v));
201 /* if we had per-port stats configured then free them here */
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200202 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300203 free_percpu(v->stats);
204 v->stats = NULL;
205 kfree(v);
206}
207
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200208/* This is the shared VLAN add function which works for both ports and bridge
209 * devices. There are four possible calls to this function in terms of the
210 * vlan entry type:
211 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100212 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200213 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100214 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100216 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 * will be used for filtering in both the port and the bridge
218 */
Petr Machata169327d2018-12-12 17:02:50 +0000219static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
220 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000221{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200222 struct net_bridge_vlan *masterv = NULL;
223 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200224 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200225 struct net_device *dev;
226 struct net_bridge *br;
227 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000228
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200229 if (br_vlan_is_master(v)) {
230 br = v->br;
231 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200232 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200233 } else {
234 p = v->port;
235 br = p->br;
236 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200237 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200238 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000239
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200240 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200241 /* Add VLAN to the device filter if it is supported.
242 * This ensures tagged traffic enters the bridge when
243 * promiscuous mode is disabled by br_manage_promisc().
244 */
Petr Machata169327d2018-12-12 17:02:50 +0000245 err = __vlan_vid_add(dev, br, v->vid, flags, extack);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400246 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200247 goto out;
248
249 /* need to work on the master vlan too */
250 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300251 bool changed;
252
253 err = br_vlan_add(br, v->vid,
254 flags | BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000255 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200256 if (err)
257 goto out_filt;
258 }
259
Petr Machata169327d2018-12-12 17:02:50 +0000260 masterv = br_vlan_get_master(br, v->vid, extack);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200261 if (!masterv)
262 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200263 v->brvlan = masterv;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300264 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
265 v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
266 if (!v->stats) {
267 err = -ENOMEM;
268 goto out_filt;
269 }
Nikolay Aleksandrov9d332e69c2018-11-16 18:50:01 +0200270 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300271 } else {
272 v->stats = masterv->stats;
273 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200274 } else {
Petr Machata169327d2018-12-12 17:02:50 +0000275 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200276 if (err && err != -EOPNOTSUPP)
277 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900278 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000279
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200280 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200281 if (br_vlan_should_use(v)) {
282 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
283 if (err) {
284 br_err(br, "failed insert local address into bridge forwarding table\n");
285 goto out_filt;
286 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200287 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000288 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200289
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200290 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
291 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200292 if (err)
293 goto out_fdb_insert;
294
295 __vlan_add_list(v);
296 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200297out:
298 return err;
299
300out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200301 if (br_vlan_should_use(v)) {
302 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
303 vg->num_vlans--;
304 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305
306out_filt:
307 if (p) {
308 __vlan_vid_del(dev, br, v->vid);
309 if (masterv) {
Li RongQing1a3aea22018-10-15 19:00:31 +0800310 if (v->stats && masterv->stats != v->stats)
311 free_percpu(v->stats);
312 v->stats = NULL;
313
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200314 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200315 v->brvlan = NULL;
316 }
Petr Machata9c86ce22018-05-30 03:00:45 +0200317 } else {
318 br_switchdev_port_vlan_del(dev, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200319 }
320
321 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000322}
323
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000325{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200327 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200328 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200330
331 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200332 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200333 } else {
334 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200335 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200336 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200337 }
338
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200339 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200340 if (p) {
341 err = __vlan_vid_del(p->dev, p->br, v->vid);
342 if (err)
343 goto out;
Petr Machata9c86ce22018-05-30 03:00:45 +0200344 } else {
345 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
346 if (err && err != -EOPNOTSUPP)
347 goto out;
348 err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349 }
350
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200351 if (br_vlan_should_use(v)) {
352 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
353 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 }
355
356 if (masterv != v) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800357 vlan_tunnel_info_del(vg, v);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200358 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
359 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200360 __vlan_del_list(v);
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300361 call_rcu(&v->rcu, nbp_vlan_rcu_free);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200362 }
363
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200364 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200365out:
366 return err;
367}
368
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200369static void __vlan_group_free(struct net_bridge_vlan_group *vg)
370{
371 WARN_ON(!list_empty(&vg->vlan_list));
372 rhashtable_destroy(&vg->vlan_hash);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800373 vlan_tunnel_deinit(vg);
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200374 kfree(vg);
375}
376
377static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200378{
379 struct net_bridge_vlan *vlan, *tmp;
380
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200381 __vlan_delete_pvid(vg, vg->pvid);
382 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200383 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000384}
385
Vlad Yasevich78851982013-02-13 12:00:14 +0000386struct sk_buff *br_handle_vlan(struct net_bridge *br,
Roopa Prabhu11538d02017-01-31 22:59:55 -0800387 const struct net_bridge_port *p,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200388 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000389 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000390{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200391 struct br_vlan_stats *stats;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200392 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000393 u16 vid;
394
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400395 /* If this packet was not filtered at input, let it pass */
396 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000397 goto out;
398
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200399 /* At this point, we know that the frame was filtered and contains
400 * a valid vlan id. If the vlan id has untagged flag set,
401 * send untagged; otherwise, send tagged.
402 */
403 br_vlan_get_tag(skb, &vid);
404 v = br_vlan_find(vg, vid);
405 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400406 * only exception is the bridge is set in promisc mode and the
407 * packet is destined for the bridge device. In this case
408 * pass the packet as is.
409 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200410 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400411 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
412 goto out;
413 } else {
414 kfree_skb(skb);
415 return NULL;
416 }
417 }
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300418 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200419 stats = this_cpu_ptr(v->stats);
420 u64_stats_update_begin(&stats->syncp);
421 stats->tx_bytes += skb->len;
422 stats->tx_packets++;
423 u64_stats_update_end(&stats->syncp);
424 }
425
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200426 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100427 __vlan_hwaccel_clear_tag(skb);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800428
429 if (p && (p->flags & BR_VLAN_TUNNEL) &&
430 br_handle_egress_vlan_tunnel(skb, v)) {
431 kfree_skb(skb);
432 return NULL;
433 }
Vlad Yasevich78851982013-02-13 12:00:14 +0000434out:
435 return skb;
436}
437
438/* Called under RCU */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200439static bool __allowed_ingress(const struct net_bridge *br,
440 struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200441 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000442{
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200443 struct br_vlan_stats *stats;
444 struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900445 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000446
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400447 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900448 /* If vlan tx offload is disabled on bridge device and frame was
449 * sent from vlan device on the bridge device, it does not have
450 * HW accelerated vlan tag.
451 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100452 if (unlikely(!skb_vlan_tag_present(skb) &&
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200453 skb->protocol == br->vlan_proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400454 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900455 if (unlikely(!skb))
456 return false;
457 }
458
Toshiaki Makita8580e212014-06-10 20:59:23 +0900459 if (!br_vlan_get_tag(skb, vid)) {
460 /* Tagged frame */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200461 if (skb->vlan_proto != br->vlan_proto) {
Toshiaki Makita8580e212014-06-10 20:59:23 +0900462 /* Protocol-mismatch, empty out vlan_tci for new tag */
463 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100464 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100465 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900466 if (unlikely(!skb))
467 return false;
468
469 skb_pull(skb, ETH_HLEN);
470 skb_reset_mac_len(skb);
471 *vid = 0;
472 tagged = false;
473 } else {
474 tagged = true;
475 }
476 } else {
477 /* Untagged frame */
478 tagged = false;
479 }
480
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900481 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200482 u16 pvid = br_get_pvid(vg);
483
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900484 /* Frame had a tag with VID 0 or did not have a tag.
485 * See if pvid is set on this port. That tells us which
486 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000487 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400488 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900489 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000490
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900491 /* PVID is set on this port. Any untagged or priority-tagged
492 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000493 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900494 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900495 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900496 /* Untagged Frame. */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200497 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900498 else
499 /* Priority-tagged Frame.
Michał Mirosław5978f8a2018-11-09 00:18:03 +0100500 * At this point, we know that skb->vlan_tci VID
501 * field was 0.
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900502 * We update only VID field and preserve PCP field.
503 */
504 skb->vlan_tci |= pvid;
505
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200506 /* if stats are disabled we can avoid the lookup */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300507 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200508 return true;
509 }
510 v = br_vlan_find(vg, *vid);
511 if (!v || !br_vlan_should_use(v))
512 goto drop;
513
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300514 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200515 stats = this_cpu_ptr(v->stats);
516 u64_stats_update_begin(&stats->syncp);
517 stats->rx_bytes += skb->len;
518 stats->rx_packets++;
519 u64_stats_update_end(&stats->syncp);
Vlad Yasevich78851982013-02-13 12:00:14 +0000520 }
521
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200522 return true;
523
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900524drop:
525 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000526 return false;
527}
528
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200529bool br_allowed_ingress(const struct net_bridge *br,
530 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
531 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200532{
533 /* If VLAN filtering is disabled on the bridge, all packets are
534 * permitted.
535 */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300536 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200537 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
538 return true;
539 }
540
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200541 return __allowed_ingress(br, vg, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200542}
543
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000544/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200545bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000546 const struct sk_buff *skb)
547{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200548 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000549 u16 vid;
550
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400551 /* If this packet was not filtered at input, let it pass */
552 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000553 return true;
554
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000555 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200556 v = br_vlan_find(vg, vid);
557 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000558 return true;
559
560 return false;
561}
562
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900563/* Called under RCU */
564bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
565{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200566 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900567 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900568
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400569 /* If filtering was disabled at input, let it pass. */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300570 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900571 return true;
572
Ido Schimmeleca1e002015-11-01 18:31:45 +0200573 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200574 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900575 return false;
576
Toshiaki Makita8580e212014-06-10 20:59:23 +0900577 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
578 *vid = 0;
579
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900580 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200581 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400582 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900583 return false;
584
585 return true;
586 }
587
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200588 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900589 return true;
590
591 return false;
592}
593
Petr Machatadbd6dc72018-05-30 02:56:55 +0200594static int br_vlan_add_existing(struct net_bridge *br,
595 struct net_bridge_vlan_group *vg,
596 struct net_bridge_vlan *vlan,
Petr Machata169327d2018-12-12 17:02:50 +0000597 u16 flags, bool *changed,
598 struct netlink_ext_ack *extack)
Petr Machatadbd6dc72018-05-30 02:56:55 +0200599{
600 int err;
601
Petr Machata169327d2018-12-12 17:02:50 +0000602 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
Petr Machata9c86ce22018-05-30 03:00:45 +0200603 if (err && err != -EOPNOTSUPP)
604 return err;
605
Petr Machatadbd6dc72018-05-30 02:56:55 +0200606 if (!br_vlan_is_brentry(vlan)) {
607 /* Trying to change flags of non-existent bridge vlan */
Petr Machata9c86ce22018-05-30 03:00:45 +0200608 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
609 err = -EINVAL;
610 goto err_flags;
611 }
Petr Machatadbd6dc72018-05-30 02:56:55 +0200612 /* It was only kept for port vlans, now make it real */
613 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
614 vlan->vid);
615 if (err) {
616 br_err(br, "failed to insert local address into bridge forwarding table\n");
Petr Machata9c86ce22018-05-30 03:00:45 +0200617 goto err_fdb_insert;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200618 }
619
620 refcount_inc(&vlan->refcnt);
621 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
622 vg->num_vlans++;
623 *changed = true;
624 }
625
626 if (__vlan_add_flags(vlan, flags))
627 *changed = true;
628
629 return 0;
Petr Machata9c86ce22018-05-30 03:00:45 +0200630
631err_fdb_insert:
632err_flags:
633 br_switchdev_port_vlan_del(br->dev, vlan->vid);
634 return err;
Petr Machatadbd6dc72018-05-30 02:56:55 +0200635}
636
Toshiaki Makita8adff412013-10-16 17:07:13 +0900637/* Must be protected by RTNL.
638 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300639 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +0900640 */
Petr Machata169327d2018-12-12 17:02:50 +0000641int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
642 struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000643{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200644 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200645 struct net_bridge_vlan *vlan;
646 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000647
648 ASSERT_RTNL();
649
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300650 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200651 vg = br_vlan_group(br);
652 vlan = br_vlan_find(vg, vid);
Petr Machatadbd6dc72018-05-30 02:56:55 +0200653 if (vlan)
Petr Machata169327d2018-12-12 17:02:50 +0000654 return br_vlan_add_existing(br, vg, vlan, flags, changed,
655 extack);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000656
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200657 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
658 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000659 return -ENOMEM;
660
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200661 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
662 if (!vlan->stats) {
663 kfree(vlan);
664 return -ENOMEM;
665 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200666 vlan->vid = vid;
667 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
668 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
669 vlan->br = br;
670 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
Reshetova, Elena25127752017-07-04 15:53:05 +0300671 refcount_set(&vlan->refcnt, 1);
Petr Machata169327d2018-12-12 17:02:50 +0000672 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200673 if (ret) {
674 free_percpu(vlan->stats);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200675 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300676 } else {
677 *changed = true;
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200678 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000679
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200680 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000681}
682
Toshiaki Makita8adff412013-10-16 17:07:13 +0900683/* Must be protected by RTNL.
684 * Must be called with vid in range from 1 to 4094 inclusive.
685 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000686int br_vlan_delete(struct net_bridge *br, u16 vid)
687{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200688 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200689 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000690
691 ASSERT_RTNL();
692
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200693 vg = br_vlan_group(br);
694 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200695 if (!v || !br_vlan_is_brentry(v))
696 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000697
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900698 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700699 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000700
Roopa Prabhuefa53562017-01-31 22:59:54 -0800701 vlan_tunnel_info_del(vg, v);
702
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200703 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000704}
705
706void br_vlan_flush(struct net_bridge *br)
707{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200708 struct net_bridge_vlan_group *vg;
709
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000710 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000711
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200712 vg = br_vlan_group(br);
713 __vlan_flush(vg);
714 RCU_INIT_POINTER(br->vlgrp, NULL);
715 synchronize_rcu();
716 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000717}
718
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200719struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900720{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200721 if (!vg)
722 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900723
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200724 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900725}
726
Toshiaki Makita204177f2014-06-10 20:59:25 +0900727/* Must be protected by RTNL. */
728static void recalculate_group_addr(struct net_bridge *br)
729{
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +0300730 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900731 return;
732
733 spin_lock_bh(&br->lock);
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300734 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
735 br->vlan_proto == htons(ETH_P_8021Q)) {
Toshiaki Makita204177f2014-06-10 20:59:25 +0900736 /* Bridge Group Address */
737 br->group_addr[5] = 0x00;
738 } else { /* vlan_enabled && ETH_P_8021AD */
739 /* Provider Bridge Group Address */
740 br->group_addr[5] = 0x08;
741 }
742 spin_unlock_bh(&br->lock);
743}
744
745/* Must be protected by RTNL. */
746void br_recalculate_fwd_mask(struct net_bridge *br)
747{
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300748 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
749 br->vlan_proto == htons(ETH_P_8021Q))
Toshiaki Makita204177f2014-06-10 20:59:25 +0900750 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
751 else /* vlan_enabled && ETH_P_8021AD */
752 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
753 ~(1u << br->group_addr[5]);
754}
755
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300756int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000757{
Elad Raz6b72a772016-01-06 13:01:06 +0100758 struct switchdev_attr attr = {
759 .orig_dev = br->dev,
760 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
761 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
762 .u.vlan_filtering = val,
763 };
764 int err;
765
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300766 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300767 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000768
Elad Raz6b72a772016-01-06 13:01:06 +0100769 err = switchdev_port_attr_set(br->dev, &attr);
770 if (err && err != -EOPNOTSUPP)
771 return err;
772
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300773 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400774 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900775 recalculate_group_addr(br);
776 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000777
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300778 return 0;
779}
780
781int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
782{
Xin Long047831a2016-04-09 00:03:31 +0800783 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000784}
785
Ido Schimmel1f514452017-05-26 08:37:23 +0200786bool br_vlan_enabled(const struct net_device *dev)
787{
788 struct net_bridge *br = netdev_priv(dev);
789
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300790 return br_opt_get(br, BROPT_VLAN_ENABLED);
Ido Schimmel1f514452017-05-26 08:37:23 +0200791}
792EXPORT_SYMBOL_GPL(br_vlan_enabled);
793
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900794int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900795{
796 int err = 0;
797 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200798 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200799 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900800 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900801
Toshiaki Makita204177f2014-06-10 20:59:25 +0900802 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900803 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900804
805 /* Add VLANs for the new proto to the device filter. */
806 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200807 vg = nbp_vlan_group(p);
808 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200809 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900810 if (err)
811 goto err_filt;
812 }
813 }
814
815 oldproto = br->vlan_proto;
816 br->vlan_proto = proto;
817
818 recalculate_group_addr(br);
819 br_recalculate_fwd_mask(br);
820
821 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200822 list_for_each_entry(p, &br->port_list, list) {
823 vg = nbp_vlan_group(p);
824 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200825 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200826 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900827
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900828 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900829
830err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200831 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200832 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900833
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200834 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
835 vg = nbp_vlan_group(p);
836 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200837 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200838 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900839
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900840 return err;
841}
842
843int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
844{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900845 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
846 return -EPROTONOSUPPORT;
847
Xin Long047831a2016-04-09 00:03:31 +0800848 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900849}
850
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200851int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
852{
853 switch (val) {
854 case 0:
855 case 1:
Nikolay Aleksandrovae757672018-09-26 17:01:00 +0300856 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200857 break;
858 default:
859 return -EINVAL;
860 }
861
862 return 0;
863}
864
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +0300865int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
866{
867 struct net_bridge_port *p;
868
869 /* allow to change the option if there are no port vlans configured */
870 list_for_each_entry(p, &br->port_list, list) {
871 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
872
873 if (vg->num_vlans)
874 return -EBUSY;
875 }
876
877 switch (val) {
878 case 0:
879 case 1:
880 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
881 break;
882 default:
883 return -EINVAL;
884 }
885
886 return 0;
887}
888
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200889static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400890{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200891 struct net_bridge_vlan *v;
892
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200893 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200894 return false;
895
896 v = br_vlan_lookup(&vg->vlan_hash, vid);
897 if (v && br_vlan_should_use(v) &&
898 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
899 return true;
900
901 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400902}
903
904static void br_vlan_disable_default_pvid(struct net_bridge *br)
905{
906 struct net_bridge_port *p;
907 u16 pvid = br->default_pvid;
908
909 /* Disable default_pvid on all ports where it is still
910 * configured.
911 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200912 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400913 br_vlan_delete(br, pvid);
914
915 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200916 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400917 nbp_vlan_delete(p, pvid);
918 }
919
920 br->default_pvid = 0;
921}
922
Petr Machata169327d2018-12-12 17:02:50 +0000923int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
924 struct netlink_ext_ack *extack)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400925{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200926 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200927 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400928 struct net_bridge_port *p;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300929 unsigned long *changed;
930 bool vlchange;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400931 u16 old_pvid;
932 int err = 0;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400933
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200934 if (!pvid) {
935 br_vlan_disable_default_pvid(br);
936 return 0;
937 }
938
Andy Shevchenko459479d2018-08-30 13:33:18 +0300939 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400940 if (!changed)
941 return -ENOMEM;
942
943 old_pvid = br->default_pvid;
944
945 /* Update default_pvid config only if we do not conflict with
946 * user configuration.
947 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200948 vg = br_vlan_group(br);
949 pvent = br_vlan_find(vg, pvid);
950 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200951 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400952 err = br_vlan_add(br, pvid,
953 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200954 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300955 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +0000956 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400957 if (err)
958 goto out;
959 br_vlan_delete(br, old_pvid);
960 set_bit(0, changed);
961 }
962
963 list_for_each_entry(p, &br->port_list, list) {
964 /* Update default_pvid config only if we do not conflict with
965 * user configuration.
966 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200967 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400968 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200969 !vlan_default_pvid(vg, old_pvid)) ||
970 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400971 continue;
972
973 err = nbp_vlan_add(p, pvid,
974 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300975 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000976 &vlchange, extack);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400977 if (err)
978 goto err_port;
979 nbp_vlan_delete(p, old_pvid);
980 set_bit(p->port_no, changed);
981 }
982
983 br->default_pvid = pvid;
984
985out:
Andy Shevchenko459479d2018-08-30 13:33:18 +0300986 bitmap_free(changed);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400987 return err;
988
989err_port:
990 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
991 if (!test_bit(p->port_no, changed))
992 continue;
993
994 if (old_pvid)
995 nbp_vlan_add(p, old_pvid,
996 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300997 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +0000998 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400999 nbp_vlan_delete(p, pvid);
1000 }
1001
1002 if (test_bit(0, changed)) {
1003 if (old_pvid)
1004 br_vlan_add(br, old_pvid,
1005 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001006 BRIDGE_VLAN_INFO_UNTAGGED |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001007 BRIDGE_VLAN_INFO_BRENTRY,
Petr Machata169327d2018-12-12 17:02:50 +00001008 &vlchange, NULL);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001009 br_vlan_delete(br, pvid);
1010 }
1011 goto out;
1012}
1013
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001014int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1015{
1016 u16 pvid = val;
1017 int err = 0;
1018
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001019 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001020 return -EINVAL;
1021
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001022 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +08001023 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001024
1025 /* Only allow default pvid change when filtering is disabled */
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001026 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001027 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1028 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +08001029 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001030 }
Petr Machata169327d2018-12-12 17:02:50 +00001031 err = __br_vlan_set_default_pvid(br, pvid, NULL);
Xin Long047831a2016-04-09 00:03:31 +08001032out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001033 return err;
1034}
1035
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001036int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +09001037{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001038 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001039 int ret = -ENOMEM;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001040 bool changed;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001041
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001042 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1043 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001044 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001045 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001046 if (ret)
1047 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001048 ret = vlan_tunnel_init(vg);
1049 if (ret)
1050 goto err_tunnel_init;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001051 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +09001052 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -04001053 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001054 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001055 ret = br_vlan_add(br, 1,
1056 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
Petr Machata169327d2018-12-12 17:02:50 +00001057 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001058 if (ret)
1059 goto err_vlan_add;
1060
1061out:
1062 return ret;
1063
1064err_vlan_add:
Roopa Prabhuefa53562017-01-31 22:59:54 -08001065 vlan_tunnel_deinit(vg);
1066err_tunnel_init:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001067 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001068err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001069 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001070
1071 goto out;
1072}
1073
Petr Machata169327d2018-12-12 17:02:50 +00001074int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001075{
Elad Raz404cdbf2016-01-06 13:01:07 +01001076 struct switchdev_attr attr = {
1077 .orig_dev = p->br->dev,
1078 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1079 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001080 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
Elad Raz404cdbf2016-01-06 13:01:07 +01001081 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001082 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001083 int ret = -ENOMEM;
1084
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001085 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1086 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001087 goto out;
1088
Elad Raz404cdbf2016-01-06 13:01:07 +01001089 ret = switchdev_port_attr_set(p->dev, &attr);
1090 if (ret && ret != -EOPNOTSUPP)
1091 goto err_vlan_enabled;
1092
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001093 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001094 if (ret)
1095 goto err_rhtbl;
Roopa Prabhuefa53562017-01-31 22:59:54 -08001096 ret = vlan_tunnel_init(vg);
1097 if (ret)
1098 goto err_tunnel_init;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001099 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001100 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001101 if (p->br->default_pvid) {
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001102 bool changed;
1103
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001104 ret = nbp_vlan_add(p, p->br->default_pvid,
1105 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001106 BRIDGE_VLAN_INFO_UNTAGGED,
Petr Machata169327d2018-12-12 17:02:50 +00001107 &changed, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001108 if (ret)
1109 goto err_vlan_add;
1110 }
1111out:
1112 return ret;
1113
1114err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +01001115 RCU_INIT_POINTER(p->vlgrp, NULL);
1116 synchronize_rcu();
Roopa Prabhuefa53562017-01-31 22:59:54 -08001117 vlan_tunnel_deinit(vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -08001118err_tunnel_init:
1119 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001120err_rhtbl:
Yotam Gigidf2c4332017-03-01 16:50:45 +02001121err_vlan_enabled:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +02001122 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001123
1124 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +09001125}
1126
Toshiaki Makita8adff412013-10-16 17:07:13 +09001127/* Must be protected by RTNL.
1128 * Must be called with vid in range from 1 to 4094 inclusive.
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001129 * changed must be true only if the vlan was created or updated
Toshiaki Makita8adff412013-10-16 17:07:13 +09001130 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001131int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
Petr Machata169327d2018-12-12 17:02:50 +00001132 bool *changed, struct netlink_ext_ack *extack)
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001133{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001134 struct net_bridge_vlan *vlan;
1135 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001136
1137 ASSERT_RTNL();
1138
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001139 *changed = false;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001140 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001141 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +01001142 /* Pass the flags to the hardware bridge */
Petr Machata169327d2018-12-12 17:02:50 +00001143 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
Ido Schimmel7fbac982016-02-18 14:01:46 +01001144 if (ret && ret != -EOPNOTSUPP)
1145 return ret;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001146 *changed = __vlan_add_flags(vlan, flags);
1147
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001148 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001149 }
1150
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001151 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1152 if (!vlan)
1153 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001154
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001155 vlan->vid = vid;
1156 vlan->port = port;
Petr Machata169327d2018-12-12 17:02:50 +00001157 ret = __vlan_add(vlan, flags, extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001158 if (ret)
1159 kfree(vlan);
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +03001160 else
1161 *changed = true;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001162
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001163 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001164}
1165
Toshiaki Makita8adff412013-10-16 17:07:13 +09001166/* Must be protected by RTNL.
1167 * Must be called with vid in range from 1 to 4094 inclusive.
1168 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001169int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1170{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001171 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001172
1173 ASSERT_RTNL();
1174
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001175 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001176 if (!v)
1177 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001178 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001179 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001180
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001181 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001182}
1183
1184void nbp_vlan_flush(struct net_bridge_port *port)
1185{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001186 struct net_bridge_vlan_group *vg;
1187
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001188 ASSERT_RTNL();
1189
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001190 vg = nbp_vlan_group(port);
1191 __vlan_flush(vg);
1192 RCU_INIT_POINTER(port->vlgrp, NULL);
1193 synchronize_rcu();
1194 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001195}
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001196
1197void br_vlan_get_stats(const struct net_bridge_vlan *v,
1198 struct br_vlan_stats *stats)
1199{
1200 int i;
1201
1202 memset(stats, 0, sizeof(*stats));
1203 for_each_possible_cpu(i) {
1204 u64 rxpackets, rxbytes, txpackets, txbytes;
1205 struct br_vlan_stats *cpu_stats;
1206 unsigned int start;
1207
1208 cpu_stats = per_cpu_ptr(v->stats, i);
1209 do {
1210 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1211 rxpackets = cpu_stats->rx_packets;
1212 rxbytes = cpu_stats->rx_bytes;
1213 txbytes = cpu_stats->tx_bytes;
1214 txpackets = cpu_stats->tx_packets;
1215 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1216
1217 stats->rx_packets += rxpackets;
1218 stats->rx_bytes += rxbytes;
1219 stats->tx_bytes += txbytes;
1220 stats->tx_packets += txpackets;
1221 }
1222}
Petr Machata4d4fd362018-04-29 10:56:08 +03001223
1224int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1225{
1226 struct net_bridge_vlan_group *vg;
Ido Schimmel5a6db042018-11-28 20:06:58 +00001227 struct net_bridge_port *p;
Petr Machata4d4fd362018-04-29 10:56:08 +03001228
1229 ASSERT_RTNL();
Ido Schimmel5a6db042018-11-28 20:06:58 +00001230 p = br_port_get_check_rtnl(dev);
1231 if (p)
1232 vg = nbp_vlan_group(p);
1233 else if (netif_is_bridge_master(dev))
Petr Machata4d4fd362018-04-29 10:56:08 +03001234 vg = br_vlan_group(netdev_priv(dev));
1235 else
1236 return -EINVAL;
1237
1238 *p_pvid = br_get_pvid(vg);
1239 return 0;
1240}
1241EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1242
1243int br_vlan_get_info(const struct net_device *dev, u16 vid,
1244 struct bridge_vlan_info *p_vinfo)
1245{
1246 struct net_bridge_vlan_group *vg;
1247 struct net_bridge_vlan *v;
1248 struct net_bridge_port *p;
1249
1250 ASSERT_RTNL();
1251 p = br_port_get_check_rtnl(dev);
1252 if (p)
1253 vg = nbp_vlan_group(p);
Petr Machata2b18d792018-05-10 13:13:03 +03001254 else if (netif_is_bridge_master(dev))
1255 vg = br_vlan_group(netdev_priv(dev));
Petr Machata4d4fd362018-04-29 10:56:08 +03001256 else
1257 return -EINVAL;
1258
1259 v = br_vlan_find(vg, vid);
1260 if (!v)
1261 return -ENOENT;
1262
1263 p_vinfo->vid = vid;
1264 p_vinfo->flags = v->flags;
1265 return 0;
1266}
1267EXPORT_SYMBOL_GPL(br_vlan_get_info);