Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/netdevice.h> |
| 4 | #include <linux/rtnetlink.h> |
| 5 | #include <linux/slab.h> |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 6 | #include <net/switchdev.h> |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 7 | |
| 8 | #include "br_private.h" |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 9 | #include "br_private_tunnel.h" |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 10 | |
Mike Manning | 80900ac | 2019-04-18 18:35:34 +0100 | [diff] [blame] | 11 | static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid); |
| 12 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 13 | static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg, |
| 14 | const void *ptr) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 15 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 16 | const struct net_bridge_vlan *vle = ptr; |
| 17 | u16 vid = *(u16 *)arg->key; |
| 18 | |
| 19 | return vle->vid != vid; |
| 20 | } |
| 21 | |
| 22 | static const struct rhashtable_params br_vlan_rht_params = { |
| 23 | .head_offset = offsetof(struct net_bridge_vlan, vnode), |
| 24 | .key_offset = offsetof(struct net_bridge_vlan, vid), |
| 25 | .key_len = sizeof(u16), |
Nikolay Aleksandrov | 8af78b6 | 2015-09-30 20:16:51 +0200 | [diff] [blame] | 26 | .nelem_hint = 3, |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 27 | .max_size = VLAN_N_VID, |
| 28 | .obj_cmpfn = br_vlan_cmp, |
| 29 | .automatic_shrinking = true, |
| 30 | }; |
| 31 | |
| 32 | static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid) |
| 33 | { |
| 34 | return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params); |
| 35 | } |
| 36 | |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 37 | static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, |
| 38 | const struct net_bridge_vlan *v) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 39 | { |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 40 | if (vg->pvid == v->vid) |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 41 | return false; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 42 | |
| 43 | smp_wmb(); |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 44 | br_vlan_set_pvid_state(vg, v->state); |
| 45 | vg->pvid = v->vid; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 46 | |
| 47 | return true; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 50 | 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] | 51 | { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 52 | if (vg->pvid != vid) |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 53 | return false; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 54 | |
| 55 | smp_wmb(); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 56 | vg->pvid = 0; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 57 | |
| 58 | return true; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 61 | /* return true if anything changed, false otherwise */ |
| 62 | static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags) |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 63 | { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 64 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 65 | u16 old_flags = v->flags; |
| 66 | bool ret; |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 67 | |
| 68 | if (br_vlan_is_master(v)) |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 69 | vg = br_vlan_group(v->br); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 70 | else |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 71 | vg = nbp_vlan_group(v->port); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 72 | |
| 73 | if (flags & BRIDGE_VLAN_INFO_PVID) |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 74 | ret = __vlan_add_pvid(vg, v); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 75 | else |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 76 | ret = __vlan_delete_pvid(vg, v->vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 77 | |
| 78 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 79 | v->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
Vlad Yasevich | 635126b | 2014-09-12 16:26:17 -0400 | [diff] [blame] | 80 | else |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 81 | v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 82 | |
| 83 | return ret || !!(old_flags ^ v->flags); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 86 | static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 87 | struct net_bridge_vlan *v, u16 flags, |
| 88 | struct netlink_ext_ack *extack) |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 89 | { |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 90 | int err; |
| 91 | |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 92 | /* Try switchdev op first. In case it is not supported, fallback to |
| 93 | * 8021q add. |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 94 | */ |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 95 | err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack); |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 96 | if (err == -EOPNOTSUPP) |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 97 | return vlan_vid_add(dev, br->vlan_proto, v->vid); |
| 98 | v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 99 | return err; |
| 100 | } |
| 101 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 102 | static void __vlan_add_list(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 103 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 104 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 105 | struct list_head *headp, *hpos; |
| 106 | struct net_bridge_vlan *vent; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 107 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 108 | if (br_vlan_is_master(v)) |
| 109 | vg = br_vlan_group(v->br); |
| 110 | else |
| 111 | vg = nbp_vlan_group(v->port); |
| 112 | |
| 113 | headp = &vg->vlan_list; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 114 | list_for_each_prev(hpos, headp) { |
| 115 | vent = list_entry(hpos, struct net_bridge_vlan, vlist); |
| 116 | if (v->vid < vent->vid) |
| 117 | continue; |
| 118 | else |
| 119 | break; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 120 | } |
Nikolay Aleksandrov | 586c2b5 | 2015-10-02 15:05:10 +0200 | [diff] [blame] | 121 | list_add_rcu(&v->vlist, hpos); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 122 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 123 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 124 | static void __vlan_del_list(struct net_bridge_vlan *v) |
| 125 | { |
Nikolay Aleksandrov | 586c2b5 | 2015-10-02 15:05:10 +0200 | [diff] [blame] | 126 | list_del_rcu(&v->vlist); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 129 | static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 130 | const struct net_bridge_vlan *v) |
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 | int err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 133 | |
Jiri Pirko | 0944d6b | 2015-10-09 13:54:11 +0200 | [diff] [blame] | 134 | /* Try switchdev op first. In case it is not supported, fallback to |
| 135 | * 8021q del. |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 136 | */ |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 137 | err = br_switchdev_port_vlan_del(dev, v->vid); |
| 138 | if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)) |
| 139 | vlan_vid_del(dev, br->vlan_proto, v->vid); |
| 140 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Randy Dunlap | 4bbd026 | 2020-09-17 21:35:21 -0700 | [diff] [blame] | 143 | /* Returns a master vlan, if it didn't exist it gets created. In all cases |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 144 | * a reference is taken to the master vlan before returning. |
| 145 | */ |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 146 | static struct net_bridge_vlan * |
| 147 | br_vlan_get_master(struct net_bridge *br, u16 vid, |
| 148 | struct netlink_ext_ack *extack) |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 149 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 150 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 151 | struct net_bridge_vlan *masterv; |
| 152 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 153 | vg = br_vlan_group(br); |
| 154 | masterv = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 155 | if (!masterv) { |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 156 | bool changed; |
| 157 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 158 | /* missing global ctx, create it now */ |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 159 | if (br_vlan_add(br, vid, 0, &changed, extack)) |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 160 | return NULL; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 161 | masterv = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 162 | if (WARN_ON(!masterv)) |
| 163 | return NULL; |
Ido Schimmel | 0e5a82e | 2018-02-25 21:59:06 +0200 | [diff] [blame] | 164 | refcount_set(&masterv->refcnt, 1); |
| 165 | return masterv; |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 166 | } |
Reshetova, Elena | 2512775 | 2017-07-04 15:53:05 +0300 | [diff] [blame] | 167 | refcount_inc(&masterv->refcnt); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 168 | |
| 169 | return masterv; |
| 170 | } |
| 171 | |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 172 | static void br_master_vlan_rcu_free(struct rcu_head *rcu) |
| 173 | { |
| 174 | struct net_bridge_vlan *v; |
| 175 | |
| 176 | v = container_of(rcu, struct net_bridge_vlan, rcu); |
| 177 | WARN_ON(!br_vlan_is_master(v)); |
| 178 | free_percpu(v->stats); |
| 179 | v->stats = NULL; |
| 180 | kfree(v); |
| 181 | } |
| 182 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 183 | static void br_vlan_put_master(struct net_bridge_vlan *masterv) |
| 184 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 185 | struct net_bridge_vlan_group *vg; |
| 186 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 187 | if (!br_vlan_is_master(masterv)) |
| 188 | return; |
| 189 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 190 | vg = br_vlan_group(masterv->br); |
Reshetova, Elena | 2512775 | 2017-07-04 15:53:05 +0300 | [diff] [blame] | 191 | if (refcount_dec_and_test(&masterv->refcnt)) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 192 | rhashtable_remove_fast(&vg->vlan_hash, |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 193 | &masterv->vnode, br_vlan_rht_params); |
| 194 | __vlan_del_list(masterv); |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 195 | call_rcu(&masterv->rcu, br_master_vlan_rcu_free); |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 199 | static void nbp_vlan_rcu_free(struct rcu_head *rcu) |
| 200 | { |
| 201 | struct net_bridge_vlan *v; |
| 202 | |
| 203 | v = container_of(rcu, struct net_bridge_vlan, rcu); |
| 204 | WARN_ON(br_vlan_is_master(v)); |
| 205 | /* if we had per-port stats configured then free them here */ |
Nikolay Aleksandrov | 9d332e69c | 2018-11-16 18:50:01 +0200 | [diff] [blame] | 206 | if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS) |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 207 | free_percpu(v->stats); |
| 208 | v->stats = NULL; |
| 209 | kfree(v); |
| 210 | } |
| 211 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 212 | /* This is the shared VLAN add function which works for both ports and bridge |
| 213 | * devices. There are four possible calls to this function in terms of the |
| 214 | * vlan entry type: |
| 215 | * 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] | 216 | * 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] | 217 | * 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] | 218 | * is being created right now (master flag set, brentry flag unset), the |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 219 | * 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] | 220 | * 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] | 221 | * will be used for filtering in both the port and the bridge |
| 222 | */ |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 223 | static int __vlan_add(struct net_bridge_vlan *v, u16 flags, |
| 224 | struct netlink_ext_ack *extack) |
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 | struct net_bridge_vlan *masterv = NULL; |
| 227 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 228 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 229 | struct net_device *dev; |
| 230 | struct net_bridge *br; |
| 231 | int err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 232 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 233 | if (br_vlan_is_master(v)) { |
| 234 | br = v->br; |
| 235 | dev = br->dev; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 236 | vg = br_vlan_group(br); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 237 | } else { |
| 238 | p = v->port; |
| 239 | br = p->br; |
| 240 | dev = p->dev; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 241 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 242 | } |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 243 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 244 | if (p) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 245 | /* Add VLAN to the device filter if it is supported. |
| 246 | * This ensures tagged traffic enters the bridge when |
| 247 | * promiscuous mode is disabled by br_manage_promisc(). |
| 248 | */ |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 249 | err = __vlan_vid_add(dev, br, v, flags, extack); |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame] | 250 | if (err) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 251 | goto out; |
| 252 | |
| 253 | /* need to work on the master vlan too */ |
| 254 | if (flags & BRIDGE_VLAN_INFO_MASTER) { |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 255 | bool changed; |
| 256 | |
| 257 | err = br_vlan_add(br, v->vid, |
| 258 | flags | BRIDGE_VLAN_INFO_BRENTRY, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 259 | &changed, extack); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 260 | if (err) |
| 261 | goto out_filt; |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 262 | |
| 263 | if (changed) |
| 264 | br_vlan_notify(br, NULL, v->vid, 0, |
| 265 | RTM_NEWVLAN); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 268 | masterv = br_vlan_get_master(br, v->vid, extack); |
Zhang Changzhong | ee4f52a8 | 2020-12-04 16:48:56 +0800 | [diff] [blame] | 269 | if (!masterv) { |
| 270 | err = -ENOMEM; |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 271 | goto out_filt; |
Zhang Changzhong | ee4f52a8 | 2020-12-04 16:48:56 +0800 | [diff] [blame] | 272 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 273 | v->brvlan = masterv; |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 274 | if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) { |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 275 | v->stats = |
| 276 | netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 277 | if (!v->stats) { |
| 278 | err = -ENOMEM; |
| 279 | goto out_filt; |
| 280 | } |
Nikolay Aleksandrov | 9d332e69c | 2018-11-16 18:50:01 +0200 | [diff] [blame] | 281 | v->priv_flags |= BR_VLFLAG_PER_PORT_STATS; |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 282 | } else { |
| 283 | v->stats = masterv->stats; |
| 284 | } |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 285 | } else { |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 286 | err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack); |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 287 | if (err && err != -EOPNOTSUPP) |
| 288 | goto out; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 289 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 290 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 291 | /* 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] | 292 | if (br_vlan_should_use(v)) { |
| 293 | err = br_fdb_insert(br, p, dev->dev_addr, v->vid); |
| 294 | if (err) { |
| 295 | br_err(br, "failed insert local address into bridge forwarding table\n"); |
| 296 | goto out_filt; |
| 297 | } |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 298 | vg->num_vlans++; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 299 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 300 | |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 301 | /* set the state before publishing */ |
| 302 | v->state = BR_STATE_FORWARDING; |
| 303 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 304 | err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode, |
| 305 | br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 306 | if (err) |
| 307 | goto out_fdb_insert; |
| 308 | |
| 309 | __vlan_add_list(v); |
| 310 | __vlan_add_flags(v, flags); |
Mike Manning | 80900ac | 2019-04-18 18:35:34 +0100 | [diff] [blame] | 311 | |
| 312 | if (p) |
| 313 | nbp_vlan_set_vlan_dev_state(p, v->vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 314 | out: |
| 315 | return err; |
| 316 | |
| 317 | out_fdb_insert: |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 318 | if (br_vlan_should_use(v)) { |
| 319 | br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid); |
| 320 | vg->num_vlans--; |
| 321 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 322 | |
| 323 | out_filt: |
| 324 | if (p) { |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 325 | __vlan_vid_del(dev, br, v); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 326 | if (masterv) { |
Li RongQing | 1a3aea2 | 2018-10-15 19:00:31 +0800 | [diff] [blame] | 327 | if (v->stats && masterv->stats != v->stats) |
| 328 | free_percpu(v->stats); |
| 329 | v->stats = NULL; |
| 330 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 331 | br_vlan_put_master(masterv); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 332 | v->brvlan = NULL; |
| 333 | } |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 334 | } else { |
| 335 | br_switchdev_port_vlan_del(dev, v->vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | goto out; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 341 | static int __vlan_del(struct net_bridge_vlan *v) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 342 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 343 | struct net_bridge_vlan *masterv = v; |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 344 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 345 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 346 | int err = 0; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 347 | |
| 348 | if (br_vlan_is_master(v)) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 349 | vg = br_vlan_group(v->br); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 350 | } else { |
| 351 | p = v->port; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 352 | vg = nbp_vlan_group(v->port); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 353 | masterv = v->brvlan; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 356 | __vlan_delete_pvid(vg, v->vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 357 | if (p) { |
Ido Schimmel | 2797379 | 2019-01-08 16:48:11 +0000 | [diff] [blame] | 358 | err = __vlan_vid_del(p->dev, p->br, v); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 359 | if (err) |
| 360 | goto out; |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 361 | } else { |
| 362 | err = br_switchdev_port_vlan_del(v->br->dev, v->vid); |
| 363 | if (err && err != -EOPNOTSUPP) |
| 364 | goto out; |
| 365 | err = 0; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Nikolay Aleksandrov | 6be144f | 2015-10-02 15:05:13 +0200 | [diff] [blame] | 368 | if (br_vlan_should_use(v)) { |
| 369 | v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY; |
| 370 | vg->num_vlans--; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (masterv != v) { |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 374 | vlan_tunnel_info_del(vg, v); |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 375 | rhashtable_remove_fast(&vg->vlan_hash, &v->vnode, |
| 376 | br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 377 | __vlan_del_list(v); |
Mike Manning | 80900ac | 2019-04-18 18:35:34 +0100 | [diff] [blame] | 378 | nbp_vlan_set_vlan_dev_state(p, v->vid); |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 379 | call_rcu(&v->rcu, nbp_vlan_rcu_free); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Nikolay Aleksandrov | f8ed289 | 2015-10-02 15:05:11 +0200 | [diff] [blame] | 382 | br_vlan_put_master(masterv); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 383 | out: |
| 384 | return err; |
| 385 | } |
| 386 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 387 | static void __vlan_group_free(struct net_bridge_vlan_group *vg) |
| 388 | { |
| 389 | WARN_ON(!list_empty(&vg->vlan_list)); |
| 390 | rhashtable_destroy(&vg->vlan_hash); |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 391 | vlan_tunnel_deinit(vg); |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 392 | kfree(vg); |
| 393 | } |
| 394 | |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 395 | static void __vlan_flush(const struct net_bridge *br, |
| 396 | const struct net_bridge_port *p, |
| 397 | struct net_bridge_vlan_group *vg) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 398 | { |
| 399 | struct net_bridge_vlan *vlan, *tmp; |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 400 | u16 v_start = 0, v_end = 0; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 401 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 402 | __vlan_delete_pvid(vg, vg->pvid); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 403 | list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) { |
| 404 | /* take care of disjoint ranges */ |
| 405 | if (!v_start) { |
| 406 | v_start = vlan->vid; |
| 407 | } else if (vlan->vid - v_end != 1) { |
| 408 | /* found range end, notify and start next one */ |
| 409 | br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN); |
| 410 | v_start = vlan->vid; |
| 411 | } |
| 412 | v_end = vlan->vid; |
| 413 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 414 | __vlan_del(vlan); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* notify about the last/whole vlan range */ |
| 418 | if (v_start) |
| 419 | br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 422 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
Roopa Prabhu | 11538d0 | 2017-01-31 22:59:55 -0800 | [diff] [blame] | 423 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 424 | struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 425 | struct sk_buff *skb) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 426 | { |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 427 | struct pcpu_sw_netstats *stats; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 428 | struct net_bridge_vlan *v; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 429 | u16 vid; |
| 430 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 431 | /* If this packet was not filtered at input, let it pass */ |
| 432 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 433 | goto out; |
| 434 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 435 | /* At this point, we know that the frame was filtered and contains |
| 436 | * a valid vlan id. If the vlan id has untagged flag set, |
| 437 | * send untagged; otherwise, send tagged. |
| 438 | */ |
| 439 | br_vlan_get_tag(skb, &vid); |
| 440 | v = br_vlan_find(vg, vid); |
| 441 | /* Vlan entry must be configured at this point. The |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 442 | * only exception is the bridge is set in promisc mode and the |
| 443 | * packet is destined for the bridge device. In this case |
| 444 | * pass the packet as is. |
| 445 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 446 | if (!v || !br_vlan_should_use(v)) { |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 447 | if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { |
| 448 | goto out; |
| 449 | } else { |
| 450 | kfree_skb(skb); |
| 451 | return NULL; |
| 452 | } |
| 453 | } |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 454 | if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) { |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 455 | stats = this_cpu_ptr(v->stats); |
| 456 | u64_stats_update_begin(&stats->syncp); |
| 457 | stats->tx_bytes += skb->len; |
| 458 | stats->tx_packets++; |
| 459 | u64_stats_update_end(&stats->syncp); |
| 460 | } |
| 461 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 462 | if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) |
Michał Mirosław | 5978f8a | 2018-11-09 00:18:03 +0100 | [diff] [blame] | 463 | __vlan_hwaccel_clear_tag(skb); |
Roopa Prabhu | 11538d0 | 2017-01-31 22:59:55 -0800 | [diff] [blame] | 464 | |
| 465 | if (p && (p->flags & BR_VLAN_TUNNEL) && |
| 466 | br_handle_egress_vlan_tunnel(skb, v)) { |
| 467 | kfree_skb(skb); |
| 468 | return NULL; |
| 469 | } |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 470 | out: |
| 471 | return skb; |
| 472 | } |
| 473 | |
| 474 | /* Called under RCU */ |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 475 | static bool __allowed_ingress(const struct net_bridge *br, |
| 476 | struct net_bridge_vlan_group *vg, |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 477 | struct sk_buff *skb, u16 *vid, |
| 478 | u8 *state) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 479 | { |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 480 | struct pcpu_sw_netstats *stats; |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 481 | struct net_bridge_vlan *v; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 482 | bool tagged; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 483 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 484 | BR_INPUT_SKB_CB(skb)->vlan_filtered = true; |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 485 | /* If vlan tx offload is disabled on bridge device and frame was |
| 486 | * sent from vlan device on the bridge device, it does not have |
| 487 | * HW accelerated vlan tag. |
| 488 | */ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 489 | if (unlikely(!skb_vlan_tag_present(skb) && |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 490 | skb->protocol == br->vlan_proto)) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 491 | skb = skb_vlan_untag(skb); |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 492 | if (unlikely(!skb)) |
| 493 | return false; |
| 494 | } |
| 495 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 496 | if (!br_vlan_get_tag(skb, vid)) { |
| 497 | /* Tagged frame */ |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 498 | if (skb->vlan_proto != br->vlan_proto) { |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 499 | /* Protocol-mismatch, empty out vlan_tci for new tag */ |
| 500 | skb_push(skb, ETH_HLEN); |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 501 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 502 | skb_vlan_tag_get(skb)); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 503 | if (unlikely(!skb)) |
| 504 | return false; |
| 505 | |
| 506 | skb_pull(skb, ETH_HLEN); |
| 507 | skb_reset_mac_len(skb); |
| 508 | *vid = 0; |
| 509 | tagged = false; |
| 510 | } else { |
| 511 | tagged = true; |
| 512 | } |
| 513 | } else { |
| 514 | /* Untagged frame */ |
| 515 | tagged = false; |
| 516 | } |
| 517 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 518 | if (!*vid) { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 519 | u16 pvid = br_get_pvid(vg); |
| 520 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 521 | /* Frame had a tag with VID 0 or did not have a tag. |
| 522 | * See if pvid is set on this port. That tells us which |
| 523 | * vlan untagged or priority-tagged traffic belongs to. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 524 | */ |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 525 | if (!pvid) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 526 | goto drop; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 527 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 528 | /* PVID is set on this port. Any untagged or priority-tagged |
| 529 | * ingress frame is considered to belong to this vlan. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 530 | */ |
Toshiaki Makita | dfb5fa3 | 2013-10-16 17:07:16 +0900 | [diff] [blame] | 531 | *vid = pvid; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 532 | if (likely(!tagged)) |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 533 | /* Untagged Frame. */ |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 534 | __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid); |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 535 | else |
| 536 | /* Priority-tagged Frame. |
Michał Mirosław | 5978f8a | 2018-11-09 00:18:03 +0100 | [diff] [blame] | 537 | * At this point, we know that skb->vlan_tci VID |
| 538 | * field was 0. |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 539 | * We update only VID field and preserve PCP field. |
| 540 | */ |
| 541 | skb->vlan_tci |= pvid; |
| 542 | |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 543 | /* if stats are disabled we can avoid the lookup */ |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 544 | if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) { |
| 545 | if (*state == BR_STATE_FORWARDING) { |
| 546 | *state = br_vlan_get_pvid_state(vg); |
| 547 | return br_vlan_state_allowed(*state, true); |
| 548 | } else { |
| 549 | return true; |
| 550 | } |
| 551 | } |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 552 | } |
| 553 | v = br_vlan_find(vg, *vid); |
| 554 | if (!v || !br_vlan_should_use(v)) |
| 555 | goto drop; |
| 556 | |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 557 | if (*state == BR_STATE_FORWARDING) { |
| 558 | *state = br_vlan_get_state(v); |
| 559 | if (!br_vlan_state_allowed(*state, true)) |
| 560 | goto drop; |
| 561 | } |
| 562 | |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 563 | if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) { |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 564 | stats = this_cpu_ptr(v->stats); |
| 565 | u64_stats_update_begin(&stats->syncp); |
| 566 | stats->rx_bytes += skb->len; |
| 567 | stats->rx_packets++; |
| 568 | u64_stats_update_end(&stats->syncp); |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 571 | return true; |
| 572 | |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 573 | drop: |
| 574 | kfree_skb(skb); |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 578 | bool br_allowed_ingress(const struct net_bridge *br, |
| 579 | struct net_bridge_vlan_group *vg, struct sk_buff *skb, |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 580 | u16 *vid, u8 *state) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 581 | { |
| 582 | /* If VLAN filtering is disabled on the bridge, all packets are |
| 583 | * permitted. |
| 584 | */ |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 585 | if (!br_opt_get(br, BROPT_VLAN_ENABLED)) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 586 | BR_INPUT_SKB_CB(skb)->vlan_filtered = false; |
| 587 | return true; |
| 588 | } |
| 589 | |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 590 | return __allowed_ingress(br, vg, skb, vid, state); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 593 | /* Called under RCU. */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 594 | bool br_allowed_egress(struct net_bridge_vlan_group *vg, |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 595 | const struct sk_buff *skb) |
| 596 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 597 | const struct net_bridge_vlan *v; |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 598 | u16 vid; |
| 599 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 600 | /* If this packet was not filtered at input, let it pass */ |
| 601 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 602 | return true; |
| 603 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 604 | br_vlan_get_tag(skb, &vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 605 | v = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 606 | if (v && br_vlan_should_use(v) && |
| 607 | br_vlan_state_allowed(br_vlan_get_state(v), false)) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 608 | return true; |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 613 | /* Called under RCU */ |
| 614 | bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) |
| 615 | { |
Nikolay Aleksandrov | 468e794 | 2015-09-30 20:16:52 +0200 | [diff] [blame] | 616 | struct net_bridge_vlan_group *vg; |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 617 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 618 | struct net_bridge_vlan *v; |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 619 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 620 | /* If filtering was disabled at input, let it pass. */ |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 621 | if (!br_opt_get(br, BROPT_VLAN_ENABLED)) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 622 | return true; |
| 623 | |
Ido Schimmel | eca1e00 | 2015-11-01 18:31:45 +0200 | [diff] [blame] | 624 | vg = nbp_vlan_group_rcu(p); |
Nikolay Aleksandrov | 468e794 | 2015-09-30 20:16:52 +0200 | [diff] [blame] | 625 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 626 | return false; |
| 627 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 628 | if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) |
| 629 | *vid = 0; |
| 630 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 631 | if (!*vid) { |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 632 | *vid = br_get_pvid(vg); |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 633 | if (!*vid || |
| 634 | !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true)) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 635 | return false; |
| 636 | |
| 637 | return true; |
| 638 | } |
| 639 | |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 640 | v = br_vlan_find(vg, *vid); |
| 641 | if (v && br_vlan_state_allowed(br_vlan_get_state(v), true)) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 642 | return true; |
| 643 | |
| 644 | return false; |
| 645 | } |
| 646 | |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 647 | static int br_vlan_add_existing(struct net_bridge *br, |
| 648 | struct net_bridge_vlan_group *vg, |
| 649 | struct net_bridge_vlan *vlan, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 650 | u16 flags, bool *changed, |
| 651 | struct netlink_ext_ack *extack) |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 652 | { |
| 653 | int err; |
| 654 | |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 655 | err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack); |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 656 | if (err && err != -EOPNOTSUPP) |
| 657 | return err; |
| 658 | |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 659 | if (!br_vlan_is_brentry(vlan)) { |
| 660 | /* Trying to change flags of non-existent bridge vlan */ |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 661 | if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) { |
| 662 | err = -EINVAL; |
| 663 | goto err_flags; |
| 664 | } |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 665 | /* It was only kept for port vlans, now make it real */ |
| 666 | err = br_fdb_insert(br, NULL, br->dev->dev_addr, |
| 667 | vlan->vid); |
| 668 | if (err) { |
| 669 | 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] | 670 | goto err_fdb_insert; |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | refcount_inc(&vlan->refcnt); |
| 674 | vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY; |
| 675 | vg->num_vlans++; |
| 676 | *changed = true; |
| 677 | } |
| 678 | |
| 679 | if (__vlan_add_flags(vlan, flags)) |
| 680 | *changed = true; |
| 681 | |
| 682 | return 0; |
Petr Machata | 9c86ce2 | 2018-05-30 03:00:45 +0200 | [diff] [blame] | 683 | |
| 684 | err_fdb_insert: |
| 685 | err_flags: |
| 686 | br_switchdev_port_vlan_del(br->dev, vlan->vid); |
| 687 | return err; |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 690 | /* Must be protected by RTNL. |
| 691 | * Must be called with vid in range from 1 to 4094 inclusive. |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 692 | * changed must be true only if the vlan was created or updated |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 693 | */ |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 694 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed, |
| 695 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 696 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 697 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 698 | struct net_bridge_vlan *vlan; |
| 699 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 700 | |
| 701 | ASSERT_RTNL(); |
| 702 | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 703 | *changed = false; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 704 | vg = br_vlan_group(br); |
| 705 | vlan = br_vlan_find(vg, vid); |
Petr Machata | dbd6dc7 | 2018-05-30 02:56:55 +0200 | [diff] [blame] | 706 | if (vlan) |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 707 | return br_vlan_add_existing(br, vg, vlan, flags, changed, |
| 708 | extack); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 709 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 710 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 711 | if (!vlan) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 712 | return -ENOMEM; |
| 713 | |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 714 | vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 715 | if (!vlan->stats) { |
| 716 | kfree(vlan); |
| 717 | return -ENOMEM; |
| 718 | } |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 719 | vlan->vid = vid; |
| 720 | vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER; |
| 721 | vlan->flags &= ~BRIDGE_VLAN_INFO_PVID; |
| 722 | vlan->br = br; |
| 723 | if (flags & BRIDGE_VLAN_INFO_BRENTRY) |
Reshetova, Elena | 2512775 | 2017-07-04 15:53:05 +0300 | [diff] [blame] | 724 | refcount_set(&vlan->refcnt, 1); |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 725 | ret = __vlan_add(vlan, flags, extack); |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 726 | if (ret) { |
| 727 | free_percpu(vlan->stats); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 728 | kfree(vlan); |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 729 | } else { |
| 730 | *changed = true; |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 731 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 732 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 733 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 736 | /* Must be protected by RTNL. |
| 737 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 738 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 739 | int br_vlan_delete(struct net_bridge *br, u16 vid) |
| 740 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 741 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 742 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 743 | |
| 744 | ASSERT_RTNL(); |
| 745 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 746 | vg = br_vlan_group(br); |
| 747 | v = br_vlan_find(vg, vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 748 | if (!v || !br_vlan_is_brentry(v)) |
| 749 | return -ENOENT; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 750 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 751 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 752 | br_fdb_delete_by_port(br, NULL, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 753 | |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 754 | vlan_tunnel_info_del(vg, v); |
| 755 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 756 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void br_vlan_flush(struct net_bridge *br) |
| 760 | { |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 761 | struct net_bridge_vlan_group *vg; |
| 762 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 763 | ASSERT_RTNL(); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 764 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 765 | vg = br_vlan_group(br); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 766 | __vlan_flush(br, NULL, vg); |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 767 | RCU_INIT_POINTER(br->vlgrp, NULL); |
| 768 | synchronize_rcu(); |
| 769 | __vlan_group_free(vg); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 772 | 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] | 773 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 774 | if (!vg) |
| 775 | return NULL; |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 776 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 777 | return br_vlan_lookup(&vg->vlan_hash, vid); |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 778 | } |
| 779 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 780 | /* Must be protected by RTNL. */ |
| 781 | static void recalculate_group_addr(struct net_bridge *br) |
| 782 | { |
Nikolay Aleksandrov | be3664a | 2018-09-26 17:01:02 +0300 | [diff] [blame] | 783 | if (br_opt_get(br, BROPT_GROUP_ADDR_SET)) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 784 | return; |
| 785 | |
| 786 | spin_lock_bh(&br->lock); |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 787 | if (!br_opt_get(br, BROPT_VLAN_ENABLED) || |
| 788 | br->vlan_proto == htons(ETH_P_8021Q)) { |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 789 | /* Bridge Group Address */ |
| 790 | br->group_addr[5] = 0x00; |
| 791 | } else { /* vlan_enabled && ETH_P_8021AD */ |
| 792 | /* Provider Bridge Group Address */ |
| 793 | br->group_addr[5] = 0x08; |
| 794 | } |
| 795 | spin_unlock_bh(&br->lock); |
| 796 | } |
| 797 | |
| 798 | /* Must be protected by RTNL. */ |
| 799 | void br_recalculate_fwd_mask(struct net_bridge *br) |
| 800 | { |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 801 | if (!br_opt_get(br, BROPT_VLAN_ENABLED) || |
| 802 | br->vlan_proto == htons(ETH_P_8021Q)) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 803 | br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; |
| 804 | else /* vlan_enabled && ETH_P_8021AD */ |
| 805 | br->group_fwd_mask_required = BR_GROUPFWD_8021AD & |
| 806 | ~(1u << br->group_addr[5]); |
| 807 | } |
| 808 | |
Vladimir Oltean | 9e78140 | 2021-02-13 22:43:16 +0200 | [diff] [blame] | 809 | int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val, |
| 810 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 811 | { |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 812 | struct switchdev_attr attr = { |
| 813 | .orig_dev = br->dev, |
| 814 | .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, |
| 815 | .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP, |
| 816 | .u.vlan_filtering = val, |
| 817 | }; |
| 818 | int err; |
| 819 | |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 820 | if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val) |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 821 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 822 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 823 | err = switchdev_port_attr_set(br->dev, &attr, extack); |
Elad Raz | 6b72a77 | 2016-01-06 13:01:06 +0100 | [diff] [blame] | 824 | if (err && err != -EOPNOTSUPP) |
| 825 | return err; |
| 826 | |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 827 | br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val); |
Vlad Yasevich | 2796d0c | 2014-05-16 09:59:20 -0400 | [diff] [blame] | 828 | br_manage_promisc(br); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 829 | recalculate_group_addr(br); |
| 830 | br_recalculate_fwd_mask(br); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 831 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
Ido Schimmel | 1f51445 | 2017-05-26 08:37:23 +0200 | [diff] [blame] | 835 | bool br_vlan_enabled(const struct net_device *dev) |
| 836 | { |
| 837 | struct net_bridge *br = netdev_priv(dev); |
| 838 | |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 839 | return br_opt_get(br, BROPT_VLAN_ENABLED); |
Ido Schimmel | 1f51445 | 2017-05-26 08:37:23 +0200 | [diff] [blame] | 840 | } |
| 841 | EXPORT_SYMBOL_GPL(br_vlan_enabled); |
| 842 | |
wenxu | 31aed46 | 2019-07-05 21:16:36 +0800 | [diff] [blame] | 843 | int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto) |
| 844 | { |
| 845 | struct net_bridge *br = netdev_priv(dev); |
| 846 | |
| 847 | *p_proto = ntohs(br->vlan_proto); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(br_vlan_get_proto); |
| 852 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 853 | int __br_vlan_set_proto(struct net_bridge *br, __be16 proto, |
| 854 | struct netlink_ext_ack *extack) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 855 | { |
Danielle Ratson | 22ec19f3 | 2020-11-29 14:54:05 +0200 | [diff] [blame] | 856 | struct switchdev_attr attr = { |
| 857 | .orig_dev = br->dev, |
| 858 | .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL, |
| 859 | .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP, |
| 860 | .u.vlan_protocol = ntohs(proto), |
| 861 | }; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 862 | int err = 0; |
| 863 | struct net_bridge_port *p; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 864 | struct net_bridge_vlan *vlan; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 865 | struct net_bridge_vlan_group *vg; |
Danielle Ratson | 22ec19f3 | 2020-11-29 14:54:05 +0200 | [diff] [blame] | 866 | __be16 oldproto = br->vlan_proto; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 867 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 868 | if (br->vlan_proto == proto) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 869 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 870 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 871 | err = switchdev_port_attr_set(br->dev, &attr, extack); |
Danielle Ratson | 22ec19f3 | 2020-11-29 14:54:05 +0200 | [diff] [blame] | 872 | if (err && err != -EOPNOTSUPP) |
| 873 | return err; |
| 874 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 875 | /* Add VLANs for the new proto to the device filter. */ |
| 876 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 877 | vg = nbp_vlan_group(p); |
| 878 | list_for_each_entry(vlan, &vg->vlan_list, vlist) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 879 | err = vlan_vid_add(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 880 | if (err) |
| 881 | goto err_filt; |
| 882 | } |
| 883 | } |
| 884 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 885 | br->vlan_proto = proto; |
| 886 | |
| 887 | recalculate_group_addr(br); |
| 888 | br_recalculate_fwd_mask(br); |
| 889 | |
| 890 | /* Delete VLANs for the old proto from the device filter. */ |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 891 | list_for_each_entry(p, &br->port_list, list) { |
| 892 | vg = nbp_vlan_group(p); |
| 893 | list_for_each_entry(vlan, &vg->vlan_list, vlist) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 894 | vlan_vid_del(p->dev, oldproto, vlan->vid); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 895 | } |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 896 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 897 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 898 | |
| 899 | err_filt: |
Danielle Ratson | 22ec19f3 | 2020-11-29 14:54:05 +0200 | [diff] [blame] | 900 | attr.u.vlan_protocol = ntohs(oldproto); |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 901 | switchdev_port_attr_set(br->dev, &attr, NULL); |
Danielle Ratson | 22ec19f3 | 2020-11-29 14:54:05 +0200 | [diff] [blame] | 902 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 903 | list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 904 | vlan_vid_del(p->dev, proto, vlan->vid); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 905 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 906 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 907 | vg = nbp_vlan_group(p); |
| 908 | list_for_each_entry(vlan, &vg->vlan_list, vlist) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 909 | vlan_vid_del(p->dev, proto, vlan->vid); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 910 | } |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 911 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 912 | return err; |
| 913 | } |
| 914 | |
Vladimir Oltean | 9e78140 | 2021-02-13 22:43:16 +0200 | [diff] [blame] | 915 | int br_vlan_set_proto(struct net_bridge *br, unsigned long val, |
| 916 | struct netlink_ext_ack *extack) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 917 | { |
Menglong Dong | a98c0c4 | 2021-01-17 16:09:50 +0800 | [diff] [blame] | 918 | if (!eth_type_vlan(htons(val))) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 919 | return -EPROTONOSUPPORT; |
| 920 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 921 | return __br_vlan_set_proto(br, htons(val), extack); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 922 | } |
| 923 | |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 924 | int br_vlan_set_stats(struct net_bridge *br, unsigned long val) |
| 925 | { |
| 926 | switch (val) { |
| 927 | case 0: |
| 928 | case 1: |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 929 | br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val); |
Nikolay Aleksandrov | 6dada9b | 2016-04-30 10:25:28 +0200 | [diff] [blame] | 930 | break; |
| 931 | default: |
| 932 | return -EINVAL; |
| 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
Nikolay Aleksandrov | 9163a0f | 2018-10-12 13:41:16 +0300 | [diff] [blame] | 938 | int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val) |
| 939 | { |
| 940 | struct net_bridge_port *p; |
| 941 | |
| 942 | /* allow to change the option if there are no port vlans configured */ |
| 943 | list_for_each_entry(p, &br->port_list, list) { |
| 944 | struct net_bridge_vlan_group *vg = nbp_vlan_group(p); |
| 945 | |
| 946 | if (vg->num_vlans) |
| 947 | return -EBUSY; |
| 948 | } |
| 949 | |
| 950 | switch (val) { |
| 951 | case 0: |
| 952 | case 1: |
| 953 | br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val); |
| 954 | break; |
| 955 | default: |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 962 | 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] | 963 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 964 | struct net_bridge_vlan *v; |
| 965 | |
Nikolay Aleksandrov | 77751ee | 2015-09-30 20:16:53 +0200 | [diff] [blame] | 966 | if (vid != vg->pvid) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 967 | return false; |
| 968 | |
| 969 | v = br_vlan_lookup(&vg->vlan_hash, vid); |
| 970 | if (v && br_vlan_should_use(v) && |
| 971 | (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)) |
| 972 | return true; |
| 973 | |
| 974 | return false; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void br_vlan_disable_default_pvid(struct net_bridge *br) |
| 978 | { |
| 979 | struct net_bridge_port *p; |
| 980 | u16 pvid = br->default_pvid; |
| 981 | |
| 982 | /* Disable default_pvid on all ports where it is still |
| 983 | * configured. |
| 984 | */ |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 985 | if (vlan_default_pvid(br_vlan_group(br), pvid)) { |
| 986 | if (!br_vlan_delete(br, pvid)) |
| 987 | br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN); |
| 988 | } |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 989 | |
| 990 | list_for_each_entry(p, &br->port_list, list) { |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 991 | if (vlan_default_pvid(nbp_vlan_group(p), pvid) && |
| 992 | !nbp_vlan_delete(p, pvid)) |
| 993 | br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | br->default_pvid = 0; |
| 997 | } |
| 998 | |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 999 | int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, |
| 1000 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1001 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1002 | const struct net_bridge_vlan *pvent; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1003 | struct net_bridge_vlan_group *vg; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1004 | struct net_bridge_port *p; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1005 | unsigned long *changed; |
| 1006 | bool vlchange; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1007 | u16 old_pvid; |
| 1008 | int err = 0; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1009 | |
Nikolay Aleksandrov | 0f963b7 | 2015-10-04 14:23:47 +0200 | [diff] [blame] | 1010 | if (!pvid) { |
| 1011 | br_vlan_disable_default_pvid(br); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Andy Shevchenko | 459479d | 2018-08-30 13:33:18 +0300 | [diff] [blame] | 1015 | changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1016 | if (!changed) |
| 1017 | return -ENOMEM; |
| 1018 | |
| 1019 | old_pvid = br->default_pvid; |
| 1020 | |
| 1021 | /* Update default_pvid config only if we do not conflict with |
| 1022 | * user configuration. |
| 1023 | */ |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1024 | vg = br_vlan_group(br); |
| 1025 | pvent = br_vlan_find(vg, pvid); |
| 1026 | if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1027 | (!pvent || !br_vlan_should_use(pvent))) { |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1028 | err = br_vlan_add(br, pvid, |
| 1029 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1030 | BRIDGE_VLAN_INFO_UNTAGGED | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1031 | BRIDGE_VLAN_INFO_BRENTRY, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1032 | &vlchange, extack); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1033 | if (err) |
| 1034 | goto out; |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1035 | |
| 1036 | if (br_vlan_delete(br, old_pvid)) |
| 1037 | br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN); |
| 1038 | br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1039 | set_bit(0, changed); |
| 1040 | } |
| 1041 | |
| 1042 | list_for_each_entry(p, &br->port_list, list) { |
| 1043 | /* Update default_pvid config only if we do not conflict with |
| 1044 | * user configuration. |
| 1045 | */ |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1046 | vg = nbp_vlan_group(p); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1047 | if ((old_pvid && |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1048 | !vlan_default_pvid(vg, old_pvid)) || |
| 1049 | br_vlan_find(vg, pvid)) |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1050 | continue; |
| 1051 | |
| 1052 | err = nbp_vlan_add(p, pvid, |
| 1053 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1054 | BRIDGE_VLAN_INFO_UNTAGGED, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1055 | &vlchange, extack); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1056 | if (err) |
| 1057 | goto err_port; |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1058 | if (nbp_vlan_delete(p, old_pvid)) |
| 1059 | br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN); |
| 1060 | br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1061 | set_bit(p->port_no, changed); |
| 1062 | } |
| 1063 | |
| 1064 | br->default_pvid = pvid; |
| 1065 | |
| 1066 | out: |
Andy Shevchenko | 459479d | 2018-08-30 13:33:18 +0300 | [diff] [blame] | 1067 | bitmap_free(changed); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1068 | return err; |
| 1069 | |
| 1070 | err_port: |
| 1071 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 1072 | if (!test_bit(p->port_no, changed)) |
| 1073 | continue; |
| 1074 | |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1075 | if (old_pvid) { |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1076 | nbp_vlan_add(p, old_pvid, |
| 1077 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1078 | BRIDGE_VLAN_INFO_UNTAGGED, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1079 | &vlchange, NULL); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1080 | br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN); |
| 1081 | } |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1082 | nbp_vlan_delete(p, pvid); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1083 | br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | if (test_bit(0, changed)) { |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1087 | if (old_pvid) { |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1088 | br_vlan_add(br, old_pvid, |
| 1089 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1090 | BRIDGE_VLAN_INFO_UNTAGGED | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1091 | BRIDGE_VLAN_INFO_BRENTRY, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1092 | &vlchange, NULL); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1093 | br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN); |
| 1094 | } |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1095 | br_vlan_delete(br, pvid); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1096 | br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1097 | } |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
Vladimir Oltean | 9e78140 | 2021-02-13 22:43:16 +0200 | [diff] [blame] | 1101 | int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val, |
| 1102 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1103 | { |
| 1104 | u16 pvid = val; |
| 1105 | int err = 0; |
| 1106 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1107 | if (val >= VLAN_VID_MASK) |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1108 | return -EINVAL; |
| 1109 | |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1110 | if (pvid == br->default_pvid) |
Xin Long | 047831a | 2016-04-09 00:03:31 +0800 | [diff] [blame] | 1111 | goto out; |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1112 | |
| 1113 | /* Only allow default pvid change when filtering is disabled */ |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 1114 | if (br_opt_get(br, BROPT_VLAN_ENABLED)) { |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1115 | pr_info_once("Please disable vlan filtering to change default_pvid\n"); |
| 1116 | err = -EPERM; |
Xin Long | 047831a | 2016-04-09 00:03:31 +0800 | [diff] [blame] | 1117 | goto out; |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1118 | } |
Vladimir Oltean | 9e78140 | 2021-02-13 22:43:16 +0200 | [diff] [blame] | 1119 | err = __br_vlan_set_default_pvid(br, pvid, extack); |
Xin Long | 047831a | 2016-04-09 00:03:31 +0800 | [diff] [blame] | 1120 | out: |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1121 | return err; |
| 1122 | } |
| 1123 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1124 | int br_vlan_init(struct net_bridge *br) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 1125 | { |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1126 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1127 | int ret = -ENOMEM; |
| 1128 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1129 | vg = kzalloc(sizeof(*vg), GFP_KERNEL); |
| 1130 | if (!vg) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1131 | goto out; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1132 | ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1133 | if (ret) |
| 1134 | goto err_rhtbl; |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 1135 | ret = vlan_tunnel_init(vg); |
| 1136 | if (ret) |
| 1137 | goto err_tunnel_init; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1138 | INIT_LIST_HEAD(&vg->vlan_list); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 1139 | br->vlan_proto = htons(ETH_P_8021Q); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 1140 | br->default_pvid = 1; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1141 | rcu_assign_pointer(br->vlgrp, vg); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1142 | |
| 1143 | out: |
| 1144 | return ret; |
| 1145 | |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 1146 | err_tunnel_init: |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1147 | rhashtable_destroy(&vg->vlan_hash); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1148 | err_rhtbl: |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1149 | kfree(vg); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1150 | |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1154 | int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1155 | { |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 1156 | struct switchdev_attr attr = { |
| 1157 | .orig_dev = p->br->dev, |
| 1158 | .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, |
| 1159 | .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP, |
Nikolay Aleksandrov | ae75767 | 2018-09-26 17:01:00 +0300 | [diff] [blame] | 1160 | .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED), |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 1161 | }; |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 1162 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1163 | int ret = -ENOMEM; |
| 1164 | |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 1165 | vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL); |
| 1166 | if (!vg) |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1167 | goto out; |
| 1168 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 1169 | ret = switchdev_port_attr_set(p->dev, &attr, extack); |
Elad Raz | 404cdbf | 2016-01-06 13:01:07 +0100 | [diff] [blame] | 1170 | if (ret && ret != -EOPNOTSUPP) |
| 1171 | goto err_vlan_enabled; |
| 1172 | |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 1173 | ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1174 | if (ret) |
| 1175 | goto err_rhtbl; |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 1176 | ret = vlan_tunnel_init(vg); |
| 1177 | if (ret) |
| 1178 | goto err_tunnel_init; |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 1179 | INIT_LIST_HEAD(&vg->vlan_list); |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1180 | rcu_assign_pointer(p->vlgrp, vg); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1181 | if (p->br->default_pvid) { |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1182 | bool changed; |
| 1183 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1184 | ret = nbp_vlan_add(p, p->br->default_pvid, |
| 1185 | BRIDGE_VLAN_INFO_PVID | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1186 | BRIDGE_VLAN_INFO_UNTAGGED, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1187 | &changed, extack); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1188 | if (ret) |
| 1189 | goto err_vlan_add; |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1190 | br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1191 | } |
| 1192 | out: |
| 1193 | return ret; |
| 1194 | |
| 1195 | err_vlan_add: |
Ido Schimmel | 07bc588 | 2015-10-30 17:46:19 +0100 | [diff] [blame] | 1196 | RCU_INIT_POINTER(p->vlgrp, NULL); |
| 1197 | synchronize_rcu(); |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 1198 | vlan_tunnel_deinit(vg); |
Roopa Prabhu | efa5356 | 2017-01-31 22:59:54 -0800 | [diff] [blame] | 1199 | err_tunnel_init: |
| 1200 | rhashtable_destroy(&vg->vlan_hash); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1201 | err_rhtbl: |
Yotam Gigi | df2c433 | 2017-03-01 16:50:45 +0200 | [diff] [blame] | 1202 | err_vlan_enabled: |
Nikolay Aleksandrov | 263344e | 2015-09-30 20:16:54 +0200 | [diff] [blame] | 1203 | kfree(vg); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1204 | |
| 1205 | goto out; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 1206 | } |
| 1207 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 1208 | /* Must be protected by RTNL. |
| 1209 | * Must be called with vid in range from 1 to 4094 inclusive. |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1210 | * changed must be true only if the vlan was created or updated |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 1211 | */ |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1212 | int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1213 | bool *changed, struct netlink_ext_ack *extack) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1214 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1215 | struct net_bridge_vlan *vlan; |
| 1216 | int ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1217 | |
| 1218 | ASSERT_RTNL(); |
| 1219 | |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1220 | *changed = false; |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1221 | vlan = br_vlan_find(nbp_vlan_group(port), vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1222 | if (vlan) { |
Ido Schimmel | 7fbac98 | 2016-02-18 14:01:46 +0100 | [diff] [blame] | 1223 | /* Pass the flags to the hardware bridge */ |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1224 | ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack); |
Ido Schimmel | 7fbac98 | 2016-02-18 14:01:46 +0100 | [diff] [blame] | 1225 | if (ret && ret != -EOPNOTSUPP) |
| 1226 | return ret; |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1227 | *changed = __vlan_add_flags(vlan, flags); |
| 1228 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1229 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1232 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 1233 | if (!vlan) |
| 1234 | return -ENOMEM; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1235 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1236 | vlan->vid = vid; |
| 1237 | vlan->port = port; |
Petr Machata | 169327d | 2018-12-12 17:02:50 +0000 | [diff] [blame] | 1238 | ret = __vlan_add(vlan, flags, extack); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1239 | if (ret) |
| 1240 | kfree(vlan); |
Nikolay Aleksandrov | f418af6 | 2017-10-27 13:19:37 +0300 | [diff] [blame] | 1241 | else |
| 1242 | *changed = true; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1243 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1244 | return ret; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 1247 | /* Must be protected by RTNL. |
| 1248 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 1249 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1250 | int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) |
| 1251 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1252 | struct net_bridge_vlan *v; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1253 | |
| 1254 | ASSERT_RTNL(); |
| 1255 | |
Nikolay Aleksandrov | 907b1e6 | 2015-10-12 21:47:02 +0200 | [diff] [blame] | 1256 | v = br_vlan_find(nbp_vlan_group(port), vid); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1257 | if (!v) |
| 1258 | return -ENOENT; |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 1259 | 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] | 1260 | br_fdb_delete_by_port(port->br, port, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 1261 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1262 | return __vlan_del(v); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | void nbp_vlan_flush(struct net_bridge_port *port) |
| 1266 | { |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 1267 | struct net_bridge_vlan_group *vg; |
| 1268 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 1269 | ASSERT_RTNL(); |
| 1270 | |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 1271 | vg = nbp_vlan_group(port); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1272 | __vlan_flush(port->br, port, vg); |
Nikolay Aleksandrov | f409d0e | 2015-10-12 21:47:05 +0200 | [diff] [blame] | 1273 | RCU_INIT_POINTER(port->vlgrp, NULL); |
| 1274 | synchronize_rcu(); |
| 1275 | __vlan_group_free(vg); |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 1276 | } |
Nikolay Aleksandrov | a60c090 | 2016-04-30 10:25:29 +0200 | [diff] [blame] | 1277 | |
| 1278 | void br_vlan_get_stats(const struct net_bridge_vlan *v, |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 1279 | struct pcpu_sw_netstats *stats) |
Nikolay Aleksandrov | a60c090 | 2016-04-30 10:25:29 +0200 | [diff] [blame] | 1280 | { |
| 1281 | int i; |
| 1282 | |
| 1283 | memset(stats, 0, sizeof(*stats)); |
| 1284 | for_each_possible_cpu(i) { |
| 1285 | u64 rxpackets, rxbytes, txpackets, txbytes; |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 1286 | struct pcpu_sw_netstats *cpu_stats; |
Nikolay Aleksandrov | a60c090 | 2016-04-30 10:25:29 +0200 | [diff] [blame] | 1287 | unsigned int start; |
| 1288 | |
| 1289 | cpu_stats = per_cpu_ptr(v->stats, i); |
| 1290 | do { |
| 1291 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 1292 | rxpackets = cpu_stats->rx_packets; |
| 1293 | rxbytes = cpu_stats->rx_bytes; |
| 1294 | txbytes = cpu_stats->tx_bytes; |
| 1295 | txpackets = cpu_stats->tx_packets; |
| 1296 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 1297 | |
| 1298 | stats->rx_packets += rxpackets; |
| 1299 | stats->rx_bytes += rxbytes; |
| 1300 | stats->tx_bytes += txbytes; |
| 1301 | stats->tx_packets += txpackets; |
| 1302 | } |
| 1303 | } |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1304 | |
Vladimir Oltean | 99f62a7 | 2020-09-22 01:07:09 +0300 | [diff] [blame] | 1305 | int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid) |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1306 | { |
| 1307 | struct net_bridge_vlan_group *vg; |
Vladimir Oltean | 99f62a7 | 2020-09-22 01:07:09 +0300 | [diff] [blame] | 1308 | struct net_bridge_port *p; |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1309 | |
Vladimir Oltean | 99f62a7 | 2020-09-22 01:07:09 +0300 | [diff] [blame] | 1310 | ASSERT_RTNL(); |
| 1311 | p = br_port_get_check_rtnl(dev); |
Ido Schimmel | 5a6db04 | 2018-11-28 20:06:58 +0000 | [diff] [blame] | 1312 | if (p) |
| 1313 | vg = nbp_vlan_group(p); |
| 1314 | else if (netif_is_bridge_master(dev)) |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1315 | vg = br_vlan_group(netdev_priv(dev)); |
| 1316 | else |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | *p_pvid = br_get_pvid(vg); |
| 1320 | return 0; |
| 1321 | } |
| 1322 | EXPORT_SYMBOL_GPL(br_vlan_get_pvid); |
| 1323 | |
Pablo Neira Ayuso | 7582f5b | 2019-07-05 21:16:34 +0800 | [diff] [blame] | 1324 | int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid) |
| 1325 | { |
Vladimir Oltean | 99f62a7 | 2020-09-22 01:07:09 +0300 | [diff] [blame] | 1326 | struct net_bridge_vlan_group *vg; |
| 1327 | struct net_bridge_port *p; |
| 1328 | |
| 1329 | p = br_port_get_check_rcu(dev); |
| 1330 | if (p) |
| 1331 | vg = nbp_vlan_group_rcu(p); |
| 1332 | else if (netif_is_bridge_master(dev)) |
| 1333 | vg = br_vlan_group_rcu(netdev_priv(dev)); |
| 1334 | else |
| 1335 | return -EINVAL; |
| 1336 | |
| 1337 | *p_pvid = br_get_pvid(vg); |
| 1338 | return 0; |
Pablo Neira Ayuso | 7582f5b | 2019-07-05 21:16:34 +0800 | [diff] [blame] | 1339 | } |
| 1340 | EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu); |
| 1341 | |
Felix Fietkau | bcf2766 | 2021-03-24 02:30:35 +0100 | [diff] [blame] | 1342 | void br_vlan_fill_forward_path_pvid(struct net_bridge *br, |
| 1343 | struct net_device_path_ctx *ctx, |
| 1344 | struct net_device_path *path) |
| 1345 | { |
| 1346 | struct net_bridge_vlan_group *vg; |
| 1347 | int idx = ctx->num_vlans - 1; |
| 1348 | u16 vid; |
| 1349 | |
| 1350 | path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP; |
| 1351 | |
| 1352 | if (!br_opt_get(br, BROPT_VLAN_ENABLED)) |
| 1353 | return; |
| 1354 | |
| 1355 | vg = br_vlan_group(br); |
| 1356 | |
| 1357 | if (idx >= 0 && |
| 1358 | ctx->vlan[idx].proto == br->vlan_proto) { |
| 1359 | vid = ctx->vlan[idx].id; |
| 1360 | } else { |
| 1361 | path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG; |
| 1362 | vid = br_get_pvid(vg); |
| 1363 | } |
| 1364 | |
| 1365 | path->bridge.vlan_id = vid; |
| 1366 | path->bridge.vlan_proto = br->vlan_proto; |
| 1367 | } |
| 1368 | |
| 1369 | int br_vlan_fill_forward_path_mode(struct net_bridge *br, |
| 1370 | struct net_bridge_port *dst, |
| 1371 | struct net_device_path *path) |
| 1372 | { |
| 1373 | struct net_bridge_vlan_group *vg; |
| 1374 | struct net_bridge_vlan *v; |
| 1375 | |
| 1376 | if (!br_opt_get(br, BROPT_VLAN_ENABLED)) |
| 1377 | return 0; |
| 1378 | |
| 1379 | vg = nbp_vlan_group_rcu(dst); |
| 1380 | v = br_vlan_find(vg, path->bridge.vlan_id); |
| 1381 | if (!v || !br_vlan_should_use(v)) |
| 1382 | return -EINVAL; |
| 1383 | |
| 1384 | if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED)) |
| 1385 | return 0; |
| 1386 | |
| 1387 | if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG) |
| 1388 | path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP; |
Felix Fietkau | 26267bf | 2021-03-24 02:30:48 +0100 | [diff] [blame] | 1389 | else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV) |
| 1390 | path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW; |
Felix Fietkau | bcf2766 | 2021-03-24 02:30:35 +0100 | [diff] [blame] | 1391 | else |
| 1392 | path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG; |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1397 | int br_vlan_get_info(const struct net_device *dev, u16 vid, |
| 1398 | struct bridge_vlan_info *p_vinfo) |
| 1399 | { |
| 1400 | struct net_bridge_vlan_group *vg; |
| 1401 | struct net_bridge_vlan *v; |
| 1402 | struct net_bridge_port *p; |
| 1403 | |
| 1404 | ASSERT_RTNL(); |
| 1405 | p = br_port_get_check_rtnl(dev); |
| 1406 | if (p) |
| 1407 | vg = nbp_vlan_group(p); |
Petr Machata | 2b18d79 | 2018-05-10 13:13:03 +0300 | [diff] [blame] | 1408 | else if (netif_is_bridge_master(dev)) |
| 1409 | vg = br_vlan_group(netdev_priv(dev)); |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1410 | else |
| 1411 | return -EINVAL; |
| 1412 | |
| 1413 | v = br_vlan_find(vg, vid); |
| 1414 | if (!v) |
| 1415 | return -ENOENT; |
| 1416 | |
| 1417 | p_vinfo->vid = vid; |
| 1418 | p_vinfo->flags = v->flags; |
Vladimir Oltean | f40d9b20 | 2019-08-30 03:53:24 +0300 | [diff] [blame] | 1419 | if (vid == br_get_pvid(vg)) |
| 1420 | p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID; |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | EXPORT_SYMBOL_GPL(br_vlan_get_info); |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1424 | |
| 1425 | static int br_vlan_is_bind_vlan_dev(const struct net_device *dev) |
| 1426 | { |
| 1427 | return is_vlan_dev(dev) && |
| 1428 | !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING); |
| 1429 | } |
| 1430 | |
| 1431 | static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev, |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1432 | __always_unused struct netdev_nested_priv *priv) |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1433 | { |
| 1434 | return br_vlan_is_bind_vlan_dev(dev); |
| 1435 | } |
| 1436 | |
| 1437 | static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev) |
| 1438 | { |
| 1439 | int found; |
| 1440 | |
| 1441 | rcu_read_lock(); |
| 1442 | found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn, |
| 1443 | NULL); |
| 1444 | rcu_read_unlock(); |
| 1445 | |
| 1446 | return !!found; |
| 1447 | } |
| 1448 | |
| 1449 | struct br_vlan_bind_walk_data { |
| 1450 | u16 vid; |
| 1451 | struct net_device *result; |
| 1452 | }; |
| 1453 | |
| 1454 | static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev, |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1455 | struct netdev_nested_priv *priv) |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1456 | { |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1457 | struct br_vlan_bind_walk_data *data = priv->data; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1458 | int found = 0; |
| 1459 | |
| 1460 | if (br_vlan_is_bind_vlan_dev(dev) && |
| 1461 | vlan_dev_priv(dev)->vlan_id == data->vid) { |
| 1462 | data->result = dev; |
| 1463 | found = 1; |
| 1464 | } |
| 1465 | |
| 1466 | return found; |
| 1467 | } |
| 1468 | |
| 1469 | static struct net_device * |
| 1470 | br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid) |
| 1471 | { |
| 1472 | struct br_vlan_bind_walk_data data = { |
| 1473 | .vid = vid, |
| 1474 | }; |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1475 | struct netdev_nested_priv priv = { |
| 1476 | .data = (void *)&data, |
| 1477 | }; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1478 | |
| 1479 | rcu_read_lock(); |
| 1480 | netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn, |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1481 | &priv); |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1482 | rcu_read_unlock(); |
| 1483 | |
| 1484 | return data.result; |
| 1485 | } |
| 1486 | |
| 1487 | static bool br_vlan_is_dev_up(const struct net_device *dev) |
| 1488 | { |
| 1489 | return !!(dev->flags & IFF_UP) && netif_oper_up(dev); |
| 1490 | } |
| 1491 | |
| 1492 | static void br_vlan_set_vlan_dev_state(const struct net_bridge *br, |
| 1493 | struct net_device *vlan_dev) |
| 1494 | { |
| 1495 | u16 vid = vlan_dev_priv(vlan_dev)->vlan_id; |
| 1496 | struct net_bridge_vlan_group *vg; |
| 1497 | struct net_bridge_port *p; |
| 1498 | bool has_carrier = false; |
| 1499 | |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1500 | if (!netif_carrier_ok(br->dev)) { |
| 1501 | netif_carrier_off(vlan_dev); |
| 1502 | return; |
| 1503 | } |
| 1504 | |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1505 | list_for_each_entry(p, &br->port_list, list) { |
| 1506 | vg = nbp_vlan_group(p); |
| 1507 | if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) { |
| 1508 | has_carrier = true; |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | if (has_carrier) |
| 1514 | netif_carrier_on(vlan_dev); |
| 1515 | else |
| 1516 | netif_carrier_off(vlan_dev); |
| 1517 | } |
| 1518 | |
| 1519 | static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p) |
| 1520 | { |
| 1521 | struct net_bridge_vlan_group *vg = nbp_vlan_group(p); |
| 1522 | struct net_bridge_vlan *vlan; |
| 1523 | struct net_device *vlan_dev; |
| 1524 | |
| 1525 | list_for_each_entry(vlan, &vg->vlan_list, vlist) { |
| 1526 | vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, |
| 1527 | vlan->vid); |
| 1528 | if (vlan_dev) { |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1529 | if (br_vlan_is_dev_up(p->dev)) { |
| 1530 | if (netif_carrier_ok(p->br->dev)) |
| 1531 | netif_carrier_on(vlan_dev); |
| 1532 | } else { |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1533 | br_vlan_set_vlan_dev_state(p->br, vlan_dev); |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1534 | } |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | static void br_vlan_upper_change(struct net_device *dev, |
| 1540 | struct net_device *upper_dev, |
| 1541 | bool linking) |
| 1542 | { |
| 1543 | struct net_bridge *br = netdev_priv(dev); |
| 1544 | |
| 1545 | if (!br_vlan_is_bind_vlan_dev(upper_dev)) |
| 1546 | return; |
| 1547 | |
| 1548 | if (linking) { |
| 1549 | br_vlan_set_vlan_dev_state(br, upper_dev); |
| 1550 | br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true); |
| 1551 | } else { |
| 1552 | br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, |
| 1553 | br_vlan_has_upper_bind_vlan_dev(dev)); |
| 1554 | } |
| 1555 | } |
| 1556 | |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1557 | struct br_vlan_link_state_walk_data { |
| 1558 | struct net_bridge *br; |
| 1559 | }; |
| 1560 | |
| 1561 | static int br_vlan_link_state_change_fn(struct net_device *vlan_dev, |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1562 | struct netdev_nested_priv *priv) |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1563 | { |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1564 | struct br_vlan_link_state_walk_data *data = priv->data; |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1565 | |
| 1566 | if (br_vlan_is_bind_vlan_dev(vlan_dev)) |
| 1567 | br_vlan_set_vlan_dev_state(data->br, vlan_dev); |
| 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static void br_vlan_link_state_change(struct net_device *dev, |
| 1573 | struct net_bridge *br) |
| 1574 | { |
| 1575 | struct br_vlan_link_state_walk_data data = { |
| 1576 | .br = br |
| 1577 | }; |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1578 | struct netdev_nested_priv priv = { |
| 1579 | .data = (void *)&data, |
| 1580 | }; |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1581 | |
| 1582 | rcu_read_lock(); |
| 1583 | netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn, |
Taehee Yoo | eff7423 | 2020-09-25 18:13:12 +0000 | [diff] [blame] | 1584 | &priv); |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1585 | rcu_read_unlock(); |
| 1586 | } |
| 1587 | |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1588 | /* Must be protected by RTNL. */ |
Mike Manning | 80900ac | 2019-04-18 18:35:34 +0100 | [diff] [blame] | 1589 | static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid) |
| 1590 | { |
| 1591 | struct net_device *vlan_dev; |
| 1592 | |
| 1593 | if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING)) |
| 1594 | return; |
| 1595 | |
| 1596 | vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid); |
| 1597 | if (vlan_dev) |
| 1598 | br_vlan_set_vlan_dev_state(p->br, vlan_dev); |
| 1599 | } |
| 1600 | |
| 1601 | /* Must be protected by RTNL. */ |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1602 | int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr) |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1603 | { |
| 1604 | struct netdev_notifier_changeupper_info *info; |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1605 | struct net_bridge *br = netdev_priv(dev); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1606 | int vlcmd = 0, ret = 0; |
| 1607 | bool changed = false; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1608 | |
| 1609 | switch (event) { |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1610 | case NETDEV_REGISTER: |
| 1611 | ret = br_vlan_add(br, br->default_pvid, |
| 1612 | BRIDGE_VLAN_INFO_PVID | |
| 1613 | BRIDGE_VLAN_INFO_UNTAGGED | |
| 1614 | BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL); |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1615 | vlcmd = RTM_NEWVLAN; |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1616 | break; |
| 1617 | case NETDEV_UNREGISTER: |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1618 | changed = !br_vlan_delete(br, br->default_pvid); |
| 1619 | vlcmd = RTM_DELVLAN; |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1620 | break; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1621 | case NETDEV_CHANGEUPPER: |
| 1622 | info = ptr; |
| 1623 | br_vlan_upper_change(dev, info->upper_dev, info->linking); |
| 1624 | break; |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1625 | |
| 1626 | case NETDEV_CHANGE: |
| 1627 | case NETDEV_UP: |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1628 | if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING)) |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1629 | break; |
Mike Manning | 8e1acd4 | 2019-04-18 18:35:35 +0100 | [diff] [blame] | 1630 | br_vlan_link_state_change(dev, br); |
| 1631 | break; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1632 | } |
Nikolay Aleksandrov | f545923 | 2020-01-14 19:56:14 +0200 | [diff] [blame] | 1633 | if (changed) |
| 1634 | br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd); |
Nikolay Aleksandrov | 091adf9 | 2019-08-02 13:57:36 +0300 | [diff] [blame] | 1635 | |
| 1636 | return ret; |
Mike Manning | 9c0ec2e | 2019-04-18 18:35:33 +0100 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | /* Must be protected by RTNL. */ |
| 1640 | void br_vlan_port_event(struct net_bridge_port *p, unsigned long event) |
| 1641 | { |
| 1642 | if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING)) |
| 1643 | return; |
| 1644 | |
| 1645 | switch (event) { |
| 1646 | case NETDEV_CHANGE: |
| 1647 | case NETDEV_DOWN: |
| 1648 | case NETDEV_UP: |
| 1649 | br_vlan_set_all_vlan_dev_state(p); |
| 1650 | break; |
| 1651 | } |
| 1652 | } |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1653 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1654 | static bool br_vlan_stats_fill(struct sk_buff *skb, |
| 1655 | const struct net_bridge_vlan *v) |
| 1656 | { |
Heiner Kallweit | 281cc28 | 2020-11-17 21:25:42 +0100 | [diff] [blame] | 1657 | struct pcpu_sw_netstats stats; |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1658 | struct nlattr *nest; |
| 1659 | |
| 1660 | nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS); |
| 1661 | if (!nest) |
| 1662 | return false; |
| 1663 | |
| 1664 | br_vlan_get_stats(v, &stats); |
| 1665 | if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes, |
| 1666 | BRIDGE_VLANDB_STATS_PAD) || |
| 1667 | nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS, |
| 1668 | stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) || |
| 1669 | nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes, |
| 1670 | BRIDGE_VLANDB_STATS_PAD) || |
| 1671 | nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS, |
| 1672 | stats.tx_packets, BRIDGE_VLANDB_STATS_PAD)) |
| 1673 | goto out_err; |
| 1674 | |
| 1675 | nla_nest_end(skb, nest); |
| 1676 | |
| 1677 | return true; |
| 1678 | |
| 1679 | out_err: |
| 1680 | nla_nest_cancel(skb, nest); |
| 1681 | return false; |
| 1682 | } |
| 1683 | |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1684 | /* v_opts is used to dump the options which must be equal in the whole range */ |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1685 | static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range, |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1686 | const struct net_bridge_vlan *v_opts, |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1687 | u16 flags, |
| 1688 | bool dump_stats) |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1689 | { |
| 1690 | struct bridge_vlan_info info; |
| 1691 | struct nlattr *nest; |
| 1692 | |
| 1693 | nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY); |
| 1694 | if (!nest) |
| 1695 | return false; |
| 1696 | |
| 1697 | memset(&info, 0, sizeof(info)); |
| 1698 | info.vid = vid; |
| 1699 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
| 1700 | info.flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 1701 | if (flags & BRIDGE_VLAN_INFO_PVID) |
| 1702 | info.flags |= BRIDGE_VLAN_INFO_PVID; |
| 1703 | |
| 1704 | if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info)) |
| 1705 | goto out_err; |
| 1706 | |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1707 | if (vid_range && vid < vid_range && |
| 1708 | !(flags & BRIDGE_VLAN_INFO_PVID) && |
| 1709 | nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range)) |
| 1710 | goto out_err; |
| 1711 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1712 | if (v_opts) { |
| 1713 | if (!br_vlan_opts_fill(skb, v_opts)) |
| 1714 | goto out_err; |
| 1715 | |
| 1716 | if (dump_stats && !br_vlan_stats_fill(skb, v_opts)) |
| 1717 | goto out_err; |
| 1718 | } |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1719 | |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1720 | nla_nest_end(skb, nest); |
| 1721 | |
| 1722 | return true; |
| 1723 | |
| 1724 | out_err: |
| 1725 | nla_nest_cancel(skb, nest); |
| 1726 | return false; |
| 1727 | } |
| 1728 | |
Nikolay Aleksandrov | cf5bddb | 2020-01-14 19:56:13 +0200 | [diff] [blame] | 1729 | static size_t rtnl_vlan_nlmsg_size(void) |
| 1730 | { |
| 1731 | return NLMSG_ALIGN(sizeof(struct br_vlan_msg)) |
| 1732 | + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */ |
| 1733 | + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */ |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1734 | + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */ |
| 1735 | + br_vlan_opts_nl_size(); /* bridge vlan options */ |
Nikolay Aleksandrov | cf5bddb | 2020-01-14 19:56:13 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | void br_vlan_notify(const struct net_bridge *br, |
| 1739 | const struct net_bridge_port *p, |
| 1740 | u16 vid, u16 vid_range, |
| 1741 | int cmd) |
| 1742 | { |
| 1743 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1744 | struct net_bridge_vlan *v = NULL; |
Nikolay Aleksandrov | cf5bddb | 2020-01-14 19:56:13 +0200 | [diff] [blame] | 1745 | struct br_vlan_msg *bvm; |
| 1746 | struct nlmsghdr *nlh; |
| 1747 | struct sk_buff *skb; |
| 1748 | int err = -ENOBUFS; |
| 1749 | struct net *net; |
| 1750 | u16 flags = 0; |
| 1751 | int ifindex; |
| 1752 | |
| 1753 | /* right now notifications are done only with rtnl held */ |
| 1754 | ASSERT_RTNL(); |
| 1755 | |
| 1756 | if (p) { |
| 1757 | ifindex = p->dev->ifindex; |
| 1758 | vg = nbp_vlan_group(p); |
| 1759 | net = dev_net(p->dev); |
| 1760 | } else { |
| 1761 | ifindex = br->dev->ifindex; |
| 1762 | vg = br_vlan_group(br); |
| 1763 | net = dev_net(br->dev); |
| 1764 | } |
| 1765 | |
| 1766 | skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL); |
| 1767 | if (!skb) |
| 1768 | goto out_err; |
| 1769 | |
| 1770 | err = -EMSGSIZE; |
| 1771 | nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0); |
| 1772 | if (!nlh) |
| 1773 | goto out_err; |
| 1774 | bvm = nlmsg_data(nlh); |
| 1775 | memset(bvm, 0, sizeof(*bvm)); |
| 1776 | bvm->family = AF_BRIDGE; |
| 1777 | bvm->ifindex = ifindex; |
| 1778 | |
| 1779 | switch (cmd) { |
| 1780 | case RTM_NEWVLAN: |
| 1781 | /* need to find the vlan due to flags/options */ |
| 1782 | v = br_vlan_find(vg, vid); |
| 1783 | if (!v || !br_vlan_should_use(v)) |
| 1784 | goto out_kfree; |
| 1785 | |
| 1786 | flags = v->flags; |
| 1787 | if (br_get_pvid(vg) == v->vid) |
| 1788 | flags |= BRIDGE_VLAN_INFO_PVID; |
| 1789 | break; |
| 1790 | case RTM_DELVLAN: |
| 1791 | break; |
| 1792 | default: |
| 1793 | goto out_kfree; |
| 1794 | } |
| 1795 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1796 | if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false)) |
Nikolay Aleksandrov | cf5bddb | 2020-01-14 19:56:13 +0200 | [diff] [blame] | 1797 | goto out_err; |
| 1798 | |
| 1799 | nlmsg_end(skb, nlh); |
| 1800 | rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL); |
| 1801 | return; |
| 1802 | |
| 1803 | out_err: |
| 1804 | rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err); |
| 1805 | out_kfree: |
| 1806 | kfree_skb(skb); |
| 1807 | } |
| 1808 | |
Vladimir Oltean | 22f67cd | 2021-03-23 01:51:46 +0200 | [diff] [blame] | 1809 | static int br_vlan_replay_one(struct notifier_block *nb, |
| 1810 | struct net_device *dev, |
| 1811 | struct switchdev_obj_port_vlan *vlan, |
| 1812 | struct netlink_ext_ack *extack) |
| 1813 | { |
| 1814 | struct switchdev_notifier_port_obj_info obj_info = { |
| 1815 | .info = { |
| 1816 | .dev = dev, |
| 1817 | .extack = extack, |
| 1818 | }, |
| 1819 | .obj = &vlan->obj, |
| 1820 | }; |
| 1821 | int err; |
| 1822 | |
| 1823 | err = nb->notifier_call(nb, SWITCHDEV_PORT_OBJ_ADD, &obj_info); |
| 1824 | return notifier_to_errno(err); |
| 1825 | } |
| 1826 | |
| 1827 | int br_vlan_replay(struct net_device *br_dev, struct net_device *dev, |
| 1828 | struct notifier_block *nb, struct netlink_ext_ack *extack) |
| 1829 | { |
| 1830 | struct net_bridge_vlan_group *vg; |
| 1831 | struct net_bridge_vlan *v; |
| 1832 | struct net_bridge_port *p; |
| 1833 | struct net_bridge *br; |
| 1834 | int err = 0; |
| 1835 | u16 pvid; |
| 1836 | |
| 1837 | ASSERT_RTNL(); |
| 1838 | |
| 1839 | if (!netif_is_bridge_master(br_dev)) |
| 1840 | return -EINVAL; |
| 1841 | |
| 1842 | if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) |
| 1843 | return -EINVAL; |
| 1844 | |
| 1845 | if (netif_is_bridge_master(dev)) { |
| 1846 | br = netdev_priv(dev); |
| 1847 | vg = br_vlan_group(br); |
| 1848 | p = NULL; |
| 1849 | } else { |
| 1850 | p = br_port_get_rtnl(dev); |
| 1851 | if (WARN_ON(!p)) |
| 1852 | return -EINVAL; |
| 1853 | vg = nbp_vlan_group(p); |
| 1854 | br = p->br; |
| 1855 | } |
| 1856 | |
| 1857 | if (!vg) |
| 1858 | return 0; |
| 1859 | |
| 1860 | pvid = br_get_pvid(vg); |
| 1861 | |
| 1862 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 1863 | struct switchdev_obj_port_vlan vlan = { |
| 1864 | .obj.orig_dev = dev, |
| 1865 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 1866 | .flags = br_vlan_flags(v, pvid), |
| 1867 | .vid = v->vid, |
| 1868 | }; |
| 1869 | |
| 1870 | if (!br_vlan_should_use(v)) |
| 1871 | continue; |
| 1872 | |
Colin Ian King | ad248f7 | 2021-03-24 15:09:50 +0000 | [diff] [blame] | 1873 | err = br_vlan_replay_one(nb, dev, &vlan, extack); |
Vladimir Oltean | 22f67cd | 2021-03-23 01:51:46 +0200 | [diff] [blame] | 1874 | if (err) |
| 1875 | return err; |
| 1876 | } |
| 1877 | |
| 1878 | return err; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL_GPL(br_vlan_replay); |
| 1881 | |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1882 | /* check if v_curr can enter a range ending in range_end */ |
Nikolay Aleksandrov | a5d29ae | 2020-01-24 13:40:21 +0200 | [diff] [blame] | 1883 | bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr, |
| 1884 | const struct net_bridge_vlan *range_end) |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1885 | { |
| 1886 | return v_curr->vid - range_end->vid == 1 && |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1887 | range_end->flags == v_curr->flags && |
Nikolay Aleksandrov | 99f7c5e | 2020-03-17 14:08:33 +0200 | [diff] [blame] | 1888 | br_vlan_opts_eq_range(v_curr, range_end); |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1891 | static int br_vlan_dump_dev(const struct net_device *dev, |
| 1892 | struct sk_buff *skb, |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1893 | struct netlink_callback *cb, |
| 1894 | u32 dump_flags) |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1895 | { |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1896 | struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL; |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1897 | bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1898 | struct net_bridge_vlan_group *vg; |
| 1899 | int idx = 0, s_idx = cb->args[1]; |
| 1900 | struct nlmsghdr *nlh = NULL; |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1901 | struct net_bridge_port *p; |
| 1902 | struct br_vlan_msg *bvm; |
| 1903 | struct net_bridge *br; |
| 1904 | int err = 0; |
| 1905 | u16 pvid; |
| 1906 | |
| 1907 | if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) |
| 1908 | return -EINVAL; |
| 1909 | |
| 1910 | if (netif_is_bridge_master(dev)) { |
| 1911 | br = netdev_priv(dev); |
| 1912 | vg = br_vlan_group_rcu(br); |
| 1913 | p = NULL; |
| 1914 | } else { |
| 1915 | p = br_port_get_rcu(dev); |
| 1916 | if (WARN_ON(!p)) |
| 1917 | return -EINVAL; |
| 1918 | vg = nbp_vlan_group_rcu(p); |
| 1919 | br = p->br; |
| 1920 | } |
| 1921 | |
| 1922 | if (!vg) |
| 1923 | return 0; |
| 1924 | |
| 1925 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 1926 | RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI); |
| 1927 | if (!nlh) |
| 1928 | return -EMSGSIZE; |
| 1929 | bvm = nlmsg_data(nlh); |
| 1930 | memset(bvm, 0, sizeof(*bvm)); |
| 1931 | bvm->family = PF_BRIDGE; |
| 1932 | bvm->ifindex = dev->ifindex; |
| 1933 | pvid = br_get_pvid(vg); |
| 1934 | |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1935 | /* idx must stay at range's beginning until it is filled in */ |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1936 | list_for_each_entry_rcu(v, &vg->vlan_list, vlist) { |
| 1937 | if (!br_vlan_should_use(v)) |
| 1938 | continue; |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1939 | if (idx < s_idx) { |
| 1940 | idx++; |
| 1941 | continue; |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1942 | } |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1943 | |
| 1944 | if (!range_start) { |
| 1945 | range_start = v; |
| 1946 | range_end = v; |
| 1947 | continue; |
| 1948 | } |
| 1949 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1950 | if (dump_stats || v->vid == pvid || |
| 1951 | !br_vlan_can_enter_range(v, range_end)) { |
| 1952 | u16 vlan_flags = br_vlan_flags(range_start, pvid); |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1953 | |
| 1954 | if (!br_vlan_fill_vids(skb, range_start->vid, |
Nikolay Aleksandrov | 7a53e71 | 2020-01-24 13:40:20 +0200 | [diff] [blame] | 1955 | range_end->vid, range_start, |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1956 | vlan_flags, dump_stats)) { |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1957 | err = -EMSGSIZE; |
| 1958 | break; |
| 1959 | } |
| 1960 | /* advance number of filled vlans */ |
| 1961 | idx += range_end->vid - range_start->vid + 1; |
| 1962 | |
| 1963 | range_start = v; |
| 1964 | } |
| 1965 | range_end = v; |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1966 | } |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1967 | |
| 1968 | /* err will be 0 and range_start will be set in 3 cases here: |
| 1969 | * - first vlan (range_start == range_end) |
| 1970 | * - last vlan (range_start == range_end, not in range) |
| 1971 | * - last vlan range (range_start != range_end, in range) |
| 1972 | */ |
| 1973 | if (!err && range_start && |
| 1974 | !br_vlan_fill_vids(skb, range_start->vid, range_end->vid, |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1975 | range_start, br_vlan_flags(range_start, pvid), |
| 1976 | dump_stats)) |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 1977 | err = -EMSGSIZE; |
| 1978 | |
| 1979 | cb->args[1] = err ? idx : 0; |
| 1980 | |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1981 | nlmsg_end(skb, nlh); |
| 1982 | |
| 1983 | return err; |
| 1984 | } |
| 1985 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1986 | static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = { |
| 1987 | [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 }, |
| 1988 | }; |
| 1989 | |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1990 | static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1991 | { |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1992 | struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1]; |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1993 | int idx = 0, err = 0, s_idx = cb->args[0]; |
| 1994 | struct net *net = sock_net(skb->sk); |
| 1995 | struct br_vlan_msg *bvm; |
| 1996 | struct net_device *dev; |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1997 | u32 dump_flags = 0; |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 1998 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 1999 | err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX, |
| 2000 | br_vlan_db_dump_pol, cb->extack); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2001 | if (err < 0) |
| 2002 | return err; |
| 2003 | |
| 2004 | bvm = nlmsg_data(cb->nlh); |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 2005 | if (dtb[BRIDGE_VLANDB_DUMP_FLAGS]) |
| 2006 | dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2007 | |
| 2008 | rcu_read_lock(); |
| 2009 | if (bvm->ifindex) { |
| 2010 | dev = dev_get_by_index_rcu(net, bvm->ifindex); |
| 2011 | if (!dev) { |
| 2012 | err = -ENODEV; |
| 2013 | goto out_err; |
| 2014 | } |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 2015 | err = br_vlan_dump_dev(dev, skb, cb, dump_flags); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2016 | if (err && err != -EMSGSIZE) |
| 2017 | goto out_err; |
| 2018 | } else { |
| 2019 | for_each_netdev_rcu(net, dev) { |
| 2020 | if (idx < s_idx) |
| 2021 | goto skip; |
| 2022 | |
Nikolay Aleksandrov | 56d0997 | 2020-03-19 12:14:14 +0200 | [diff] [blame] | 2023 | err = br_vlan_dump_dev(dev, skb, cb, dump_flags); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2024 | if (err == -EMSGSIZE) |
| 2025 | break; |
| 2026 | skip: |
| 2027 | idx++; |
| 2028 | } |
| 2029 | } |
| 2030 | cb->args[0] = idx; |
| 2031 | rcu_read_unlock(); |
| 2032 | |
| 2033 | return skb->len; |
| 2034 | |
| 2035 | out_err: |
| 2036 | rcu_read_unlock(); |
| 2037 | |
| 2038 | return err; |
| 2039 | } |
| 2040 | |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2041 | static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = { |
Johannes Berg | 8140860 | 2020-08-18 10:17:31 +0200 | [diff] [blame] | 2042 | [BRIDGE_VLANDB_ENTRY_INFO] = |
| 2043 | NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)), |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 2044 | [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 }, |
Nikolay Aleksandrov | a580c76 | 2020-01-24 13:40:22 +0200 | [diff] [blame] | 2045 | [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 }, |
Nikolay Aleksandrov | fa388f2 | 2020-03-20 13:23:02 +0200 | [diff] [blame] | 2046 | [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED }, |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2047 | }; |
| 2048 | |
| 2049 | static int br_vlan_rtm_process_one(struct net_device *dev, |
| 2050 | const struct nlattr *attr, |
| 2051 | int cmd, struct netlink_ext_ack *extack) |
| 2052 | { |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 2053 | struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL; |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2054 | struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1]; |
Nikolay Aleksandrov | a5d29ae | 2020-01-24 13:40:21 +0200 | [diff] [blame] | 2055 | bool changed = false, skip_processing = false; |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2056 | struct net_bridge_vlan_group *vg; |
| 2057 | struct net_bridge_port *p = NULL; |
| 2058 | int err = 0, cmdmap = 0; |
| 2059 | struct net_bridge *br; |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2060 | |
| 2061 | if (netif_is_bridge_master(dev)) { |
| 2062 | br = netdev_priv(dev); |
| 2063 | vg = br_vlan_group(br); |
| 2064 | } else { |
| 2065 | p = br_port_get_rtnl(dev); |
| 2066 | if (WARN_ON(!p)) |
| 2067 | return -ENODEV; |
| 2068 | br = p->br; |
| 2069 | vg = nbp_vlan_group(p); |
| 2070 | } |
| 2071 | |
| 2072 | if (WARN_ON(!vg)) |
| 2073 | return -ENODEV; |
| 2074 | |
| 2075 | err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr, |
| 2076 | br_vlan_db_policy, extack); |
| 2077 | if (err) |
| 2078 | return err; |
| 2079 | |
| 2080 | if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) { |
| 2081 | NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info"); |
| 2082 | return -EINVAL; |
| 2083 | } |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 2084 | memset(&vrange_end, 0, sizeof(vrange_end)); |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2085 | |
| 2086 | vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]); |
| 2087 | if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN | |
| 2088 | BRIDGE_VLAN_INFO_RANGE_END)) { |
| 2089 | NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls"); |
| 2090 | return -EINVAL; |
| 2091 | } |
| 2092 | if (!br_vlan_valid_id(vinfo->vid, extack)) |
| 2093 | return -EINVAL; |
| 2094 | |
Nikolay Aleksandrov | 0ab5587 | 2020-01-14 19:56:12 +0200 | [diff] [blame] | 2095 | if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) { |
| 2096 | vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]); |
| 2097 | /* validate user-provided flags without RANGE_BEGIN */ |
| 2098 | vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags; |
| 2099 | vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 2100 | |
| 2101 | /* vinfo_last is the range start, vinfo the range end */ |
| 2102 | vinfo_last = vinfo; |
| 2103 | vinfo = &vrange_end; |
| 2104 | |
| 2105 | if (!br_vlan_valid_id(vinfo->vid, extack) || |
| 2106 | !br_vlan_valid_range(vinfo, vinfo_last, extack)) |
| 2107 | return -EINVAL; |
| 2108 | } |
| 2109 | |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2110 | switch (cmd) { |
| 2111 | case RTM_NEWVLAN: |
| 2112 | cmdmap = RTM_SETLINK; |
Nikolay Aleksandrov | a5d29ae | 2020-01-24 13:40:21 +0200 | [diff] [blame] | 2113 | skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS); |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2114 | break; |
Nikolay Aleksandrov | adb3ce9 | 2020-01-14 19:56:11 +0200 | [diff] [blame] | 2115 | case RTM_DELVLAN: |
| 2116 | cmdmap = RTM_DELLINK; |
| 2117 | break; |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Nikolay Aleksandrov | a5d29ae | 2020-01-24 13:40:21 +0200 | [diff] [blame] | 2120 | if (!skip_processing) { |
| 2121 | struct bridge_vlan_info *tmp_last = vinfo_last; |
| 2122 | |
| 2123 | /* br_process_vlan_info may overwrite vinfo_last */ |
| 2124 | err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last, |
| 2125 | &changed, extack); |
| 2126 | |
| 2127 | /* notify first if anything changed */ |
| 2128 | if (changed) |
| 2129 | br_ifinfo_notify(cmdmap, br, p); |
| 2130 | |
| 2131 | if (err) |
| 2132 | return err; |
| 2133 | } |
| 2134 | |
| 2135 | /* deal with options */ |
| 2136 | if (cmd == RTM_NEWVLAN) { |
| 2137 | struct net_bridge_vlan *range_start, *range_end; |
| 2138 | |
| 2139 | if (vinfo_last) { |
| 2140 | range_start = br_vlan_find(vg, vinfo_last->vid); |
| 2141 | range_end = br_vlan_find(vg, vinfo->vid); |
| 2142 | } else { |
| 2143 | range_start = br_vlan_find(vg, vinfo->vid); |
| 2144 | range_end = range_start; |
| 2145 | } |
| 2146 | |
| 2147 | err = br_vlan_process_options(br, p, range_start, range_end, |
| 2148 | tb, extack); |
| 2149 | } |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2150 | |
| 2151 | return err; |
| 2152 | } |
| 2153 | |
| 2154 | static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 2155 | struct netlink_ext_ack *extack) |
| 2156 | { |
| 2157 | struct net *net = sock_net(skb->sk); |
| 2158 | struct br_vlan_msg *bvm; |
| 2159 | struct net_device *dev; |
| 2160 | struct nlattr *attr; |
| 2161 | int err, vlans = 0; |
| 2162 | int rem; |
| 2163 | |
| 2164 | /* this should validate the header and check for remaining bytes */ |
| 2165 | err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL, |
| 2166 | extack); |
| 2167 | if (err < 0) |
| 2168 | return err; |
| 2169 | |
| 2170 | bvm = nlmsg_data(nlh); |
| 2171 | dev = __dev_get_by_index(net, bvm->ifindex); |
| 2172 | if (!dev) |
| 2173 | return -ENODEV; |
| 2174 | |
| 2175 | if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) { |
| 2176 | NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port"); |
| 2177 | return -EINVAL; |
| 2178 | } |
| 2179 | |
| 2180 | nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) { |
| 2181 | if (nla_type(attr) != BRIDGE_VLANDB_ENTRY) |
| 2182 | continue; |
| 2183 | |
| 2184 | vlans++; |
| 2185 | err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type, |
| 2186 | extack); |
| 2187 | if (err) |
| 2188 | break; |
| 2189 | } |
| 2190 | if (!vlans) { |
| 2191 | NL_SET_ERR_MSG_MOD(extack, "No vlans found to process"); |
| 2192 | err = -EINVAL; |
| 2193 | } |
| 2194 | |
| 2195 | return err; |
| 2196 | } |
| 2197 | |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2198 | void br_vlan_rtnl_init(void) |
| 2199 | { |
| 2200 | rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL, |
| 2201 | br_vlan_rtm_dump, 0); |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2202 | rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN, |
| 2203 | br_vlan_rtm_process, NULL, 0); |
Nikolay Aleksandrov | adb3ce9 | 2020-01-14 19:56:11 +0200 | [diff] [blame] | 2204 | rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN, |
| 2205 | br_vlan_rtm_process, NULL, 0); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | void br_vlan_rtnl_uninit(void) |
| 2209 | { |
| 2210 | rtnl_unregister(PF_BRIDGE, RTM_GETVLAN); |
Nikolay Aleksandrov | f26b296 | 2020-01-14 19:56:10 +0200 | [diff] [blame] | 2211 | rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN); |
Nikolay Aleksandrov | adb3ce9 | 2020-01-14 19:56:11 +0200 | [diff] [blame] | 2212 | rtnl_unregister(PF_BRIDGE, RTM_DELVLAN); |
Nikolay Aleksandrov | 8dcea18 | 2020-01-14 19:56:09 +0200 | [diff] [blame] | 2213 | } |