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