net: cdc_mbim: handle IPv6 Neigbor Solicitations
[firefly-linux-kernel-4.4.55.git] / drivers / net / usb / cdc_mbim.c
1 /*
2  * Copyright (c) 2012  Smith Micro Software, Inc.
3  * Copyright (c) 2012  Bjørn Mork <bjorn@mork.no>
4  *
5  * This driver is based on and reuse most of cdc_ncm, which is
6  * Copyright (C) ST-Ericsson 2010-2012
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/if_vlan.h>
17 #include <linux/ip.h>
18 #include <linux/mii.h>
19 #include <linux/usb.h>
20 #include <linux/usb/cdc.h>
21 #include <linux/usb/usbnet.h>
22 #include <linux/usb/cdc-wdm.h>
23 #include <linux/usb/cdc_ncm.h>
24 #include <net/ipv6.h>
25 #include <net/addrconf.h>
26
27 /* driver specific data - must match cdc_ncm usage */
28 struct cdc_mbim_state {
29         struct cdc_ncm_ctx *ctx;
30         atomic_t pmcount;
31         struct usb_driver *subdriver;
32         struct usb_interface *control;
33         struct usb_interface *data;
34 };
35
36 /* using a counter to merge subdriver requests with our own into a combined state */
37 static int cdc_mbim_manage_power(struct usbnet *dev, int on)
38 {
39         struct cdc_mbim_state *info = (void *)&dev->data;
40         int rv = 0;
41
42         dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
43
44         if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
45                 /* need autopm_get/put here to ensure the usbcore sees the new value */
46                 rv = usb_autopm_get_interface(dev->intf);
47                 if (rv < 0)
48                         goto err;
49                 dev->intf->needs_remote_wakeup = on;
50                 usb_autopm_put_interface(dev->intf);
51         }
52 err:
53         return rv;
54 }
55
56 static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
57 {
58         struct usbnet *dev = usb_get_intfdata(intf);
59
60         /* can be called while disconnecting */
61         if (!dev)
62                 return 0;
63
64         return cdc_mbim_manage_power(dev, status);
65 }
66
67
68 static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
69 {
70         struct cdc_ncm_ctx *ctx;
71         struct usb_driver *subdriver = ERR_PTR(-ENODEV);
72         int ret = -ENODEV;
73         u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
74         struct cdc_mbim_state *info = (void *)&dev->data;
75
76         /* Probably NCM, defer for cdc_ncm_bind */
77         if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
78                 goto err;
79
80         ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
81         if (ret)
82                 goto err;
83
84         ctx = info->ctx;
85
86         /* The MBIM descriptor and the status endpoint are required */
87         if (ctx->mbim_desc && dev->status)
88                 subdriver = usb_cdc_wdm_register(ctx->control,
89                                                  &dev->status->desc,
90                                                  le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
91                                                  cdc_mbim_wdm_manage_power);
92         if (IS_ERR(subdriver)) {
93                 ret = PTR_ERR(subdriver);
94                 cdc_ncm_unbind(dev, intf);
95                 goto err;
96         }
97
98         /* can't let usbnet use the interrupt endpoint */
99         dev->status = NULL;
100         info->subdriver = subdriver;
101
102         /* MBIM cannot do ARP */
103         dev->net->flags |= IFF_NOARP;
104
105         /* no need to put the VLAN tci in the packet headers */
106         dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
107 err:
108         return ret;
109 }
110
111 static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
112 {
113         struct cdc_mbim_state *info = (void *)&dev->data;
114         struct cdc_ncm_ctx *ctx = info->ctx;
115
116         /* disconnect subdriver from control interface */
117         if (info->subdriver && info->subdriver->disconnect)
118                 info->subdriver->disconnect(ctx->control);
119         info->subdriver = NULL;
120
121         /* let NCM unbind clean up both control and data interface */
122         cdc_ncm_unbind(dev, intf);
123 }
124
125
126 static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
127 {
128         struct sk_buff *skb_out;
129         struct cdc_mbim_state *info = (void *)&dev->data;
130         struct cdc_ncm_ctx *ctx = info->ctx;
131         __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
132         u16 tci = 0;
133         u8 *c;
134
135         if (!ctx)
136                 goto error;
137
138         if (skb) {
139                 if (skb->len <= ETH_HLEN)
140                         goto error;
141
142                 /* mapping VLANs to MBIM sessions:
143                  *   no tag     => IPS session <0>
144                  *   1 - 255    => IPS session <vlanid>
145                  *   256 - 511  => DSS session <vlanid - 256>
146                  *   512 - 4095 => unsupported, drop
147                  */
148                 vlan_get_tag(skb, &tci);
149
150                 switch (tci & 0x0f00) {
151                 case 0x0000: /* VLAN ID 0 - 255 */
152                         /* verify that datagram is IPv4 or IPv6 */
153                         skb_reset_mac_header(skb);
154                         switch (eth_hdr(skb)->h_proto) {
155                         case htons(ETH_P_IP):
156                         case htons(ETH_P_IPV6):
157                                 break;
158                         default:
159                                 goto error;
160                         }
161                         c = (u8 *)&sign;
162                         c[3] = tci;
163                         break;
164                 case 0x0100: /* VLAN ID 256 - 511 */
165                         sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
166                         c = (u8 *)&sign;
167                         c[3] = tci;
168                         break;
169                 default:
170                         netif_err(dev, tx_err, dev->net,
171                                   "unsupported tci=0x%04x\n", tci);
172                         goto error;
173                 }
174                 skb_pull(skb, ETH_HLEN);
175         }
176
177         spin_lock_bh(&ctx->mtx);
178         skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
179         spin_unlock_bh(&ctx->mtx);
180         return skb_out;
181
182 error:
183         if (skb)
184                 dev_kfree_skb_any(skb);
185
186         return NULL;
187 }
188
189 /* Some devices are known to send Neigbor Solicitation messages and
190  * require Neigbor Advertisement replies.  The IPv6 core will not
191  * respond since IFF_NOARP is set, so we must handle them ourselves.
192  */
193 static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
194 {
195         struct ipv6hdr *iph = (void *)buf;
196         struct nd_msg *msg = (void *)(iph + 1);
197         struct net_device *netdev;
198         struct inet6_dev *in6_dev;
199         bool is_router;
200
201         /* we'll only respond to requests from unicast addresses to
202          * our solicited node addresses.
203          */
204         if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
205             !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
206                 return;
207
208         /* need to send the NA on the VLAN dev, if any */
209         if (tci)
210                 netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
211                                               tci);
212         else
213                 netdev = dev->net;
214         if (!netdev)
215                 return;
216
217         in6_dev = in6_dev_get(netdev);
218         if (!in6_dev)
219                 return;
220         is_router = !!in6_dev->cnf.forwarding;
221         in6_dev_put(in6_dev);
222
223         /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
224         ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
225                                  is_router /* router */,
226                                  true /* solicited */,
227                                  false /* override */,
228                                  true /* inc_opt */);
229 }
230
231 static bool is_neigh_solicit(u8 *buf, size_t len)
232 {
233         struct ipv6hdr *iph = (void *)buf;
234         struct nd_msg *msg = (void *)(iph + 1);
235
236         return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
237                 iph->nexthdr == IPPROTO_ICMPV6 &&
238                 msg->icmph.icmp6_code == 0 &&
239                 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
240 }
241
242
243 static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
244 {
245         __be16 proto = htons(ETH_P_802_3);
246         struct sk_buff *skb = NULL;
247
248         if (tci < 256) { /* IPS session? */
249                 if (len < sizeof(struct iphdr))
250                         goto err;
251
252                 switch (*buf & 0xf0) {
253                 case 0x40:
254                         proto = htons(ETH_P_IP);
255                         break;
256                 case 0x60:
257                         if (is_neigh_solicit(buf, len))
258                                 do_neigh_solicit(dev, buf, tci);
259                         proto = htons(ETH_P_IPV6);
260                         break;
261                 default:
262                         goto err;
263                 }
264         }
265
266         skb = netdev_alloc_skb_ip_align(dev->net,  len + ETH_HLEN);
267         if (!skb)
268                 goto err;
269
270         /* add an ethernet header */
271         skb_put(skb, ETH_HLEN);
272         skb_reset_mac_header(skb);
273         eth_hdr(skb)->h_proto = proto;
274         memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
275         memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
276
277         /* add datagram */
278         memcpy(skb_put(skb, len), buf, len);
279
280         /* map MBIM session to VLAN */
281         if (tci)
282                 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
283 err:
284         return skb;
285 }
286
287 static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
288 {
289         struct sk_buff *skb;
290         struct cdc_mbim_state *info = (void *)&dev->data;
291         struct cdc_ncm_ctx *ctx = info->ctx;
292         int len;
293         int nframes;
294         int x;
295         int offset;
296         struct usb_cdc_ncm_ndp16 *ndp16;
297         struct usb_cdc_ncm_dpe16 *dpe16;
298         int ndpoffset;
299         int loopcount = 50; /* arbitrary max preventing infinite loop */
300         u8 *c;
301         u16 tci;
302
303         ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
304         if (ndpoffset < 0)
305                 goto error;
306
307 next_ndp:
308         nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
309         if (nframes < 0)
310                 goto error;
311
312         ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
313
314         switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
315         case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
316                 c = (u8 *)&ndp16->dwSignature;
317                 tci = c[3];
318                 break;
319         case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
320                 c = (u8 *)&ndp16->dwSignature;
321                 tci = c[3] + 256;
322                 break;
323         default:
324                 netif_dbg(dev, rx_err, dev->net,
325                           "unsupported NDP signature <0x%08x>\n",
326                           le32_to_cpu(ndp16->dwSignature));
327                 goto err_ndp;
328
329         }
330
331         dpe16 = ndp16->dpe16;
332         for (x = 0; x < nframes; x++, dpe16++) {
333                 offset = le16_to_cpu(dpe16->wDatagramIndex);
334                 len = le16_to_cpu(dpe16->wDatagramLength);
335
336                 /*
337                  * CDC NCM ch. 3.7
338                  * All entries after first NULL entry are to be ignored
339                  */
340                 if ((offset == 0) || (len == 0)) {
341                         if (!x)
342                                 goto err_ndp; /* empty NTB */
343                         break;
344                 }
345
346                 /* sanity checking */
347                 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
348                         netif_dbg(dev, rx_err, dev->net,
349                                   "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
350                                   x, offset, len, skb_in);
351                         if (!x)
352                                 goto err_ndp;
353                         break;
354                 } else {
355                         skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
356                         if (!skb)
357                                 goto error;
358                         usbnet_skb_return(dev, skb);
359                 }
360         }
361 err_ndp:
362         /* are there more NDPs to process? */
363         ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
364         if (ndpoffset && loopcount--)
365                 goto next_ndp;
366
367         return 1;
368 error:
369         return 0;
370 }
371
372 static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
373 {
374         int ret = 0;
375         struct usbnet *dev = usb_get_intfdata(intf);
376         struct cdc_mbim_state *info = (void *)&dev->data;
377         struct cdc_ncm_ctx *ctx = info->ctx;
378
379         if (ctx == NULL) {
380                 ret = -1;
381                 goto error;
382         }
383
384         /*
385          * Both usbnet_suspend() and subdriver->suspend() MUST return 0
386          * in system sleep context, otherwise, the resume callback has
387          * to recover device from previous suspend failure.
388          */
389         ret = usbnet_suspend(intf, message);
390         if (ret < 0)
391                 goto error;
392
393         if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
394                 ret = info->subdriver->suspend(intf, message);
395         if (ret < 0)
396                 usbnet_resume(intf);
397
398 error:
399         return ret;
400 }
401
402 static int cdc_mbim_resume(struct usb_interface *intf)
403 {
404         int  ret = 0;
405         struct usbnet *dev = usb_get_intfdata(intf);
406         struct cdc_mbim_state *info = (void *)&dev->data;
407         struct cdc_ncm_ctx *ctx = info->ctx;
408         bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
409
410         if (callsub)
411                 ret = info->subdriver->resume(intf);
412         if (ret < 0)
413                 goto err;
414         ret = usbnet_resume(intf);
415         if (ret < 0 && callsub && info->subdriver->suspend)
416                 info->subdriver->suspend(intf, PMSG_SUSPEND);
417 err:
418         return ret;
419 }
420
421 static const struct driver_info cdc_mbim_info = {
422         .description = "CDC MBIM",
423         .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
424         .bind = cdc_mbim_bind,
425         .unbind = cdc_mbim_unbind,
426         .manage_power = cdc_mbim_manage_power,
427         .rx_fixup = cdc_mbim_rx_fixup,
428         .tx_fixup = cdc_mbim_tx_fixup,
429 };
430
431 /* MBIM and NCM devices should not need a ZLP after NTBs with
432  * dwNtbOutMaxSize length. This driver_info is for the exceptional
433  * devices requiring it anyway, allowing them to be supported without
434  * forcing the performance penalty on all the sane devices.
435  */
436 static const struct driver_info cdc_mbim_info_zlp = {
437         .description = "CDC MBIM",
438         .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
439         .bind = cdc_mbim_bind,
440         .unbind = cdc_mbim_unbind,
441         .manage_power = cdc_mbim_manage_power,
442         .rx_fixup = cdc_mbim_rx_fixup,
443         .tx_fixup = cdc_mbim_tx_fixup,
444 };
445
446 static const struct usb_device_id mbim_devs[] = {
447         /* This duplicate NCM entry is intentional. MBIM devices can
448          * be disguised as NCM by default, and this is necessary to
449          * allow us to bind the correct driver_info to such devices.
450          *
451          * bind() will sort out this for us, selecting the correct
452          * entry and reject the other
453          */
454         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
455           .driver_info = (unsigned long)&cdc_mbim_info,
456         },
457         /* Sierra Wireless MC7710 need ZLPs */
458         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68a2, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
459           .driver_info = (unsigned long)&cdc_mbim_info_zlp,
460         },
461         /* HP hs2434 Mobile Broadband Module needs ZLPs */
462         { USB_DEVICE_AND_INTERFACE_INFO(0x3f0, 0x4b1d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
463           .driver_info = (unsigned long)&cdc_mbim_info_zlp,
464         },
465         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
466           .driver_info = (unsigned long)&cdc_mbim_info,
467         },
468         {
469         },
470 };
471 MODULE_DEVICE_TABLE(usb, mbim_devs);
472
473 static struct usb_driver cdc_mbim_driver = {
474         .name = "cdc_mbim",
475         .id_table = mbim_devs,
476         .probe = usbnet_probe,
477         .disconnect = usbnet_disconnect,
478         .suspend = cdc_mbim_suspend,
479         .resume = cdc_mbim_resume,
480         .reset_resume = cdc_mbim_resume,
481         .supports_autosuspend = 1,
482         .disable_hub_initiated_lpm = 1,
483 };
484 module_usb_driver(cdc_mbim_driver);
485
486 MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
487 MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
488 MODULE_DESCRIPTION("USB CDC MBIM host driver");
489 MODULE_LICENSE("GPL");