if_umb.c revision 1.5 1 /* $NetBSD: if_umb.c,v 1.5 2018/09/20 09:45:16 khorben Exp $ */
2 /* $OpenBSD: if_umb.c,v 1.18 2018/02/19 08:59:52 mpi Exp $ */
3
4 /*
5 * Copyright (c) 2016 genua mbH
6 * All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*
22 * Mobile Broadband Interface Model specification:
23 * http://www.usb.org/developers/docs/devclass_docs/MBIM10Errata1_073013.zip
24 * Compliance testing guide
25 * http://www.usb.org/developers/docs/devclass_docs/MBIM-Compliance-1.0.pdf
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_umb.c,v 1.5 2018/09/20 09:45:16 khorben Exp $");
30
31 #ifdef _KERNEL_OPT
32 #include "opt_inet.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/endian.h>
38 #include <sys/kauth.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/mbuf.h>
42 #include <sys/rndsource.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
45 #include <sys/systm.h>
46
47 #include <net/bpf.h>
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #endif
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdivar.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdevs.h>
64 #include <dev/usb/usbcdc.h>
65
66 #include <dev/usb/mbim.h>
67 #include <dev/usb/if_umbreg.h>
68
69 #ifdef UMB_DEBUG
70 #define DPRINTF(x...) \
71 do { if (umb_debug) log(LOG_DEBUG, x); } while (0)
72
73 #define DPRINTFN(n, x...) \
74 do { if (umb_debug >= (n)) log(LOG_DEBUG, x); } while (0)
75
76 #define DDUMPN(n, b, l) \
77 do { \
78 if (umb_debug >= (n)) \
79 umb_dump((b), (l)); \
80 } while (0)
81
82 int umb_debug = 0;
83 Static char *umb_uuid2str(uint8_t [MBIM_UUID_LEN]);
84 Static void umb_dump(void *, int);
85
86 #else
87 #define DPRINTF(x...) do { } while (0)
88 #define DPRINTFN(n, x...) do { } while (0)
89 #define DDUMPN(n, b, l) do { } while (0)
90 #endif
91
92 #define DEVNAM(sc) device_xname((sc)->sc_dev)
93
94 /*
95 * State change timeout
96 */
97 #define UMB_STATE_CHANGE_TIMEOUT 30
98
99 /*
100 * State change flags
101 */
102 #define UMB_NS_DONT_DROP 0x0001 /* do not drop below current state */
103 #define UMB_NS_DONT_RAISE 0x0002 /* do not raise below current state */
104
105 /*
106 * Diagnostic macros
107 */
108 const struct umb_valdescr umb_regstates[] = MBIM_REGSTATE_DESCRIPTIONS;
109 const struct umb_valdescr umb_dataclasses[] = MBIM_DATACLASS_DESCRIPTIONS;
110 const struct umb_valdescr umb_simstate[] = MBIM_SIMSTATE_DESCRIPTIONS;
111 const struct umb_valdescr umb_messages[] = MBIM_MESSAGES_DESCRIPTIONS;
112 const struct umb_valdescr umb_status[] = MBIM_STATUS_DESCRIPTIONS;
113 const struct umb_valdescr umb_cids[] = MBIM_CID_DESCRIPTIONS;
114 const struct umb_valdescr umb_pktstate[] = MBIM_PKTSRV_STATE_DESCRIPTIONS;
115 const struct umb_valdescr umb_actstate[] = MBIM_ACTIVATION_STATE_DESCRIPTIONS;
116 const struct umb_valdescr umb_error[] = MBIM_ERROR_DESCRIPTIONS;
117 const struct umb_valdescr umb_pintype[] = MBIM_PINTYPE_DESCRIPTIONS;
118 const struct umb_valdescr umb_istate[] = UMB_INTERNAL_STATE_DESCRIPTIONS;
119
120 #define umb_regstate(c) umb_val2descr(umb_regstates, (c))
121 #define umb_dataclass(c) umb_val2descr(umb_dataclasses, (c))
122 #define umb_simstate(s) umb_val2descr(umb_simstate, (s))
123 #define umb_request2str(m) umb_val2descr(umb_messages, (m))
124 #define umb_status2str(s) umb_val2descr(umb_status, (s))
125 #define umb_cid2str(c) umb_val2descr(umb_cids, (c))
126 #define umb_packet_state(s) umb_val2descr(umb_pktstate, (s))
127 #define umb_activation(s) umb_val2descr(umb_actstate, (s))
128 #define umb_error2str(e) umb_val2descr(umb_error, (e))
129 #define umb_pin_type(t) umb_val2descr(umb_pintype, (t))
130 #define umb_istate(s) umb_val2descr(umb_istate, (s))
131
132 Static int umb_match(device_t, cfdata_t, void *);
133 Static void umb_attach(device_t, device_t, void *);
134 Static int umb_detach(device_t, int);
135 Static int umb_activate(device_t, enum devact);
136 Static void umb_ncm_setup(struct umb_softc *);
137 Static int umb_alloc_xfers(struct umb_softc *);
138 Static void umb_free_xfers(struct umb_softc *);
139 Static int umb_alloc_bulkpipes(struct umb_softc *);
140 Static void umb_close_bulkpipes(struct umb_softc *);
141 Static int umb_ioctl(struct ifnet *, u_long, void *);
142 Static int umb_output(struct ifnet *, struct mbuf *,
143 const struct sockaddr *, const struct rtentry *);
144 Static void umb_input(struct ifnet *, struct mbuf *);
145 Static void umb_start(struct ifnet *);
146 Static void umb_watchdog(struct ifnet *);
147 Static void umb_statechg_timeout(void *);
148
149 Static int umb_mediachange(struct ifnet *);
150 Static void umb_mediastatus(struct ifnet *, struct ifmediareq *);
151
152 Static void umb_newstate(struct umb_softc *, enum umb_state, int);
153 Static void umb_state_task(void *);
154 Static void umb_up(struct umb_softc *);
155 Static void umb_down(struct umb_softc *, int);
156
157 Static void umb_get_response_task(void *);
158
159 Static void umb_decode_response(struct umb_softc *, void *, int);
160 Static void umb_handle_indicate_status_msg(struct umb_softc *, void *,
161 int);
162 Static void umb_handle_opendone_msg(struct umb_softc *, void *, int);
163 Static void umb_handle_closedone_msg(struct umb_softc *, void *, int);
164 Static int umb_decode_register_state(struct umb_softc *, void *, int);
165 Static int umb_decode_devices_caps(struct umb_softc *, void *, int);
166 Static int umb_decode_subscriber_status(struct umb_softc *, void *, int);
167 Static int umb_decode_radio_state(struct umb_softc *, void *, int);
168 Static int umb_decode_pin(struct umb_softc *, void *, int);
169 Static int umb_decode_packet_service(struct umb_softc *, void *, int);
170 Static int umb_decode_signal_state(struct umb_softc *, void *, int);
171 Static int umb_decode_connect_info(struct umb_softc *, void *, int);
172 Static int umb_decode_ip_configuration(struct umb_softc *, void *, int);
173 Static void umb_rx(struct umb_softc *);
174 Static void umb_rxeof(struct usbd_xfer *, void *, usbd_status);
175 Static int umb_encap(struct umb_softc *, struct mbuf *);
176 Static void umb_txeof(struct usbd_xfer *, void *, usbd_status);
177 Static void umb_decap(struct umb_softc *, struct usbd_xfer *);
178
179 Static usbd_status umb_send_encap_command(struct umb_softc *, void *, int);
180 Static int umb_get_encap_response(struct umb_softc *, void *, int *);
181 Static void umb_ctrl_msg(struct umb_softc *, uint32_t, void *, int);
182
183 Static void umb_open(struct umb_softc *);
184 Static void umb_close(struct umb_softc *);
185
186 Static int umb_setpin(struct umb_softc *, int, int, void *, int, void *,
187 int);
188 Static void umb_setdataclass(struct umb_softc *);
189 Static void umb_radio(struct umb_softc *, int);
190 Static void umb_allocate_cid(struct umb_softc *);
191 Static void umb_send_fcc_auth(struct umb_softc *);
192 Static void umb_packet_service(struct umb_softc *, int);
193 Static void umb_connect(struct umb_softc *);
194 Static void umb_disconnect(struct umb_softc *);
195 Static void umb_send_connect(struct umb_softc *, int);
196
197 Static void umb_qry_ipconfig(struct umb_softc *);
198 Static void umb_cmd(struct umb_softc *, int, int, const void *, int);
199 Static void umb_cmd1(struct umb_softc *, int, int, const void *, int, uint8_t *);
200 Static void umb_command_done(struct umb_softc *, void *, int);
201 Static void umb_decode_cid(struct umb_softc *, uint32_t, void *, int);
202 Static void umb_decode_qmi(struct umb_softc *, uint8_t *, int);
203
204 Static void umb_intr(struct usbd_xfer *, void *, usbd_status);
205
206 Static char *umb_ntop(struct sockaddr *);
207
208 Static const char *
209 inet_ntop(int af, const void *src, char *dst, socklen_t size);
210 static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
211 #ifdef INET6
212 static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
213 #endif /* INET6 */
214
215 Static int umb_xfer_tout = USBD_DEFAULT_TIMEOUT;
216
217 Static uint8_t umb_uuid_basic_connect[] = MBIM_UUID_BASIC_CONNECT;
218 Static uint8_t umb_uuid_context_internet[] = MBIM_UUID_CONTEXT_INTERNET;
219 Static uint8_t umb_uuid_qmi_mbim[] = MBIM_UUID_QMI_MBIM;
220 Static uint32_t umb_session_id = 0;
221
222 CFATTACH_DECL_NEW(umb, sizeof(struct umb_softc), umb_match, umb_attach,
223 umb_detach, umb_activate);
224
225 const int umb_delay = 4000;
226
227 /*
228 * These devices require an "FCC Authentication" command.
229 */
230 const struct usb_devno umb_fccauth_devs[] = {
231 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM7455 },
232 };
233
234 Static const uint8_t umb_qmi_alloc_cid[] = {
235 0x01,
236 0x0f, 0x00, /* len */
237 0x00, /* QMUX flags */
238 0x00, /* service "ctl" */
239 0x00, /* CID */
240 0x00, /* QMI flags */
241 0x01, /* transaction */
242 0x22, 0x00, /* msg "Allocate CID" */
243 0x04, 0x00, /* TLV len */
244 0x01, 0x01, 0x00, 0x02 /* TLV */
245 };
246
247 Static const uint8_t umb_qmi_fcc_auth[] = {
248 0x01,
249 0x0c, 0x00, /* len */
250 0x00, /* QMUX flags */
251 0x02, /* service "dms" */
252 #define UMB_QMI_CID_OFFS 5
253 0x00, /* CID (filled in later) */
254 0x00, /* QMI flags */
255 0x01, 0x00, /* transaction */
256 0x5f, 0x55, /* msg "Send FCC Authentication" */
257 0x00, 0x00 /* TLV len */
258 };
259
260 Static int
261 umb_match(device_t parent, cfdata_t match, void *aux)
262 {
263 struct usbif_attach_arg *uiaa = aux;
264 usb_interface_descriptor_t *id;
265
266 if (!uiaa->uiaa_iface)
267 return UMATCH_NONE;
268 if ((id = usbd_get_interface_descriptor(uiaa->uiaa_iface)) == NULL)
269 return UMATCH_NONE;
270
271 /*
272 * If this function implements NCM, check if alternate setting
273 * 1 implements MBIM.
274 */
275 if (id->bInterfaceClass == UICLASS_CDC &&
276 id->bInterfaceSubClass ==
277 UISUBCLASS_NETWORK_CONTROL_MODEL)
278 id = usbd_find_idesc(uiaa->uiaa_device->ud_cdesc, uiaa->uiaa_iface->ui_index, 1);
279 if (id == NULL)
280 return UMATCH_NONE;
281
282 if (id->bInterfaceClass == UICLASS_CDC &&
283 id->bInterfaceSubClass ==
284 UISUBCLASS_MOBILE_BROADBAND_INTERFACE_MODEL &&
285 id->bInterfaceProtocol == 0)
286 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
287
288 return UMATCH_NONE;
289 }
290
291 Static void
292 umb_attach(device_t parent, device_t self, void *aux)
293 {
294 struct umb_softc *sc = device_private(self);
295 struct usbif_attach_arg *uiaa = aux;
296 char *devinfop;
297 usbd_status status;
298 usbd_desc_iter_t iter;
299 const usb_descriptor_t *desc;
300 int v;
301 const usb_cdc_union_descriptor_t *ud;
302 const struct mbim_descriptor *md;
303 int i;
304 int ctrl_ep;
305 const usb_interface_descriptor_t *id;
306 usb_config_descriptor_t *cd;
307 usb_endpoint_descriptor_t *ed;
308 const usb_interface_assoc_descriptor_t *ad;
309 int current_ifaceno = -1;
310 int data_ifaceno = -1;
311 int altnum;
312 int s;
313 struct ifnet *ifp;
314 int rv;
315
316 sc->sc_dev = self;
317 sc->sc_udev = uiaa->uiaa_device;
318
319 aprint_naive("\n");
320 aprint_normal("\n");
321
322 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
323 aprint_normal_dev(self, "%s\n", devinfop);
324 usbd_devinfo_free(devinfop);
325
326 sc->sc_ctrl_ifaceno = uiaa->uiaa_ifaceno;
327
328 /*
329 * Some MBIM hardware does not provide the mandatory CDC Union
330 * Descriptor, so we also look at matching Interface
331 * Association Descriptors to find out the MBIM Data Interface
332 * number.
333 */
334 sc->sc_ver_maj = sc->sc_ver_min = -1;
335 sc->sc_maxpktlen = MBIM_MAXSEGSZ_MINVAL;
336 usb_desc_iter_init(sc->sc_udev, &iter);
337 while ((desc = usb_desc_iter_next(&iter))) {
338 if (desc->bDescriptorType == UDESC_INTERFACE_ASSOC) {
339 ad = (const usb_interface_assoc_descriptor_t *)desc;
340 if (ad->bFirstInterface == uiaa->uiaa_ifaceno &&
341 ad->bInterfaceCount > 1)
342 data_ifaceno = uiaa->uiaa_ifaceno + 1;
343 continue;
344 }
345 if (desc->bDescriptorType == UDESC_INTERFACE) {
346 id = (const usb_interface_descriptor_t *)desc;
347 current_ifaceno = id->bInterfaceNumber;
348 continue;
349 }
350 if (current_ifaceno != uiaa->uiaa_ifaceno)
351 continue;
352 if (desc->bDescriptorType != UDESC_CS_INTERFACE)
353 continue;
354 switch (desc->bDescriptorSubtype) {
355 case UDESCSUB_CDC_UNION:
356 ud = (const usb_cdc_union_descriptor_t *)desc;
357 data_ifaceno = ud->bSlaveInterface[0];
358 break;
359 case UDESCSUB_MBIM:
360 md = (const struct mbim_descriptor *)desc;
361 v = UGETW(md->bcdMBIMVersion);
362 sc->sc_ver_maj = MBIM_VER_MAJOR(v);
363 sc->sc_ver_min = MBIM_VER_MINOR(v);
364 sc->sc_ctrl_len = UGETW(md->wMaxControlMessage);
365 /* Never trust a USB device! Could try to exploit us */
366 if (sc->sc_ctrl_len < MBIM_CTRLMSG_MINLEN ||
367 sc->sc_ctrl_len > MBIM_CTRLMSG_MAXLEN) {
368 DPRINTF("%s: control message len %d out of "
369 "bounds [%d .. %d]\n", DEVNAM(sc),
370 sc->sc_ctrl_len, MBIM_CTRLMSG_MINLEN,
371 MBIM_CTRLMSG_MAXLEN);
372 /* cont. anyway */
373 }
374 sc->sc_maxpktlen = UGETW(md->wMaxSegmentSize);
375 DPRINTFN(2, "%s: ctrl_len=%d, maxpktlen=%d, cap=0x%x\n",
376 DEVNAM(sc), sc->sc_ctrl_len, sc->sc_maxpktlen,
377 md->bmNetworkCapabilities);
378 break;
379 default:
380 break;
381 }
382 }
383 if (sc->sc_ver_maj < 0) {
384 aprint_error_dev(self, "missing MBIM descriptor\n");
385 goto fail;
386 }
387
388 aprint_normal_dev(self, "version %d.%d\n", sc->sc_ver_maj,
389 sc->sc_ver_min);
390
391 if (usb_lookup(umb_fccauth_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)) {
392 sc->sc_flags |= UMBFLG_FCC_AUTH_REQUIRED;
393 sc->sc_cid = -1;
394 }
395
396 for (i = 0; i < uiaa->uiaa_nifaces; i++) {
397 id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
398 if (id != NULL && id->bInterfaceNumber == data_ifaceno) {
399 sc->sc_data_iface = uiaa->uiaa_ifaces[i];
400 }
401 }
402 if (sc->sc_data_iface == NULL) {
403 aprint_error_dev(self, "no data interface found\n");
404 goto fail;
405 }
406
407 /*
408 * If this is a combined NCM/MBIM function, switch to
409 * alternate setting one to enable MBIM.
410 */
411 id = usbd_get_interface_descriptor(uiaa->uiaa_iface);
412 if (id->bInterfaceClass == UICLASS_CDC &&
413 id->bInterfaceSubClass ==
414 UISUBCLASS_NETWORK_CONTROL_MODEL)
415 usbd_set_interface(uiaa->uiaa_iface, 1);
416
417 id = usbd_get_interface_descriptor(uiaa->uiaa_iface);
418 ctrl_ep = -1;
419 for (i = 0; i < id->bNumEndpoints && ctrl_ep == -1; i++) {
420 ed = usbd_interface2endpoint_descriptor(uiaa->uiaa_iface, i);
421 if (ed == NULL)
422 break;
423 if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
424 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
425 ctrl_ep = ed->bEndpointAddress;
426 }
427 if (ctrl_ep == -1) {
428 aprint_error_dev(self, "missing interrupt endpoint\n");
429 goto fail;
430 }
431
432 /*
433 * For the MBIM Data Interface, select the appropriate
434 * alternate setting by looking for a matching descriptor that
435 * has two endpoints.
436 */
437 cd = usbd_get_config_descriptor(sc->sc_udev);
438 altnum = usbd_get_no_alts(cd, data_ifaceno);
439 for (i = 0; i < altnum; i++) {
440 id = usbd_find_idesc(cd, sc->sc_data_iface->ui_index, i);
441 if (id == NULL)
442 continue;
443 if (id->bInterfaceClass == UICLASS_CDC_DATA &&
444 id->bInterfaceSubClass == UISUBCLASS_DATA &&
445 id->bInterfaceProtocol == UIPROTO_DATA_MBIM &&
446 id->bNumEndpoints == 2)
447 break;
448 }
449 if (i == altnum || id == NULL) {
450 aprint_error_dev(self, "missing alt setting for interface #%d\n",
451 data_ifaceno);
452 goto fail;
453 }
454 status = usbd_set_interface(sc->sc_data_iface, i);
455 if (status) {
456 aprint_error_dev(self, "select alt setting %d for interface #%d "
457 "failed: %s\n", i, data_ifaceno, usbd_errstr(status));
458 goto fail;
459 }
460
461 id = usbd_get_interface_descriptor(sc->sc_data_iface);
462 sc->sc_rx_ep = sc->sc_tx_ep = -1;
463 for (i = 0; i < id->bNumEndpoints; i++) {
464 if ((ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface,
465 i)) == NULL)
466 break;
467 if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
468 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
469 sc->sc_rx_ep = ed->bEndpointAddress;
470 else if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
471 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
472 sc->sc_tx_ep = ed->bEndpointAddress;
473 }
474 if (sc->sc_rx_ep == -1 || sc->sc_tx_ep == -1) {
475 aprint_error_dev(self, "missing bulk endpoints\n");
476 goto fail;
477 }
478
479 DPRINTFN(2, "%s: ctrl-ifno#%d: ep-ctrl=%d, data-ifno#%d: ep-rx=%d, "
480 "ep-tx=%d\n", DEVNAM(sc), sc->sc_ctrl_ifaceno,
481 UE_GET_ADDR(ctrl_ep), data_ifaceno,
482 UE_GET_ADDR(sc->sc_rx_ep), UE_GET_ADDR(sc->sc_tx_ep));
483
484 usb_init_task(&sc->sc_umb_task, umb_state_task, sc,
485 0);
486 usb_init_task(&sc->sc_get_response_task, umb_get_response_task, sc,
487 0);
488 callout_init(&sc->sc_statechg_timer, 0);
489 callout_setfunc(&sc->sc_statechg_timer, umb_statechg_timeout, sc);
490
491 if (usbd_open_pipe_intr(uiaa->uiaa_iface, ctrl_ep, USBD_SHORT_XFER_OK,
492 &sc->sc_ctrl_pipe, sc, &sc->sc_intr_msg, sizeof(sc->sc_intr_msg),
493 umb_intr, USBD_DEFAULT_INTERVAL)) {
494 aprint_error_dev(self, "failed to open control pipe\n");
495 goto fail;
496 }
497
498 sc->sc_resp_buf = kmem_alloc(sc->sc_ctrl_len, KM_SLEEP);
499 sc->sc_ctrl_msg = kmem_alloc(sc->sc_ctrl_len, KM_SLEEP);
500
501 sc->sc_info.regstate = MBIM_REGSTATE_UNKNOWN;
502 sc->sc_info.pin_attempts_left = UMB_VALUE_UNKNOWN;
503 sc->sc_info.rssi = UMB_VALUE_UNKNOWN;
504 sc->sc_info.ber = UMB_VALUE_UNKNOWN;
505
506 umb_ncm_setup(sc);
507 DPRINTFN(2, "%s: rx/tx size %d/%d\n", DEVNAM(sc),
508 sc->sc_rx_bufsz, sc->sc_tx_bufsz);
509
510 s = splnet();
511
512 /* initialize the interface */
513 ifp = GET_IFP(sc);
514 ifp->if_softc = sc;
515 ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_POINTOPOINT;
516 ifp->if_ioctl = umb_ioctl;
517 ifp->if_start = umb_start;
518
519 ifp->if_watchdog = umb_watchdog;
520 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
521 ifp->if_link_state = LINK_STATE_DOWN;
522 ifmedia_init(&sc->sc_im, 0, umb_mediachange, umb_mediastatus);
523
524 ifp->if_type = IFT_MBIM;
525 ifp->if_addrlen = 0;
526 ifp->if_hdrlen = sizeof(struct ncm_header16) +
527 sizeof(struct ncm_pointer16);
528 ifp->if_mtu = 1500; /* use a common default */
529 ifp->if_mtu = sc->sc_maxpktlen;
530 ifp->if_output = umb_output;
531 ifp->_if_input = umb_input;
532 IFQ_SET_READY(&ifp->if_snd);
533
534 /* attach the interface */
535 rv = if_initialize(ifp);
536 if (rv != 0) {
537 aprint_error_dev(self, "if_initialize failed(%d)\n", rv);
538 splx(s);
539 return;
540 }
541 if_register(ifp);
542 if_alloc_sadl(ifp);
543
544 bpf_attach(ifp, DLT_RAW, 0);
545 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
546 RND_TYPE_NET, RND_FLAG_DEFAULT);
547
548 /*
549 * Open the device now so that we are able to query device information.
550 * XXX maybe close when done?
551 */
552 umb_open(sc);
553
554 sc->sc_attached = 1;
555 splx(s);
556
557 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
558
559 if (!pmf_device_register(self, NULL, NULL))
560 aprint_error_dev(self, "couldn't establish power handler\n");
561
562 return;
563
564 fail:
565 umb_activate(sc->sc_dev, DVACT_DEACTIVATE);
566 return;
567 }
568
569 Static int
570 umb_detach(device_t self, int flags)
571 {
572 struct umb_softc *sc = (struct umb_softc *)self;
573 struct ifnet *ifp = GET_IFP(sc);
574 int s;
575
576 pmf_device_deregister(self);
577
578 s = splnet();
579 if (ifp->if_flags & IFF_RUNNING)
580 umb_down(sc, 1);
581 umb_close(sc);
582
583 usb_rem_task_wait(sc->sc_udev, &sc->sc_get_response_task,
584 USB_TASKQ_DRIVER, NULL);
585 sc->sc_nresp = 0;
586 if (sc->sc_rx_ep != -1 && sc->sc_tx_ep != -1) {
587 callout_destroy(&sc->sc_statechg_timer);
588 usb_rem_task_wait(sc->sc_udev, &sc->sc_umb_task,
589 USB_TASKQ_DRIVER, NULL);
590 }
591 if (sc->sc_ctrl_pipe) {
592 usbd_close_pipe(sc->sc_ctrl_pipe);
593 sc->sc_ctrl_pipe = NULL;
594 }
595 if (sc->sc_ctrl_msg) {
596 kmem_free(sc->sc_ctrl_msg, sc->sc_ctrl_len);
597 sc->sc_ctrl_msg = NULL;
598 }
599 if (sc->sc_resp_buf) {
600 kmem_free(sc->sc_resp_buf, sc->sc_ctrl_len);
601 sc->sc_resp_buf = NULL;
602 }
603 if (ifp->if_softc) {
604 ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
605 }
606 if (sc->sc_attached) {
607 rnd_detach_source(&sc->sc_rnd_source);
608 bpf_detach(ifp);
609 if_detach(ifp);
610 }
611
612 sc->sc_attached = 0;
613 splx(s);
614 return 0;
615 }
616
617 Static int
618 umb_activate(device_t self, enum devact act)
619 {
620 struct umb_softc *sc = device_private(self);
621
622 switch (act) {
623 case DVACT_DEACTIVATE:
624 if_deactivate(GET_IFP(sc));
625 sc->sc_dying = 1;
626 return 0;
627 default:
628 return EOPNOTSUPP;
629 }
630 }
631
632 Static void
633 umb_ncm_setup(struct umb_softc *sc)
634 {
635 usb_device_request_t req;
636 struct ncm_ntb_parameters np;
637
638 /* Query NTB tranfers sizes */
639 req.bmRequestType = UT_READ_CLASS_INTERFACE;
640 req.bRequest = NCM_GET_NTB_PARAMETERS;
641 USETW(req.wValue, 0);
642 USETW(req.wIndex, sc->sc_ctrl_ifaceno);
643 USETW(req.wLength, sizeof(np));
644 if (usbd_do_request(sc->sc_udev, &req, &np) == USBD_NORMAL_COMPLETION &&
645 UGETW(np.wLength) == sizeof(np)) {
646 sc->sc_rx_bufsz = UGETDW(np.dwNtbInMaxSize);
647 sc->sc_tx_bufsz = UGETDW(np.dwNtbOutMaxSize);
648 } else
649 sc->sc_rx_bufsz = sc->sc_tx_bufsz = 8 * 1024;
650 }
651
652 Static int
653 umb_alloc_xfers(struct umb_softc *sc)
654 {
655 int err = 0;
656
657 if (!sc->sc_rx_xfer) {
658 err |= usbd_create_xfer(sc->sc_rx_pipe,
659 sc->sc_rx_bufsz,
660 0, 0, &sc->sc_rx_xfer);
661 }
662 if (!sc->sc_tx_xfer) {
663 err |= usbd_create_xfer(sc->sc_tx_pipe,
664 sc->sc_tx_bufsz,
665 0, 0, &sc->sc_tx_xfer);
666 }
667 if (err)
668 return err;
669
670 sc->sc_rx_buf = usbd_get_buffer(sc->sc_rx_xfer);
671 sc->sc_tx_buf = usbd_get_buffer(sc->sc_tx_xfer);
672
673 return 0;
674 }
675
676 Static void
677 umb_free_xfers(struct umb_softc *sc)
678 {
679 if (sc->sc_rx_xfer) {
680 /* implicit usbd_free_buffer() */
681 usbd_destroy_xfer(sc->sc_rx_xfer);
682 sc->sc_rx_xfer = NULL;
683 sc->sc_rx_buf = NULL;
684 }
685 if (sc->sc_tx_xfer) {
686 usbd_destroy_xfer(sc->sc_tx_xfer);
687 sc->sc_tx_xfer = NULL;
688 sc->sc_tx_buf = NULL;
689 }
690 if (sc->sc_tx_m) {
691 m_freem(sc->sc_tx_m);
692 sc->sc_tx_m = NULL;
693 }
694 }
695
696 Static int
697 umb_alloc_bulkpipes(struct umb_softc *sc)
698 {
699 struct ifnet *ifp = GET_IFP(sc);
700 int rv;
701
702 if (!(ifp->if_flags & IFF_RUNNING)) {
703 if ((rv = usbd_open_pipe(sc->sc_data_iface, sc->sc_rx_ep,
704 USBD_EXCLUSIVE_USE, &sc->sc_rx_pipe))) {
705 DPRINTFN(4, "usbd_open_pipe() failed (RX) %d\n", rv);
706 return 0;
707 }
708 if ((rv = usbd_open_pipe(sc->sc_data_iface, sc->sc_tx_ep,
709 USBD_EXCLUSIVE_USE, &sc->sc_tx_pipe))) {
710 DPRINTFN(4, "usbd_open_pipe() failed (TX) %d\n", rv);
711 return 0;
712 }
713
714 if ((rv = umb_alloc_xfers(sc)) != 0) {
715 DPRINTFN(4, "umb_alloc_xfers() failed %d\n", rv);
716 return 0;
717 }
718
719 ifp->if_flags |= IFF_RUNNING;
720 ifp->if_flags &= ~IFF_OACTIVE;
721 umb_rx(sc);
722 }
723 return 1;
724 }
725
726 Static void
727 umb_close_bulkpipes(struct umb_softc *sc)
728 {
729 struct ifnet *ifp = GET_IFP(sc);
730
731 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
732 ifp->if_timer = 0;
733 if (sc->sc_rx_pipe) {
734 usbd_close_pipe(sc->sc_rx_pipe);
735 sc->sc_rx_pipe = NULL;
736 }
737 if (sc->sc_tx_pipe) {
738 usbd_close_pipe(sc->sc_tx_pipe);
739 sc->sc_tx_pipe = NULL;
740 }
741 }
742
743 Static int
744 umb_ioctl(struct ifnet *ifp, u_long cmd, void *data)
745 {
746 struct umb_softc *sc = ifp->if_softc;
747 struct ifaddr *ifa = (struct ifaddr *)data;
748 struct ifreq *ifr = (struct ifreq *)data;
749 int s, error = 0;
750 struct umb_parameter mp;
751
752 if (sc->sc_dying)
753 return EIO;
754
755 s = splnet();
756 switch (cmd) {
757 case SIOCINITIFADDR:
758 ifp->if_flags |= IFF_UP;
759 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
760 switch (ifa->ifa_addr->sa_family) {
761 #ifdef INET
762 case AF_INET:
763 break;
764 #endif /* INET */
765 #ifdef INET6
766 case AF_INET6:
767 break;
768 #endif /* INET6 */
769 default:
770 error = EAFNOSUPPORT;
771 break;
772 }
773 ifa->ifa_rtrequest = p2p_rtrequest;
774 break;
775 case SIOCSIFFLAGS:
776 error = ifioctl_common(ifp, cmd, data);
777 if (error)
778 break;
779 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
780 break;
781 case SIOCGUMBINFO:
782 error = copyout(&sc->sc_info, ifr->ifr_data,
783 sizeof(sc->sc_info));
784 break;
785 case SIOCSUMBPARAM:
786 error = kauth_authorize_network(curlwp->l_cred,
787 KAUTH_NETWORK_INTERFACE,
788 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
789 NULL);
790 if (error)
791 break;
792
793 if ((error = copyin(ifr->ifr_data, &mp, sizeof(mp))) != 0)
794 break;
795
796 if ((error = umb_setpin(sc, mp.op, mp.is_puk, mp.pin, mp.pinlen,
797 mp.newpin, mp.newpinlen)) != 0)
798 break;
799
800 if (mp.apnlen < 0 || mp.apnlen > sizeof(sc->sc_info.apn)) {
801 error = EINVAL;
802 break;
803 }
804 sc->sc_roaming = mp.roaming ? 1 : 0;
805 memset(sc->sc_info.apn, 0, sizeof(sc->sc_info.apn));
806 memcpy(sc->sc_info.apn, mp.apn, mp.apnlen);
807 sc->sc_info.apnlen = mp.apnlen;
808 memset(sc->sc_info.username, 0, sizeof(sc->sc_info.username));
809 memcpy(sc->sc_info.username, mp.username, mp.usernamelen);
810 sc->sc_info.usernamelen = mp.usernamelen;
811 memset(sc->sc_info.password, 0, sizeof(sc->sc_info.password));
812 memcpy(sc->sc_info.password, mp.password, mp.passwordlen);
813 sc->sc_info.passwordlen = mp.passwordlen;
814 sc->sc_info.preferredclasses = mp.preferredclasses;
815 umb_setdataclass(sc);
816 break;
817 case SIOCGUMBPARAM:
818 memset(&mp, 0, sizeof(mp));
819 memcpy(mp.apn, sc->sc_info.apn, sc->sc_info.apnlen);
820 mp.apnlen = sc->sc_info.apnlen;
821 mp.roaming = sc->sc_roaming;
822 mp.preferredclasses = sc->sc_info.preferredclasses;
823 error = copyout(&mp, ifr->ifr_data, sizeof(mp));
824 break;
825 case SIOCSIFMTU:
826 /* Does this include the NCM headers and tail? */
827 if (ifr->ifr_mtu > ifp->if_mtu) {
828 error = EINVAL;
829 break;
830 }
831 ifp->if_mtu = ifr->ifr_mtu;
832 break;
833 case SIOCSIFADDR:
834 case SIOCAIFADDR:
835 case SIOCSIFDSTADDR:
836 case SIOCADDMULTI:
837 case SIOCDELMULTI:
838 break;
839 case SIOCGIFMEDIA:
840 error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
841 break;
842 default:
843 error = ifioctl_common(ifp, cmd, data);
844 break;
845 }
846 splx(s);
847 return error;
848 }
849
850 Static int
851 umb_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
852 const struct rtentry *rtp)
853 {
854 int error;
855
856 DPRINTFN(10, "%s: %s: enter\n",
857 device_xname(((struct umb_softc *)ifp->if_softc)->sc_dev),
858 __func__);
859
860 /*
861 * if the queueing discipline needs packet classification,
862 * do it now.
863 */
864 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
865
866 /*
867 * Queue message on interface, and start output if interface
868 * not yet active.
869 */
870 error = if_transmit_lock(ifp, m);
871
872 return error;
873 }
874
875 Static void
876 umb_input(struct ifnet *ifp, struct mbuf *m)
877 {
878 size_t pktlen = m->m_len;
879 int s;
880
881 if ((ifp->if_flags & IFF_UP) == 0) {
882 m_freem(m);
883 return;
884 }
885 if (pktlen < sizeof(struct ip)) {
886 ifp->if_ierrors++;
887 DPRINTFN(4, "%s: dropping short packet (len %zd)\n", __func__,
888 pktlen);
889 m_freem(m);
890 return;
891 }
892 s = splnet();
893 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
894 ifp->if_iqdrops++;
895 m_freem(m);
896 } else {
897 ifp->if_ipackets++;
898 ifp->if_ibytes += pktlen;
899 }
900 splx(s);
901 }
902
903 Static void
904 umb_start(struct ifnet *ifp)
905 {
906 struct umb_softc *sc = ifp->if_softc;
907 struct mbuf *m_head = NULL;
908
909 if (sc->sc_dying || (ifp->if_flags & IFF_OACTIVE))
910 return;
911
912 IFQ_POLL(&ifp->if_snd, m_head);
913 if (m_head == NULL)
914 return;
915
916 if (!umb_encap(sc, m_head)) {
917 ifp->if_flags |= IFF_OACTIVE;
918 return;
919 }
920 IFQ_DEQUEUE(&ifp->if_snd, m_head);
921
922 bpf_mtap(ifp, m_head, BPF_D_OUT);
923
924 ifp->if_flags |= IFF_OACTIVE;
925 ifp->if_timer = (2 * umb_xfer_tout) / 1000;
926 }
927
928 Static void
929 umb_watchdog(struct ifnet *ifp)
930 {
931 struct umb_softc *sc = ifp->if_softc;
932
933 if (sc->sc_dying)
934 return;
935
936 ifp->if_oerrors++;
937 printf("%s: watchdog timeout\n", DEVNAM(sc));
938 usbd_abort_pipe(sc->sc_tx_pipe);
939 return;
940 }
941
942 Static void
943 umb_statechg_timeout(void *arg)
944 {
945 struct umb_softc *sc = arg;
946
947 if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) {
948 /*
949 * Query the registration state until we're with the home
950 * network again.
951 */
952 umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_QRY, NULL, 0);
953 } else
954 printf("%s: state change timeout\n",DEVNAM(sc));
955 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
956 }
957
958 Static int
959 umb_mediachange(struct ifnet * ifp)
960 {
961 return 0;
962 }
963
964 Static void
965 umb_mediastatus(struct ifnet * ifp, struct ifmediareq * imr)
966 {
967 switch (ifp->if_link_state) {
968 case LINK_STATE_UP:
969 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
970 break;
971 case LINK_STATE_DOWN:
972 imr->ifm_status = IFM_AVALID;
973 break;
974 default:
975 imr->ifm_status = 0;
976 break;
977 }
978 }
979
980 Static void
981 umb_newstate(struct umb_softc *sc, enum umb_state newstate, int flags)
982 {
983 struct ifnet *ifp = GET_IFP(sc);
984
985 if (newstate == sc->sc_state)
986 return;
987 if (((flags & UMB_NS_DONT_DROP) && newstate < sc->sc_state) ||
988 ((flags & UMB_NS_DONT_RAISE) && newstate > sc->sc_state))
989 return;
990 if (ifp->if_flags & IFF_DEBUG)
991 log(LOG_DEBUG, "%s: state going %s from '%s' to '%s'\n",
992 DEVNAM(sc), newstate > sc->sc_state ? "up" : "down",
993 umb_istate(sc->sc_state), umb_istate(newstate));
994 sc->sc_state = newstate;
995 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
996 }
997
998 Static void
999 umb_state_task(void *arg)
1000 {
1001 struct umb_softc *sc = arg;
1002 struct ifnet *ifp = GET_IFP(sc);
1003 struct ifreq ifr;
1004 int s;
1005 int state;
1006
1007 s = splnet();
1008 if (ifp->if_flags & IFF_UP)
1009 umb_up(sc);
1010 else
1011 umb_down(sc, 0);
1012
1013 state = sc->sc_state == UMB_S_UP ? LINK_STATE_UP : LINK_STATE_DOWN;
1014 if (ifp->if_link_state != state) {
1015 if (ifp->if_flags & IFF_DEBUG)
1016 log(LOG_DEBUG, "%s: link state changed from %s to %s\n",
1017 DEVNAM(sc),
1018 (ifp->if_link_state == LINK_STATE_UP)
1019 ? "up" : "down",
1020 (state == LINK_STATE_UP) ? "up" : "down");
1021 ifp->if_link_state = state;
1022 if (state != LINK_STATE_UP) {
1023 /*
1024 * Purge any existing addresses
1025 */
1026 memset(sc->sc_info.ipv4dns, 0,
1027 sizeof(sc->sc_info.ipv4dns));
1028 if (in_control(NULL, SIOCGIFADDR, &ifr, ifp) == 0 &&
1029 satosin(&ifr.ifr_addr)->sin_addr.s_addr !=
1030 INADDR_ANY) {
1031 in_control(NULL, SIOCDIFADDR, &ifr, ifp);
1032 }
1033 }
1034 if_link_state_change(ifp, state);
1035 }
1036 splx(s);
1037 }
1038
1039 Static void
1040 umb_up(struct umb_softc *sc)
1041 {
1042 switch (sc->sc_state) {
1043 case UMB_S_DOWN:
1044 DPRINTF("%s: init: opening ...\n", DEVNAM(sc));
1045 umb_open(sc);
1046 break;
1047 case UMB_S_OPEN:
1048 if (sc->sc_flags & UMBFLG_FCC_AUTH_REQUIRED) {
1049 if (sc->sc_cid == -1) {
1050 DPRINTF("%s: init: allocating CID ...\n",
1051 DEVNAM(sc));
1052 umb_allocate_cid(sc);
1053 break;
1054 } else
1055 umb_newstate(sc, UMB_S_CID, UMB_NS_DONT_DROP);
1056 } else {
1057 DPRINTF("%s: init: turning radio on ...\n", DEVNAM(sc));
1058 umb_radio(sc, 1);
1059 break;
1060 }
1061 /*FALLTHROUGH*/
1062 case UMB_S_CID:
1063 DPRINTF("%s: init: sending FCC auth ...\n", DEVNAM(sc));
1064 umb_send_fcc_auth(sc);
1065 break;
1066 case UMB_S_RADIO:
1067 DPRINTF("%s: init: checking SIM state ...\n", DEVNAM(sc));
1068 umb_cmd(sc, MBIM_CID_SUBSCRIBER_READY_STATUS, MBIM_CMDOP_QRY,
1069 NULL, 0);
1070 break;
1071 case UMB_S_SIMREADY:
1072 DPRINTF("%s: init: attaching ...\n", DEVNAM(sc));
1073 umb_packet_service(sc, 1);
1074 break;
1075 case UMB_S_ATTACHED:
1076 sc->sc_tx_seq = 0;
1077 DPRINTF("%s: init: connecting ...\n", DEVNAM(sc));
1078 umb_connect(sc);
1079 break;
1080 case UMB_S_CONNECTED:
1081 DPRINTF("%s: init: getting IP config ...\n", DEVNAM(sc));
1082 umb_qry_ipconfig(sc);
1083 break;
1084 case UMB_S_UP:
1085 DPRINTF("%s: init: reached state UP\n", DEVNAM(sc));
1086 if (!umb_alloc_bulkpipes(sc)) {
1087 printf("%s: opening bulk pipes failed\n", DEVNAM(sc));
1088 umb_down(sc, 1);
1089 }
1090 break;
1091 }
1092 if (sc->sc_state < UMB_S_UP)
1093 callout_schedule(&sc->sc_statechg_timer,
1094 UMB_STATE_CHANGE_TIMEOUT * hz);
1095 else
1096 callout_stop(&sc->sc_statechg_timer);
1097 return;
1098 }
1099
1100 Static void
1101 umb_down(struct umb_softc *sc, int force)
1102 {
1103 umb_close_bulkpipes(sc);
1104 if (sc->sc_state < UMB_S_CONNECTED)
1105 umb_free_xfers(sc);
1106
1107 switch (sc->sc_state) {
1108 case UMB_S_UP:
1109 case UMB_S_CONNECTED:
1110 DPRINTF("%s: stop: disconnecting ...\n", DEVNAM(sc));
1111 umb_disconnect(sc);
1112 if (!force)
1113 break;
1114 /*FALLTHROUGH*/
1115 case UMB_S_ATTACHED:
1116 DPRINTF("%s: stop: detaching ...\n", DEVNAM(sc));
1117 umb_packet_service(sc, 0);
1118 if (!force)
1119 break;
1120 /*FALLTHROUGH*/
1121 case UMB_S_SIMREADY:
1122 case UMB_S_RADIO:
1123 DPRINTF("%s: stop: turning radio off ...\n", DEVNAM(sc));
1124 umb_radio(sc, 0);
1125 if (!force)
1126 break;
1127 /*FALLTHROUGH*/
1128 case UMB_S_CID:
1129 case UMB_S_OPEN:
1130 case UMB_S_DOWN:
1131 /* Do not close the device */
1132 DPRINTF("%s: stop: reached state DOWN\n", DEVNAM(sc));
1133 break;
1134 }
1135 if (force)
1136 sc->sc_state = UMB_S_OPEN;
1137
1138 if (sc->sc_state > UMB_S_OPEN)
1139 callout_schedule(&sc->sc_statechg_timer,
1140 UMB_STATE_CHANGE_TIMEOUT * hz);
1141 else
1142 callout_stop(&sc->sc_statechg_timer);
1143 }
1144
1145 Static void
1146 umb_get_response_task(void *arg)
1147 {
1148 struct umb_softc *sc = arg;
1149 int len;
1150 int s;
1151
1152 /*
1153 * Function is required to send on RESPONSE_AVAILABLE notification for
1154 * each encapsulated response that is to be processed by the host.
1155 * But of course, we can receive multiple notifications before the
1156 * response task is run.
1157 */
1158 s = splusb();
1159 while (sc->sc_nresp > 0) {
1160 --sc->sc_nresp;
1161 len = sc->sc_ctrl_len;
1162 if (umb_get_encap_response(sc, sc->sc_resp_buf, &len))
1163 umb_decode_response(sc, sc->sc_resp_buf, len);
1164 }
1165 splx(s);
1166 }
1167
1168 Static void
1169 umb_decode_response(struct umb_softc *sc, void *response, int len)
1170 {
1171 struct mbim_msghdr *hdr = response;
1172 struct mbim_fragmented_msg_hdr *fraghdr;
1173 uint32_t type;
1174
1175 DPRINTFN(3, "%s: got response: len %d\n", DEVNAM(sc), len);
1176 DDUMPN(4, response, len);
1177
1178 if (len < sizeof(*hdr) || le32toh(hdr->len) != len) {
1179 /*
1180 * We should probably cancel a transaction, but since the
1181 * message is too short, we cannot decode the transaction
1182 * id (tid) and hence don't know, whom to cancel. Must wait
1183 * for the timeout.
1184 */
1185 DPRINTF("%s: received short response (len %d)\n",
1186 DEVNAM(sc), len);
1187 return;
1188 }
1189
1190 /*
1191 * XXX FIXME: if message is fragmented, store it until last frag
1192 * is received and then re-assemble all fragments.
1193 */
1194 type = le32toh(hdr->type);
1195 switch (type) {
1196 case MBIM_INDICATE_STATUS_MSG:
1197 case MBIM_COMMAND_DONE:
1198 fraghdr = response;
1199 if (le32toh(fraghdr->frag.nfrag) != 1) {
1200 DPRINTF("%s: discarding fragmented messages\n",
1201 DEVNAM(sc));
1202 return;
1203 }
1204 break;
1205 default:
1206 break;
1207 }
1208
1209 DPRINTF("%s: <- rcv %s (tid %u)\n", DEVNAM(sc), umb_request2str(type),
1210 le32toh(hdr->tid));
1211 switch (type) {
1212 case MBIM_FUNCTION_ERROR_MSG:
1213 case MBIM_HOST_ERROR_MSG:
1214 {
1215 struct mbim_f2h_hosterr *e;
1216 int err;
1217
1218 if (len >= sizeof(*e)) {
1219 e = response;
1220 err = le32toh(e->err);
1221
1222 DPRINTF("%s: %s message, error %s (tid %u)\n",
1223 DEVNAM(sc), umb_request2str(type),
1224 umb_error2str(err), le32toh(hdr->tid));
1225 if (err == MBIM_ERROR_NOT_OPENED)
1226 umb_newstate(sc, UMB_S_DOWN, 0);
1227 }
1228 break;
1229 }
1230 case MBIM_INDICATE_STATUS_MSG:
1231 umb_handle_indicate_status_msg(sc, response, len);
1232 break;
1233 case MBIM_OPEN_DONE:
1234 umb_handle_opendone_msg(sc, response, len);
1235 break;
1236 case MBIM_CLOSE_DONE:
1237 umb_handle_closedone_msg(sc, response, len);
1238 break;
1239 case MBIM_COMMAND_DONE:
1240 umb_command_done(sc, response, len);
1241 break;
1242 default:
1243 DPRINTF("%s: discard messsage %s\n", DEVNAM(sc),
1244 umb_request2str(type));
1245 break;
1246 }
1247 }
1248
1249 Static void
1250 umb_handle_indicate_status_msg(struct umb_softc *sc, void *data, int len)
1251 {
1252 struct mbim_f2h_indicate_status *m = data;
1253 uint32_t infolen;
1254 uint32_t cid;
1255
1256 if (len < sizeof(*m)) {
1257 DPRINTF("%s: discard short %s messsage\n", DEVNAM(sc),
1258 umb_request2str(le32toh(m->hdr.type)));
1259 return;
1260 }
1261 if (memcmp(m->devid, umb_uuid_basic_connect, sizeof(m->devid))) {
1262 DPRINTF("%s: discard %s messsage for other UUID '%s'\n",
1263 DEVNAM(sc), umb_request2str(le32toh(m->hdr.type)),
1264 umb_uuid2str(m->devid));
1265 return;
1266 }
1267 infolen = le32toh(m->infolen);
1268 if (len < sizeof(*m) + infolen) {
1269 DPRINTF("%s: discard truncated %s messsage (want %d, got %d)\n",
1270 DEVNAM(sc), umb_request2str(le32toh(m->hdr.type)),
1271 (int)sizeof(*m) + infolen, len);
1272 return;
1273 }
1274
1275 cid = le32toh(m->cid);
1276 DPRINTF("%s: indicate %s status\n", DEVNAM(sc), umb_cid2str(cid));
1277 umb_decode_cid(sc, cid, m->info, infolen);
1278 }
1279
1280 Static void
1281 umb_handle_opendone_msg(struct umb_softc *sc, void *data, int len)
1282 {
1283 struct mbim_f2h_openclosedone *resp = data;
1284 struct ifnet *ifp = GET_IFP(sc);
1285 uint32_t status;
1286
1287 status = le32toh(resp->status);
1288 if (status == MBIM_STATUS_SUCCESS) {
1289 if (sc->sc_maxsessions == 0) {
1290 umb_cmd(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_QRY, NULL,
1291 0);
1292 umb_cmd(sc, MBIM_CID_PIN, MBIM_CMDOP_QRY, NULL, 0);
1293 umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_QRY,
1294 NULL, 0);
1295 }
1296 umb_newstate(sc, UMB_S_OPEN, UMB_NS_DONT_DROP);
1297 } else if (ifp->if_flags & IFF_DEBUG)
1298 log(LOG_ERR, "%s: open error: %s\n", DEVNAM(sc),
1299 umb_status2str(status));
1300 return;
1301 }
1302
1303 Static void
1304 umb_handle_closedone_msg(struct umb_softc *sc, void *data, int len)
1305 {
1306 struct mbim_f2h_openclosedone *resp = data;
1307 uint32_t status;
1308
1309 status = le32toh(resp->status);
1310 if (status == MBIM_STATUS_SUCCESS)
1311 umb_newstate(sc, UMB_S_DOWN, 0);
1312 else
1313 DPRINTF("%s: close error: %s\n", DEVNAM(sc),
1314 umb_status2str(status));
1315 return;
1316 }
1317
1318 static inline void
1319 umb_getinfobuf(char *in, int inlen, uint32_t offs, uint32_t sz,
1320 void *out, size_t outlen)
1321 {
1322 offs = le32toh(offs);
1323 sz = le32toh(sz);
1324 if (inlen >= offs + sz) {
1325 memset(out, 0, outlen);
1326 memcpy(out, in + offs, MIN(sz, outlen));
1327 }
1328 }
1329
1330 static inline int
1331 umb_padding(void *data, int len, size_t sz)
1332 {
1333 char *p = data;
1334 int np = 0;
1335
1336 while (len < sz && (len % 4) != 0) {
1337 *p++ = '\0';
1338 len++;
1339 np++;
1340 }
1341 return np;
1342 }
1343
1344 static inline int
1345 umb_addstr(void *buf, size_t bufsz, int *offs, void *str, int slen,
1346 uint32_t *offsmember, uint32_t *sizemember)
1347 {
1348 if (*offs + slen > bufsz)
1349 return 0;
1350
1351 *sizemember = htole32((uint32_t)slen);
1352 if (slen && str) {
1353 *offsmember = htole32((uint32_t)*offs);
1354 memcpy((char *)buf + *offs, str, slen);
1355 *offs += slen;
1356 *offs += umb_padding(buf, *offs, bufsz);
1357 } else
1358 *offsmember = htole32(0);
1359 return 1;
1360 }
1361
1362 static void
1363 umb_in_len2mask(struct in_addr *mask, int len)
1364 {
1365 int i;
1366 u_char *p;
1367
1368 p = (u_char *)mask;
1369 memset(mask, 0, sizeof(*mask));
1370 for (i = 0; i < len / 8; i++)
1371 p[i] = 0xff;
1372 if (len % 8)
1373 p[i] = (0xff00 >> (len % 8)) & 0xff;
1374 }
1375
1376 Static int
1377 umb_decode_register_state(struct umb_softc *sc, void *data, int len)
1378 {
1379 struct mbim_cid_registration_state_info *rs = data;
1380 struct ifnet *ifp = GET_IFP(sc);
1381
1382 if (len < sizeof(*rs))
1383 return 0;
1384 sc->sc_info.nwerror = le32toh(rs->nwerror);
1385 sc->sc_info.regstate = le32toh(rs->regstate);
1386 sc->sc_info.regmode = le32toh(rs->regmode);
1387 sc->sc_info.cellclass = le32toh(rs->curcellclass);
1388
1389 /* XXX should we remember the provider_id? */
1390 umb_getinfobuf(data, len, rs->provname_offs, rs->provname_size,
1391 sc->sc_info.provider, sizeof(sc->sc_info.provider));
1392 umb_getinfobuf(data, len, rs->roamingtxt_offs, rs->roamingtxt_size,
1393 sc->sc_info.roamingtxt, sizeof(sc->sc_info.roamingtxt));
1394
1395 DPRINTFN(2, "%s: %s, availclass 0x%x, class 0x%x, regmode %d\n",
1396 DEVNAM(sc), umb_regstate(sc->sc_info.regstate),
1397 le32toh(rs->availclasses), sc->sc_info.cellclass,
1398 sc->sc_info.regmode);
1399
1400 if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING &&
1401 !sc->sc_roaming &&
1402 sc->sc_info.activation == MBIM_ACTIVATION_STATE_ACTIVATED) {
1403 if (ifp->if_flags & IFF_DEBUG)
1404 log(LOG_INFO,
1405 "%s: disconnecting from roaming network\n",
1406 DEVNAM(sc));
1407 umb_disconnect(sc);
1408 }
1409 return 1;
1410 }
1411
1412 Static int
1413 umb_decode_devices_caps(struct umb_softc *sc, void *data, int len)
1414 {
1415 struct mbim_cid_device_caps *dc = data;
1416
1417 if (len < sizeof(*dc))
1418 return 0;
1419 sc->sc_maxsessions = le32toh(dc->max_sessions);
1420 sc->sc_info.supportedclasses = le32toh(dc->dataclass);
1421 umb_getinfobuf(data, len, dc->devid_offs, dc->devid_size,
1422 sc->sc_info.devid, sizeof(sc->sc_info.devid));
1423 umb_getinfobuf(data, len, dc->fwinfo_offs, dc->fwinfo_size,
1424 sc->sc_info.fwinfo, sizeof(sc->sc_info.fwinfo));
1425 umb_getinfobuf(data, len, dc->hwinfo_offs, dc->hwinfo_size,
1426 sc->sc_info.hwinfo, sizeof(sc->sc_info.hwinfo));
1427 DPRINTFN(2, "%s: max sessions %d, supported classes 0x%x\n",
1428 DEVNAM(sc), sc->sc_maxsessions, sc->sc_info.supportedclasses);
1429 return 1;
1430 }
1431
1432 Static int
1433 umb_decode_subscriber_status(struct umb_softc *sc, void *data, int len)
1434 {
1435 struct mbim_cid_subscriber_ready_info *si = data;
1436 struct ifnet *ifp = GET_IFP(sc);
1437 int npn;
1438
1439 if (len < sizeof(*si))
1440 return 0;
1441 sc->sc_info.sim_state = le32toh(si->ready);
1442
1443 umb_getinfobuf(data, len, si->sid_offs, si->sid_size,
1444 sc->sc_info.sid, sizeof(sc->sc_info.sid));
1445 umb_getinfobuf(data, len, si->icc_offs, si->icc_size,
1446 sc->sc_info.iccid, sizeof(sc->sc_info.iccid));
1447
1448 npn = le32toh(si->no_pn);
1449 if (npn > 0)
1450 umb_getinfobuf(data, len, si->pn[0].offs, si->pn[0].size,
1451 sc->sc_info.pn, sizeof(sc->sc_info.pn));
1452 else
1453 memset(sc->sc_info.pn, 0, sizeof(sc->sc_info.pn));
1454
1455 if (sc->sc_info.sim_state == MBIM_SIMSTATE_LOCKED)
1456 sc->sc_info.pin_state = UMB_PUK_REQUIRED;
1457 if (ifp->if_flags & IFF_DEBUG)
1458 log(LOG_INFO, "%s: SIM %s\n", DEVNAM(sc),
1459 umb_simstate(sc->sc_info.sim_state));
1460 if (sc->sc_info.sim_state == MBIM_SIMSTATE_INITIALIZED)
1461 umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_DROP);
1462 return 1;
1463 }
1464
1465 Static int
1466 umb_decode_radio_state(struct umb_softc *sc, void *data, int len)
1467 {
1468 struct mbim_cid_radio_state_info *rs = data;
1469 struct ifnet *ifp = GET_IFP(sc);
1470
1471 if (len < sizeof(*rs))
1472 return 0;
1473
1474 sc->sc_info.hw_radio_on =
1475 (le32toh(rs->hw_state) == MBIM_RADIO_STATE_ON) ? 1 : 0;
1476 sc->sc_info.sw_radio_on =
1477 (le32toh(rs->sw_state) == MBIM_RADIO_STATE_ON) ? 1 : 0;
1478 if (!sc->sc_info.hw_radio_on) {
1479 printf("%s: radio is disabled by hardware switch\n",
1480 DEVNAM(sc));
1481 /*
1482 * XXX do we need a time to poll the state of the rfkill switch
1483 * or will the device send an unsolicited notification
1484 * in case the state changes?
1485 */
1486 umb_newstate(sc, UMB_S_OPEN, 0);
1487 } else if (!sc->sc_info.sw_radio_on) {
1488 if (ifp->if_flags & IFF_DEBUG)
1489 log(LOG_INFO, "%s: radio is off\n", DEVNAM(sc));
1490 umb_newstate(sc, UMB_S_OPEN, 0);
1491 } else
1492 umb_newstate(sc, UMB_S_RADIO, UMB_NS_DONT_DROP);
1493 return 1;
1494 }
1495
1496 Static int
1497 umb_decode_pin(struct umb_softc *sc, void *data, int len)
1498 {
1499 struct mbim_cid_pin_info *pi = data;
1500 struct ifnet *ifp = GET_IFP(sc);
1501 uint32_t attempts_left;
1502
1503 if (len < sizeof(*pi))
1504 return 0;
1505
1506 attempts_left = le32toh(pi->remaining_attempts);
1507 if (attempts_left != 0xffffffff)
1508 sc->sc_info.pin_attempts_left = attempts_left;
1509
1510 switch (le32toh(pi->state)) {
1511 case MBIM_PIN_STATE_UNLOCKED:
1512 sc->sc_info.pin_state = UMB_PIN_UNLOCKED;
1513 break;
1514 case MBIM_PIN_STATE_LOCKED:
1515 switch (le32toh(pi->type)) {
1516 case MBIM_PIN_TYPE_PIN1:
1517 sc->sc_info.pin_state = UMB_PIN_REQUIRED;
1518 break;
1519 case MBIM_PIN_TYPE_PUK1:
1520 sc->sc_info.pin_state = UMB_PUK_REQUIRED;
1521 break;
1522 case MBIM_PIN_TYPE_PIN2:
1523 case MBIM_PIN_TYPE_PUK2:
1524 /* Assume that PIN1 was accepted */
1525 sc->sc_info.pin_state = UMB_PIN_UNLOCKED;
1526 break;
1527 }
1528 break;
1529 }
1530 if (ifp->if_flags & IFF_DEBUG)
1531 log(LOG_INFO, "%s: %s state %s (%d attempts left)\n",
1532 DEVNAM(sc), umb_pin_type(le32toh(pi->type)),
1533 (le32toh(pi->state) == MBIM_PIN_STATE_UNLOCKED) ?
1534 "unlocked" : "locked",
1535 le32toh(pi->remaining_attempts));
1536
1537 /*
1538 * In case the PIN was set after IFF_UP, retrigger the state machine
1539 */
1540 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
1541 return 1;
1542 }
1543
1544 Static int
1545 umb_decode_packet_service(struct umb_softc *sc, void *data, int len)
1546 {
1547 struct mbim_cid_packet_service_info *psi = data;
1548 int state, highestclass;
1549 uint64_t up_speed, down_speed;
1550 struct ifnet *ifp = GET_IFP(sc);
1551
1552 if (len < sizeof(*psi))
1553 return 0;
1554
1555 sc->sc_info.nwerror = le32toh(psi->nwerror);
1556 state = le32toh(psi->state);
1557 highestclass = le32toh(psi->highest_dataclass);
1558 up_speed = le64toh(psi->uplink_speed);
1559 down_speed = le64toh(psi->downlink_speed);
1560 if (sc->sc_info.packetstate != state ||
1561 sc->sc_info.uplink_speed != up_speed ||
1562 sc->sc_info.downlink_speed != down_speed) {
1563 if (ifp->if_flags & IFF_DEBUG) {
1564 log(LOG_INFO, "%s: packet service ", DEVNAM(sc));
1565 if (sc->sc_info.packetstate != state)
1566 addlog("changed from %s to ",
1567 umb_packet_state(sc->sc_info.packetstate));
1568 addlog("%s, class %s, speed: %" PRIu64 " up / %" PRIu64 " down\n",
1569 umb_packet_state(state),
1570 umb_dataclass(highestclass), up_speed, down_speed);
1571 }
1572 }
1573 sc->sc_info.packetstate = state;
1574 sc->sc_info.highestclass = highestclass;
1575 sc->sc_info.uplink_speed = up_speed;
1576 sc->sc_info.downlink_speed = down_speed;
1577
1578 if (sc->sc_info.regmode == MBIM_REGMODE_AUTOMATIC) {
1579 /*
1580 * For devices using automatic registration mode, just proceed,
1581 * once registration has completed.
1582 */
1583 if (ifp->if_flags & IFF_UP) {
1584 switch (sc->sc_info.regstate) {
1585 case MBIM_REGSTATE_HOME:
1586 case MBIM_REGSTATE_ROAMING:
1587 case MBIM_REGSTATE_PARTNER:
1588 umb_newstate(sc, UMB_S_ATTACHED,
1589 UMB_NS_DONT_DROP);
1590 break;
1591 default:
1592 break;
1593 }
1594 } else
1595 umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_RAISE);
1596 } else switch (sc->sc_info.packetstate) {
1597 case MBIM_PKTSERVICE_STATE_ATTACHED:
1598 umb_newstate(sc, UMB_S_ATTACHED, UMB_NS_DONT_DROP);
1599 break;
1600 case MBIM_PKTSERVICE_STATE_DETACHED:
1601 umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_RAISE);
1602 break;
1603 }
1604 return 1;
1605 }
1606
1607 Static int
1608 umb_decode_signal_state(struct umb_softc *sc, void *data, int len)
1609 {
1610 struct mbim_cid_signal_state *ss = data;
1611 struct ifnet *ifp = GET_IFP(sc);
1612 int rssi;
1613
1614 if (len < sizeof(*ss))
1615 return 0;
1616
1617 if (le32toh(ss->rssi) == 99)
1618 rssi = UMB_VALUE_UNKNOWN;
1619 else {
1620 rssi = -113 + 2 * le32toh(ss->rssi);
1621 if ((ifp->if_flags & IFF_DEBUG) && sc->sc_info.rssi != rssi &&
1622 sc->sc_state >= UMB_S_CONNECTED)
1623 log(LOG_INFO, "%s: rssi %d dBm\n", DEVNAM(sc), rssi);
1624 }
1625 sc->sc_info.rssi = rssi;
1626 sc->sc_info.ber = le32toh(ss->err_rate);
1627 if (sc->sc_info.ber == -99)
1628 sc->sc_info.ber = UMB_VALUE_UNKNOWN;
1629 return 1;
1630 }
1631
1632 Static int
1633 umb_decode_connect_info(struct umb_softc *sc, void *data, int len)
1634 {
1635 struct mbim_cid_connect_info *ci = data;
1636 struct ifnet *ifp = GET_IFP(sc);
1637 int act;
1638
1639 if (len < sizeof(*ci))
1640 return 0;
1641
1642 if (le32toh(ci->sessionid) != umb_session_id) {
1643 DPRINTF("%s: discard connection info for session %u\n",
1644 DEVNAM(sc), le32toh(ci->sessionid));
1645 return 1;
1646 }
1647 if (memcmp(ci->context, umb_uuid_context_internet,
1648 sizeof(ci->context))) {
1649 DPRINTF("%s: discard connection info for other context\n",
1650 DEVNAM(sc));
1651 return 1;
1652 }
1653 act = le32toh(ci->activation);
1654 if (sc->sc_info.activation != act) {
1655 if (ifp->if_flags & IFF_DEBUG)
1656 log(LOG_INFO, "%s: connection %s\n", DEVNAM(sc),
1657 umb_activation(act));
1658 if ((ifp->if_flags & IFF_DEBUG) &&
1659 le32toh(ci->iptype) != MBIM_CONTEXT_IPTYPE_DEFAULT &&
1660 le32toh(ci->iptype) != MBIM_CONTEXT_IPTYPE_IPV4)
1661 log(LOG_DEBUG, "%s: got iptype %d connection\n",
1662 DEVNAM(sc), le32toh(ci->iptype));
1663
1664 sc->sc_info.activation = act;
1665 sc->sc_info.nwerror = le32toh(ci->nwerror);
1666
1667 if (sc->sc_info.activation == MBIM_ACTIVATION_STATE_ACTIVATED)
1668 umb_newstate(sc, UMB_S_CONNECTED, UMB_NS_DONT_DROP);
1669 else if (sc->sc_info.activation ==
1670 MBIM_ACTIVATION_STATE_DEACTIVATED)
1671 umb_newstate(sc, UMB_S_ATTACHED, 0);
1672 /* else: other states are purely transitional */
1673 }
1674 return 1;
1675 }
1676
1677 Static int
1678 umb_decode_ip_configuration(struct umb_softc *sc, void *data, int len)
1679 {
1680 struct mbim_cid_ip_configuration_info *ic = data;
1681 struct ifnet *ifp = GET_IFP(sc);
1682 int s;
1683 uint32_t avail;
1684 uint32_t val;
1685 int n, i;
1686 int off;
1687 struct mbim_cid_ipv4_element ipv4elem;
1688 struct in_aliasreq ifra;
1689 struct sockaddr_in *sin;
1690 int state = -1;
1691 int rv;
1692
1693 if (len < sizeof(*ic))
1694 return 0;
1695 if (le32toh(ic->sessionid) != umb_session_id) {
1696 DPRINTF("%s: ignore IP configuration for session id %d\n",
1697 DEVNAM(sc), le32toh(ic->sessionid));
1698 return 0;
1699 }
1700 s = splnet();
1701
1702 /*
1703 * IPv4 configuation
1704 */
1705 avail = le32toh(ic->ipv4_available);
1706 if (avail & MBIM_IPCONF_HAS_ADDRINFO) {
1707 n = le32toh(ic->ipv4_naddr);
1708 off = le32toh(ic->ipv4_addroffs);
1709
1710 if (n == 0 || off + sizeof(ipv4elem) > len)
1711 goto done;
1712
1713 /* Only pick the first one */
1714 memcpy(&ipv4elem, (char *)data + off, sizeof(ipv4elem));
1715 ipv4elem.prefixlen = le32toh(ipv4elem.prefixlen);
1716
1717 memset(&ifra, 0, sizeof(ifra));
1718 sin = (struct sockaddr_in *)&ifra.ifra_addr;
1719 sin->sin_family = AF_INET;
1720 sin->sin_len = sizeof(ifra.ifra_addr);
1721 sin->sin_addr.s_addr = ipv4elem.addr;
1722
1723 sin = (struct sockaddr_in *)&ifra.ifra_dstaddr;
1724 sin->sin_family = AF_INET;
1725 sin->sin_len = sizeof(ifra.ifra_dstaddr);
1726 if (avail & MBIM_IPCONF_HAS_GWINFO) {
1727 off = le32toh(ic->ipv4_gwoffs);
1728 sin->sin_addr.s_addr = *((uint32_t *)((char *)data + off));
1729 }
1730
1731 sin = (struct sockaddr_in *)&ifra.ifra_mask;
1732 sin->sin_family = AF_INET;
1733 sin->sin_len = sizeof(ifra.ifra_mask);
1734 umb_in_len2mask(&sin->sin_addr, ipv4elem.prefixlen);
1735
1736 rv = in_control(NULL, SIOCAIFADDR, &ifra, ifp);
1737 if (rv == 0) {
1738 if (ifp->if_flags & IFF_DEBUG)
1739 log(LOG_INFO, "%s: IPv4 addr %s, mask %s, "
1740 "gateway %s\n", device_xname(sc->sc_dev),
1741 umb_ntop(sintosa(&ifra.ifra_addr)),
1742 umb_ntop(sintosa(&ifra.ifra_mask)),
1743 umb_ntop(sintosa(&ifra.ifra_dstaddr)));
1744 state = UMB_S_UP;
1745 } else
1746 printf("%s: unable to set IPv4 address, error %d\n",
1747 device_xname(sc->sc_dev), rv);
1748 }
1749
1750 memset(sc->sc_info.ipv4dns, 0, sizeof(sc->sc_info.ipv4dns));
1751 if (avail & MBIM_IPCONF_HAS_DNSINFO) {
1752 n = le32toh(ic->ipv4_ndnssrv);
1753 off = le32toh(ic->ipv4_dnssrvoffs);
1754 i = 0;
1755 while (n-- > 0) {
1756 if (off + sizeof(uint32_t) > len)
1757 break;
1758 val = *((uint32_t *)((char *)data + off));
1759 if (i < UMB_MAX_DNSSRV)
1760 sc->sc_info.ipv4dns[i++] = val;
1761 off += sizeof(uint32_t);
1762 }
1763 }
1764
1765 if ((avail & MBIM_IPCONF_HAS_MTUINFO)) {
1766 val = le32toh(ic->ipv4_mtu);
1767 if (ifp->if_mtu != val && val <= sc->sc_maxpktlen) {
1768 ifp->if_mtu = val;
1769 if (ifp->if_mtu > val)
1770 ifp->if_mtu = val;
1771 if (ifp->if_flags & IFF_DEBUG)
1772 log(LOG_INFO, "%s: MTU %d\n", DEVNAM(sc), val);
1773 }
1774 }
1775
1776 avail = le32toh(ic->ipv6_available);
1777 if ((ifp->if_flags & IFF_DEBUG) && avail & MBIM_IPCONF_HAS_ADDRINFO) {
1778 /* XXX FIXME: IPv6 configuration missing */
1779 log(LOG_INFO, "%s: ignoring IPv6 configuration\n", DEVNAM(sc));
1780 }
1781 if (state != -1)
1782 umb_newstate(sc, state, 0);
1783
1784 done:
1785 splx(s);
1786 return 1;
1787 }
1788
1789 Static void
1790 umb_rx(struct umb_softc *sc)
1791 {
1792 usbd_setup_xfer(sc->sc_rx_xfer, sc, sc->sc_rx_buf,
1793 sc->sc_rx_bufsz, USBD_SHORT_XFER_OK,
1794 USBD_NO_TIMEOUT, umb_rxeof);
1795 usbd_transfer(sc->sc_rx_xfer);
1796 }
1797
1798 Static void
1799 umb_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1800 {
1801 struct umb_softc *sc = priv;
1802 struct ifnet *ifp = GET_IFP(sc);
1803
1804 if (sc->sc_dying || !(ifp->if_flags & IFF_RUNNING))
1805 return;
1806
1807 if (status != USBD_NORMAL_COMPLETION) {
1808 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1809 return;
1810 DPRINTF("%s: rx error: %s\n", DEVNAM(sc), usbd_errstr(status));
1811 if (status == USBD_STALLED)
1812 usbd_clear_endpoint_stall_async(sc->sc_rx_pipe);
1813 if (++sc->sc_rx_nerr > 100) {
1814 log(LOG_ERR, "%s: too many rx errors, disabling\n",
1815 DEVNAM(sc));
1816 umb_activate(sc->sc_dev, DVACT_DEACTIVATE);
1817 }
1818 } else {
1819 sc->sc_rx_nerr = 0;
1820 umb_decap(sc, xfer);
1821 }
1822
1823 umb_rx(sc);
1824 return;
1825 }
1826
1827 Static int
1828 umb_encap(struct umb_softc *sc, struct mbuf *m)
1829 {
1830 struct ncm_header16 *hdr;
1831 struct ncm_pointer16 *ptr;
1832 usbd_status err;
1833 int len;
1834
1835 /* All size constraints have been validated by the caller! */
1836 hdr = (struct ncm_header16 *)sc->sc_tx_buf;
1837 ptr = (struct ncm_pointer16 *)(hdr + 1);
1838 USETDW(hdr->dwSignature, NCM_HDR16_SIG);
1839 USETW(hdr->wHeaderLength, sizeof(*hdr));
1840 USETW(hdr->wSequence, sc->sc_tx_seq);
1841 sc->sc_tx_seq++;
1842
1843 len = m->m_pkthdr.len;
1844
1845 USETDW(ptr->dwSignature, MBIM_NCM_NTH16_SIG(umb_session_id));
1846 USETW(ptr->wLength, sizeof(*ptr));
1847 USETW(ptr->wNextNdpIndex, 0);
1848 USETW(ptr->dgram[0].wDatagramIndex, MBIM_HDR16_LEN);
1849 USETW(ptr->dgram[0].wDatagramLen, len);
1850 USETW(ptr->dgram[1].wDatagramIndex, 0);
1851 USETW(ptr->dgram[1].wDatagramLen, 0);
1852
1853 KASSERT(len <= sc->sc_tx_bufsz - sizeof(*hdr) - sizeof(*ptr));
1854 m_copydata(m, 0, len, ptr + 1);
1855 sc->sc_tx_m = m;
1856 len += MBIM_HDR16_LEN;
1857 USETW(hdr->wBlockLength, len);
1858
1859 DPRINTFN(3, "%s: encap %d bytes\n", DEVNAM(sc), len);
1860 DDUMPN(5, sc->sc_tx_buf, len);
1861 usbd_setup_xfer(sc->sc_tx_xfer, sc, sc->sc_tx_buf, len,
1862 USBD_FORCE_SHORT_XFER, umb_xfer_tout, umb_txeof);
1863 err = usbd_transfer(sc->sc_tx_xfer);
1864 if (err != USBD_IN_PROGRESS) {
1865 DPRINTF("%s: start tx error: %s\n", DEVNAM(sc),
1866 usbd_errstr(err));
1867 return 0;
1868 }
1869 return 1;
1870 }
1871
1872 Static void
1873 umb_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1874 {
1875 struct umb_softc *sc = priv;
1876 struct ifnet *ifp = GET_IFP(sc);
1877 int s;
1878
1879 s = splnet();
1880 ifp->if_flags &= ~IFF_OACTIVE;
1881 ifp->if_timer = 0;
1882
1883 m_freem(sc->sc_tx_m);
1884 sc->sc_tx_m = NULL;
1885
1886 if (status != USBD_NORMAL_COMPLETION) {
1887 if (status != USBD_NOT_STARTED && status != USBD_CANCELLED) {
1888 ifp->if_oerrors++;
1889 DPRINTF("%s: tx error: %s\n", DEVNAM(sc),
1890 usbd_errstr(status));
1891 if (status == USBD_STALLED)
1892 usbd_clear_endpoint_stall_async(sc->sc_tx_pipe);
1893 }
1894 }
1895 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1896 umb_start(ifp);
1897
1898 splx(s);
1899 }
1900
1901 Static void
1902 umb_decap(struct umb_softc *sc, struct usbd_xfer *xfer)
1903 {
1904 struct ifnet *ifp = GET_IFP(sc);
1905 int s;
1906 char *buf;
1907 uint32_t len;
1908 char *dp;
1909 struct ncm_header16 *hdr16;
1910 struct ncm_header32 *hdr32;
1911 struct ncm_pointer16 *ptr16;
1912 struct ncm_pointer16_dgram *dgram16;
1913 struct ncm_pointer32_dgram *dgram32;
1914 uint32_t hsig, psig;
1915 int hlen, blen;
1916 int ptrlen, ptroff, dgentryoff;
1917 uint32_t doff, dlen;
1918 struct mbuf *m;
1919
1920 usbd_get_xfer_status(xfer, NULL, (void **)&buf, &len, NULL);
1921 DPRINTFN(4, "%s: recv %d bytes\n", DEVNAM(sc), len);
1922 DDUMPN(5, buf, len);
1923 s = splnet();
1924 if (len < sizeof(*hdr16))
1925 goto toosmall;
1926
1927 hdr16 = (struct ncm_header16 *)buf;
1928 hsig = UGETDW(hdr16->dwSignature);
1929 hlen = UGETW(hdr16->wHeaderLength);
1930 if (len < hlen)
1931 goto toosmall;
1932 if (len > sc->sc_rx_bufsz) {
1933 DPRINTF("%s: packet too large (%d)\n", DEVNAM(sc), len);
1934 goto fail;
1935 }
1936 switch (hsig) {
1937 case NCM_HDR16_SIG:
1938 blen = UGETW(hdr16->wBlockLength);
1939 ptroff = UGETW(hdr16->wNdpIndex);
1940 if (hlen != sizeof(*hdr16)) {
1941 DPRINTF("%s: bad header len %d for NTH16 (exp %zu)\n",
1942 DEVNAM(sc), hlen, sizeof(*hdr16));
1943 goto fail;
1944 }
1945 break;
1946 case NCM_HDR32_SIG:
1947 hdr32 = (struct ncm_header32 *)hdr16;
1948 blen = UGETDW(hdr32->dwBlockLength);
1949 ptroff = UGETDW(hdr32->dwNdpIndex);
1950 if (hlen != sizeof(*hdr32)) {
1951 DPRINTF("%s: bad header len %d for NTH32 (exp %zu)\n",
1952 DEVNAM(sc), hlen, sizeof(*hdr32));
1953 goto fail;
1954 }
1955 break;
1956 default:
1957 DPRINTF("%s: unsupported NCM header signature (0x%08x)\n",
1958 DEVNAM(sc), hsig);
1959 goto fail;
1960 }
1961 if (len < blen) {
1962 DPRINTF("%s: bad NTB len (%d) for %d bytes of data\n",
1963 DEVNAM(sc), blen, len);
1964 goto fail;
1965 }
1966
1967 ptr16 = (struct ncm_pointer16 *)(buf + ptroff);
1968 psig = UGETDW(ptr16->dwSignature);
1969 ptrlen = UGETW(ptr16->wLength);
1970 if (len < ptrlen + ptroff)
1971 goto toosmall;
1972 if (!MBIM_NCM_NTH16_ISISG(psig) && !MBIM_NCM_NTH32_ISISG(psig)) {
1973 DPRINTF("%s: unsupported NCM pointer signature (0x%08x)\n",
1974 DEVNAM(sc), psig);
1975 goto fail;
1976 }
1977
1978 switch (hsig) {
1979 case NCM_HDR16_SIG:
1980 dgentryoff = offsetof(struct ncm_pointer16, dgram);
1981 break;
1982 case NCM_HDR32_SIG:
1983 dgentryoff = offsetof(struct ncm_pointer32, dgram);
1984 break;
1985 default:
1986 goto fail;
1987 }
1988
1989 while (dgentryoff < ptrlen) {
1990 switch (hsig) {
1991 case NCM_HDR16_SIG:
1992 if (ptroff + dgentryoff < sizeof(*dgram16))
1993 goto done;
1994 dgram16 = (struct ncm_pointer16_dgram *)
1995 (buf + ptroff + dgentryoff);
1996 dgentryoff += sizeof(*dgram16);
1997 dlen = UGETW(dgram16->wDatagramLen);
1998 doff = UGETW(dgram16->wDatagramIndex);
1999 break;
2000 case NCM_HDR32_SIG:
2001 if (ptroff + dgentryoff < sizeof(*dgram32))
2002 goto done;
2003 dgram32 = (struct ncm_pointer32_dgram *)
2004 (buf + ptroff + dgentryoff);
2005 dgentryoff += sizeof(*dgram32);
2006 dlen = UGETDW(dgram32->dwDatagramLen);
2007 doff = UGETDW(dgram32->dwDatagramIndex);
2008 break;
2009 default:
2010 ifp->if_ierrors++;
2011 goto done;
2012 }
2013
2014 /* Terminating zero entry */
2015 if (dlen == 0 || doff == 0)
2016 break;
2017 if (len < dlen + doff) {
2018 /* Skip giant datagram but continue processing */
2019 DPRINTF("%s: datagram too large (%d @ off %d)\n",
2020 DEVNAM(sc), dlen, doff);
2021 continue;
2022 }
2023
2024 dp = buf + doff;
2025 DPRINTFN(3, "%s: decap %d bytes\n", DEVNAM(sc), dlen);
2026 m = m_devget(dp, dlen, 0, ifp, NULL);
2027 if (m == NULL) {
2028 ifp->if_iqdrops++;
2029 continue;
2030 }
2031
2032 if_percpuq_enqueue((ifp)->if_percpuq, (m));
2033 }
2034 done:
2035 splx(s);
2036 return;
2037 toosmall:
2038 DPRINTF("%s: packet too small (%d)\n", DEVNAM(sc), len);
2039 fail:
2040 ifp->if_ierrors++;
2041 splx(s);
2042 }
2043
2044 Static usbd_status
2045 umb_send_encap_command(struct umb_softc *sc, void *data, int len)
2046 {
2047 struct usbd_xfer *xfer;
2048 usb_device_request_t req;
2049 char *buf;
2050
2051 if (len > sc->sc_ctrl_len)
2052 return USBD_INVAL;
2053
2054 if (usbd_create_xfer(sc->sc_udev->ud_pipe0, len, 0, 0, &xfer) != 0)
2055 return USBD_NOMEM;
2056 buf = usbd_get_buffer(xfer);
2057 memcpy(buf, data, len);
2058
2059 /* XXX FIXME: if (total len > sc->sc_ctrl_len) => must fragment */
2060 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2061 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
2062 USETW(req.wValue, 0);
2063 USETW(req.wIndex, sc->sc_ctrl_ifaceno);
2064 USETW(req.wLength, len);
2065 DELAY(umb_delay);
2066 return usbd_request_async(sc->sc_udev, xfer, &req, NULL, NULL);
2067 }
2068
2069 Static int
2070 umb_get_encap_response(struct umb_softc *sc, void *buf, int *len)
2071 {
2072 usb_device_request_t req;
2073 usbd_status err;
2074
2075 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2076 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
2077 USETW(req.wValue, 0);
2078 USETW(req.wIndex, sc->sc_ctrl_ifaceno);
2079 USETW(req.wLength, *len);
2080 /* XXX FIXME: re-assemble fragments */
2081
2082 DELAY(umb_delay);
2083 err = usbd_do_request_flags(sc->sc_udev, &req, buf, USBD_SHORT_XFER_OK,
2084 len, umb_xfer_tout);
2085 if (err == USBD_NORMAL_COMPLETION)
2086 return 1;
2087 DPRINTF("%s: ctrl recv: %s\n", DEVNAM(sc), usbd_errstr(err));
2088 return 0;
2089 }
2090
2091 Static void
2092 umb_ctrl_msg(struct umb_softc *sc, uint32_t req, void *data, int len)
2093 {
2094 struct ifnet *ifp = GET_IFP(sc);
2095 uint32_t tid;
2096 struct mbim_msghdr *hdr = data;
2097 usbd_status err;
2098 int s;
2099
2100 if (sc->sc_dying)
2101 return;
2102 if (len < sizeof(*hdr))
2103 return;
2104 tid = ++sc->sc_tid;
2105
2106 hdr->type = htole32(req);
2107 hdr->len = htole32(len);
2108 hdr->tid = htole32(tid);
2109
2110 #ifdef UMB_DEBUG
2111 if (umb_debug) {
2112 const char *op, *str;
2113 if (req == MBIM_COMMAND_MSG) {
2114 struct mbim_h2f_cmd *c = data;
2115 if (le32toh(c->op) == MBIM_CMDOP_SET)
2116 op = "set";
2117 else
2118 op = "qry";
2119 str = umb_cid2str(le32toh(c->cid));
2120 } else {
2121 op = "snd";
2122 str = umb_request2str(req);
2123 }
2124 DPRINTF("%s: -> %s %s (tid %u)\n", DEVNAM(sc), op, str, tid);
2125 }
2126 #endif
2127 s = splusb();
2128 err = umb_send_encap_command(sc, data, len);
2129 splx(s);
2130 if (err != USBD_NORMAL_COMPLETION) {
2131 if (ifp->if_flags & IFF_DEBUG)
2132 log(LOG_ERR, "%s: send %s msg (tid %u) failed: %s\n",
2133 DEVNAM(sc), umb_request2str(req), tid,
2134 usbd_errstr(err));
2135
2136 /* will affect other transactions, too */
2137 usbd_abort_pipe(sc->sc_udev->ud_pipe0);
2138 } else {
2139 DPRINTFN(2, "%s: sent %s (tid %u)\n", DEVNAM(sc),
2140 umb_request2str(req), tid);
2141 DDUMPN(3, data, len);
2142 }
2143 return;
2144 }
2145
2146 Static void
2147 umb_open(struct umb_softc *sc)
2148 {
2149 struct mbim_h2f_openmsg msg;
2150
2151 memset(&msg, 0, sizeof(msg));
2152 msg.maxlen = htole32(sc->sc_ctrl_len);
2153 umb_ctrl_msg(sc, MBIM_OPEN_MSG, &msg, sizeof(msg));
2154 return;
2155 }
2156
2157 Static void
2158 umb_close(struct umb_softc *sc)
2159 {
2160 struct mbim_h2f_closemsg msg;
2161
2162 memset(&msg, 0, sizeof(msg));
2163 umb_ctrl_msg(sc, MBIM_CLOSE_MSG, &msg, sizeof(msg));
2164 }
2165
2166 Static int
2167 umb_setpin(struct umb_softc *sc, int op, int is_puk, void *pin, int pinlen,
2168 void *newpin, int newpinlen)
2169 {
2170 struct mbim_cid_pin cp;
2171 int off;
2172
2173 if (pinlen == 0)
2174 return 0;
2175 if (pinlen < 0 || pinlen > MBIM_PIN_MAXLEN ||
2176 newpinlen < 0 || newpinlen > MBIM_PIN_MAXLEN ||
2177 op < 0 || op > MBIM_PIN_OP_CHANGE ||
2178 (is_puk && op != MBIM_PIN_OP_ENTER))
2179 return EINVAL;
2180
2181 memset(&cp, 0, sizeof(cp));
2182 cp.type = htole32(is_puk ? MBIM_PIN_TYPE_PUK1 : MBIM_PIN_TYPE_PIN1);
2183
2184 off = offsetof(struct mbim_cid_pin, data);
2185 if (!umb_addstr(&cp, sizeof(cp), &off, pin, pinlen,
2186 &cp.pin_offs, &cp.pin_size))
2187 return EINVAL;
2188
2189 cp.op = htole32(op);
2190 if (newpinlen) {
2191 if (!umb_addstr(&cp, sizeof(cp), &off, newpin, newpinlen,
2192 &cp.newpin_offs, &cp.newpin_size))
2193 return EINVAL;
2194 } else {
2195 if ((op == MBIM_PIN_OP_CHANGE) || is_puk)
2196 return EINVAL;
2197 if (!umb_addstr(&cp, sizeof(cp), &off, NULL, 0,
2198 &cp.newpin_offs, &cp.newpin_size))
2199 return EINVAL;
2200 }
2201 umb_cmd(sc, MBIM_CID_PIN, MBIM_CMDOP_SET, &cp, off);
2202 return 0;
2203 }
2204
2205 Static void
2206 umb_setdataclass(struct umb_softc *sc)
2207 {
2208 struct mbim_cid_registration_state rs;
2209 uint32_t classes;
2210
2211 if (sc->sc_info.supportedclasses == MBIM_DATACLASS_NONE)
2212 return;
2213
2214 memset(&rs, 0, sizeof(rs));
2215 rs.regaction = htole32(MBIM_REGACTION_AUTOMATIC);
2216 classes = sc->sc_info.supportedclasses;
2217 if (sc->sc_info.preferredclasses != MBIM_DATACLASS_NONE)
2218 classes &= sc->sc_info.preferredclasses;
2219 rs.data_class = htole32(classes);
2220 umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_SET, &rs, sizeof(rs));
2221 }
2222
2223 Static void
2224 umb_radio(struct umb_softc *sc, int on)
2225 {
2226 struct mbim_cid_radio_state s;
2227
2228 DPRINTF("%s: set radio %s\n", DEVNAM(sc), on ? "on" : "off");
2229 memset(&s, 0, sizeof(s));
2230 s.state = htole32(on ? MBIM_RADIO_STATE_ON : MBIM_RADIO_STATE_OFF);
2231 umb_cmd(sc, MBIM_CID_RADIO_STATE, MBIM_CMDOP_SET, &s, sizeof(s));
2232 }
2233
2234 Static void
2235 umb_allocate_cid(struct umb_softc *sc)
2236 {
2237 umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET,
2238 umb_qmi_alloc_cid, sizeof(umb_qmi_alloc_cid), umb_uuid_qmi_mbim);
2239 }
2240
2241 Static void
2242 umb_send_fcc_auth(struct umb_softc *sc)
2243 {
2244 uint8_t fccauth[sizeof(umb_qmi_fcc_auth)];
2245
2246 if (sc->sc_cid == -1) {
2247 DPRINTF("%s: missing CID, cannot send FCC auth\n", DEVNAM(sc));
2248 umb_allocate_cid(sc);
2249 return;
2250 }
2251 memcpy(fccauth, umb_qmi_fcc_auth, sizeof(fccauth));
2252 fccauth[UMB_QMI_CID_OFFS] = sc->sc_cid;
2253 umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET,
2254 fccauth, sizeof(fccauth), umb_uuid_qmi_mbim);
2255 }
2256
2257 Static void
2258 umb_packet_service(struct umb_softc *sc, int attach)
2259 {
2260 struct mbim_cid_packet_service s;
2261
2262 DPRINTF("%s: %s packet service\n", DEVNAM(sc),
2263 attach ? "attach" : "detach");
2264 memset(&s, 0, sizeof(s));
2265 s.action = htole32(attach ?
2266 MBIM_PKTSERVICE_ACTION_ATTACH : MBIM_PKTSERVICE_ACTION_DETACH);
2267 umb_cmd(sc, MBIM_CID_PACKET_SERVICE, MBIM_CMDOP_SET, &s, sizeof(s));
2268 }
2269
2270 Static void
2271 umb_connect(struct umb_softc *sc)
2272 {
2273 struct ifnet *ifp = GET_IFP(sc);
2274
2275 if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) {
2276 log(LOG_INFO, "%s: connection disabled in roaming network\n",
2277 DEVNAM(sc));
2278 return;
2279 }
2280 if (ifp->if_flags & IFF_DEBUG)
2281 log(LOG_DEBUG, "%s: connecting ...\n", DEVNAM(sc));
2282 umb_send_connect(sc, MBIM_CONNECT_ACTIVATE);
2283 }
2284
2285 Static void
2286 umb_disconnect(struct umb_softc *sc)
2287 {
2288 struct ifnet *ifp = GET_IFP(sc);
2289
2290 if (ifp->if_flags & IFF_DEBUG)
2291 log(LOG_DEBUG, "%s: disconnecting ...\n", DEVNAM(sc));
2292 umb_send_connect(sc, MBIM_CONNECT_DEACTIVATE);
2293 }
2294
2295 Static void
2296 umb_send_connect(struct umb_softc *sc, int command)
2297 {
2298 struct mbim_cid_connect *c;
2299 int off;
2300
2301 /* Too large or the stack */
2302 c = kmem_zalloc(sizeof(*c), KM_SLEEP);
2303 c->sessionid = htole32(umb_session_id);
2304 c->command = htole32(command);
2305 off = offsetof(struct mbim_cid_connect, data);
2306 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.apn,
2307 sc->sc_info.apnlen, &c->access_offs, &c->access_size))
2308 goto done;
2309 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.username,
2310 sc->sc_info.usernamelen, &c->user_offs, &c->user_size))
2311 goto done;
2312 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.password,
2313 sc->sc_info.passwordlen, &c->passwd_offs, &c->passwd_size))
2314 goto done;
2315 c->authprot = htole32(MBIM_AUTHPROT_NONE);
2316 c->compression = htole32(MBIM_COMPRESSION_NONE);
2317 c->iptype = htole32(MBIM_CONTEXT_IPTYPE_IPV4);
2318 memcpy(c->context, umb_uuid_context_internet, sizeof(c->context));
2319 umb_cmd(sc, MBIM_CID_CONNECT, MBIM_CMDOP_SET, c, off);
2320 done:
2321 kmem_free(c, sizeof(*c));
2322 return;
2323 }
2324
2325 Static void
2326 umb_qry_ipconfig(struct umb_softc *sc)
2327 {
2328 struct mbim_cid_ip_configuration_info ipc;
2329
2330 memset(&ipc, 0, sizeof(ipc));
2331 ipc.sessionid = htole32(umb_session_id);
2332 umb_cmd(sc, MBIM_CID_IP_CONFIGURATION, MBIM_CMDOP_QRY,
2333 &ipc, sizeof(ipc));
2334 }
2335
2336 Static void
2337 umb_cmd(struct umb_softc *sc, int cid, int op, const void *data, int len)
2338 {
2339 umb_cmd1(sc, cid, op, data, len, umb_uuid_basic_connect);
2340 }
2341
2342 Static void
2343 umb_cmd1(struct umb_softc *sc, int cid, int op, const void *data, int len,
2344 uint8_t *uuid)
2345 {
2346 struct mbim_h2f_cmd *cmd;
2347 int totlen;
2348
2349 /* XXX FIXME support sending fragments */
2350 if (sizeof(*cmd) + len > sc->sc_ctrl_len) {
2351 DPRINTF("%s: set %s msg too long: cannot send\n",
2352 DEVNAM(sc), umb_cid2str(cid));
2353 return;
2354 }
2355 cmd = sc->sc_ctrl_msg;
2356 memset(cmd, 0, sizeof(*cmd));
2357 cmd->frag.nfrag = htole32(1);
2358 memcpy(cmd->devid, uuid, sizeof(cmd->devid));
2359 cmd->cid = htole32(cid);
2360 cmd->op = htole32(op);
2361 cmd->infolen = htole32(len);
2362 totlen = sizeof(*cmd);
2363 if (len > 0) {
2364 memcpy(cmd + 1, data, len);
2365 totlen += len;
2366 }
2367 umb_ctrl_msg(sc, MBIM_COMMAND_MSG, cmd, totlen);
2368 }
2369
2370 Static void
2371 umb_command_done(struct umb_softc *sc, void *data, int len)
2372 {
2373 struct mbim_f2h_cmddone *cmd = data;
2374 struct ifnet *ifp = GET_IFP(sc);
2375 uint32_t status;
2376 uint32_t cid;
2377 uint32_t infolen;
2378 int qmimsg = 0;
2379
2380 if (len < sizeof(*cmd)) {
2381 DPRINTF("%s: discard short %s messsage\n", DEVNAM(sc),
2382 umb_request2str(le32toh(cmd->hdr.type)));
2383 return;
2384 }
2385 cid = le32toh(cmd->cid);
2386 if (memcmp(cmd->devid, umb_uuid_basic_connect, sizeof(cmd->devid))) {
2387 if (memcmp(cmd->devid, umb_uuid_qmi_mbim,
2388 sizeof(cmd->devid))) {
2389 DPRINTF("%s: discard %s messsage for other UUID '%s'\n",
2390 DEVNAM(sc), umb_request2str(le32toh(cmd->hdr.type)),
2391 umb_uuid2str(cmd->devid));
2392 return;
2393 } else
2394 qmimsg = 1;
2395 }
2396
2397 status = le32toh(cmd->status);
2398 switch (status) {
2399 case MBIM_STATUS_SUCCESS:
2400 break;
2401 case MBIM_STATUS_NOT_INITIALIZED:
2402 if (ifp->if_flags & IFF_DEBUG)
2403 log(LOG_ERR, "%s: SIM not initialized (PIN missing)\n",
2404 DEVNAM(sc));
2405 return;
2406 case MBIM_STATUS_PIN_REQUIRED:
2407 sc->sc_info.pin_state = UMB_PIN_REQUIRED;
2408 /*FALLTHROUGH*/
2409 default:
2410 if (ifp->if_flags & IFF_DEBUG)
2411 log(LOG_ERR, "%s: set/qry %s failed: %s\n", DEVNAM(sc),
2412 umb_cid2str(cid), umb_status2str(status));
2413 return;
2414 }
2415
2416 infolen = le32toh(cmd->infolen);
2417 if (len < sizeof(*cmd) + infolen) {
2418 DPRINTF("%s: discard truncated %s messsage (want %d, got %d)\n",
2419 DEVNAM(sc), umb_cid2str(cid),
2420 (int)sizeof(*cmd) + infolen, len);
2421 return;
2422 }
2423 if (qmimsg) {
2424 if (sc->sc_flags & UMBFLG_FCC_AUTH_REQUIRED)
2425 umb_decode_qmi(sc, cmd->info, infolen);
2426 } else {
2427 DPRINTFN(2, "%s: set/qry %s done\n", DEVNAM(sc),
2428 umb_cid2str(cid));
2429 umb_decode_cid(sc, cid, cmd->info, infolen);
2430 }
2431 }
2432
2433 Static void
2434 umb_decode_cid(struct umb_softc *sc, uint32_t cid, void *data, int len)
2435 {
2436 int ok = 1;
2437
2438 switch (cid) {
2439 case MBIM_CID_DEVICE_CAPS:
2440 ok = umb_decode_devices_caps(sc, data, len);
2441 break;
2442 case MBIM_CID_SUBSCRIBER_READY_STATUS:
2443 ok = umb_decode_subscriber_status(sc, data, len);
2444 break;
2445 case MBIM_CID_RADIO_STATE:
2446 ok = umb_decode_radio_state(sc, data, len);
2447 break;
2448 case MBIM_CID_PIN:
2449 ok = umb_decode_pin(sc, data, len);
2450 break;
2451 case MBIM_CID_REGISTER_STATE:
2452 ok = umb_decode_register_state(sc, data, len);
2453 break;
2454 case MBIM_CID_PACKET_SERVICE:
2455 ok = umb_decode_packet_service(sc, data, len);
2456 break;
2457 case MBIM_CID_SIGNAL_STATE:
2458 ok = umb_decode_signal_state(sc, data, len);
2459 break;
2460 case MBIM_CID_CONNECT:
2461 ok = umb_decode_connect_info(sc, data, len);
2462 break;
2463 case MBIM_CID_IP_CONFIGURATION:
2464 ok = umb_decode_ip_configuration(sc, data, len);
2465 break;
2466 default:
2467 /*
2468 * Note: the above list is incomplete and only contains
2469 * mandatory CIDs from the BASIC_CONNECT set.
2470 * So alternate values are not unusual.
2471 */
2472 DPRINTFN(4, "%s: ignore %s\n", DEVNAM(sc), umb_cid2str(cid));
2473 break;
2474 }
2475 if (!ok)
2476 DPRINTF("%s: discard %s with bad info length %d\n",
2477 DEVNAM(sc), umb_cid2str(cid), len);
2478 return;
2479 }
2480
2481 Static void
2482 umb_decode_qmi(struct umb_softc *sc, uint8_t *data, int len)
2483 {
2484 uint8_t srv;
2485 uint16_t msg, tlvlen;
2486 uint32_t val;
2487
2488 #define UMB_QMI_QMUXLEN 6
2489 if (len < UMB_QMI_QMUXLEN)
2490 goto tooshort;
2491
2492 srv = data[4];
2493 data += UMB_QMI_QMUXLEN;
2494 len -= UMB_QMI_QMUXLEN;
2495
2496 #define UMB_GET16(p) ((uint16_t)*p | (uint16_t)*(p + 1) << 8)
2497 #define UMB_GET32(p) ((uint32_t)*p | (uint32_t)*(p + 1) << 8 | \
2498 (uint32_t)*(p + 2) << 16 |(uint32_t)*(p + 3) << 24)
2499 switch (srv) {
2500 case 0: /* ctl */
2501 #define UMB_QMI_CTLLEN 6
2502 if (len < UMB_QMI_CTLLEN)
2503 goto tooshort;
2504 msg = UMB_GET16(&data[2]);
2505 tlvlen = UMB_GET16(&data[4]);
2506 data += UMB_QMI_CTLLEN;
2507 len -= UMB_QMI_CTLLEN;
2508 break;
2509 case 2: /* dms */
2510 #define UMB_QMI_DMSLEN 7
2511 if (len < UMB_QMI_DMSLEN)
2512 goto tooshort;
2513 msg = UMB_GET16(&data[3]);
2514 tlvlen = UMB_GET16(&data[5]);
2515 data += UMB_QMI_DMSLEN;
2516 len -= UMB_QMI_DMSLEN;
2517 break;
2518 default:
2519 DPRINTF("%s: discard QMI message for unknown service type %d\n",
2520 DEVNAM(sc), srv);
2521 return;
2522 }
2523
2524 if (len < tlvlen)
2525 goto tooshort;
2526
2527 #define UMB_QMI_TLVLEN 3
2528 while (len > 0) {
2529 if (len < UMB_QMI_TLVLEN)
2530 goto tooshort;
2531 tlvlen = UMB_GET16(&data[1]);
2532 if (len < UMB_QMI_TLVLEN + tlvlen)
2533 goto tooshort;
2534 switch (data[0]) {
2535 case 1: /* allocation info */
2536 if (msg == 0x0022) { /* Allocate CID */
2537 if (tlvlen != 2 || data[3] != 2) /* dms */
2538 break;
2539 sc->sc_cid = data[4];
2540 DPRINTF("%s: QMI CID %d allocated\n",
2541 DEVNAM(sc), sc->sc_cid);
2542 umb_newstate(sc, UMB_S_CID, UMB_NS_DONT_DROP);
2543 }
2544 break;
2545 case 2: /* response */
2546 if (tlvlen != sizeof(val))
2547 break;
2548 val = UMB_GET32(&data[3]);
2549 switch (msg) {
2550 case 0x0022: /* Allocate CID */
2551 if (val != 0) {
2552 log(LOG_ERR, "%s: allocation of QMI CID"
2553 " failed, error 0x%x\n", DEVNAM(sc),
2554 val);
2555 /* XXX how to proceed? */
2556 return;
2557 }
2558 break;
2559 case 0x555f: /* Send FCC Authentication */
2560 if (val == 0)
2561 log(LOG_INFO, "%s: send FCC "
2562 "Authentication succeeded\n",
2563 DEVNAM(sc));
2564 else if (val == 0x001a0001)
2565 log(LOG_INFO, "%s: FCC Authentication "
2566 "not required\n", DEVNAM(sc));
2567 else
2568 log(LOG_INFO, "%s: send FCC "
2569 "Authentication failed, "
2570 "error 0x%x\n", DEVNAM(sc), val);
2571
2572 /* FCC Auth is needed only once after power-on*/
2573 sc->sc_flags &= ~UMBFLG_FCC_AUTH_REQUIRED;
2574
2575 /* Try to proceed anyway */
2576 DPRINTF("%s: init: turning radio on ...\n",
2577 DEVNAM(sc));
2578 umb_radio(sc, 1);
2579 break;
2580 default:
2581 break;
2582 }
2583 break;
2584 default:
2585 break;
2586 }
2587 data += UMB_QMI_TLVLEN + tlvlen;
2588 len -= UMB_QMI_TLVLEN + tlvlen;
2589 }
2590 return;
2591
2592 tooshort:
2593 DPRINTF("%s: discard short QMI message\n", DEVNAM(sc));
2594 return;
2595 }
2596
2597 Static void
2598 umb_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
2599 {
2600 struct umb_softc *sc = priv;
2601 struct ifnet *ifp = GET_IFP(sc);
2602 int total_len;
2603
2604 if (status != USBD_NORMAL_COMPLETION) {
2605 DPRINTF("%s: notification error: %s\n", DEVNAM(sc),
2606 usbd_errstr(status));
2607 if (status == USBD_STALLED)
2608 usbd_clear_endpoint_stall_async(sc->sc_ctrl_pipe);
2609 return;
2610 }
2611 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
2612 if (total_len < UCDC_NOTIFICATION_LENGTH) {
2613 DPRINTF("%s: short notification (%d<%d)\n", DEVNAM(sc),
2614 total_len, UCDC_NOTIFICATION_LENGTH);
2615 return;
2616 }
2617 if (sc->sc_intr_msg.bmRequestType != UCDC_NOTIFICATION) {
2618 DPRINTF("%s: unexpected notification (type=0x%02x)\n",
2619 DEVNAM(sc), sc->sc_intr_msg.bmRequestType);
2620 return;
2621 }
2622
2623 switch (sc->sc_intr_msg.bNotification) {
2624 case UCDC_N_NETWORK_CONNECTION:
2625 if (ifp->if_flags & IFF_DEBUG)
2626 log(LOG_DEBUG, "%s: network %sconnected\n", DEVNAM(sc),
2627 UGETW(sc->sc_intr_msg.wValue) ? "" : "dis");
2628 break;
2629 case UCDC_N_RESPONSE_AVAILABLE:
2630 DPRINTFN(2, "%s: umb_intr: response available\n", DEVNAM(sc));
2631 ++sc->sc_nresp;
2632 usb_add_task(sc->sc_udev, &sc->sc_get_response_task, USB_TASKQ_DRIVER);
2633 break;
2634 case UCDC_N_CONNECTION_SPEED_CHANGE:
2635 DPRINTFN(2, "%s: umb_intr: connection speed changed\n",
2636 DEVNAM(sc));
2637 break;
2638 default:
2639 DPRINTF("%s: unexpected notifiation (0x%02x)\n",
2640 DEVNAM(sc), sc->sc_intr_msg.bNotification);
2641 break;
2642 }
2643 }
2644
2645 /*
2646 * Diagnostic routines
2647 */
2648 Static char *
2649 umb_ntop(struct sockaddr *sa)
2650 {
2651 #define NUMBUFS 4
2652 static char astr[NUMBUFS][INET_ADDRSTRLEN];
2653 static unsigned nbuf = 0;
2654 char *s;
2655
2656 s = astr[nbuf++];
2657 if (nbuf >= NUMBUFS)
2658 nbuf = 0;
2659
2660 switch (sa->sa_family) {
2661 case AF_INET:
2662 default:
2663 inet_ntop(AF_INET, &satosin(sa)->sin_addr, s, sizeof(astr[0]));
2664 break;
2665 case AF_INET6:
2666 inet_ntop(AF_INET6, &satosin6(sa)->sin6_addr, s,
2667 sizeof(astr[0]));
2668 break;
2669 }
2670 return s;
2671 }
2672
2673 #ifdef UMB_DEBUG
2674 Static char *
2675 umb_uuid2str(uint8_t uuid[MBIM_UUID_LEN])
2676 {
2677 static char uuidstr[2 * MBIM_UUID_LEN + 5];
2678
2679 #define UUID_BFMT "%02X"
2680 #define UUID_SEP "-"
2681 snprintf(uuidstr, sizeof(uuidstr),
2682 UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_SEP
2683 UUID_BFMT UUID_BFMT UUID_SEP
2684 UUID_BFMT UUID_BFMT UUID_SEP
2685 UUID_BFMT UUID_BFMT UUID_SEP
2686 UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT,
2687 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
2688 uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
2689 uuid[12], uuid[13], uuid[14], uuid[15]);
2690 return uuidstr;
2691 }
2692
2693 Static void
2694 umb_dump(void *buf, int len)
2695 {
2696 int i = 0;
2697 uint8_t *c = buf;
2698
2699 if (len == 0)
2700 return;
2701 while (i < len) {
2702 if ((i % 16) == 0) {
2703 if (i > 0)
2704 addlog("\n");
2705 log(LOG_DEBUG, "%4d: ", i);
2706 }
2707 addlog(" %02x", *c);
2708 c++;
2709 i++;
2710 }
2711 addlog("\n");
2712 }
2713 #endif /* UMB_DEBUG */
2714
2715 /* char *
2716 * inet_ntop(af, src, dst, size)
2717 * convert a network format address to presentation format.
2718 * return:
2719 * pointer to presentation format address (`dst'), or NULL (see errno).
2720 * author:
2721 * Paul Vixie, 1996.
2722 */
2723 Static const char *
2724 inet_ntop(int af, const void *src, char *dst, socklen_t size)
2725 {
2726 switch (af) {
2727 case AF_INET:
2728 return inet_ntop4(src, dst, (size_t)size);
2729 #ifdef INET6
2730 case AF_INET6:
2731 return inet_ntop6(src, dst, (size_t)size);
2732 #endif /* INET6 */
2733 default:
2734 return NULL;
2735 }
2736 /* NOTREACHED */
2737 }
2738
2739 /* const char *
2740 * inet_ntop4(src, dst, size)
2741 * format an IPv4 address, more or less like inet_ntoa()
2742 * return:
2743 * `dst' (as a const)
2744 * notes:
2745 * (1) uses no statics
2746 * (2) takes a u_char* not an in_addr as input
2747 * author:
2748 * Paul Vixie, 1996.
2749 */
2750 Static const char *
2751 inet_ntop4(const u_char *src, char *dst, size_t size)
2752 {
2753 char tmp[sizeof "255.255.255.255"];
2754 int l;
2755
2756 l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
2757 src[0], src[1], src[2], src[3]);
2758 if (l <= 0 || l >= size) {
2759 return NULL;
2760 }
2761 strlcpy(dst, tmp, size);
2762 return dst;
2763 }
2764
2765 #ifdef INET6
2766 /* const char *
2767 * inet_ntop6(src, dst, size)
2768 * convert IPv6 binary address into presentation (printable) format
2769 * author:
2770 * Paul Vixie, 1996.
2771 */
2772 Static const char *
2773 inet_ntop6(const u_char *src, char *dst, size_t size)
2774 {
2775 /*
2776 * Note that int32_t and int16_t need only be "at least" large enough
2777 * to contain a value of the specified size. On some systems, like
2778 * Crays, there is no such thing as an integer variable with 16 bits.
2779 * Keep this in mind if you think this function should have been coded
2780 * to use pointer overlays. All the world's not a VAX.
2781 */
2782 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
2783 char *tp, *ep;
2784 struct { int base, len; } best, cur;
2785 #define IN6ADDRSZ 16
2786 #define INT16SZ 2
2787 u_int words[IN6ADDRSZ / INT16SZ];
2788 int i;
2789 int advance;
2790
2791 /*
2792 * Preprocess:
2793 * Copy the input (bytewise) array into a wordwise array.
2794 * Find the longest run of 0x00's in src[] for :: shorthanding.
2795 */
2796 memset(words, '\0', sizeof words);
2797 for (i = 0; i < IN6ADDRSZ; i++)
2798 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
2799 best.base = -1;
2800 best.len = 0;
2801 cur.base = -1;
2802 cur.len = 0;
2803 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
2804 if (words[i] == 0) {
2805 if (cur.base == -1)
2806 cur.base = i, cur.len = 1;
2807 else
2808 cur.len++;
2809 } else {
2810 if (cur.base != -1) {
2811 if (best.base == -1 || cur.len > best.len)
2812 best = cur;
2813 cur.base = -1;
2814 }
2815 }
2816 }
2817 if (cur.base != -1) {
2818 if (best.base == -1 || cur.len > best.len)
2819 best = cur;
2820 }
2821 if (best.base != -1 && best.len < 2)
2822 best.base = -1;
2823
2824 /*
2825 * Format the result.
2826 */
2827 tp = tmp;
2828 ep = tmp + sizeof(tmp);
2829 for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
2830 /* Are we inside the best run of 0x00's? */
2831 if (best.base != -1 && i >= best.base &&
2832 i < (best.base + best.len)) {
2833 if (i == best.base) {
2834 if (tp + 1 >= ep)
2835 return NULL;
2836 *tp++ = ':';
2837 }
2838 continue;
2839 }
2840 /* Are we following an initial run of 0x00s or any real hex? */
2841 if (i != 0) {
2842 if (tp + 1 >= ep)
2843 return NULL;
2844 *tp++ = ':';
2845 }
2846 /* Is this address an encapsulated IPv4? */
2847 if (i == 6 && best.base == 0 &&
2848 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
2849 if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
2850 return NULL;
2851 tp += strlen(tp);
2852 break;
2853 }
2854 advance = snprintf(tp, ep - tp, "%x", words[i]);
2855 if (advance <= 0 || advance >= ep - tp)
2856 return NULL;
2857 tp += advance;
2858 }
2859 /* Was it a trailing run of 0x00's? */
2860 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
2861 if (tp + 1 >= ep)
2862 return NULL;
2863 *tp++ = ':';
2864 }
2865 if (tp + 1 >= ep)
2866 return NULL;
2867 *tp++ = '\0';
2868
2869 /*
2870 * Check for overflow, copy, and we're done.
2871 */
2872 if ((size_t)(tp - tmp) > size) {
2873 return NULL;
2874 }
2875 strlcpy(dst, tmp, size);
2876 return dst;
2877 }
2878 #endif /* INET6 */
2879