1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * VLAN An implementation of 802.1Q VLAN tagging. 4 * 5 * Authors: Ben Greear <greearb@candelatech.com> 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 /* The additional bytes required by VLAN 17 * (in addition to the Ethernet header) 18 */ 19 #define VLAN_ETH_HLEN 18 /* Total octets in header. */ 20 #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */ 21 22 /* 23 * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan 24 */ 25 #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */ 26 #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */ 27 28 #define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */ 29 30 /* 31 * struct vlan_hdr - vlan header 32 * @h_vlan_TCI: priority and VLAN ID 33 * @h_vlan_encapsulated_proto: packet type ID or len 34 */ 35 struct vlan_hdr { 36 __be16 h_vlan_TCI; 37 __be16 h_vlan_encapsulated_proto; 38 }; 39 40 /** 41 * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr) 42 * @h_dest: destination ethernet address 43 * @h_source: source ethernet address 44 * @h_vlan_proto: ethernet protocol 45 * @h_vlan_TCI: priority and VLAN ID 46 * @h_vlan_encapsulated_proto: packet type ID or len 47 */ 48 struct vlan_ethhdr { 49 struct_group(addrs, 50 unsigned char h_dest[ETH_ALEN]; 51 unsigned char h_source[ETH_ALEN]; 52 ); 53 __be16 h_vlan_proto; 54 __be16 h_vlan_TCI; 55 __be16 h_vlan_encapsulated_proto; 56 }; 57 58 #include <linux/skbuff.h> 59 60 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb) 61 { 62 return (struct vlan_ethhdr *)skb_mac_header(skb); 63 } 64 65 /* Prefer this version in TX path, instead of 66 * skb_reset_mac_header() + vlan_eth_hdr() 67 */ 68 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb) 69 { 70 return (struct vlan_ethhdr *)skb->data; 71 } 72 73 #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */ 74 #define VLAN_PRIO_SHIFT 13 75 #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */ 76 #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */ 77 #define VLAN_N_VID 4096 78 79 /* found in socket.c */ 80 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *)); 81 82 #define skb_vlan_tag_present(__skb) (!!(__skb)->vlan_all) 83 #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci) 84 #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK) 85 #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK)) 86 #define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT) 87 88 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev) 89 { 90 ASSERT_RTNL(); 91 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev)); 92 } 93 94 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev) 95 { 96 ASSERT_RTNL(); 97 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev); 98 } 99 100 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev) 101 { 102 ASSERT_RTNL(); 103 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev)); 104 } 105 106 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev) 107 { 108 ASSERT_RTNL(); 109 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev); 110 } 111 112 /** 113 * struct vlan_pcpu_stats - VLAN percpu rx/tx stats 114 * @rx_packets: number of received packets 115 * @rx_bytes: number of received bytes 116 * @rx_multicast: number of received multicast packets 117 * @tx_packets: number of transmitted packets 118 * @tx_bytes: number of transmitted bytes 119 * @syncp: synchronization point for 64bit counters 120 * @rx_errors: number of rx errors 121 * @tx_dropped: number of tx drops 122 */ 123 struct vlan_pcpu_stats { 124 u64_stats_t rx_packets; 125 u64_stats_t rx_bytes; 126 u64_stats_t rx_multicast; 127 u64_stats_t tx_packets; 128 u64_stats_t tx_bytes; 129 struct u64_stats_sync syncp; 130 u32 rx_errors; 131 u32 tx_dropped; 132 }; 133 134 #if IS_ENABLED(CONFIG_VLAN_8021Q) 135 136 extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev, 137 __be16 vlan_proto, u16 vlan_id); 138 extern int vlan_for_each(struct net_device *dev, 139 int (*action)(struct net_device *dev, int vid, 140 void *arg), void *arg); 141 extern struct net_device *vlan_dev_real_dev(const struct net_device *dev); 142 extern u16 vlan_dev_vlan_id(const struct net_device *dev); 143 extern __be16 vlan_dev_vlan_proto(const struct net_device *dev); 144 145 /** 146 * struct vlan_priority_tci_mapping - vlan egress priority mappings 147 * @priority: skb priority 148 * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000 149 * @next: pointer to next struct 150 * @rcu: used for deferred freeing of mapping nodes 151 */ 152 struct vlan_priority_tci_mapping { 153 u32 priority; 154 u16 vlan_qos; 155 struct vlan_priority_tci_mapping __rcu *next; 156 struct rcu_head rcu; 157 }; 158 159 struct proc_dir_entry; 160 struct netpoll; 161 162 /** 163 * struct vlan_dev_priv - VLAN private device data 164 * @nr_ingress_mappings: number of ingress priority mappings 165 * @ingress_priority_map: ingress priority mappings 166 * @nr_egress_mappings: number of egress priority mappings 167 * @egress_priority_map: hash of egress priority mappings 168 * @vlan_proto: VLAN encapsulation protocol 169 * @vlan_id: VLAN identifier 170 * @flags: device flags 171 * @real_dev: underlying netdevice 172 * @dev_tracker: refcount tracker for @real_dev reference 173 * @real_dev_addr: address of underlying netdevice 174 * @dent: proc dir entry 175 * @vlan_pcpu_stats: ptr to percpu rx stats 176 * @netpoll: netpoll instance "propagated" down to @real_dev 177 */ 178 struct vlan_dev_priv { 179 unsigned int nr_ingress_mappings; 180 u32 ingress_priority_map[8]; 181 unsigned int nr_egress_mappings; 182 struct vlan_priority_tci_mapping __rcu *egress_priority_map[16]; 183 184 __be16 vlan_proto; 185 u16 vlan_id; 186 u16 flags; 187 188 struct net_device *real_dev; 189 netdevice_tracker dev_tracker; 190 191 unsigned char real_dev_addr[ETH_ALEN]; 192 193 struct proc_dir_entry *dent; 194 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats; 195 #ifdef CONFIG_NET_POLL_CONTROLLER 196 struct netpoll *netpoll; 197 #endif 198 }; 199 200 static inline bool is_vlan_dev(const struct net_device *dev) 201 { 202 return dev->priv_flags & IFF_802_1Q_VLAN; 203 } 204 205 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev) 206 { 207 return netdev_priv(dev); 208 } 209 210 static inline u16 211 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio) 212 { 213 struct vlan_priority_tci_mapping *mp; 214 u16 vlan_qos = 0; 215 216 rcu_read_lock(); 217 218 mp = rcu_dereference(vlan_dev_priv(dev)->egress_priority_map[skprio & 0xF]); 219 while (mp) { 220 if (mp->priority == skprio) { 221 vlan_qos = READ_ONCE(mp->vlan_qos); 222 break; 223 } 224 mp = rcu_dereference(mp->next); 225 } 226 rcu_read_unlock(); 227 228 /* This should already be shifted to mask correctly with 229 * the VLAN's TCI. 230 */ 231 return vlan_qos; 232 } 233 234 extern bool vlan_do_receive(struct sk_buff **skb); 235 236 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid); 237 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid); 238 239 extern int vlan_vids_add_by_dev(struct net_device *dev, 240 const struct net_device *by_dev); 241 extern void vlan_vids_del_by_dev(struct net_device *dev, 242 const struct net_device *by_dev); 243 244 extern bool vlan_uses_dev(const struct net_device *dev); 245 246 #else 247 static inline bool is_vlan_dev(const struct net_device *dev) 248 { 249 return false; 250 } 251 252 static inline struct net_device * 253 __vlan_find_dev_deep_rcu(struct net_device *real_dev, 254 __be16 vlan_proto, u16 vlan_id) 255 { 256 return NULL; 257 } 258 259 static inline int 260 vlan_for_each(struct net_device *dev, 261 int (*action)(struct net_device *dev, int vid, void *arg), 262 void *arg) 263 { 264 return 0; 265 } 266 267 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev) 268 { 269 WARN_ON_ONCE(1); 270 return NULL; 271 } 272 273 static inline u16 vlan_dev_vlan_id(const struct net_device *dev) 274 { 275 WARN_ON_ONCE(1); 276 return 0; 277 } 278 279 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev) 280 { 281 WARN_ON_ONCE(1); 282 return 0; 283 } 284 285 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev, 286 u32 skprio) 287 { 288 return 0; 289 } 290 291 static inline bool vlan_do_receive(struct sk_buff **skb) 292 { 293 return false; 294 } 295 296 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid) 297 { 298 return 0; 299 } 300 301 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid) 302 { 303 } 304 305 static inline int vlan_vids_add_by_dev(struct net_device *dev, 306 const struct net_device *by_dev) 307 { 308 return 0; 309 } 310 311 static inline void vlan_vids_del_by_dev(struct net_device *dev, 312 const struct net_device *by_dev) 313 { 314 } 315 316 static inline bool vlan_uses_dev(const struct net_device *dev) 317 { 318 return false; 319 } 320 #endif 321 322 /** 323 * eth_type_vlan - check for valid vlan ether type. 324 * @ethertype: ether type to check 325 * 326 * Returns: true if the ether type is a vlan ether type. 327 */ 328 static inline bool eth_type_vlan(__be16 ethertype) 329 { 330 switch (ethertype) { 331 case htons(ETH_P_8021Q): 332 case htons(ETH_P_8021AD): 333 return true; 334 default: 335 return false; 336 } 337 } 338 339 static inline bool vlan_hw_offload_capable(netdev_features_t features, 340 __be16 proto) 341 { 342 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX) 343 return true; 344 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX) 345 return true; 346 return false; 347 } 348 349 /** 350 * __vlan_insert_inner_tag - inner VLAN tag inserting 351 * @skb: skbuff to tag 352 * @vlan_proto: VLAN encapsulation protocol 353 * @vlan_tci: VLAN TCI to insert 354 * @mac_len: MAC header length including outer vlan headers 355 * 356 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len 357 * Does not change skb->protocol so this function can be used during receive. 358 * 359 * Returns: error if skb_cow_head fails. 360 */ 361 static inline int __vlan_insert_inner_tag(struct sk_buff *skb, 362 __be16 vlan_proto, u16 vlan_tci, 363 unsigned int mac_len) 364 { 365 const u8 meta_len = mac_len > ETH_TLEN ? skb_metadata_len(skb) : 0; 366 struct vlan_ethhdr *veth; 367 368 if (unlikely(!pskb_may_pull(skb, mac_len))) 369 return -EINVAL; 370 371 if (skb_cow_head(skb, meta_len + VLAN_HLEN) < 0) 372 return -ENOMEM; 373 374 skb_push(skb, VLAN_HLEN); 375 376 /* Move the mac header sans proto to the beginning of the new header. */ 377 if (likely(mac_len > ETH_TLEN)) 378 skb_postpush_data_move(skb, VLAN_HLEN, mac_len - ETH_TLEN); 379 if (skb_mac_header_was_set(skb)) 380 skb->mac_header -= VLAN_HLEN; 381 382 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN); 383 384 /* first, the ethernet type */ 385 if (likely(mac_len >= ETH_TLEN)) { 386 /* h_vlan_encapsulated_proto should already be populated, and 387 * skb->data has space for h_vlan_proto 388 */ 389 veth->h_vlan_proto = vlan_proto; 390 } else { 391 /* h_vlan_encapsulated_proto should not be populated, and 392 * skb->data has no space for h_vlan_proto 393 */ 394 veth->h_vlan_encapsulated_proto = skb->protocol; 395 } 396 397 /* now, the TCI */ 398 veth->h_vlan_TCI = htons(vlan_tci); 399 400 return 0; 401 } 402 403 /** 404 * __vlan_insert_tag - regular VLAN tag inserting 405 * @skb: skbuff to tag 406 * @vlan_proto: VLAN encapsulation protocol 407 * @vlan_tci: VLAN TCI to insert 408 * 409 * Inserts the VLAN tag into @skb as part of the payload 410 * Does not change skb->protocol so this function can be used during receive. 411 * 412 * Returns: error if skb_cow_head fails. 413 */ 414 static inline int __vlan_insert_tag(struct sk_buff *skb, 415 __be16 vlan_proto, u16 vlan_tci) 416 { 417 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); 418 } 419 420 /** 421 * vlan_insert_inner_tag - inner VLAN tag inserting 422 * @skb: skbuff to tag 423 * @vlan_proto: VLAN encapsulation protocol 424 * @vlan_tci: VLAN TCI to insert 425 * @mac_len: MAC header length including outer vlan headers 426 * 427 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len 428 * Returns a VLAN tagged skb. This might change skb->head. 429 * 430 * Following the skb_unclone_full() example, in case of error, the calling 431 * function doesn't have to worry about freeing the original skb. 432 * 433 * Does not change skb->protocol so this function can be used during receive. 434 * 435 * Return: modified @skb on success, NULL on error (@skb is freed). 436 */ 437 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb, 438 __be16 vlan_proto, 439 u16 vlan_tci, 440 unsigned int mac_len) 441 { 442 int err; 443 444 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len); 445 if (err) { 446 dev_kfree_skb_any(skb); 447 return NULL; 448 } 449 return skb; 450 } 451 452 /** 453 * vlan_insert_tag - regular VLAN tag inserting 454 * @skb: skbuff to tag 455 * @vlan_proto: VLAN encapsulation protocol 456 * @vlan_tci: VLAN TCI to insert 457 * 458 * Inserts the VLAN tag into @skb as part of the payload 459 * Returns a VLAN tagged skb. This might change skb->head. 460 * 461 * Following the skb_unclone_full() example, in case of error, the calling 462 * function doesn't have to worry about freeing the original skb. 463 * 464 * Does not change skb->protocol so this function can be used during receive. 465 * 466 * Return: modified @skb on success, NULL on error (@skb is freed). 467 */ 468 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb, 469 __be16 vlan_proto, u16 vlan_tci) 470 { 471 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN); 472 } 473 474 /** 475 * vlan_insert_tag_set_proto - regular VLAN tag inserting 476 * @skb: skbuff to tag 477 * @vlan_proto: VLAN encapsulation protocol 478 * @vlan_tci: VLAN TCI to insert 479 * 480 * Inserts the VLAN tag into @skb as part of the payload 481 * Returns a VLAN tagged skb. This might change skb->head. 482 * 483 * Following the skb_unclone_full() example, in case of error, the calling 484 * function doesn't have to worry about freeing the original skb. 485 * 486 * Return: modified @skb on success, NULL on error (@skb is freed). 487 */ 488 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb, 489 __be16 vlan_proto, 490 u16 vlan_tci) 491 { 492 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci); 493 if (skb) 494 skb->protocol = vlan_proto; 495 return skb; 496 } 497 498 /** 499 * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info 500 * @skb: skbuff to clear 501 * 502 * Clears the VLAN information from @skb 503 */ 504 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb) 505 { 506 skb->vlan_all = 0; 507 } 508 509 /** 510 * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb 511 * @dst: skbuff to copy to 512 * @src: skbuff to copy from 513 * 514 * Copies VLAN information from @src to @dst (for branchless code) 515 */ 516 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src) 517 { 518 dst->vlan_all = src->vlan_all; 519 } 520 521 /* 522 * __vlan_hwaccel_push_inside - pushes vlan tag to the payload 523 * @skb: skbuff to tag 524 * 525 * Pushes the VLAN tag from @skb->vlan_tci inside to the payload. 526 * 527 * Following the skb_unclone_full() example, in case of error, the calling 528 * function doesn't have to worry about freeing the original skb. 529 */ 530 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb) 531 { 532 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, 533 skb_vlan_tag_get(skb)); 534 if (likely(skb)) 535 __vlan_hwaccel_clear_tag(skb); 536 return skb; 537 } 538 539 /** 540 * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting 541 * @skb: skbuff to tag 542 * @vlan_proto: VLAN encapsulation protocol 543 * @vlan_tci: VLAN TCI to insert 544 * 545 * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest 546 */ 547 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb, 548 __be16 vlan_proto, u16 vlan_tci) 549 { 550 skb->vlan_proto = vlan_proto; 551 skb->vlan_tci = vlan_tci; 552 } 553 554 /** 555 * __vlan_get_tag - get the VLAN ID that is part of the payload 556 * @skb: skbuff to query 557 * @vlan_tci: buffer to store value 558 * 559 * Returns: error if the skb is not of VLAN type 560 */ 561 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 562 { 563 struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb); 564 565 if (!eth_type_vlan(veth->h_vlan_proto)) 566 return -ENODATA; 567 568 *vlan_tci = ntohs(veth->h_vlan_TCI); 569 return 0; 570 } 571 572 /** 573 * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[] 574 * @skb: skbuff to query 575 * @vlan_tci: buffer to store value 576 * 577 * Returns: error if @skb->vlan_tci is not set correctly 578 */ 579 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb, 580 u16 *vlan_tci) 581 { 582 if (skb_vlan_tag_present(skb)) { 583 *vlan_tci = skb_vlan_tag_get(skb); 584 return 0; 585 } else { 586 *vlan_tci = 0; 587 return -ENODATA; 588 } 589 } 590 591 /** 592 * vlan_get_tag - get the VLAN ID from the skb 593 * @skb: skbuff to query 594 * @vlan_tci: buffer to store value 595 * 596 * Returns: error if the skb is not VLAN tagged 597 */ 598 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) 599 { 600 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) { 601 return __vlan_hwaccel_get_tag(skb, vlan_tci); 602 } else { 603 return __vlan_get_tag(skb, vlan_tci); 604 } 605 } 606 607 struct vlan_type_depth { 608 __be16 type; 609 u16 depth; 610 }; 611 612 struct vlan_type_depth __vlan_get_protocol_offset(const struct sk_buff *skb, 613 __be16 type, 614 int mac_offset); 615 616 /** 617 * vlan_get_protocol_offset_inline() - get protocol EtherType. 618 * @skb: skbuff to query 619 * @type: first vlan protocol 620 * @mac_offset: MAC offset 621 * @depth: buffer to store length of eth and vlan tags in bytes 622 * 623 * Returns: the EtherType of the packet, regardless of whether it is 624 * vlan encapsulated (normal or hardware accelerated) or not. 625 */ 626 static inline 627 __be16 vlan_get_protocol_offset_inline(const struct sk_buff *skb, 628 __be16 type, 629 int mac_offset, 630 int *depth) 631 { 632 if (eth_type_vlan(type)) { 633 struct vlan_type_depth res; 634 635 res = __vlan_get_protocol_offset(skb, type, mac_offset); 636 637 if (depth && res.type) 638 *depth = res.depth; 639 return res.type; 640 } 641 642 if (depth) 643 *depth = skb->mac_len; 644 645 return type; 646 } 647 648 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type, 649 int *depth) 650 { 651 return vlan_get_protocol_offset_inline(skb, type, 0, depth); 652 } 653 654 /** 655 * vlan_get_protocol - get protocol EtherType. 656 * @skb: skbuff to query 657 * 658 * Returns: the EtherType of the packet, regardless of whether it is 659 * vlan encapsulated (normal or hardware accelerated) or not. 660 */ 661 static inline __be16 vlan_get_protocol(const struct sk_buff *skb) 662 { 663 return __vlan_get_protocol(skb, skb->protocol, NULL); 664 } 665 666 /* This version of __vlan_get_protocol() also pulls mac header in skb->head */ 667 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb, 668 __be16 type, int *depth) 669 { 670 int maclen; 671 672 type = __vlan_get_protocol(skb, type, &maclen); 673 674 if (type) { 675 if (!pskb_may_pull(skb, maclen)) 676 type = 0; 677 else if (depth) 678 *depth = maclen; 679 } 680 return type; 681 } 682 683 /* A getter for the SKB protocol field which will handle VLAN tags consistently 684 * whether VLAN acceleration is enabled or not. 685 */ 686 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan) 687 { 688 if (!skip_vlan) 689 /* VLAN acceleration strips the VLAN header from the skb and 690 * moves it to skb->vlan_proto 691 */ 692 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol; 693 694 return vlan_get_protocol(skb); 695 } 696 697 static inline void vlan_set_encap_proto(struct sk_buff *skb, 698 struct vlan_hdr *vhdr) 699 { 700 __be16 proto; 701 702 /* 703 * Was a VLAN packet, grab the encapsulated protocol, which the layer 704 * three protocols care about. 705 */ 706 707 proto = vhdr->h_vlan_encapsulated_proto; 708 if (eth_proto_is_802_3(proto)) { 709 skb->protocol = proto; 710 return; 711 } 712 713 /* No ethertype: this is an 802.2 LLC frame (length field). */ 714 skb->protocol = htons(ETH_P_802_2); 715 } 716 717 /** 718 * vlan_remove_tag - remove outer VLAN tag from payload 719 * @skb: skbuff to remove tag from 720 * @vlan_tci: buffer to store value 721 * 722 * Expects the skb to contain a VLAN tag in the payload, and to have skb->data 723 * pointing at the MAC header. 724 */ 725 static inline void vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci) 726 { 727 struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 728 729 *vlan_tci = ntohs(vhdr->h_vlan_TCI); 730 731 vlan_set_encap_proto(skb, vhdr); 732 __skb_pull(skb, VLAN_HLEN); 733 skb_postpull_data_move(skb, VLAN_HLEN, 2 * ETH_ALEN); 734 } 735 736 /** 737 * skb_vlan_tagged - check if skb is vlan tagged. 738 * @skb: skbuff to query 739 * 740 * Returns: true if the skb is tagged, regardless of whether it is hardware 741 * accelerated or not. 742 */ 743 static inline bool skb_vlan_tagged(const struct sk_buff *skb) 744 { 745 if (!skb_vlan_tag_present(skb) && 746 likely(!eth_type_vlan(skb->protocol))) 747 return false; 748 749 return true; 750 } 751 752 /** 753 * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers. 754 * @skb: skbuff to query 755 * 756 * Returns: true if the skb is tagged with multiple vlan headers, regardless 757 * of whether it is hardware accelerated or not. 758 */ 759 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb) 760 { 761 __be16 protocol = skb->protocol; 762 763 if (!skb_vlan_tag_present(skb)) { 764 struct vlan_ethhdr *veh; 765 766 if (likely(!eth_type_vlan(protocol))) 767 return false; 768 769 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN))) 770 return false; 771 772 veh = skb_vlan_eth_hdr(skb); 773 protocol = veh->h_vlan_encapsulated_proto; 774 } 775 776 if (!eth_type_vlan(protocol)) 777 return false; 778 779 return true; 780 } 781 782 /** 783 * vlan_features_check - drop unsafe features for skb with multiple tags. 784 * @skb: skbuff to query 785 * @features: features to be checked 786 * 787 * Returns: features without unsafe ones if the skb has multiple tags. 788 */ 789 static inline netdev_features_t vlan_features_check(struct sk_buff *skb, 790 netdev_features_t features) 791 { 792 if (skb_vlan_tagged_multi(skb)) { 793 /* In the case of multi-tagged packets, use a direct mask 794 * instead of using netdev_interesect_features(), to make 795 * sure that only devices supporting NETIF_F_HW_CSUM will 796 * have checksum offloading support. 797 */ 798 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | 799 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX | 800 NETIF_F_HW_VLAN_STAG_TX; 801 } 802 803 return features; 804 } 805 806 /** 807 * compare_vlan_header - Compare two vlan headers 808 * @h1: Pointer to vlan header 809 * @h2: Pointer to vlan header 810 * 811 * Compare two vlan headers. 812 * 813 * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits. 814 * 815 * Return: 0 if equal, arbitrary non-zero value if not equal. 816 */ 817 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1, 818 const struct vlan_hdr *h2) 819 { 820 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 821 return *(u32 *)h1 ^ *(u32 *)h2; 822 #else 823 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) | 824 ((__force u32)h1->h_vlan_encapsulated_proto ^ 825 (__force u32)h2->h_vlan_encapsulated_proto); 826 #endif 827 } 828 #endif /* !(_LINUX_IF_VLAN_H_) */ 829