0001
0002
0003
0004
0005
0006
0007 #ifndef _LINUX_IF_VLAN_H_
0008 #define _LINUX_IF_VLAN_H_
0009
0010 #include <linux/netdevice.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/rtnetlink.h>
0013 #include <linux/bug.h>
0014 #include <uapi/linux/if_vlan.h>
0015
0016 #define VLAN_HLEN 4
0017
0018
0019 #define VLAN_ETH_HLEN 18
0020 #define VLAN_ETH_ZLEN 64
0021
0022
0023
0024
0025 #define VLAN_ETH_DATA_LEN 1500
0026 #define VLAN_ETH_FRAME_LEN 1518
0027
0028 #define VLAN_MAX_DEPTH 8
0029
0030
0031
0032
0033
0034
0035 struct vlan_hdr {
0036 __be16 h_vlan_TCI;
0037 __be16 h_vlan_encapsulated_proto;
0038 };
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 struct vlan_ethhdr {
0049 struct_group(addrs,
0050 unsigned char h_dest[ETH_ALEN];
0051 unsigned char h_source[ETH_ALEN];
0052 );
0053 __be16 h_vlan_proto;
0054 __be16 h_vlan_TCI;
0055 __be16 h_vlan_encapsulated_proto;
0056 };
0057
0058 #include <linux/skbuff.h>
0059
0060 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
0061 {
0062 return (struct vlan_ethhdr *)skb_mac_header(skb);
0063 }
0064
0065 #define VLAN_PRIO_MASK 0xe000
0066 #define VLAN_PRIO_SHIFT 13
0067 #define VLAN_CFI_MASK 0x1000
0068 #define VLAN_VID_MASK 0x0fff
0069 #define VLAN_N_VID 4096
0070
0071
0072 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
0073
0074 static inline bool is_vlan_dev(const struct net_device *dev)
0075 {
0076 return dev->priv_flags & IFF_802_1Q_VLAN;
0077 }
0078
0079 #define skb_vlan_tag_present(__skb) ((__skb)->vlan_present)
0080 #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci)
0081 #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
0082 #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK))
0083 #define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
0084
0085 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
0086 {
0087 ASSERT_RTNL();
0088 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
0089 }
0090
0091 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
0092 {
0093 ASSERT_RTNL();
0094 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
0095 }
0096
0097 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
0098 {
0099 ASSERT_RTNL();
0100 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
0101 }
0102
0103 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
0104 {
0105 ASSERT_RTNL();
0106 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 struct vlan_pcpu_stats {
0121 u64_stats_t rx_packets;
0122 u64_stats_t rx_bytes;
0123 u64_stats_t rx_multicast;
0124 u64_stats_t tx_packets;
0125 u64_stats_t tx_bytes;
0126 struct u64_stats_sync syncp;
0127 u32 rx_errors;
0128 u32 tx_dropped;
0129 };
0130
0131 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
0132
0133 extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
0134 __be16 vlan_proto, u16 vlan_id);
0135 extern int vlan_for_each(struct net_device *dev,
0136 int (*action)(struct net_device *dev, int vid,
0137 void *arg), void *arg);
0138 extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
0139 extern u16 vlan_dev_vlan_id(const struct net_device *dev);
0140 extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
0141
0142
0143
0144
0145
0146
0147
0148 struct vlan_priority_tci_mapping {
0149 u32 priority;
0150 u16 vlan_qos;
0151 struct vlan_priority_tci_mapping *next;
0152 };
0153
0154 struct proc_dir_entry;
0155 struct netpoll;
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 struct vlan_dev_priv {
0173 unsigned int nr_ingress_mappings;
0174 u32 ingress_priority_map[8];
0175 unsigned int nr_egress_mappings;
0176 struct vlan_priority_tci_mapping *egress_priority_map[16];
0177
0178 __be16 vlan_proto;
0179 u16 vlan_id;
0180 u16 flags;
0181
0182 struct net_device *real_dev;
0183 netdevice_tracker dev_tracker;
0184
0185 unsigned char real_dev_addr[ETH_ALEN];
0186
0187 struct proc_dir_entry *dent;
0188 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
0189 #ifdef CONFIG_NET_POLL_CONTROLLER
0190 struct netpoll *netpoll;
0191 #endif
0192 };
0193
0194 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
0195 {
0196 return netdev_priv(dev);
0197 }
0198
0199 static inline u16
0200 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
0201 {
0202 struct vlan_priority_tci_mapping *mp;
0203
0204 smp_rmb();
0205
0206 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
0207 while (mp) {
0208 if (mp->priority == skprio) {
0209 return mp->vlan_qos;
0210
0211
0212 }
0213 mp = mp->next;
0214 }
0215 return 0;
0216 }
0217
0218 extern bool vlan_do_receive(struct sk_buff **skb);
0219
0220 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
0221 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
0222
0223 extern int vlan_vids_add_by_dev(struct net_device *dev,
0224 const struct net_device *by_dev);
0225 extern void vlan_vids_del_by_dev(struct net_device *dev,
0226 const struct net_device *by_dev);
0227
0228 extern bool vlan_uses_dev(const struct net_device *dev);
0229
0230 #else
0231 static inline struct net_device *
0232 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
0233 __be16 vlan_proto, u16 vlan_id)
0234 {
0235 return NULL;
0236 }
0237
0238 static inline int
0239 vlan_for_each(struct net_device *dev,
0240 int (*action)(struct net_device *dev, int vid, void *arg),
0241 void *arg)
0242 {
0243 return 0;
0244 }
0245
0246 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
0247 {
0248 BUG();
0249 return NULL;
0250 }
0251
0252 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
0253 {
0254 BUG();
0255 return 0;
0256 }
0257
0258 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
0259 {
0260 BUG();
0261 return 0;
0262 }
0263
0264 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
0265 u32 skprio)
0266 {
0267 return 0;
0268 }
0269
0270 static inline bool vlan_do_receive(struct sk_buff **skb)
0271 {
0272 return false;
0273 }
0274
0275 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
0276 {
0277 return 0;
0278 }
0279
0280 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
0281 {
0282 }
0283
0284 static inline int vlan_vids_add_by_dev(struct net_device *dev,
0285 const struct net_device *by_dev)
0286 {
0287 return 0;
0288 }
0289
0290 static inline void vlan_vids_del_by_dev(struct net_device *dev,
0291 const struct net_device *by_dev)
0292 {
0293 }
0294
0295 static inline bool vlan_uses_dev(const struct net_device *dev)
0296 {
0297 return false;
0298 }
0299 #endif
0300
0301
0302
0303
0304
0305
0306
0307 static inline bool eth_type_vlan(__be16 ethertype)
0308 {
0309 switch (ethertype) {
0310 case htons(ETH_P_8021Q):
0311 case htons(ETH_P_8021AD):
0312 return true;
0313 default:
0314 return false;
0315 }
0316 }
0317
0318 static inline bool vlan_hw_offload_capable(netdev_features_t features,
0319 __be16 proto)
0320 {
0321 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
0322 return true;
0323 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
0324 return true;
0325 return false;
0326 }
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
0341 __be16 vlan_proto, u16 vlan_tci,
0342 unsigned int mac_len)
0343 {
0344 struct vlan_ethhdr *veth;
0345
0346 if (skb_cow_head(skb, VLAN_HLEN) < 0)
0347 return -ENOMEM;
0348
0349 skb_push(skb, VLAN_HLEN);
0350
0351
0352 if (likely(mac_len > ETH_TLEN))
0353 memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
0354 skb->mac_header -= VLAN_HLEN;
0355
0356 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
0357
0358
0359 if (likely(mac_len >= ETH_TLEN)) {
0360
0361
0362
0363 veth->h_vlan_proto = vlan_proto;
0364 } else {
0365
0366
0367
0368 veth->h_vlan_encapsulated_proto = skb->protocol;
0369 }
0370
0371
0372 veth->h_vlan_TCI = htons(vlan_tci);
0373
0374 return 0;
0375 }
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 static inline int __vlan_insert_tag(struct sk_buff *skb,
0389 __be16 vlan_proto, u16 vlan_tci)
0390 {
0391 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
0410 __be16 vlan_proto,
0411 u16 vlan_tci,
0412 unsigned int mac_len)
0413 {
0414 int err;
0415
0416 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
0417 if (err) {
0418 dev_kfree_skb_any(skb);
0419 return NULL;
0420 }
0421 return skb;
0422 }
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
0439 __be16 vlan_proto, u16 vlan_tci)
0440 {
0441 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
0457 __be16 vlan_proto,
0458 u16 vlan_tci)
0459 {
0460 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
0461 if (skb)
0462 skb->protocol = vlan_proto;
0463 return skb;
0464 }
0465
0466
0467
0468
0469
0470
0471
0472 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
0473 {
0474 skb->vlan_present = 0;
0475 }
0476
0477
0478
0479
0480
0481
0482
0483
0484 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
0485 {
0486 dst->vlan_present = src->vlan_present;
0487 dst->vlan_proto = src->vlan_proto;
0488 dst->vlan_tci = src->vlan_tci;
0489 }
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
0501 {
0502 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
0503 skb_vlan_tag_get(skb));
0504 if (likely(skb))
0505 __vlan_hwaccel_clear_tag(skb);
0506 return skb;
0507 }
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
0518 __be16 vlan_proto, u16 vlan_tci)
0519 {
0520 skb->vlan_proto = vlan_proto;
0521 skb->vlan_tci = vlan_tci;
0522 skb->vlan_present = 1;
0523 }
0524
0525
0526
0527
0528
0529
0530
0531
0532 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
0533 {
0534 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
0535
0536 if (!eth_type_vlan(veth->h_vlan_proto))
0537 return -EINVAL;
0538
0539 *vlan_tci = ntohs(veth->h_vlan_TCI);
0540 return 0;
0541 }
0542
0543
0544
0545
0546
0547
0548
0549
0550 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
0551 u16 *vlan_tci)
0552 {
0553 if (skb_vlan_tag_present(skb)) {
0554 *vlan_tci = skb_vlan_tag_get(skb);
0555 return 0;
0556 } else {
0557 *vlan_tci = 0;
0558 return -EINVAL;
0559 }
0560 }
0561
0562
0563
0564
0565
0566
0567
0568
0569 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
0570 {
0571 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
0572 return __vlan_hwaccel_get_tag(skb, vlan_tci);
0573 } else {
0574 return __vlan_get_tag(skb, vlan_tci);
0575 }
0576 }
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
0588 int *depth)
0589 {
0590 unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
0591
0592
0593
0594
0595
0596 if (eth_type_vlan(type)) {
0597 if (vlan_depth) {
0598 if (WARN_ON(vlan_depth < VLAN_HLEN))
0599 return 0;
0600 vlan_depth -= VLAN_HLEN;
0601 } else {
0602 vlan_depth = ETH_HLEN;
0603 }
0604 do {
0605 struct vlan_hdr vhdr, *vh;
0606
0607 vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr);
0608 if (unlikely(!vh || !--parse_depth))
0609 return 0;
0610
0611 type = vh->h_vlan_encapsulated_proto;
0612 vlan_depth += VLAN_HLEN;
0613 } while (eth_type_vlan(type));
0614 }
0615
0616 if (depth)
0617 *depth = vlan_depth;
0618
0619 return type;
0620 }
0621
0622
0623
0624
0625
0626
0627
0628
0629 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
0630 {
0631 return __vlan_get_protocol(skb, skb->protocol, NULL);
0632 }
0633
0634
0635
0636
0637 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
0638 {
0639 if (!skip_vlan)
0640
0641
0642
0643 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
0644
0645 return vlan_get_protocol(skb);
0646 }
0647
0648 static inline void vlan_set_encap_proto(struct sk_buff *skb,
0649 struct vlan_hdr *vhdr)
0650 {
0651 __be16 proto;
0652 unsigned short *rawp;
0653
0654
0655
0656
0657
0658
0659 proto = vhdr->h_vlan_encapsulated_proto;
0660 if (eth_proto_is_802_3(proto)) {
0661 skb->protocol = proto;
0662 return;
0663 }
0664
0665 rawp = (unsigned short *)(vhdr + 1);
0666 if (*rawp == 0xFFFF)
0667
0668
0669
0670
0671
0672
0673
0674 skb->protocol = htons(ETH_P_802_3);
0675 else
0676
0677
0678
0679 skb->protocol = htons(ETH_P_802_2);
0680 }
0681
0682
0683
0684
0685
0686
0687
0688
0689 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
0690 {
0691 if (!skb_vlan_tag_present(skb) &&
0692 likely(!eth_type_vlan(skb->protocol)))
0693 return false;
0694
0695 return true;
0696 }
0697
0698
0699
0700
0701
0702
0703
0704
0705 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
0706 {
0707 __be16 protocol = skb->protocol;
0708
0709 if (!skb_vlan_tag_present(skb)) {
0710 struct vlan_ethhdr *veh;
0711
0712 if (likely(!eth_type_vlan(protocol)))
0713 return false;
0714
0715 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
0716 return false;
0717
0718 veh = (struct vlan_ethhdr *)skb->data;
0719 protocol = veh->h_vlan_encapsulated_proto;
0720 }
0721
0722 if (!eth_type_vlan(protocol))
0723 return false;
0724
0725 return true;
0726 }
0727
0728
0729
0730
0731
0732
0733
0734
0735 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
0736 netdev_features_t features)
0737 {
0738 if (skb_vlan_tagged_multi(skb)) {
0739
0740
0741
0742
0743
0744 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
0745 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
0746 NETIF_F_HW_VLAN_STAG_TX;
0747 }
0748
0749 return features;
0750 }
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
0762 const struct vlan_hdr *h2)
0763 {
0764 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
0765 return *(u32 *)h1 ^ *(u32 *)h2;
0766 #else
0767 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
0768 ((__force u32)h1->h_vlan_encapsulated_proto ^
0769 (__force u32)h2->h_vlan_encapsulated_proto);
0770 #endif
0771 }
0772 #endif