Correct .gbs.conf settings
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / bridge / br_vlan.c
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
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
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
35 static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
36 {
37         struct net_bridge_port *p = NULL;
38         struct net_bridge *br;
39         struct net_device *dev;
40         int err;
41
42         if (test_bit(vid, v->vlan_bitmap)) {
43                 __vlan_add_flags(v, vid, flags);
44                 return 0;
45         }
46
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         }
55
56         if (p) {
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                  */
63                 err = vlan_vid_add(dev, htons(ETH_P_8021Q), vid);
64                 if (err)
65                         return err;
66         }
67
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;
73         }
74
75         set_bit(vid, v->vlan_bitmap);
76         v->num_vlans++;
77         __vlan_add_flags(v, vid, flags);
78
79         return 0;
80
81 out_filt:
82         if (p)
83                 vlan_vid_del(dev, htons(ETH_P_8021Q), vid);
84         return err;
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
92         __vlan_delete_pvid(v, vid);
93         clear_bit(vid, v->untagged_bitmap);
94
95         if (v->port_idx)
96                 vlan_vid_del(v->parent.port->dev, htons(ETH_P_8021Q), vid);
97
98         clear_bit(vid, v->vlan_bitmap);
99         v->num_vlans--;
100         if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
101                 if (v->port_idx)
102                         rcu_assign_pointer(v->parent.port->vlan_info, NULL);
103                 else
104                         rcu_assign_pointer(v->parent.br->vlan_info, NULL);
105                 kfree_rcu(v, rcu);
106         }
107         return 0;
108 }
109
110 static void __vlan_flush(struct net_port_vlans *v)
111 {
112         smp_wmb();
113         v->pvid = 0;
114         bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
115         if (v->port_idx)
116                 rcu_assign_pointer(v->parent.port->vlan_info, NULL);
117         else
118                 rcu_assign_pointer(v->parent.br->vlan_info, NULL);
119         kfree_rcu(v, rcu);
120 }
121
122 struct sk_buff *br_handle_vlan(struct net_bridge *br,
123                                const struct net_port_vlans *pv,
124                                struct sk_buff *skb)
125 {
126         u16 vid;
127
128         /* If this packet was not filtered at input, let it pass */
129         if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
130                 goto out;
131
132         /* Vlan filter table must be configured at this point.  The
133          * only exception is the bridge is set in promisc mode and the
134          * packet is destined for the bridge device.  In this case
135          * pass the packet as is.
136          */
137         if (!pv) {
138                 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
139                         goto out;
140                 } else {
141                         kfree_skb(skb);
142                         return NULL;
143                 }
144         }
145
146         /* At this point, we know that the frame was filtered and contains
147          * a valid vlan id.  If the vlan id is set in the untagged bitmap,
148          * send untagged; otherwise, send tagged.
149          */
150         br_vlan_get_tag(skb, &vid);
151         if (test_bit(vid, pv->untagged_bitmap))
152                 skb->vlan_tci = 0;
153
154 out:
155         return skb;
156 }
157
158 /* Called under RCU */
159 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
160                         struct sk_buff *skb, u16 *vid)
161 {
162         int err;
163
164         /* If VLAN filtering is disabled on the bridge, all packets are
165          * permitted.
166          */
167         if (!br->vlan_enabled) {
168                 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
169                 return true;
170         }
171
172         /* If there are no vlan in the permitted list, all packets are
173          * rejected.
174          */
175         if (!v)
176                 goto drop;
177
178         BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
179
180         /* If vlan tx offload is disabled on bridge device and frame was
181          * sent from vlan device on the bridge device, it does not have
182          * HW accelerated vlan tag.
183          */
184         if (unlikely(!vlan_tx_tag_present(skb) &&
185                      (skb->protocol == htons(ETH_P_8021Q) ||
186                       skb->protocol == htons(ETH_P_8021AD)))) {
187                 skb = skb_vlan_untag(skb);
188                 if (unlikely(!skb))
189                         return false;
190         }
191
192         err = br_vlan_get_tag(skb, vid);
193         if (!*vid) {
194                 u16 pvid = br_get_pvid(v);
195
196                 /* Frame had a tag with VID 0 or did not have a tag.
197                  * See if pvid is set on this port.  That tells us which
198                  * vlan untagged or priority-tagged traffic belongs to.
199                  */
200                 if (pvid == VLAN_N_VID)
201                         goto drop;
202
203                 /* PVID is set on this port.  Any untagged or priority-tagged
204                  * ingress frame is considered to belong to this vlan.
205                  */
206                 *vid = pvid;
207                 if (likely(err))
208                         /* Untagged Frame. */
209                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
210                 else
211                         /* Priority-tagged Frame.
212                          * At this point, We know that skb->vlan_tci had
213                          * VLAN_TAG_PRESENT bit and its VID field was 0x000.
214                          * We update only VID field and preserve PCP field.
215                          */
216                         skb->vlan_tci |= pvid;
217
218                 return true;
219         }
220
221         /* Frame had a valid vlan tag.  See if vlan is allowed */
222         if (test_bit(*vid, v->vlan_bitmap))
223                 return true;
224 drop:
225         kfree_skb(skb);
226         return false;
227 }
228
229 /* Called under RCU. */
230 bool br_allowed_egress(struct net_bridge *br,
231                        const struct net_port_vlans *v,
232                        const struct sk_buff *skb)
233 {
234         u16 vid;
235
236         /* If this packet was not filtered at input, let it pass */
237         if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
238                 return true;
239
240         if (!v)
241                 return false;
242
243         br_vlan_get_tag(skb, &vid);
244         if (test_bit(vid, v->vlan_bitmap))
245                 return true;
246
247         return false;
248 }
249
250 /* Called under RCU */
251 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
252 {
253         struct net_bridge *br = p->br;
254         struct net_port_vlans *v;
255
256         /* If filtering was disabled at input, let it pass. */
257         if (!br->vlan_enabled)
258                 return true;
259
260         v = rcu_dereference(p->vlan_info);
261         if (!v)
262                 return false;
263
264         br_vlan_get_tag(skb, vid);
265         if (!*vid) {
266                 *vid = br_get_pvid(v);
267                 if (*vid == VLAN_N_VID)
268                         return false;
269
270                 return true;
271         }
272
273         if (test_bit(*vid, v->vlan_bitmap))
274                 return true;
275
276         return false;
277 }
278
279 /* Must be protected by RTNL.
280  * Must be called with vid in range from 1 to 4094 inclusive.
281  */
282 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
283 {
284         struct net_port_vlans *pv = NULL;
285         int err;
286
287         ASSERT_RTNL();
288
289         pv = rtnl_dereference(br->vlan_info);
290         if (pv)
291                 return __vlan_add(pv, vid, flags);
292
293         /* Create port vlan infomration
294          */
295         pv = kzalloc(sizeof(*pv), GFP_KERNEL);
296         if (!pv)
297                 return -ENOMEM;
298
299         pv->parent.br = br;
300         err = __vlan_add(pv, vid, flags);
301         if (err)
302                 goto out;
303
304         rcu_assign_pointer(br->vlan_info, pv);
305         return 0;
306 out:
307         kfree(pv);
308         return err;
309 }
310
311 /* Must be protected by RTNL.
312  * Must be called with vid in range from 1 to 4094 inclusive.
313  */
314 int br_vlan_delete(struct net_bridge *br, u16 vid)
315 {
316         struct net_port_vlans *pv;
317
318         ASSERT_RTNL();
319
320         pv = rtnl_dereference(br->vlan_info);
321         if (!pv)
322                 return -EINVAL;
323
324         br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
325
326         __vlan_del(pv, vid);
327         return 0;
328 }
329
330 void br_vlan_flush(struct net_bridge *br)
331 {
332         struct net_port_vlans *pv;
333
334         ASSERT_RTNL();
335         pv = rtnl_dereference(br->vlan_info);
336         if (!pv)
337                 return;
338
339         __vlan_flush(pv);
340 }
341
342 bool br_vlan_find(struct net_bridge *br, u16 vid)
343 {
344         struct net_port_vlans *pv;
345         bool found = false;
346
347         rcu_read_lock();
348         pv = rcu_dereference(br->vlan_info);
349
350         if (!pv)
351                 goto out;
352
353         if (test_bit(vid, pv->vlan_bitmap))
354                 found = true;
355
356 out:
357         rcu_read_unlock();
358         return found;
359 }
360
361 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
362 {
363         if (!rtnl_trylock())
364                 return restart_syscall();
365
366         if (br->vlan_enabled == val)
367                 goto unlock;
368
369         br->vlan_enabled = val;
370
371 unlock:
372         rtnl_unlock();
373         return 0;
374 }
375
376 /* Must be protected by RTNL.
377  * Must be called with vid in range from 1 to 4094 inclusive.
378  */
379 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
380 {
381         struct net_port_vlans *pv = NULL;
382         int err;
383
384         ASSERT_RTNL();
385
386         pv = rtnl_dereference(port->vlan_info);
387         if (pv)
388                 return __vlan_add(pv, vid, flags);
389
390         /* Create port vlan infomration
391          */
392         pv = kzalloc(sizeof(*pv), GFP_KERNEL);
393         if (!pv) {
394                 err = -ENOMEM;
395                 goto clean_up;
396         }
397
398         pv->port_idx = port->port_no;
399         pv->parent.port = port;
400         err = __vlan_add(pv, vid, flags);
401         if (err)
402                 goto clean_up;
403
404         rcu_assign_pointer(port->vlan_info, pv);
405         return 0;
406
407 clean_up:
408         kfree(pv);
409         return err;
410 }
411
412 /* Must be protected by RTNL.
413  * Must be called with vid in range from 1 to 4094 inclusive.
414  */
415 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
416 {
417         struct net_port_vlans *pv;
418
419         ASSERT_RTNL();
420
421         pv = rtnl_dereference(port->vlan_info);
422         if (!pv)
423                 return -EINVAL;
424
425         br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
426
427         return __vlan_del(pv, vid);
428 }
429
430 void nbp_vlan_flush(struct net_bridge_port *port)
431 {
432         struct net_port_vlans *pv;
433         u16 vid;
434
435         ASSERT_RTNL();
436
437         pv = rtnl_dereference(port->vlan_info);
438         if (!pv)
439                 return;
440
441         for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
442                 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
443
444         __vlan_flush(pv);
445 }
446
447 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
448 {
449         struct net_port_vlans *pv;
450         bool found = false;
451
452         rcu_read_lock();
453         pv = rcu_dereference(port->vlan_info);
454
455         if (!pv)
456                 goto out;
457
458         if (test_bit(vid, pv->vlan_bitmap))
459                 found = true;
460
461 out:
462         rcu_read_unlock();
463         return found;
464 }