Commit df7b6015 authored by David S. Miller's avatar David S. Miller

Merge branch 'asix-rx-mem-handling'

Mark Craske says:

====================
Improve ASIX RX memory allocation error handling

The ASIX RX handler algorithm is weak on error handling.
There is a design flaw in the ASIX RX handler algorithm because the
implementation for handling RX Ethernet frames for the DUB-E100 C1 can
have Ethernet frames spanning multiple URBs. This means that payload data
from more than 1 URB is sometimes needed to fill the socket buffer with a
complete Ethernet frame. When the URB with the start of an Ethernet frame
is received then an attempt is made to allocate a socket buffer. If the
memory allocation fails then the algorithm sets the buffer pointer member
to NULL and the function exits (no crash yet). Subsequently, the RX hander
is called again to process the next URB which assumes there is a socket
buffer available and the kernel crashes when there is no buffer.

This patchset implements an improvement to the RX handling algorithm to
avoid a crash when no memory is available for the socket buffer.

The patchset will apply cleanly to the net-next master branch but the
created kernel has not been tested. The driver was tested on ARM kernels
v3.8 and v3.14 for a commercial product.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents bab18991 6a570814
...@@ -168,7 +168,7 @@ struct asix_data { ...@@ -168,7 +168,7 @@ struct asix_data {
struct asix_rx_fixup_info { struct asix_rx_fixup_info {
struct sk_buff *ax_skb; struct sk_buff *ax_skb;
u32 header; u32 header;
u16 size; u16 remaining;
bool split_head; bool split_head;
}; };
......
...@@ -54,69 +54,101 @@ int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb, ...@@ -54,69 +54,101 @@ int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
struct asix_rx_fixup_info *rx) struct asix_rx_fixup_info *rx)
{ {
int offset = 0; int offset = 0;
u16 size;
/* When an Ethernet frame spans multiple URB socket buffers,
* do a sanity test for the Data header synchronisation.
* Attempt to detect the situation of the previous socket buffer having
* been truncated or a socket buffer was missing. These situations
* cause a discontinuity in the data stream and therefore need to avoid
* appending bad data to the end of the current netdev socket buffer.
* Also avoid unnecessarily discarding a good current netdev socket
* buffer.
*/
if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
offset = ((rx->remaining + 1) & 0xfffe) + sizeof(u32);
rx->header = get_unaligned_le32(skb->data + offset);
offset = 0;
size = (u16)(rx->header & 0x7ff);
if (size != ((~rx->header >> 16) & 0x7ff)) {
netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
rx->remaining);
if (rx->ax_skb) {
kfree_skb(rx->ax_skb);
rx->ax_skb = NULL;
/* Discard the incomplete netdev Ethernet frame
* and assume the Data header is at the start of
* the current URB socket buffer.
*/
}
rx->remaining = 0;
}
}
while (offset + sizeof(u16) <= skb->len) { while (offset + sizeof(u16) <= skb->len) {
u16 remaining = 0; u16 copy_length;
unsigned char *data; unsigned char *data;
if (!rx->size) { if (!rx->remaining) {
if ((skb->len - offset == sizeof(u16)) || if (skb->len - offset == sizeof(u16)) {
rx->split_head) {
if(!rx->split_head) {
rx->header = get_unaligned_le16( rx->header = get_unaligned_le16(
skb->data + offset); skb->data + offset);
rx->split_head = true; rx->split_head = true;
offset += sizeof(u16); offset += sizeof(u16);
break; break;
} else { }
if (rx->split_head == true) {
rx->header |= (get_unaligned_le16( rx->header |= (get_unaligned_le16(
skb->data + offset) skb->data + offset) << 16);
<< 16);
rx->split_head = false; rx->split_head = false;
offset += sizeof(u16); offset += sizeof(u16);
}
} else { } else {
rx->header = get_unaligned_le32(skb->data + rx->header = get_unaligned_le32(skb->data +
offset); offset);
offset += sizeof(u32); offset += sizeof(u32);
} }
/* get the packet length */ /* take frame length from Data header 32-bit word */
rx->size = (u16) (rx->header & 0x7ff); size = (u16)(rx->header & 0x7ff);
if (rx->size != ((~rx->header >> 16) & 0x7ff)) { if (size != ((~rx->header >> 16) & 0x7ff)) {
netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n", netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
rx->header, offset); rx->header, offset);
rx->size = 0;
return 0; return 0;
} }
rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
rx->size); netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
if (!rx->ax_skb) size);
return 0; return 0;
} }
if (rx->size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) { /* Sometimes may fail to get a netdev socket buffer but
netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n", * continue to process the URB socket buffer so that
rx->size); * synchronisation of the Ethernet frame Data header
kfree_skb(rx->ax_skb); * word is maintained.
rx->ax_skb = NULL; */
rx->size = 0U; rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
return 0; rx->remaining = size;
} }
if (rx->size > skb->len - offset) { if (rx->remaining > skb->len - offset) {
remaining = rx->size - (skb->len - offset); copy_length = skb->len - offset;
rx->size = skb->len - offset; rx->remaining -= copy_length;
} else {
copy_length = rx->remaining;
rx->remaining = 0;
} }
data = skb_put(rx->ax_skb, rx->size); if (rx->ax_skb) {
memcpy(data, skb->data + offset, rx->size); data = skb_put(rx->ax_skb, copy_length);
if (!remaining) memcpy(data, skb->data + offset, copy_length);
if (!rx->remaining)
usbnet_skb_return(dev, rx->ax_skb); usbnet_skb_return(dev, rx->ax_skb);
}
offset += (rx->size + 1) & 0xfffe; offset += (copy_length + 1) & 0xfffe;
rx->size = remaining;
} }
if (skb->len != offset) { if (skb->len != offset) {
......
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