Commit | Line | Data |
---|---|---|
243a2e63 VY |
1 | #include <linux/kernel.h> |
2 | #include <linux/netdevice.h> | |
3 | #include <linux/rtnetlink.h> | |
4 | #include <linux/slab.h> | |
7f109539 | 5 | #include <net/switchdev.h> |
243a2e63 VY |
6 | |
7 | #include "br_private.h" | |
8 | ||
552406c4 VY |
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 | ||
35e03f3a VY |
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); | |
635126b7 VY |
31 | else |
32 | __vlan_delete_pvid(v, vid); | |
35e03f3a VY |
33 | |
34 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) | |
35 | set_bit(vid, v->untagged_bitmap); | |
635126b7 VY |
36 | else |
37 | clear_bit(vid, v->untagged_bitmap); | |
35e03f3a VY |
38 | } |
39 | ||
7f109539 SF |
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, | |
3e3a78b4 | 57 | .vid_begin = vid, |
7f109539 SF |
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 | ||
552406c4 | 70 | static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags) |
243a2e63 | 71 | { |
bc9a25d2 VY |
72 | struct net_bridge_port *p = NULL; |
73 | struct net_bridge *br; | |
74 | struct net_device *dev; | |
243a2e63 VY |
75 | int err; |
76 | ||
552406c4 | 77 | if (test_bit(vid, v->vlan_bitmap)) { |
35e03f3a | 78 | __vlan_add_flags(v, vid, flags); |
552406c4 VY |
79 | return 0; |
80 | } | |
243a2e63 | 81 | |
8adff41c TM |
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 | } | |
8adff41c | 90 | |
19236837 | 91 | if (p) { |
8adff41c | 92 | /* Add VLAN to the device filter if it is supported. |
fdb0a662 TM |
93 | * This ensures tagged traffic enters the bridge when |
94 | * promiscuous mode is disabled by br_manage_promisc(). | |
8adff41c | 95 | */ |
7f109539 | 96 | err = __vlan_vid_add(dev, br, vid, flags); |
8adff41c TM |
97 | if (err) |
98 | return err; | |
99 | } | |
bc9a25d2 | 100 | |
8adff41c TM |
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; | |
243a2e63 VY |
106 | } |
107 | ||
108 | set_bit(vid, v->vlan_bitmap); | |
6cbdceeb | 109 | v->num_vlans++; |
35e03f3a | 110 | __vlan_add_flags(v, vid, flags); |
552406c4 | 111 | |
243a2e63 | 112 | return 0; |
bc9a25d2 VY |
113 | |
114 | out_filt: | |
19236837 | 115 | if (p) |
8580e211 | 116 | vlan_vid_del(dev, br->vlan_proto, vid); |
bc9a25d2 | 117 | return err; |
243a2e63 VY |
118 | } |
119 | ||
bf361ad3 VD |
120 | static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, |
121 | u16 vid) | |
7f109539 SF |
122 | { |
123 | const struct net_device_ops *ops = dev->netdev_ops; | |
bf361ad3 | 124 | int err = 0; |
7f109539 SF |
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 = { | |
3e3a78b4 | 136 | .vid_begin = vid, |
7f109539 SF |
137 | .vid_end = vid, |
138 | }, | |
139 | }; | |
140 | ||
bf361ad3 VD |
141 | err = switchdev_port_obj_del(dev, &vlan_obj); |
142 | if (err == -EOPNOTSUPP) | |
143 | err = 0; | |
7f109539 | 144 | } |
bf361ad3 VD |
145 | |
146 | return err; | |
7f109539 SF |
147 | } |
148 | ||
243a2e63 VY |
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 | ||
552406c4 | 154 | __vlan_delete_pvid(v, vid); |
35e03f3a | 155 | clear_bit(vid, v->untagged_bitmap); |
552406c4 | 156 | |
8580e211 TM |
157 | if (v->port_idx) { |
158 | struct net_bridge_port *p = v->parent.port; | |
bf361ad3 VD |
159 | int err; |
160 | ||
161 | err = __vlan_vid_del(p->dev, p->br, vid); | |
162 | if (err) | |
163 | return err; | |
8580e211 | 164 | } |
243a2e63 VY |
165 | |
166 | clear_bit(vid, v->vlan_bitmap); | |
6cbdceeb | 167 | v->num_vlans--; |
ef40b7ef | 168 | if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) { |
243a2e63 | 169 | if (v->port_idx) |
cd18721e | 170 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
243a2e63 | 171 | else |
cd18721e | 172 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
243a2e63 VY |
173 | kfree_rcu(v, rcu); |
174 | } | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static void __vlan_flush(struct net_port_vlans *v) | |
179 | { | |
552406c4 VY |
180 | smp_wmb(); |
181 | v->pvid = 0; | |
ef40b7ef | 182 | bitmap_zero(v->vlan_bitmap, VLAN_N_VID); |
243a2e63 | 183 | if (v->port_idx) |
cd18721e | 184 | RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); |
243a2e63 | 185 | else |
cd18721e | 186 | RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); |
243a2e63 VY |
187 | kfree_rcu(v, rcu); |
188 | } | |
189 | ||
78851988 VY |
190 | struct sk_buff *br_handle_vlan(struct net_bridge *br, |
191 | const struct net_port_vlans *pv, | |
192 | struct sk_buff *skb) | |
a37b85c9 VY |
193 | { |
194 | u16 vid; | |
195 | ||
20adfa1a VY |
196 | /* If this packet was not filtered at input, let it pass */ |
197 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) | |
78851988 VY |
198 | goto out; |
199 | ||
fc92f745 VY |
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 | ||
78851988 | 214 | /* At this point, we know that the frame was filtered and contains |
35e03f3a | 215 | * a valid vlan id. If the vlan id is set in the untagged bitmap, |
1a81a2e0 | 216 | * send untagged; otherwise, send tagged. |
78851988 VY |
217 | */ |
218 | br_vlan_get_tag(skb, &vid); | |
35e03f3a | 219 | if (test_bit(vid, pv->untagged_bitmap)) |
99b192da | 220 | skb->vlan_tci = 0; |
78851988 VY |
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 | { | |
8580e211 TM |
230 | bool tagged; |
231 | __be16 proto; | |
b90356ce | 232 | |
a37b85c9 VY |
233 | /* If VLAN filtering is disabled on the bridge, all packets are |
234 | * permitted. | |
235 | */ | |
20adfa1a VY |
236 | if (!br->vlan_enabled) { |
237 | BR_INPUT_SKB_CB(skb)->vlan_filtered = false; | |
a37b85c9 | 238 | return true; |
20adfa1a | 239 | } |
a37b85c9 VY |
240 | |
241 | /* If there are no vlan in the permitted list, all packets are | |
242 | * rejected. | |
243 | */ | |
244 | if (!v) | |
eb707618 | 245 | goto drop; |
a37b85c9 | 246 | |
20adfa1a | 247 | BR_INPUT_SKB_CB(skb)->vlan_filtered = true; |
8580e211 TM |
248 | proto = br->vlan_proto; |
249 | ||
12464bb8 TM |
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 | */ | |
df8a39de | 254 | if (unlikely(!skb_vlan_tag_present(skb) && |
8580e211 | 255 | skb->protocol == proto)) { |
0d5501c1 | 256 | skb = skb_vlan_untag(skb); |
12464bb8 TM |
257 | if (unlikely(!skb)) |
258 | return false; | |
259 | } | |
260 | ||
8580e211 TM |
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); | |
62749e2c | 266 | skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, |
df8a39de | 267 | skb_vlan_tag_get(skb)); |
8580e211 TM |
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 | ||
b90356ce | 283 | if (!*vid) { |
78851988 VY |
284 | u16 pvid = br_get_pvid(v); |
285 | ||
b90356ce TM |
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. | |
78851988 | 289 | */ |
3df6bf45 | 290 | if (!pvid) |
eb707618 | 291 | goto drop; |
78851988 | 292 | |
b90356ce TM |
293 | /* PVID is set on this port. Any untagged or priority-tagged |
294 | * ingress frame is considered to belong to this vlan. | |
78851988 | 295 | */ |
dfb5fa32 | 296 | *vid = pvid; |
8580e211 | 297 | if (likely(!tagged)) |
b90356ce | 298 | /* Untagged Frame. */ |
8580e211 | 299 | __vlan_hwaccel_put_tag(skb, proto, pvid); |
b90356ce TM |
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 | ||
78851988 VY |
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)) | |
a37b85c9 | 313 | return true; |
eb707618 TM |
314 | drop: |
315 | kfree_skb(skb); | |
a37b85c9 VY |
316 | return false; |
317 | } | |
318 | ||
85f46c6b VY |
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 | ||
20adfa1a VY |
326 | /* If this packet was not filtered at input, let it pass */ |
327 | if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) | |
85f46c6b VY |
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 | ||
e0d7968a TM |
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 | ||
20adfa1a | 346 | /* If filtering was disabled at input, let it pass. */ |
c095f248 | 347 | if (!br->vlan_enabled) |
e0d7968a TM |
348 | return true; |
349 | ||
350 | v = rcu_dereference(p->vlan_info); | |
351 | if (!v) | |
352 | return false; | |
353 | ||
8580e211 TM |
354 | if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) |
355 | *vid = 0; | |
356 | ||
e0d7968a TM |
357 | if (!*vid) { |
358 | *vid = br_get_pvid(v); | |
3df6bf45 | 359 | if (!*vid) |
e0d7968a TM |
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 | ||
8adff41c TM |
371 | /* Must be protected by RTNL. |
372 | * Must be called with vid in range from 1 to 4094 inclusive. | |
373 | */ | |
552406c4 | 374 | int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) |
243a2e63 VY |
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) | |
552406c4 | 383 | return __vlan_add(pv, vid, flags); |
243a2e63 VY |
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; | |
552406c4 | 392 | err = __vlan_add(pv, vid, flags); |
243a2e63 VY |
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 | ||
8adff41c TM |
403 | /* Must be protected by RTNL. |
404 | * Must be called with vid in range from 1 to 4094 inclusive. | |
405 | */ | |
243a2e63 VY |
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 | ||
424bb9c9 | 416 | br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); |
bc9a25d2 | 417 | |
243a2e63 VY |
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(); | |
243a2e63 VY |
427 | pv = rtnl_dereference(br->vlan_info); |
428 | if (!pv) | |
429 | return; | |
430 | ||
431 | __vlan_flush(pv); | |
432 | } | |
433 | ||
2b292fb4 TM |
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 | ||
204177f3 TM |
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 | ||
a7854037 | 480 | int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) |
243a2e63 | 481 | { |
243a2e63 | 482 | if (br->vlan_enabled == val) |
a7854037 | 483 | return 0; |
243a2e63 VY |
484 | |
485 | br->vlan_enabled = val; | |
2796d0c6 | 486 | br_manage_promisc(br); |
204177f3 TM |
487 | recalculate_group_addr(br); |
488 | br_recalculate_fwd_mask(br); | |
243a2e63 | 489 | |
a7854037 NA |
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); | |
243a2e63 | 499 | rtnl_unlock(); |
a7854037 | 500 | |
243a2e63 VY |
501 | return 0; |
502 | } | |
503 | ||
d2d427b3 | 504 | int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) |
204177f3 TM |
505 | { |
506 | int err = 0; | |
507 | struct net_bridge_port *p; | |
508 | struct net_port_vlans *pv; | |
d2d427b3 | 509 | __be16 oldproto; |
204177f3 TM |
510 | u16 vid, errvid; |
511 | ||
204177f3 | 512 | if (br->vlan_proto == proto) |
d2d427b3 | 513 | return 0; |
204177f3 TM |
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 | ||
d2d427b3 | 544 | return 0; |
204177f3 TM |
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 | ||
d2d427b3 TM |
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; | |
204177f3 TM |
577 | } |
578 | ||
5be5a2df VY |
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 | ||
96a20d9d VY |
677 | int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) |
678 | { | |
679 | u16 pvid = val; | |
680 | int err = 0; | |
681 | ||
5be5a2df | 682 | if (val >= VLAN_VID_MASK) |
96a20d9d VY |
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 | ||
5be5a2df VY |
698 | if (!pvid) |
699 | br_vlan_disable_default_pvid(br); | |
700 | else | |
701 | err = __br_vlan_set_default_pvid(br, pvid); | |
96a20d9d VY |
702 | |
703 | unlock: | |
704 | rtnl_unlock(); | |
705 | return err; | |
706 | } | |
707 | ||
5be5a2df | 708 | int br_vlan_init(struct net_bridge *br) |
8580e211 TM |
709 | { |
710 | br->vlan_proto = htons(ETH_P_8021Q); | |
96a20d9d | 711 | br->default_pvid = 1; |
5be5a2df VY |
712 | return br_vlan_add(br, 1, |
713 | BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED); | |
8580e211 TM |
714 | } |
715 | ||
8adff41c TM |
716 | /* Must be protected by RTNL. |
717 | * Must be called with vid in range from 1 to 4094 inclusive. | |
718 | */ | |
552406c4 | 719 | int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags) |
243a2e63 VY |
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) | |
552406c4 | 728 | return __vlan_add(pv, vid, flags); |
243a2e63 VY |
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; | |
552406c4 | 740 | err = __vlan_add(pv, vid, flags); |
243a2e63 VY |
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 | ||
8adff41c TM |
752 | /* Must be protected by RTNL. |
753 | * Must be called with vid in range from 1 to 4094 inclusive. | |
754 | */ | |
243a2e63 VY |
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 | ||
424bb9c9 | 765 | br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid); |
1ea2d020 | 766 | br_fdb_delete_by_port(port->br, port, vid, 0); |
bc9a25d2 | 767 | |
243a2e63 VY |
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; | |
dbbaf949 | 774 | u16 vid; |
243a2e63 VY |
775 | |
776 | ASSERT_RTNL(); | |
777 | ||
778 | pv = rtnl_dereference(port->vlan_info); | |
779 | if (!pv) | |
780 | return; | |
781 | ||
dbbaf949 | 782 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) |
8580e211 | 783 | vlan_vid_del(port->dev, port->br->vlan_proto, vid); |
dbbaf949 | 784 | |
243a2e63 VY |
785 | __vlan_flush(pv); |
786 | } | |
bc9a25d2 VY |
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 | } | |
5be5a2df VY |
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 | } |