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" |
| 8 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 9 | static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid) |
| 10 | { |
| 11 | if (v->pvid == vid) |
| 12 | return; |
| 13 | |
| 14 | smp_wmb(); |
| 15 | v->pvid = vid; |
| 16 | } |
| 17 | |
| 18 | static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid) |
| 19 | { |
| 20 | if (v->pvid != vid) |
| 21 | return; |
| 22 | |
| 23 | smp_wmb(); |
| 24 | v->pvid = 0; |
| 25 | } |
| 26 | |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 27 | static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags) |
| 28 | { |
| 29 | if (flags & BRIDGE_VLAN_INFO_PVID) |
| 30 | __vlan_add_pvid(v, vid); |
Vlad Yasevich | 635126b | 2014-09-12 16:26:17 -0400 | [diff] [blame] | 31 | else |
| 32 | __vlan_delete_pvid(v, vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 33 | |
| 34 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
| 35 | set_bit(vid, v->untagged_bitmap); |
Vlad Yasevich | 635126b | 2014-09-12 16:26:17 -0400 | [diff] [blame] | 36 | else |
| 37 | clear_bit(vid, v->untagged_bitmap); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 40 | static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, |
| 41 | u16 vid, u16 flags) |
| 42 | { |
| 43 | const struct net_device_ops *ops = dev->netdev_ops; |
| 44 | int err; |
| 45 | |
| 46 | /* If driver uses VLAN ndo ops, use 8021q to install vid |
| 47 | * on device, otherwise try switchdev ops to install vid. |
| 48 | */ |
| 49 | |
| 50 | if (ops->ndo_vlan_rx_add_vid) { |
| 51 | err = vlan_vid_add(dev, br->vlan_proto, vid); |
| 52 | } else { |
| 53 | struct switchdev_obj vlan_obj = { |
| 54 | .id = SWITCHDEV_OBJ_PORT_VLAN, |
| 55 | .u.vlan = { |
| 56 | .flags = flags, |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 57 | .vid_begin = vid, |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 58 | .vid_end = vid, |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | err = switchdev_port_obj_add(dev, &vlan_obj); |
| 63 | if (err == -EOPNOTSUPP) |
| 64 | err = 0; |
| 65 | } |
| 66 | |
| 67 | return err; |
| 68 | } |
| 69 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 70 | static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 71 | { |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 72 | struct net_bridge_port *p = NULL; |
| 73 | struct net_bridge *br; |
| 74 | struct net_device *dev; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 75 | int err; |
| 76 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 77 | if (test_bit(vid, v->vlan_bitmap)) { |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 78 | __vlan_add_flags(v, vid, flags); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 81 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 82 | if (v->port_idx) { |
| 83 | p = v->parent.port; |
| 84 | br = p->br; |
| 85 | dev = p->dev; |
| 86 | } else { |
| 87 | br = v->parent.br; |
| 88 | dev = br->dev; |
| 89 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 90 | |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 91 | if (p) { |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 92 | /* Add VLAN to the device filter if it is supported. |
Toshiaki Makita | fdb0a66 | 2014-08-05 15:57:15 +0900 | [diff] [blame] | 93 | * This ensures tagged traffic enters the bridge when |
| 94 | * promiscuous mode is disabled by br_manage_promisc(). |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 95 | */ |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 96 | err = __vlan_vid_add(dev, br, vid, flags); |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 97 | if (err) |
| 98 | return err; |
| 99 | } |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 100 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 101 | err = br_fdb_insert(br, p, dev->dev_addr, vid); |
| 102 | if (err) { |
| 103 | br_err(br, "failed insert local address into bridge " |
| 104 | "forwarding table\n"); |
| 105 | goto out_filt; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | set_bit(vid, v->vlan_bitmap); |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 109 | v->num_vlans++; |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 110 | __vlan_add_flags(v, vid, flags); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 111 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 112 | return 0; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 113 | |
| 114 | out_filt: |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 115 | if (p) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 116 | vlan_vid_del(dev, br->vlan_proto, vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 117 | return err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame^] | 120 | static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, |
| 121 | u16 vid) |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 122 | { |
| 123 | const struct net_device_ops *ops = dev->netdev_ops; |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame^] | 124 | int err = 0; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 125 | |
| 126 | /* If driver uses VLAN ndo ops, use 8021q to delete vid |
| 127 | * on device, otherwise try switchdev ops to delete vid. |
| 128 | */ |
| 129 | |
| 130 | if (ops->ndo_vlan_rx_kill_vid) { |
| 131 | vlan_vid_del(dev, br->vlan_proto, vid); |
| 132 | } else { |
| 133 | struct switchdev_obj vlan_obj = { |
| 134 | .id = SWITCHDEV_OBJ_PORT_VLAN, |
| 135 | .u.vlan = { |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 136 | .vid_begin = vid, |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 137 | .vid_end = vid, |
| 138 | }, |
| 139 | }; |
| 140 | |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame^] | 141 | err = switchdev_port_obj_del(dev, &vlan_obj); |
| 142 | if (err == -EOPNOTSUPP) |
| 143 | err = 0; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 144 | } |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame^] | 145 | |
| 146 | return err; |
Scott Feldman | 7f10953 | 2015-06-12 17:39:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 149 | static int __vlan_del(struct net_port_vlans *v, u16 vid) |
| 150 | { |
| 151 | if (!test_bit(vid, v->vlan_bitmap)) |
| 152 | return -EINVAL; |
| 153 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 154 | __vlan_delete_pvid(v, vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 155 | clear_bit(vid, v->untagged_bitmap); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 156 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 157 | if (v->port_idx) { |
| 158 | struct net_bridge_port *p = v->parent.port; |
Vivien Didelot | bf361ad | 2015-09-05 21:27:57 -0400 | [diff] [blame^] | 159 | int err; |
| 160 | |
| 161 | err = __vlan_vid_del(p->dev, p->br, vid); |
| 162 | if (err) |
| 163 | return err; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 164 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 165 | |
| 166 | clear_bit(vid, v->vlan_bitmap); |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 167 | v->num_vlans--; |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 168 | if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) { |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 169 | if (v->port_idx) |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 170 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 171 | else |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 172 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 173 | kfree_rcu(v, rcu); |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void __vlan_flush(struct net_port_vlans *v) |
| 179 | { |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 180 | smp_wmb(); |
| 181 | v->pvid = 0; |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 182 | bitmap_zero(v->vlan_bitmap, VLAN_N_VID); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 183 | if (v->port_idx) |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 184 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 185 | else |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 186 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 187 | kfree_rcu(v, rcu); |
| 188 | } |
| 189 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 190 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
| 191 | const struct net_port_vlans *pv, |
| 192 | struct sk_buff *skb) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 193 | { |
| 194 | u16 vid; |
| 195 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 196 | /* If this packet was not filtered at input, let it pass */ |
| 197 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 198 | goto out; |
| 199 | |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 200 | /* Vlan filter table must be configured at this point. The |
| 201 | * only exception is the bridge is set in promisc mode and the |
| 202 | * packet is destined for the bridge device. In this case |
| 203 | * pass the packet as is. |
| 204 | */ |
| 205 | if (!pv) { |
| 206 | if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { |
| 207 | goto out; |
| 208 | } else { |
| 209 | kfree_skb(skb); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 214 | /* At this point, we know that the frame was filtered and contains |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 215 | * a valid vlan id. If the vlan id is set in the untagged bitmap, |
tanxiaojun | 1a81a2e | 2013-12-16 21:32:46 +0800 | [diff] [blame] | 216 | * send untagged; otherwise, send tagged. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 217 | */ |
| 218 | br_vlan_get_tag(skb, &vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 219 | if (test_bit(vid, pv->untagged_bitmap)) |
Toshiaki Makita | 99b192d | 2014-03-27 21:46:56 +0900 | [diff] [blame] | 220 | skb->vlan_tci = 0; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 221 | |
| 222 | out: |
| 223 | return skb; |
| 224 | } |
| 225 | |
| 226 | /* Called under RCU */ |
| 227 | bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v, |
| 228 | struct sk_buff *skb, u16 *vid) |
| 229 | { |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 230 | bool tagged; |
| 231 | __be16 proto; |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 232 | |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 233 | /* If VLAN filtering is disabled on the bridge, all packets are |
| 234 | * permitted. |
| 235 | */ |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 236 | if (!br->vlan_enabled) { |
| 237 | BR_INPUT_SKB_CB(skb)->vlan_filtered = false; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 238 | return true; |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 239 | } |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 240 | |
| 241 | /* If there are no vlan in the permitted list, all packets are |
| 242 | * rejected. |
| 243 | */ |
| 244 | if (!v) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 245 | goto drop; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 246 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 247 | BR_INPUT_SKB_CB(skb)->vlan_filtered = true; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 248 | proto = br->vlan_proto; |
| 249 | |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 250 | /* If vlan tx offload is disabled on bridge device and frame was |
| 251 | * sent from vlan device on the bridge device, it does not have |
| 252 | * HW accelerated vlan tag. |
| 253 | */ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 254 | if (unlikely(!skb_vlan_tag_present(skb) && |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 255 | skb->protocol == proto)) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 256 | skb = skb_vlan_untag(skb); |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 257 | if (unlikely(!skb)) |
| 258 | return false; |
| 259 | } |
| 260 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 261 | if (!br_vlan_get_tag(skb, vid)) { |
| 262 | /* Tagged frame */ |
| 263 | if (skb->vlan_proto != proto) { |
| 264 | /* Protocol-mismatch, empty out vlan_tci for new tag */ |
| 265 | skb_push(skb, ETH_HLEN); |
Jiri Pirko | 62749e2 | 2014-11-19 14:04:58 +0100 | [diff] [blame] | 266 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 267 | skb_vlan_tag_get(skb)); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 268 | if (unlikely(!skb)) |
| 269 | return false; |
| 270 | |
| 271 | skb_pull(skb, ETH_HLEN); |
| 272 | skb_reset_mac_len(skb); |
| 273 | *vid = 0; |
| 274 | tagged = false; |
| 275 | } else { |
| 276 | tagged = true; |
| 277 | } |
| 278 | } else { |
| 279 | /* Untagged frame */ |
| 280 | tagged = false; |
| 281 | } |
| 282 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 283 | if (!*vid) { |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 284 | u16 pvid = br_get_pvid(v); |
| 285 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 286 | /* Frame had a tag with VID 0 or did not have a tag. |
| 287 | * See if pvid is set on this port. That tells us which |
| 288 | * vlan untagged or priority-tagged traffic belongs to. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 289 | */ |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 290 | if (!pvid) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 291 | goto drop; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 292 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 293 | /* PVID is set on this port. Any untagged or priority-tagged |
| 294 | * ingress frame is considered to belong to this vlan. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 295 | */ |
Toshiaki Makita | dfb5fa3 | 2013-10-16 17:07:16 +0900 | [diff] [blame] | 296 | *vid = pvid; |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 297 | if (likely(!tagged)) |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 298 | /* Untagged Frame. */ |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 299 | __vlan_hwaccel_put_tag(skb, proto, pvid); |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 300 | else |
| 301 | /* Priority-tagged Frame. |
| 302 | * At this point, We know that skb->vlan_tci had |
| 303 | * VLAN_TAG_PRESENT bit and its VID field was 0x000. |
| 304 | * We update only VID field and preserve PCP field. |
| 305 | */ |
| 306 | skb->vlan_tci |= pvid; |
| 307 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
| 311 | /* Frame had a valid vlan tag. See if vlan is allowed */ |
| 312 | if (test_bit(*vid, v->vlan_bitmap)) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 313 | return true; |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 314 | drop: |
| 315 | kfree_skb(skb); |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 319 | /* Called under RCU. */ |
| 320 | bool br_allowed_egress(struct net_bridge *br, |
| 321 | const struct net_port_vlans *v, |
| 322 | const struct sk_buff *skb) |
| 323 | { |
| 324 | u16 vid; |
| 325 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 326 | /* If this packet was not filtered at input, let it pass */ |
| 327 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 328 | return true; |
| 329 | |
| 330 | if (!v) |
| 331 | return false; |
| 332 | |
| 333 | br_vlan_get_tag(skb, &vid); |
| 334 | if (test_bit(vid, v->vlan_bitmap)) |
| 335 | return true; |
| 336 | |
| 337 | return false; |
| 338 | } |
| 339 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 340 | /* Called under RCU */ |
| 341 | bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) |
| 342 | { |
| 343 | struct net_bridge *br = p->br; |
| 344 | struct net_port_vlans *v; |
| 345 | |
Vlad Yasevich | 20adfa1 | 2014-09-12 16:26:16 -0400 | [diff] [blame] | 346 | /* If filtering was disabled at input, let it pass. */ |
Vlad Yasevich | c095f24 | 2014-09-15 15:24:26 -0400 | [diff] [blame] | 347 | if (!br->vlan_enabled) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 348 | return true; |
| 349 | |
| 350 | v = rcu_dereference(p->vlan_info); |
| 351 | if (!v) |
| 352 | return false; |
| 353 | |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 354 | if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) |
| 355 | *vid = 0; |
| 356 | |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 357 | if (!*vid) { |
| 358 | *vid = br_get_pvid(v); |
Vlad Yasevich | 3df6bf4 | 2014-10-03 11:29:17 -0400 | [diff] [blame] | 359 | if (!*vid) |
Toshiaki Makita | e0d7968 | 2014-05-26 15:15:53 +0900 | [diff] [blame] | 360 | return false; |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | if (test_bit(*vid, v->vlan_bitmap)) |
| 366 | return true; |
| 367 | |
| 368 | return false; |
| 369 | } |
| 370 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 371 | /* Must be protected by RTNL. |
| 372 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 373 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 374 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 375 | { |
| 376 | struct net_port_vlans *pv = NULL; |
| 377 | int err; |
| 378 | |
| 379 | ASSERT_RTNL(); |
| 380 | |
| 381 | pv = rtnl_dereference(br->vlan_info); |
| 382 | if (pv) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 383 | return __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 384 | |
| 385 | /* Create port vlan infomration |
| 386 | */ |
| 387 | pv = kzalloc(sizeof(*pv), GFP_KERNEL); |
| 388 | if (!pv) |
| 389 | return -ENOMEM; |
| 390 | |
| 391 | pv->parent.br = br; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 392 | err = __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 393 | if (err) |
| 394 | goto out; |
| 395 | |
| 396 | rcu_assign_pointer(br->vlan_info, pv); |
| 397 | return 0; |
| 398 | out: |
| 399 | kfree(pv); |
| 400 | return err; |
| 401 | } |
| 402 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 403 | /* Must be protected by RTNL. |
| 404 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 405 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 406 | int br_vlan_delete(struct net_bridge *br, u16 vid) |
| 407 | { |
| 408 | struct net_port_vlans *pv; |
| 409 | |
| 410 | ASSERT_RTNL(); |
| 411 | |
| 412 | pv = rtnl_dereference(br->vlan_info); |
| 413 | if (!pv) |
| 414 | return -EINVAL; |
| 415 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 416 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 417 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 418 | __vlan_del(pv, vid); |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | void br_vlan_flush(struct net_bridge *br) |
| 423 | { |
| 424 | struct net_port_vlans *pv; |
| 425 | |
| 426 | ASSERT_RTNL(); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 427 | pv = rtnl_dereference(br->vlan_info); |
| 428 | if (!pv) |
| 429 | return; |
| 430 | |
| 431 | __vlan_flush(pv); |
| 432 | } |
| 433 | |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 434 | bool br_vlan_find(struct net_bridge *br, u16 vid) |
| 435 | { |
| 436 | struct net_port_vlans *pv; |
| 437 | bool found = false; |
| 438 | |
| 439 | rcu_read_lock(); |
| 440 | pv = rcu_dereference(br->vlan_info); |
| 441 | |
| 442 | if (!pv) |
| 443 | goto out; |
| 444 | |
| 445 | if (test_bit(vid, pv->vlan_bitmap)) |
| 446 | found = true; |
| 447 | |
| 448 | out: |
| 449 | rcu_read_unlock(); |
| 450 | return found; |
| 451 | } |
| 452 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 453 | /* Must be protected by RTNL. */ |
| 454 | static void recalculate_group_addr(struct net_bridge *br) |
| 455 | { |
| 456 | if (br->group_addr_set) |
| 457 | return; |
| 458 | |
| 459 | spin_lock_bh(&br->lock); |
| 460 | if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) { |
| 461 | /* Bridge Group Address */ |
| 462 | br->group_addr[5] = 0x00; |
| 463 | } else { /* vlan_enabled && ETH_P_8021AD */ |
| 464 | /* Provider Bridge Group Address */ |
| 465 | br->group_addr[5] = 0x08; |
| 466 | } |
| 467 | spin_unlock_bh(&br->lock); |
| 468 | } |
| 469 | |
| 470 | /* Must be protected by RTNL. */ |
| 471 | void br_recalculate_fwd_mask(struct net_bridge *br) |
| 472 | { |
| 473 | if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) |
| 474 | br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; |
| 475 | else /* vlan_enabled && ETH_P_8021AD */ |
| 476 | br->group_fwd_mask_required = BR_GROUPFWD_8021AD & |
| 477 | ~(1u << br->group_addr[5]); |
| 478 | } |
| 479 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 480 | int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 481 | { |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 482 | if (br->vlan_enabled == val) |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 483 | return 0; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 484 | |
| 485 | br->vlan_enabled = val; |
Vlad Yasevich | 2796d0c | 2014-05-16 09:59:20 -0400 | [diff] [blame] | 486 | br_manage_promisc(br); |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 487 | recalculate_group_addr(br); |
| 488 | br_recalculate_fwd_mask(br); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 489 | |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
| 494 | { |
| 495 | if (!rtnl_trylock()) |
| 496 | return restart_syscall(); |
| 497 | |
| 498 | __br_vlan_filter_toggle(br, val); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 499 | rtnl_unlock(); |
Nikolay Aleksandrov | a785403 | 2015-08-07 19:40:45 +0300 | [diff] [blame] | 500 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 504 | int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 505 | { |
| 506 | int err = 0; |
| 507 | struct net_bridge_port *p; |
| 508 | struct net_port_vlans *pv; |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 509 | __be16 oldproto; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 510 | u16 vid, errvid; |
| 511 | |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 512 | if (br->vlan_proto == proto) |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 513 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 514 | |
| 515 | /* Add VLANs for the new proto to the device filter. */ |
| 516 | list_for_each_entry(p, &br->port_list, list) { |
| 517 | pv = rtnl_dereference(p->vlan_info); |
| 518 | if (!pv) |
| 519 | continue; |
| 520 | |
| 521 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
| 522 | err = vlan_vid_add(p->dev, proto, vid); |
| 523 | if (err) |
| 524 | goto err_filt; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | oldproto = br->vlan_proto; |
| 529 | br->vlan_proto = proto; |
| 530 | |
| 531 | recalculate_group_addr(br); |
| 532 | br_recalculate_fwd_mask(br); |
| 533 | |
| 534 | /* Delete VLANs for the old proto from the device filter. */ |
| 535 | list_for_each_entry(p, &br->port_list, list) { |
| 536 | pv = rtnl_dereference(p->vlan_info); |
| 537 | if (!pv) |
| 538 | continue; |
| 539 | |
| 540 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) |
| 541 | vlan_vid_del(p->dev, oldproto, vid); |
| 542 | } |
| 543 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 544 | return 0; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 545 | |
| 546 | err_filt: |
| 547 | errvid = vid; |
| 548 | for_each_set_bit(vid, pv->vlan_bitmap, errvid) |
| 549 | vlan_vid_del(p->dev, proto, vid); |
| 550 | |
| 551 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 552 | pv = rtnl_dereference(p->vlan_info); |
| 553 | if (!pv) |
| 554 | continue; |
| 555 | |
| 556 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) |
| 557 | vlan_vid_del(p->dev, proto, vid); |
| 558 | } |
| 559 | |
Toshiaki Makita | d2d427b | 2015-08-27 15:32:26 +0900 | [diff] [blame] | 560 | return err; |
| 561 | } |
| 562 | |
| 563 | int br_vlan_set_proto(struct net_bridge *br, unsigned long val) |
| 564 | { |
| 565 | int err; |
| 566 | |
| 567 | if (val != ETH_P_8021Q && val != ETH_P_8021AD) |
| 568 | return -EPROTONOSUPPORT; |
| 569 | |
| 570 | if (!rtnl_trylock()) |
| 571 | return restart_syscall(); |
| 572 | |
| 573 | err = __br_vlan_set_proto(br, htons(val)); |
| 574 | rtnl_unlock(); |
| 575 | |
| 576 | return err; |
Toshiaki Makita | 204177f | 2014-06-10 20:59:25 +0900 | [diff] [blame] | 577 | } |
| 578 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 579 | static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid) |
| 580 | { |
| 581 | return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap); |
| 582 | } |
| 583 | |
| 584 | static void br_vlan_disable_default_pvid(struct net_bridge *br) |
| 585 | { |
| 586 | struct net_bridge_port *p; |
| 587 | u16 pvid = br->default_pvid; |
| 588 | |
| 589 | /* Disable default_pvid on all ports where it is still |
| 590 | * configured. |
| 591 | */ |
| 592 | if (vlan_default_pvid(br_get_vlan_info(br), pvid)) |
| 593 | br_vlan_delete(br, pvid); |
| 594 | |
| 595 | list_for_each_entry(p, &br->port_list, list) { |
| 596 | if (vlan_default_pvid(nbp_get_vlan_info(p), pvid)) |
| 597 | nbp_vlan_delete(p, pvid); |
| 598 | } |
| 599 | |
| 600 | br->default_pvid = 0; |
| 601 | } |
| 602 | |
| 603 | static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid) |
| 604 | { |
| 605 | struct net_bridge_port *p; |
| 606 | u16 old_pvid; |
| 607 | int err = 0; |
| 608 | unsigned long *changed; |
| 609 | |
| 610 | changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), |
| 611 | GFP_KERNEL); |
| 612 | if (!changed) |
| 613 | return -ENOMEM; |
| 614 | |
| 615 | old_pvid = br->default_pvid; |
| 616 | |
| 617 | /* Update default_pvid config only if we do not conflict with |
| 618 | * user configuration. |
| 619 | */ |
| 620 | if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) && |
| 621 | !br_vlan_find(br, pvid)) { |
| 622 | err = br_vlan_add(br, pvid, |
| 623 | BRIDGE_VLAN_INFO_PVID | |
| 624 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 625 | if (err) |
| 626 | goto out; |
| 627 | br_vlan_delete(br, old_pvid); |
| 628 | set_bit(0, changed); |
| 629 | } |
| 630 | |
| 631 | list_for_each_entry(p, &br->port_list, list) { |
| 632 | /* Update default_pvid config only if we do not conflict with |
| 633 | * user configuration. |
| 634 | */ |
| 635 | if ((old_pvid && |
| 636 | !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) || |
| 637 | nbp_vlan_find(p, pvid)) |
| 638 | continue; |
| 639 | |
| 640 | err = nbp_vlan_add(p, pvid, |
| 641 | BRIDGE_VLAN_INFO_PVID | |
| 642 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 643 | if (err) |
| 644 | goto err_port; |
| 645 | nbp_vlan_delete(p, old_pvid); |
| 646 | set_bit(p->port_no, changed); |
| 647 | } |
| 648 | |
| 649 | br->default_pvid = pvid; |
| 650 | |
| 651 | out: |
| 652 | kfree(changed); |
| 653 | return err; |
| 654 | |
| 655 | err_port: |
| 656 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 657 | if (!test_bit(p->port_no, changed)) |
| 658 | continue; |
| 659 | |
| 660 | if (old_pvid) |
| 661 | nbp_vlan_add(p, old_pvid, |
| 662 | BRIDGE_VLAN_INFO_PVID | |
| 663 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 664 | nbp_vlan_delete(p, pvid); |
| 665 | } |
| 666 | |
| 667 | if (test_bit(0, changed)) { |
| 668 | if (old_pvid) |
| 669 | br_vlan_add(br, old_pvid, |
| 670 | BRIDGE_VLAN_INFO_PVID | |
| 671 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 672 | br_vlan_delete(br, pvid); |
| 673 | } |
| 674 | goto out; |
| 675 | } |
| 676 | |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 677 | int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) |
| 678 | { |
| 679 | u16 pvid = val; |
| 680 | int err = 0; |
| 681 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 682 | if (val >= VLAN_VID_MASK) |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 683 | return -EINVAL; |
| 684 | |
| 685 | if (!rtnl_trylock()) |
| 686 | return restart_syscall(); |
| 687 | |
| 688 | if (pvid == br->default_pvid) |
| 689 | goto unlock; |
| 690 | |
| 691 | /* Only allow default pvid change when filtering is disabled */ |
| 692 | if (br->vlan_enabled) { |
| 693 | pr_info_once("Please disable vlan filtering to change default_pvid\n"); |
| 694 | err = -EPERM; |
| 695 | goto unlock; |
| 696 | } |
| 697 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 698 | if (!pvid) |
| 699 | br_vlan_disable_default_pvid(br); |
| 700 | else |
| 701 | err = __br_vlan_set_default_pvid(br, pvid); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 702 | |
| 703 | unlock: |
| 704 | rtnl_unlock(); |
| 705 | return err; |
| 706 | } |
| 707 | |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 708 | int br_vlan_init(struct net_bridge *br) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 709 | { |
| 710 | br->vlan_proto = htons(ETH_P_8021Q); |
Vlad Yasevich | 96a20d9 | 2014-10-03 11:29:16 -0400 | [diff] [blame] | 711 | br->default_pvid = 1; |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 712 | return br_vlan_add(br, 1, |
| 713 | BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED); |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 714 | } |
| 715 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 716 | /* Must be protected by RTNL. |
| 717 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 718 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 719 | int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 720 | { |
| 721 | struct net_port_vlans *pv = NULL; |
| 722 | int err; |
| 723 | |
| 724 | ASSERT_RTNL(); |
| 725 | |
| 726 | pv = rtnl_dereference(port->vlan_info); |
| 727 | if (pv) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 728 | return __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 729 | |
| 730 | /* Create port vlan infomration |
| 731 | */ |
| 732 | pv = kzalloc(sizeof(*pv), GFP_KERNEL); |
| 733 | if (!pv) { |
| 734 | err = -ENOMEM; |
| 735 | goto clean_up; |
| 736 | } |
| 737 | |
| 738 | pv->port_idx = port->port_no; |
| 739 | pv->parent.port = port; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 740 | err = __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 741 | if (err) |
| 742 | goto clean_up; |
| 743 | |
| 744 | rcu_assign_pointer(port->vlan_info, pv); |
| 745 | return 0; |
| 746 | |
| 747 | clean_up: |
| 748 | kfree(pv); |
| 749 | return err; |
| 750 | } |
| 751 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 752 | /* Must be protected by RTNL. |
| 753 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 754 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 755 | int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) |
| 756 | { |
| 757 | struct net_port_vlans *pv; |
| 758 | |
| 759 | ASSERT_RTNL(); |
| 760 | |
| 761 | pv = rtnl_dereference(port->vlan_info); |
| 762 | if (!pv) |
| 763 | return -EINVAL; |
| 764 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 765 | 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] | 766 | br_fdb_delete_by_port(port->br, port, vid, 0); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 767 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 768 | return __vlan_del(pv, vid); |
| 769 | } |
| 770 | |
| 771 | void nbp_vlan_flush(struct net_bridge_port *port) |
| 772 | { |
| 773 | struct net_port_vlans *pv; |
Toshiaki Makita | dbbaf94 | 2013-11-13 17:26:13 +0900 | [diff] [blame] | 774 | u16 vid; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 775 | |
| 776 | ASSERT_RTNL(); |
| 777 | |
| 778 | pv = rtnl_dereference(port->vlan_info); |
| 779 | if (!pv) |
| 780 | return; |
| 781 | |
Toshiaki Makita | dbbaf94 | 2013-11-13 17:26:13 +0900 | [diff] [blame] | 782 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) |
Toshiaki Makita | 8580e21 | 2014-06-10 20:59:23 +0900 | [diff] [blame] | 783 | vlan_vid_del(port->dev, port->br->vlan_proto, vid); |
Toshiaki Makita | dbbaf94 | 2013-11-13 17:26:13 +0900 | [diff] [blame] | 784 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 785 | __vlan_flush(pv); |
| 786 | } |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 787 | |
| 788 | bool nbp_vlan_find(struct net_bridge_port *port, u16 vid) |
| 789 | { |
| 790 | struct net_port_vlans *pv; |
| 791 | bool found = false; |
| 792 | |
| 793 | rcu_read_lock(); |
| 794 | pv = rcu_dereference(port->vlan_info); |
| 795 | |
| 796 | if (!pv) |
| 797 | goto out; |
| 798 | |
| 799 | if (test_bit(vid, pv->vlan_bitmap)) |
| 800 | found = true; |
| 801 | |
| 802 | out: |
| 803 | rcu_read_unlock(); |
| 804 | return found; |
| 805 | } |
Vlad Yasevich | 5be5a2d | 2014-10-03 11:29:18 -0400 | [diff] [blame] | 806 | |
| 807 | int nbp_vlan_init(struct net_bridge_port *p) |
| 808 | { |
| 809 | return p->br->default_pvid ? |
| 810 | nbp_vlan_add(p, p->br->default_pvid, |
| 811 | BRIDGE_VLAN_INFO_PVID | |
| 812 | BRIDGE_VLAN_INFO_UNTAGGED) : |
| 813 | 0; |
| 814 | } |