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