1
2
3
4
5
6
7#ifndef _LINUX_IF_VLAN_H_
8#define _LINUX_IF_VLAN_H_
9
10#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/rtnetlink.h>
13#include <linux/bug.h>
14#include <uapi/linux/if_vlan.h>
15
16#define VLAN_HLEN 4
17
18
19#define VLAN_ETH_HLEN 18
20#define VLAN_ETH_ZLEN 64
21
22
23
24
25#define VLAN_ETH_DATA_LEN 1500
26#define VLAN_ETH_FRAME_LEN 1518
27
28#define VLAN_MAX_DEPTH 8
29
30
31
32
33
34
35struct vlan_hdr {
36 __be16 h_vlan_TCI;
37 __be16 h_vlan_encapsulated_proto;
38};
39
40
41
42
43
44
45
46
47
48struct vlan_ethhdr {
49 unsigned char h_dest[ETH_ALEN];
50 unsigned char h_source[ETH_ALEN];
51 __be16 h_vlan_proto;
52 __be16 h_vlan_TCI;
53 __be16 h_vlan_encapsulated_proto;
54};
55
56#include <linux/skbuff.h>
57
58static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
59{
60 return (struct vlan_ethhdr *)skb_mac_header(skb);
61}
62
63#define VLAN_PRIO_MASK 0xe000
64#define VLAN_PRIO_SHIFT 13
65#define VLAN_CFI_MASK 0x1000
66#define VLAN_VID_MASK 0x0fff
67#define VLAN_N_VID 4096
68
69
70extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
71
72static inline bool is_vlan_dev(const struct net_device *dev)
73{
74 return dev->priv_flags & IFF_802_1Q_VLAN;
75}
76
77#define skb_vlan_tag_present(__skb) ((__skb)->vlan_present)
78#define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci)
79#define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
80#define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK))
81#define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
82
83static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
84{
85 ASSERT_RTNL();
86 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
87}
88
89static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
90{
91 ASSERT_RTNL();
92 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
93}
94
95static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
96{
97 ASSERT_RTNL();
98 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
99}
100
101static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
102{
103 ASSERT_RTNL();
104 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
105}
106
107
108
109
110
111
112
113
114
115
116
117
118struct vlan_pcpu_stats {
119 u64 rx_packets;
120 u64 rx_bytes;
121 u64 rx_multicast;
122 u64 tx_packets;
123 u64 tx_bytes;
124 struct u64_stats_sync syncp;
125 u32 rx_errors;
126 u32 tx_dropped;
127};
128
129#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
130
131extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
132 __be16 vlan_proto, u16 vlan_id);
133extern int vlan_for_each(struct net_device *dev,
134 int (*action)(struct net_device *dev, int vid,
135 void *arg), void *arg);
136extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
137extern u16 vlan_dev_vlan_id(const struct net_device *dev);
138extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
139
140
141
142
143
144
145
146struct vlan_priority_tci_mapping {
147 u32 priority;
148 u16 vlan_qos;
149 struct vlan_priority_tci_mapping *next;
150};
151
152struct proc_dir_entry;
153struct netpoll;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169struct vlan_dev_priv {
170 unsigned int nr_ingress_mappings;
171 u32 ingress_priority_map[8];
172 unsigned int nr_egress_mappings;
173 struct vlan_priority_tci_mapping *egress_priority_map[16];
174
175 __be16 vlan_proto;
176 u16 vlan_id;
177 u16 flags;
178
179 struct net_device *real_dev;
180 unsigned char real_dev_addr[ETH_ALEN];
181
182 struct proc_dir_entry *dent;
183 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
184#ifdef CONFIG_NET_POLL_CONTROLLER
185 struct netpoll *netpoll;
186#endif
187};
188
189static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
190{
191 return netdev_priv(dev);
192}
193
194static inline u16
195vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
196{
197 struct vlan_priority_tci_mapping *mp;
198
199 smp_rmb();
200
201 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
202 while (mp) {
203 if (mp->priority == skprio) {
204 return mp->vlan_qos;
205
206
207 }
208 mp = mp->next;
209 }
210 return 0;
211}
212
213extern bool vlan_do_receive(struct sk_buff **skb);
214
215extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
216extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
217
218extern int vlan_vids_add_by_dev(struct net_device *dev,
219 const struct net_device *by_dev);
220extern void vlan_vids_del_by_dev(struct net_device *dev,
221 const struct net_device *by_dev);
222
223extern bool vlan_uses_dev(const struct net_device *dev);
224
225#else
226static inline struct net_device *
227__vlan_find_dev_deep_rcu(struct net_device *real_dev,
228 __be16 vlan_proto, u16 vlan_id)
229{
230 return NULL;
231}
232
233static inline int
234vlan_for_each(struct net_device *dev,
235 int (*action)(struct net_device *dev, int vid, void *arg),
236 void *arg)
237{
238 return 0;
239}
240
241static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
242{
243 BUG();
244 return NULL;
245}
246
247static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
248{
249 BUG();
250 return 0;
251}
252
253static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
254{
255 BUG();
256 return 0;
257}
258
259static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
260 u32 skprio)
261{
262 return 0;
263}
264
265static inline bool vlan_do_receive(struct sk_buff **skb)
266{
267 return false;
268}
269
270static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
271{
272 return 0;
273}
274
275static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
276{
277}
278
279static inline int vlan_vids_add_by_dev(struct net_device *dev,
280 const struct net_device *by_dev)
281{
282 return 0;
283}
284
285static inline void vlan_vids_del_by_dev(struct net_device *dev,
286 const struct net_device *by_dev)
287{
288}
289
290static inline bool vlan_uses_dev(const struct net_device *dev)
291{
292 return false;
293}
294#endif
295
296
297
298
299
300
301
302static inline bool eth_type_vlan(__be16 ethertype)
303{
304 switch (ethertype) {
305 case htons(ETH_P_8021Q):
306 case htons(ETH_P_8021AD):
307 return true;
308 default:
309 return false;
310 }
311}
312
313static inline bool vlan_hw_offload_capable(netdev_features_t features,
314 __be16 proto)
315{
316 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
317 return true;
318 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
319 return true;
320 return false;
321}
322
323
324
325
326
327
328
329
330
331
332
333
334
335static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
336 __be16 vlan_proto, u16 vlan_tci,
337 unsigned int mac_len)
338{
339 struct vlan_ethhdr *veth;
340
341 if (skb_cow_head(skb, VLAN_HLEN) < 0)
342 return -ENOMEM;
343
344 skb_push(skb, VLAN_HLEN);
345
346
347 if (likely(mac_len > ETH_TLEN))
348 memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
349 skb->mac_header -= VLAN_HLEN;
350
351 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
352
353
354 if (likely(mac_len >= ETH_TLEN)) {
355
356
357
358 veth->h_vlan_proto = vlan_proto;
359 } else {
360
361
362
363 veth->h_vlan_encapsulated_proto = skb->protocol;
364 }
365
366
367 veth->h_vlan_TCI = htons(vlan_tci);
368
369 return 0;
370}
371
372
373
374
375
376
377
378
379
380
381
382
383static inline int __vlan_insert_tag(struct sk_buff *skb,
384 __be16 vlan_proto, u16 vlan_tci)
385{
386 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
387}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
405 __be16 vlan_proto,
406 u16 vlan_tci,
407 unsigned int mac_len)
408{
409 int err;
410
411 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
412 if (err) {
413 dev_kfree_skb_any(skb);
414 return NULL;
415 }
416 return skb;
417}
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
434 __be16 vlan_proto, u16 vlan_tci)
435{
436 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
437}
438
439
440
441
442
443
444
445
446
447
448
449
450
451static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
452 __be16 vlan_proto,
453 u16 vlan_tci)
454{
455 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
456 if (skb)
457 skb->protocol = vlan_proto;
458 return skb;
459}
460
461
462
463
464
465
466
467static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
468{
469 skb->vlan_present = 0;
470}
471
472
473
474
475
476
477
478
479static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
480{
481 dst->vlan_present = src->vlan_present;
482 dst->vlan_proto = src->vlan_proto;
483 dst->vlan_tci = src->vlan_tci;
484}
485
486
487
488
489
490
491
492
493
494
495static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
496{
497 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
498 skb_vlan_tag_get(skb));
499 if (likely(skb))
500 __vlan_hwaccel_clear_tag(skb);
501 return skb;
502}
503
504
505
506
507
508
509
510
511
512static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
513 __be16 vlan_proto, u16 vlan_tci)
514{
515 skb->vlan_proto = vlan_proto;
516 skb->vlan_tci = vlan_tci;
517 skb->vlan_present = 1;
518}
519
520
521
522
523
524
525
526
527static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
528{
529 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
530
531 if (!eth_type_vlan(veth->h_vlan_proto))
532 return -EINVAL;
533
534 *vlan_tci = ntohs(veth->h_vlan_TCI);
535 return 0;
536}
537
538
539
540
541
542
543
544
545static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
546 u16 *vlan_tci)
547{
548 if (skb_vlan_tag_present(skb)) {
549 *vlan_tci = skb_vlan_tag_get(skb);
550 return 0;
551 } else {
552 *vlan_tci = 0;
553 return -EINVAL;
554 }
555}
556
557
558
559
560
561
562
563
564static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
565{
566 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
567 return __vlan_hwaccel_get_tag(skb, vlan_tci);
568 } else {
569 return __vlan_get_tag(skb, vlan_tci);
570 }
571}
572
573
574
575
576
577
578
579
580
581
582static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
583 int *depth)
584{
585 unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
586
587
588
589
590
591 if (eth_type_vlan(type)) {
592 if (vlan_depth) {
593 if (WARN_ON(vlan_depth < VLAN_HLEN))
594 return 0;
595 vlan_depth -= VLAN_HLEN;
596 } else {
597 vlan_depth = ETH_HLEN;
598 }
599 do {
600 struct vlan_hdr vhdr, *vh;
601
602 vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
603 if (unlikely(!vh || !--parse_depth))
604 return 0;
605
606 type = vh->h_vlan_encapsulated_proto;
607 vlan_depth += VLAN_HLEN;
608 } while (eth_type_vlan(type));
609 }
610
611 if (depth)
612 *depth = vlan_depth;
613
614 return type;
615}
616
617
618
619
620
621
622
623
624static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
625{
626 return __vlan_get_protocol(skb, skb->protocol, NULL);
627}
628
629
630
631
632static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
633{
634 if (!skip_vlan)
635
636
637
638 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
639
640 return vlan_get_protocol(skb);
641}
642
643static inline void vlan_set_encap_proto(struct sk_buff *skb,
644 struct vlan_hdr *vhdr)
645{
646 __be16 proto;
647 unsigned short *rawp;
648
649
650
651
652
653
654 proto = vhdr->h_vlan_encapsulated_proto;
655 if (eth_proto_is_802_3(proto)) {
656 skb->protocol = proto;
657 return;
658 }
659
660 rawp = (unsigned short *)(vhdr + 1);
661 if (*rawp == 0xFFFF)
662
663
664
665
666
667
668
669 skb->protocol = htons(ETH_P_802_3);
670 else
671
672
673
674 skb->protocol = htons(ETH_P_802_2);
675}
676
677
678
679
680
681
682
683
684static inline bool skb_vlan_tagged(const struct sk_buff *skb)
685{
686 if (!skb_vlan_tag_present(skb) &&
687 likely(!eth_type_vlan(skb->protocol)))
688 return false;
689
690 return true;
691}
692
693
694
695
696
697
698
699
700static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
701{
702 __be16 protocol = skb->protocol;
703
704 if (!skb_vlan_tag_present(skb)) {
705 struct vlan_ethhdr *veh;
706
707 if (likely(!eth_type_vlan(protocol)))
708 return false;
709
710 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
711 return false;
712
713 veh = (struct vlan_ethhdr *)skb->data;
714 protocol = veh->h_vlan_encapsulated_proto;
715 }
716
717 if (!eth_type_vlan(protocol))
718 return false;
719
720 return true;
721}
722
723
724
725
726
727
728
729
730static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
731 netdev_features_t features)
732{
733 if (skb_vlan_tagged_multi(skb)) {
734
735
736
737
738
739 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
740 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
741 NETIF_F_HW_VLAN_STAG_TX;
742 }
743
744 return features;
745}
746
747
748
749
750
751
752
753
754
755
756static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
757 const struct vlan_hdr *h2)
758{
759#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
760 return *(u32 *)h1 ^ *(u32 *)h2;
761#else
762 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
763 ((__force u32)h1->h_vlan_encapsulated_proto ^
764 (__force u32)h2->h_vlan_encapsulated_proto);
765#endif
766}
767#endif
768