* net: usb: ipheth: add CDC NCM support
@ 2023-05-11 9:47 Georgi Valkov
2023-05-11 12:02 ` Forst
2023-05-11 14:40 ` Jakub Kicinski
0 siblings, 2 replies; 4+ messages in thread
From: Georgi Valkov @ 2023-05-11 9:47 UTC (permalink / raw)
To: Greg KH, Foster Snowhill, Linux Kernel Mailing List, Jan Kiszka,
Jakub Kicinski
Cc: Георги Георгиев Вълков
[-- Attachment #1: Type: text/plain, Size: 1438 bytes --]
Can you please review, and accept the following patch. I’ve been using it for one year.
I added the author Foster Snowhill to this mail, as well as a link to his Github [1].
If needed, we can ask him to add the tag Signed-off-by:
From 257d843a7f90e4e519635f82fe5b73fba918ec33 Mon Sep 17 00:00:00 2001
From: Foster Snowhill <Forst@users.noreply.github.com>
Date: Fri, 22 Apr 2022 23:50:09 +0200
Subject: [PATCH] net: usb: ipheth: add CDC NCM support (PoC)
Recent iOS releases support CDC NCM encapsulation on RX. This mode is
the default on macOS and Windows.
When reconnecting a device from a macOS/Windows machine to a Linux
machine, the device stays in NCM mode. Therefore to correctly support
such a device, the driver has to either support the NCM mode too, or
somehow put the device back into legacy mode.
To match the behaviour of the macOS/Windows driver, and since there
are no documented control commands to revert to legacy mode, I chose
to implement basic NCM support.
With this change, the device is attempted to be put into NCM mode by
default, and falls back to legacy mode if said attempt failed.
Tested-by: Georgi Valkov <gvalkov@abv.bg>
[1] https://github.com/Forst/linux-stable/commit/257d843a7f90e4e519635f82fe5b73fba918ec33#diff-d13f85b8acc1204f31e7abc538e9fdb1faa07b94cd928800b6b976561d32f6e3
This can be applied directly to Linux master, as well as 5.10 and 5.15 stable.
[-- Attachment #2: 0001-net-usb-ipheth-add-CDC-NCM-support-PoC.patch --]
[-- Type: application/octet-stream, Size: 10522 bytes --]
From 7d530b3969fb1a3eb34bcf83cbf1da86e9224a85 Mon Sep 17 00:00:00 2001
From: Foster Snowhill <Forst@users.noreply.github.com>
Date: Fri, 22 Apr 2022 23:50:09 +0200
Subject: [PATCH] net: usb: ipheth: add CDC NCM support (PoC)
Recent iOS releases support CDC NCM encapsulation on RX. This mode is
the default on macOS and Windows.
When reconnecting a device from a macOS/Windows machine to a Linux
machine, the device stays in NCM mode. Therefore to correctly support
such a device, the driver has to either support the NCM mode too, or
somehow put the device back into legacy mode.
To match the behaviour of the macOS/Windows driver, and since there
are no documented control commands to revert to legacy mode, I chose
to implement basic NCM support.
With this change, the device is attempted to be put into NCM mode by
default, and falls back to legacy mode if said attempt failed.
Tested-by: Georgi Valkov <gvalkov@abv.bg>
---
drivers/net/usb/ipheth.c | 175 +++++++++++++++++++++++++++++++--------
1 file changed, 142 insertions(+), 33 deletions(-)
diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c
index 6a769df0b421..4d05d8d6b5bc 100644
--- a/drivers/net/usb/ipheth.c
+++ b/drivers/net/usb/ipheth.c
@@ -52,6 +52,7 @@
#include <linux/ethtool.h>
#include <linux/usb.h>
#include <linux/workqueue.h>
+#include <linux/usb/cdc.h>
#define USB_VENDOR_APPLE 0x05ac
@@ -59,8 +60,11 @@
#define IPHETH_USBINTF_SUBCLASS 253
#define IPHETH_USBINTF_PROTO 1
-#define IPHETH_BUF_SIZE 1514
#define IPHETH_IP_ALIGN 2 /* padding at front of URB */
+#define IPHETH_NCM_HEADER_SIZE (12 + 96) /* NCMH + NCM0 */
+#define IPHETH_TX_BUF_SIZE ETH_FRAME_LEN
+#define IPHETH_RX_BUF_SIZE 65536
+
#define IPHETH_TX_TIMEOUT (5 * HZ)
#define IPHETH_INTFNUM 2
@@ -71,6 +75,7 @@
#define IPHETH_CTRL_TIMEOUT (5 * HZ)
#define IPHETH_CMD_GET_MACADDR 0x00
+#define IPHETH_CMD_ENABLE_NCM 0x04
#define IPHETH_CMD_CARRIER_CHECK 0x45
#define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
@@ -84,6 +89,8 @@ static const struct usb_device_id ipheth_table[] = {
};
MODULE_DEVICE_TABLE(usb, ipheth_table);
+static const char ipheth_start_packet[] = { 0x00, 0x01, 0x01, 0x00 };
+
struct ipheth_device {
struct usb_device *udev;
struct usb_interface *intf;
@@ -97,6 +104,7 @@ struct ipheth_device {
u8 bulk_out;
struct delayed_work carrier_work;
bool confirmed_pairing;
+ int (*rcvbulk_callback)(struct urb *urb);
};
static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
@@ -116,12 +124,12 @@ static int ipheth_alloc_urbs(struct ipheth_device *iphone)
if (rx_urb == NULL)
goto free_tx_urb;
- tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
+ tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_TX_BUF_SIZE,
GFP_KERNEL, &tx_urb->transfer_dma);
if (tx_buf == NULL)
goto free_rx_urb;
- rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN,
+ rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_RX_BUF_SIZE,
GFP_KERNEL, &rx_urb->transfer_dma);
if (rx_buf == NULL)
goto free_tx_buf;
@@ -134,7 +142,7 @@ static int ipheth_alloc_urbs(struct ipheth_device *iphone)
return 0;
free_tx_buf:
- usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf,
+ usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, tx_buf,
tx_urb->transfer_dma);
free_rx_urb:
usb_free_urb(rx_urb);
@@ -146,9 +154,9 @@ static int ipheth_alloc_urbs(struct ipheth_device *iphone)
static void ipheth_free_urbs(struct ipheth_device *iphone)
{
- usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN, iphone->rx_buf,
+ usb_free_coherent(iphone->udev, IPHETH_RX_BUF_SIZE, iphone->rx_buf,
iphone->rx_urb->transfer_dma);
- usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf,
+ usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, iphone->tx_buf,
iphone->tx_urb->transfer_dma);
usb_free_urb(iphone->rx_urb);
usb_free_urb(iphone->tx_urb);
@@ -160,14 +168,96 @@ static void ipheth_kill_urbs(struct ipheth_device *dev)
usb_kill_urb(dev->rx_urb);
}
-static void ipheth_rcvbulk_callback(struct urb *urb)
+static int ipheth_consume_skb(char* buf, int len, struct ipheth_device *dev)
{
- struct ipheth_device *dev;
struct sk_buff *skb;
- int status;
+
+ skb = dev_alloc_skb(len);
+ if (!skb) {
+ dev->net->stats.rx_dropped++;
+ return -ENOMEM;
+ }
+
+ skb_put_data(skb, buf, len);
+ skb->dev = dev->net;
+ skb->protocol = eth_type_trans(skb, dev->net);
+
+ dev->net->stats.rx_packets++;
+ dev->net->stats.rx_bytes += len;
+ netif_rx(skb);
+
+ return 0;
+}
+
+static int ipheth_rcvbulk_callback_legacy(struct urb *urb)
+{
+ struct ipheth_device *dev;
char *buf;
int len;
+ dev = urb->context;
+
+ if (urb->actual_length <= IPHETH_IP_ALIGN) {
+ dev->net->stats.rx_length_errors++;
+ return -EINVAL;
+ }
+ len = urb->actual_length - IPHETH_IP_ALIGN;
+ buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
+
+ return ipheth_consume_skb(buf, len, dev);
+}
+
+static int ipheth_rcvbulk_callback_ncm(struct urb *urb)
+{
+ struct ipheth_device *dev;
+ struct usb_cdc_ncm_nth16 *ncmh;
+ struct usb_cdc_ncm_ndp16 *ncm0;
+ struct usb_cdc_ncm_dpe16 *dpe;
+ char *buf;
+ int len;
+ int retval = -EINVAL;
+
+ dev = urb->context;
+
+ if (urb->actual_length < IPHETH_NCM_HEADER_SIZE) {
+ dev->net->stats.rx_length_errors++;
+ return retval;
+ }
+
+ ncmh = (struct usb_cdc_ncm_nth16 *)(urb->transfer_buffer);
+ if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) ||
+ le16_to_cpu(ncmh->wNdpIndex) >= urb->actual_length) {
+ dev->net->stats.rx_errors++;
+ return retval;
+ }
+
+ ncm0 = (struct usb_cdc_ncm_ndp16 *)(urb->transfer_buffer + le16_to_cpu(ncmh->wNdpIndex));
+ if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN) ||
+ le16_to_cpu(ncmh->wHeaderLength) + le16_to_cpu(ncm0->wLength) >= urb->actual_length) {
+ dev->net->stats.rx_errors++;
+ return retval;
+ }
+
+ dpe = ncm0->dpe16;
+ while (le16_to_cpu(dpe->wDatagramIndex) != 0 && le16_to_cpu(dpe->wDatagramLength) != 0) {
+ buf = urb->transfer_buffer + le16_to_cpu(dpe->wDatagramIndex);
+ len = le16_to_cpu(dpe->wDatagramLength);
+
+ retval = ipheth_consume_skb(buf, len, dev);
+ if (retval != 0)
+ return retval;
+
+ dpe++;
+ }
+
+ return 0;
+}
+
+static void ipheth_rcvbulk_callback(struct urb *urb)
+{
+ struct ipheth_device *dev;
+ int retval, status;
+
dev = urb->context;
if (dev == NULL)
return;
@@ -187,29 +277,24 @@ static void ipheth_rcvbulk_callback(struct urb *urb)
return;
}
- if (urb->actual_length <= IPHETH_IP_ALIGN) {
- dev->net->stats.rx_length_errors++;
- return;
- }
- len = urb->actual_length - IPHETH_IP_ALIGN;
- buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
-
- skb = dev_alloc_skb(len);
- if (!skb) {
- dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n",
- __func__);
- dev->net->stats.rx_dropped++;
+ /* The very first frame we receive from device has a fixed 4-byte value
+ * We can safely skip it
+ */
+ if (unlikely(
+ urb->actual_length == sizeof(ipheth_start_packet) &&
+ memcmp(urb->transfer_buffer, ipheth_start_packet, sizeof(ipheth_start_packet)) == 0
+ ))
+ goto rx_submit;
+
+ retval = dev->rcvbulk_callback(urb);
+ if (retval != 0) {
+ dev_err(&dev->intf->dev, "%s: callback retval: %d\n",
+ __func__, retval);
return;
}
- skb_put_data(skb, buf, len);
- skb->dev = dev->net;
- skb->protocol = eth_type_trans(skb, dev->net);
-
- dev->net->stats.rx_packets++;
- dev->net->stats.rx_bytes += len;
+rx_submit:
dev->confirmed_pairing = true;
- netif_rx(skb);
ipheth_rx_submit(dev, GFP_ATOMIC);
}
@@ -310,6 +395,27 @@ static int ipheth_get_macaddr(struct ipheth_device *dev)
return retval;
}
+static int ipheth_enable_ncm(struct ipheth_device *dev)
+{
+ struct usb_device *udev = dev->udev;
+ int retval;
+
+ retval = usb_control_msg(udev,
+ usb_sndctrlpipe(udev, IPHETH_CTRL_ENDP),
+ IPHETH_CMD_ENABLE_NCM, /* request */
+ 0x40, /* request type */
+ 0x00, /* value */
+ 0x02, /* index */
+ NULL,
+ 0,
+ IPHETH_CTRL_TIMEOUT);
+
+ dev_info(&dev->intf->dev, "%s: usb_control_msg: %d\n",
+ __func__, retval);
+
+ return retval;
+}
+
static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
{
struct usb_device *udev = dev->udev;
@@ -317,7 +423,7 @@ static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
usb_fill_bulk_urb(dev->rx_urb, udev,
usb_rcvbulkpipe(udev, dev->bulk_in),
- dev->rx_buf, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN,
+ dev->rx_buf, IPHETH_RX_BUF_SIZE,
ipheth_rcvbulk_callback,
dev);
dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
@@ -365,7 +471,7 @@ static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
int retval;
/* Paranoid */
- if (skb->len > IPHETH_BUF_SIZE) {
+ if (skb->len > IPHETH_TX_BUF_SIZE) {
WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
dev->net->stats.tx_dropped++;
dev_kfree_skb_any(skb);
@@ -373,12 +479,10 @@ static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
}
memcpy(dev->tx_buf, skb->data, skb->len);
- if (skb->len < IPHETH_BUF_SIZE)
- memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len);
usb_fill_bulk_urb(dev->tx_urb, udev,
usb_sndbulkpipe(udev, dev->bulk_out),
- dev->tx_buf, IPHETH_BUF_SIZE,
+ dev->tx_buf, skb->len,
ipheth_sndbulk_callback,
dev);
dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
@@ -450,6 +554,7 @@ static int ipheth_probe(struct usb_interface *intf,
dev->net = netdev;
dev->intf = intf;
dev->confirmed_pairing = false;
+ dev->rcvbulk_callback = ipheth_rcvbulk_callback_legacy;
/* Set up endpoints */
hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
if (hintf == NULL) {
@@ -481,6 +586,10 @@ static int ipheth_probe(struct usb_interface *intf,
if (retval)
goto err_get_macaddr;
+ retval = ipheth_enable_ncm(dev);
+ if (retval == 0)
+ dev->rcvbulk_callback = ipheth_rcvbulk_callback_ncm;
+
INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
retval = ipheth_alloc_urbs(dev);
--
2.40.1
[-- Attachment #3: Type: text/plain, Size: 4 bytes --]
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: net: usb: ipheth: add CDC NCM support
2023-05-11 9:47 net: usb: ipheth: add CDC NCM support Georgi Valkov
@ 2023-05-11 12:02 ` Forst
2023-05-11 12:58 ` Jan Kiszka
2023-05-11 14:40 ` Jakub Kicinski
1 sibling, 1 reply; 4+ messages in thread
From: Forst @ 2023-05-11 12:02 UTC (permalink / raw)
To: Georgi Valkov
Cc: Greg KH, Linux Kernel Mailing List, Jan Kiszka, Jakub Kicinski
Hello,
> I added the author Foster Snowhill to this mail, as well as a link to his Github [1].
> If needed, we can ask him to add the tag Signed-off-by:
I do not mind my patch being reviewed and included in Linux kernel if it is deemed of good quality. I would like to point out that I wrote it for personal use and may have cut some corners, so I likely missed some places when it comes to code style and data validation. The latter is important from a security standpoint, so extra attention to that would be appreciated.
If I need to resubmit it myself, and maybe to a different mailing list, please advise on the appropriate procedure.
Thank you!
/ Foster
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: net: usb: ipheth: add CDC NCM support
2023-05-11 12:02 ` Forst
@ 2023-05-11 12:58 ` Jan Kiszka
0 siblings, 0 replies; 4+ messages in thread
From: Jan Kiszka @ 2023-05-11 12:58 UTC (permalink / raw)
To: Forst, Georgi Valkov; +Cc: Greg KH, Linux Kernel Mailing List, Jakub Kicinski
On 11.05.23 14:02, Forst wrote:
> Hello,
>
>> I added the author Foster Snowhill to this mail, as well as a link to his Github [1].
>> If needed, we can ask him to add the tag Signed-off-by:
>
> I do not mind my patch being reviewed and included in Linux kernel if it is deemed of good quality. I would like to point out that I wrote it for personal use and may have cut some corners, so I likely missed some places when it comes to code style and data validation. The latter is important from a security standpoint, so extra attention to that would be appreciated.
>
> If I need to resubmit it myself, and maybe to a different mailing list, please advise on the appropriate procedure.
>
Sending it inline would be needed, and your signed-off. Regarding style,
use scripts/checkpatch.pl, regarding where to send it to,
scripts/get_maintainer.pl.
Thanks,
Jan
--
Siemens AG, Technology
Competence Center Embedded Linux
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: net: usb: ipheth: add CDC NCM support
2023-05-11 9:47 net: usb: ipheth: add CDC NCM support Georgi Valkov
2023-05-11 12:02 ` Forst
@ 2023-05-11 14:40 ` Jakub Kicinski
1 sibling, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2023-05-11 14:40 UTC (permalink / raw)
To: Georgi Valkov
Cc: Greg KH, Foster Snowhill, Linux Kernel Mailing List, Jan Kiszka
On Thu, 11 May 2023 12:47:56 +0300 Georgi Valkov wrote:
> Can you please review, and accept the following patch. I’ve been using it for one year.
> I added the author Foster Snowhill to this mail, as well as a link to his Github [1].
> If needed, we can ask him to add the tag Signed-off-by:
>
>
> From 257d843a7f90e4e519635f82fe5b73fba918ec33 Mon Sep 17 00:00:00 2001
> From: Foster Snowhill <Forst@users.noreply.github.com>
> Date: Fri, 22 Apr 2022 23:50:09 +0200
> Subject: [PATCH] net: usb: ipheth: add CDC NCM support (PoC)
>
> Recent iOS releases support CDC NCM encapsulation on RX. This mode is
> the default on macOS and Windows.
>
> When reconnecting a device from a macOS/Windows machine to a Linux
> machine, the device stays in NCM mode. Therefore to correctly support
> such a device, the driver has to either support the NCM mode too, or
> somehow put the device back into legacy mode.
>
> To match the behaviour of the macOS/Windows driver, and since there
> are no documented control commands to revert to legacy mode, I chose
> to implement basic NCM support.
>
> With this change, the device is attempted to be put into NCM mode by
> default, and falls back to legacy mode if said attempt failed.
>
> Tested-by: Georgi Valkov <gvalkov@abv.bg>
Take a look at submitting patches, the best way to send a change is
with git send-mail. And make sure you CC the right lists
(./scripts/get_maintainer.pl)
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-05-11 14:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-11 9:47 net: usb: ipheth: add CDC NCM support Georgi Valkov
2023-05-11 12:02 ` Forst
2023-05-11 12:58 ` Jan Kiszka
2023-05-11 14:40 ` Jakub Kicinski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox