Home | History | Annotate | Line # | Download | only in netbt
hci_socket.c revision 1.20.18.2
      1  1.20.18.2    rmind /*	$NetBSD: hci_socket.c,v 1.20.18.2 2014/05/18 17:46:13 rmind Exp $	*/
      2        1.1  gdamore 
      3        1.1  gdamore /*-
      4        1.1  gdamore  * Copyright (c) 2005 Iain Hibbert.
      5        1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      6        1.1  gdamore  * All rights reserved.
      7        1.1  gdamore  *
      8        1.1  gdamore  * Redistribution and use in source and binary forms, with or without
      9        1.1  gdamore  * modification, are permitted provided that the following conditions
     10        1.1  gdamore  * are met:
     11        1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     12        1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     13        1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     15        1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     16        1.1  gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     17        1.1  gdamore  *    or promote products derived from this software without specific
     18        1.1  gdamore  *    prior written permission.
     19        1.1  gdamore  *
     20        1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21        1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24        1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25        1.1  gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26        1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27        1.1  gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1  gdamore  */
     32        1.1  gdamore 
     33        1.1  gdamore #include <sys/cdefs.h>
     34  1.20.18.2    rmind __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.20.18.2 2014/05/18 17:46:13 rmind Exp $");
     35        1.1  gdamore 
     36       1.10   plunky /* load symbolic names */
     37        1.1  gdamore #ifdef BLUETOOTH_DEBUG
     38       1.10   plunky #define PRUREQUESTS
     39        1.1  gdamore #define PRCOREQUESTS
     40        1.1  gdamore #endif
     41        1.1  gdamore 
     42        1.1  gdamore #include <sys/param.h>
     43        1.1  gdamore #include <sys/domain.h>
     44        1.1  gdamore #include <sys/kauth.h>
     45        1.1  gdamore #include <sys/kernel.h>
     46  1.20.18.1    rmind #include <sys/kmem.h>
     47        1.1  gdamore #include <sys/mbuf.h>
     48        1.1  gdamore #include <sys/proc.h>
     49        1.1  gdamore #include <sys/protosw.h>
     50        1.1  gdamore #include <sys/socket.h>
     51        1.1  gdamore #include <sys/socketvar.h>
     52        1.1  gdamore #include <sys/systm.h>
     53        1.1  gdamore 
     54        1.1  gdamore #include <netbt/bluetooth.h>
     55        1.1  gdamore #include <netbt/hci.h>
     56        1.1  gdamore 
     57        1.1  gdamore /*******************************************************************************
     58        1.1  gdamore  *
     59        1.1  gdamore  * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
     60        1.1  gdamore  *
     61        1.1  gdamore  */
     62        1.1  gdamore 
     63        1.1  gdamore /*
     64        1.1  gdamore  * the raw HCI protocol control block
     65        1.1  gdamore  */
     66        1.1  gdamore struct hci_pcb {
     67        1.1  gdamore 	struct socket		*hp_socket;	/* socket */
     68       1.18   plunky 	kauth_cred_t		hp_cred;	/* owner credential */
     69        1.1  gdamore 	unsigned int		hp_flags;	/* flags */
     70        1.1  gdamore 	bdaddr_t		hp_laddr;	/* local address */
     71        1.1  gdamore 	bdaddr_t		hp_raddr;	/* remote address */
     72        1.1  gdamore 	struct hci_filter	hp_efilter;	/* user event filter */
     73        1.1  gdamore 	struct hci_filter	hp_pfilter;	/* user packet filter */
     74        1.1  gdamore 	LIST_ENTRY(hci_pcb)	hp_next;	/* next HCI pcb */
     75        1.1  gdamore };
     76        1.1  gdamore 
     77        1.1  gdamore /* hp_flags */
     78        1.1  gdamore #define HCI_DIRECTION		(1<<1)	/* direction control messages */
     79        1.1  gdamore #define HCI_PROMISCUOUS		(1<<2)	/* listen to all units */
     80        1.1  gdamore 
     81        1.1  gdamore LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
     82        1.1  gdamore 
     83        1.1  gdamore /* sysctl defaults */
     84        1.1  gdamore int hci_sendspace = HCI_CMD_PKT_SIZE;
     85        1.1  gdamore int hci_recvspace = 4096;
     86        1.1  gdamore 
     87       1.18   plunky /* unprivileged commands opcode table */
     88       1.13   plunky static const struct {
     89       1.13   plunky 	uint16_t	opcode;
     90       1.13   plunky 	uint8_t		offs;	/* 0 - 63 */
     91       1.13   plunky 	uint8_t		mask;	/* bit 0 - 7 */
     92       1.18   plunky 	uint8_t		length;	/* approved length */
     93       1.13   plunky } hci_cmds[] = {
     94       1.13   plunky 	{ HCI_CMD_INQUIRY,
     95       1.13   plunky 	  0,  0x01, sizeof(hci_inquiry_cp) },
     96       1.13   plunky 	{ HCI_CMD_REMOTE_NAME_REQ,
     97       1.13   plunky 	  2,  0x08, sizeof(hci_remote_name_req_cp) },
     98       1.13   plunky 	{ HCI_CMD_READ_REMOTE_FEATURES,
     99       1.13   plunky 	  2,  0x20, sizeof(hci_read_remote_features_cp) },
    100       1.13   plunky 	{ HCI_CMD_READ_REMOTE_EXTENDED_FEATURES,
    101       1.13   plunky 	  2,  0x40, sizeof(hci_read_remote_extended_features_cp) },
    102       1.13   plunky 	{ HCI_CMD_READ_REMOTE_VER_INFO,
    103       1.13   plunky 	  2,  0x80, sizeof(hci_read_remote_ver_info_cp) },
    104       1.13   plunky 	{ HCI_CMD_READ_CLOCK_OFFSET,
    105       1.13   plunky 	  3,  0x01, sizeof(hci_read_clock_offset_cp) },
    106       1.13   plunky 	{ HCI_CMD_READ_LMP_HANDLE,
    107       1.13   plunky 	  3,  0x02, sizeof(hci_read_lmp_handle_cp) },
    108       1.13   plunky 	{ HCI_CMD_ROLE_DISCOVERY,
    109       1.13   plunky 	  4,  0x80, sizeof(hci_role_discovery_cp) },
    110       1.13   plunky 	{ HCI_CMD_READ_LINK_POLICY_SETTINGS,
    111       1.13   plunky 	  5,  0x02, sizeof(hci_read_link_policy_settings_cp) },
    112       1.13   plunky 	{ HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS,
    113       1.13   plunky 	  5,  0x08, 0 },
    114       1.13   plunky 	{ HCI_CMD_READ_PIN_TYPE,
    115       1.13   plunky 	  6,  0x04, 0 },
    116       1.13   plunky 	{ HCI_CMD_READ_LOCAL_NAME,
    117       1.13   plunky 	  7,  0x02, 0 },
    118       1.13   plunky 	{ HCI_CMD_READ_CON_ACCEPT_TIMEOUT,
    119       1.13   plunky 	  7,  0x04, 0 },
    120       1.13   plunky 	{ HCI_CMD_READ_PAGE_TIMEOUT,
    121       1.13   plunky 	  7,  0x10, 0 },
    122       1.13   plunky 	{ HCI_CMD_READ_SCAN_ENABLE,
    123       1.13   plunky 	  7,  0x40, 0 },
    124       1.13   plunky 	{ HCI_CMD_READ_PAGE_SCAN_ACTIVITY,
    125       1.13   plunky 	  8,  0x01, 0 },
    126       1.13   plunky 	{ HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY,
    127       1.13   plunky 	  8,  0x04, 0 },
    128       1.13   plunky 	{ HCI_CMD_READ_AUTH_ENABLE,
    129       1.13   plunky 	  8,  0x10, 0 },
    130       1.13   plunky 	{ HCI_CMD_READ_ENCRYPTION_MODE,
    131       1.13   plunky 	  8,  0x40, 0 },
    132       1.13   plunky 	{ HCI_CMD_READ_UNIT_CLASS,
    133       1.13   plunky 	  9,  0x01, 0 },
    134       1.13   plunky 	{ HCI_CMD_READ_VOICE_SETTING,
    135       1.13   plunky 	  9,  0x04, 0 },
    136       1.13   plunky 	{ HCI_CMD_READ_AUTO_FLUSH_TIMEOUT,
    137       1.13   plunky 	  9,  0x10, sizeof(hci_read_auto_flush_timeout_cp) },
    138       1.13   plunky 	{ HCI_CMD_READ_NUM_BROADCAST_RETRANS,
    139       1.13   plunky 	  9,  0x40, 0 },
    140       1.13   plunky 	{ HCI_CMD_READ_HOLD_MODE_ACTIVITY,
    141       1.13   plunky 	  10, 0x01, 0 },
    142       1.13   plunky 	{ HCI_CMD_READ_XMIT_LEVEL,
    143       1.13   plunky 	  10, 0x04, sizeof(hci_read_xmit_level_cp) },
    144       1.13   plunky 	{ HCI_CMD_READ_SCO_FLOW_CONTROL,
    145       1.13   plunky 	  10, 0x08, 0 },
    146       1.13   plunky 	{ HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT,
    147       1.13   plunky 	  11, 0x01, sizeof(hci_read_link_supervision_timeout_cp) },
    148       1.13   plunky 	{ HCI_CMD_READ_NUM_SUPPORTED_IAC,
    149       1.13   plunky 	  11, 0x04, 0 },
    150       1.13   plunky 	{ HCI_CMD_READ_IAC_LAP,
    151       1.13   plunky 	  11, 0x08, 0 },
    152       1.13   plunky 	{ HCI_CMD_READ_PAGE_SCAN_PERIOD,
    153       1.13   plunky 	  11, 0x20, 0 },
    154       1.13   plunky 	{ HCI_CMD_READ_PAGE_SCAN,
    155       1.13   plunky 	  11, 0x80, 0 },
    156       1.13   plunky 	{ HCI_CMD_READ_INQUIRY_SCAN_TYPE,
    157       1.13   plunky 	  12, 0x10, 0 },
    158       1.13   plunky 	{ HCI_CMD_READ_INQUIRY_MODE,
    159       1.13   plunky 	  12, 0x40, 0 },
    160       1.13   plunky 	{ HCI_CMD_READ_PAGE_SCAN_TYPE,
    161       1.13   plunky 	  13, 0x01, 0 },
    162       1.13   plunky 	{ HCI_CMD_READ_AFH_ASSESSMENT,
    163       1.13   plunky 	  13, 0x04, 0 },
    164       1.13   plunky 	{ HCI_CMD_READ_LOCAL_VER,
    165       1.13   plunky 	  14, 0x08, 0 },
    166       1.13   plunky 	{ HCI_CMD_READ_LOCAL_COMMANDS,
    167       1.13   plunky 	  14, 0x10, 0 },
    168       1.13   plunky 	{ HCI_CMD_READ_LOCAL_FEATURES,
    169       1.13   plunky 	  14, 0x20, 0 },
    170       1.13   plunky 	{ HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
    171       1.13   plunky 	  14, 0x40, sizeof(hci_read_local_extended_features_cp) },
    172       1.13   plunky 	{ HCI_CMD_READ_BUFFER_SIZE,
    173       1.13   plunky 	  14, 0x80, 0 },
    174       1.13   plunky 	{ HCI_CMD_READ_COUNTRY_CODE,
    175       1.13   plunky 	  15, 0x01, 0 },
    176       1.13   plunky 	{ HCI_CMD_READ_BDADDR,
    177       1.13   plunky 	  15, 0x02, 0 },
    178       1.13   plunky 	{ HCI_CMD_READ_FAILED_CONTACT_CNTR,
    179       1.13   plunky 	  15, 0x04, sizeof(hci_read_failed_contact_cntr_cp) },
    180       1.13   plunky 	{ HCI_CMD_READ_LINK_QUALITY,
    181       1.13   plunky 	  15, 0x10, sizeof(hci_read_link_quality_cp) },
    182       1.13   plunky 	{ HCI_CMD_READ_RSSI,
    183       1.13   plunky 	  15, 0x20, sizeof(hci_read_rssi_cp) },
    184       1.13   plunky 	{ HCI_CMD_READ_AFH_CHANNEL_MAP,
    185       1.13   plunky 	  15, 0x40, sizeof(hci_read_afh_channel_map_cp) },
    186       1.13   plunky 	{ HCI_CMD_READ_CLOCK,
    187       1.13   plunky 	  15, 0x80, sizeof(hci_read_clock_cp) },
    188       1.13   plunky 	{ HCI_CMD_READ_LOOPBACK_MODE,
    189       1.13   plunky 	  16, 0x01, 0 },
    190       1.14   plunky 	{ HCI_CMD_READ_EXTENDED_INQUIRY_RSP,
    191       1.14   plunky 	  17, 0x01, 0 },
    192       1.14   plunky 	{ HCI_CMD_READ_SIMPLE_PAIRING_MODE,
    193       1.14   plunky 	  17, 0x20, 0 },
    194       1.14   plunky 	{ HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER,
    195       1.14   plunky 	  18, 0x01, 0 },
    196       1.14   plunky 	{ HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING,
    197       1.14   plunky 	  18, 0x04, 0 },
    198       1.13   plunky };
    199       1.13   plunky 
    200        1.1  gdamore /*
    201       1.18   plunky  * supply a basic device send/recv policy
    202        1.1  gdamore  */
    203        1.4   plunky static int
    204       1.18   plunky hci_device_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    205       1.18   plunky     void *arg0, void *arg1, void *arg2, void *arg3)
    206        1.1  gdamore {
    207       1.18   plunky 	int i, result;
    208       1.18   plunky 
    209       1.18   plunky 	result = KAUTH_RESULT_DEFER;
    210       1.18   plunky 
    211       1.18   plunky 	switch (action) {
    212       1.19   plunky 	case KAUTH_DEVICE_BLUETOOTH_SEND: {
    213       1.18   plunky 		struct hci_unit *unit = (struct hci_unit *)arg0;
    214       1.18   plunky 		hci_cmd_hdr_t *hdr = (hci_cmd_hdr_t *)arg1;
    215       1.18   plunky 
    216       1.18   plunky 		/*
    217       1.18   plunky 		 * Allow sending unprivileged commands if the packet size
    218       1.18   plunky 		 * is correct and the unit claims to support it
    219       1.18   plunky 		 */
    220       1.18   plunky 
    221       1.19   plunky 		if (hdr->type != HCI_CMD_PKT)
    222       1.19   plunky 			break;
    223       1.19   plunky 
    224       1.18   plunky 		for (i = 0; i < __arraycount(hci_cmds); i++) {
    225       1.18   plunky 			if (hdr->opcode == hci_cmds[i].opcode
    226       1.18   plunky 			    && hdr->length == hci_cmds[i].length
    227       1.18   plunky 			    && (unit->hci_cmds[hci_cmds[i].offs] & hci_cmds[i].mask)) {
    228       1.18   plunky 				result = KAUTH_RESULT_ALLOW;
    229       1.18   plunky 				break;
    230       1.18   plunky 			}
    231       1.18   plunky 		}
    232       1.18   plunky 
    233       1.18   plunky 		break;
    234       1.18   plunky 		}
    235       1.18   plunky 
    236       1.19   plunky 	case KAUTH_DEVICE_BLUETOOTH_RECV:
    237       1.19   plunky 		switch((uint8_t)(uintptr_t)arg0) {
    238       1.19   plunky 		case HCI_CMD_PKT: {
    239       1.19   plunky 			uint16_t opcode = (uint16_t)(uintptr_t)arg1;
    240       1.19   plunky 
    241       1.19   plunky 			/*
    242       1.19   plunky 			 * Allow to see any unprivileged command packet
    243       1.19   plunky 			 */
    244       1.19   plunky 
    245       1.19   plunky 			for (i = 0; i < __arraycount(hci_cmds); i++) {
    246       1.19   plunky 				if (opcode == hci_cmds[i].opcode) {
    247       1.19   plunky 					result = KAUTH_RESULT_ALLOW;
    248       1.19   plunky 					break;
    249       1.19   plunky 				}
    250       1.19   plunky 			}
    251       1.19   plunky 
    252       1.19   plunky 			break;
    253       1.19   plunky 			}
    254       1.19   plunky 
    255       1.19   plunky 		case HCI_EVENT_PKT: {
    256       1.19   plunky 			uint8_t event = (uint8_t)(uintptr_t)arg1;
    257       1.18   plunky 
    258       1.19   plunky 			/*
    259       1.19   plunky 			 * Allow to receive most events
    260       1.19   plunky 			 */
    261       1.19   plunky 
    262       1.19   plunky 			switch (event) {
    263       1.19   plunky 			case HCI_EVENT_RETURN_LINK_KEYS:
    264       1.19   plunky 			case HCI_EVENT_LINK_KEY_NOTIFICATION:
    265       1.19   plunky 			case HCI_EVENT_USER_CONFIRM_REQ:
    266       1.19   plunky 			case HCI_EVENT_USER_PASSKEY_NOTIFICATION:
    267       1.19   plunky 			case HCI_EVENT_VENDOR:
    268       1.19   plunky 				break;
    269       1.18   plunky 
    270       1.19   plunky 			default:
    271       1.18   plunky 				result = KAUTH_RESULT_ALLOW;
    272       1.18   plunky 				break;
    273       1.18   plunky 			}
    274       1.18   plunky 
    275       1.19   plunky 		    	break;
    276       1.19   plunky 			}
    277       1.18   plunky 
    278       1.19   plunky 		case HCI_ACL_DATA_PKT:
    279       1.19   plunky 		case HCI_SCO_DATA_PKT: {
    280       1.19   plunky 			/* uint16_t handle = (uint16_t)(uintptr_t)arg1; */
    281       1.19   plunky 			/*
    282       1.19   plunky 			 * don't normally allow receiving data packets
    283       1.19   plunky 			 */
    284       1.18   plunky 			break;
    285       1.19   plunky 			}
    286       1.18   plunky 
    287       1.18   plunky 		default:
    288       1.18   plunky 			break;
    289       1.18   plunky 		}
    290       1.13   plunky 
    291       1.18   plunky 		break;
    292       1.18   plunky 
    293       1.18   plunky 	default:
    294       1.13   plunky 		break;
    295        1.4   plunky 	}
    296        1.1  gdamore 
    297       1.18   plunky 	return result;
    298        1.1  gdamore }
    299        1.1  gdamore 
    300       1.18   plunky /*
    301       1.18   plunky  * HCI protocol init routine,
    302       1.18   plunky  * - set up a kauth listener to provide basic packet access policy
    303       1.18   plunky  */
    304       1.18   plunky void
    305       1.18   plunky hci_init(void)
    306        1.1  gdamore {
    307        1.1  gdamore 
    308       1.18   plunky 	if (kauth_listen_scope(KAUTH_SCOPE_DEVICE, hci_device_cb, NULL) == NULL)
    309       1.18   plunky 		panic("Bluetooth HCI: cannot listen on device scope");
    310        1.1  gdamore }
    311        1.1  gdamore 
    312        1.1  gdamore /*
    313        1.1  gdamore  * When command packet reaches the device, we can drop
    314        1.1  gdamore  * it from the socket buffer (called from hci_output_acl)
    315        1.1  gdamore  */
    316        1.1  gdamore void
    317        1.1  gdamore hci_drop(void *arg)
    318        1.1  gdamore {
    319        1.1  gdamore 	struct socket *so = arg;
    320        1.1  gdamore 
    321        1.1  gdamore 	sbdroprecord(&so->so_snd);
    322        1.1  gdamore 	sowwakeup(so);
    323        1.1  gdamore }
    324        1.1  gdamore 
    325        1.1  gdamore /*
    326        1.1  gdamore  * HCI socket is going away and has some pending packets. We let them
    327        1.1  gdamore  * go by design, but remove the context pointer as it will be invalid
    328        1.1  gdamore  * and we no longer need to be notified.
    329        1.1  gdamore  */
    330        1.1  gdamore static void
    331        1.1  gdamore hci_cmdwait_flush(struct socket *so)
    332        1.1  gdamore {
    333        1.1  gdamore 	struct hci_unit *unit;
    334        1.1  gdamore 	struct socket *ctx;
    335        1.1  gdamore 	struct mbuf *m;
    336        1.1  gdamore 
    337        1.1  gdamore 	DPRINTF("flushing %p\n", so);
    338        1.1  gdamore 
    339        1.1  gdamore 	SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
    340        1.1  gdamore 		m = MBUFQ_FIRST(&unit->hci_cmdwait);
    341        1.1  gdamore 		while (m != NULL) {
    342        1.1  gdamore 			ctx = M_GETCTX(m, struct socket *);
    343        1.1  gdamore 			if (ctx == so)
    344        1.1  gdamore 				M_SETCTX(m, NULL);
    345        1.1  gdamore 
    346        1.1  gdamore 			m = MBUFQ_NEXT(m);
    347        1.1  gdamore 		}
    348        1.1  gdamore 	}
    349        1.1  gdamore }
    350        1.1  gdamore 
    351        1.1  gdamore /*
    352        1.1  gdamore  * HCI send packet
    353        1.1  gdamore  *     This came from userland, so check it out.
    354        1.1  gdamore  */
    355        1.1  gdamore static int
    356        1.1  gdamore hci_send(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr)
    357        1.1  gdamore {
    358        1.1  gdamore 	struct hci_unit *unit;
    359        1.1  gdamore 	struct mbuf *m0;
    360        1.1  gdamore 	hci_cmd_hdr_t hdr;
    361        1.1  gdamore 	int err;
    362        1.1  gdamore 
    363        1.9   plunky 	KASSERT(m != NULL);
    364        1.9   plunky 	KASSERT(addr != NULL);
    365        1.1  gdamore 
    366        1.1  gdamore 	/* wants at least a header to start with */
    367        1.1  gdamore 	if (m->m_pkthdr.len < sizeof(hdr)) {
    368        1.1  gdamore 		err = EMSGSIZE;
    369        1.1  gdamore 		goto bad;
    370        1.1  gdamore 	}
    371        1.1  gdamore 	m_copydata(m, 0, sizeof(hdr), &hdr);
    372       1.13   plunky 	hdr.opcode = le16toh(hdr.opcode);
    373        1.1  gdamore 
    374        1.1  gdamore 	/* only allows CMD packets to be sent */
    375        1.1  gdamore 	if (hdr.type != HCI_CMD_PKT) {
    376        1.1  gdamore 		err = EINVAL;
    377        1.1  gdamore 		goto bad;
    378        1.1  gdamore 	}
    379        1.1  gdamore 
    380        1.1  gdamore 	/* validates packet length */
    381        1.1  gdamore 	if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
    382        1.1  gdamore 		err = EMSGSIZE;
    383        1.1  gdamore 		goto bad;
    384        1.1  gdamore 	}
    385        1.1  gdamore 
    386        1.1  gdamore 	/* finds destination */
    387        1.1  gdamore 	unit = hci_unit_lookup(addr);
    388        1.1  gdamore 	if (unit == NULL) {
    389        1.1  gdamore 		err = ENETDOWN;
    390        1.1  gdamore 		goto bad;
    391        1.1  gdamore 	}
    392        1.1  gdamore 
    393       1.13   plunky 	/* security checks for unprivileged users */
    394       1.18   plunky 	if (pcb->hp_cred != NULL
    395       1.18   plunky 	    && kauth_authorize_device(pcb->hp_cred,
    396       1.19   plunky 	    KAUTH_DEVICE_BLUETOOTH_SEND,
    397       1.18   plunky 	    unit, &hdr, NULL, NULL) != 0) {
    398       1.13   plunky 		err = EPERM;
    399       1.13   plunky 		goto bad;
    400       1.13   plunky 	}
    401       1.13   plunky 
    402        1.1  gdamore 	/* makess a copy for precious to keep */
    403        1.1  gdamore 	m0 = m_copypacket(m, M_DONTWAIT);
    404        1.1  gdamore 	if (m0 == NULL) {
    405        1.1  gdamore 		err = ENOMEM;
    406        1.1  gdamore 		goto bad;
    407        1.1  gdamore 	}
    408        1.1  gdamore 	sbappendrecord(&pcb->hp_socket->so_snd, m0);
    409        1.1  gdamore 	M_SETCTX(m, pcb->hp_socket);	/* enable drop callback */
    410        1.1  gdamore 
    411       1.12   plunky 	DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", device_xname(unit->hci_dev),
    412       1.13   plunky 		HCI_OGF(hdr.opcode), HCI_OCF(hdr.opcode));
    413        1.1  gdamore 
    414        1.1  gdamore 	/* Sendss it */
    415        1.1  gdamore 	if (unit->hci_num_cmd_pkts == 0)
    416        1.1  gdamore 		MBUFQ_ENQUEUE(&unit->hci_cmdwait, m);
    417        1.1  gdamore 	else
    418        1.1  gdamore 		hci_output_cmd(unit, m);
    419        1.1  gdamore 
    420        1.1  gdamore 	return 0;
    421        1.1  gdamore 
    422        1.1  gdamore bad:
    423        1.1  gdamore 	DPRINTF("packet (%d bytes) not sent (error %d)\n",
    424        1.1  gdamore 			m->m_pkthdr.len, err);
    425        1.1  gdamore 	if (m) m_freem(m);
    426        1.1  gdamore 	return err;
    427        1.1  gdamore }
    428        1.1  gdamore 
    429  1.20.18.1    rmind static int
    430  1.20.18.1    rmind hci_attach1(struct socket *so, int proto)
    431  1.20.18.1    rmind {
    432  1.20.18.1    rmind 	struct hci_pcb *pcb;
    433  1.20.18.1    rmind 	int error;
    434  1.20.18.1    rmind 
    435  1.20.18.1    rmind 	KASSERT(so->so_pcb == NULL);
    436  1.20.18.1    rmind 
    437  1.20.18.1    rmind 	if (so->so_lock == NULL) {
    438  1.20.18.1    rmind 		mutex_obj_hold(bt_lock);
    439  1.20.18.1    rmind 		so->so_lock = bt_lock;
    440  1.20.18.1    rmind 	}
    441  1.20.18.1    rmind 	error = soreserve(so, hci_sendspace, hci_recvspace);
    442  1.20.18.1    rmind 	if (error) {
    443  1.20.18.1    rmind 		return error;
    444  1.20.18.1    rmind 	}
    445  1.20.18.1    rmind 
    446  1.20.18.1    rmind 	pcb = kmem_zalloc(sizeof(struct hci_pcb), KM_SLEEP);
    447  1.20.18.1    rmind 	pcb->hp_cred = kauth_cred_dup(curlwp->l_cred);
    448  1.20.18.1    rmind 	pcb->hp_socket = so;
    449  1.20.18.1    rmind 
    450  1.20.18.1    rmind 	/*
    451  1.20.18.1    rmind 	 * Set default user filter. By default, socket only passes
    452  1.20.18.1    rmind 	 * Command_Complete and Command_Status Events.
    453  1.20.18.1    rmind 	 */
    454  1.20.18.1    rmind 	hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
    455  1.20.18.1    rmind 	hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
    456  1.20.18.1    rmind 	hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
    457  1.20.18.1    rmind 
    458  1.20.18.1    rmind 	LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
    459  1.20.18.1    rmind 	so->so_pcb = pcb;
    460  1.20.18.1    rmind 
    461  1.20.18.1    rmind 	return 0;
    462  1.20.18.1    rmind }
    463  1.20.18.1    rmind 
    464  1.20.18.1    rmind static void
    465  1.20.18.1    rmind hci_detach1(struct socket *so)
    466  1.20.18.1    rmind {
    467  1.20.18.1    rmind 	struct hci_pcb *pcb;
    468  1.20.18.1    rmind 
    469  1.20.18.1    rmind 	pcb = (struct hci_pcb *)so->so_pcb;
    470  1.20.18.1    rmind 	KASSERT(pcb != NULL);
    471  1.20.18.1    rmind 
    472  1.20.18.1    rmind 	if (so->so_snd.sb_mb != NULL)
    473  1.20.18.1    rmind 		hci_cmdwait_flush(so);
    474  1.20.18.1    rmind 
    475  1.20.18.1    rmind 	if (pcb->hp_cred != NULL)
    476  1.20.18.1    rmind 		kauth_cred_free(pcb->hp_cred);
    477  1.20.18.1    rmind 
    478  1.20.18.1    rmind 	so->so_pcb = NULL;
    479  1.20.18.1    rmind 	LIST_REMOVE(pcb, hp_next);
    480  1.20.18.1    rmind 	kmem_free(pcb, sizeof(*pcb));
    481  1.20.18.1    rmind }
    482  1.20.18.1    rmind 
    483        1.1  gdamore /*
    484        1.1  gdamore  * User Request.
    485        1.1  gdamore  * up is socket
    486        1.1  gdamore  * m is either
    487        1.1  gdamore  *	optional mbuf chain containing message
    488        1.1  gdamore  *	ioctl command (PRU_CONTROL)
    489        1.1  gdamore  * nam is either
    490        1.1  gdamore  *	optional mbuf chain containing an address
    491        1.1  gdamore  *	ioctl data (PRU_CONTROL)
    492        1.1  gdamore  * ctl is optional mbuf chain containing socket options
    493        1.1  gdamore  * l is pointer to process requesting action (if any)
    494        1.1  gdamore  *
    495        1.1  gdamore  * we are responsible for disposing of m and ctl if
    496        1.1  gdamore  * they are mbuf chains
    497        1.1  gdamore  */
    498  1.20.18.1    rmind static int
    499        1.1  gdamore hci_usrreq(struct socket *up, int req, struct mbuf *m,
    500        1.1  gdamore 		struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    501        1.1  gdamore {
    502        1.1  gdamore 	struct hci_pcb *pcb = (struct hci_pcb *)up->so_pcb;
    503        1.1  gdamore 	struct sockaddr_bt *sa;
    504        1.4   plunky 	int err = 0;
    505        1.1  gdamore 
    506        1.1  gdamore 	DPRINTFN(2, "%s\n", prurequests[req]);
    507  1.20.18.1    rmind 	KASSERT(req != PRU_ATTACH);
    508  1.20.18.1    rmind 	KASSERT(req != PRU_DETACH);
    509        1.1  gdamore 
    510        1.1  gdamore 	switch(req) {
    511        1.1  gdamore 	case PRU_CONTROL:
    512       1.16       ad 		mutex_enter(bt_lock);
    513       1.16       ad 		err = hci_ioctl((unsigned long)m, (void *)nam, l);
    514       1.16       ad 		mutex_exit(bt_lock);
    515       1.16       ad 		return err;
    516        1.1  gdamore 
    517        1.1  gdamore 	case PRU_PURGEIF:
    518        1.1  gdamore 		return EOPNOTSUPP;
    519        1.1  gdamore 	}
    520        1.1  gdamore 
    521        1.1  gdamore 	/* anything after here *requires* a pcb */
    522        1.1  gdamore 	if (pcb == NULL) {
    523        1.1  gdamore 		err = EINVAL;
    524        1.1  gdamore 		goto release;
    525        1.1  gdamore 	}
    526        1.1  gdamore 
    527        1.1  gdamore 	switch(req) {
    528        1.1  gdamore 	case PRU_DISCONNECT:
    529        1.1  gdamore 		bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
    530        1.1  gdamore 
    531        1.1  gdamore 		/* XXX we cannot call soisdisconnected() here, as it sets
    532        1.1  gdamore 		 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being,
    533        1.1  gdamore 		 * that soisconnected() does not clear these and if you
    534        1.1  gdamore 		 * try to reconnect this socket (which is permitted) you
    535        1.1  gdamore 		 * get a broken pipe when you try to write any data.
    536        1.1  gdamore 		 */
    537        1.1  gdamore 		up->so_state &= ~SS_ISCONNECTED;
    538        1.1  gdamore 		break;
    539        1.1  gdamore 
    540        1.1  gdamore 	case PRU_ABORT:
    541        1.1  gdamore 		soisdisconnected(up);
    542  1.20.18.1    rmind 		hci_detach1(up);
    543        1.1  gdamore 		return 0;
    544        1.1  gdamore 
    545        1.1  gdamore 	case PRU_BIND:
    546        1.9   plunky 		KASSERT(nam != NULL);
    547        1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    548        1.1  gdamore 
    549        1.1  gdamore 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    550        1.1  gdamore 			return EINVAL;
    551        1.1  gdamore 
    552        1.1  gdamore 		if (sa->bt_family != AF_BLUETOOTH)
    553        1.1  gdamore 			return EAFNOSUPPORT;
    554        1.1  gdamore 
    555        1.1  gdamore 		bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
    556        1.1  gdamore 
    557        1.1  gdamore 		if (bdaddr_any(&sa->bt_bdaddr))
    558        1.1  gdamore 			pcb->hp_flags |= HCI_PROMISCUOUS;
    559        1.1  gdamore 		else
    560        1.1  gdamore 			pcb->hp_flags &= ~HCI_PROMISCUOUS;
    561        1.1  gdamore 
    562        1.1  gdamore 		return 0;
    563        1.1  gdamore 
    564        1.1  gdamore 	case PRU_CONNECT:
    565        1.9   plunky 		KASSERT(nam != NULL);
    566        1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    567        1.1  gdamore 
    568        1.1  gdamore 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    569        1.1  gdamore 			return EINVAL;
    570        1.1  gdamore 
    571        1.1  gdamore 		if (sa->bt_family != AF_BLUETOOTH)
    572        1.1  gdamore 			return EAFNOSUPPORT;
    573        1.1  gdamore 
    574        1.1  gdamore 		if (hci_unit_lookup(&sa->bt_bdaddr) == NULL)
    575        1.1  gdamore 			return EADDRNOTAVAIL;
    576        1.1  gdamore 
    577        1.1  gdamore 		bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
    578        1.1  gdamore 		soisconnected(up);
    579        1.1  gdamore 		return 0;
    580        1.1  gdamore 
    581        1.1  gdamore 	case PRU_PEERADDR:
    582        1.9   plunky 		KASSERT(nam != NULL);
    583        1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    584        1.1  gdamore 
    585        1.1  gdamore 		memset(sa, 0, sizeof(struct sockaddr_bt));
    586        1.1  gdamore 		nam->m_len =
    587        1.1  gdamore 		sa->bt_len = sizeof(struct sockaddr_bt);
    588        1.1  gdamore 		sa->bt_family = AF_BLUETOOTH;
    589        1.1  gdamore 		bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
    590        1.1  gdamore 		return 0;
    591        1.1  gdamore 
    592        1.1  gdamore 	case PRU_SOCKADDR:
    593        1.9   plunky 		KASSERT(nam != NULL);
    594        1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    595        1.1  gdamore 
    596        1.1  gdamore 		memset(sa, 0, sizeof(struct sockaddr_bt));
    597        1.1  gdamore 		nam->m_len =
    598        1.1  gdamore 		sa->bt_len = sizeof(struct sockaddr_bt);
    599        1.1  gdamore 		sa->bt_family = AF_BLUETOOTH;
    600        1.1  gdamore 		bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
    601        1.1  gdamore 		return 0;
    602        1.1  gdamore 
    603        1.1  gdamore 	case PRU_SHUTDOWN:
    604        1.1  gdamore 		socantsendmore(up);
    605        1.1  gdamore 		break;
    606        1.1  gdamore 
    607        1.1  gdamore 	case PRU_SEND:
    608        1.1  gdamore 		sa = NULL;
    609        1.1  gdamore 		if (nam) {
    610        1.1  gdamore 			sa = mtod(nam, struct sockaddr_bt *);
    611        1.1  gdamore 
    612        1.1  gdamore 			if (sa->bt_len != sizeof(struct sockaddr_bt)) {
    613        1.1  gdamore 				err = EINVAL;
    614        1.1  gdamore 				goto release;
    615        1.1  gdamore 			}
    616        1.1  gdamore 
    617        1.1  gdamore 			if (sa->bt_family != AF_BLUETOOTH) {
    618        1.1  gdamore 				err = EAFNOSUPPORT;
    619        1.1  gdamore 				goto release;
    620        1.1  gdamore 			}
    621        1.1  gdamore 		}
    622        1.1  gdamore 
    623        1.1  gdamore 		if (ctl) /* have no use for this */
    624        1.1  gdamore 			m_freem(ctl);
    625        1.1  gdamore 
    626        1.1  gdamore 		return hci_send(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
    627        1.1  gdamore 
    628        1.1  gdamore 	case PRU_SENSE:
    629        1.1  gdamore 		return 0;		/* (no sense - Doh!) */
    630        1.1  gdamore 
    631        1.1  gdamore 	case PRU_RCVD:
    632        1.1  gdamore 	case PRU_RCVOOB:
    633        1.1  gdamore 		return EOPNOTSUPP;	/* (no release) */
    634        1.1  gdamore 
    635        1.1  gdamore 	case PRU_ACCEPT:
    636        1.1  gdamore 	case PRU_CONNECT2:
    637        1.1  gdamore 	case PRU_LISTEN:
    638        1.1  gdamore 	case PRU_SENDOOB:
    639        1.1  gdamore 	case PRU_FASTTIMO:
    640        1.1  gdamore 	case PRU_SLOWTIMO:
    641        1.1  gdamore 	case PRU_PROTORCV:
    642        1.1  gdamore 	case PRU_PROTOSEND:
    643        1.1  gdamore 		err = EOPNOTSUPP;
    644        1.1  gdamore 		break;
    645        1.1  gdamore 
    646        1.1  gdamore 	default:
    647        1.1  gdamore 		UNKNOWN(req);
    648        1.1  gdamore 		err = EOPNOTSUPP;
    649        1.1  gdamore 		break;
    650        1.1  gdamore 	}
    651        1.1  gdamore 
    652        1.1  gdamore release:
    653        1.2       ad 	if (m)
    654        1.2       ad 		m_freem(m);
    655        1.2       ad 	if (ctl)
    656        1.2       ad 		m_freem(ctl);
    657        1.1  gdamore 	return err;
    658        1.1  gdamore }
    659        1.1  gdamore 
    660        1.1  gdamore /*
    661        1.1  gdamore  * get/set socket options
    662        1.1  gdamore  */
    663        1.1  gdamore int
    664       1.17   plunky hci_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    665        1.1  gdamore {
    666        1.1  gdamore 	struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
    667       1.17   plunky 	int optval, err = 0;
    668        1.1  gdamore 
    669        1.1  gdamore 	DPRINTFN(2, "req %s\n", prcorequests[req]);
    670        1.1  gdamore 
    671        1.1  gdamore 	if (pcb == NULL)
    672        1.1  gdamore 		return EINVAL;
    673        1.1  gdamore 
    674       1.17   plunky 	if (sopt->sopt_level != BTPROTO_HCI)
    675        1.7   plunky 		return ENOPROTOOPT;
    676        1.1  gdamore 
    677        1.1  gdamore 	switch(req) {
    678        1.1  gdamore 	case PRCO_GETOPT:
    679       1.17   plunky 		switch (sopt->sopt_name) {
    680        1.1  gdamore 		case SO_HCI_EVT_FILTER:
    681       1.17   plunky 			err = sockopt_set(sopt, &pcb->hp_efilter,
    682       1.17   plunky 			    sizeof(struct hci_filter));
    683       1.17   plunky 
    684        1.1  gdamore 			break;
    685        1.1  gdamore 
    686        1.1  gdamore 		case SO_HCI_PKT_FILTER:
    687       1.17   plunky 			err = sockopt_set(sopt, &pcb->hp_pfilter,
    688       1.17   plunky 			    sizeof(struct hci_filter));
    689       1.17   plunky 
    690        1.1  gdamore 			break;
    691        1.1  gdamore 
    692        1.1  gdamore 		case SO_HCI_DIRECTION:
    693       1.17   plunky 			err = sockopt_setint(sopt,
    694       1.17   plunky 			    (pcb->hp_flags & HCI_DIRECTION ? 1 : 0));
    695       1.17   plunky 
    696        1.1  gdamore 			break;
    697        1.1  gdamore 
    698        1.1  gdamore 		default:
    699        1.7   plunky 			err = ENOPROTOOPT;
    700        1.1  gdamore 			break;
    701        1.1  gdamore 		}
    702        1.1  gdamore 		break;
    703        1.1  gdamore 
    704        1.1  gdamore 	case PRCO_SETOPT:
    705       1.17   plunky 		switch (sopt->sopt_name) {
    706        1.1  gdamore 		case SO_HCI_EVT_FILTER:	/* set event filter */
    707       1.17   plunky 			err = sockopt_get(sopt, &pcb->hp_efilter,
    708       1.17   plunky 			    sizeof(pcb->hp_efilter));
    709       1.17   plunky 
    710        1.1  gdamore 			break;
    711        1.1  gdamore 
    712        1.1  gdamore 		case SO_HCI_PKT_FILTER:	/* set packet filter */
    713       1.17   plunky 			err = sockopt_get(sopt, &pcb->hp_pfilter,
    714       1.17   plunky 			    sizeof(pcb->hp_pfilter));
    715       1.17   plunky 
    716        1.1  gdamore 			break;
    717        1.1  gdamore 
    718        1.1  gdamore 		case SO_HCI_DIRECTION:	/* request direction ctl messages */
    719       1.17   plunky 			err = sockopt_getint(sopt, &optval);
    720       1.17   plunky 			if (err)
    721       1.17   plunky 				break;
    722       1.17   plunky 
    723       1.17   plunky 			if (optval)
    724        1.1  gdamore 				pcb->hp_flags |= HCI_DIRECTION;
    725        1.1  gdamore 			else
    726        1.1  gdamore 				pcb->hp_flags &= ~HCI_DIRECTION;
    727        1.1  gdamore 			break;
    728        1.1  gdamore 
    729        1.1  gdamore 		default:
    730        1.7   plunky 			err = ENOPROTOOPT;
    731        1.1  gdamore 			break;
    732        1.1  gdamore 		}
    733        1.1  gdamore 		break;
    734        1.1  gdamore 
    735        1.1  gdamore 	default:
    736        1.7   plunky 		err = ENOPROTOOPT;
    737        1.1  gdamore 		break;
    738        1.1  gdamore 	}
    739        1.1  gdamore 
    740        1.1  gdamore 	return err;
    741        1.1  gdamore }
    742        1.1  gdamore 
    743        1.1  gdamore /*
    744        1.1  gdamore  * HCI mbuf tap routine
    745        1.1  gdamore  *
    746        1.1  gdamore  * copy packets to any raw HCI sockets that wish (and are
    747        1.1  gdamore  * permitted) to see them
    748        1.1  gdamore  */
    749        1.1  gdamore void
    750        1.1  gdamore hci_mtap(struct mbuf *m, struct hci_unit *unit)
    751        1.1  gdamore {
    752        1.1  gdamore 	struct hci_pcb *pcb;
    753        1.1  gdamore 	struct mbuf *m0, *ctlmsg, **ctl;
    754        1.1  gdamore 	struct sockaddr_bt sa;
    755        1.1  gdamore 	uint8_t type;
    756        1.1  gdamore 	uint8_t event;
    757       1.19   plunky 	uint16_t arg1;
    758        1.1  gdamore 
    759        1.1  gdamore 	KASSERT(m->m_len >= sizeof(type));
    760        1.1  gdamore 
    761        1.1  gdamore 	type = *mtod(m, uint8_t *);
    762        1.1  gdamore 
    763        1.1  gdamore 	memset(&sa, 0, sizeof(sa));
    764        1.1  gdamore 	sa.bt_len = sizeof(struct sockaddr_bt);
    765        1.1  gdamore 	sa.bt_family = AF_BLUETOOTH;
    766        1.1  gdamore 	bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
    767        1.1  gdamore 
    768        1.1  gdamore 	LIST_FOREACH(pcb, &hci_pcb, hp_next) {
    769        1.1  gdamore 		/*
    770        1.1  gdamore 		 * filter according to source address
    771        1.1  gdamore 		 */
    772        1.1  gdamore 		if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
    773        1.1  gdamore 		    && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
    774        1.1  gdamore 			continue;
    775        1.1  gdamore 
    776        1.1  gdamore 		/*
    777        1.1  gdamore 		 * filter according to packet type filter
    778        1.1  gdamore 		 */
    779        1.1  gdamore 		if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
    780        1.1  gdamore 			continue;
    781        1.1  gdamore 
    782        1.1  gdamore 		/*
    783        1.1  gdamore 		 * filter according to event/security filters
    784        1.1  gdamore 		 */
    785        1.1  gdamore 		switch(type) {
    786        1.1  gdamore 		case HCI_EVENT_PKT:
    787        1.1  gdamore 			KASSERT(m->m_len >= sizeof(hci_event_hdr_t));
    788        1.1  gdamore 
    789        1.1  gdamore 			event = mtod(m, hci_event_hdr_t *)->event;
    790        1.1  gdamore 
    791        1.1  gdamore 			if (hci_filter_test(event, &pcb->hp_efilter) == 0)
    792        1.1  gdamore 				continue;
    793        1.1  gdamore 
    794       1.19   plunky 			arg1 = event;
    795        1.1  gdamore 			break;
    796        1.1  gdamore 
    797        1.1  gdamore 		case HCI_CMD_PKT:
    798        1.1  gdamore 			KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
    799       1.19   plunky 			arg1 = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode);
    800       1.19   plunky 			break;
    801        1.1  gdamore 
    802       1.19   plunky 		case HCI_ACL_DATA_PKT:
    803       1.19   plunky 			KASSERT(m->m_len >= sizeof(hci_acldata_hdr_t));
    804       1.19   plunky 			arg1 = le16toh(mtod(m, hci_acldata_hdr_t *)->con_handle);
    805       1.19   plunky 			arg1 = HCI_CON_HANDLE(arg1);
    806       1.19   plunky 			break;
    807       1.18   plunky 
    808       1.19   plunky 		case HCI_SCO_DATA_PKT:
    809       1.19   plunky 			KASSERT(m->m_len >= sizeof(hci_scodata_hdr_t));
    810       1.19   plunky 			arg1 = le16toh(mtod(m, hci_scodata_hdr_t *)->con_handle);
    811       1.19   plunky 			arg1 = HCI_CON_HANDLE(arg1);
    812        1.1  gdamore 			break;
    813        1.1  gdamore 
    814        1.1  gdamore 		default:
    815       1.19   plunky 			arg1 = 0;
    816        1.1  gdamore 			break;
    817        1.1  gdamore 		}
    818        1.1  gdamore 
    819       1.19   plunky 		if (pcb->hp_cred != NULL
    820       1.19   plunky 		    && kauth_authorize_device(pcb->hp_cred,
    821       1.19   plunky 		    KAUTH_DEVICE_BLUETOOTH_RECV,
    822       1.19   plunky 		    KAUTH_ARG(type), KAUTH_ARG(arg1), NULL, NULL) != 0)
    823       1.19   plunky 			continue;
    824       1.19   plunky 
    825        1.1  gdamore 		/*
    826        1.1  gdamore 		 * create control messages
    827        1.1  gdamore 		 */
    828        1.1  gdamore 		ctlmsg = NULL;
    829        1.1  gdamore 		ctl = &ctlmsg;
    830        1.1  gdamore 		if (pcb->hp_flags & HCI_DIRECTION) {
    831        1.1  gdamore 			int dir = m->m_flags & M_LINK0 ? 1 : 0;
    832        1.1  gdamore 
    833       1.11   plunky 			*ctl = sbcreatecontrol(&dir, sizeof(dir),
    834        1.1  gdamore 			    SCM_HCI_DIRECTION, BTPROTO_HCI);
    835        1.1  gdamore 
    836        1.1  gdamore 			if (*ctl != NULL)
    837        1.1  gdamore 				ctl = &((*ctl)->m_next);
    838        1.1  gdamore 		}
    839       1.20   plunky 		if (pcb->hp_socket->so_options & SO_TIMESTAMP) {
    840       1.20   plunky 			struct timeval tv;
    841       1.20   plunky 
    842       1.20   plunky 			microtime(&tv);
    843       1.20   plunky 			*ctl = sbcreatecontrol(&tv, sizeof(tv),
    844       1.20   plunky 			    SCM_TIMESTAMP, SOL_SOCKET);
    845       1.20   plunky 
    846       1.20   plunky 			if (*ctl != NULL)
    847       1.20   plunky 				ctl = &((*ctl)->m_next);
    848       1.20   plunky 		}
    849        1.1  gdamore 
    850        1.1  gdamore 		/*
    851        1.1  gdamore 		 * copy to socket
    852        1.1  gdamore 		 */
    853        1.1  gdamore 		m0 = m_copypacket(m, M_DONTWAIT);
    854        1.1  gdamore 		if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv,
    855        1.1  gdamore 				(struct sockaddr *)&sa, m0, ctlmsg)) {
    856        1.1  gdamore 			sorwakeup(pcb->hp_socket);
    857        1.1  gdamore 		} else {
    858        1.1  gdamore 			m_freem(ctlmsg);
    859        1.1  gdamore 			m_freem(m0);
    860        1.1  gdamore 		}
    861        1.1  gdamore 	}
    862        1.1  gdamore }
    863  1.20.18.1    rmind 
    864  1.20.18.1    rmind PR_WRAP_USRREQ(hci_usrreq)
    865  1.20.18.2    rmind 
    866  1.20.18.1    rmind #define	hci_usrreq		hci_usrreq_wrapper
    867  1.20.18.1    rmind 
    868  1.20.18.1    rmind const struct pr_usrreqs hci_usrreqs = {
    869  1.20.18.1    rmind 	.pr_attach	= hci_attach1,
    870  1.20.18.1    rmind 	.pr_detach	= hci_detach1,
    871  1.20.18.1    rmind 	.pr_generic	= hci_usrreq,
    872  1.20.18.1    rmind };
    873