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