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