1 /* $NetBSD: if_umb.c,v 1.19 2020/03/24 07:12:16 maxv Exp $ */ 2 /* $OpenBSD: if_umb.c,v 1.20 2018/09/10 17:00:45 gerhard 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.19 2020/03/24 07:12:16 maxv 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=%#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 = device_private(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_fini(&sc->sc_im); 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 = kauth_authorize_network(curlwp->l_cred, 783 KAUTH_NETWORK_INTERFACE, 784 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 785 NULL); 786 if (error) 787 break; 788 error = copyout(&sc->sc_info, ifr->ifr_data, 789 sizeof(sc->sc_info)); 790 break; 791 case SIOCSUMBPARAM: 792 error = kauth_authorize_network(curlwp->l_cred, 793 KAUTH_NETWORK_INTERFACE, 794 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 795 NULL); 796 if (error) 797 break; 798 799 if ((error = copyin(ifr->ifr_data, &mp, sizeof(mp))) != 0) 800 break; 801 802 if ((error = umb_setpin(sc, mp.op, mp.is_puk, mp.pin, mp.pinlen, 803 mp.newpin, mp.newpinlen)) != 0) 804 break; 805 806 if (mp.apnlen < 0 || mp.apnlen > sizeof(sc->sc_info.apn)) { 807 error = EINVAL; 808 break; 809 } 810 sc->sc_roaming = mp.roaming ? 1 : 0; 811 memset(sc->sc_info.apn, 0, sizeof(sc->sc_info.apn)); 812 memcpy(sc->sc_info.apn, mp.apn, mp.apnlen); 813 sc->sc_info.apnlen = mp.apnlen; 814 memset(sc->sc_info.username, 0, sizeof(sc->sc_info.username)); 815 memcpy(sc->sc_info.username, mp.username, mp.usernamelen); 816 sc->sc_info.usernamelen = mp.usernamelen; 817 memset(sc->sc_info.password, 0, sizeof(sc->sc_info.password)); 818 memcpy(sc->sc_info.password, mp.password, mp.passwordlen); 819 sc->sc_info.passwordlen = mp.passwordlen; 820 sc->sc_info.preferredclasses = mp.preferredclasses; 821 umb_setdataclass(sc); 822 break; 823 case SIOCGUMBPARAM: 824 memset(&mp, 0, sizeof(mp)); 825 memcpy(mp.apn, sc->sc_info.apn, sc->sc_info.apnlen); 826 mp.apnlen = sc->sc_info.apnlen; 827 mp.roaming = sc->sc_roaming; 828 mp.preferredclasses = sc->sc_info.preferredclasses; 829 error = copyout(&mp, ifr->ifr_data, sizeof(mp)); 830 break; 831 case SIOCSIFMTU: 832 /* Does this include the NCM headers and tail? */ 833 if (ifr->ifr_mtu > ifp->if_mtu) { 834 error = EINVAL; 835 break; 836 } 837 ifp->if_mtu = ifr->ifr_mtu; 838 break; 839 case SIOCSIFADDR: 840 case SIOCAIFADDR: 841 case SIOCSIFDSTADDR: 842 case SIOCADDMULTI: 843 case SIOCDELMULTI: 844 break; 845 case SIOCGIFMEDIA: 846 error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd); 847 break; 848 default: 849 error = ifioctl_common(ifp, cmd, data); 850 break; 851 } 852 splx(s); 853 return error; 854 } 855 856 Static int 857 umb_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 858 const struct rtentry *rtp) 859 { 860 int error; 861 862 DPRINTFN(10, "%s: %s: enter\n", 863 device_xname(((struct umb_softc *)ifp->if_softc)->sc_dev), 864 __func__); 865 866 /* 867 * if the queueing discipline needs packet classification, 868 * do it now. 869 */ 870 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family); 871 872 /* 873 * Queue message on interface, and start output if interface 874 * not yet active. 875 */ 876 error = if_transmit_lock(ifp, m); 877 878 return error; 879 } 880 881 Static void 882 umb_input(struct ifnet *ifp, struct mbuf *m) 883 { 884 size_t pktlen = m->m_len; 885 int s; 886 887 if ((ifp->if_flags & IFF_UP) == 0) { 888 m_freem(m); 889 return; 890 } 891 if (pktlen < sizeof(struct ip)) { 892 if_statinc(ifp, if_ierrors); 893 DPRINTFN(4, "%s: dropping short packet (len %zd)\n", __func__, 894 pktlen); 895 m_freem(m); 896 return; 897 } 898 s = splnet(); 899 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) { 900 if_statinc(ifp, if_iqdrops); 901 m_freem(m); 902 } else { 903 if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen); 904 } 905 splx(s); 906 } 907 908 Static void 909 umb_start(struct ifnet *ifp) 910 { 911 struct umb_softc *sc = ifp->if_softc; 912 struct mbuf *m_head = NULL; 913 914 if (sc->sc_dying || (ifp->if_flags & IFF_OACTIVE)) 915 return; 916 917 IFQ_POLL(&ifp->if_snd, m_head); 918 if (m_head == NULL) 919 return; 920 921 if (!umb_encap(sc, m_head)) { 922 ifp->if_flags |= IFF_OACTIVE; 923 return; 924 } 925 IFQ_DEQUEUE(&ifp->if_snd, m_head); 926 927 bpf_mtap(ifp, m_head, BPF_D_OUT); 928 929 ifp->if_flags |= IFF_OACTIVE; 930 ifp->if_timer = (2 * umb_xfer_tout) / 1000; 931 } 932 933 Static void 934 umb_watchdog(struct ifnet *ifp) 935 { 936 struct umb_softc *sc = ifp->if_softc; 937 938 if (sc->sc_dying) 939 return; 940 941 if_statinc(ifp, if_oerrors); 942 printf("%s: watchdog timeout\n", DEVNAM(sc)); 943 usbd_abort_pipe(sc->sc_tx_pipe); 944 return; 945 } 946 947 Static void 948 umb_statechg_timeout(void *arg) 949 { 950 struct umb_softc *sc = arg; 951 struct ifnet *ifp = GET_IFP(sc); 952 953 if (sc->sc_info.regstate != MBIM_REGSTATE_ROAMING || sc->sc_roaming) 954 if (ifp->if_flags & IFF_DEBUG) 955 log(LOG_DEBUG, "%s: state change timeout\n", 956 DEVNAM(sc)); 957 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER); 958 } 959 960 Static int 961 umb_mediachange(struct ifnet * ifp) 962 { 963 return 0; 964 } 965 966 Static void 967 umb_mediastatus(struct ifnet * ifp, struct ifmediareq * imr) 968 { 969 switch (ifp->if_link_state) { 970 case LINK_STATE_UP: 971 imr->ifm_status = IFM_AVALID | IFM_ACTIVE; 972 break; 973 case LINK_STATE_DOWN: 974 imr->ifm_status = IFM_AVALID; 975 break; 976 default: 977 imr->ifm_status = 0; 978 break; 979 } 980 } 981 982 Static void 983 umb_newstate(struct umb_softc *sc, enum umb_state newstate, int flags) 984 { 985 struct ifnet *ifp = GET_IFP(sc); 986 987 if (newstate == sc->sc_state) 988 return; 989 if (((flags & UMB_NS_DONT_DROP) && newstate < sc->sc_state) || 990 ((flags & UMB_NS_DONT_RAISE) && newstate > sc->sc_state)) 991 return; 992 if (ifp->if_flags & IFF_DEBUG) 993 log(LOG_DEBUG, "%s: state going %s from '%s' to '%s'\n", 994 DEVNAM(sc), newstate > sc->sc_state ? "up" : "down", 995 umb_istate(sc->sc_state), umb_istate(newstate)); 996 sc->sc_state = newstate; 997 usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER); 998 } 999 1000 Static void 1001 umb_state_task(void *arg) 1002 { 1003 struct umb_softc *sc = arg; 1004 struct ifnet *ifp = GET_IFP(sc); 1005 struct ifreq ifr; 1006 int s; 1007 int state; 1008 1009 if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) { 1010 /* 1011 * Query the registration state until we're with the home 1012 * network again. 1013 */ 1014 umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_QRY, NULL, 0); 1015 return; 1016 } 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 message %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 message\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 message 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 message (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 %#x, class %#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 %#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 configuration 1715 */ 1716 avail = le32toh(ic->ipv4_available); 1717 if ((avail & (MBIM_IPCONF_HAS_ADDRINFO | MBIM_IPCONF_HAS_GWINFO)) == 1718 (MBIM_IPCONF_HAS_ADDRINFO | MBIM_IPCONF_HAS_GWINFO)) { 1719 n = le32toh(ic->ipv4_naddr); 1720 off = le32toh(ic->ipv4_addroffs); 1721 1722 if (n == 0 || off + sizeof(ipv4elem) > len) 1723 goto done; 1724 1725 /* Only pick the first one */ 1726 memcpy(&ipv4elem, (char *)data + off, sizeof(ipv4elem)); 1727 ipv4elem.prefixlen = le32toh(ipv4elem.prefixlen); 1728 1729 memset(&ifra, 0, sizeof(ifra)); 1730 sin = (struct sockaddr_in *)&ifra.ifra_addr; 1731 sin->sin_family = AF_INET; 1732 sin->sin_len = sizeof(ifra.ifra_addr); 1733 sin->sin_addr.s_addr = ipv4elem.addr; 1734 1735 sin = (struct sockaddr_in *)&ifra.ifra_dstaddr; 1736 sin->sin_family = AF_INET; 1737 sin->sin_len = sizeof(ifra.ifra_dstaddr); 1738 off = le32toh(ic->ipv4_gwoffs); 1739 sin->sin_addr.s_addr = *((uint32_t *)((char *)data + off)); 1740 1741 sin = (struct sockaddr_in *)&ifra.ifra_mask; 1742 sin->sin_family = AF_INET; 1743 sin->sin_len = sizeof(ifra.ifra_mask); 1744 umb_in_len2mask(&sin->sin_addr, ipv4elem.prefixlen); 1745 1746 rv = in_control(NULL, SIOCAIFADDR, &ifra, ifp); 1747 if (rv == 0) { 1748 if (ifp->if_flags & IFF_DEBUG) 1749 log(LOG_INFO, "%s: IPv4 addr %s, mask %s, " 1750 "gateway %s\n", device_xname(sc->sc_dev), 1751 umb_ntop(sintosa(&ifra.ifra_addr)), 1752 umb_ntop(sintosa(&ifra.ifra_mask)), 1753 umb_ntop(sintosa(&ifra.ifra_dstaddr))); 1754 state = UMB_S_UP; 1755 } else 1756 printf("%s: unable to set IPv4 address, error %d\n", 1757 device_xname(sc->sc_dev), rv); 1758 } 1759 1760 memset(sc->sc_info.ipv4dns, 0, sizeof(sc->sc_info.ipv4dns)); 1761 if (avail & MBIM_IPCONF_HAS_DNSINFO) { 1762 n = le32toh(ic->ipv4_ndnssrv); 1763 off = le32toh(ic->ipv4_dnssrvoffs); 1764 i = 0; 1765 while (n-- > 0) { 1766 if (off + sizeof(uint32_t) > len) 1767 break; 1768 val = *((uint32_t *)((char *)data + off)); 1769 if (i < UMB_MAX_DNSSRV) 1770 sc->sc_info.ipv4dns[i++] = val; 1771 off += sizeof(uint32_t); 1772 } 1773 } 1774 1775 if ((avail & MBIM_IPCONF_HAS_MTUINFO)) { 1776 val = le32toh(ic->ipv4_mtu); 1777 if (ifp->if_mtu != val && val <= sc->sc_maxpktlen) { 1778 ifp->if_mtu = val; 1779 if (ifp->if_mtu > val) 1780 ifp->if_mtu = val; 1781 if (ifp->if_flags & IFF_DEBUG) 1782 log(LOG_INFO, "%s: MTU %d\n", DEVNAM(sc), val); 1783 } 1784 } 1785 1786 avail = le32toh(ic->ipv6_available); 1787 if ((ifp->if_flags & IFF_DEBUG) && avail & MBIM_IPCONF_HAS_ADDRINFO) { 1788 /* XXX FIXME: IPv6 configuration missing */ 1789 log(LOG_INFO, "%s: ignoring IPv6 configuration\n", DEVNAM(sc)); 1790 } 1791 if (state != -1) 1792 umb_newstate(sc, state, 0); 1793 1794 done: 1795 splx(s); 1796 return 1; 1797 } 1798 1799 Static void 1800 umb_rx(struct umb_softc *sc) 1801 { 1802 usbd_setup_xfer(sc->sc_rx_xfer, sc, sc->sc_rx_buf, 1803 sc->sc_rx_bufsz, USBD_SHORT_XFER_OK, 1804 USBD_NO_TIMEOUT, umb_rxeof); 1805 usbd_transfer(sc->sc_rx_xfer); 1806 } 1807 1808 Static void 1809 umb_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1810 { 1811 struct umb_softc *sc = priv; 1812 struct ifnet *ifp = GET_IFP(sc); 1813 1814 if (sc->sc_dying || !(ifp->if_flags & IFF_RUNNING)) 1815 return; 1816 1817 if (status != USBD_NORMAL_COMPLETION) { 1818 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1819 return; 1820 DPRINTF("%s: rx error: %s\n", DEVNAM(sc), usbd_errstr(status)); 1821 if (status == USBD_STALLED) 1822 usbd_clear_endpoint_stall_async(sc->sc_rx_pipe); 1823 if (++sc->sc_rx_nerr > 100) { 1824 log(LOG_ERR, "%s: too many rx errors, disabling\n", 1825 DEVNAM(sc)); 1826 umb_activate(sc->sc_dev, DVACT_DEACTIVATE); 1827 } 1828 } else { 1829 sc->sc_rx_nerr = 0; 1830 umb_decap(sc, xfer); 1831 } 1832 1833 umb_rx(sc); 1834 return; 1835 } 1836 1837 Static int 1838 umb_encap(struct umb_softc *sc, struct mbuf *m) 1839 { 1840 struct ncm_header16 *hdr; 1841 struct ncm_pointer16 *ptr; 1842 usbd_status err; 1843 int len; 1844 1845 /* All size constraints have been validated by the caller! */ 1846 hdr = (struct ncm_header16 *)sc->sc_tx_buf; 1847 ptr = (struct ncm_pointer16 *)(hdr + 1); 1848 USETDW(hdr->dwSignature, NCM_HDR16_SIG); 1849 USETW(hdr->wHeaderLength, sizeof(*hdr)); 1850 USETW(hdr->wSequence, sc->sc_tx_seq); 1851 sc->sc_tx_seq++; 1852 1853 len = m->m_pkthdr.len; 1854 1855 USETDW(ptr->dwSignature, MBIM_NCM_NTH16_SIG(umb_session_id)); 1856 USETW(ptr->wLength, sizeof(*ptr)); 1857 USETW(ptr->wNextNdpIndex, 0); 1858 USETW(ptr->dgram[0].wDatagramIndex, MBIM_HDR16_LEN); 1859 USETW(ptr->dgram[0].wDatagramLen, len); 1860 USETW(ptr->dgram[1].wDatagramIndex, 0); 1861 USETW(ptr->dgram[1].wDatagramLen, 0); 1862 1863 KASSERT(len <= sc->sc_tx_bufsz - sizeof(*hdr) - sizeof(*ptr)); 1864 m_copydata(m, 0, len, ptr + 1); 1865 sc->sc_tx_m = m; 1866 len += MBIM_HDR16_LEN; 1867 USETW(hdr->wBlockLength, len); 1868 1869 DPRINTFN(3, "%s: encap %d bytes\n", DEVNAM(sc), len); 1870 DDUMPN(5, sc->sc_tx_buf, len); 1871 usbd_setup_xfer(sc->sc_tx_xfer, sc, sc->sc_tx_buf, len, 1872 USBD_FORCE_SHORT_XFER, umb_xfer_tout, umb_txeof); 1873 err = usbd_transfer(sc->sc_tx_xfer); 1874 if (err != USBD_IN_PROGRESS) { 1875 DPRINTF("%s: start tx error: %s\n", DEVNAM(sc), 1876 usbd_errstr(err)); 1877 return 0; 1878 } 1879 return 1; 1880 } 1881 1882 Static void 1883 umb_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1884 { 1885 struct umb_softc *sc = priv; 1886 struct ifnet *ifp = GET_IFP(sc); 1887 int s; 1888 1889 s = splnet(); 1890 ifp->if_flags &= ~IFF_OACTIVE; 1891 ifp->if_timer = 0; 1892 1893 m_freem(sc->sc_tx_m); 1894 sc->sc_tx_m = NULL; 1895 1896 if (status != USBD_NORMAL_COMPLETION) { 1897 if (status != USBD_NOT_STARTED && status != USBD_CANCELLED) { 1898 if_statinc(ifp, if_oerrors); 1899 DPRINTF("%s: tx error: %s\n", DEVNAM(sc), 1900 usbd_errstr(status)); 1901 if (status == USBD_STALLED) 1902 usbd_clear_endpoint_stall_async(sc->sc_tx_pipe); 1903 } 1904 } 1905 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1906 umb_start(ifp); 1907 1908 splx(s); 1909 } 1910 1911 Static void 1912 umb_decap(struct umb_softc *sc, struct usbd_xfer *xfer) 1913 { 1914 struct ifnet *ifp = GET_IFP(sc); 1915 int s; 1916 char *buf; 1917 uint32_t len; 1918 char *dp; 1919 struct ncm_header16 *hdr16; 1920 struct ncm_header32 *hdr32; 1921 struct ncm_pointer16 *ptr16; 1922 struct ncm_pointer16_dgram *dgram16; 1923 struct ncm_pointer32_dgram *dgram32; 1924 uint32_t hsig, psig; 1925 int hlen, blen; 1926 int ptrlen, ptroff, dgentryoff; 1927 uint32_t doff, dlen; 1928 struct mbuf *m; 1929 1930 usbd_get_xfer_status(xfer, NULL, (void **)&buf, &len, NULL); 1931 DPRINTFN(4, "%s: recv %d bytes\n", DEVNAM(sc), len); 1932 DDUMPN(5, buf, len); 1933 s = splnet(); 1934 if (len < sizeof(*hdr16)) 1935 goto toosmall; 1936 1937 hdr16 = (struct ncm_header16 *)buf; 1938 hsig = UGETDW(hdr16->dwSignature); 1939 hlen = UGETW(hdr16->wHeaderLength); 1940 if (len < hlen) 1941 goto toosmall; 1942 if (len > sc->sc_rx_bufsz) { 1943 DPRINTF("%s: packet too large (%d)\n", DEVNAM(sc), len); 1944 goto fail; 1945 } 1946 switch (hsig) { 1947 case NCM_HDR16_SIG: 1948 blen = UGETW(hdr16->wBlockLength); 1949 ptroff = UGETW(hdr16->wNdpIndex); 1950 if (hlen != sizeof(*hdr16)) { 1951 DPRINTF("%s: bad header len %d for NTH16 (exp %zu)\n", 1952 DEVNAM(sc), hlen, sizeof(*hdr16)); 1953 goto fail; 1954 } 1955 break; 1956 case NCM_HDR32_SIG: 1957 hdr32 = (struct ncm_header32 *)hdr16; 1958 blen = UGETDW(hdr32->dwBlockLength); 1959 ptroff = UGETDW(hdr32->dwNdpIndex); 1960 if (hlen != sizeof(*hdr32)) { 1961 DPRINTF("%s: bad header len %d for NTH32 (exp %zu)\n", 1962 DEVNAM(sc), hlen, sizeof(*hdr32)); 1963 goto fail; 1964 } 1965 break; 1966 default: 1967 DPRINTF("%s: unsupported NCM header signature (0x%08x)\n", 1968 DEVNAM(sc), hsig); 1969 goto fail; 1970 } 1971 if (len < blen) { 1972 DPRINTF("%s: bad NTB len (%d) for %d bytes of data\n", 1973 DEVNAM(sc), blen, len); 1974 goto fail; 1975 } 1976 1977 ptr16 = (struct ncm_pointer16 *)(buf + ptroff); 1978 psig = UGETDW(ptr16->dwSignature); 1979 ptrlen = UGETW(ptr16->wLength); 1980 if (len < ptrlen + ptroff) 1981 goto toosmall; 1982 if (!MBIM_NCM_NTH16_ISISG(psig) && !MBIM_NCM_NTH32_ISISG(psig)) { 1983 DPRINTF("%s: unsupported NCM pointer signature (0x%08x)\n", 1984 DEVNAM(sc), psig); 1985 goto fail; 1986 } 1987 1988 switch (hsig) { 1989 case NCM_HDR16_SIG: 1990 dgentryoff = offsetof(struct ncm_pointer16, dgram); 1991 break; 1992 case NCM_HDR32_SIG: 1993 dgentryoff = offsetof(struct ncm_pointer32, dgram); 1994 break; 1995 default: 1996 goto fail; 1997 } 1998 1999 while (dgentryoff < ptrlen) { 2000 switch (hsig) { 2001 case NCM_HDR16_SIG: 2002 if (ptroff + dgentryoff < sizeof(*dgram16)) 2003 goto done; 2004 dgram16 = (struct ncm_pointer16_dgram *) 2005 (buf + ptroff + dgentryoff); 2006 dgentryoff += sizeof(*dgram16); 2007 dlen = UGETW(dgram16->wDatagramLen); 2008 doff = UGETW(dgram16->wDatagramIndex); 2009 break; 2010 case NCM_HDR32_SIG: 2011 if (ptroff + dgentryoff < sizeof(*dgram32)) 2012 goto done; 2013 dgram32 = (struct ncm_pointer32_dgram *) 2014 (buf + ptroff + dgentryoff); 2015 dgentryoff += sizeof(*dgram32); 2016 dlen = UGETDW(dgram32->dwDatagramLen); 2017 doff = UGETDW(dgram32->dwDatagramIndex); 2018 break; 2019 default: 2020 if_statinc(ifp, if_ierrors); 2021 goto done; 2022 } 2023 2024 /* Terminating zero entry */ 2025 if (dlen == 0 || doff == 0) 2026 break; 2027 if (len < dlen + doff) { 2028 /* Skip giant datagram but continue processing */ 2029 DPRINTF("%s: datagram too large (%d @ off %d)\n", 2030 DEVNAM(sc), dlen, doff); 2031 continue; 2032 } 2033 2034 dp = buf + doff; 2035 DPRINTFN(3, "%s: decap %d bytes\n", DEVNAM(sc), dlen); 2036 m = m_devget(dp, dlen, 0, ifp); 2037 if (m == NULL) { 2038 if_statinc(ifp, if_iqdrops); 2039 continue; 2040 } 2041 2042 if_percpuq_enqueue((ifp)->if_percpuq, (m)); 2043 } 2044 done: 2045 splx(s); 2046 return; 2047 toosmall: 2048 DPRINTF("%s: packet too small (%d)\n", DEVNAM(sc), len); 2049 fail: 2050 if_statinc(ifp, if_ierrors); 2051 splx(s); 2052 } 2053 2054 Static usbd_status 2055 umb_send_encap_command(struct umb_softc *sc, void *data, int len) 2056 { 2057 struct usbd_xfer *xfer; 2058 usb_device_request_t req; 2059 char *buf; 2060 2061 if (len > sc->sc_ctrl_len) 2062 return USBD_INVAL; 2063 2064 if (usbd_create_xfer(sc->sc_udev->ud_pipe0, len, 0, 0, &xfer) != 0) 2065 return USBD_NOMEM; 2066 buf = usbd_get_buffer(xfer); 2067 memcpy(buf, data, len); 2068 2069 /* XXX FIXME: if (total len > sc->sc_ctrl_len) => must fragment */ 2070 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 2071 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 2072 USETW(req.wValue, 0); 2073 USETW(req.wIndex, sc->sc_ctrl_ifaceno); 2074 USETW(req.wLength, len); 2075 DELAY(umb_delay); 2076 return usbd_request_async(sc->sc_udev, xfer, &req, NULL, NULL); 2077 } 2078 2079 Static int 2080 umb_get_encap_response(struct umb_softc *sc, void *buf, int *len) 2081 { 2082 usb_device_request_t req; 2083 usbd_status err; 2084 2085 req.bmRequestType = UT_READ_CLASS_INTERFACE; 2086 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 2087 USETW(req.wValue, 0); 2088 USETW(req.wIndex, sc->sc_ctrl_ifaceno); 2089 USETW(req.wLength, *len); 2090 /* XXX FIXME: re-assemble fragments */ 2091 2092 DELAY(umb_delay); 2093 err = usbd_do_request_flags(sc->sc_udev, &req, buf, USBD_SHORT_XFER_OK, 2094 len, umb_xfer_tout); 2095 if (err == USBD_NORMAL_COMPLETION) 2096 return 1; 2097 DPRINTF("%s: ctrl recv: %s\n", DEVNAM(sc), usbd_errstr(err)); 2098 return 0; 2099 } 2100 2101 Static void 2102 umb_ctrl_msg(struct umb_softc *sc, uint32_t req, void *data, int len) 2103 { 2104 struct ifnet *ifp = GET_IFP(sc); 2105 uint32_t tid; 2106 struct mbim_msghdr *hdr = data; 2107 usbd_status err; 2108 int s; 2109 2110 if (sc->sc_dying) 2111 return; 2112 if (len < sizeof(*hdr)) 2113 return; 2114 tid = ++sc->sc_tid; 2115 2116 hdr->type = htole32(req); 2117 hdr->len = htole32(len); 2118 hdr->tid = htole32(tid); 2119 2120 #ifdef UMB_DEBUG 2121 if (umb_debug) { 2122 const char *op, *str; 2123 if (req == MBIM_COMMAND_MSG) { 2124 struct mbim_h2f_cmd *c = data; 2125 if (le32toh(c->op) == MBIM_CMDOP_SET) 2126 op = "set"; 2127 else 2128 op = "qry"; 2129 str = umb_cid2str(le32toh(c->cid)); 2130 } else { 2131 op = "snd"; 2132 str = umb_request2str(req); 2133 } 2134 DPRINTF("%s: -> %s %s (tid %u)\n", DEVNAM(sc), op, str, tid); 2135 } 2136 #endif 2137 s = splusb(); 2138 err = umb_send_encap_command(sc, data, len); 2139 splx(s); 2140 if (err != USBD_NORMAL_COMPLETION) { 2141 if (ifp->if_flags & IFF_DEBUG) 2142 log(LOG_ERR, "%s: send %s msg (tid %u) failed: %s\n", 2143 DEVNAM(sc), umb_request2str(req), tid, 2144 usbd_errstr(err)); 2145 2146 /* will affect other transactions, too */ 2147 usbd_abort_pipe(sc->sc_udev->ud_pipe0); 2148 } else { 2149 DPRINTFN(2, "%s: sent %s (tid %u)\n", DEVNAM(sc), 2150 umb_request2str(req), tid); 2151 DDUMPN(3, data, len); 2152 } 2153 return; 2154 } 2155 2156 Static void 2157 umb_open(struct umb_softc *sc) 2158 { 2159 struct mbim_h2f_openmsg msg; 2160 2161 memset(&msg, 0, sizeof(msg)); 2162 msg.maxlen = htole32(sc->sc_ctrl_len); 2163 umb_ctrl_msg(sc, MBIM_OPEN_MSG, &msg, sizeof(msg)); 2164 return; 2165 } 2166 2167 Static void 2168 umb_close(struct umb_softc *sc) 2169 { 2170 struct mbim_h2f_closemsg msg; 2171 2172 memset(&msg, 0, sizeof(msg)); 2173 umb_ctrl_msg(sc, MBIM_CLOSE_MSG, &msg, sizeof(msg)); 2174 } 2175 2176 Static int 2177 umb_setpin(struct umb_softc *sc, int op, int is_puk, void *pin, int pinlen, 2178 void *newpin, int newpinlen) 2179 { 2180 struct mbim_cid_pin cp; 2181 int off; 2182 2183 if (pinlen == 0) 2184 return 0; 2185 if (pinlen < 0 || pinlen > MBIM_PIN_MAXLEN || 2186 newpinlen < 0 || newpinlen > MBIM_PIN_MAXLEN || 2187 op < 0 || op > MBIM_PIN_OP_CHANGE || 2188 (is_puk && op != MBIM_PIN_OP_ENTER)) 2189 return EINVAL; 2190 2191 memset(&cp, 0, sizeof(cp)); 2192 cp.type = htole32(is_puk ? MBIM_PIN_TYPE_PUK1 : MBIM_PIN_TYPE_PIN1); 2193 2194 off = offsetof(struct mbim_cid_pin, data); 2195 if (!umb_addstr(&cp, sizeof(cp), &off, pin, pinlen, 2196 &cp.pin_offs, &cp.pin_size)) 2197 return EINVAL; 2198 2199 cp.op = htole32(op); 2200 if (newpinlen) { 2201 if (!umb_addstr(&cp, sizeof(cp), &off, newpin, newpinlen, 2202 &cp.newpin_offs, &cp.newpin_size)) 2203 return EINVAL; 2204 } else { 2205 if ((op == MBIM_PIN_OP_CHANGE) || is_puk) 2206 return EINVAL; 2207 if (!umb_addstr(&cp, sizeof(cp), &off, NULL, 0, 2208 &cp.newpin_offs, &cp.newpin_size)) 2209 return EINVAL; 2210 } 2211 umb_cmd(sc, MBIM_CID_PIN, MBIM_CMDOP_SET, &cp, off); 2212 return 0; 2213 } 2214 2215 Static void 2216 umb_setdataclass(struct umb_softc *sc) 2217 { 2218 struct mbim_cid_registration_state rs; 2219 uint32_t classes; 2220 2221 if (sc->sc_info.supportedclasses == MBIM_DATACLASS_NONE) 2222 return; 2223 2224 memset(&rs, 0, sizeof(rs)); 2225 rs.regaction = htole32(MBIM_REGACTION_AUTOMATIC); 2226 classes = sc->sc_info.supportedclasses; 2227 if (sc->sc_info.preferredclasses != MBIM_DATACLASS_NONE) 2228 classes &= sc->sc_info.preferredclasses; 2229 rs.data_class = htole32(classes); 2230 umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_SET, &rs, sizeof(rs)); 2231 } 2232 2233 Static void 2234 umb_radio(struct umb_softc *sc, int on) 2235 { 2236 struct mbim_cid_radio_state s; 2237 2238 DPRINTF("%s: set radio %s\n", DEVNAM(sc), on ? "on" : "off"); 2239 memset(&s, 0, sizeof(s)); 2240 s.state = htole32(on ? MBIM_RADIO_STATE_ON : MBIM_RADIO_STATE_OFF); 2241 umb_cmd(sc, MBIM_CID_RADIO_STATE, MBIM_CMDOP_SET, &s, sizeof(s)); 2242 } 2243 2244 Static void 2245 umb_allocate_cid(struct umb_softc *sc) 2246 { 2247 umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET, 2248 umb_qmi_alloc_cid, sizeof(umb_qmi_alloc_cid), umb_uuid_qmi_mbim); 2249 } 2250 2251 Static void 2252 umb_send_fcc_auth(struct umb_softc *sc) 2253 { 2254 uint8_t fccauth[sizeof(umb_qmi_fcc_auth)]; 2255 2256 if (sc->sc_cid == -1) { 2257 DPRINTF("%s: missing CID, cannot send FCC auth\n", DEVNAM(sc)); 2258 umb_allocate_cid(sc); 2259 return; 2260 } 2261 memcpy(fccauth, umb_qmi_fcc_auth, sizeof(fccauth)); 2262 fccauth[UMB_QMI_CID_OFFS] = sc->sc_cid; 2263 umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET, 2264 fccauth, sizeof(fccauth), umb_uuid_qmi_mbim); 2265 } 2266 2267 Static void 2268 umb_packet_service(struct umb_softc *sc, int attach) 2269 { 2270 struct mbim_cid_packet_service s; 2271 2272 DPRINTF("%s: %s packet service\n", DEVNAM(sc), 2273 attach ? "attach" : "detach"); 2274 memset(&s, 0, sizeof(s)); 2275 s.action = htole32(attach ? 2276 MBIM_PKTSERVICE_ACTION_ATTACH : MBIM_PKTSERVICE_ACTION_DETACH); 2277 umb_cmd(sc, MBIM_CID_PACKET_SERVICE, MBIM_CMDOP_SET, &s, sizeof(s)); 2278 } 2279 2280 Static void 2281 umb_connect(struct umb_softc *sc) 2282 { 2283 struct ifnet *ifp = GET_IFP(sc); 2284 2285 if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) { 2286 log(LOG_INFO, "%s: connection disabled in roaming network\n", 2287 DEVNAM(sc)); 2288 return; 2289 } 2290 if (ifp->if_flags & IFF_DEBUG) 2291 log(LOG_DEBUG, "%s: connecting ...\n", DEVNAM(sc)); 2292 umb_send_connect(sc, MBIM_CONNECT_ACTIVATE); 2293 } 2294 2295 Static void 2296 umb_disconnect(struct umb_softc *sc) 2297 { 2298 struct ifnet *ifp = GET_IFP(sc); 2299 2300 if (ifp->if_flags & IFF_DEBUG) 2301 log(LOG_DEBUG, "%s: disconnecting ...\n", DEVNAM(sc)); 2302 umb_send_connect(sc, MBIM_CONNECT_DEACTIVATE); 2303 } 2304 2305 Static void 2306 umb_send_connect(struct umb_softc *sc, int command) 2307 { 2308 struct mbim_cid_connect *c; 2309 int off; 2310 2311 /* Too large or the stack */ 2312 c = kmem_zalloc(sizeof(*c), KM_SLEEP); 2313 c->sessionid = htole32(umb_session_id); 2314 c->command = htole32(command); 2315 off = offsetof(struct mbim_cid_connect, data); 2316 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.apn, 2317 sc->sc_info.apnlen, &c->access_offs, &c->access_size)) 2318 goto done; 2319 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.username, 2320 sc->sc_info.usernamelen, &c->user_offs, &c->user_size)) 2321 goto done; 2322 if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.password, 2323 sc->sc_info.passwordlen, &c->passwd_offs, &c->passwd_size)) 2324 goto done; 2325 c->authprot = htole32(MBIM_AUTHPROT_NONE); 2326 c->compression = htole32(MBIM_COMPRESSION_NONE); 2327 c->iptype = htole32(MBIM_CONTEXT_IPTYPE_IPV4); 2328 memcpy(c->context, umb_uuid_context_internet, sizeof(c->context)); 2329 umb_cmd(sc, MBIM_CID_CONNECT, MBIM_CMDOP_SET, c, off); 2330 done: 2331 kmem_free(c, sizeof(*c)); 2332 return; 2333 } 2334 2335 Static void 2336 umb_qry_ipconfig(struct umb_softc *sc) 2337 { 2338 struct mbim_cid_ip_configuration_info ipc; 2339 2340 memset(&ipc, 0, sizeof(ipc)); 2341 ipc.sessionid = htole32(umb_session_id); 2342 umb_cmd(sc, MBIM_CID_IP_CONFIGURATION, MBIM_CMDOP_QRY, 2343 &ipc, sizeof(ipc)); 2344 } 2345 2346 Static void 2347 umb_cmd(struct umb_softc *sc, int cid, int op, const void *data, int len) 2348 { 2349 umb_cmd1(sc, cid, op, data, len, umb_uuid_basic_connect); 2350 } 2351 2352 Static void 2353 umb_cmd1(struct umb_softc *sc, int cid, int op, const void *data, int len, 2354 uint8_t *uuid) 2355 { 2356 struct mbim_h2f_cmd *cmd; 2357 int totlen; 2358 2359 /* XXX FIXME support sending fragments */ 2360 if (sizeof(*cmd) + len > sc->sc_ctrl_len) { 2361 DPRINTF("%s: set %s msg too long: cannot send\n", 2362 DEVNAM(sc), umb_cid2str(cid)); 2363 return; 2364 } 2365 cmd = sc->sc_ctrl_msg; 2366 memset(cmd, 0, sizeof(*cmd)); 2367 cmd->frag.nfrag = htole32(1); 2368 memcpy(cmd->devid, uuid, sizeof(cmd->devid)); 2369 cmd->cid = htole32(cid); 2370 cmd->op = htole32(op); 2371 cmd->infolen = htole32(len); 2372 totlen = sizeof(*cmd); 2373 if (len > 0) { 2374 memcpy(cmd + 1, data, len); 2375 totlen += len; 2376 } 2377 umb_ctrl_msg(sc, MBIM_COMMAND_MSG, cmd, totlen); 2378 } 2379 2380 Static void 2381 umb_command_done(struct umb_softc *sc, void *data, int len) 2382 { 2383 struct mbim_f2h_cmddone *cmd = data; 2384 struct ifnet *ifp = GET_IFP(sc); 2385 uint32_t status; 2386 uint32_t cid; 2387 uint32_t infolen; 2388 int qmimsg = 0; 2389 2390 if (len < sizeof(*cmd)) { 2391 DPRINTF("%s: discard short %s message\n", DEVNAM(sc), 2392 umb_request2str(le32toh(cmd->hdr.type))); 2393 return; 2394 } 2395 cid = le32toh(cmd->cid); 2396 if (memcmp(cmd->devid, umb_uuid_basic_connect, sizeof(cmd->devid))) { 2397 if (memcmp(cmd->devid, umb_uuid_qmi_mbim, 2398 sizeof(cmd->devid))) { 2399 DPRINTF("%s: discard %s message for other UUID '%s'\n", 2400 DEVNAM(sc), umb_request2str(le32toh(cmd->hdr.type)), 2401 umb_uuid2str(cmd->devid)); 2402 return; 2403 } else 2404 qmimsg = 1; 2405 } 2406 2407 status = le32toh(cmd->status); 2408 switch (status) { 2409 case MBIM_STATUS_SUCCESS: 2410 break; 2411 case MBIM_STATUS_NOT_INITIALIZED: 2412 if (ifp->if_flags & IFF_DEBUG) 2413 log(LOG_ERR, "%s: SIM not initialized (PIN missing)\n", 2414 DEVNAM(sc)); 2415 return; 2416 case MBIM_STATUS_PIN_REQUIRED: 2417 sc->sc_info.pin_state = UMB_PIN_REQUIRED; 2418 /*FALLTHROUGH*/ 2419 default: 2420 if (ifp->if_flags & IFF_DEBUG) 2421 log(LOG_ERR, "%s: set/qry %s failed: %s\n", DEVNAM(sc), 2422 umb_cid2str(cid), umb_status2str(status)); 2423 return; 2424 } 2425 2426 infolen = le32toh(cmd->infolen); 2427 if (len < sizeof(*cmd) + infolen) { 2428 DPRINTF("%s: discard truncated %s message (want %d, got %d)\n", 2429 DEVNAM(sc), umb_cid2str(cid), 2430 (int)sizeof(*cmd) + infolen, len); 2431 return; 2432 } 2433 if (qmimsg) { 2434 if (sc->sc_flags & UMBFLG_FCC_AUTH_REQUIRED) 2435 umb_decode_qmi(sc, cmd->info, infolen); 2436 } else { 2437 DPRINTFN(2, "%s: set/qry %s done\n", DEVNAM(sc), 2438 umb_cid2str(cid)); 2439 umb_decode_cid(sc, cid, cmd->info, infolen); 2440 } 2441 } 2442 2443 Static void 2444 umb_decode_cid(struct umb_softc *sc, uint32_t cid, void *data, int len) 2445 { 2446 int ok = 1; 2447 2448 switch (cid) { 2449 case MBIM_CID_DEVICE_CAPS: 2450 ok = umb_decode_devices_caps(sc, data, len); 2451 break; 2452 case MBIM_CID_SUBSCRIBER_READY_STATUS: 2453 ok = umb_decode_subscriber_status(sc, data, len); 2454 break; 2455 case MBIM_CID_RADIO_STATE: 2456 ok = umb_decode_radio_state(sc, data, len); 2457 break; 2458 case MBIM_CID_PIN: 2459 ok = umb_decode_pin(sc, data, len); 2460 break; 2461 case MBIM_CID_REGISTER_STATE: 2462 ok = umb_decode_register_state(sc, data, len); 2463 break; 2464 case MBIM_CID_PACKET_SERVICE: 2465 ok = umb_decode_packet_service(sc, data, len); 2466 break; 2467 case MBIM_CID_SIGNAL_STATE: 2468 ok = umb_decode_signal_state(sc, data, len); 2469 break; 2470 case MBIM_CID_CONNECT: 2471 ok = umb_decode_connect_info(sc, data, len); 2472 break; 2473 case MBIM_CID_IP_CONFIGURATION: 2474 ok = umb_decode_ip_configuration(sc, data, len); 2475 break; 2476 default: 2477 /* 2478 * Note: the above list is incomplete and only contains 2479 * mandatory CIDs from the BASIC_CONNECT set. 2480 * So alternate values are not unusual. 2481 */ 2482 DPRINTFN(4, "%s: ignore %s\n", DEVNAM(sc), umb_cid2str(cid)); 2483 break; 2484 } 2485 if (!ok) 2486 DPRINTF("%s: discard %s with bad info length %d\n", 2487 DEVNAM(sc), umb_cid2str(cid), len); 2488 return; 2489 } 2490 2491 Static void 2492 umb_decode_qmi(struct umb_softc *sc, uint8_t *data, int len) 2493 { 2494 uint8_t srv; 2495 uint16_t msg, tlvlen; 2496 uint32_t val; 2497 2498 #define UMB_QMI_QMUXLEN 6 2499 if (len < UMB_QMI_QMUXLEN) 2500 goto tooshort; 2501 2502 srv = data[4]; 2503 data += UMB_QMI_QMUXLEN; 2504 len -= UMB_QMI_QMUXLEN; 2505 2506 #define UMB_GET16(p) ((uint16_t)*p | (uint16_t)*(p + 1) << 8) 2507 #define UMB_GET32(p) ((uint32_t)*p | (uint32_t)*(p + 1) << 8 | \ 2508 (uint32_t)*(p + 2) << 16 |(uint32_t)*(p + 3) << 24) 2509 switch (srv) { 2510 case 0: /* ctl */ 2511 #define UMB_QMI_CTLLEN 6 2512 if (len < UMB_QMI_CTLLEN) 2513 goto tooshort; 2514 msg = UMB_GET16(&data[2]); 2515 tlvlen = UMB_GET16(&data[4]); 2516 data += UMB_QMI_CTLLEN; 2517 len -= UMB_QMI_CTLLEN; 2518 break; 2519 case 2: /* dms */ 2520 #define UMB_QMI_DMSLEN 7 2521 if (len < UMB_QMI_DMSLEN) 2522 goto tooshort; 2523 msg = UMB_GET16(&data[3]); 2524 tlvlen = UMB_GET16(&data[5]); 2525 data += UMB_QMI_DMSLEN; 2526 len -= UMB_QMI_DMSLEN; 2527 break; 2528 default: 2529 DPRINTF("%s: discard QMI message for unknown service type %d\n", 2530 DEVNAM(sc), srv); 2531 return; 2532 } 2533 2534 if (len < tlvlen) 2535 goto tooshort; 2536 2537 #define UMB_QMI_TLVLEN 3 2538 while (len > 0) { 2539 if (len < UMB_QMI_TLVLEN) 2540 goto tooshort; 2541 tlvlen = UMB_GET16(&data[1]); 2542 if (len < UMB_QMI_TLVLEN + tlvlen) 2543 goto tooshort; 2544 switch (data[0]) { 2545 case 1: /* allocation info */ 2546 if (msg == 0x0022) { /* Allocate CID */ 2547 if (tlvlen != 2 || data[3] != 2) /* dms */ 2548 break; 2549 sc->sc_cid = data[4]; 2550 DPRINTF("%s: QMI CID %d allocated\n", 2551 DEVNAM(sc), sc->sc_cid); 2552 umb_newstate(sc, UMB_S_CID, UMB_NS_DONT_DROP); 2553 } 2554 break; 2555 case 2: /* response */ 2556 if (tlvlen != sizeof(val)) 2557 break; 2558 val = UMB_GET32(&data[3]); 2559 switch (msg) { 2560 case 0x0022: /* Allocate CID */ 2561 if (val != 0) { 2562 log(LOG_ERR, "%s: allocation of QMI CID" 2563 " failed, error %#x\n", DEVNAM(sc), 2564 val); 2565 /* XXX how to proceed? */ 2566 return; 2567 } 2568 break; 2569 case 0x555f: /* Send FCC Authentication */ 2570 if (val == 0) 2571 DPRINTF("%s: send FCC " 2572 "Authentication succeeded\n", 2573 DEVNAM(sc)); 2574 else if (val == 0x001a0001) 2575 DPRINTF("%s: FCC Authentication " 2576 "not required\n", DEVNAM(sc)); 2577 else 2578 log(LOG_INFO, "%s: send FCC " 2579 "Authentication failed, " 2580 "error %#x\n", DEVNAM(sc), val); 2581 2582 /* FCC Auth is needed only once after power-on*/ 2583 sc->sc_flags &= ~UMBFLG_FCC_AUTH_REQUIRED; 2584 2585 /* Try to proceed anyway */ 2586 DPRINTF("%s: init: turning radio on ...\n", 2587 DEVNAM(sc)); 2588 umb_radio(sc, 1); 2589 break; 2590 default: 2591 break; 2592 } 2593 break; 2594 default: 2595 break; 2596 } 2597 data += UMB_QMI_TLVLEN + tlvlen; 2598 len -= UMB_QMI_TLVLEN + tlvlen; 2599 } 2600 return; 2601 2602 tooshort: 2603 DPRINTF("%s: discard short QMI message\n", DEVNAM(sc)); 2604 return; 2605 } 2606 2607 Static void 2608 umb_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 2609 { 2610 struct umb_softc *sc = priv; 2611 struct ifnet *ifp = GET_IFP(sc); 2612 int total_len; 2613 2614 if (status != USBD_NORMAL_COMPLETION) { 2615 DPRINTF("%s: notification error: %s\n", DEVNAM(sc), 2616 usbd_errstr(status)); 2617 if (status == USBD_STALLED) 2618 usbd_clear_endpoint_stall_async(sc->sc_ctrl_pipe); 2619 return; 2620 } 2621 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 2622 if (total_len < UCDC_NOTIFICATION_LENGTH) { 2623 DPRINTF("%s: short notification (%d<%d)\n", DEVNAM(sc), 2624 total_len, UCDC_NOTIFICATION_LENGTH); 2625 return; 2626 } 2627 if (sc->sc_intr_msg.bmRequestType != UCDC_NOTIFICATION) { 2628 DPRINTF("%s: unexpected notification (type=0x%02x)\n", 2629 DEVNAM(sc), sc->sc_intr_msg.bmRequestType); 2630 return; 2631 } 2632 2633 switch (sc->sc_intr_msg.bNotification) { 2634 case UCDC_N_NETWORK_CONNECTION: 2635 if (ifp->if_flags & IFF_DEBUG) 2636 log(LOG_DEBUG, "%s: network %sconnected\n", DEVNAM(sc), 2637 UGETW(sc->sc_intr_msg.wValue) ? "" : "dis"); 2638 break; 2639 case UCDC_N_RESPONSE_AVAILABLE: 2640 DPRINTFN(2, "%s: umb_intr: response available\n", DEVNAM(sc)); 2641 ++sc->sc_nresp; 2642 usb_add_task(sc->sc_udev, &sc->sc_get_response_task, USB_TASKQ_DRIVER); 2643 break; 2644 case UCDC_N_CONNECTION_SPEED_CHANGE: 2645 DPRINTFN(2, "%s: umb_intr: connection speed changed\n", 2646 DEVNAM(sc)); 2647 break; 2648 default: 2649 DPRINTF("%s: unexpected notification (0x%02x)\n", 2650 DEVNAM(sc), sc->sc_intr_msg.bNotification); 2651 break; 2652 } 2653 } 2654 2655 /* 2656 * Diagnostic routines 2657 */ 2658 Static char * 2659 umb_ntop(struct sockaddr *sa) 2660 { 2661 #define NUMBUFS 4 2662 static char astr[NUMBUFS][INET_ADDRSTRLEN]; 2663 static unsigned nbuf = 0; 2664 char *s; 2665 2666 s = astr[nbuf++]; 2667 if (nbuf >= NUMBUFS) 2668 nbuf = 0; 2669 2670 switch (sa->sa_family) { 2671 case AF_INET: 2672 default: 2673 inet_ntop(AF_INET, &satosin(sa)->sin_addr, s, sizeof(astr[0])); 2674 break; 2675 case AF_INET6: 2676 inet_ntop(AF_INET6, &satosin6(sa)->sin6_addr, s, 2677 sizeof(astr[0])); 2678 break; 2679 } 2680 return s; 2681 } 2682 2683 #ifdef UMB_DEBUG 2684 Static char * 2685 umb_uuid2str(uint8_t uuid[MBIM_UUID_LEN]) 2686 { 2687 static char uuidstr[2 * MBIM_UUID_LEN + 5]; 2688 2689 #define UUID_BFMT "%02X" 2690 #define UUID_SEP "-" 2691 snprintf(uuidstr, sizeof(uuidstr), 2692 UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_SEP 2693 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_BFMT UUID_BFMT UUID_BFMT UUID_BFMT, 2697 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], 2698 uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11], 2699 uuid[12], uuid[13], uuid[14], uuid[15]); 2700 return uuidstr; 2701 } 2702 2703 Static void 2704 umb_dump(void *buf, int len) 2705 { 2706 int i = 0; 2707 uint8_t *c = buf; 2708 2709 if (len == 0) 2710 return; 2711 while (i < len) { 2712 if ((i % 16) == 0) { 2713 if (i > 0) 2714 addlog("\n"); 2715 log(LOG_DEBUG, "%4d: ", i); 2716 } 2717 addlog(" %02x", *c); 2718 c++; 2719 i++; 2720 } 2721 addlog("\n"); 2722 } 2723 #endif /* UMB_DEBUG */ 2724 2725 /* char * 2726 * inet_ntop(af, src, dst, size) 2727 * convert a network format address to presentation format. 2728 * return: 2729 * pointer to presentation format address (`dst'), or NULL (see errno). 2730 * author: 2731 * Paul Vixie, 1996. 2732 */ 2733 Static const char * 2734 inet_ntop(int af, const void *src, char *dst, socklen_t size) 2735 { 2736 switch (af) { 2737 case AF_INET: 2738 return inet_ntop4(src, dst, (size_t)size); 2739 #ifdef INET6 2740 case AF_INET6: 2741 return inet_ntop6(src, dst, (size_t)size); 2742 #endif /* INET6 */ 2743 default: 2744 return NULL; 2745 } 2746 /* NOTREACHED */ 2747 } 2748 2749 /* const char * 2750 * inet_ntop4(src, dst, size) 2751 * format an IPv4 address, more or less like inet_ntoa() 2752 * return: 2753 * `dst' (as a const) 2754 * notes: 2755 * (1) uses no statics 2756 * (2) takes a u_char* not an in_addr as input 2757 * author: 2758 * Paul Vixie, 1996. 2759 */ 2760 Static const char * 2761 inet_ntop4(const u_char *src, char *dst, size_t size) 2762 { 2763 char tmp[sizeof("255.255.255.255")]; 2764 int l; 2765 2766 l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u", 2767 src[0], src[1], src[2], src[3]); 2768 if (l <= 0 || l >= size) { 2769 return NULL; 2770 } 2771 strlcpy(dst, tmp, size); 2772 return dst; 2773 } 2774 2775 #ifdef INET6 2776 /* const char * 2777 * inet_ntop6(src, dst, size) 2778 * convert IPv6 binary address into presentation (printable) format 2779 * author: 2780 * Paul Vixie, 1996. 2781 */ 2782 Static const char * 2783 inet_ntop6(const u_char *src, char *dst, size_t size) 2784 { 2785 /* 2786 * Note that int32_t and int16_t need only be "at least" large enough 2787 * to contain a value of the specified size. On some systems, like 2788 * Crays, there is no such thing as an integer variable with 16 bits. 2789 * Keep this in mind if you think this function should have been coded 2790 * to use pointer overlays. All the world's not a VAX. 2791 */ 2792 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")]; 2793 char *tp, *ep; 2794 struct { int base, len; } best, cur; 2795 #define IN6ADDRSZ 16 2796 #define INT16SZ 2 2797 u_int words[IN6ADDRSZ / INT16SZ]; 2798 int i; 2799 int advance; 2800 2801 /* 2802 * Preprocess: 2803 * Copy the input (bytewise) array into a wordwise array. 2804 * Find the longest run of 0x00's in src[] for :: shorthanding. 2805 */ 2806 memset(words, '\0', sizeof(words)); 2807 for (i = 0; i < IN6ADDRSZ; i++) 2808 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 2809 best.base = -1; 2810 best.len = 0; 2811 cur.base = -1; 2812 cur.len = 0; 2813 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { 2814 if (words[i] == 0) { 2815 if (cur.base == -1) 2816 cur.base = i, cur.len = 1; 2817 else 2818 cur.len++; 2819 } else { 2820 if (cur.base != -1) { 2821 if (best.base == -1 || cur.len > best.len) 2822 best = cur; 2823 cur.base = -1; 2824 } 2825 } 2826 } 2827 if (cur.base != -1) { 2828 if (best.base == -1 || cur.len > best.len) 2829 best = cur; 2830 } 2831 if (best.base != -1 && best.len < 2) 2832 best.base = -1; 2833 2834 /* 2835 * Format the result. 2836 */ 2837 tp = tmp; 2838 ep = tmp + sizeof(tmp); 2839 for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { 2840 /* Are we inside the best run of 0x00's? */ 2841 if (best.base != -1 && i >= best.base && 2842 i < (best.base + best.len)) { 2843 if (i == best.base) { 2844 if (tp + 1 >= ep) 2845 return NULL; 2846 *tp++ = ':'; 2847 } 2848 continue; 2849 } 2850 /* Are we following an initial run of 0x00s or any real hex? */ 2851 if (i != 0) { 2852 if (tp + 1 >= ep) 2853 return NULL; 2854 *tp++ = ':'; 2855 } 2856 /* Is this address an encapsulated IPv4? */ 2857 if (i == 6 && best.base == 0 && 2858 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { 2859 if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) 2860 return NULL; 2861 tp += strlen(tp); 2862 break; 2863 } 2864 advance = snprintf(tp, ep - tp, "%x", words[i]); 2865 if (advance <= 0 || advance >= ep - tp) 2866 return NULL; 2867 tp += advance; 2868 } 2869 /* Was it a trailing run of 0x00's? */ 2870 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { 2871 if (tp + 1 >= ep) 2872 return NULL; 2873 *tp++ = ':'; 2874 } 2875 if (tp + 1 >= ep) 2876 return NULL; 2877 *tp++ = '\0'; 2878 2879 /* 2880 * Check for overflow, copy, and we're done. 2881 */ 2882 if ((size_t)(tp - tmp) > size) { 2883 return NULL; 2884 } 2885 strlcpy(dst, tmp, size); 2886 return dst; 2887 } 2888 #endif /* INET6 */ 2889