Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbnet: ipheth: add CDC NCM support

Recent iOS releases support CDC NCM encapsulation on RX. This mode is
the default on macOS and Windows. In this mode, an iOS device may include
one or more Ethernet frames inside a single URB.

Freshly booted iOS devices start in legacy mode, but are put into
NCM mode by the official Apple driver. When reconnecting such a device
from a macOS/Windows machine to a Linux host, the device stays in
NCM mode, making it unusable with the legacy ipheth driver code.

To correctly support such a device, the driver has to either support
the NCM mode too, or put the device back into legacy mode.

To match the behaviour of the macOS/Windows driver, and since there
is no documented control command to revert to legacy mode, implement
NCM support. The device is attempted to be put into NCM mode by default,
and falls back to legacy mode if the attempt fails.

Signed-off-by: Foster Snowhill <forst@pen.gy>
Tested-by: Georgi Valkov <gvalkov@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Foster Snowhill and committed by
David S. Miller
a2d274c6 3e65efcc

+156 -26
+156 -26
drivers/net/usb/ipheth.c
··· 52 52 #include <linux/ethtool.h> 53 53 #include <linux/usb.h> 54 54 #include <linux/workqueue.h> 55 + #include <linux/usb/cdc.h> 55 56 56 57 #define USB_VENDOR_APPLE 0x05ac 57 58 ··· 60 59 #define IPHETH_USBINTF_SUBCLASS 253 61 60 #define IPHETH_USBINTF_PROTO 1 62 61 63 - #define IPHETH_BUF_SIZE 1514 64 62 #define IPHETH_IP_ALIGN 2 /* padding at front of URB */ 63 + #define IPHETH_NCM_HEADER_SIZE (12 + 96) /* NCMH + NCM0 */ 64 + #define IPHETH_TX_BUF_SIZE ETH_FRAME_LEN 65 + #define IPHETH_RX_BUF_SIZE_LEGACY (IPHETH_IP_ALIGN + ETH_FRAME_LEN) 66 + #define IPHETH_RX_BUF_SIZE_NCM 65536 67 + 65 68 #define IPHETH_TX_TIMEOUT (5 * HZ) 66 69 67 70 #define IPHETH_INTFNUM 2 ··· 76 71 #define IPHETH_CTRL_TIMEOUT (5 * HZ) 77 72 78 73 #define IPHETH_CMD_GET_MACADDR 0x00 74 + #define IPHETH_CMD_ENABLE_NCM 0x04 79 75 #define IPHETH_CMD_CARRIER_CHECK 0x45 80 76 81 77 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ) ··· 103 97 u8 bulk_out; 104 98 struct delayed_work carrier_work; 105 99 bool confirmed_pairing; 100 + int (*rcvbulk_callback)(struct urb *urb); 101 + size_t rx_buf_len; 106 102 }; 107 103 108 104 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags); ··· 124 116 if (rx_urb == NULL) 125 117 goto free_tx_urb; 126 118 127 - tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE, 119 + tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, 128 120 GFP_KERNEL, &tx_urb->transfer_dma); 129 121 if (tx_buf == NULL) 130 122 goto free_rx_urb; 131 123 132 - rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN, 124 + rx_buf = usb_alloc_coherent(iphone->udev, iphone->rx_buf_len, 133 125 GFP_KERNEL, &rx_urb->transfer_dma); 134 126 if (rx_buf == NULL) 135 127 goto free_tx_buf; ··· 142 134 return 0; 143 135 144 136 free_tx_buf: 145 - usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf, 137 + usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, tx_buf, 146 138 tx_urb->transfer_dma); 147 139 free_rx_urb: 148 140 usb_free_urb(rx_urb); ··· 154 146 155 147 static void ipheth_free_urbs(struct ipheth_device *iphone) 156 148 { 157 - usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN, iphone->rx_buf, 149 + usb_free_coherent(iphone->udev, iphone->rx_buf_len, iphone->rx_buf, 158 150 iphone->rx_urb->transfer_dma); 159 - usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf, 151 + usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, iphone->tx_buf, 160 152 iphone->tx_urb->transfer_dma); 161 153 usb_free_urb(iphone->rx_urb); 162 154 usb_free_urb(iphone->tx_urb); ··· 168 160 usb_kill_urb(dev->rx_urb); 169 161 } 170 162 163 + static int ipheth_consume_skb(char *buf, int len, struct ipheth_device *dev) 164 + { 165 + struct sk_buff *skb; 166 + 167 + skb = dev_alloc_skb(len); 168 + if (!skb) { 169 + dev->net->stats.rx_dropped++; 170 + return -ENOMEM; 171 + } 172 + 173 + skb_put_data(skb, buf, len); 174 + skb->dev = dev->net; 175 + skb->protocol = eth_type_trans(skb, dev->net); 176 + 177 + dev->net->stats.rx_packets++; 178 + dev->net->stats.rx_bytes += len; 179 + netif_rx(skb); 180 + 181 + return 0; 182 + } 183 + 184 + static int ipheth_rcvbulk_callback_legacy(struct urb *urb) 185 + { 186 + struct ipheth_device *dev; 187 + char *buf; 188 + int len; 189 + 190 + dev = urb->context; 191 + 192 + if (urb->actual_length <= IPHETH_IP_ALIGN) { 193 + dev->net->stats.rx_length_errors++; 194 + return -EINVAL; 195 + } 196 + len = urb->actual_length - IPHETH_IP_ALIGN; 197 + buf = urb->transfer_buffer + IPHETH_IP_ALIGN; 198 + 199 + return ipheth_consume_skb(buf, len, dev); 200 + } 201 + 202 + static int ipheth_rcvbulk_callback_ncm(struct urb *urb) 203 + { 204 + struct usb_cdc_ncm_nth16 *ncmh; 205 + struct usb_cdc_ncm_ndp16 *ncm0; 206 + struct usb_cdc_ncm_dpe16 *dpe; 207 + struct ipheth_device *dev; 208 + int retval = -EINVAL; 209 + char *buf; 210 + int len; 211 + 212 + dev = urb->context; 213 + 214 + if (urb->actual_length < IPHETH_NCM_HEADER_SIZE) { 215 + dev->net->stats.rx_length_errors++; 216 + return retval; 217 + } 218 + 219 + ncmh = urb->transfer_buffer; 220 + if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) || 221 + le16_to_cpu(ncmh->wNdpIndex) >= urb->actual_length) { 222 + dev->net->stats.rx_errors++; 223 + return retval; 224 + } 225 + 226 + ncm0 = urb->transfer_buffer + le16_to_cpu(ncmh->wNdpIndex); 227 + if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN) || 228 + le16_to_cpu(ncmh->wHeaderLength) + le16_to_cpu(ncm0->wLength) >= 229 + urb->actual_length) { 230 + dev->net->stats.rx_errors++; 231 + return retval; 232 + } 233 + 234 + dpe = ncm0->dpe16; 235 + while (le16_to_cpu(dpe->wDatagramIndex) != 0 && 236 + le16_to_cpu(dpe->wDatagramLength) != 0) { 237 + if (le16_to_cpu(dpe->wDatagramIndex) >= urb->actual_length || 238 + le16_to_cpu(dpe->wDatagramIndex) + 239 + le16_to_cpu(dpe->wDatagramLength) > urb->actual_length) { 240 + dev->net->stats.rx_length_errors++; 241 + return retval; 242 + } 243 + 244 + buf = urb->transfer_buffer + le16_to_cpu(dpe->wDatagramIndex); 245 + len = le16_to_cpu(dpe->wDatagramLength); 246 + 247 + retval = ipheth_consume_skb(buf, len, dev); 248 + if (retval != 0) 249 + return retval; 250 + 251 + dpe++; 252 + } 253 + 254 + return 0; 255 + } 256 + 171 257 static void ipheth_rcvbulk_callback(struct urb *urb) 172 258 { 173 259 struct ipheth_device *dev; 174 - struct sk_buff *skb; 175 - int status; 176 - char *buf; 177 - int len; 260 + int retval, status; 178 261 179 262 dev = urb->context; 180 263 if (dev == NULL) ··· 290 191 dev->net->stats.rx_length_errors++; 291 192 return; 292 193 } 293 - len = urb->actual_length - IPHETH_IP_ALIGN; 294 - buf = urb->transfer_buffer + IPHETH_IP_ALIGN; 295 194 296 - skb = dev_alloc_skb(len); 297 - if (!skb) { 298 - dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n", 299 - __func__); 300 - dev->net->stats.rx_dropped++; 195 + /* RX URBs starting with 0x00 0x01 do not encapsulate Ethernet frames, 196 + * but rather are control frames. Their purpose is not documented, and 197 + * they don't affect driver functionality, okay to drop them. 198 + * There is usually just one 4-byte control frame as the very first 199 + * URB received from the bulk IN endpoint. 200 + */ 201 + if (unlikely 202 + (((char *)urb->transfer_buffer)[0] == 0 && 203 + ((char *)urb->transfer_buffer)[1] == 1)) 204 + goto rx_submit; 205 + 206 + retval = dev->rcvbulk_callback(urb); 207 + if (retval != 0) { 208 + dev_err(&dev->intf->dev, "%s: callback retval: %d\n", 209 + __func__, retval); 301 210 return; 302 211 } 303 212 304 - skb_put_data(skb, buf, len); 305 - skb->dev = dev->net; 306 - skb->protocol = eth_type_trans(skb, dev->net); 307 - 308 - dev->net->stats.rx_packets++; 309 - dev->net->stats.rx_bytes += len; 213 + rx_submit: 310 214 dev->confirmed_pairing = true; 311 - netif_rx(skb); 312 215 ipheth_rx_submit(dev, GFP_ATOMIC); 313 216 } 314 217 ··· 411 310 return retval; 412 311 } 413 312 313 + static int ipheth_enable_ncm(struct ipheth_device *dev) 314 + { 315 + struct usb_device *udev = dev->udev; 316 + int retval; 317 + 318 + retval = usb_control_msg(udev, 319 + usb_sndctrlpipe(udev, IPHETH_CTRL_ENDP), 320 + IPHETH_CMD_ENABLE_NCM, /* request */ 321 + 0x41, /* request type */ 322 + 0x00, /* value */ 323 + 0x02, /* index */ 324 + NULL, 325 + 0, 326 + IPHETH_CTRL_TIMEOUT); 327 + 328 + dev_info(&dev->intf->dev, "%s: usb_control_msg: %d\n", 329 + __func__, retval); 330 + 331 + return retval; 332 + } 333 + 414 334 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags) 415 335 { 416 336 struct usb_device *udev = dev->udev; ··· 439 317 440 318 usb_fill_bulk_urb(dev->rx_urb, udev, 441 319 usb_rcvbulkpipe(udev, dev->bulk_in), 442 - dev->rx_buf, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN, 320 + dev->rx_buf, dev->rx_buf_len, 443 321 ipheth_rcvbulk_callback, 444 322 dev); 445 323 dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; ··· 487 365 int retval; 488 366 489 367 /* Paranoid */ 490 - if (skb->len > IPHETH_BUF_SIZE) { 368 + if (skb->len > IPHETH_TX_BUF_SIZE) { 491 369 WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len); 492 370 dev->net->stats.tx_dropped++; 493 371 dev_kfree_skb_any(skb); ··· 570 448 dev->net = netdev; 571 449 dev->intf = intf; 572 450 dev->confirmed_pairing = false; 451 + dev->rx_buf_len = IPHETH_RX_BUF_SIZE_LEGACY; 452 + dev->rcvbulk_callback = ipheth_rcvbulk_callback_legacy; 573 453 /* Set up endpoints */ 574 454 hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM); 575 455 if (hintf == NULL) { ··· 602 478 retval = ipheth_get_macaddr(dev); 603 479 if (retval) 604 480 goto err_get_macaddr; 481 + 482 + retval = ipheth_enable_ncm(dev); 483 + if (!retval) { 484 + dev->rx_buf_len = IPHETH_RX_BUF_SIZE_NCM; 485 + dev->rcvbulk_callback = ipheth_rcvbulk_callback_ncm; 486 + } 605 487 606 488 INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work); 607 489