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