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> |
| 5 | |
| 6 | #include "br_private.h" |
| 7 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 8 | static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid) |
| 9 | { |
| 10 | if (v->pvid == vid) |
| 11 | return; |
| 12 | |
| 13 | smp_wmb(); |
| 14 | v->pvid = vid; |
| 15 | } |
| 16 | |
| 17 | static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid) |
| 18 | { |
| 19 | if (v->pvid != vid) |
| 20 | return; |
| 21 | |
| 22 | smp_wmb(); |
| 23 | v->pvid = 0; |
| 24 | } |
| 25 | |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 26 | static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags) |
| 27 | { |
| 28 | if (flags & BRIDGE_VLAN_INFO_PVID) |
| 29 | __vlan_add_pvid(v, vid); |
| 30 | |
| 31 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
| 32 | set_bit(vid, v->untagged_bitmap); |
| 33 | } |
| 34 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 35 | 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] | 36 | { |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 37 | struct net_bridge_port *p = NULL; |
| 38 | struct net_bridge *br; |
| 39 | struct net_device *dev; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 40 | int err; |
| 41 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 42 | if (test_bit(vid, v->vlan_bitmap)) { |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 43 | __vlan_add_flags(v, vid, flags); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 46 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 47 | if (v->port_idx) { |
| 48 | p = v->parent.port; |
| 49 | br = p->br; |
| 50 | dev = p->dev; |
| 51 | } else { |
| 52 | br = v->parent.br; |
| 53 | dev = br->dev; |
| 54 | } |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 55 | |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 56 | if (p) { |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 57 | /* Add VLAN to the device filter if it is supported. |
| 58 | * Stricly speaking, this is not necessary now, since |
| 59 | * devices are made promiscuous by the bridge, but if |
| 60 | * that ever changes this code will allow tagged |
| 61 | * traffic to enter the bridge. |
| 62 | */ |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 63 | err = vlan_vid_add(dev, htons(ETH_P_8021Q), vid); |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 64 | if (err) |
| 65 | return err; |
| 66 | } |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 67 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 68 | err = br_fdb_insert(br, p, dev->dev_addr, vid); |
| 69 | if (err) { |
| 70 | br_err(br, "failed insert local address into bridge " |
| 71 | "forwarding table\n"); |
| 72 | goto out_filt; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | set_bit(vid, v->vlan_bitmap); |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 76 | v->num_vlans++; |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 77 | __vlan_add_flags(v, vid, flags); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 78 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 79 | return 0; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 80 | |
| 81 | out_filt: |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 82 | if (p) |
| 83 | vlan_vid_del(dev, htons(ETH_P_8021Q), vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 84 | return err; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int __vlan_del(struct net_port_vlans *v, u16 vid) |
| 88 | { |
| 89 | if (!test_bit(vid, v->vlan_bitmap)) |
| 90 | return -EINVAL; |
| 91 | |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 92 | __vlan_delete_pvid(v, vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 93 | clear_bit(vid, v->untagged_bitmap); |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 94 | |
Toshiaki Makita | 1923683 | 2013-11-13 17:26:12 +0900 | [diff] [blame] | 95 | if (v->port_idx) |
| 96 | vlan_vid_del(v->parent.port->dev, htons(ETH_P_8021Q), vid); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 97 | |
| 98 | clear_bit(vid, v->vlan_bitmap); |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 99 | v->num_vlans--; |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 100 | if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) { |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 101 | if (v->port_idx) |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 102 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 103 | else |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 104 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 105 | kfree_rcu(v, rcu); |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void __vlan_flush(struct net_port_vlans *v) |
| 111 | { |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 112 | smp_wmb(); |
| 113 | v->pvid = 0; |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 114 | bitmap_zero(v->vlan_bitmap, VLAN_N_VID); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 115 | if (v->port_idx) |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 116 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 117 | else |
Monam Agarwal | cd18721 | 2014-03-24 00:41:13 +0530 | [diff] [blame] | 118 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 119 | kfree_rcu(v, rcu); |
| 120 | } |
| 121 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 122 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
| 123 | const struct net_port_vlans *pv, |
| 124 | struct sk_buff *skb) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 125 | { |
| 126 | u16 vid; |
| 127 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 128 | if (!br->vlan_enabled) |
| 129 | goto out; |
| 130 | |
Vlad Yasevich | fc92f74 | 2014-03-27 21:51:18 -0400 | [diff] [blame] | 131 | /* Vlan filter table must be configured at this point. The |
| 132 | * only exception is the bridge is set in promisc mode and the |
| 133 | * packet is destined for the bridge device. In this case |
| 134 | * pass the packet as is. |
| 135 | */ |
| 136 | if (!pv) { |
| 137 | if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { |
| 138 | goto out; |
| 139 | } else { |
| 140 | kfree_skb(skb); |
| 141 | return NULL; |
| 142 | } |
| 143 | } |
| 144 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 145 | /* At this point, we know that the frame was filtered and contains |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 146 | * 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] | 147 | * send untagged; otherwise, send tagged. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 148 | */ |
| 149 | br_vlan_get_tag(skb, &vid); |
Vlad Yasevich | 35e03f3 | 2013-02-13 12:00:20 +0000 | [diff] [blame] | 150 | if (test_bit(vid, pv->untagged_bitmap)) |
Toshiaki Makita | 99b192d | 2014-03-27 21:46:56 +0900 | [diff] [blame] | 151 | skb->vlan_tci = 0; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 152 | |
| 153 | out: |
| 154 | return skb; |
| 155 | } |
| 156 | |
| 157 | /* Called under RCU */ |
| 158 | bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v, |
| 159 | struct sk_buff *skb, u16 *vid) |
| 160 | { |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 161 | int err; |
| 162 | |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 163 | /* If VLAN filtering is disabled on the bridge, all packets are |
| 164 | * permitted. |
| 165 | */ |
| 166 | if (!br->vlan_enabled) |
| 167 | return true; |
| 168 | |
| 169 | /* If there are no vlan in the permitted list, all packets are |
| 170 | * rejected. |
| 171 | */ |
| 172 | if (!v) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 173 | goto drop; |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 174 | |
Toshiaki Makita | 12464bb | 2014-03-27 21:46:55 +0900 | [diff] [blame] | 175 | /* If vlan tx offload is disabled on bridge device and frame was |
| 176 | * sent from vlan device on the bridge device, it does not have |
| 177 | * HW accelerated vlan tag. |
| 178 | */ |
| 179 | if (unlikely(!vlan_tx_tag_present(skb) && |
| 180 | (skb->protocol == htons(ETH_P_8021Q) || |
| 181 | skb->protocol == htons(ETH_P_8021AD)))) { |
| 182 | skb = vlan_untag(skb); |
| 183 | if (unlikely(!skb)) |
| 184 | return false; |
| 185 | } |
| 186 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 187 | err = br_vlan_get_tag(skb, vid); |
| 188 | if (!*vid) { |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 189 | u16 pvid = br_get_pvid(v); |
| 190 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 191 | /* Frame had a tag with VID 0 or did not have a tag. |
| 192 | * See if pvid is set on this port. That tells us which |
| 193 | * vlan untagged or priority-tagged traffic belongs to. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 194 | */ |
| 195 | if (pvid == VLAN_N_VID) |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 196 | goto drop; |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 197 | |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 198 | /* PVID is set on this port. Any untagged or priority-tagged |
| 199 | * ingress frame is considered to belong to this vlan. |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 200 | */ |
Toshiaki Makita | dfb5fa3 | 2013-10-16 17:07:16 +0900 | [diff] [blame] | 201 | *vid = pvid; |
Toshiaki Makita | b90356ce | 2013-10-16 17:07:14 +0900 | [diff] [blame] | 202 | if (likely(err)) |
| 203 | /* Untagged Frame. */ |
| 204 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid); |
| 205 | else |
| 206 | /* Priority-tagged Frame. |
| 207 | * At this point, We know that skb->vlan_tci had |
| 208 | * VLAN_TAG_PRESENT bit and its VID field was 0x000. |
| 209 | * We update only VID field and preserve PCP field. |
| 210 | */ |
| 211 | skb->vlan_tci |= pvid; |
| 212 | |
Vlad Yasevich | 7885198 | 2013-02-13 12:00:14 +0000 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
| 216 | /* Frame had a valid vlan tag. See if vlan is allowed */ |
| 217 | if (test_bit(*vid, v->vlan_bitmap)) |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 218 | return true; |
Toshiaki Makita | eb70761 | 2014-04-09 17:00:30 +0900 | [diff] [blame] | 219 | drop: |
| 220 | kfree_skb(skb); |
Vlad Yasevich | a37b85c | 2013-02-13 12:00:10 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
Vlad Yasevich | 85f46c6 | 2013-02-13 12:00:11 +0000 | [diff] [blame] | 224 | /* Called under RCU. */ |
| 225 | bool br_allowed_egress(struct net_bridge *br, |
| 226 | const struct net_port_vlans *v, |
| 227 | const struct sk_buff *skb) |
| 228 | { |
| 229 | u16 vid; |
| 230 | |
| 231 | if (!br->vlan_enabled) |
| 232 | return true; |
| 233 | |
| 234 | if (!v) |
| 235 | return false; |
| 236 | |
| 237 | br_vlan_get_tag(skb, &vid); |
| 238 | if (test_bit(vid, v->vlan_bitmap)) |
| 239 | return true; |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 244 | /* Must be protected by RTNL. |
| 245 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 246 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 247 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 248 | { |
| 249 | struct net_port_vlans *pv = NULL; |
| 250 | int err; |
| 251 | |
| 252 | ASSERT_RTNL(); |
| 253 | |
| 254 | pv = rtnl_dereference(br->vlan_info); |
| 255 | if (pv) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 256 | return __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 257 | |
| 258 | /* Create port vlan infomration |
| 259 | */ |
| 260 | pv = kzalloc(sizeof(*pv), GFP_KERNEL); |
| 261 | if (!pv) |
| 262 | return -ENOMEM; |
| 263 | |
| 264 | pv->parent.br = br; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 265 | err = __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 266 | if (err) |
| 267 | goto out; |
| 268 | |
| 269 | rcu_assign_pointer(br->vlan_info, pv); |
| 270 | return 0; |
| 271 | out: |
| 272 | kfree(pv); |
| 273 | return err; |
| 274 | } |
| 275 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 276 | /* Must be protected by RTNL. |
| 277 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 278 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 279 | int br_vlan_delete(struct net_bridge *br, u16 vid) |
| 280 | { |
| 281 | struct net_port_vlans *pv; |
| 282 | |
| 283 | ASSERT_RTNL(); |
| 284 | |
| 285 | pv = rtnl_dereference(br->vlan_info); |
| 286 | if (!pv) |
| 287 | return -EINVAL; |
| 288 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 289 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 290 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 291 | __vlan_del(pv, vid); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | void br_vlan_flush(struct net_bridge *br) |
| 296 | { |
| 297 | struct net_port_vlans *pv; |
| 298 | |
| 299 | ASSERT_RTNL(); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 300 | pv = rtnl_dereference(br->vlan_info); |
| 301 | if (!pv) |
| 302 | return; |
| 303 | |
| 304 | __vlan_flush(pv); |
| 305 | } |
| 306 | |
Toshiaki Makita | 2b292fb | 2014-02-07 16:48:22 +0900 | [diff] [blame] | 307 | bool br_vlan_find(struct net_bridge *br, u16 vid) |
| 308 | { |
| 309 | struct net_port_vlans *pv; |
| 310 | bool found = false; |
| 311 | |
| 312 | rcu_read_lock(); |
| 313 | pv = rcu_dereference(br->vlan_info); |
| 314 | |
| 315 | if (!pv) |
| 316 | goto out; |
| 317 | |
| 318 | if (test_bit(vid, pv->vlan_bitmap)) |
| 319 | found = true; |
| 320 | |
| 321 | out: |
| 322 | rcu_read_unlock(); |
| 323 | return found; |
| 324 | } |
| 325 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 326 | int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
| 327 | { |
| 328 | if (!rtnl_trylock()) |
| 329 | return restart_syscall(); |
| 330 | |
| 331 | if (br->vlan_enabled == val) |
| 332 | goto unlock; |
| 333 | |
| 334 | br->vlan_enabled = val; |
| 335 | |
| 336 | unlock: |
| 337 | rtnl_unlock(); |
| 338 | return 0; |
| 339 | } |
| 340 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 341 | /* Must be protected by RTNL. |
| 342 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 343 | */ |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 344 | 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] | 345 | { |
| 346 | struct net_port_vlans *pv = NULL; |
| 347 | int err; |
| 348 | |
| 349 | ASSERT_RTNL(); |
| 350 | |
| 351 | pv = rtnl_dereference(port->vlan_info); |
| 352 | if (pv) |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 353 | return __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 354 | |
| 355 | /* Create port vlan infomration |
| 356 | */ |
| 357 | pv = kzalloc(sizeof(*pv), GFP_KERNEL); |
| 358 | if (!pv) { |
| 359 | err = -ENOMEM; |
| 360 | goto clean_up; |
| 361 | } |
| 362 | |
| 363 | pv->port_idx = port->port_no; |
| 364 | pv->parent.port = port; |
Vlad Yasevich | 552406c | 2013-02-13 12:00:15 +0000 | [diff] [blame] | 365 | err = __vlan_add(pv, vid, flags); |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 366 | if (err) |
| 367 | goto clean_up; |
| 368 | |
| 369 | rcu_assign_pointer(port->vlan_info, pv); |
| 370 | return 0; |
| 371 | |
| 372 | clean_up: |
| 373 | kfree(pv); |
| 374 | return err; |
| 375 | } |
| 376 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 377 | /* Must be protected by RTNL. |
| 378 | * Must be called with vid in range from 1 to 4094 inclusive. |
| 379 | */ |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 380 | int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) |
| 381 | { |
| 382 | struct net_port_vlans *pv; |
| 383 | |
| 384 | ASSERT_RTNL(); |
| 385 | |
| 386 | pv = rtnl_dereference(port->vlan_info); |
| 387 | if (!pv) |
| 388 | return -EINVAL; |
| 389 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 390 | br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 391 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 392 | return __vlan_del(pv, vid); |
| 393 | } |
| 394 | |
| 395 | void nbp_vlan_flush(struct net_bridge_port *port) |
| 396 | { |
| 397 | struct net_port_vlans *pv; |
Toshiaki Makita | dbbaf94 | 2013-11-13 17:26:13 +0900 | [diff] [blame] | 398 | u16 vid; |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 399 | |
| 400 | ASSERT_RTNL(); |
| 401 | |
| 402 | pv = rtnl_dereference(port->vlan_info); |
| 403 | if (!pv) |
| 404 | return; |
| 405 | |
Toshiaki Makita | dbbaf94 | 2013-11-13 17:26:13 +0900 | [diff] [blame] | 406 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) |
| 407 | vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid); |
| 408 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 409 | __vlan_flush(pv); |
| 410 | } |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 411 | |
| 412 | bool nbp_vlan_find(struct net_bridge_port *port, u16 vid) |
| 413 | { |
| 414 | struct net_port_vlans *pv; |
| 415 | bool found = false; |
| 416 | |
| 417 | rcu_read_lock(); |
| 418 | pv = rcu_dereference(port->vlan_info); |
| 419 | |
| 420 | if (!pv) |
| 421 | goto out; |
| 422 | |
| 423 | if (test_bit(vid, pv->vlan_bitmap)) |
| 424 | found = true; |
| 425 | |
| 426 | out: |
| 427 | rcu_read_unlock(); |
| 428 | return found; |
| 429 | } |