|
@@ -0,0 +1,977 @@
|
|
|
|
|
+/* USB RNDIS tethering driver.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Android phones expose their USB tethering link as an RNDIS function: a
|
|
|
|
|
+ * CDC-style control interface with encapsulated RNDIS commands and a data
|
|
|
|
|
+ * interface with bulk IN/OUT endpoints. The phone is a soft MAC; the host
|
|
|
|
|
+ * sends the RNDIS INIT/QUERY/SET messages, then Ethernet frames travel in
|
|
|
|
|
+ * RNDIS packet messages over the bulk endpoints.
|
|
|
|
|
+ *
|
|
|
|
|
+ * QEMU's usb-net device implements the same protocol, so the driver can be
|
|
|
|
|
+ * exercised without a phone (`make run-rndis`). QEMU uses configuration
|
|
|
|
|
+ * value 2 and class 02/02/FF; Android usually uses E0/01/03 (and some
|
|
|
|
|
+ * devices use an EF class). Both are parsed here.
|
|
|
|
|
+ *
|
|
|
|
|
+ * This is not WiFi: the phone's own WiFi/cellular connection is shared with
|
|
|
|
|
+ * the board over USB, and the kernel sees it as a normal netdev.
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+#include "rndis.h"
|
|
|
|
|
+#include "io.h"
|
|
|
|
|
+#include "netdev.h"
|
|
|
|
|
+#include "serial.h"
|
|
|
|
|
+#include "timer.h"
|
|
|
|
|
+#include "usb.h"
|
|
|
|
|
+
|
|
|
|
|
+/* RNDIS message types. */
|
|
|
|
|
+#define RNDIS_MSG_PACKET 0x00000001u
|
|
|
|
|
+#define RNDIS_MSG_INIT 0x00000002u
|
|
|
|
|
+#define RNDIS_MSG_HALT 0x00000003u
|
|
|
|
|
+#define RNDIS_MSG_QUERY 0x00000004u
|
|
|
|
|
+#define RNDIS_MSG_SET 0x00000005u
|
|
|
|
|
+#define RNDIS_MSG_RESET 0x00000006u
|
|
|
|
|
+#define RNDIS_MSG_INDICATE 0x00000007u
|
|
|
|
|
+#define RNDIS_MSG_KEEPALIVE 0x00000008u
|
|
|
|
|
+#define RNDIS_MSG_COMPLETION 0x80000000u
|
|
|
|
|
+
|
|
|
|
|
+#define RNDIS_MSG_INIT_C (RNDIS_MSG_INIT | RNDIS_MSG_COMPLETION)
|
|
|
|
|
+#define RNDIS_MSG_QUERY_C (RNDIS_MSG_QUERY | RNDIS_MSG_COMPLETION)
|
|
|
|
|
+#define RNDIS_MSG_SET_C (RNDIS_MSG_SET | RNDIS_MSG_COMPLETION)
|
|
|
|
|
+#define RNDIS_MSG_KEEPALIVE_C (RNDIS_MSG_KEEPALIVE | RNDIS_MSG_COMPLETION)
|
|
|
|
|
+
|
|
|
|
|
+#define RNDIS_STATUS_SUCCESS 0x00000000u
|
|
|
|
|
+#define RNDIS_STATUS_MEDIA_CONNECT 0x4001000Bu
|
|
|
|
|
+#define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000Cu
|
|
|
|
|
+
|
|
|
|
|
+/* Information objects used during bring-up. */
|
|
|
|
|
+#define RNDIS_OID_GEN_CURRENT_PACKET_FILTER 0x0001010Eu
|
|
|
|
|
+#define RNDIS_OID_GEN_MEDIA_CONNECT_STATUS 0x00010114u
|
|
|
|
|
+#define RNDIS_OID_802_3_PERMANENT_ADDRESS 0x01010101u
|
|
|
|
|
+#define RNDIS_OID_802_3_CURRENT_ADDRESS 0x01010102u
|
|
|
|
|
+
|
|
|
|
|
+/* Packet filter bits. A phone's DHCP and ARP traffic needs broadcast and
|
|
|
|
|
+ * directed frames; multicast keeps DNS/mDNS from disappearing. */
|
|
|
|
|
+#define RNDIS_PACKET_TYPE_DIRECTED 0x00000001u
|
|
|
|
|
+#define RNDIS_PACKET_TYPE_MULTICAST 0x00000002u
|
|
|
|
|
+#define RNDIS_PACKET_TYPE_ALL_MULTICAST 0x00000004u
|
|
|
|
|
+#define RNDIS_PACKET_TYPE_BROADCAST 0x00000008u
|
|
|
|
|
+#define RNDIS_PACKET_TYPE_PROMISCUOUS 0x00000020u
|
|
|
|
|
+#define RNDIS_PACKET_FILTER_DEFAULT \
|
|
|
|
|
+ (RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_MULTICAST | \
|
|
|
|
|
+ RNDIS_PACKET_TYPE_ALL_MULTICAST | RNDIS_PACKET_TYPE_BROADCAST | \
|
|
|
|
|
+ RNDIS_PACKET_TYPE_PROMISCUOUS)
|
|
|
|
|
+
|
|
|
|
|
+#define RNDIS_INIT_MAX_TRANSFER 2048u
|
|
|
|
|
+#define RNDIS_RX_MAX 2048u
|
|
|
|
|
+#define RNDIS_RX_ASSEMBLY 4096u
|
|
|
|
|
+#define RNDIS_TX_MAX 1600u
|
|
|
|
|
+#define RNDIS_CTRL_MAX 1025u
|
|
|
|
|
+#define RNDIS_CFG_MAX 1024u
|
|
|
|
|
+
|
|
|
|
|
+#define RNDIS_CONTROL_TIMEOUT_MS 200u
|
|
|
|
|
+#define RNDIS_POLL_MS 20u
|
|
|
|
|
+
|
|
|
|
|
+/* USB interface classes that identify an RNDIS control function. */
|
|
|
|
|
+#define RNDIS_CLASS_COMM 0x02u
|
|
|
|
|
+#define RNDIS_SUBCLASS_ACM 0x02u
|
|
|
|
|
+#define RNDIS_PROTO_VENDOR 0xFFu
|
|
|
|
|
+#define RNDIS_CLASS_WIRELESS 0xE0u
|
|
|
|
|
+#define RNDIS_CLASS_MISC 0xEFu
|
|
|
|
|
+#define RNDIS_CLASS_DATA 0x0Au
|
|
|
|
|
+
|
|
|
|
|
+struct rndis_dev {
|
|
|
|
|
+ int present;
|
|
|
|
|
+ int ready;
|
|
|
|
|
+ int link;
|
|
|
|
|
+ unsigned int ctrl, addr, mps, port, config;
|
|
|
|
|
+ int low;
|
|
|
|
|
+ unsigned int ctrl_iface, data_iface;
|
|
|
|
|
+ unsigned int ep_in, ep_in_mps;
|
|
|
|
|
+ unsigned int ep_out, ep_out_mps;
|
|
|
|
|
+ unsigned int ep_int, ep_int_mps;
|
|
|
|
|
+ unsigned char mac[6];
|
|
|
|
|
+ unsigned int max_transfer;
|
|
|
|
|
+ unsigned int align;
|
|
|
|
|
+ unsigned int xid;
|
|
|
|
|
+ unsigned int gen;
|
|
|
|
|
+ unsigned int rx_pos, rx_len;
|
|
|
|
|
+ unsigned int rx_frames, tx_frames, rx_errors, tx_errors;
|
|
|
|
|
+ /* Bulk reads always land in raw_buf; rx_buf is the reassembly
|
|
|
|
|
+ * stream. xHCI arms directly into the caller's buffer, so the read
|
|
|
|
|
+ * buffer must stay at a fixed address while a transfer is pending. */
|
|
|
|
|
+ unsigned char rx_buf[RNDIS_RX_ASSEMBLY] __attribute__((aligned(64)));
|
|
|
|
|
+ unsigned char raw_buf[RNDIS_RX_MAX] __attribute__((aligned(64)));
|
|
|
|
|
+ unsigned char tx_buf[RNDIS_TX_MAX] __attribute__((aligned(64)));
|
|
|
|
|
+ unsigned char ctrl_buf[RNDIS_CTRL_MAX] __attribute__((aligned(64)));
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static struct rndis_dev dev;
|
|
|
|
|
+static unsigned char cfg_buf[RNDIS_CFG_MAX] __attribute__((aligned(64)));
|
|
|
|
|
+
|
|
|
|
|
+/* Small part of the device state needed to undo a probe of an unrelated USB
|
|
|
|
|
+ * device. The USB core calls every device probe for every device, so a
|
|
|
|
|
+ * mouse/keyboard probe after a working phone must not clear its state. */
|
|
|
|
|
+struct rndis_saved {
|
|
|
|
|
+ int present, ready, link;
|
|
|
|
|
+ unsigned int ctrl, addr, mps, port, config;
|
|
|
|
|
+ int low;
|
|
|
|
|
+ unsigned int ctrl_iface, data_iface;
|
|
|
|
|
+ unsigned int ep_in, ep_in_mps, ep_out, ep_out_mps;
|
|
|
|
|
+ unsigned int ep_int, ep_int_mps;
|
|
|
|
|
+ unsigned char mac[6];
|
|
|
|
|
+ unsigned int max_transfer, align, xid, gen;
|
|
|
|
|
+ unsigned int rx_pos, rx_len;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_save(struct rndis_saved *s)
|
|
|
|
|
+{
|
|
|
|
|
+ s->present = dev.present;
|
|
|
|
|
+ s->ready = dev.ready;
|
|
|
|
|
+ s->link = dev.link;
|
|
|
|
|
+ s->ctrl = dev.ctrl;
|
|
|
|
|
+ s->addr = dev.addr;
|
|
|
|
|
+ s->mps = dev.mps;
|
|
|
|
|
+ s->port = dev.port;
|
|
|
|
|
+ s->config = dev.config;
|
|
|
|
|
+ s->low = dev.low;
|
|
|
|
|
+ s->ctrl_iface = dev.ctrl_iface;
|
|
|
|
|
+ s->data_iface = dev.data_iface;
|
|
|
|
|
+ s->ep_in = dev.ep_in;
|
|
|
|
|
+ s->ep_in_mps = dev.ep_in_mps;
|
|
|
|
|
+ s->ep_out = dev.ep_out;
|
|
|
|
|
+ s->ep_out_mps = dev.ep_out_mps;
|
|
|
|
|
+ s->ep_int = dev.ep_int;
|
|
|
|
|
+ s->ep_int_mps = dev.ep_int_mps;
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 6; i++)
|
|
|
|
|
+ s->mac[i] = dev.mac[i];
|
|
|
|
|
+ }
|
|
|
|
|
+ s->max_transfer = dev.max_transfer;
|
|
|
|
|
+ s->align = dev.align;
|
|
|
|
|
+ s->xid = dev.xid;
|
|
|
|
|
+ s->gen = dev.gen;
|
|
|
|
|
+ s->rx_pos = dev.rx_pos;
|
|
|
|
|
+ s->rx_len = dev.rx_len;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_restore(const struct rndis_saved *s)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ dev.present = s->present;
|
|
|
|
|
+ dev.ready = s->ready;
|
|
|
|
|
+ dev.link = s->link;
|
|
|
|
|
+ dev.ctrl = s->ctrl;
|
|
|
|
|
+ dev.addr = s->addr;
|
|
|
|
|
+ dev.mps = s->mps;
|
|
|
|
|
+ dev.port = s->port;
|
|
|
|
|
+ dev.config = s->config;
|
|
|
|
|
+ dev.low = s->low;
|
|
|
|
|
+ dev.ctrl_iface = s->ctrl_iface;
|
|
|
|
|
+ dev.data_iface = s->data_iface;
|
|
|
|
|
+ dev.ep_in = s->ep_in;
|
|
|
|
|
+ dev.ep_in_mps = s->ep_in_mps;
|
|
|
|
|
+ dev.ep_out = s->ep_out;
|
|
|
|
|
+ dev.ep_out_mps = s->ep_out_mps;
|
|
|
|
|
+ dev.ep_int = s->ep_int;
|
|
|
|
|
+ dev.ep_int_mps = s->ep_int_mps;
|
|
|
|
|
+ for (i = 0; i < 6; i++)
|
|
|
|
|
+ dev.mac[i] = s->mac[i];
|
|
|
|
|
+ dev.max_transfer = s->max_transfer;
|
|
|
|
|
+ dev.align = s->align;
|
|
|
|
|
+ dev.xid = s->xid;
|
|
|
|
|
+ dev.gen = s->gen;
|
|
|
|
|
+ dev.rx_pos = s->rx_pos;
|
|
|
|
|
+ dev.rx_len = s->rx_len;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* A failed probe normally restores the previous phone. If the failed probe
|
|
|
|
|
+ * is for another device on the phone's own root port, the phone was
|
|
|
|
|
+ * unplugged and replaced between hotplug polls, so do not resurrect it. */
|
|
|
|
|
+static void rndis_restore_after_probe(const struct rndis_saved *saved,
|
|
|
|
|
+ unsigned int ctrl, unsigned int port,
|
|
|
|
|
+ unsigned int gen)
|
|
|
|
|
+{
|
|
|
|
|
+ if (saved->present && saved->port != 0xFFu &&
|
|
|
|
|
+ saved->ctrl == ctrl && saved->port == port) {
|
|
|
|
|
+ dev.present = 0;
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ dev.gen = gen;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ rndis_restore(saved);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static unsigned int rd32le(const unsigned char *p)
|
|
|
|
|
+{
|
|
|
|
|
+ return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
|
|
|
|
|
+ ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static unsigned int rd16le(const unsigned char *p)
|
|
|
|
|
+{
|
|
|
|
|
+ return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void wr32le(unsigned char *p, unsigned int v)
|
|
|
|
|
+{
|
|
|
|
|
+ p[0] = (unsigned char)(v & 0xFF);
|
|
|
|
|
+ p[1] = (unsigned char)((v >> 8) & 0xFF);
|
|
|
|
|
+ p[2] = (unsigned char)((v >> 16) & 0xFF);
|
|
|
|
|
+ p[3] = (unsigned char)((v >> 24) & 0xFF);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_delay(unsigned int ms)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int t0 = get_ticks_ms();
|
|
|
|
|
+
|
|
|
|
|
+ while (get_ticks_ms() - t0 < ms)
|
|
|
|
|
+ ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void put_mac_serial(void)
|
|
|
|
|
+{
|
|
|
|
|
+ static const char hex[] = "0123456789ABCDEF";
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 6; i++) {
|
|
|
|
|
+ if (i)
|
|
|
|
|
+ serial_putc(SERIAL_COM1, ':');
|
|
|
|
|
+ serial_putc(SERIAL_COM1, hex[(dev.mac[i] >> 4) & 0xF]);
|
|
|
|
|
+ serial_putc(SERIAL_COM1, hex[dev.mac[i] & 0xF]);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void put_mac_io(void)
|
|
|
|
|
+{
|
|
|
|
|
+ static const char hex[] = "0123456789ABCDEF";
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 6; i++) {
|
|
|
|
|
+ if (i)
|
|
|
|
|
+ io_putchar(':');
|
|
|
|
|
+ io_putchar(hex[(dev.mac[i] >> 4) & 0xF]);
|
|
|
|
|
+ io_putchar(hex[dev.mac[i] & 0xF]);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_ctrl_send(const unsigned char *buf, unsigned int len)
|
|
|
|
|
+{
|
|
|
|
|
+ int r = usb_control_out_data(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x21, 0x00, 0,
|
|
|
|
|
+ (unsigned short)dev.ctrl_iface,
|
|
|
|
|
+ buf, len);
|
|
|
|
|
+ return r < 0 ? -1 : 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_ctrl_poll(unsigned char *buf, unsigned int len)
|
|
|
|
|
+{
|
|
|
|
|
+ return usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0xA1, 0x01, 0,
|
|
|
|
|
+ (unsigned short)dev.ctrl_iface, buf, len);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_send_keepalive_c(unsigned int xid)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned char msg[16];
|
|
|
|
|
+
|
|
|
|
|
+ wr32le(msg + 0, RNDIS_MSG_KEEPALIVE_C);
|
|
|
|
|
+ wr32le(msg + 4, sizeof(msg));
|
|
|
|
|
+ wr32le(msg + 8, xid);
|
|
|
|
|
+ wr32le(msg + 12, RNDIS_STATUS_SUCCESS);
|
|
|
|
|
+ (void)rndis_ctrl_send(msg, sizeof(msg));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Wait for the completion of the current request. RNDIS queues responses on
|
|
|
|
|
+ * the control channel; the interrupt endpoint only says "a response is
|
|
|
|
|
+ * available" and is deliberately not required here. */
|
|
|
|
|
+static int rndis_wait(unsigned int expect)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int tries = RNDIS_CONTROL_TIMEOUT_MS / RNDIS_POLL_MS;
|
|
|
|
|
+
|
|
|
|
|
+ while (tries--) {
|
|
|
|
|
+ int n;
|
|
|
|
|
+
|
|
|
|
|
+ if (dev.port != 0xFFu &&
|
|
|
|
|
+ !usb_port_connected(dev.ctrl, dev.port))
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ n = rndis_ctrl_poll(dev.ctrl_buf, RNDIS_CTRL_MAX);
|
|
|
|
|
+ if (n >= 8) {
|
|
|
|
|
+ unsigned int type = rd32le(dev.ctrl_buf);
|
|
|
|
|
+
|
|
|
|
|
+ if (type == expect && n >= 16 &&
|
|
|
|
|
+ rd32le(dev.ctrl_buf + 8) == dev.xid)
|
|
|
|
|
+ return n;
|
|
|
|
|
+ if (type == RNDIS_MSG_INDICATE && n >= 12) {
|
|
|
|
|
+ unsigned int status = rd32le(dev.ctrl_buf + 8);
|
|
|
|
|
+
|
|
|
|
|
+ if (status == RNDIS_STATUS_MEDIA_CONNECT)
|
|
|
|
|
+ dev.link = 1;
|
|
|
|
|
+ else if (status == RNDIS_STATUS_MEDIA_DISCONNECT)
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ } else if (type == RNDIS_MSG_KEEPALIVE && n >= 12) {
|
|
|
|
|
+ rndis_send_keepalive_c(rd32le(dev.ctrl_buf + 8));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ rndis_delay(RNDIS_POLL_MS);
|
|
|
|
|
+ }
|
|
|
|
|
+ return -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_send_halt(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned char msg[12];
|
|
|
|
|
+
|
|
|
|
|
+ wr32le(msg + 0, RNDIS_MSG_HALT);
|
|
|
|
|
+ wr32le(msg + 4, sizeof(msg));
|
|
|
|
|
+ wr32le(msg + 8, 0);
|
|
|
|
|
+ (void)rndis_ctrl_send(msg, sizeof(msg));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_msg_init(void)
|
|
|
|
|
+{
|
|
|
|
|
+ int n;
|
|
|
|
|
+ unsigned int status, max_transfer, alignment;
|
|
|
|
|
+
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ if (!dev.xid)
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ for (n = 0; n < (int)24; n++)
|
|
|
|
|
+ dev.ctrl_buf[n] = 0;
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 0, RNDIS_MSG_INIT);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 4, 24);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 8, dev.xid);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 12, 1); /* RNDIS 1.0 */
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 16, 0);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 20, RNDIS_INIT_MAX_TRANSFER);
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_ctrl_send(dev.ctrl_buf, 24) < 0)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ n = rndis_wait(RNDIS_MSG_INIT_C);
|
|
|
|
|
+ if (n < 52)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+
|
|
|
|
|
+ status = rd32le(dev.ctrl_buf + 12);
|
|
|
|
|
+ if (status != RNDIS_STATUS_SUCCESS)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ max_transfer = rd32le(dev.ctrl_buf + 36);
|
|
|
|
|
+ alignment = rd32le(dev.ctrl_buf + 40);
|
|
|
|
|
+ if (alignment > 3)
|
|
|
|
|
+ alignment = 3;
|
|
|
|
|
+ dev.align = 1u << alignment;
|
|
|
|
|
+ dev.max_transfer = max_transfer;
|
|
|
|
|
+ if (max_transfer && max_transfer < 1558u) {
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "rndis: warning: small max transfer ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, max_transfer);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_query(unsigned int oid, unsigned int in_len,
|
|
|
|
|
+ unsigned char *out, unsigned int out_max,
|
|
|
|
|
+ unsigned int *out_len)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int len = 28 + in_len;
|
|
|
|
|
+ unsigned int off, rlen;
|
|
|
|
|
+ int n;
|
|
|
|
|
+
|
|
|
|
|
+ if (len > RNDIS_CTRL_MAX)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ if (!dev.xid)
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ for (n = 0; n < (int)len; n++)
|
|
|
|
|
+ dev.ctrl_buf[n] = 0;
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 0, RNDIS_MSG_QUERY);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 4, len);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 8, dev.xid);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 12, oid);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 16, in_len);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 20, 20);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 24, 0);
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_ctrl_send(dev.ctrl_buf, len) < 0)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ n = rndis_wait(RNDIS_MSG_QUERY_C);
|
|
|
|
|
+ if (n < 24)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (rd32le(dev.ctrl_buf + 12) != RNDIS_STATUS_SUCCESS)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+
|
|
|
|
|
+ rlen = rd32le(dev.ctrl_buf + 16);
|
|
|
|
|
+ off = rd32le(dev.ctrl_buf + 20);
|
|
|
|
|
+ if (rlen > out_max)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (off > (unsigned int)n || rlen > (unsigned int)n - off ||
|
|
|
|
|
+ 8u + off + rlen > (unsigned int)n)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < rlen; i++)
|
|
|
|
|
+ out[i] = dev.ctrl_buf[8 + off + i];
|
|
|
|
|
+ }
|
|
|
|
|
+ *out_len = rlen;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_set_filter(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int len = 32;
|
|
|
|
|
+ int n;
|
|
|
|
|
+
|
|
|
|
|
+ for (n = 0; n < (int)len; n++)
|
|
|
|
|
+ dev.ctrl_buf[n] = 0;
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 0, RNDIS_MSG_SET);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 4, len);
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ if (!dev.xid)
|
|
|
|
|
+ dev.xid++;
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 8, dev.xid);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 12, RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 16, 4);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 20, 20);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 24, 0);
|
|
|
|
|
+ wr32le(dev.ctrl_buf + 28, RNDIS_PACKET_FILTER_DEFAULT);
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_ctrl_send(dev.ctrl_buf, len) < 0)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ n = rndis_wait(RNDIS_MSG_SET_C);
|
|
|
|
|
+ if (n < 16)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ return rd32le(dev.ctrl_buf + 12) == RNDIS_STATUS_SUCCESS ? 0 : -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_start(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned char macbuf[16];
|
|
|
|
|
+ unsigned int mac_len = 0;
|
|
|
|
|
+ unsigned int status = 0;
|
|
|
|
|
+ unsigned int status_len = 0;
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+ int mac_ok = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (!dev.present)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (dev.ready)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_msg_init() < 0)
|
|
|
|
|
+ goto fail;
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_query(RNDIS_OID_802_3_PERMANENT_ADDRESS, 48,
|
|
|
|
|
+ macbuf, sizeof(macbuf), &mac_len) == 0 && mac_len >= 6)
|
|
|
|
|
+ mac_ok = 1;
|
|
|
|
|
+ else if (rndis_query(RNDIS_OID_802_3_CURRENT_ADDRESS, 48,
|
|
|
|
|
+ macbuf, sizeof(macbuf), &mac_len) == 0 &&
|
|
|
|
|
+ mac_len >= 6)
|
|
|
|
|
+ mac_ok = 1;
|
|
|
|
|
+ if (!mac_ok)
|
|
|
|
|
+ goto fail;
|
|
|
|
|
+ for (i = 0; i < 6; i++)
|
|
|
|
|
+ dev.mac[i] = macbuf[i];
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned int zero = 1;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 6; i++)
|
|
|
|
|
+ if (dev.mac[i])
|
|
|
|
|
+ zero = 0;
|
|
|
|
|
+ if (zero)
|
|
|
|
|
+ goto fail;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* A phone with tethering switched off may report "disconnected"; keep
|
|
|
|
|
+ * the link usable and let a later DHCP attempt discover the state. */
|
|
|
|
|
+ if (rndis_query(RNDIS_OID_GEN_MEDIA_CONNECT_STATUS, 4,
|
|
|
|
|
+ (unsigned char *)&status, sizeof(status),
|
|
|
|
|
+ &status_len) == 0 && status_len >= 4)
|
|
|
|
|
+ dev.link = (status == 0);
|
|
|
|
|
+ else
|
|
|
|
|
+ dev.link = 1;
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_set_filter() < 0)
|
|
|
|
|
+ goto fail;
|
|
|
|
|
+
|
|
|
|
|
+ dev.ready = 1;
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "rndis: ready, MAC ");
|
|
|
|
|
+ put_mac_serial();
|
|
|
|
|
+ serial_puts(SERIAL_COM1, dev.link ? ", link up\n" : ", link down\n");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+fail:
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "rndis: initialization failed\n");
|
|
|
|
|
+ rndis_send_halt();
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_is_control(unsigned int cls, unsigned int sub,
|
|
|
|
|
+ unsigned int proto)
|
|
|
|
|
+{
|
|
|
|
|
+ return (cls == RNDIS_CLASS_COMM && sub == RNDIS_SUBCLASS_ACM &&
|
|
|
|
|
+ proto == RNDIS_PROTO_VENDOR) ||
|
|
|
|
|
+ (cls == RNDIS_CLASS_WIRELESS && sub == 0x01 && proto == 0x03) ||
|
|
|
|
|
+ (cls == RNDIS_CLASS_MISC && sub == 0x01 && proto == 0x01) ||
|
|
|
|
|
+ (cls == RNDIS_CLASS_MISC && sub == 0x04 && proto == 0x01);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+struct rndis_cfg {
|
|
|
|
|
+ unsigned int config;
|
|
|
|
|
+ unsigned int ctrl_iface;
|
|
|
|
|
+ unsigned int data_iface;
|
|
|
|
|
+ unsigned int data_alt;
|
|
|
|
|
+ unsigned int ep_in, ep_in_mps;
|
|
|
|
|
+ unsigned int ep_out, ep_out_mps;
|
|
|
|
|
+ unsigned int ep_int, ep_int_mps;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_parse_config(unsigned int total, struct rndis_cfg *out)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i = 0;
|
|
|
|
|
+ unsigned int iface = 0, alt = 0, cls = 0;
|
|
|
|
|
+ unsigned int ctrl_iface = 0, data_iface = 0, data_alt = 0;
|
|
|
|
|
+ unsigned int int_ep = 0, int_mps = 0;
|
|
|
|
|
+ unsigned int in_ep = 0, in_mps = 0, in_alt = 0;
|
|
|
|
|
+ unsigned int out_ep = 0, out_mps = 0, out_alt = 0;
|
|
|
|
|
+ int have_ctrl = 0;
|
|
|
|
|
+ int have_data = 0;
|
|
|
|
|
+
|
|
|
|
|
+ while (i + 2 <= total) {
|
|
|
|
|
+ unsigned int blen = cfg_buf[i];
|
|
|
|
|
+ unsigned int type = cfg_buf[i + 1];
|
|
|
|
|
+
|
|
|
|
|
+ if (blen < 2 || i + blen > total)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (type == 0x04 && blen >= 9) {
|
|
|
|
|
+ iface = cfg_buf[i + 2];
|
|
|
|
|
+ alt = cfg_buf[i + 3];
|
|
|
|
|
+ cls = cfg_buf[i + 5];
|
|
|
|
|
+ if (alt == 0 && !have_ctrl &&
|
|
|
|
|
+ rndis_is_control(cfg_buf[i + 5], cfg_buf[i + 6],
|
|
|
|
|
+ cfg_buf[i + 7])) {
|
|
|
|
|
+ have_ctrl = 1;
|
|
|
|
|
+ ctrl_iface = iface;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (alt == 0 && cls == RNDIS_CLASS_DATA && !have_data) {
|
|
|
|
|
+ data_iface = iface;
|
|
|
|
|
+ have_data = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (type == 0x05 && blen >= 7) {
|
|
|
|
|
+ unsigned int epa = cfg_buf[i + 2];
|
|
|
|
|
+ unsigned int attr = cfg_buf[i + 3] & 3u;
|
|
|
|
|
+ unsigned int mps = rd16le(cfg_buf + i + 4) & 0x7FFu;
|
|
|
|
|
+
|
|
|
|
|
+ if (attr == 2 && (cls == RNDIS_CLASS_DATA ||
|
|
|
|
|
+ iface == ctrl_iface)) {
|
|
|
|
|
+ if (cls == RNDIS_CLASS_DATA && !have_data) {
|
|
|
|
|
+ data_iface = iface;
|
|
|
|
|
+ have_data = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (epa & 0x80) {
|
|
|
|
|
+ if (!in_ep) {
|
|
|
|
|
+ in_ep = epa & 0x0F;
|
|
|
|
|
+ in_mps = mps;
|
|
|
|
|
+ in_alt = alt;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (!out_ep) {
|
|
|
|
|
+ out_ep = epa & 0x0F;
|
|
|
|
|
+ out_mps = mps;
|
|
|
|
|
+ out_alt = alt;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (attr == 3 && (epa & 0x80) &&
|
|
|
|
|
+ iface == ctrl_iface && !int_ep) {
|
|
|
|
|
+ int_ep = epa & 0x0F;
|
|
|
|
|
+ int_mps = mps;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ i += blen;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!have_ctrl || !in_ep || !out_ep || !cfg_buf[5])
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (in_alt != out_alt)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (!have_data)
|
|
|
|
|
+ data_iface = ctrl_iface;
|
|
|
|
|
+ data_alt = in_alt;
|
|
|
|
|
+
|
|
|
|
|
+ out->config = cfg_buf[5];
|
|
|
|
|
+ out->ctrl_iface = ctrl_iface;
|
|
|
|
|
+ out->data_iface = data_iface;
|
|
|
|
|
+ out->data_alt = data_alt;
|
|
|
|
|
+ out->ep_in = in_ep;
|
|
|
|
|
+ out->ep_in_mps = in_mps ? in_mps : 64;
|
|
|
|
|
+ out->ep_out = out_ep;
|
|
|
|
|
+ out->ep_out_mps = out_mps ? out_mps : 64;
|
|
|
|
|
+ out->ep_int = int_ep;
|
|
|
|
|
+ out->ep_int_mps = int_mps;
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_read_config(unsigned int index, unsigned int *total_out)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned char hdr[9];
|
|
|
|
|
+ unsigned int total;
|
|
|
|
|
+ int n;
|
|
|
|
|
+
|
|
|
|
|
+ n = usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x80, 0x06, (unsigned short)((2u << 8) | index), 0,
|
|
|
|
|
+ hdr, sizeof(hdr));
|
|
|
|
|
+ if (n < 9 || hdr[1] != 0x02)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ total = rd16le(hdr + 2);
|
|
|
|
|
+ if (total < 9 || total > RNDIS_CFG_MAX)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ n = usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x80, 0x06, (unsigned short)((2u << 8) | index), 0,
|
|
|
|
|
+ cfg_buf, total);
|
|
|
|
|
+ if (n < 9 || cfg_buf[1] != 0x02)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if ((unsigned int)n < total)
|
|
|
|
|
+ total = (unsigned int)n;
|
|
|
|
|
+ *total_out = total;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int rndis_choose_config(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned char desc[18];
|
|
|
|
|
+ unsigned int ncfg, i;
|
|
|
|
|
+ int n;
|
|
|
|
|
+
|
|
|
|
|
+ n = usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x80, 0x06, (unsigned short)(1u << 8), 0,
|
|
|
|
|
+ desc, sizeof(desc));
|
|
|
|
|
+ if (n < 18)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ ncfg = desc[17];
|
|
|
|
|
+ if (!ncfg)
|
|
|
|
|
+ ncfg = 1;
|
|
|
|
|
+ if (ncfg > 8)
|
|
|
|
|
+ ncfg = 8;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < ncfg; i++) {
|
|
|
|
|
+ struct rndis_cfg cfg;
|
|
|
|
|
+ unsigned int total;
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_read_config(i, &total) < 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (!rndis_parse_config(total, &cfg))
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (usb_control_out(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x00, 0x09,
|
|
|
|
|
+ (unsigned short)cfg.config, 0) < 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ rndis_delay(10);
|
|
|
|
|
+ if (cfg.data_alt != 0 &&
|
|
|
|
|
+ usb_control_out(dev.ctrl, dev.addr, dev.mps, dev.low,
|
|
|
|
|
+ 0x01, 0x0B,
|
|
|
|
|
+ (unsigned short)cfg.data_alt,
|
|
|
|
|
+ (unsigned short)cfg.data_iface) < 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ dev.config = cfg.config;
|
|
|
|
|
+ dev.ctrl_iface = cfg.ctrl_iface;
|
|
|
|
|
+ dev.data_iface = cfg.data_iface;
|
|
|
|
|
+ dev.ep_in = cfg.ep_in;
|
|
|
|
|
+ dev.ep_in_mps = cfg.ep_in_mps;
|
|
|
|
|
+ dev.ep_out = cfg.ep_out;
|
|
|
|
|
+ dev.ep_out_mps = cfg.ep_out_mps;
|
|
|
|
|
+ dev.ep_int = cfg.ep_int;
|
|
|
|
|
+ dev.ep_int_mps = cfg.ep_int_mps;
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "rndis: config ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, dev.config);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, ", iface ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, dev.ctrl_iface);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "/");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, dev.data_iface);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, ", ep ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, dev.ep_in);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "/");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, dev.ep_out);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "\n");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ return -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void rndis_check(void)
|
|
|
|
|
+{
|
|
|
|
|
+ if (!dev.present)
|
|
|
|
|
+ return;
|
|
|
|
|
+ if (dev.gen != usb_generation() ||
|
|
|
|
|
+ (dev.port != 0xFFu && !usb_port_connected(dev.ctrl, dev.port))) {
|
|
|
|
|
+ dev.present = 0;
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int net_init(void)
|
|
|
|
|
+{
|
|
|
|
|
+ rndis_check();
|
|
|
|
|
+ if (!dev.present)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (dev.ready)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ return rndis_start();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void net_get_mac(unsigned char mac[6])
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 6; i++)
|
|
|
|
|
+ mac[i] = dev.mac[i];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int net_rx(unsigned char *buf, unsigned int maxlen)
|
|
|
|
|
+{
|
|
|
|
|
+ rndis_check();
|
|
|
|
|
+ if (!dev.present || !dev.ready)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+
|
|
|
|
|
+ for (;;) {
|
|
|
|
|
+ /* Parse every complete message in the current chunk. */
|
|
|
|
|
+ while (dev.rx_pos + 44 <= dev.rx_len) {
|
|
|
|
|
+ unsigned int msg_type = rd32le(dev.rx_buf + dev.rx_pos);
|
|
|
|
|
+ unsigned int msg_len = rd32le(dev.rx_buf + dev.rx_pos + 4);
|
|
|
|
|
+ unsigned int data_off, data_len, advance, data_pos, i;
|
|
|
|
|
+
|
|
|
|
|
+ if (msg_type != RNDIS_MSG_PACKET ||
|
|
|
|
|
+ msg_len < 44 || msg_len > RNDIS_RX_MAX) {
|
|
|
|
|
+ dev.rx_pos = dev.rx_len;
|
|
|
|
|
+ dev.rx_errors++;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ /* A message split across bulk transfers: keep the
|
|
|
|
|
+ * tail and append the next transfer. */
|
|
|
|
|
+ if (msg_len > dev.rx_len - dev.rx_pos)
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ data_off = rd32le(dev.rx_buf + dev.rx_pos + 8);
|
|
|
|
|
+ data_len = rd32le(dev.rx_buf + dev.rx_pos + 12);
|
|
|
|
|
+ advance = msg_len;
|
|
|
|
|
+ if (dev.align > 1)
|
|
|
|
|
+ advance = (advance + dev.align - 1) &
|
|
|
|
|
+ ~(dev.align - 1);
|
|
|
|
|
+ if (data_len > msg_len || data_off > msg_len - data_len ||
|
|
|
|
|
+ msg_len - data_len - data_off < 8) {
|
|
|
|
|
+ dev.rx_pos += advance;
|
|
|
|
|
+ if (dev.rx_pos > dev.rx_len)
|
|
|
|
|
+ dev.rx_pos = dev.rx_len;
|
|
|
|
|
+ dev.rx_errors++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ data_pos = dev.rx_pos + 8 + data_off;
|
|
|
|
|
+ dev.rx_pos += advance;
|
|
|
|
|
+ if (dev.rx_pos > dev.rx_len)
|
|
|
|
|
+ dev.rx_pos = dev.rx_len;
|
|
|
|
|
+
|
|
|
|
|
+ if (data_len > maxlen || data_len < 14) {
|
|
|
|
|
+ dev.rx_errors++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < data_len; i++)
|
|
|
|
|
+ buf[i] = dev.rx_buf[data_pos + i];
|
|
|
|
|
+ dev.rx_frames++;
|
|
|
|
|
+ return (int)data_len;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* Move an incomplete message to the start and append more
|
|
|
|
|
+ * bytes from the next bulk transfer. */
|
|
|
|
|
+ if (dev.rx_pos > 0) {
|
|
|
|
|
+ unsigned int rem;
|
|
|
|
|
+
|
|
|
|
|
+ if (dev.rx_pos > dev.rx_len) {
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ rem = dev.rx_len - dev.rx_pos;
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < rem; i++)
|
|
|
|
|
+ dev.rx_buf[i] =
|
|
|
|
|
+ dev.rx_buf[dev.rx_pos + i];
|
|
|
|
|
+ }
|
|
|
|
|
+ dev.rx_pos = 0;
|
|
|
|
|
+ dev.rx_len = rem;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dev.rx_len >= sizeof(dev.rx_buf)) {
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ dev.rx_errors++;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ {
|
|
|
|
|
+ int n = usb_bulk_in(dev.ctrl, dev.addr, dev.ep_in,
|
|
|
|
|
+ dev.ep_in_mps, dev.raw_buf,
|
|
|
|
|
+ sizeof(dev.raw_buf));
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ if (n <= 0)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (dev.rx_len + (unsigned int)n > sizeof(dev.rx_buf)) {
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ dev.rx_errors++;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < (unsigned int)n; i++)
|
|
|
|
|
+ dev.rx_buf[dev.rx_len + i] = dev.raw_buf[i];
|
|
|
|
|
+ dev.rx_len += (unsigned int)n;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int net_tx(const unsigned char *buf, unsigned int len)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ rndis_check();
|
|
|
|
|
+ if (!dev.present || !dev.ready || len > 1514)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (dev.max_transfer && 44 + len > dev.max_transfer)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 44; i++)
|
|
|
|
|
+ dev.tx_buf[i] = 0;
|
|
|
|
|
+ wr32le(dev.tx_buf + 0, RNDIS_MSG_PACKET);
|
|
|
|
|
+ wr32le(dev.tx_buf + 4, 44 + len);
|
|
|
|
|
+ wr32le(dev.tx_buf + 8, 36); /* offset from the 8-byte prefix */
|
|
|
|
|
+ wr32le(dev.tx_buf + 12, len);
|
|
|
|
|
+ for (i = 0; i < len; i++)
|
|
|
|
|
+ dev.tx_buf[44 + i] = buf[i];
|
|
|
|
|
+
|
|
|
|
|
+ if (usb_bulk_out(dev.ctrl, dev.addr, dev.ep_out, dev.ep_out_mps,
|
|
|
|
|
+ dev.tx_buf, 44 + len) != 0) {
|
|
|
|
|
+ dev.tx_errors++;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ dev.tx_frames++;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void net_print_info(void)
|
|
|
|
|
+{
|
|
|
|
|
+ rndis_check();
|
|
|
|
|
+ io_putstr("Android USB tethering (RNDIS)\n");
|
|
|
|
|
+ io_putstr(" MAC ");
|
|
|
|
|
+ put_mac_io();
|
|
|
|
|
+ io_putstr("\n");
|
|
|
|
|
+ if (!dev.present) {
|
|
|
|
|
+ io_putstr(" not attached\n");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!dev.ready)
|
|
|
|
|
+ io_putstr(" RNDIS not initialized\n");
|
|
|
|
|
+ else if (dev.link)
|
|
|
|
|
+ io_putstr(" link up (DHCP)\n");
|
|
|
|
|
+ else
|
|
|
|
|
+ io_putstr(" link down (enable USB tethering on the phone)\n");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void net_reset(void)
|
|
|
|
|
+{
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ if (!dev.present)
|
|
|
|
|
+ return;
|
|
|
|
|
+ rndis_send_halt();
|
|
|
|
|
+ if (rndis_choose_config() < 0) {
|
|
|
|
|
+ dev.present = 0;
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ (void)rndis_start();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void net_stop(void)
|
|
|
|
|
+{
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const struct netdev rndis_netdev = {
|
|
|
|
|
+ "rndis",
|
|
|
|
|
+ "Android USB tethering (RNDIS)",
|
|
|
|
|
+ net_init,
|
|
|
|
|
+ net_get_mac,
|
|
|
|
|
+ net_rx,
|
|
|
|
|
+ net_tx,
|
|
|
|
|
+ net_print_info,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ net_reset,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ net_stop,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static int device_probe(unsigned int ctrl, unsigned int addr,
|
|
|
|
|
+ unsigned int mps, int low_speed, unsigned int port,
|
|
|
|
|
+ unsigned int vendor, unsigned int product)
|
|
|
|
|
+{
|
|
|
|
|
+ struct rndis_saved saved;
|
|
|
|
|
+ unsigned int gen = usb_generation();
|
|
|
|
|
+ int was_ready = dev.ready;
|
|
|
|
|
+
|
|
|
|
|
+ (void)vendor;
|
|
|
|
|
+ (void)product;
|
|
|
|
|
+
|
|
|
|
|
+ if (!usb_bulk_supported(ctrl))
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+ /* A probe runs for every USB device, not just ours. Save the current
|
|
|
|
|
+ * phone state and put it back if this device is not an RNDIS
|
|
|
|
|
+ * function. Do not clear an old ready state on a generation change
|
|
|
|
|
+ * here: a full scan may probe a mouse before the phone. */
|
|
|
|
|
+ rndis_save(&saved);
|
|
|
|
|
+
|
|
|
|
|
+ dev.ctrl = ctrl;
|
|
|
|
|
+ dev.addr = addr;
|
|
|
|
|
+ dev.mps = mps;
|
|
|
|
|
+ dev.low = low_speed;
|
|
|
|
|
+ dev.port = port;
|
|
|
|
|
+ dev.config = 0;
|
|
|
|
|
+ dev.ready = 0;
|
|
|
|
|
+ dev.link = 0;
|
|
|
|
|
+ dev.rx_pos = dev.rx_len = 0;
|
|
|
|
|
+ dev.xid = 0;
|
|
|
|
|
+ dev.present = 1;
|
|
|
|
|
+
|
|
|
|
|
+ if (rndis_choose_config() < 0) {
|
|
|
|
|
+ rndis_restore_after_probe(&saved, ctrl, port, gen);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* Do not run the RNDIS control handshake for a fresh boot-time probe:
|
|
|
|
|
+ * an Ethernet NIC may be the active link. If the phone was already
|
|
|
|
|
+ * ready before this scan, re-establish it now (a full `lsusb` scan
|
|
|
|
|
+ * resets the device configuration). */
|
|
|
|
|
+ dev.gen = gen;
|
|
|
|
|
+ if (was_ready) {
|
|
|
|
|
+ if (rndis_start() < 0 && saved.present &&
|
|
|
|
|
+ (saved.ctrl != ctrl || saved.port != port ||
|
|
|
|
|
+ saved.addr != addr)) {
|
|
|
|
|
+ rndis_restore(&saved);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ netdev_hotplug();
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void rndis_register(void)
|
|
|
|
|
+{
|
|
|
|
|
+ usb_register_device_probe(device_probe);
|
|
|
|
|
+}
|