lg-vl600.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Ethernet interface part of the LG VL600 LTE modem (4G dongle)
  4. *
  5. * Copyright (C) 2011 Intel Corporation
  6. * Author: Andrzej Zaborowski <balrogg@gmail.com>
  7. */
  8. #include <linux/etherdevice.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/mii.h>
  11. #include <linux/usb.h>
  12. #include <linux/usb/cdc.h>
  13. #include <linux/usb/usbnet.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/inetdevice.h>
  17. #include <linux/module.h>
  18. /*
  19. * The device has a CDC ACM port for modem control (it claims to be
  20. * CDC ACM anyway) and a CDC Ethernet port for actual network data.
  21. * It will however ignore data on both ports that is not encapsulated
  22. * in a specific way, any data returned is also encapsulated the same
  23. * way. The headers don't seem to follow any popular standard.
  24. *
  25. * This driver adds and strips these headers from the ethernet frames
  26. * sent/received from the CDC Ethernet port. The proprietary header
  27. * replaces the standard ethernet header in a packet so only actual
  28. * ethernet frames are allowed. The headers allow some form of
  29. * multiplexing by using non standard values of the .h_proto field.
  30. * Windows/Mac drivers do send a couple of such frames to the device
  31. * during initialisation, with protocol set to 0x0906 or 0x0b06 and (what
  32. * seems to be) a flag in the .dummy_flags. This doesn't seem necessary
  33. * for modem operation but can possibly be used for GPS or other funcitons.
  34. */
  35. struct vl600_frame_hdr {
  36. __le32 len;
  37. __le32 serial;
  38. __le32 pkt_cnt;
  39. __le32 dummy_flags;
  40. __le32 dummy;
  41. __le32 magic;
  42. } __attribute__((packed));
  43. struct vl600_pkt_hdr {
  44. __le32 dummy[2];
  45. __le32 len;
  46. __be16 h_proto;
  47. } __attribute__((packed));
  48. struct vl600_state {
  49. struct sk_buff *current_rx_buf;
  50. };
  51. static int vl600_bind(struct usbnet *dev, struct usb_interface *intf)
  52. {
  53. int ret;
  54. struct vl600_state *s = kzalloc(sizeof(struct vl600_state), GFP_KERNEL);
  55. if (!s)
  56. return -ENOMEM;
  57. ret = usbnet_cdc_bind(dev, intf);
  58. if (ret) {
  59. kfree(s);
  60. return ret;
  61. }
  62. dev->driver_priv = s;
  63. /* ARP packets don't go through, but they're also of no use. The
  64. * subnet has only two hosts anyway: us and the gateway / DHCP
  65. * server (probably simulated by modem firmware or network operator)
  66. * whose address changes everytime we connect to the intarwebz and
  67. * who doesn't bother answering ARP requests either. So hardware
  68. * addresses have no meaning, the destination and the source of every
  69. * packet depend only on whether it is on the IN or OUT endpoint. */
  70. dev->net->flags |= IFF_NOARP;
  71. /* IPv6 NDP relies on multicast. Enable it by default. */
  72. dev->net->flags |= IFF_MULTICAST;
  73. return ret;
  74. }
  75. static void vl600_unbind(struct usbnet *dev, struct usb_interface *intf)
  76. {
  77. struct vl600_state *s = dev->driver_priv;
  78. dev_kfree_skb(s->current_rx_buf);
  79. kfree(s);
  80. return usbnet_cdc_unbind(dev, intf);
  81. }
  82. static int vl600_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  83. {
  84. struct vl600_frame_hdr *frame;
  85. struct vl600_pkt_hdr *packet;
  86. struct ethhdr *ethhdr;
  87. int packet_len, count;
  88. struct sk_buff *buf = skb;
  89. struct sk_buff *clone;
  90. struct vl600_state *s = dev->driver_priv;
  91. /* Frame lengths are generally 4B multiplies but every couple of
  92. * hours there's an odd number of bytes sized yet correct frame,
  93. * so don't require this. */
  94. /* Allow a packet (or multiple packets batched together) to be
  95. * split across many frames. We don't allow a new batch to
  96. * begin in the same frame another one is ending however, and no
  97. * leading or trailing pad bytes. */
  98. if (s->current_rx_buf) {
  99. frame = (struct vl600_frame_hdr *) s->current_rx_buf->data;
  100. if (skb->len + s->current_rx_buf->len >
  101. le32_to_cpup(&frame->len)) {
  102. netif_err(dev, ifup, dev->net, "Fragment too long\n");
  103. dev->net->stats.rx_length_errors++;
  104. goto error;
  105. }
  106. buf = s->current_rx_buf;
  107. skb_put_data(buf, skb->data, skb->len);
  108. } else if (skb->len < 4) {
  109. netif_err(dev, ifup, dev->net, "Frame too short\n");
  110. dev->net->stats.rx_length_errors++;
  111. goto error;
  112. }
  113. frame = (struct vl600_frame_hdr *) buf->data;
  114. /* Yes, check that frame->magic == 0x53544448 (or 0x44544d48),
  115. * otherwise we may run out of memory w/a bad packet */
  116. if (ntohl(frame->magic) != 0x53544448 &&
  117. ntohl(frame->magic) != 0x44544d48)
  118. goto error;
  119. if (buf->len < sizeof(*frame) ||
  120. buf->len != le32_to_cpup(&frame->len)) {
  121. /* Save this fragment for later assembly */
  122. if (s->current_rx_buf)
  123. return 0;
  124. s->current_rx_buf = skb_copy_expand(skb, 0,
  125. le32_to_cpup(&frame->len), GFP_ATOMIC);
  126. if (!s->current_rx_buf)
  127. dev->net->stats.rx_errors++;
  128. return 0;
  129. }
  130. count = le32_to_cpup(&frame->pkt_cnt);
  131. skb_pull(buf, sizeof(*frame));
  132. while (count--) {
  133. if (buf->len < sizeof(*packet)) {
  134. netif_err(dev, ifup, dev->net, "Packet too short\n");
  135. goto error;
  136. }
  137. packet = (struct vl600_pkt_hdr *) buf->data;
  138. packet_len = sizeof(*packet) + le32_to_cpup(&packet->len);
  139. if (packet_len > buf->len) {
  140. netif_err(dev, ifup, dev->net,
  141. "Bad packet length stored in header\n");
  142. goto error;
  143. }
  144. /* Packet header is same size as the ethernet header
  145. * (sizeof(*packet) == sizeof(*ethhdr)), additionally
  146. * the h_proto field is in the same place so we just leave it
  147. * alone and fill in the remaining fields.
  148. */
  149. ethhdr = (struct ethhdr *) skb->data;
  150. if (be16_to_cpup(&ethhdr->h_proto) == ETH_P_ARP &&
  151. buf->len > 0x26) {
  152. /* Copy the addresses from packet contents */
  153. memcpy(ethhdr->h_source,
  154. &buf->data[sizeof(*ethhdr) + 0x8],
  155. ETH_ALEN);
  156. memcpy(ethhdr->h_dest,
  157. &buf->data[sizeof(*ethhdr) + 0x12],
  158. ETH_ALEN);
  159. } else {
  160. eth_zero_addr(ethhdr->h_source);
  161. memcpy(ethhdr->h_dest, dev->net->dev_addr, ETH_ALEN);
  162. /* Inbound IPv6 packets have an IPv4 ethertype (0x800)
  163. * for some reason. Peek at the L3 header to check
  164. * for IPv6 packets, and set the ethertype to IPv6
  165. * (0x86dd) so Linux can understand it.
  166. */
  167. if ((buf->data[sizeof(*ethhdr)] & 0xf0) == 0x60)
  168. ethhdr->h_proto = htons(ETH_P_IPV6);
  169. }
  170. if (count) {
  171. /* Not the last packet in this batch */
  172. clone = skb_clone(buf, GFP_ATOMIC);
  173. if (!clone)
  174. goto error;
  175. skb_trim(clone, packet_len);
  176. usbnet_skb_return(dev, clone);
  177. skb_pull(buf, (packet_len + 3) & ~3);
  178. } else {
  179. skb_trim(buf, packet_len);
  180. if (s->current_rx_buf) {
  181. usbnet_skb_return(dev, buf);
  182. s->current_rx_buf = NULL;
  183. return 0;
  184. }
  185. return 1;
  186. }
  187. }
  188. error:
  189. if (s->current_rx_buf) {
  190. dev_kfree_skb_any(s->current_rx_buf);
  191. s->current_rx_buf = NULL;
  192. }
  193. dev->net->stats.rx_errors++;
  194. return 0;
  195. }
  196. static struct sk_buff *vl600_tx_fixup(struct usbnet *dev,
  197. struct sk_buff *skb, gfp_t flags)
  198. {
  199. struct sk_buff *ret;
  200. struct vl600_frame_hdr *frame;
  201. struct vl600_pkt_hdr *packet;
  202. static uint32_t serial = 1;
  203. int orig_len = skb->len - sizeof(struct ethhdr);
  204. int full_len = (skb->len + sizeof(struct vl600_frame_hdr) + 3) & ~3;
  205. frame = (struct vl600_frame_hdr *) skb->data;
  206. if (skb->len > sizeof(*frame) && skb->len == le32_to_cpup(&frame->len))
  207. return skb; /* Already encapsulated? */
  208. if (skb->len < sizeof(struct ethhdr))
  209. /* Drop, device can only deal with ethernet packets */
  210. return NULL;
  211. if (!skb_cloned(skb)) {
  212. int headroom = skb_headroom(skb);
  213. int tailroom = skb_tailroom(skb);
  214. if (tailroom >= full_len - skb->len - sizeof(*frame) &&
  215. headroom >= sizeof(*frame))
  216. /* There's enough head and tail room */
  217. goto encapsulate;
  218. if (headroom + tailroom + skb->len >= full_len) {
  219. /* There's enough total room, just readjust */
  220. skb->data = memmove(skb->head + sizeof(*frame),
  221. skb->data, skb->len);
  222. skb_set_tail_pointer(skb, skb->len);
  223. goto encapsulate;
  224. }
  225. }
  226. /* Alloc a new skb with the required size */
  227. ret = skb_copy_expand(skb, sizeof(struct vl600_frame_hdr), full_len -
  228. skb->len - sizeof(struct vl600_frame_hdr), flags);
  229. dev_kfree_skb_any(skb);
  230. if (!ret)
  231. return ret;
  232. skb = ret;
  233. encapsulate:
  234. /* Packet header is same size as ethernet packet header
  235. * (sizeof(*packet) == sizeof(struct ethhdr)), additionally the
  236. * h_proto field is in the same place so we just leave it alone and
  237. * overwrite the remaining fields.
  238. */
  239. packet = (struct vl600_pkt_hdr *) skb->data;
  240. /* The VL600 wants IPv6 packets to have an IPv4 ethertype
  241. * Since this modem only supports IPv4 and IPv6, just set all
  242. * frames to 0x0800 (ETH_P_IP)
  243. */
  244. packet->h_proto = htons(ETH_P_IP);
  245. memset(&packet->dummy, 0, sizeof(packet->dummy));
  246. packet->len = cpu_to_le32(orig_len);
  247. frame = skb_push(skb, sizeof(*frame));
  248. memset(frame, 0, sizeof(*frame));
  249. frame->len = cpu_to_le32(full_len);
  250. frame->serial = cpu_to_le32(serial++);
  251. frame->pkt_cnt = cpu_to_le32(1);
  252. if (skb->len < full_len) /* Pad */
  253. skb_put(skb, full_len - skb->len);
  254. return skb;
  255. }
  256. static const struct driver_info vl600_info = {
  257. .description = "LG VL600 modem",
  258. .flags = FLAG_RX_ASSEMBLE | FLAG_WWAN,
  259. .bind = vl600_bind,
  260. .unbind = vl600_unbind,
  261. .status = usbnet_cdc_status,
  262. .rx_fixup = vl600_rx_fixup,
  263. .tx_fixup = vl600_tx_fixup,
  264. };
  265. static const struct usb_device_id products[] = {
  266. {
  267. USB_DEVICE_AND_INTERFACE_INFO(0x1004, 0x61aa, USB_CLASS_COMM,
  268. USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
  269. .driver_info = (unsigned long) &vl600_info,
  270. },
  271. {}, /* End */
  272. };
  273. MODULE_DEVICE_TABLE(usb, products);
  274. static struct usb_driver lg_vl600_driver = {
  275. .name = "lg-vl600",
  276. .id_table = products,
  277. .probe = usbnet_probe,
  278. .disconnect = usbnet_disconnect,
  279. .suspend = usbnet_suspend,
  280. .resume = usbnet_resume,
  281. .disable_hub_initiated_lpm = 1,
  282. };
  283. module_usb_driver(lg_vl600_driver);
  284. MODULE_AUTHOR("Anrzej Zaborowski");
  285. MODULE_DESCRIPTION("LG-VL600 modem's ethernet link");
  286. MODULE_LICENSE("GPL");