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