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