Skip to content

Commit fe19c4f

Browse files
Eric Garverdavem330
authored andcommitted
vlan: Check for vlan ethernet types for 8021.q or 802.1ad
This is to simplify using double tagged vlans. This function allows all valid vlan ethertypes to be checked in a single function call. Also replace some instances that check for both ETH_P_8021Q and ETH_P_8021AD. Patch based on one originally by Thomas F Herbert. Signed-off-by: Thomas F Herbert <thomasfherbert@gmail.com> Signed-off-by: Eric Garver <e@erig.me> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8c146bb commit fe19c4f

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

‎include/linux/if_vlan.h‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ static inline int vlan_get_encap_level(struct net_device *dev)
272272
}
273273
#endif
274274

275+
/**
276+
* eth_type_vlan - check for valid vlan ether type.
277+
* @ethertype: ether type to check
278+
*
279+
* Returns true if the ether type is a vlan ether type.
280+
*/
281+
static inline bool eth_type_vlan(__be16 ethertype)
282+
{
283+
switch (ethertype) {
284+
case htons(ETH_P_8021Q):
285+
case htons(ETH_P_8021AD):
286+
return true;
287+
default:
288+
return false;
289+
}
290+
}
291+
275292
static inline bool vlan_hw_offload_capable(netdev_features_t features,
276293
__be16 proto)
277294
{
@@ -425,8 +442,7 @@ static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
425442
{
426443
struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
427444

428-
if (veth->h_vlan_proto != htons(ETH_P_8021Q) &&
429-
veth->h_vlan_proto != htons(ETH_P_8021AD))
445+
if (!eth_type_vlan(veth->h_vlan_proto))
430446
return -EINVAL;
431447

432448
*vlan_tci = ntohs(veth->h_vlan_TCI);
@@ -488,7 +504,7 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
488504
* present at mac_len - VLAN_HLEN (if mac_len > 0), or at
489505
* ETH_HLEN otherwise
490506
*/
491-
if (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
507+
if (eth_type_vlan(type)) {
492508
if (vlan_depth) {
493509
if (WARN_ON(vlan_depth < VLAN_HLEN))
494510
return 0;
@@ -506,8 +522,7 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
506522
vh = (struct vlan_hdr *)(skb->data + vlan_depth);
507523
type = vh->h_vlan_encapsulated_proto;
508524
vlan_depth += VLAN_HLEN;
509-
} while (type == htons(ETH_P_8021Q) ||
510-
type == htons(ETH_P_8021AD));
525+
} while (eth_type_vlan(type));
511526
}
512527

513528
if (depth)
@@ -572,8 +587,7 @@ static inline void vlan_set_encap_proto(struct sk_buff *skb,
572587
static inline bool skb_vlan_tagged(const struct sk_buff *skb)
573588
{
574589
if (!skb_vlan_tag_present(skb) &&
575-
likely(skb->protocol != htons(ETH_P_8021Q) &&
576-
skb->protocol != htons(ETH_P_8021AD)))
590+
likely(!eth_type_vlan(skb->protocol)))
577591
return false;
578592

579593
return true;
@@ -593,15 +607,14 @@ static inline bool skb_vlan_tagged_multi(const struct sk_buff *skb)
593607
if (!skb_vlan_tag_present(skb)) {
594608
struct vlan_ethhdr *veh;
595609

596-
if (likely(protocol != htons(ETH_P_8021Q) &&
597-
protocol != htons(ETH_P_8021AD)))
610+
if (likely(!eth_type_vlan(protocol)))
598611
return false;
599612

600613
veh = (struct vlan_ethhdr *)skb->data;
601614
protocol = veh->h_vlan_encapsulated_proto;
602615
}
603616

604-
if (protocol != htons(ETH_P_8021Q) && protocol != htons(ETH_P_8021AD))
617+
if (!eth_type_vlan(protocol))
605618
return false;
606619

607620
return true;

0 commit comments

Comments
 (0)