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 | 2594e906 | 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 | 2594e906 | 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), |
| 22 | .max_size = VLAN_N_VID, |
| 23 | .obj_cmpfn = br_vlan_cmp, |
| 24 | .automatic_shrinking = true, |
| 25 | }; |
| 26 | |
| 27 | static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid) |
| 28 | { |
| 29 | return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params); |
| 30 | } |
| 31 | |
| 32 | static void __vlan_add_pvid(u16 *pvid, u16 vid) |
| 33 | { |
| 34 | if (*pvid == vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 35 | return; |
| 36 | |
| 37 | smp_wmb(); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 38 | *pvid = vid; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 41 | static void __vlan_delete_pvid(u16 *pvid, u16 vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 42 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 43 | if (*pvid != vid) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 44 | return; |
| 45 | |
| 46 | smp_wmb(); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 47 | *pvid = 0; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 50 | static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags) |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 51 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 52 | if (flags & BRIDGE_VLAN_INFO_PVID) { |
| 53 | if (br_vlan_is_master(v)) |
| 54 | __vlan_add_pvid(&v->br->pvid, v->vid); |
| 55 | else |
| 56 | __vlan_add_pvid(&v->port->pvid, v->vid); |
| 57 | } else { |
| 58 | if (br_vlan_is_master(v)) |
| 59 | __vlan_delete_pvid(&v->br->pvid, v->vid); |
| 60 | else |
| 61 | __vlan_delete_pvid(&v->port->pvid, v->vid); |
| 62 | } |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 63 | |
| 64 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 65 | v->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
Vlad Yasevich | 635126b | 2014-09-12 16:26:17 -0400 | [diff] [blame] | 66 | else |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 67 | v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED; |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 70 | static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, |
| 71 | u16 vid, u16 flags) |
| 72 | { |
| 73 | const struct net_device_ops *ops = dev->netdev_ops; |
| 74 | int err; |
| 75 | |
| 76 | /* If driver uses VLAN ndo ops, use 8021q to install vid |
| 77 | * on device, otherwise try switchdev ops to install vid. |
| 78 | */ |
| 79 | |
| 80 | if (ops->ndo_vlan_rx_add_vid) { |
| 81 | err = vlan_vid_add(dev, br->vlan_proto, vid); |
| 82 | } else { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame^] | 83 | struct switchdev_obj_vlan v = { |
| 84 | .flags = flags, |
| 85 | .vid_begin = vid, |
| 86 | .vid_end = vid, |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame^] | 89 | err = switchdev_port_obj_add(dev, SWITCHDEV_OBJ_PORT_VLAN, &v); |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 90 | if (err == -EOPNOTSUPP) |
| 91 | err = 0; |
| 92 | } |
| 93 | |
| 94 | return err; |
| 95 | } |
| 96 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 97 | static void __vlan_add_list(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 98 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 99 | struct list_head *headp, *hpos; |
| 100 | struct net_bridge_vlan *vent; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 101 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 102 | headp = br_vlan_is_master(v) ? &v->br->vlgrp->vlan_list : |
| 103 | &v->port->vlgrp->vlan_list; |
| 104 | list_for_each_prev(hpos, headp) { |
| 105 | vent = list_entry(hpos, struct net_bridge_vlan, vlist); |
| 106 | if (v->vid < vent->vid) |
| 107 | continue; |
| 108 | else |
| 109 | break; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 110 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 111 | list_add(&v->vlist, hpos); |
| 112 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 113 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 114 | static void __vlan_del_list(struct net_bridge_vlan *v) |
| 115 | { |
| 116 | list_del(&v->vlist); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 119 | static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, |
| 120 | u16 vid) |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 121 | { |
| 122 | const struct net_device_ops *ops = dev->netdev_ops; |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 123 | int err = 0; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 124 | |
| 125 | /* If driver uses VLAN ndo ops, use 8021q to delete vid |
| 126 | * on device, otherwise try switchdev ops to delete vid. |
| 127 | */ |
| 128 | |
| 129 | if (ops->ndo_vlan_rx_kill_vid) { |
| 130 | vlan_vid_del(dev, br->vlan_proto, vid); |
| 131 | } else { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame^] | 132 | struct switchdev_obj_vlan v = { |
| 133 | .vid_begin = vid, |
| 134 | .vid_end = vid, |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame^] | 137 | err = switchdev_port_obj_del(dev, SWITCHDEV_OBJ_PORT_VLAN, &v); |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 138 | if (err == -EOPNOTSUPP) |
| 139 | err = 0; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 140 | } |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 141 | |
| 142 | return err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 145 | /* This is the shared VLAN add function which works for both ports and bridge |
| 146 | * devices. There are four possible calls to this function in terms of the |
| 147 | * vlan entry type: |
| 148 | * 1. vlan is being added on a port (no master flags, global entry exists) |
| 149 | * 2. vlan is being added on a bridge (both master and brvlan flags) |
| 150 | * 3. vlan is being added on a port, but a global entry didn't exist which |
| 151 | * is being created right now (master flag set, brvlan flag unset), the |
| 152 | * global entry is used for global per-vlan features, but not for filtering |
| 153 | * 4. same as 3 but with both master and brvlan flags set so the entry |
| 154 | * will be used for filtering in both the port and the bridge |
| 155 | */ |
| 156 | static int __vlan_add(struct net_bridge_vlan *v, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 157 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 158 | struct net_bridge_vlan *masterv = NULL; |
| 159 | struct net_bridge_port *p = NULL; |
| 160 | struct rhashtable *tbl; |
| 161 | struct net_device *dev; |
| 162 | struct net_bridge *br; |
| 163 | int err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 164 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 165 | if (br_vlan_is_master(v)) { |
| 166 | br = v->br; |
| 167 | dev = br->dev; |
| 168 | tbl = &br->vlgrp->vlan_hash; |
| 169 | } else { |
| 170 | p = v->port; |
| 171 | br = p->br; |
| 172 | dev = p->dev; |
| 173 | tbl = &p->vlgrp->vlan_hash; |
| 174 | } |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 175 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 176 | if (p) { |
| 177 | u16 master_flags = flags; |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 178 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 179 | /* Add VLAN to the device filter if it is supported. |
| 180 | * This ensures tagged traffic enters the bridge when |
| 181 | * promiscuous mode is disabled by br_manage_promisc(). |
| 182 | */ |
| 183 | err = __vlan_vid_add(dev, br, v->vid, flags); |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 184 | if (err) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 185 | goto out; |
| 186 | |
| 187 | /* need to work on the master vlan too */ |
| 188 | if (flags & BRIDGE_VLAN_INFO_MASTER) { |
| 189 | master_flags |= BRIDGE_VLAN_INFO_BRENTRY; |
| 190 | err = br_vlan_add(br, v->vid, master_flags); |
| 191 | if (err) |
| 192 | goto out_filt; |
| 193 | } |
| 194 | |
| 195 | masterv = br_vlan_find(br->vlgrp, v->vid); |
| 196 | if (!masterv) { |
| 197 | /* missing global ctx, create it now */ |
| 198 | err = br_vlan_add(br, v->vid, master_flags); |
| 199 | if (err) |
| 200 | goto out_filt; |
| 201 | masterv = br_vlan_find(br->vlgrp, v->vid); |
| 202 | WARN_ON(!masterv); |
| 203 | } |
| 204 | atomic_inc(&masterv->refcnt); |
| 205 | v->brvlan = masterv; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 206 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 207 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 208 | /* Add the dev mac only if it's a usable vlan */ |
| 209 | if (br_vlan_should_use(v)) { |
| 210 | err = br_fdb_insert(br, p, dev->dev_addr, v->vid); |
| 211 | if (err) { |
| 212 | br_err(br, "failed insert local address into bridge forwarding table\n"); |
| 213 | goto out_filt; |
| 214 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 215 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 216 | |
| 217 | err = rhashtable_lookup_insert_fast(tbl, &v->vnode, br_vlan_rht_params); |
| 218 | if (err) |
| 219 | goto out_fdb_insert; |
| 220 | |
| 221 | __vlan_add_list(v); |
| 222 | __vlan_add_flags(v, flags); |
| 223 | if (br_vlan_is_master(v)) { |
| 224 | if (br_vlan_is_brentry(v)) |
| 225 | br->vlgrp->num_vlans++; |
| 226 | } else { |
| 227 | p->vlgrp->num_vlans++; |
| 228 | } |
| 229 | out: |
| 230 | return err; |
| 231 | |
| 232 | out_fdb_insert: |
| 233 | br_fdb_find_delete_local(br, p, br->dev->dev_addr, v->vid); |
| 234 | |
| 235 | out_filt: |
| 236 | if (p) { |
| 237 | __vlan_vid_del(dev, br, v->vid); |
| 238 | if (masterv) { |
| 239 | atomic_dec(&masterv->refcnt); |
| 240 | v->brvlan = NULL; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | goto out; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 247 | static int __vlan_del(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 248 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 249 | struct net_bridge_vlan *masterv = v; |
| 250 | struct net_bridge_port *p = NULL; |
| 251 | struct net_bridge *br; |
| 252 | int err = 0; |
| 253 | struct rhashtable *tbl; |
| 254 | u16 *pvid; |
| 255 | |
| 256 | if (br_vlan_is_master(v)) { |
| 257 | br = v->br; |
| 258 | tbl = &v->br->vlgrp->vlan_hash; |
| 259 | pvid = &v->br->pvid; |
| 260 | } else { |
| 261 | p = v->port; |
| 262 | br = p->br; |
| 263 | tbl = &p->vlgrp->vlan_hash; |
| 264 | masterv = v->brvlan; |
| 265 | pvid = &p->pvid; |
| 266 | } |
| 267 | |
| 268 | __vlan_delete_pvid(pvid, v->vid); |
| 269 | if (p) { |
| 270 | err = __vlan_vid_del(p->dev, p->br, v->vid); |
| 271 | if (err) |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | if (br_vlan_is_master(v)) { |
| 276 | if (br_vlan_is_brentry(v)) { |
| 277 | v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY; |
| 278 | br->vlgrp->num_vlans--; |
| 279 | } |
| 280 | } else { |
| 281 | p->vlgrp->num_vlans--; |
| 282 | } |
| 283 | |
| 284 | if (masterv != v) { |
| 285 | rhashtable_remove_fast(tbl, &v->vnode, br_vlan_rht_params); |
| 286 | __vlan_del_list(v); |
| 287 | kfree_rcu(v, rcu); |
| 288 | } |
| 289 | |
| 290 | if (atomic_dec_and_test(&masterv->refcnt)) { |
| 291 | rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash, |
| 292 | &masterv->vnode, br_vlan_rht_params); |
| 293 | __vlan_del_list(masterv); |
| 294 | kfree_rcu(masterv, rcu); |
| 295 | } |
| 296 | out: |
| 297 | return err; |
| 298 | } |
| 299 | |
| 300 | static void __vlan_flush(struct net_bridge_vlan_group *vlgrp, u16 *pvid) |
| 301 | { |
| 302 | struct net_bridge_vlan *vlan, *tmp; |
| 303 | |
| 304 | __vlan_delete_pvid(pvid, *pvid); |
| 305 | list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist) |
| 306 | __vlan_del(vlan); |
| 307 | rhashtable_destroy(&vlgrp->vlan_hash); |
| 308 | kfree(vlgrp); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 311 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 312 | struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 313 | struct sk_buff *skb) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 314 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 315 | struct net_bridge_vlan *v; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 316 | u16 vid; |
| 317 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 318 | /* If this packet was not filtered at input, let it pass */ |
| 319 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 320 | goto out; |
| 321 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 322 | /* At this point, we know that the frame was filtered and contains |
| 323 | * a valid vlan id. If the vlan id has untagged flag set, |
| 324 | * send untagged; otherwise, send tagged. |
| 325 | */ |
| 326 | br_vlan_get_tag(skb, &vid); |
| 327 | v = br_vlan_find(vg, vid); |
| 328 | /* Vlan entry must be configured at this point. The |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 329 | * only exception is the bridge is set in promisc mode and the |
| 330 | * packet is destined for the bridge device. In this case |
| 331 | * pass the packet as is. |
| 332 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 333 | if (!v || !br_vlan_should_use(v)) { |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 334 | if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { |
| 335 | goto out; |
| 336 | } else { |
| 337 | kfree_skb(skb); |
| 338 | return NULL; |
| 339 | } |
| 340 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 341 | if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Toshiaki Makita | 99b192d | 2014-03-27 21:46:56 +0900 | [diff] [blame] | 342 | skb->vlan_tci = 0; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 343 | |
| 344 | out: |
| 345 | return skb; |
| 346 | } |
| 347 | |
| 348 | /* Called under RCU */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 349 | static bool __allowed_ingress(struct rhashtable *tbl, u16 pvid, __be16 proto, |
| 350 | struct sk_buff *skb, u16 *vid) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 351 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 352 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 353 | bool tagged; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 354 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 355 | BR_INPUT_SKB_CB(skb)->vlan_filtered = true; |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 356 | /* If vlan tx offload is disabled on bridge device and frame was |
| 357 | * sent from vlan device on the bridge device, it does not have |
| 358 | * HW accelerated vlan tag. |
| 359 | */ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 360 | if (unlikely(!skb_vlan_tag_present(skb) && |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 361 | skb->protocol == proto)) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 362 | skb = skb_vlan_untag(skb); |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 363 | if (unlikely(!skb)) |
| 364 | return false; |
| 365 | } |
| 366 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 367 | if (!br_vlan_get_tag(skb, vid)) { |
| 368 | /* Tagged frame */ |
| 369 | if (skb->vlan_proto != proto) { |
| 370 | /* Protocol-mismatch, empty out vlan_tci for new tag */ |
| 371 | skb_push(skb, ETH_HLEN); |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 372 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 373 | skb_vlan_tag_get(skb)); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 374 | if (unlikely(!skb)) |
| 375 | return false; |
| 376 | |
| 377 | skb_pull(skb, ETH_HLEN); |
| 378 | skb_reset_mac_len(skb); |
| 379 | *vid = 0; |
| 380 | tagged = false; |
| 381 | } else { |
| 382 | tagged = true; |
| 383 | } |
| 384 | } else { |
| 385 | /* Untagged frame */ |
| 386 | tagged = false; |
| 387 | } |
| 388 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 389 | if (!*vid) { |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 390 | /* Frame had a tag with VID 0 or did not have a tag. |
| 391 | * See if pvid is set on this port. That tells us which |
| 392 | * vlan untagged or priority-tagged traffic belongs to. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 393 | */ |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 394 | if (!pvid) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 395 | goto drop; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 396 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 397 | /* PVID is set on this port. Any untagged or priority-tagged |
| 398 | * ingress frame is considered to belong to this vlan. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 399 | */ |
Toshiaki Makita | dfb5fa3 | 2013-10-16 17:07:16 +0900 | [diff] [blame] | 400 | *vid = pvid; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 401 | if (likely(!tagged)) |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 402 | /* Untagged Frame. */ |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 403 | __vlan_hwaccel_put_tag(skb, proto, pvid); |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 404 | else |
| 405 | /* Priority-tagged Frame. |
| 406 | * At this point, We know that skb->vlan_tci had |
| 407 | * VLAN_TAG_PRESENT bit and its VID field was 0x000. |
| 408 | * We update only VID field and preserve PCP field. |
| 409 | */ |
| 410 | skb->vlan_tci |= pvid; |
| 411 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
| 415 | /* Frame had a valid vlan tag. See if vlan is allowed */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 416 | v = br_vlan_lookup(tbl, *vid); |
| 417 | if (v && br_vlan_should_use(v)) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 418 | return true; |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 419 | drop: |
| 420 | kfree_skb(skb); |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 424 | bool br_allowed_ingress(struct net_bridge *br, struct sk_buff *skb, u16 *vid) |
| 425 | { |
| 426 | /* If VLAN filtering is disabled on the bridge, all packets are |
| 427 | * permitted. |
| 428 | */ |
| 429 | if (!br->vlan_enabled) { |
| 430 | BR_INPUT_SKB_CB(skb)->vlan_filtered = false; |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | return __allowed_ingress(&br->vlgrp->vlan_hash, br->pvid, |
| 435 | br->vlan_proto, skb, vid); |
| 436 | } |
| 437 | |
| 438 | bool nbp_allowed_ingress(struct net_bridge_port *p, struct sk_buff *skb, |
| 439 | u16 *vid) |
| 440 | { |
| 441 | struct net_bridge *br = p->br; |
| 442 | |
| 443 | /* If VLAN filtering is disabled on the bridge, all packets are |
| 444 | * permitted. |
| 445 | */ |
| 446 | if (!br->vlan_enabled) { |
| 447 | BR_INPUT_SKB_CB(skb)->vlan_filtered = false; |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | return __allowed_ingress(&p->vlgrp->vlan_hash, p->pvid, br->vlan_proto, |
| 452 | skb, vid); |
| 453 | } |
| 454 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 455 | /* Called under RCU. */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 456 | bool br_allowed_egress(struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 457 | const struct sk_buff *skb) |
| 458 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 459 | const struct net_bridge_vlan *v; |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 460 | u16 vid; |
| 461 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 462 | /* If this packet was not filtered at input, let it pass */ |
| 463 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 464 | return true; |
| 465 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 466 | br_vlan_get_tag(skb, &vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 467 | v = br_vlan_find(vg, vid); |
| 468 | if (v && br_vlan_should_use(v)) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 469 | return true; |
| 470 | |
| 471 | return false; |
| 472 | } |
| 473 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 474 | /* Called under RCU */ |
| 475 | bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) |
| 476 | { |
| 477 | struct net_bridge *br = p->br; |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 478 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 479 | /* If filtering was disabled at input, let it pass. */ |
Vlad Yasevich | c095f24 | 2014-09-15 15:24:26 -0400 | [diff] [blame] | 480 | if (!br->vlan_enabled) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 481 | return true; |
| 482 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 483 | if (!p->vlgrp->num_vlans) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 484 | return false; |
| 485 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 486 | if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) |
| 487 | *vid = 0; |
| 488 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 489 | if (!*vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 490 | *vid = nbp_get_pvid(p); |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 491 | if (!*vid) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 492 | return false; |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 497 | if (br_vlan_find(p->vlgrp, *vid)) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 498 | return true; |
| 499 | |
| 500 | return false; |
| 501 | } |
| 502 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 503 | /* Must be protected by RTNL. |
| 504 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 505 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 506 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 507 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 508 | struct net_bridge_vlan *vlan; |
| 509 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 510 | |
| 511 | ASSERT_RTNL(); |
| 512 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 513 | vlan = br_vlan_find(br->vlgrp, vid); |
| 514 | if (vlan) { |
| 515 | if (!br_vlan_is_brentry(vlan)) { |
| 516 | /* Trying to change flags of non-existent bridge vlan */ |
| 517 | if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) |
| 518 | return -EINVAL; |
| 519 | /* It was only kept for port vlans, now make it real */ |
| 520 | ret = br_fdb_insert(br, NULL, br->dev->dev_addr, |
| 521 | vlan->vid); |
| 522 | if (ret) { |
| 523 | br_err(br, "failed insert local address into bridge forwarding table\n"); |
| 524 | return ret; |
| 525 | } |
| 526 | atomic_inc(&vlan->refcnt); |
| 527 | vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY; |
| 528 | br->vlgrp->num_vlans++; |
| 529 | } |
| 530 | __vlan_add_flags(vlan, flags); |
| 531 | return 0; |
| 532 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 533 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 534 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 535 | if (!vlan) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 536 | return -ENOMEM; |
| 537 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 538 | vlan->vid = vid; |
| 539 | vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER; |
| 540 | vlan->flags &= ~BRIDGE_VLAN_INFO_PVID; |
| 541 | vlan->br = br; |
| 542 | if (flags & BRIDGE_VLAN_INFO_BRENTRY) |
| 543 | atomic_set(&vlan->refcnt, 1); |
| 544 | ret = __vlan_add(vlan, flags); |
| 545 | if (ret) |
| 546 | kfree(vlan); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 547 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 548 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 551 | /* Must be protected by RTNL. |
| 552 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 553 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 554 | int br_vlan_delete(struct net_bridge *br, u16 vid) |
| 555 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 556 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 557 | |
| 558 | ASSERT_RTNL(); |
| 559 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 560 | v = br_vlan_find(br->vlgrp, vid); |
| 561 | if (!v || !br_vlan_is_brentry(v)) |
| 562 | return -ENOENT; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 563 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 564 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 565 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 566 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void br_vlan_flush(struct net_bridge *br) |
| 570 | { |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 571 | ASSERT_RTNL(); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 572 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 573 | __vlan_flush(br_vlan_group(br), &br->pvid); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 576 | 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] | 577 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 578 | if (!vg) |
| 579 | return NULL; |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 580 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 581 | return br_vlan_lookup(&vg->vlan_hash, vid); |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 582 | } |
| 583 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 584 | /* Must be protected by RTNL. */ |
| 585 | static void recalculate_group_addr(struct net_bridge *br) |
| 586 | { |
| 587 | if (br->group_addr_set) |
| 588 | return; |
| 589 | |
| 590 | spin_lock_bh(&br->lock); |
| 591 | if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) { |
| 592 | /* Bridge Group Address */ |
| 593 | br->group_addr[5] = 0x00; |
| 594 | } else { /* vlan_enabled && ETH_P_8021AD */ |
| 595 | /* Provider Bridge Group Address */ |
| 596 | br->group_addr[5] = 0x08; |
| 597 | } |
| 598 | spin_unlock_bh(&br->lock); |
| 599 | } |
| 600 | |
| 601 | /* Must be protected by RTNL. */ |
| 602 | void br_recalculate_fwd_mask(struct net_bridge *br) |
| 603 | { |
| 604 | if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) |
| 605 | br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; |
| 606 | else /* vlan_enabled && ETH_P_8021AD */ |
| 607 | br->group_fwd_mask_required = BR_GROUPFWD_8021AD & |
| 608 | ~(1u << br->group_addr[5]); |
| 609 | } |
| 610 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 611 | int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 612 | { |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 613 | if (br->vlan_enabled == val) |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 614 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 615 | |
| 616 | br->vlan_enabled = val; |
Vlad Yasevich | 2796d0c | 2014-05-16 09:59:20 -0400 | [diff] [blame] | 617 | br_manage_promisc(br); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 618 | recalculate_group_addr(br); |
| 619 | br_recalculate_fwd_mask(br); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 620 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
| 625 | { |
| 626 | if (!rtnl_trylock()) |
| 627 | return restart_syscall(); |
| 628 | |
| 629 | __br_vlan_filter_toggle(br, val); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 630 | rtnl_unlock(); |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 631 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 635 | int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 636 | { |
| 637 | int err = 0; |
| 638 | struct net_bridge_port *p; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 639 | struct net_bridge_vlan *vlan; |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 640 | __be16 oldproto; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 641 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 642 | if (br->vlan_proto == proto) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 643 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 644 | |
| 645 | /* Add VLANs for the new proto to the device filter. */ |
| 646 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 647 | list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) { |
| 648 | err = vlan_vid_add(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 649 | if (err) |
| 650 | goto err_filt; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | oldproto = br->vlan_proto; |
| 655 | br->vlan_proto = proto; |
| 656 | |
| 657 | recalculate_group_addr(br); |
| 658 | br_recalculate_fwd_mask(br); |
| 659 | |
| 660 | /* Delete VLANs for the old proto from the device filter. */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 661 | list_for_each_entry(p, &br->port_list, list) |
| 662 | list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) |
| 663 | vlan_vid_del(p->dev, oldproto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 664 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 665 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 666 | |
| 667 | err_filt: |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 668 | list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist) |
| 669 | vlan_vid_del(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 670 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 671 | list_for_each_entry_continue_reverse(p, &br->port_list, list) |
| 672 | list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) |
| 673 | vlan_vid_del(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 674 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 675 | return err; |
| 676 | } |
| 677 | |
| 678 | int br_vlan_set_proto(struct net_bridge *br, unsigned long val) |
| 679 | { |
| 680 | int err; |
| 681 | |
| 682 | if (val != ETH_P_8021Q && val != ETH_P_8021AD) |
| 683 | return -EPROTONOSUPPORT; |
| 684 | |
| 685 | if (!rtnl_trylock()) |
| 686 | return restart_syscall(); |
| 687 | |
| 688 | err = __br_vlan_set_proto(br, htons(val)); |
| 689 | rtnl_unlock(); |
| 690 | |
| 691 | return err; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 692 | } |
| 693 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 694 | static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 pvid, |
| 695 | u16 vid) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 696 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 697 | struct net_bridge_vlan *v; |
| 698 | |
| 699 | if (vid != pvid) |
| 700 | return false; |
| 701 | |
| 702 | v = br_vlan_lookup(&vg->vlan_hash, vid); |
| 703 | if (v && br_vlan_should_use(v) && |
| 704 | (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)) |
| 705 | return true; |
| 706 | |
| 707 | return false; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static void br_vlan_disable_default_pvid(struct net_bridge *br) |
| 711 | { |
| 712 | struct net_bridge_port *p; |
| 713 | u16 pvid = br->default_pvid; |
| 714 | |
| 715 | /* Disable default_pvid on all ports where it is still |
| 716 | * configured. |
| 717 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 718 | if (vlan_default_pvid(br->vlgrp, br->pvid, pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 719 | br_vlan_delete(br, pvid); |
| 720 | |
| 721 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 722 | if (vlan_default_pvid(p->vlgrp, p->pvid, pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 723 | nbp_vlan_delete(p, pvid); |
| 724 | } |
| 725 | |
| 726 | br->default_pvid = 0; |
| 727 | } |
| 728 | |
| 729 | static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid) |
| 730 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 731 | const struct net_bridge_vlan *pvent; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 732 | struct net_bridge_port *p; |
| 733 | u16 old_pvid; |
| 734 | int err = 0; |
| 735 | unsigned long *changed; |
| 736 | |
| 737 | changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), |
| 738 | GFP_KERNEL); |
| 739 | if (!changed) |
| 740 | return -ENOMEM; |
| 741 | |
| 742 | old_pvid = br->default_pvid; |
| 743 | |
| 744 | /* Update default_pvid config only if we do not conflict with |
| 745 | * user configuration. |
| 746 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 747 | pvent = br_vlan_find(br->vlgrp, pvid); |
| 748 | if ((!old_pvid || vlan_default_pvid(br->vlgrp, br->pvid, old_pvid)) && |
| 749 | (!pvent || !br_vlan_should_use(pvent))) { |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 750 | err = br_vlan_add(br, pvid, |
| 751 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 752 | BRIDGE_VLAN_INFO_UNTAGGED | |
| 753 | BRIDGE_VLAN_INFO_BRENTRY); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 754 | if (err) |
| 755 | goto out; |
| 756 | br_vlan_delete(br, old_pvid); |
| 757 | set_bit(0, changed); |
| 758 | } |
| 759 | |
| 760 | list_for_each_entry(p, &br->port_list, list) { |
| 761 | /* Update default_pvid config only if we do not conflict with |
| 762 | * user configuration. |
| 763 | */ |
| 764 | if ((old_pvid && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 765 | !vlan_default_pvid(p->vlgrp, p->pvid, old_pvid)) || |
| 766 | br_vlan_find(p->vlgrp, pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 767 | continue; |
| 768 | |
| 769 | err = nbp_vlan_add(p, pvid, |
| 770 | BRIDGE_VLAN_INFO_PVID | |
| 771 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 772 | if (err) |
| 773 | goto err_port; |
| 774 | nbp_vlan_delete(p, old_pvid); |
| 775 | set_bit(p->port_no, changed); |
| 776 | } |
| 777 | |
| 778 | br->default_pvid = pvid; |
| 779 | |
| 780 | out: |
| 781 | kfree(changed); |
| 782 | return err; |
| 783 | |
| 784 | err_port: |
| 785 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 786 | if (!test_bit(p->port_no, changed)) |
| 787 | continue; |
| 788 | |
| 789 | if (old_pvid) |
| 790 | nbp_vlan_add(p, old_pvid, |
| 791 | BRIDGE_VLAN_INFO_PVID | |
| 792 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 793 | nbp_vlan_delete(p, pvid); |
| 794 | } |
| 795 | |
| 796 | if (test_bit(0, changed)) { |
| 797 | if (old_pvid) |
| 798 | br_vlan_add(br, old_pvid, |
| 799 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 800 | BRIDGE_VLAN_INFO_UNTAGGED | |
| 801 | BRIDGE_VLAN_INFO_BRENTRY); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 802 | br_vlan_delete(br, pvid); |
| 803 | } |
| 804 | goto out; |
| 805 | } |
| 806 | |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 807 | int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) |
| 808 | { |
| 809 | u16 pvid = val; |
| 810 | int err = 0; |
| 811 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 812 | if (val >= VLAN_VID_MASK) |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 813 | return -EINVAL; |
| 814 | |
| 815 | if (!rtnl_trylock()) |
| 816 | return restart_syscall(); |
| 817 | |
| 818 | if (pvid == br->default_pvid) |
| 819 | goto unlock; |
| 820 | |
| 821 | /* Only allow default pvid change when filtering is disabled */ |
| 822 | if (br->vlan_enabled) { |
| 823 | pr_info_once("Please disable vlan filtering to change default_pvid\n"); |
| 824 | err = -EPERM; |
| 825 | goto unlock; |
| 826 | } |
| 827 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 828 | if (!pvid) |
| 829 | br_vlan_disable_default_pvid(br); |
| 830 | else |
| 831 | err = __br_vlan_set_default_pvid(br, pvid); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 832 | |
| 833 | unlock: |
| 834 | rtnl_unlock(); |
| 835 | return err; |
| 836 | } |
| 837 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 838 | int br_vlan_init(struct net_bridge *br) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 839 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 840 | int ret = -ENOMEM; |
| 841 | |
| 842 | br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL); |
| 843 | if (!br->vlgrp) |
| 844 | goto out; |
| 845 | ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params); |
| 846 | if (ret) |
| 847 | goto err_rhtbl; |
| 848 | INIT_LIST_HEAD(&br->vlgrp->vlan_list); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 849 | br->vlan_proto = htons(ETH_P_8021Q); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 850 | br->default_pvid = 1; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 851 | ret = br_vlan_add(br, 1, |
| 852 | BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED | |
| 853 | BRIDGE_VLAN_INFO_BRENTRY); |
| 854 | if (ret) |
| 855 | goto err_vlan_add; |
| 856 | |
| 857 | out: |
| 858 | return ret; |
| 859 | |
| 860 | err_vlan_add: |
| 861 | rhashtable_destroy(&br->vlgrp->vlan_hash); |
| 862 | err_rhtbl: |
| 863 | kfree(br->vlgrp); |
| 864 | |
| 865 | goto out; |
| 866 | } |
| 867 | |
| 868 | int nbp_vlan_init(struct net_bridge_port *p) |
| 869 | { |
| 870 | int ret = -ENOMEM; |
| 871 | |
| 872 | p->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL); |
| 873 | if (!p->vlgrp) |
| 874 | goto out; |
| 875 | |
| 876 | ret = rhashtable_init(&p->vlgrp->vlan_hash, &br_vlan_rht_params); |
| 877 | if (ret) |
| 878 | goto err_rhtbl; |
| 879 | INIT_LIST_HEAD(&p->vlgrp->vlan_list); |
| 880 | if (p->br->default_pvid) { |
| 881 | ret = nbp_vlan_add(p, p->br->default_pvid, |
| 882 | BRIDGE_VLAN_INFO_PVID | |
| 883 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 884 | if (ret) |
| 885 | goto err_vlan_add; |
| 886 | } |
| 887 | out: |
| 888 | return ret; |
| 889 | |
| 890 | err_vlan_add: |
| 891 | rhashtable_destroy(&p->vlgrp->vlan_hash); |
| 892 | err_rhtbl: |
| 893 | kfree(p->vlgrp); |
| 894 | |
| 895 | goto out; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 896 | } |
| 897 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 898 | /* Must be protected by RTNL. |
| 899 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 900 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 901 | 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] | 902 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 903 | struct net_bridge_vlan *vlan; |
| 904 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 905 | |
| 906 | ASSERT_RTNL(); |
| 907 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 908 | vlan = br_vlan_find(port->vlgrp, vid); |
| 909 | if (vlan) { |
| 910 | __vlan_add_flags(vlan, flags); |
| 911 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 914 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 915 | if (!vlan) |
| 916 | return -ENOMEM; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 917 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 918 | vlan->vid = vid; |
| 919 | vlan->port = port; |
| 920 | ret = __vlan_add(vlan, flags); |
| 921 | if (ret) |
| 922 | kfree(vlan); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 923 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 924 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 927 | /* Must be protected by RTNL. |
| 928 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 929 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 930 | int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) |
| 931 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 932 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 933 | |
| 934 | ASSERT_RTNL(); |
| 935 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 936 | v = br_vlan_find(port->vlgrp, vid); |
| 937 | if (!v) |
| 938 | return -ENOENT; |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 939 | 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] | 940 | br_fdb_delete_by_port(port->br, port, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 941 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 942 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | void nbp_vlan_flush(struct net_bridge_port *port) |
| 946 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 947 | struct net_bridge_vlan *vlan; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 948 | |
| 949 | ASSERT_RTNL(); |
| 950 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 951 | list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist) |
| 952 | vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 953 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 954 | __vlan_flush(nbp_vlan_group(port), &port->pvid); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 955 | } |