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