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