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