blob: e001152d6ad1a406c81c4fe3e91d6f983b189aeb [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"
8
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02009static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
10 const void *ptr)
Vlad Yasevich552406c2013-02-13 12:00:15 +000011{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020012 const struct net_bridge_vlan *vle = ptr;
13 u16 vid = *(u16 *)arg->key;
14
15 return vle->vid != vid;
16}
17
18static const struct rhashtable_params br_vlan_rht_params = {
19 .head_offset = offsetof(struct net_bridge_vlan, vnode),
20 .key_offset = offsetof(struct net_bridge_vlan, vid),
21 .key_len = sizeof(u16),
Nikolay Aleksandrov8af78b62015-09-30 20:16:51 +020022 .nelem_hint = 3,
23 .locks_mul = 1,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020024 .max_size = VLAN_N_VID,
25 .obj_cmpfn = br_vlan_cmp,
26 .automatic_shrinking = true,
27};
28
29static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
30{
31 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
32}
33
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020034static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020035{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020036 if (vg->pvid == vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000037 return;
38
39 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020040 vg->pvid = vid;
Vlad Yasevich552406c2013-02-13 12:00:15 +000041}
42
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020043static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000044{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020045 if (vg->pvid != vid)
Vlad Yasevich552406c2013-02-13 12:00:15 +000046 return;
47
48 smp_wmb();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020049 vg->pvid = 0;
Vlad Yasevich552406c2013-02-13 12:00:15 +000050}
51
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020052static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich35e03f32013-02-13 12:00:20 +000053{
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020054 struct net_bridge_vlan_group *vg;
55
56 if (br_vlan_is_master(v))
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020057 vg = br_vlan_group(v->br);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020058 else
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020059 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020060
61 if (flags & BRIDGE_VLAN_INFO_PVID)
62 __vlan_add_pvid(vg, v->vid);
63 else
64 __vlan_delete_pvid(vg, v->vid);
Vlad Yasevich35e03f32013-02-13 12:00:20 +000065
66 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020067 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040068 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020069 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich35e03f32013-02-13 12:00:20 +000070}
71
Scott Feldman7f109532015-06-12 17:39:50 -070072static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
73 u16 vid, u16 flags)
74{
Jiri Pirko0944d6b2015-10-09 13:54:11 +020075 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010076 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +020077 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
78 .flags = flags,
79 .vid_begin = vid,
80 .vid_end = vid,
81 };
Scott Feldman7f109532015-06-12 17:39:50 -070082 int err;
83
Jiri Pirko0944d6b2015-10-09 13:54:11 +020084 /* Try switchdev op first. In case it is not supported, fallback to
85 * 8021q add.
Scott Feldman7f109532015-06-12 17:39:50 -070086 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +020087 err = switchdev_port_obj_add(dev, &v.obj);
88 if (err == -EOPNOTSUPP)
89 return vlan_vid_add(dev, br->vlan_proto, vid);
Scott Feldman7f109532015-06-12 17:39:50 -070090 return err;
91}
92
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020093static void __vlan_add_list(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +000094{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020095 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020096 struct list_head *headp, *hpos;
97 struct net_bridge_vlan *vent;
Vlad Yasevich243a2e62013-02-13 12:00:09 +000098
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020099 if (br_vlan_is_master(v))
100 vg = br_vlan_group(v->br);
101 else
102 vg = nbp_vlan_group(v->port);
103
104 headp = &vg->vlan_list;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200105 list_for_each_prev(hpos, headp) {
106 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
107 if (v->vid < vent->vid)
108 continue;
109 else
110 break;
Vlad Yasevich552406c2013-02-13 12:00:15 +0000111 }
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200112 list_add_rcu(&v->vlist, hpos);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200113}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000114
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200115static void __vlan_del_list(struct net_bridge_vlan *v)
116{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +0200117 list_del_rcu(&v->vlist);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000118}
119
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400120static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
121 u16 vid)
Scott Feldman7f109532015-06-12 17:39:50 -0700122{
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200123 struct switchdev_obj_port_vlan v = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100124 .obj.orig_dev = dev,
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200125 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
126 .vid_begin = vid,
127 .vid_end = vid,
128 };
129 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 */
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200134 err = switchdev_port_obj_del(dev, &v.obj);
135 if (err == -EOPNOTSUPP) {
Scott Feldman7f109532015-06-12 17:39:50 -0700136 vlan_vid_del(dev, br->vlan_proto, vid);
Jiri Pirko0944d6b2015-10-09 13:54:11 +0200137 return 0;
Scott Feldman7f109532015-06-12 17:39:50 -0700138 }
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400139 return err;
Scott Feldman7f109532015-06-12 17:39:50 -0700140}
141
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200142/* Returns a master vlan, if it didn't exist it gets created. In all cases a
143 * a reference is taken to the master vlan before returning.
144 */
145static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
146{
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) {
153 /* missing global ctx, create it now */
154 if (br_vlan_add(br, vid, 0))
155 return NULL;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200156 masterv = br_vlan_find(vg, vid);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200157 if (WARN_ON(!masterv))
158 return NULL;
159 }
160 atomic_inc(&masterv->refcnt);
161
162 return masterv;
163}
164
165static void br_vlan_put_master(struct net_bridge_vlan *masterv)
166{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200167 struct net_bridge_vlan_group *vg;
168
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200169 if (!br_vlan_is_master(masterv))
170 return;
171
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200172 vg = br_vlan_group(masterv->br);
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200173 if (atomic_dec_and_test(&masterv->refcnt)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200174 rhashtable_remove_fast(&vg->vlan_hash,
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200175 &masterv->vnode, br_vlan_rht_params);
176 __vlan_del_list(masterv);
177 kfree_rcu(masterv, rcu);
178 }
179}
180
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200181/* This is the shared VLAN add function which works for both ports and bridge
182 * devices. There are four possible calls to this function in terms of the
183 * vlan entry type:
184 * 1. vlan is being added on a port (no master flags, global entry exists)
Ido Schimmelddd611d2015-10-30 17:46:20 +0100185 * 2. vlan is being added on a bridge (both master and brentry flags)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200186 * 3. vlan is being added on a port, but a global entry didn't exist which
Ido Schimmelddd611d2015-10-30 17:46:20 +0100187 * is being created right now (master flag set, brentry flag unset), the
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200188 * global entry is used for global per-vlan features, but not for filtering
Ido Schimmelddd611d2015-10-30 17:46:20 +0100189 * 4. same as 3 but with both master and brentry flags set so the entry
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200190 * will be used for filtering in both the port and the bridge
191 */
192static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000193{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200194 struct net_bridge_vlan *masterv = NULL;
195 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200196 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200197 struct net_device *dev;
198 struct net_bridge *br;
199 int err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000200
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200201 if (br_vlan_is_master(v)) {
202 br = v->br;
203 dev = br->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200204 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200205 } else {
206 p = v->port;
207 br = p->br;
208 dev = p->dev;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200209 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000211
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200212 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200213 /* Add VLAN to the device filter if it is supported.
214 * This ensures tagged traffic enters the bridge when
215 * promiscuous mode is disabled by br_manage_promisc().
216 */
217 err = __vlan_vid_add(dev, br, v->vid, flags);
Vivien Didelotbf361ad2015-09-05 21:27:57 -0400218 if (err)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200219 goto out;
220
221 /* need to work on the master vlan too */
222 if (flags & BRIDGE_VLAN_INFO_MASTER) {
Nikolay Aleksandrov2ffdf5082015-10-02 15:05:12 +0200223 err = br_vlan_add(br, v->vid, flags |
224 BRIDGE_VLAN_INFO_BRENTRY);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200225 if (err)
226 goto out_filt;
227 }
228
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200229 masterv = br_vlan_get_master(br, v->vid);
230 if (!masterv)
231 goto out_filt;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200232 v->brvlan = masterv;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900233 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000234
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200235 /* Add the dev mac and count the vlan only if it's usable */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200236 if (br_vlan_should_use(v)) {
237 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
238 if (err) {
239 br_err(br, "failed insert local address into bridge forwarding table\n");
240 goto out_filt;
241 }
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200242 vg->num_vlans++;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000243 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200244
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200245 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
246 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200247 if (err)
248 goto out_fdb_insert;
249
250 __vlan_add_list(v);
251 __vlan_add_flags(v, flags);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200252out:
253 return err;
254
255out_fdb_insert:
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200256 if (br_vlan_should_use(v)) {
257 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
258 vg->num_vlans--;
259 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200260
261out_filt:
262 if (p) {
263 __vlan_vid_del(dev, br, v->vid);
264 if (masterv) {
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200265 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200266 v->brvlan = NULL;
267 }
268 }
269
270 goto out;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000271}
272
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200273static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000274{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200275 struct net_bridge_vlan *masterv = v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200276 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200277 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200278 int err = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200279
280 if (br_vlan_is_master(v)) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200281 vg = br_vlan_group(v->br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200282 } else {
283 p = v->port;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200284 vg = nbp_vlan_group(v->port);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200285 masterv = v->brvlan;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200286 }
287
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200288 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200289 if (p) {
290 err = __vlan_vid_del(p->dev, p->br, v->vid);
291 if (err)
292 goto out;
293 }
294
Nikolay Aleksandrov6be144f2015-10-02 15:05:13 +0200295 if (br_vlan_should_use(v)) {
296 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
297 vg->num_vlans--;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200298 }
299
300 if (masterv != v) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200301 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
302 br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200303 __vlan_del_list(v);
304 kfree_rcu(v, rcu);
305 }
306
Nikolay Aleksandrovf8ed2892015-10-02 15:05:11 +0200307 br_vlan_put_master(masterv);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200308out:
309 return err;
310}
311
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200312static void __vlan_group_free(struct net_bridge_vlan_group *vg)
313{
314 WARN_ON(!list_empty(&vg->vlan_list));
315 rhashtable_destroy(&vg->vlan_hash);
316 kfree(vg);
317}
318
319static void __vlan_flush(struct net_bridge_vlan_group *vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200320{
321 struct net_bridge_vlan *vlan, *tmp;
322
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200323 __vlan_delete_pvid(vg, vg->pvid);
324 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 __vlan_del(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000326}
327
Vlad Yasevich78851982013-02-13 12:00:14 +0000328struct sk_buff *br_handle_vlan(struct net_bridge *br,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200329 struct net_bridge_vlan_group *vg,
Vlad Yasevich78851982013-02-13 12:00:14 +0000330 struct sk_buff *skb)
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000331{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 struct net_bridge_vlan *v;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000333 u16 vid;
334
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400335 /* If this packet was not filtered at input, let it pass */
336 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich78851982013-02-13 12:00:14 +0000337 goto out;
338
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200339 /* At this point, we know that the frame was filtered and contains
340 * a valid vlan id. If the vlan id has untagged flag set,
341 * send untagged; otherwise, send tagged.
342 */
343 br_vlan_get_tag(skb, &vid);
344 v = br_vlan_find(vg, vid);
345 /* Vlan entry must be configured at this point. The
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400346 * only exception is the bridge is set in promisc mode and the
347 * packet is destined for the bridge device. In this case
348 * pass the packet as is.
349 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200350 if (!v || !br_vlan_should_use(v)) {
Vlad Yasevichfc92f742014-03-27 21:51:18 -0400351 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
352 goto out;
353 } else {
354 kfree_skb(skb);
355 return NULL;
356 }
357 }
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200358 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Toshiaki Makita99b192d2014-03-27 21:46:56 +0900359 skb->vlan_tci = 0;
Vlad Yasevich78851982013-02-13 12:00:14 +0000360
361out:
362 return skb;
363}
364
365/* Called under RCU */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200366static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200367 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000368{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200369 const struct net_bridge_vlan *v;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900370 bool tagged;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000371
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400372 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900373 /* If vlan tx offload is disabled on bridge device and frame was
374 * sent from vlan device on the bridge device, it does not have
375 * HW accelerated vlan tag.
376 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100377 if (unlikely(!skb_vlan_tag_present(skb) &&
Toshiaki Makita8580e212014-06-10 20:59:23 +0900378 skb->protocol == proto)) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -0400379 skb = skb_vlan_untag(skb);
Toshiaki Makita12464bb2014-03-27 21:46:55 +0900380 if (unlikely(!skb))
381 return false;
382 }
383
Toshiaki Makita8580e212014-06-10 20:59:23 +0900384 if (!br_vlan_get_tag(skb, vid)) {
385 /* Tagged frame */
386 if (skb->vlan_proto != proto) {
387 /* Protocol-mismatch, empty out vlan_tci for new tag */
388 skb_push(skb, ETH_HLEN);
Jiri Pirko62749e22014-11-19 14:04:58 +0100389 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100390 skb_vlan_tag_get(skb));
Toshiaki Makita8580e212014-06-10 20:59:23 +0900391 if (unlikely(!skb))
392 return false;
393
394 skb_pull(skb, ETH_HLEN);
395 skb_reset_mac_len(skb);
396 *vid = 0;
397 tagged = false;
398 } else {
399 tagged = true;
400 }
401 } else {
402 /* Untagged frame */
403 tagged = false;
404 }
405
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900406 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200407 u16 pvid = br_get_pvid(vg);
408
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900409 /* Frame had a tag with VID 0 or did not have a tag.
410 * See if pvid is set on this port. That tells us which
411 * vlan untagged or priority-tagged traffic belongs to.
Vlad Yasevich78851982013-02-13 12:00:14 +0000412 */
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400413 if (!pvid)
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900414 goto drop;
Vlad Yasevich78851982013-02-13 12:00:14 +0000415
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900416 /* PVID is set on this port. Any untagged or priority-tagged
417 * ingress frame is considered to belong to this vlan.
Vlad Yasevich78851982013-02-13 12:00:14 +0000418 */
Toshiaki Makitadfb5fa32013-10-16 17:07:16 +0900419 *vid = pvid;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900420 if (likely(!tagged))
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900421 /* Untagged Frame. */
Toshiaki Makita8580e212014-06-10 20:59:23 +0900422 __vlan_hwaccel_put_tag(skb, proto, pvid);
Toshiaki Makitab90356ce2013-10-16 17:07:14 +0900423 else
424 /* Priority-tagged Frame.
425 * At this point, We know that skb->vlan_tci had
426 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
427 * We update only VID field and preserve PCP field.
428 */
429 skb->vlan_tci |= pvid;
430
Vlad Yasevich78851982013-02-13 12:00:14 +0000431 return true;
432 }
433
434 /* Frame had a valid vlan tag. See if vlan is allowed */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200435 v = br_vlan_find(vg, *vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200436 if (v && br_vlan_should_use(v))
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000437 return true;
Toshiaki Makitaeb707612014-04-09 17:00:30 +0900438drop:
439 kfree_skb(skb);
Vlad Yasevicha37b85c2013-02-13 12:00:10 +0000440 return false;
441}
442
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200443bool br_allowed_ingress(const struct net_bridge *br,
444 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
445 u16 *vid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200446{
447 /* If VLAN filtering is disabled on the bridge, all packets are
448 * permitted.
449 */
450 if (!br->vlan_enabled) {
451 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
452 return true;
453 }
454
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200455 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200456}
457
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000458/* Called under RCU. */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200459bool br_allowed_egress(struct net_bridge_vlan_group *vg,
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000460 const struct sk_buff *skb)
461{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200462 const struct net_bridge_vlan *v;
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000463 u16 vid;
464
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400465 /* If this packet was not filtered at input, let it pass */
466 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000467 return true;
468
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000469 br_vlan_get_tag(skb, &vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200470 v = br_vlan_find(vg, vid);
471 if (v && br_vlan_should_use(v))
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000472 return true;
473
474 return false;
475}
476
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900477/* Called under RCU */
478bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
479{
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200480 struct net_bridge_vlan_group *vg;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900481 struct net_bridge *br = p->br;
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900482
Vlad Yasevich20adfa12014-09-12 16:26:16 -0400483 /* If filtering was disabled at input, let it pass. */
Vlad Yasevichc095f242014-09-15 15:24:26 -0400484 if (!br->vlan_enabled)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900485 return true;
486
Ido Schimmeleca1e002015-11-01 18:31:45 +0200487 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov468e7942015-09-30 20:16:52 +0200488 if (!vg || !vg->num_vlans)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900489 return false;
490
Toshiaki Makita8580e212014-06-10 20:59:23 +0900491 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
492 *vid = 0;
493
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900494 if (!*vid) {
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200495 *vid = br_get_pvid(vg);
Vlad Yasevich3df6bf42014-10-03 11:29:17 -0400496 if (!*vid)
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900497 return false;
498
499 return true;
500 }
501
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200502 if (br_vlan_find(vg, *vid))
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900503 return true;
504
505 return false;
506}
507
Toshiaki Makita8adff412013-10-16 17:07:13 +0900508/* Must be protected by RTNL.
509 * Must be called with vid in range from 1 to 4094 inclusive.
510 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000511int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000512{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200513 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200514 struct net_bridge_vlan *vlan;
515 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000516
517 ASSERT_RTNL();
518
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200519 vg = br_vlan_group(br);
520 vlan = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200521 if (vlan) {
522 if (!br_vlan_is_brentry(vlan)) {
523 /* Trying to change flags of non-existent bridge vlan */
524 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
525 return -EINVAL;
526 /* It was only kept for port vlans, now make it real */
527 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
528 vlan->vid);
529 if (ret) {
530 br_err(br, "failed insert local address into bridge forwarding table\n");
531 return ret;
532 }
533 atomic_inc(&vlan->refcnt);
534 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200535 vg->num_vlans++;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 }
537 __vlan_add_flags(vlan, flags);
538 return 0;
539 }
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000540
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200541 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
542 if (!vlan)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000543 return -ENOMEM;
544
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200545 vlan->vid = vid;
546 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
547 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
548 vlan->br = br;
549 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
550 atomic_set(&vlan->refcnt, 1);
551 ret = __vlan_add(vlan, flags);
552 if (ret)
553 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000554
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200555 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000556}
557
Toshiaki Makita8adff412013-10-16 17:07:13 +0900558/* Must be protected by RTNL.
559 * Must be called with vid in range from 1 to 4094 inclusive.
560 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000561int br_vlan_delete(struct net_bridge *br, u16 vid)
562{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200563 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200564 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000565
566 ASSERT_RTNL();
567
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200568 vg = br_vlan_group(br);
569 v = br_vlan_find(vg, vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200570 if (!v || !br_vlan_is_brentry(v))
571 return -ENOENT;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000572
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900573 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700574 br_fdb_delete_by_port(br, NULL, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000575
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200576 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000577}
578
579void br_vlan_flush(struct net_bridge *br)
580{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200581 struct net_bridge_vlan_group *vg;
582
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000583 ASSERT_RTNL();
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000584
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200585 vg = br_vlan_group(br);
586 __vlan_flush(vg);
587 RCU_INIT_POINTER(br->vlgrp, NULL);
588 synchronize_rcu();
589 __vlan_group_free(vg);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000590}
591
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200592struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900593{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200594 if (!vg)
595 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900596
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200597 return br_vlan_lookup(&vg->vlan_hash, vid);
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900598}
599
Toshiaki Makita204177f2014-06-10 20:59:25 +0900600/* Must be protected by RTNL. */
601static void recalculate_group_addr(struct net_bridge *br)
602{
603 if (br->group_addr_set)
604 return;
605
606 spin_lock_bh(&br->lock);
607 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
608 /* Bridge Group Address */
609 br->group_addr[5] = 0x00;
610 } else { /* vlan_enabled && ETH_P_8021AD */
611 /* Provider Bridge Group Address */
612 br->group_addr[5] = 0x08;
613 }
614 spin_unlock_bh(&br->lock);
615}
616
617/* Must be protected by RTNL. */
618void br_recalculate_fwd_mask(struct net_bridge *br)
619{
620 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
621 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
622 else /* vlan_enabled && ETH_P_8021AD */
623 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
624 ~(1u << br->group_addr[5]);
625}
626
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300627int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000628{
Elad Raz6b72a772016-01-06 13:01:06 +0100629 struct switchdev_attr attr = {
630 .orig_dev = br->dev,
631 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
632 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
633 .u.vlan_filtering = val,
634 };
635 int err;
636
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000637 if (br->vlan_enabled == val)
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300638 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000639
Elad Raz6b72a772016-01-06 13:01:06 +0100640 err = switchdev_port_attr_set(br->dev, &attr);
641 if (err && err != -EOPNOTSUPP)
642 return err;
643
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000644 br->vlan_enabled = val;
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400645 br_manage_promisc(br);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900646 recalculate_group_addr(br);
647 br_recalculate_fwd_mask(br);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000648
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300649 return 0;
650}
651
652int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
653{
Xin Long047831a2016-04-09 00:03:31 +0800654 return __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000655}
656
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900657int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900658{
659 int err = 0;
660 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200661 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200662 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900663 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900664
Toshiaki Makita204177f2014-06-10 20:59:25 +0900665 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900666 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900667
668 /* Add VLANs for the new proto to the device filter. */
669 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200670 vg = nbp_vlan_group(p);
671 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200672 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900673 if (err)
674 goto err_filt;
675 }
676 }
677
678 oldproto = br->vlan_proto;
679 br->vlan_proto = proto;
680
681 recalculate_group_addr(br);
682 br_recalculate_fwd_mask(br);
683
684 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200685 list_for_each_entry(p, &br->port_list, list) {
686 vg = nbp_vlan_group(p);
687 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200688 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200689 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900690
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900691 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900692
693err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200694 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200695 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900696
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200697 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
698 vg = nbp_vlan_group(p);
699 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200700 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200701 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900702
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900703 return err;
704}
705
706int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
707{
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900708 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
709 return -EPROTONOSUPPORT;
710
Xin Long047831a2016-04-09 00:03:31 +0800711 return __br_vlan_set_proto(br, htons(val));
Toshiaki Makita204177f2014-06-10 20:59:25 +0900712}
713
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200714static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400715{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200716 struct net_bridge_vlan *v;
717
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200718 if (vid != vg->pvid)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200719 return false;
720
721 v = br_vlan_lookup(&vg->vlan_hash, vid);
722 if (v && br_vlan_should_use(v) &&
723 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
724 return true;
725
726 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400727}
728
729static void br_vlan_disable_default_pvid(struct net_bridge *br)
730{
731 struct net_bridge_port *p;
732 u16 pvid = br->default_pvid;
733
734 /* Disable default_pvid on all ports where it is still
735 * configured.
736 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200737 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400738 br_vlan_delete(br, pvid);
739
740 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200741 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400742 nbp_vlan_delete(p, pvid);
743 }
744
745 br->default_pvid = 0;
746}
747
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200748int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400749{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200750 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200751 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400752 struct net_bridge_port *p;
753 u16 old_pvid;
754 int err = 0;
755 unsigned long *changed;
756
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200757 if (!pvid) {
758 br_vlan_disable_default_pvid(br);
759 return 0;
760 }
761
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400762 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
763 GFP_KERNEL);
764 if (!changed)
765 return -ENOMEM;
766
767 old_pvid = br->default_pvid;
768
769 /* Update default_pvid config only if we do not conflict with
770 * user configuration.
771 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200772 vg = br_vlan_group(br);
773 pvent = br_vlan_find(vg, pvid);
774 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200775 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400776 err = br_vlan_add(br, pvid,
777 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200778 BRIDGE_VLAN_INFO_UNTAGGED |
779 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400780 if (err)
781 goto out;
782 br_vlan_delete(br, old_pvid);
783 set_bit(0, changed);
784 }
785
786 list_for_each_entry(p, &br->port_list, list) {
787 /* Update default_pvid config only if we do not conflict with
788 * user configuration.
789 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200790 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400791 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200792 !vlan_default_pvid(vg, old_pvid)) ||
793 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400794 continue;
795
796 err = nbp_vlan_add(p, pvid,
797 BRIDGE_VLAN_INFO_PVID |
798 BRIDGE_VLAN_INFO_UNTAGGED);
799 if (err)
800 goto err_port;
801 nbp_vlan_delete(p, old_pvid);
802 set_bit(p->port_no, changed);
803 }
804
805 br->default_pvid = pvid;
806
807out:
808 kfree(changed);
809 return err;
810
811err_port:
812 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
813 if (!test_bit(p->port_no, changed))
814 continue;
815
816 if (old_pvid)
817 nbp_vlan_add(p, old_pvid,
818 BRIDGE_VLAN_INFO_PVID |
819 BRIDGE_VLAN_INFO_UNTAGGED);
820 nbp_vlan_delete(p, pvid);
821 }
822
823 if (test_bit(0, changed)) {
824 if (old_pvid)
825 br_vlan_add(br, old_pvid,
826 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200827 BRIDGE_VLAN_INFO_UNTAGGED |
828 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400829 br_vlan_delete(br, pvid);
830 }
831 goto out;
832}
833
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400834int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
835{
836 u16 pvid = val;
837 int err = 0;
838
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400839 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400840 return -EINVAL;
841
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400842 if (pvid == br->default_pvid)
Xin Long047831a2016-04-09 00:03:31 +0800843 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400844
845 /* Only allow default pvid change when filtering is disabled */
846 if (br->vlan_enabled) {
847 pr_info_once("Please disable vlan filtering to change default_pvid\n");
848 err = -EPERM;
Xin Long047831a2016-04-09 00:03:31 +0800849 goto out;
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400850 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200851 err = __br_vlan_set_default_pvid(br, pvid);
Xin Long047831a2016-04-09 00:03:31 +0800852out:
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400853 return err;
854}
855
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400856int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900857{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200858 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200859 int ret = -ENOMEM;
860
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200861 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
862 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200863 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200864 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200865 if (ret)
866 goto err_rhtbl;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200867 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900868 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400869 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200870 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200871 ret = br_vlan_add(br, 1,
872 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
873 BRIDGE_VLAN_INFO_BRENTRY);
874 if (ret)
875 goto err_vlan_add;
876
877out:
878 return ret;
879
880err_vlan_add:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200881 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200882err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200883 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200884
885 goto out;
886}
887
888int nbp_vlan_init(struct net_bridge_port *p)
889{
Elad Raz404cdbf2016-01-06 13:01:07 +0100890 struct switchdev_attr attr = {
891 .orig_dev = p->br->dev,
892 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
893 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
894 .u.vlan_filtering = p->br->vlan_enabled,
895 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200896 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200897 int ret = -ENOMEM;
898
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200899 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
900 if (!vg)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200901 goto out;
902
Elad Raz404cdbf2016-01-06 13:01:07 +0100903 ret = switchdev_port_attr_set(p->dev, &attr);
904 if (ret && ret != -EOPNOTSUPP)
905 goto err_vlan_enabled;
906
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200907 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200908 if (ret)
909 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200910 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200911 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200912 if (p->br->default_pvid) {
913 ret = nbp_vlan_add(p, p->br->default_pvid,
914 BRIDGE_VLAN_INFO_PVID |
915 BRIDGE_VLAN_INFO_UNTAGGED);
916 if (ret)
917 goto err_vlan_add;
918 }
919out:
920 return ret;
921
922err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +0100923 RCU_INIT_POINTER(p->vlgrp, NULL);
924 synchronize_rcu();
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200925 rhashtable_destroy(&vg->vlan_hash);
Elad Raz404cdbf2016-01-06 13:01:07 +0100926err_vlan_enabled:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200927err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200928 kfree(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200929
930 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900931}
932
Toshiaki Makita8adff412013-10-16 17:07:13 +0900933/* Must be protected by RTNL.
934 * Must be called with vid in range from 1 to 4094 inclusive.
935 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000936int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000937{
Ido Schimmel7fbac982016-02-18 14:01:46 +0100938 struct switchdev_obj_port_vlan v = {
939 .obj.orig_dev = port->dev,
940 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
941 .flags = flags,
942 .vid_begin = vid,
943 .vid_end = vid,
944 };
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200945 struct net_bridge_vlan *vlan;
946 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000947
948 ASSERT_RTNL();
949
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200950 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200951 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +0100952 /* Pass the flags to the hardware bridge */
953 ret = switchdev_port_obj_add(port->dev, &v.obj);
954 if (ret && ret != -EOPNOTSUPP)
955 return ret;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200956 __vlan_add_flags(vlan, flags);
957 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000958 }
959
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200960 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
961 if (!vlan)
962 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000963
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200964 vlan->vid = vid;
965 vlan->port = port;
966 ret = __vlan_add(vlan, flags);
967 if (ret)
968 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000969
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200970 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000971}
972
Toshiaki Makita8adff412013-10-16 17:07:13 +0900973/* Must be protected by RTNL.
974 * Must be called with vid in range from 1 to 4094 inclusive.
975 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000976int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
977{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200978 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000979
980 ASSERT_RTNL();
981
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200982 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200983 if (!v)
984 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900985 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700986 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000987
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200988 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000989}
990
991void nbp_vlan_flush(struct net_bridge_port *port)
992{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200993 struct net_bridge_vlan_group *vg;
994
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000995 ASSERT_RTNL();
996
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200997 vg = nbp_vlan_group(port);
998 __vlan_flush(vg);
999 RCU_INIT_POINTER(port->vlgrp, NULL);
1000 synchronize_rcu();
1001 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001002}