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