vlan_get_protocol() could not get network protocol if a skb has a 802.1ad vlan tag or multiple vlans, which caused incorrect checksum calculation in several drivers. Fix vlan_get_protocol() to retrieve network protocol instead of incorrect vlan protocol. As the logic is the same as skb_network_protocol(), create a common helper function __vlan_get_protocol() and call it from existing functions.
Unified · Split
| @@ -472,27 +472,59 @@ static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) · 471 unmodified lines | ||
| 472 | 472 | /** |
| 473 | 473 | * vlan_get_protocol - get protocol EtherType. |
| 474 | 474 | * @skb: skbuff to query |
| 475 | + * @type: first vlan protocol | |
| 476 | + * @depth: buffer to store length of eth and vlan tags in bytes | |
| 475 | 477 | * |
| 476 | 478 | * Returns the EtherType of the packet, regardless of whether it is |
| 477 | 479 | * vlan encapsulated (normal or hardware accelerated) or not. |
| 478 | 480 | */ |
| 479 | −static inline __be16 vlan_get_protocol(const struct sk_buff *skb) | |
| 481 | +static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, | |
| 482 | + int *depth) | |
| 480 | 483 | { |
| 481 | − __be16 protocol = 0; | |
| 482 | − | |
| 483 | − if (vlan_tx_tag_present(skb) || | |
| 484 | − skb->protocol != cpu_to_be16(ETH_P_8021Q)) | |
| 485 | − protocol = skb->protocol; | |
| 486 | − else { | |
| 487 | − __be16 proto, *protop; | |
| 488 | − protop = skb_header_pointer(skb, offsetof(struct vlan_ethhdr, | |
| 489 | − h_vlan_encapsulated_proto), | |
| 490 | − sizeof(proto), &proto); | |
| 491 | − if (likely(protop)) | |
| 492 | − protocol = *protop; | |
| 484 | + unsigned int vlan_depth = skb->mac_len; | |
| 485 | + | |
| 486 | + /* if type is 802.1Q/AD then the header should already be | |
| 487 | + * present at mac_len - VLAN_HLEN (if mac_len > 0), or at | |
| 488 | + * ETH_HLEN otherwise | |
| 489 | + */ | |
| 490 | + if (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) { | |
| 491 | + if (vlan_depth) { | |
| 492 | + if (WARN_ON(vlan_depth < VLAN_HLEN)) | |
| 493 | + return 0; | |
| 494 | + vlan_depth -= VLAN_HLEN; | |
| 495 | + } else { | |
| 496 | + vlan_depth = ETH_HLEN; | |
| 497 | + } | |
| 498 | + do { | |
| 499 | + struct vlan_hdr *vh; | |
| 500 | + | |
| 501 | + if (unlikely(!pskb_may_pull(skb, | |
| 502 | + vlan_depth + VLAN_HLEN))) | |
| 503 | + return 0; | |
| 504 | + | |
| 505 | + vh = (struct vlan_hdr *)(skb->data + vlan_depth); | |
| 506 | + type = vh->h_vlan_encapsulated_proto; | |
| 507 | + vlan_depth += VLAN_HLEN; | |
| 508 | + } while (type == htons(ETH_P_8021Q) || | |
| 509 | + type == htons(ETH_P_8021AD)); | |
| 493 | 510 | } |
| 494 | 511 | |
| 495 | − return protocol; | |
| 512 | + if (depth) | |
| 513 | + *depth = vlan_depth; | |
| 514 | + | |
| 515 | + return type; | |
| 516 | +} | |
| 517 | + | |
| 518 | +/** | |
| 519 | + * vlan_get_protocol - get protocol EtherType. | |
| 520 | + * @skb: skbuff to query | |
| 521 | + * | |
| 522 | + * Returns the EtherType of the packet, regardless of whether it is | |
| 523 | + * vlan encapsulated (normal or hardware accelerated) or not. | |
| 524 | + */ | |
| 525 | +static inline __be16 vlan_get_protocol(struct sk_buff *skb) | |
| 526 | +{ | |
| 527 | + return __vlan_get_protocol(skb, skb->protocol, NULL); | |
| 496 | 528 | } |
| 497 | 529 | |
| 498 | 530 | static inline void vlan_set_encap_proto(struct sk_buff *skb, |
| @@ -2352,7 +2352,6 @@ EXPORT_SYMBOL(skb_checksum_help); · 2351 unmodified lines | ||
| 2352 | 2352 | |
| 2353 | 2353 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
| 2354 | 2354 | { |
| 2355 | − unsigned int vlan_depth = skb->mac_len; | |
| 2356 | 2355 | __be16 type = skb->protocol; |
| 2357 | 2356 | |
| 2358 | 2357 | /* Tunnel gso handlers can set protocol to ethernet. */ |
| @@ -2366,35 +2365,7 @@ __be16 skb_network_protocol(struct sk_buff *skb, int *depth) · 7 unmodified lines | ||
| 2366 | 2365 | type = eth->h_proto; |
| 2367 | 2366 | } |
| 2368 | 2367 | |
| 2369 | − /* if skb->protocol is 802.1Q/AD then the header should already be | |
| 2370 | − * present at mac_len - VLAN_HLEN (if mac_len > 0), or at | |
| 2371 | − * ETH_HLEN otherwise | |
| 2372 | − */ | |
| 2373 | − if (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) { | |
| 2374 | − if (vlan_depth) { | |
| 2375 | − if (WARN_ON(vlan_depth < VLAN_HLEN)) | |
| 2376 | − return 0; | |
| 2377 | − vlan_depth -= VLAN_HLEN; | |
| 2378 | − } else { | |
| 2379 | − vlan_depth = ETH_HLEN; | |
| 2380 | − } | |
| 2381 | − do { | |
| 2382 | − struct vlan_hdr *vh; | |
| 2383 | − | |
| 2384 | − if (unlikely(!pskb_may_pull(skb, | |
| 2385 | − vlan_depth + VLAN_HLEN))) | |
| 2386 | − return 0; | |
| 2387 | − | |
| 2388 | − vh = (struct vlan_hdr *)(skb->data + vlan_depth); | |
| 2389 | − type = vh->h_vlan_encapsulated_proto; | |
| 2390 | − vlan_depth += VLAN_HLEN; | |
| 2391 | − } while (type == htons(ETH_P_8021Q) || | |
| 2392 | − type == htons(ETH_P_8021AD)); | |
| 2393 | − } | |
| 2394 | − | |
| 2395 | − *depth = vlan_depth; | |
| 2396 | − | |
| 2397 | − return type; | |
| 2368 | + return __vlan_get_protocol(skb, type, depth); | |
| 2398 | 2369 | } |
| 2399 | 2370 | |
| 2400 | 2371 | /** |