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