1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/netdevice.h>
12#include <linux/ethtool.h>
13#include <linux/if_vlan.h>
14#include <linux/ip.h>
15#include <linux/mii.h>
16#include <linux/usb.h>
17#include <linux/usb/cdc.h>
18#include <linux/usb/usbnet.h>
19#include <linux/usb/cdc-wdm.h>
20#include <linux/usb/cdc_ncm.h>
21#include <net/ipv6.h>
22#include <net/addrconf.h>
23#include <net/ipv6_stubs.h>
24
25
26#define MBIM_IPS0_VID 4094
27
28
29struct cdc_mbim_state {
30 struct cdc_ncm_ctx *ctx;
31 atomic_t pmcount;
32 struct usb_driver *subdriver;
33 unsigned long _unused;
34 unsigned long flags;
35};
36
37
38enum cdc_mbim_flags {
39 FLAG_IPS0_VLAN = 1 << 0,
40};
41
42
43static int cdc_mbim_manage_power(struct usbnet *dev, int on)
44{
45 struct cdc_mbim_state *info = (void *)&dev->data;
46 int rv = 0;
47
48 dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
49
50 if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
51
52 rv = usb_autopm_get_interface(dev->intf);
53 dev->intf->needs_remote_wakeup = on;
54 if (!rv)
55 usb_autopm_put_interface(dev->intf);
56 }
57 return 0;
58}
59
60static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
61{
62 struct usbnet *dev = usb_get_intfdata(intf);
63
64
65 if (!dev)
66 return 0;
67
68 return cdc_mbim_manage_power(dev, status);
69}
70
71static int cdc_mbim_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
72{
73 struct usbnet *dev = netdev_priv(netdev);
74 struct cdc_mbim_state *info = (void *)&dev->data;
75
76
77 if (vid == MBIM_IPS0_VID)
78 info->flags |= FLAG_IPS0_VLAN;
79 else
80 if (vid >= 512)
81 return -EINVAL;
82 return 0;
83}
84
85static int cdc_mbim_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
86{
87 struct usbnet *dev = netdev_priv(netdev);
88 struct cdc_mbim_state *info = (void *)&dev->data;
89
90
91 if (vid == MBIM_IPS0_VID)
92 info->flags &= ~FLAG_IPS0_VLAN;
93 return 0;
94}
95
96static const struct net_device_ops cdc_mbim_netdev_ops = {
97 .ndo_open = usbnet_open,
98 .ndo_stop = usbnet_stop,
99 .ndo_start_xmit = usbnet_start_xmit,
100 .ndo_tx_timeout = usbnet_tx_timeout,
101 .ndo_get_stats64 = dev_get_tstats64,
102 .ndo_change_mtu = cdc_ncm_change_mtu,
103 .ndo_set_mac_address = eth_mac_addr,
104 .ndo_validate_addr = eth_validate_addr,
105 .ndo_vlan_rx_add_vid = cdc_mbim_rx_add_vid,
106 .ndo_vlan_rx_kill_vid = cdc_mbim_rx_kill_vid,
107};
108
109
110
111
112
113static int cdc_mbim_set_ctrlalt(struct usbnet *dev, struct usb_interface *intf, u8 alt)
114{
115 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
116 const struct usb_device_id *id;
117 struct driver_info *info;
118 int ret;
119
120 ret = usb_set_interface(dev->udev,
121 intf->cur_altsetting->desc.bInterfaceNumber,
122 alt);
123 if (ret)
124 return ret;
125
126 id = usb_match_id(intf, driver->id_table);
127 if (!id)
128 return -ENODEV;
129
130 info = (struct driver_info *)id->driver_info;
131 if (info != dev->driver_info) {
132 dev_dbg(&intf->dev, "driver_info updated to '%s'\n",
133 info->description);
134 dev->driver_info = info;
135 }
136 return 0;
137}
138
139static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
140{
141 struct cdc_ncm_ctx *ctx;
142 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
143 int ret = -ENODEV;
144 u8 data_altsetting = 1;
145 struct cdc_mbim_state *info = (void *)&dev->data;
146
147
148 if (cdc_ncm_select_altsetting(intf) == CDC_NCM_COMM_ALTSETTING_MBIM) {
149 data_altsetting = CDC_NCM_DATA_ALTSETTING_MBIM;
150 ret = cdc_mbim_set_ctrlalt(dev, intf, CDC_NCM_COMM_ALTSETTING_MBIM);
151 if (ret)
152 goto err;
153 ret = -ENODEV;
154 }
155
156
157 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
158 goto err;
159
160 ret = cdc_ncm_bind_common(dev, intf, data_altsetting, dev->driver_info->data);
161 if (ret)
162 goto err;
163
164 ctx = info->ctx;
165
166
167 if (ctx->mbim_desc && dev->status)
168 subdriver = usb_cdc_wdm_register(ctx->control,
169 &dev->status->desc,
170 le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
171 WWAN_PORT_MBIM,
172 cdc_mbim_wdm_manage_power);
173 if (IS_ERR(subdriver)) {
174 ret = PTR_ERR(subdriver);
175 cdc_ncm_unbind(dev, intf);
176 goto err;
177 }
178
179
180 dev->status = NULL;
181 info->subdriver = subdriver;
182
183
184 dev->net->flags |= IFF_NOARP;
185
186
187 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_FILTER;
188
189
190 dev->net->netdev_ops = &cdc_mbim_netdev_ops;
191err:
192 return ret;
193}
194
195static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
196{
197 struct cdc_mbim_state *info = (void *)&dev->data;
198 struct cdc_ncm_ctx *ctx = info->ctx;
199
200
201 if (info->subdriver && info->subdriver->disconnect)
202 info->subdriver->disconnect(ctx->control);
203 info->subdriver = NULL;
204
205
206 cdc_ncm_unbind(dev, intf);
207}
208
209
210static bool is_ip_proto(__be16 proto)
211{
212 switch (proto) {
213 case htons(ETH_P_IP):
214 case htons(ETH_P_IPV6):
215 return true;
216 }
217 return false;
218}
219
220static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
221{
222 struct sk_buff *skb_out;
223 struct cdc_mbim_state *info = (void *)&dev->data;
224 struct cdc_ncm_ctx *ctx = info->ctx;
225 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
226 u16 tci = 0;
227 bool is_ip;
228 u8 *c;
229
230 if (!ctx)
231 goto error;
232
233 if (skb) {
234 if (skb->len <= ETH_HLEN)
235 goto error;
236
237
238
239
240
241
242
243 skb_reset_mac_header(skb);
244 if (vlan_get_tag(skb, &tci) < 0 && skb->len > VLAN_ETH_HLEN &&
245 __vlan_get_tag(skb, &tci) == 0) {
246 is_ip = is_ip_proto(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
247 skb_pull(skb, VLAN_ETH_HLEN);
248 } else {
249 is_ip = is_ip_proto(eth_hdr(skb)->h_proto);
250 skb_pull(skb, ETH_HLEN);
251 }
252
253
254 if (info->flags & FLAG_IPS0_VLAN) {
255
256 if (!tci)
257 goto error;
258
259 if (tci == MBIM_IPS0_VID)
260 tci = 0;
261 }
262
263
264
265
266
267
268
269
270
271 switch (tci & 0x0f00) {
272 case 0x0000:
273 if (!is_ip)
274 goto error;
275 c = (u8 *)&sign;
276 c[3] = tci;
277 break;
278 case 0x0100:
279 if (is_ip)
280 goto error;
281 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
282 c = (u8 *)&sign;
283 c[3] = tci;
284 break;
285 default:
286 netif_err(dev, tx_err, dev->net,
287 "unsupported tci=0x%04x\n", tci);
288 goto error;
289 }
290 }
291
292 spin_lock_bh(&ctx->mtx);
293 skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
294 spin_unlock_bh(&ctx->mtx);
295 return skb_out;
296
297error:
298 if (skb)
299 dev_kfree_skb_any(skb);
300
301 return NULL;
302}
303
304
305
306
307
308static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
309{
310 struct ipv6hdr *iph = (void *)buf;
311 struct nd_msg *msg = (void *)(iph + 1);
312 struct net_device *netdev;
313 struct inet6_dev *in6_dev;
314 bool is_router;
315
316
317
318
319 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
320 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
321 return;
322
323
324 rcu_read_lock();
325 if (tci) {
326 netdev = __vlan_find_dev_deep_rcu(dev->net, htons(ETH_P_8021Q),
327 tci);
328 if (!netdev) {
329 rcu_read_unlock();
330 return;
331 }
332 } else {
333 netdev = dev->net;
334 }
335 dev_hold(netdev);
336 rcu_read_unlock();
337
338 in6_dev = in6_dev_get(netdev);
339 if (!in6_dev)
340 goto out;
341 is_router = !!in6_dev->cnf.forwarding;
342 in6_dev_put(in6_dev);
343
344
345 ipv6_stub->ndisc_send_na(netdev, &iph->saddr, &msg->target,
346 is_router ,
347 true ,
348 false ,
349 true );
350out:
351 dev_put(netdev);
352}
353
354static bool is_neigh_solicit(u8 *buf, size_t len)
355{
356 struct ipv6hdr *iph = (void *)buf;
357 struct nd_msg *msg = (void *)(iph + 1);
358
359 return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
360 iph->nexthdr == IPPROTO_ICMPV6 &&
361 msg->icmph.icmp6_code == 0 &&
362 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
363}
364
365
366static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
367{
368 __be16 proto = htons(ETH_P_802_3);
369 struct sk_buff *skb = NULL;
370
371 if (tci < 256 || tci == MBIM_IPS0_VID) {
372 if (len < sizeof(struct iphdr))
373 goto err;
374
375 switch (*buf & 0xf0) {
376 case 0x40:
377 proto = htons(ETH_P_IP);
378 break;
379 case 0x60:
380 if (is_neigh_solicit(buf, len))
381 do_neigh_solicit(dev, buf, tci);
382 proto = htons(ETH_P_IPV6);
383 break;
384 default:
385 goto err;
386 }
387 }
388
389 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
390 if (!skb)
391 goto err;
392
393
394 skb_put(skb, ETH_HLEN);
395 skb_reset_mac_header(skb);
396 eth_hdr(skb)->h_proto = proto;
397 eth_zero_addr(eth_hdr(skb)->h_source);
398 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
399
400
401 skb_put_data(skb, buf, len);
402
403
404 if (tci)
405 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tci);
406err:
407 return skb;
408}
409
410static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
411{
412 struct sk_buff *skb;
413 struct cdc_mbim_state *info = (void *)&dev->data;
414 struct cdc_ncm_ctx *ctx = info->ctx;
415 int len;
416 int nframes;
417 int x;
418 int offset;
419 struct usb_cdc_ncm_ndp16 *ndp16;
420 struct usb_cdc_ncm_dpe16 *dpe16;
421 int ndpoffset;
422 int loopcount = 50;
423 u32 payload = 0;
424 u8 *c;
425 u16 tci;
426
427 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
428 if (ndpoffset < 0)
429 goto error;
430
431next_ndp:
432 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
433 if (nframes < 0)
434 goto error;
435
436 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
437
438 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
439 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
440 c = (u8 *)&ndp16->dwSignature;
441 tci = c[3];
442
443 if (!tci && info->flags & FLAG_IPS0_VLAN)
444 tci = MBIM_IPS0_VID;
445 break;
446 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
447 c = (u8 *)&ndp16->dwSignature;
448 tci = c[3] + 256;
449 break;
450 default:
451 netif_dbg(dev, rx_err, dev->net,
452 "unsupported NDP signature <0x%08x>\n",
453 le32_to_cpu(ndp16->dwSignature));
454 goto err_ndp;
455
456 }
457
458 dpe16 = ndp16->dpe16;
459 for (x = 0; x < nframes; x++, dpe16++) {
460 offset = le16_to_cpu(dpe16->wDatagramIndex);
461 len = le16_to_cpu(dpe16->wDatagramLength);
462
463
464
465
466
467 if ((offset == 0) || (len == 0)) {
468 if (!x)
469 goto err_ndp;
470 break;
471 }
472
473
474 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
475 netif_dbg(dev, rx_err, dev->net,
476 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
477 x, offset, len, skb_in);
478 if (!x)
479 goto err_ndp;
480 break;
481 } else {
482 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
483 if (!skb)
484 goto error;
485 usbnet_skb_return(dev, skb);
486 payload += len;
487 }
488 }
489err_ndp:
490
491 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
492 if (ndpoffset && loopcount--)
493 goto next_ndp;
494
495
496 ctx->rx_overhead += skb_in->len - payload;
497 ctx->rx_ntbs++;
498
499 return 1;
500error:
501 return 0;
502}
503
504static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
505{
506 int ret = -ENODEV;
507 struct usbnet *dev = usb_get_intfdata(intf);
508 struct cdc_mbim_state *info = (void *)&dev->data;
509 struct cdc_ncm_ctx *ctx = info->ctx;
510
511 if (!ctx)
512 goto error;
513
514
515
516
517
518
519 ret = usbnet_suspend(intf, message);
520 if (ret < 0)
521 goto error;
522
523 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
524 ret = info->subdriver->suspend(intf, message);
525 if (ret < 0)
526 usbnet_resume(intf);
527
528error:
529 return ret;
530}
531
532static int cdc_mbim_resume(struct usb_interface *intf)
533{
534 int ret = 0;
535 struct usbnet *dev = usb_get_intfdata(intf);
536 struct cdc_mbim_state *info = (void *)&dev->data;
537 struct cdc_ncm_ctx *ctx = info->ctx;
538 bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
539
540 if (callsub)
541 ret = info->subdriver->resume(intf);
542 if (ret < 0)
543 goto err;
544 ret = usbnet_resume(intf);
545 if (ret < 0 && callsub)
546 info->subdriver->suspend(intf, PMSG_SUSPEND);
547err:
548 return ret;
549}
550
551static const struct driver_info cdc_mbim_info = {
552 .description = "CDC MBIM",
553 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
554 .bind = cdc_mbim_bind,
555 .unbind = cdc_mbim_unbind,
556 .manage_power = cdc_mbim_manage_power,
557 .rx_fixup = cdc_mbim_rx_fixup,
558 .tx_fixup = cdc_mbim_tx_fixup,
559};
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575static const struct driver_info cdc_mbim_info_zlp = {
576 .description = "CDC MBIM",
577 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
578 .bind = cdc_mbim_bind,
579 .unbind = cdc_mbim_unbind,
580 .manage_power = cdc_mbim_manage_power,
581 .rx_fixup = cdc_mbim_rx_fixup,
582 .tx_fixup = cdc_mbim_tx_fixup,
583};
584
585
586
587
588
589
590
591
592
593
594static const struct driver_info cdc_mbim_info_ndp_to_end = {
595 .description = "CDC MBIM",
596 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
597 .bind = cdc_mbim_bind,
598 .unbind = cdc_mbim_unbind,
599 .manage_power = cdc_mbim_manage_power,
600 .rx_fixup = cdc_mbim_rx_fixup,
601 .tx_fixup = cdc_mbim_tx_fixup,
602 .data = CDC_NCM_FLAG_NDP_TO_END,
603};
604
605
606
607
608
609static const struct driver_info cdc_mbim_info_avoid_altsetting_toggle = {
610 .description = "CDC MBIM",
611 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
612 .bind = cdc_mbim_bind,
613 .unbind = cdc_mbim_unbind,
614 .manage_power = cdc_mbim_manage_power,
615 .rx_fixup = cdc_mbim_rx_fixup,
616 .tx_fixup = cdc_mbim_tx_fixup,
617 .data = CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE,
618};
619
620static const struct usb_device_id mbim_devs[] = {
621
622
623
624
625
626
627
628 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
629 .driver_info = (unsigned long)&cdc_mbim_info,
630 },
631
632 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
633 .driver_info = (unsigned long)&cdc_mbim_info,
634 },
635
636
637
638
639
640
641 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
642 .driver_info = (unsigned long)&cdc_mbim_info_ndp_to_end,
643 },
644
645
646
647
648 { USB_DEVICE_AND_INTERFACE_INFO(0x03f0, 0xa31d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
649 .driver_info = (unsigned long)&cdc_mbim_info_ndp_to_end,
650 },
651
652
653 { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x1041, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
654 .driver_info = (unsigned long)&cdc_mbim_info_avoid_altsetting_toggle,
655 },
656
657
658 { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x1061, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
659 .driver_info = (unsigned long)&cdc_mbim_info_avoid_altsetting_toggle,
660 },
661
662
663 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
664 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
665 },
666 {
667 },
668};
669MODULE_DEVICE_TABLE(usb, mbim_devs);
670
671static struct usb_driver cdc_mbim_driver = {
672 .name = "cdc_mbim",
673 .id_table = mbim_devs,
674 .probe = usbnet_probe,
675 .disconnect = usbnet_disconnect,
676 .suspend = cdc_mbim_suspend,
677 .resume = cdc_mbim_resume,
678 .reset_resume = cdc_mbim_resume,
679 .supports_autosuspend = 1,
680 .disable_hub_initiated_lpm = 1,
681};
682module_usb_driver(cdc_mbim_driver);
683
684MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
685MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
686MODULE_DESCRIPTION("USB CDC MBIM host driver");
687MODULE_LICENSE("GPL");
688