From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:26
Hans reported that packets injected by a correct-looking and trivial
libpcap-based program was not being accepted by wireguard. In
investigating that, I noticed that a few devices weren't properly
handling AF_PACKET- injected packes, and so this series introduces a bit
of shared infrastructure to support that.
The basic problem begins with socket(AF_PACKET, SOCK_RAW,
htons(ETH_P_ALL)) sockets. When sendto is called, AF_PACKET examines the
headers of the packet with this logic:
static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
{
if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
sock->type == SOCK_RAW) {
skb_reset_mac_header(skb);
skb->protocol = dev_parse_header_protocol(skb);
}
skb_probe_transport_header(skb);
}
The middle condition there triggers, and we jump to
dev_parse_header_protocol. Note that this is the only caller of
dev_parse_header_protocol in the kernel, and I assume it was designed
for this purpose:
static inline __be16 dev_parse_header_protocol(const struct sk_buff *skb)
{
const struct net_device *dev = skb->dev;
if (!dev->header_ops || !dev->header_ops->parse_protocol)
return 0;
return dev->header_ops->parse_protocol(skb);
}
Since AF_PACKET already knows which netdev the packet is going to, the
dev_parse_header_protocol function can see if that netdev has a way it
prefers to figure out the protocol from the header. This, again, is the
only use of parse_protocol in the kernel. At the moment, it's only used
with ethernet devices, via eth_header_parse_protocol. This makes sense,
as mostly people are used to AF_PACKET-injecting ethernet frames rather
than layer 3 frames. But with nothing in place for layer 3 netdevs, this
function winds up returning 0, and skb->protocol then is set to 0, and
then by the time it hits the netdev's ndo_start_xmit, the driver doesn't
know what to do with it.
This is a problem because drivers very much rely on skb->protocol being
correct, and routinely reject packets where it's incorrect. That's why
having this parsing happen for injected packets is quite important. In
wireguard, ipip, and ipip6, for example, packets from AF_PACKET are just
dropped entirely. For tun devices, it's sort of uglier, with the tun
"packet information" header being passed to userspace containing a bogus
protocol value. Some userspace programs are ill-equipped to deal with
that. (But of course, that doesn't happen with tap devices, which
benefit from the similar shared infrastructure for layer 2 netdevs,
further motiviating this patchset for layer 3 netdevs.)
This patchset addresses the issue by first adding a layer 3 header parse
function, much akin to the existing one for layer 2 packets, and then
adds a shared header_ops structure that, also much akin to the existing
one for layer 2 packets. Then it wires it up to a few immediate places
that stuck out as requiring it, and does a bit of cleanup.
This patchset seems like it's fixing real bugs, so it might be
appropriate for stable. But they're also very old bugs, so if you'd
rather not backport to stable, that'd make sense to me too.
Jason A. Donenfeld (5):
net: ip_tunnel: add header_ops for layer 3 devices
net: ipip: implement header_ops->parse_protocol for AF_PACKET
wireguard: implement header_ops->parse_protocol for AF_PACKET
wireguard: queueing: make use of ip_tunnel_parse_protocol
tun: implement header_ops->parse_protocol for AF_PACKET
drivers/net/tun.c | 2 ++
drivers/net/wireguard/device.c | 1 +
drivers/net/wireguard/queueing.h | 19 ++-----------------
drivers/net/wireguard/receive.c | 2 +-
include/net/ip_tunnels.h | 3 +++
net/ipv4/ip_tunnel_core.c | 19 +++++++++++++++++++
net/ipv4/ipip.c | 1 +
net/ipv6/ip6_tunnel.c | 1 +
8 files changed, 30 insertions(+), 18 deletions(-)
Cc: Hans Wippel <redacted>
--
2.27.0
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:31
Some devices that take straight up layer 3 packets benefit from having a
shared header_ops so that AF_PACKET sockets can inject packets that are
recognized. This shared infrastructure will be used by other drivers
that currently can't inject packets using AF_PACKET. It also exposes the
parser function, as it is useful in standalone form too.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/net/ip_tunnels.h | 3 +++
net/ipv4/ip_tunnel_core.c | 19 +++++++++++++++++++
2 files changed, 22 insertions(+)
@@ -844,3 +845,21 @@ void ip_tunnel_unneed_metadata(void)static_branch_dec(&ip_tunnel_metadata_cnt);}EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);++/* Returns either the correct skb->protocol value, or 0 if invalid. */+__be16ip_tunnel_parse_protocol(conststructsk_buff*skb)+{+if(skb_network_header(skb)>=skb->head&&+(skb_network_header(skb)+sizeof(structiphdr))<=skb_tail_pointer(skb)&&+ip_hdr(skb)->version==4)+returnhtons(ETH_P_IP);+if(skb_network_header(skb)>=skb->head&&+(skb_network_header(skb)+sizeof(structipv6hdr))<=skb_tail_pointer(skb)&&+ipv6_hdr(skb)->version==6)+returnhtons(ETH_P_IPV6);+return0;+}+EXPORT_SYMBOL(ip_tunnel_parse_protocol);++conststructheader_opsip_tunnel_header_ops={.parse_protocol=ip_tunnel_parse_protocol};+EXPORT_SYMBOL(ip_tunnel_header_ops);
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:32
Ipip uses skb->protocol to determine packet type, and bails out if it's
not set. For AF_PACKET injection, we need to support its call chain of:
packet_sendmsg -> packet_snd -> packet_parse_headers ->
dev_parse_header_protocol -> parse_protocol
Without a valid parse_protocol, this returns zero, and ipip rejects the
skb. So, this wires up the ip_tunnel handler for layer 3 packets for
that case.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
net/ipv4/ipip.c | 1 +
net/ipv6/ip6_tunnel.c | 1 +
2 files changed, 2 insertions(+)
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:36
WireGuard uses skb->protocol to determine packet type, and bails out if
it's not set or set to something it's not expecting. For AF_PACKET
injection, we need to support its call chain of:
packet_sendmsg -> packet_snd -> packet_parse_headers ->
dev_parse_header_protocol -> parse_protocol
Without a valid parse_protocol, this returns zero, and wireguard then
rejects the skb. So, this wires up the ip_tunnel handler for layer 3
packets for that case.
Reported-by: Hans Wippel <redacted>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/net/wireguard/device.c | 1 +
1 file changed, 1 insertion(+)
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:38
Now that wg_examine_packet_protocol has been added for general
consumption as ip_tunnel_parse_protocol, it's possible to remove
wg_examine_packet_protocol and simply use the new
ip_tunnel_parse_protocol function directly.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/net/wireguard/queueing.h | 19 ++-----------------
drivers/net/wireguard/receive.c | 2 +-
2 files changed, 3 insertions(+), 18 deletions(-)
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-27 08:07:40
The tun driver passes up skb->protocol to userspace in the form of PI headers.
For AF_PACKET injection, we need to support its call chain of:
packet_sendmsg -> packet_snd -> packet_parse_headers ->
dev_parse_header_protocol -> parse_protocol
Without a valid parse_protocol, this returns zero, and the tun driver
then gives userspace bogus values that it can't deal with.
Note that this isn't the case with tap, because tap already benefits
from the shared infrastructure for ethernet headers. But with tun,
there's nothing.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/net/tun.c | 2 ++
1 file changed, 2 insertions(+)
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com> Date: 2020-06-28 20:42:03
On Sat, Jun 27, 2020 at 4:07 AM Jason A. Donenfeld [off-list ref] wrote:
quoted hunk
Some devices that take straight up layer 3 packets benefit from having a
shared header_ops so that AF_PACKET sockets can inject packets that are
recognized. This shared infrastructure will be used by other drivers
that currently can't inject packets using AF_PACKET. It also exposes the
parser function, as it is useful in standalone form too.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/net/ip_tunnels.h | 3 +++
net/ipv4/ip_tunnel_core.c | 19 +++++++++++++++++++
2 files changed, 22 insertions(+)
It's not customary to append these when touching existing files.
Though I'm not sure what the policy is.
quoted hunk
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -844,3 +845,21 @@ void ip_tunnel_unneed_metadata(void) static_branch_dec(&ip_tunnel_metadata_cnt); } EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);++/* Returns either the correct skb->protocol value, or 0 if invalid. */+__be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)+{+ if (skb_network_header(skb) >= skb->head &&+ (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&+ ip_hdr(skb)->version == 4)+ return htons(ETH_P_IP);+ if (skb_network_header(skb) >= skb->head &&+ (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&+ ipv6_hdr(skb)->version == 6)+ return htons(ETH_P_IPV6);+ return 0;
Perhaps worth stressing that this works in a closed environment that
only handles IP, but not for arbitrary L3 tunnel devices that may
accept a broader set of inner protocols.
Even the ipip device supports MPLS as of commit 1b69e7e6c4da ("ipip:
support MPLS over IPv4"). When injecting MPLS + IP over packet
sockets, a label could easily be misinterpreted. That said, such
processes should pass the protocol explicitly to avoid
misclassification.
Applied strictly to ipip, ip6ip6 this definitely improves packet
socket support for those, and is similar to what tun does:
if (tun->flags & IFF_NO_PI) {
u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
Sit is another candidate.
From: David Miller <davem@davemloft.net> Date: 2020-06-30 00:17:58
We customarily don't add new Copyright lines for every change and
fix made to existing files, please get rid of that.
Also please add the sit.c code handling as indicated by Willem.
Thank you.
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2020-06-30 00:41:23
Hey Dave, Willem,
Sorry for the delay in getting back to you; this weekend was spent
moving across the state again, so not much time for emails.
We customarily don't add new Copyright lines for every change and
fix made to existing files, please get rid of that.
Sure, no problem. I had done that out of an abundance of caution
because the code came out of another file bearing the copyright, but I
don't care much about it, so it'll be removed in v2.
Also please add the sit.c code handling as indicated by Willem.
Nice catch, Willem. I'll do just that. Looks like I forgot about vti
as well. I'll send a v2 shortly with those and will do another pass
through net/ and drivers/net/. And if you think of others, let me
know.
Jason