blob: 9309bb4f2a5b240edcea6f9c3a4a45d2c6413874 [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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +020067 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Vlad Yasevich635126b2014-09-12 16:26:17 -040068 else
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200113}
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000114
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200210 }
Vlad Yasevich552406c2013-02-13 12:00:15 +0000211
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200212 if (p) {
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200273static int __vlan_del(struct net_bridge_vlan *v)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000274{
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200277 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200278 int err = 0;
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200285 masterv = v->brvlan;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200286 }
287
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200288 __vlan_delete_pvid(vg, v->vid);
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200367 struct sk_buff *skb, u16 *vid)
Vlad Yasevich78851982013-02-13 12:00:14 +0000368{
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200456}
457
Vlad Yasevich85f46c62013-02-13 12:00:11 +0000458/* Called under RCU. */
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +0200594 if (!vg)
595 return NULL;
Toshiaki Makita2b292fb2014-02-07 16:48:22 +0900596
Nikolay Aleksandrov2594e902015-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{
Elad Raz6b72a772016-01-06 13:01:06 +0100654 int err;
655
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300656 if (!rtnl_trylock())
657 return restart_syscall();
658
Elad Raz6b72a772016-01-06 13:01:06 +0100659 err = __br_vlan_filter_toggle(br, val);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000660 rtnl_unlock();
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300661
Elad Raz6b72a772016-01-06 13:01:06 +0100662 return err;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000663}
664
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900665int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
Toshiaki Makita204177f2014-06-10 20:59:25 +0900666{
667 int err = 0;
668 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200669 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200670 struct net_bridge_vlan_group *vg;
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900671 __be16 oldproto;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900672
Toshiaki Makita204177f2014-06-10 20:59:25 +0900673 if (br->vlan_proto == proto)
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900674 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900675
676 /* Add VLANs for the new proto to the device filter. */
677 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200678 vg = nbp_vlan_group(p);
679 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200680 err = vlan_vid_add(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900681 if (err)
682 goto err_filt;
683 }
684 }
685
686 oldproto = br->vlan_proto;
687 br->vlan_proto = proto;
688
689 recalculate_group_addr(br);
690 br_recalculate_fwd_mask(br);
691
692 /* Delete VLANs for the old proto from the device filter. */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200693 list_for_each_entry(p, &br->port_list, list) {
694 vg = nbp_vlan_group(p);
695 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200696 vlan_vid_del(p->dev, oldproto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200697 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900698
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900699 return 0;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900700
701err_filt:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200702 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200703 vlan_vid_del(p->dev, proto, vlan->vid);
Toshiaki Makita204177f2014-06-10 20:59:25 +0900704
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200705 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
706 vg = nbp_vlan_group(p);
707 list_for_each_entry(vlan, &vg->vlan_list, vlist)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200708 vlan_vid_del(p->dev, proto, vlan->vid);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200709 }
Toshiaki Makita204177f2014-06-10 20:59:25 +0900710
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900711 return err;
712}
713
714int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
715{
716 int err;
717
718 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
719 return -EPROTONOSUPPORT;
720
721 if (!rtnl_trylock())
722 return restart_syscall();
723
724 err = __br_vlan_set_proto(br, htons(val));
725 rtnl_unlock();
726
727 return err;
Toshiaki Makita204177f2014-06-10 20:59:25 +0900728}
729
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200730static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400731{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200732 struct net_bridge_vlan *v;
733
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200734 if (vid != vg->pvid)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200735 return false;
736
737 v = br_vlan_lookup(&vg->vlan_hash, vid);
738 if (v && br_vlan_should_use(v) &&
739 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
740 return true;
741
742 return false;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400743}
744
745static void br_vlan_disable_default_pvid(struct net_bridge *br)
746{
747 struct net_bridge_port *p;
748 u16 pvid = br->default_pvid;
749
750 /* Disable default_pvid on all ports where it is still
751 * configured.
752 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200753 if (vlan_default_pvid(br_vlan_group(br), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400754 br_vlan_delete(br, pvid);
755
756 list_for_each_entry(p, &br->port_list, list) {
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200757 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400758 nbp_vlan_delete(p, pvid);
759 }
760
761 br->default_pvid = 0;
762}
763
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200764int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400765{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200766 const struct net_bridge_vlan *pvent;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200767 struct net_bridge_vlan_group *vg;
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400768 struct net_bridge_port *p;
769 u16 old_pvid;
770 int err = 0;
771 unsigned long *changed;
772
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200773 if (!pvid) {
774 br_vlan_disable_default_pvid(br);
775 return 0;
776 }
777
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400778 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
779 GFP_KERNEL);
780 if (!changed)
781 return -ENOMEM;
782
783 old_pvid = br->default_pvid;
784
785 /* Update default_pvid config only if we do not conflict with
786 * user configuration.
787 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200788 vg = br_vlan_group(br);
789 pvent = br_vlan_find(vg, pvid);
790 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200791 (!pvent || !br_vlan_should_use(pvent))) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400792 err = br_vlan_add(br, pvid,
793 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200794 BRIDGE_VLAN_INFO_UNTAGGED |
795 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400796 if (err)
797 goto out;
798 br_vlan_delete(br, old_pvid);
799 set_bit(0, changed);
800 }
801
802 list_for_each_entry(p, &br->port_list, list) {
803 /* Update default_pvid config only if we do not conflict with
804 * user configuration.
805 */
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200806 vg = nbp_vlan_group(p);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400807 if ((old_pvid &&
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200808 !vlan_default_pvid(vg, old_pvid)) ||
809 br_vlan_find(vg, pvid))
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400810 continue;
811
812 err = nbp_vlan_add(p, pvid,
813 BRIDGE_VLAN_INFO_PVID |
814 BRIDGE_VLAN_INFO_UNTAGGED);
815 if (err)
816 goto err_port;
817 nbp_vlan_delete(p, old_pvid);
818 set_bit(p->port_no, changed);
819 }
820
821 br->default_pvid = pvid;
822
823out:
824 kfree(changed);
825 return err;
826
827err_port:
828 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
829 if (!test_bit(p->port_no, changed))
830 continue;
831
832 if (old_pvid)
833 nbp_vlan_add(p, old_pvid,
834 BRIDGE_VLAN_INFO_PVID |
835 BRIDGE_VLAN_INFO_UNTAGGED);
836 nbp_vlan_delete(p, pvid);
837 }
838
839 if (test_bit(0, changed)) {
840 if (old_pvid)
841 br_vlan_add(br, old_pvid,
842 BRIDGE_VLAN_INFO_PVID |
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200843 BRIDGE_VLAN_INFO_UNTAGGED |
844 BRIDGE_VLAN_INFO_BRENTRY);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400845 br_vlan_delete(br, pvid);
846 }
847 goto out;
848}
849
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400850int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
851{
852 u16 pvid = val;
853 int err = 0;
854
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400855 if (val >= VLAN_VID_MASK)
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400856 return -EINVAL;
857
858 if (!rtnl_trylock())
859 return restart_syscall();
860
861 if (pvid == br->default_pvid)
862 goto unlock;
863
864 /* Only allow default pvid change when filtering is disabled */
865 if (br->vlan_enabled) {
866 pr_info_once("Please disable vlan filtering to change default_pvid\n");
867 err = -EPERM;
868 goto unlock;
869 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200870 err = __br_vlan_set_default_pvid(br, pvid);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400871unlock:
872 rtnl_unlock();
873 return err;
874}
875
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400876int br_vlan_init(struct net_bridge *br)
Toshiaki Makita8580e212014-06-10 20:59:23 +0900877{
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200878 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200879 int ret = -ENOMEM;
880
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200881 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
882 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200883 goto out;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200884 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200885 if (ret)
886 goto err_rhtbl;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200887 INIT_LIST_HEAD(&vg->vlan_list);
Toshiaki Makita8580e212014-06-10 20:59:23 +0900888 br->vlan_proto = htons(ETH_P_8021Q);
Vlad Yasevich96a20d92014-10-03 11:29:16 -0400889 br->default_pvid = 1;
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200890 rcu_assign_pointer(br->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200891 ret = br_vlan_add(br, 1,
892 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
893 BRIDGE_VLAN_INFO_BRENTRY);
894 if (ret)
895 goto err_vlan_add;
896
897out:
898 return ret;
899
900err_vlan_add:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200901 rhashtable_destroy(&vg->vlan_hash);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200902err_rhtbl:
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200903 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200904
905 goto out;
906}
907
908int nbp_vlan_init(struct net_bridge_port *p)
909{
Elad Raz404cdbf2016-01-06 13:01:07 +0100910 struct switchdev_attr attr = {
911 .orig_dev = p->br->dev,
912 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
913 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
914 .u.vlan_filtering = p->br->vlan_enabled,
915 };
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200916 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200917 int ret = -ENOMEM;
918
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200919 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
920 if (!vg)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200921 goto out;
922
Elad Raz404cdbf2016-01-06 13:01:07 +0100923 ret = switchdev_port_attr_set(p->dev, &attr);
924 if (ret && ret != -EOPNOTSUPP)
925 goto err_vlan_enabled;
926
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200927 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200928 if (ret)
929 goto err_rhtbl;
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200930 INIT_LIST_HEAD(&vg->vlan_list);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200931 rcu_assign_pointer(p->vlgrp, vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200932 if (p->br->default_pvid) {
933 ret = nbp_vlan_add(p, p->br->default_pvid,
934 BRIDGE_VLAN_INFO_PVID |
935 BRIDGE_VLAN_INFO_UNTAGGED);
936 if (ret)
937 goto err_vlan_add;
938 }
939out:
940 return ret;
941
942err_vlan_add:
Ido Schimmel07bc5882015-10-30 17:46:19 +0100943 RCU_INIT_POINTER(p->vlgrp, NULL);
944 synchronize_rcu();
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200945 rhashtable_destroy(&vg->vlan_hash);
Elad Raz404cdbf2016-01-06 13:01:07 +0100946err_vlan_enabled:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200947err_rhtbl:
Nikolay Aleksandrov263344e2015-09-30 20:16:54 +0200948 kfree(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200949
950 goto out;
Toshiaki Makita8580e212014-06-10 20:59:23 +0900951}
952
Toshiaki Makita8adff412013-10-16 17:07:13 +0900953/* Must be protected by RTNL.
954 * Must be called with vid in range from 1 to 4094 inclusive.
955 */
Vlad Yasevich552406c2013-02-13 12:00:15 +0000956int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000957{
Ido Schimmel7fbac982016-02-18 14:01:46 +0100958 struct switchdev_obj_port_vlan v = {
959 .obj.orig_dev = port->dev,
960 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
961 .flags = flags,
962 .vid_begin = vid,
963 .vid_end = vid,
964 };
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200965 struct net_bridge_vlan *vlan;
966 int ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000967
968 ASSERT_RTNL();
969
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200970 vlan = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200971 if (vlan) {
Ido Schimmel7fbac982016-02-18 14:01:46 +0100972 /* Pass the flags to the hardware bridge */
973 ret = switchdev_port_obj_add(port->dev, &v.obj);
974 if (ret && ret != -EOPNOTSUPP)
975 return ret;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200976 __vlan_add_flags(vlan, flags);
977 return 0;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000978 }
979
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200980 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
981 if (!vlan)
982 return -ENOMEM;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000983
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200984 vlan->vid = vid;
985 vlan->port = port;
986 ret = __vlan_add(vlan, flags);
987 if (ret)
988 kfree(vlan);
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000989
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200990 return ret;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000991}
992
Toshiaki Makita8adff412013-10-16 17:07:13 +0900993/* Must be protected by RTNL.
994 * Must be called with vid in range from 1 to 4094 inclusive.
995 */
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000996int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
997{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200998 struct net_bridge_vlan *v;
Vlad Yasevich243a2e62013-02-13 12:00:09 +0000999
1000 ASSERT_RTNL();
1001
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +02001002 v = br_vlan_find(nbp_vlan_group(port), vid);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001003 if (!v)
1004 return -ENOENT;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +09001005 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -07001006 br_fdb_delete_by_port(port->br, port, vid, 0);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +00001007
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001008 return __vlan_del(v);
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001009}
1010
1011void nbp_vlan_flush(struct net_bridge_port *port)
1012{
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001013 struct net_bridge_vlan_group *vg;
1014
Vlad Yasevich243a2e62013-02-13 12:00:09 +00001015 ASSERT_RTNL();
1016
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +02001017 vg = nbp_vlan_group(port);
1018 __vlan_flush(vg);
1019 RCU_INIT_POINTER(port->vlgrp, NULL);
1020 synchronize_rcu();
1021 __vlan_group_free(vg);
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -04001022}