Commit b6a79208 authored by Shmulik Ladkani's avatar Shmulik Ladkani Committed by David S. Miller

net: skbuff: Limit skb_vlan_pop/push() to expect skb->data at mac header

skb_vlan_pop/push were too generic, trying to support the cases where
skb->data is at mac header, and cases where skb->data is arbitrarily
elsewhere.

Supporting an arbitrary skb->data was complex and bogus:
 - It failed to unwind skb->data to its original location post actual
   pop/push.
   (Also, semantic is not well defined for unwinding: If data was into
    the eth header, need to use same offset from start; But if data was
    at network header or beyond, need to adjust the original offset
    according to the push/pull)
 - It mangled the rcsum post actual push/pop, without taking into account
   that the eth bytes might already have been pulled out of the csum.

Most callers (ovs, bpf) already had their skb->data at mac_header upon
invoking skb_vlan_pop/push.
Last caller that failed to do so (act_vlan) has been recently fixed.

Therefore, to simplify things, no longer support arbitrary skb->data
inputs for skb_vlan_pop/push().

skb->data is expected to be exactly at mac_header; WARN otherwise.
Signed-off-by: default avatarShmulik Ladkani <shmulik.ladkani@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Pravin Shelar <pshelar@ovn.org>
Cc: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f39acc84
...@@ -4528,13 +4528,18 @@ EXPORT_SYMBOL(skb_ensure_writable); ...@@ -4528,13 +4528,18 @@ EXPORT_SYMBOL(skb_ensure_writable);
int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
{ {
struct vlan_hdr *vhdr; struct vlan_hdr *vhdr;
unsigned int offset = skb->data - skb_mac_header(skb); int offset = skb->data - skb_mac_header(skb);
int err; int err;
__skb_push(skb, offset); if (WARN_ONCE(offset,
"__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
offset)) {
return -EINVAL;
}
err = skb_ensure_writable(skb, VLAN_ETH_HLEN); err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
if (unlikely(err)) if (unlikely(err))
goto pull; return err;
skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
...@@ -4551,13 +4556,14 @@ int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) ...@@ -4551,13 +4556,14 @@ int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
skb_set_network_header(skb, ETH_HLEN); skb_set_network_header(skb, ETH_HLEN);
skb_reset_mac_len(skb); skb_reset_mac_len(skb);
pull:
__skb_pull(skb, offset);
return err; return err;
} }
EXPORT_SYMBOL(__skb_vlan_pop); EXPORT_SYMBOL(__skb_vlan_pop);
/* Pop a vlan tag either from hwaccel or from payload.
* Expects skb->data at mac header.
*/
int skb_vlan_pop(struct sk_buff *skb) int skb_vlan_pop(struct sk_buff *skb)
{ {
u16 vlan_tci; u16 vlan_tci;
...@@ -4588,29 +4594,30 @@ int skb_vlan_pop(struct sk_buff *skb) ...@@ -4588,29 +4594,30 @@ int skb_vlan_pop(struct sk_buff *skb)
} }
EXPORT_SYMBOL(skb_vlan_pop); EXPORT_SYMBOL(skb_vlan_pop);
/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
* Expects skb->data at mac header.
*/
int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
{ {
if (skb_vlan_tag_present(skb)) { if (skb_vlan_tag_present(skb)) {
unsigned int offset = skb->data - skb_mac_header(skb); int offset = skb->data - skb_mac_header(skb);
int err; int err;
/* __vlan_insert_tag expect skb->data pointing to mac header. if (WARN_ONCE(offset,
* So change skb->data before calling it and change back to "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
* original position later offset)) {
*/ return -EINVAL;
__skb_push(skb, offset); }
err = __vlan_insert_tag(skb, skb->vlan_proto, err = __vlan_insert_tag(skb, skb->vlan_proto,
skb_vlan_tag_get(skb)); skb_vlan_tag_get(skb));
if (err) { if (err)
__skb_pull(skb, offset);
return err; return err;
}
skb->protocol = skb->vlan_proto; skb->protocol = skb->vlan_proto;
skb->mac_len += VLAN_HLEN; skb->mac_len += VLAN_HLEN;
skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
__skb_pull(skb, offset);
} }
__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment