Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/netdevice.h> |
| 3 | #include <linux/rtnetlink.h> |
| 4 | #include <linux/slab.h> |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 5 | #include <net/switchdev.h> |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 6 | |
| 7 | #include "br_private.h" |
| 8 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 9 | static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg, |
| 10 | const void *ptr) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 11 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 12 | const struct net_bridge_vlan *vle = ptr; |
| 13 | u16 vid = *(u16 *)arg->key; |
| 14 | |
| 15 | return vle->vid != vid; |
| 16 | } |
| 17 | |
| 18 | static 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 Aleksandrov | 8af78b6 | 2015-09-30 20:16:51 +0200 | [diff] [blame] | 22 | .nelem_hint = 3, |
| 23 | .locks_mul = 1, |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 24 | .max_size = VLAN_N_VID, |
| 25 | .obj_cmpfn = br_vlan_cmp, |
| 26 | .automatic_shrinking = true, |
| 27 | }; |
| 28 | |
| 29 | static 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 Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 34 | static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 35 | { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 36 | if (vg->pvid == vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 37 | return; |
| 38 | |
| 39 | smp_wmb(); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 40 | vg->pvid = vid; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 43 | static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 44 | { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 45 | if (vg->pvid != vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 46 | return; |
| 47 | |
| 48 | smp_wmb(); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 49 | vg->pvid = 0; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 52 | static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags) |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 53 | { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 54 | struct net_bridge_vlan_group *vg; |
| 55 | |
| 56 | if (br_vlan_is_master(v)) |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 57 | vg = br_vlan_group(v->br); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 58 | else |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 59 | vg = nbp_vlan_group(v->port); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 60 | |
| 61 | if (flags & BRIDGE_VLAN_INFO_PVID) |
| 62 | __vlan_add_pvid(vg, v->vid); |
| 63 | else |
| 64 | __vlan_delete_pvid(vg, v->vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 65 | |
| 66 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 67 | v->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
Vlad Yasevich | 635126b | 2014-09-12 16:26:17 -0400 | [diff] [blame] | 68 | else |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 69 | v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED; |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 72 | static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, |
| 73 | u16 vid, u16 flags) |
| 74 | { |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 75 | struct switchdev_obj_port_vlan v = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 76 | .obj.orig_dev = dev, |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 77 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 78 | .flags = flags, |
| 79 | .vid_begin = vid, |
| 80 | .vid_end = vid, |
| 81 | }; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 82 | int err; |
| 83 | |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 84 | /* Try switchdev op first. In case it is not supported, fallback to |
| 85 | * 8021q add. |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 86 | */ |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 87 | err = switchdev_port_obj_add(dev, &v.obj); |
| 88 | if (err == -EOPNOTSUPP) |
| 89 | return vlan_vid_add(dev, br->vlan_proto, vid); |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 90 | return err; |
| 91 | } |
| 92 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 93 | static void __vlan_add_list(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 94 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 95 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 96 | struct list_head *headp, *hpos; |
| 97 | struct net_bridge_vlan *vent; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 98 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 99 | 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 105 | 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 Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 111 | } |
Nikolay Aleksandrov | 586c2b5 | 2015-10-02 15:05:10 +0200 | [diff] [blame] | 112 | list_add_rcu(&v->vlist, hpos); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 113 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 114 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 115 | static void __vlan_del_list(struct net_bridge_vlan *v) |
| 116 | { |
Nikolay Aleksandrov | 586c2b5 | 2015-10-02 15:05:10 +0200 | [diff] [blame] | 117 | list_del_rcu(&v->vlist); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 120 | static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, |
| 121 | u16 vid) |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 122 | { |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 123 | struct switchdev_obj_port_vlan v = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 124 | .obj.orig_dev = dev, |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 125 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 126 | .vid_begin = vid, |
| 127 | .vid_end = vid, |
| 128 | }; |
| 129 | int err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 130 | |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 131 | /* Try switchdev op first. In case it is not supported, fallback to |
| 132 | * 8021q del. |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 133 | */ |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 134 | err = switchdev_port_obj_del(dev, &v.obj); |
| 135 | if (err == -EOPNOTSUPP) { |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 136 | vlan_vid_del(dev, br->vlan_proto, vid); |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 137 | return 0; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 138 | } |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 139 | return err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 142 | /* 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 | */ |
| 145 | static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid) |
| 146 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 147 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 148 | struct net_bridge_vlan *masterv; |
| 149 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 150 | vg = br_vlan_group(br); |
| 151 | masterv = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 152 | if (!masterv) { |
| 153 | /* missing global ctx, create it now */ |
| 154 | if (br_vlan_add(br, vid, 0)) |
| 155 | return NULL; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 156 | masterv = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 157 | if (WARN_ON(!masterv)) |
| 158 | return NULL; |
| 159 | } |
| 160 | atomic_inc(&masterv->refcnt); |
| 161 | |
| 162 | return masterv; |
| 163 | } |
| 164 | |
| 165 | static void br_vlan_put_master(struct net_bridge_vlan *masterv) |
| 166 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 167 | struct net_bridge_vlan_group *vg; |
| 168 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 169 | if (!br_vlan_is_master(masterv)) |
| 170 | return; |
| 171 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 172 | vg = br_vlan_group(masterv->br); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 173 | if (atomic_dec_and_test(&masterv->refcnt)) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 174 | rhashtable_remove_fast(&vg->vlan_hash, |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 175 | &masterv->vnode, br_vlan_rht_params); |
| 176 | __vlan_del_list(masterv); |
| 177 | kfree_rcu(masterv, rcu); |
| 178 | } |
| 179 | } |
| 180 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 181 | /* 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 Schimmel | ddd611d | 2015-10-30 17:46:20 +0100 | [diff] [blame] | 185 | * 2. vlan is being added on a bridge (both master and brentry flags) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 186 | * 3. vlan is being added on a port, but a global entry didn't exist which |
Ido Schimmel | ddd611d | 2015-10-30 17:46:20 +0100 | [diff] [blame] | 187 | * is being created right now (master flag set, brentry flag unset), the |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 188 | * global entry is used for global per-vlan features, but not for filtering |
Ido Schimmel | ddd611d | 2015-10-30 17:46:20 +0100 | [diff] [blame] | 189 | * 4. same as 3 but with both master and brentry flags set so the entry |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 190 | * will be used for filtering in both the port and the bridge |
| 191 | */ |
| 192 | static int __vlan_add(struct net_bridge_vlan *v, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 193 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 194 | struct net_bridge_vlan *masterv = NULL; |
| 195 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 196 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 197 | struct net_device *dev; |
| 198 | struct net_bridge *br; |
| 199 | int err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 200 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 201 | if (br_vlan_is_master(v)) { |
| 202 | br = v->br; |
| 203 | dev = br->dev; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 204 | vg = br_vlan_group(br); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 205 | } else { |
| 206 | p = v->port; |
| 207 | br = p->br; |
| 208 | dev = p->dev; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 209 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 210 | } |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 211 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 212 | if (p) { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 213 | /* 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 Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 218 | if (err) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 219 | goto out; |
| 220 | |
| 221 | /* need to work on the master vlan too */ |
| 222 | if (flags & BRIDGE_VLAN_INFO_MASTER) { |
Nikolay Aleksandrov | 2ffdf508 | 2015-10-02 15:05:12 +0200 | [diff] [blame] | 223 | err = br_vlan_add(br, v->vid, flags | |
| 224 | BRIDGE_VLAN_INFO_BRENTRY); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 225 | if (err) |
| 226 | goto out_filt; |
| 227 | } |
| 228 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 229 | masterv = br_vlan_get_master(br, v->vid); |
| 230 | if (!masterv) |
| 231 | goto out_filt; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 232 | v->brvlan = masterv; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 233 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 234 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 235 | /* Add the dev mac and count the vlan only if it's usable */ |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 236 | 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 Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 242 | vg->num_vlans++; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 243 | } |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 244 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 245 | err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode, |
| 246 | br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 247 | if (err) |
| 248 | goto out_fdb_insert; |
| 249 | |
| 250 | __vlan_add_list(v); |
| 251 | __vlan_add_flags(v, flags); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 252 | out: |
| 253 | return err; |
| 254 | |
| 255 | out_fdb_insert: |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 256 | 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 260 | |
| 261 | out_filt: |
| 262 | if (p) { |
| 263 | __vlan_vid_del(dev, br, v->vid); |
| 264 | if (masterv) { |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 265 | br_vlan_put_master(masterv); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 266 | v->brvlan = NULL; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | goto out; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 273 | static int __vlan_del(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 274 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 275 | struct net_bridge_vlan *masterv = v; |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 276 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 277 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 278 | int err = 0; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 279 | |
| 280 | if (br_vlan_is_master(v)) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 281 | vg = br_vlan_group(v->br); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 282 | } else { |
| 283 | p = v->port; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 284 | vg = nbp_vlan_group(v->port); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 285 | masterv = v->brvlan; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 288 | __vlan_delete_pvid(vg, v->vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 289 | if (p) { |
| 290 | err = __vlan_vid_del(p->dev, p->br, v->vid); |
| 291 | if (err) |
| 292 | goto out; |
| 293 | } |
| 294 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 295 | if (br_vlan_should_use(v)) { |
| 296 | v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY; |
| 297 | vg->num_vlans--; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (masterv != v) { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 301 | rhashtable_remove_fast(&vg->vlan_hash, &v->vnode, |
| 302 | br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 303 | __vlan_del_list(v); |
| 304 | kfree_rcu(v, rcu); |
| 305 | } |
| 306 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 307 | br_vlan_put_master(masterv); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 308 | out: |
| 309 | return err; |
| 310 | } |
| 311 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 312 | static 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 | |
| 319 | static void __vlan_flush(struct net_bridge_vlan_group *vg) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 320 | { |
| 321 | struct net_bridge_vlan *vlan, *tmp; |
| 322 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 323 | __vlan_delete_pvid(vg, vg->pvid); |
| 324 | list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 325 | __vlan_del(vlan); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 328 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 329 | struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 330 | struct sk_buff *skb) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 331 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 332 | struct net_bridge_vlan *v; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 333 | u16 vid; |
| 334 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 335 | /* If this packet was not filtered at input, let it pass */ |
| 336 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 337 | goto out; |
| 338 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 339 | /* 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 Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 346 | * 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 350 | if (!v || !br_vlan_should_use(v)) { |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 351 | 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 358 | if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Toshiaki Makita | 99b192d | 2014-03-27 21:46:56 +0900 | [diff] [blame] | 359 | skb->vlan_tci = 0; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 360 | |
| 361 | out: |
| 362 | return skb; |
| 363 | } |
| 364 | |
| 365 | /* Called under RCU */ |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 366 | static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto, |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 367 | struct sk_buff *skb, u16 *vid) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 368 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 369 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 370 | bool tagged; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 371 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 372 | BR_INPUT_SKB_CB(skb)->vlan_filtered = true; |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 373 | /* 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 Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 377 | if (unlikely(!skb_vlan_tag_present(skb) && |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 378 | skb->protocol == proto)) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 379 | skb = skb_vlan_untag(skb); |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 380 | if (unlikely(!skb)) |
| 381 | return false; |
| 382 | } |
| 383 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 384 | 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 Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 389 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 390 | skb_vlan_tag_get(skb)); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 391 | 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 Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 406 | if (!*vid) { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 407 | u16 pvid = br_get_pvid(vg); |
| 408 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 409 | /* 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 Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 412 | */ |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 413 | if (!pvid) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 414 | goto drop; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 415 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 416 | /* PVID is set on this port. Any untagged or priority-tagged |
| 417 | * ingress frame is considered to belong to this vlan. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 418 | */ |
Toshiaki Makita | dfb5fa3 | 2013-10-16 17:07:16 +0900 | [diff] [blame] | 419 | *vid = pvid; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 420 | if (likely(!tagged)) |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 421 | /* Untagged Frame. */ |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 422 | __vlan_hwaccel_put_tag(skb, proto, pvid); |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 423 | 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 Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | |
| 434 | /* Frame had a valid vlan tag. See if vlan is allowed */ |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 435 | v = br_vlan_find(vg, *vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 436 | if (v && br_vlan_should_use(v)) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 437 | return true; |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 438 | drop: |
| 439 | kfree_skb(skb); |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 440 | return false; |
| 441 | } |
| 442 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 443 | bool br_allowed_ingress(const struct net_bridge *br, |
| 444 | struct net_bridge_vlan_group *vg, struct sk_buff *skb, |
| 445 | u16 *vid) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 446 | { |
| 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 Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 455 | return __allowed_ingress(vg, br->vlan_proto, skb, vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 458 | /* Called under RCU. */ |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 459 | bool br_allowed_egress(struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 460 | const struct sk_buff *skb) |
| 461 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 462 | const struct net_bridge_vlan *v; |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 463 | u16 vid; |
| 464 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 465 | /* If this packet was not filtered at input, let it pass */ |
| 466 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 467 | return true; |
| 468 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 469 | br_vlan_get_tag(skb, &vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 470 | v = br_vlan_find(vg, vid); |
| 471 | if (v && br_vlan_should_use(v)) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 472 | return true; |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 477 | /* Called under RCU */ |
| 478 | bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) |
| 479 | { |
Nikolay Aleksandrov | 468e794 | 2015-09-30 20:16:52 +0200 | [diff] [blame] | 480 | struct net_bridge_vlan_group *vg; |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 481 | struct net_bridge *br = p->br; |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 482 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 483 | /* If filtering was disabled at input, let it pass. */ |
Vlad Yasevich | c095f24 | 2014-09-15 15:24:26 -0400 | [diff] [blame] | 484 | if (!br->vlan_enabled) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 485 | return true; |
| 486 | |
Ido Schimmel | eca1e00 | 2015-11-01 18:31:45 +0200 | [diff] [blame] | 487 | vg = nbp_vlan_group_rcu(p); |
Nikolay Aleksandrov | 468e794 | 2015-09-30 20:16:52 +0200 | [diff] [blame] | 488 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 489 | return false; |
| 490 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 491 | if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) |
| 492 | *vid = 0; |
| 493 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 494 | if (!*vid) { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 495 | *vid = br_get_pvid(vg); |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 496 | if (!*vid) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 497 | return false; |
| 498 | |
| 499 | return true; |
| 500 | } |
| 501 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 502 | if (br_vlan_find(vg, *vid)) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 503 | return true; |
| 504 | |
| 505 | return false; |
| 506 | } |
| 507 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 508 | /* Must be protected by RTNL. |
| 509 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 510 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 511 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 512 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 513 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 514 | struct net_bridge_vlan *vlan; |
| 515 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 516 | |
| 517 | ASSERT_RTNL(); |
| 518 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 519 | vg = br_vlan_group(br); |
| 520 | vlan = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 521 | 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 Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 535 | vg->num_vlans++; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 536 | } |
| 537 | __vlan_add_flags(vlan, flags); |
| 538 | return 0; |
| 539 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 540 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 541 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 542 | if (!vlan) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 543 | return -ENOMEM; |
| 544 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 545 | 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 Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 554 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 555 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 558 | /* Must be protected by RTNL. |
| 559 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 560 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 561 | int br_vlan_delete(struct net_bridge *br, u16 vid) |
| 562 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 563 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 564 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 565 | |
| 566 | ASSERT_RTNL(); |
| 567 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 568 | vg = br_vlan_group(br); |
| 569 | v = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 570 | if (!v || !br_vlan_is_brentry(v)) |
| 571 | return -ENOENT; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 572 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 573 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 574 | br_fdb_delete_by_port(br, NULL, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 575 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 576 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void br_vlan_flush(struct net_bridge *br) |
| 580 | { |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 581 | struct net_bridge_vlan_group *vg; |
| 582 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 583 | ASSERT_RTNL(); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 584 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 585 | 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 Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 592 | struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid) |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 593 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 594 | if (!vg) |
| 595 | return NULL; |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 596 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 597 | return br_vlan_lookup(&vg->vlan_hash, vid); |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 598 | } |
| 599 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 600 | /* Must be protected by RTNL. */ |
| 601 | static 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. */ |
| 618 | void 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 Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 627 | int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 628 | { |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 629 | 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 Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 637 | if (br->vlan_enabled == val) |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 638 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 639 | |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 640 | err = switchdev_port_attr_set(br->dev, &attr); |
| 641 | if (err && err != -EOPNOTSUPP) |
| 642 | return err; |
| 643 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 644 | br->vlan_enabled = val; |
Vlad Yasevich | 2796d0c | 2014-05-16 09:59:20 -0400 | [diff] [blame] | 645 | br_manage_promisc(br); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 646 | recalculate_group_addr(br); |
| 647 | br_recalculate_fwd_mask(br); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 648 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
| 653 | { |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 654 | int err; |
| 655 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 656 | if (!rtnl_trylock()) |
| 657 | return restart_syscall(); |
| 658 | |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 659 | err = __br_vlan_filter_toggle(br, val); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 660 | rtnl_unlock(); |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 661 | |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 662 | return err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 665 | int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 666 | { |
| 667 | int err = 0; |
| 668 | struct net_bridge_port *p; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 669 | struct net_bridge_vlan *vlan; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 670 | struct net_bridge_vlan_group *vg; |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 671 | __be16 oldproto; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 672 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 673 | if (br->vlan_proto == proto) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 674 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 675 | |
| 676 | /* Add VLANs for the new proto to the device filter. */ |
| 677 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 678 | vg = nbp_vlan_group(p); |
| 679 | list_for_each_entry(vlan, &vg->vlan_list, vlist) { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 680 | err = vlan_vid_add(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 681 | 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 Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 693 | 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 696 | vlan_vid_del(p->dev, oldproto, vlan->vid); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 697 | } |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 698 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 699 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 700 | |
| 701 | err_filt: |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 702 | list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 703 | vlan_vid_del(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 704 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 705 | 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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 708 | vlan_vid_del(p->dev, proto, vlan->vid); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 709 | } |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 710 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 711 | return err; |
| 712 | } |
| 713 | |
| 714 | int 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 Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 728 | } |
| 729 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 730 | static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 731 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 732 | struct net_bridge_vlan *v; |
| 733 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 734 | if (vid != vg->pvid) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 735 | 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 Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static 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 Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 753 | if (vlan_default_pvid(br_vlan_group(br), pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 754 | br_vlan_delete(br, pvid); |
| 755 | |
| 756 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 757 | if (vlan_default_pvid(nbp_vlan_group(p), pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 758 | nbp_vlan_delete(p, pvid); |
| 759 | } |
| 760 | |
| 761 | br->default_pvid = 0; |
| 762 | } |
| 763 | |
Nikolay Aleksandrov | 0f963b7 | 2015-10-04 14:23:47 +0200 | [diff] [blame] | 764 | int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 765 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 766 | const struct net_bridge_vlan *pvent; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 767 | struct net_bridge_vlan_group *vg; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 768 | struct net_bridge_port *p; |
| 769 | u16 old_pvid; |
| 770 | int err = 0; |
| 771 | unsigned long *changed; |
| 772 | |
Nikolay Aleksandrov | 0f963b7 | 2015-10-04 14:23:47 +0200 | [diff] [blame] | 773 | if (!pvid) { |
| 774 | br_vlan_disable_default_pvid(br); |
| 775 | return 0; |
| 776 | } |
| 777 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 778 | 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 Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 788 | vg = br_vlan_group(br); |
| 789 | pvent = br_vlan_find(vg, pvid); |
| 790 | if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) && |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 791 | (!pvent || !br_vlan_should_use(pvent))) { |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 792 | err = br_vlan_add(br, pvid, |
| 793 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 794 | BRIDGE_VLAN_INFO_UNTAGGED | |
| 795 | BRIDGE_VLAN_INFO_BRENTRY); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 796 | 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 Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 806 | vg = nbp_vlan_group(p); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 807 | if ((old_pvid && |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 808 | !vlan_default_pvid(vg, old_pvid)) || |
| 809 | br_vlan_find(vg, pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 810 | 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 | |
| 823 | out: |
| 824 | kfree(changed); |
| 825 | return err; |
| 826 | |
| 827 | err_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 Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 843 | BRIDGE_VLAN_INFO_UNTAGGED | |
| 844 | BRIDGE_VLAN_INFO_BRENTRY); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 845 | br_vlan_delete(br, pvid); |
| 846 | } |
| 847 | goto out; |
| 848 | } |
| 849 | |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 850 | int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) |
| 851 | { |
| 852 | u16 pvid = val; |
| 853 | int err = 0; |
| 854 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 855 | if (val >= VLAN_VID_MASK) |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 856 | 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 Aleksandrov | 0f963b7 | 2015-10-04 14:23:47 +0200 | [diff] [blame] | 870 | err = __br_vlan_set_default_pvid(br, pvid); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 871 | unlock: |
| 872 | rtnl_unlock(); |
| 873 | return err; |
| 874 | } |
| 875 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 876 | int br_vlan_init(struct net_bridge *br) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 877 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 878 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 879 | int ret = -ENOMEM; |
| 880 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 881 | vg = kzalloc(sizeof(*vg), GFP_KERNEL); |
| 882 | if (!vg) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 883 | goto out; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 884 | ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 885 | if (ret) |
| 886 | goto err_rhtbl; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 887 | INIT_LIST_HEAD(&vg->vlan_list); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 888 | br->vlan_proto = htons(ETH_P_8021Q); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 889 | br->default_pvid = 1; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 890 | rcu_assign_pointer(br->vlgrp, vg); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 891 | 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 | |
| 897 | out: |
| 898 | return ret; |
| 899 | |
| 900 | err_vlan_add: |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 901 | rhashtable_destroy(&vg->vlan_hash); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 902 | err_rhtbl: |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 903 | kfree(vg); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 904 | |
| 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | int nbp_vlan_init(struct net_bridge_port *p) |
| 909 | { |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 910 | 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 Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 916 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 917 | int ret = -ENOMEM; |
| 918 | |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 919 | vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL); |
| 920 | if (!vg) |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 921 | goto out; |
| 922 | |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 923 | ret = switchdev_port_attr_set(p->dev, &attr); |
| 924 | if (ret && ret != -EOPNOTSUPP) |
| 925 | goto err_vlan_enabled; |
| 926 | |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 927 | ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 928 | if (ret) |
| 929 | goto err_rhtbl; |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 930 | INIT_LIST_HEAD(&vg->vlan_list); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 931 | rcu_assign_pointer(p->vlgrp, vg); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 932 | 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 | } |
| 939 | out: |
| 940 | return ret; |
| 941 | |
| 942 | err_vlan_add: |
Ido Schimmel | 07bc588 | 2015-10-30 17:46:19 +0100 | [diff] [blame] | 943 | RCU_INIT_POINTER(p->vlgrp, NULL); |
| 944 | synchronize_rcu(); |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 945 | rhashtable_destroy(&vg->vlan_hash); |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 946 | err_vlan_enabled: |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 947 | err_rhtbl: |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 948 | kfree(vg); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 949 | |
| 950 | goto out; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 951 | } |
| 952 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 953 | /* Must be protected by RTNL. |
| 954 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 955 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 956 | int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 957 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 958 | struct net_bridge_vlan *vlan; |
| 959 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 960 | |
| 961 | ASSERT_RTNL(); |
| 962 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 963 | vlan = br_vlan_find(nbp_vlan_group(port), vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 964 | if (vlan) { |
| 965 | __vlan_add_flags(vlan, flags); |
| 966 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 969 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 970 | if (!vlan) |
| 971 | return -ENOMEM; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 972 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 973 | vlan->vid = vid; |
| 974 | vlan->port = port; |
| 975 | ret = __vlan_add(vlan, flags); |
| 976 | if (ret) |
| 977 | kfree(vlan); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 978 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 979 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 982 | /* Must be protected by RTNL. |
| 983 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 984 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 985 | int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) |
| 986 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 987 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 988 | |
| 989 | ASSERT_RTNL(); |
| 990 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 991 | v = br_vlan_find(nbp_vlan_group(port), vid); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 992 | if (!v) |
| 993 | return -ENOENT; |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 994 | br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid); |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 995 | br_fdb_delete_by_port(port->br, port, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 996 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 997 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | void nbp_vlan_flush(struct net_bridge_port *port) |
| 1001 | { |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 1002 | struct net_bridge_vlan_group *vg; |
| 1003 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1004 | ASSERT_RTNL(); |
| 1005 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 1006 | vg = nbp_vlan_group(port); |
| 1007 | __vlan_flush(vg); |
| 1008 | RCU_INIT_POINTER(port->vlgrp, NULL); |
| 1009 | synchronize_rcu(); |
| 1010 | __vlan_group_free(vg); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1011 | } |