Home | History | Annotate | Line # | Download | only in netbt
hci_socket.c revision 1.2
      1  1.2       ad /*	$NetBSD: hci_socket.c,v 1.2 2006/07/23 22:06:13 ad 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.2       ad __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.2 2006/07/23 22:06:13 ad Exp $");
     35  1.1  gdamore 
     36  1.1  gdamore #include "opt_bluetooth.h"
     37  1.1  gdamore #ifdef BLUETOOTH_DEBUG
     38  1.1  gdamore #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.1  gdamore #include <sys/mbuf.h>
     47  1.1  gdamore #include <sys/proc.h>
     48  1.1  gdamore #include <sys/protosw.h>
     49  1.1  gdamore #include <sys/socket.h>
     50  1.1  gdamore #include <sys/socketvar.h>
     51  1.1  gdamore #include <sys/systm.h>
     52  1.1  gdamore 
     53  1.1  gdamore #include <netbt/bluetooth.h>
     54  1.1  gdamore #include <netbt/hci.h>
     55  1.1  gdamore 
     56  1.1  gdamore /*******************************************************************************
     57  1.1  gdamore  *
     58  1.1  gdamore  * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
     59  1.1  gdamore  *
     60  1.1  gdamore  */
     61  1.1  gdamore 
     62  1.1  gdamore /*
     63  1.1  gdamore  * the raw HCI protocol control block
     64  1.1  gdamore  */
     65  1.1  gdamore struct hci_pcb {
     66  1.1  gdamore 	struct socket		*hp_socket;	/* socket */
     67  1.1  gdamore 	unsigned int		hp_flags;	/* flags */
     68  1.1  gdamore 	bdaddr_t		hp_laddr;	/* local address */
     69  1.1  gdamore 	bdaddr_t		hp_raddr;	/* remote address */
     70  1.1  gdamore 	struct hci_filter	hp_efilter;	/* user event filter */
     71  1.1  gdamore 	struct hci_filter	hp_pfilter;	/* user packet filter */
     72  1.1  gdamore 	LIST_ENTRY(hci_pcb)	hp_next;	/* next HCI pcb */
     73  1.1  gdamore };
     74  1.1  gdamore 
     75  1.1  gdamore /* hp_flags */
     76  1.1  gdamore #define HCI_PRIVILEGED		(1<<0)	/* no security filter for root */
     77  1.1  gdamore #define HCI_DIRECTION		(1<<1)	/* direction control messages */
     78  1.1  gdamore #define HCI_PROMISCUOUS		(1<<2)	/* listen to all units */
     79  1.1  gdamore 
     80  1.1  gdamore LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
     81  1.1  gdamore 
     82  1.1  gdamore /* sysctl defaults */
     83  1.1  gdamore int hci_sendspace = HCI_CMD_PKT_SIZE;
     84  1.1  gdamore int hci_recvspace = 4096;
     85  1.1  gdamore 
     86  1.1  gdamore /*
     87  1.1  gdamore  * Security filter routines
     88  1.1  gdamore  *	The security mask is an bit array.
     89  1.1  gdamore  *	If the bit is set, the opcode/event is permitted.
     90  1.1  gdamore  *	I only allocate the security mask if needed, and give it back
     91  1.1  gdamore  *      if requested as its quite a chunk. This could no doubt be made
     92  1.1  gdamore  *	somewhat bit more memory efficient given that most OGF/OCF's are
     93  1.1  gdamore  *	not used, but this way is quick.
     94  1.1  gdamore  */
     95  1.1  gdamore #define HCI_OPCODE_MASK_SIZE	0x800
     96  1.1  gdamore 
     97  1.1  gdamore uint32_t *hci_event_mask = NULL;
     98  1.1  gdamore uint32_t *hci_opcode_mask = NULL;
     99  1.1  gdamore 
    100  1.1  gdamore static __inline int
    101  1.1  gdamore hci_security_check_opcode(uint16_t opcode)
    102  1.1  gdamore {
    103  1.1  gdamore 
    104  1.1  gdamore 	return (hci_opcode_mask[opcode >> 5] & (1 << (opcode & 0x1f)));
    105  1.1  gdamore }
    106  1.1  gdamore 
    107  1.1  gdamore static __inline int
    108  1.1  gdamore hci_security_check_event(uint8_t event)
    109  1.1  gdamore {
    110  1.1  gdamore 
    111  1.1  gdamore 	return (hci_event_mask[event >> 5] & (1 << (event & 0x1f)));
    112  1.1  gdamore }
    113  1.1  gdamore 
    114  1.1  gdamore static __inline void
    115  1.1  gdamore hci_security_set_opcode(uint16_t opcode)
    116  1.1  gdamore {
    117  1.1  gdamore 
    118  1.1  gdamore 	hci_opcode_mask[opcode >> 5] |= (1 << (opcode & 0x1f));
    119  1.1  gdamore }
    120  1.1  gdamore 
    121  1.1  gdamore static __inline void
    122  1.1  gdamore hci_security_clr_event(uint8_t event)
    123  1.1  gdamore {
    124  1.1  gdamore 
    125  1.1  gdamore 	hci_event_mask[event >> 5] &= ~(1 << (event & 0x1f));
    126  1.1  gdamore }
    127  1.1  gdamore 
    128  1.1  gdamore static int
    129  1.1  gdamore hci_security_init(void)
    130  1.1  gdamore {
    131  1.1  gdamore 
    132  1.1  gdamore 	if (hci_event_mask)
    133  1.1  gdamore 		return 0;
    134  1.1  gdamore 
    135  1.1  gdamore 	// XXX could wait?
    136  1.1  gdamore 	hci_event_mask = malloc(HCI_EVENT_MASK_SIZE + HCI_OPCODE_MASK_SIZE,
    137  1.1  gdamore 				M_BLUETOOTH, M_NOWAIT | M_ZERO);
    138  1.1  gdamore 	if (hci_event_mask == NULL) {
    139  1.1  gdamore 		DPRINTF("no memory\n");
    140  1.1  gdamore 		return ENOMEM;
    141  1.1  gdamore 	}
    142  1.1  gdamore 
    143  1.1  gdamore 	hci_opcode_mask = hci_event_mask + HCI_EVENT_MASK_SIZE;
    144  1.1  gdamore 
    145  1.1  gdamore 	/* Events */
    146  1.1  gdamore 	/* enable all but a few critical events */
    147  1.1  gdamore 	memset(hci_event_mask, 0xff,
    148  1.1  gdamore 		HCI_EVENT_MASK_SIZE * sizeof(*hci_event_mask));
    149  1.1  gdamore 	hci_security_clr_event(HCI_EVENT_RETURN_LINK_KEYS);
    150  1.1  gdamore 	hci_security_clr_event(HCI_EVENT_LINK_KEY_NOTIFICATION);
    151  1.1  gdamore 	hci_security_clr_event(HCI_EVENT_VENDOR);
    152  1.1  gdamore 
    153  1.1  gdamore 	/* Commands - Link control */
    154  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_INQUIRY);
    155  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_REMOTE_NAME_REQ);
    156  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_REMOTE_FEATURES);
    157  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_REMOTE_EXTENDED_FEATURES);
    158  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_REMOTE_VER_INFO);
    159  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_CLOCK_OFFSET);
    160  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LMP_HANDLE);
    161  1.1  gdamore 
    162  1.1  gdamore 	/* Commands - Link policy */
    163  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_ROLE_DISCOVERY);
    164  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LINK_POLICY_SETTINGS);
    165  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS);
    166  1.1  gdamore 
    167  1.1  gdamore 	/* Commands - Host controller and baseband */
    168  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PIN_TYPE);
    169  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOCAL_NAME);
    170  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_CON_ACCEPT_TIMEOUT);
    171  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PAGE_TIMEOUT);
    172  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_SCAN_ENABLE);
    173  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PAGE_SCAN_ACTIVITY);
    174  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY);
    175  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_AUTH_ENABLE);
    176  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_ENCRYPTION_MODE);
    177  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_UNIT_CLASS);
    178  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_VOICE_SETTING);
    179  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_AUTO_FLUSH_TIMEOUT);
    180  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_NUM_BROADCAST_RETRANS);
    181  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_HOLD_MODE_ACTIVITY);
    182  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_XMIT_LEVEL);
    183  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_SCO_FLOW_CONTROL);
    184  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT);
    185  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_NUM_SUPPORTED_IAC);
    186  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_IAC_LAP);
    187  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PAGE_SCAN_PERIOD);
    188  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PAGE_SCAN);
    189  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_INQUIRY_SCAN_TYPE);
    190  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_INQUIRY_MODE);
    191  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_PAGE_SCAN_TYPE);
    192  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_AFH_ASSESSMENT);
    193  1.1  gdamore 
    194  1.1  gdamore 	/* Commands - Informational */
    195  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOCAL_VER);
    196  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOCAL_COMMANDS);
    197  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOCAL_FEATURES);
    198  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOCAL_EXTENDED_FEATURES);
    199  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_BUFFER_SIZE);
    200  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_COUNTRY_CODE);
    201  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_BDADDR);
    202  1.1  gdamore 
    203  1.1  gdamore 	/* Commands - Status */
    204  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_FAILED_CONTACT_CNTR);
    205  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LINK_QUALITY);
    206  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_RSSI);
    207  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_AFH_CHANNEL_MAP);
    208  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_CLOCK);
    209  1.1  gdamore 
    210  1.1  gdamore 	/* Commands - Testing */
    211  1.1  gdamore 	hci_security_set_opcode(HCI_CMD_READ_LOOPBACK_MODE);
    212  1.1  gdamore 
    213  1.1  gdamore 	return 0;
    214  1.1  gdamore }
    215  1.1  gdamore 
    216  1.1  gdamore /*
    217  1.1  gdamore  * When command packet reaches the device, we can drop
    218  1.1  gdamore  * it from the socket buffer (called from hci_output_acl)
    219  1.1  gdamore  */
    220  1.1  gdamore void
    221  1.1  gdamore hci_drop(void *arg)
    222  1.1  gdamore {
    223  1.1  gdamore 	struct socket *so = arg;
    224  1.1  gdamore 
    225  1.1  gdamore 	sbdroprecord(&so->so_snd);
    226  1.1  gdamore 	sowwakeup(so);
    227  1.1  gdamore }
    228  1.1  gdamore 
    229  1.1  gdamore /*
    230  1.1  gdamore  * HCI socket is going away and has some pending packets. We let them
    231  1.1  gdamore  * go by design, but remove the context pointer as it will be invalid
    232  1.1  gdamore  * and we no longer need to be notified.
    233  1.1  gdamore  */
    234  1.1  gdamore static void
    235  1.1  gdamore hci_cmdwait_flush(struct socket *so)
    236  1.1  gdamore {
    237  1.1  gdamore 	struct hci_unit *unit;
    238  1.1  gdamore 	struct socket *ctx;
    239  1.1  gdamore 	struct mbuf *m;
    240  1.1  gdamore 
    241  1.1  gdamore 	DPRINTF("flushing %p\n", so);
    242  1.1  gdamore 
    243  1.1  gdamore 	SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
    244  1.1  gdamore 		m = MBUFQ_FIRST(&unit->hci_cmdwait);
    245  1.1  gdamore 		while (m != NULL) {
    246  1.1  gdamore 			ctx = M_GETCTX(m, struct socket *);
    247  1.1  gdamore 			if (ctx == so)
    248  1.1  gdamore 				M_SETCTX(m, NULL);
    249  1.1  gdamore 
    250  1.1  gdamore 			m = MBUFQ_NEXT(m);
    251  1.1  gdamore 		}
    252  1.1  gdamore 	}
    253  1.1  gdamore }
    254  1.1  gdamore 
    255  1.1  gdamore /*
    256  1.1  gdamore  * HCI send packet
    257  1.1  gdamore  *     This came from userland, so check it out.
    258  1.1  gdamore  */
    259  1.1  gdamore static int
    260  1.1  gdamore hci_send(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr)
    261  1.1  gdamore {
    262  1.1  gdamore 	struct hci_unit *unit;
    263  1.1  gdamore 	struct mbuf *m0;
    264  1.1  gdamore 	hci_cmd_hdr_t hdr;
    265  1.1  gdamore 	int err;
    266  1.1  gdamore 
    267  1.1  gdamore 	KASSERT(m);
    268  1.1  gdamore 	KASSERT(addr);
    269  1.1  gdamore 
    270  1.1  gdamore 	/* wants at least a header to start with */
    271  1.1  gdamore 	if (m->m_pkthdr.len < sizeof(hdr)) {
    272  1.1  gdamore 		err = EMSGSIZE;
    273  1.1  gdamore 		goto bad;
    274  1.1  gdamore 	}
    275  1.1  gdamore 	m_copydata(m, 0, sizeof(hdr), &hdr);
    276  1.1  gdamore 
    277  1.1  gdamore 	/* only allows CMD packets to be sent */
    278  1.1  gdamore 	if (hdr.type != HCI_CMD_PKT) {
    279  1.1  gdamore 		err = EINVAL;
    280  1.1  gdamore 		goto bad;
    281  1.1  gdamore 	}
    282  1.1  gdamore 
    283  1.1  gdamore 	/* validates packet length */
    284  1.1  gdamore 	if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
    285  1.1  gdamore 		err = EMSGSIZE;
    286  1.1  gdamore 		goto bad;
    287  1.1  gdamore 	}
    288  1.1  gdamore 
    289  1.1  gdamore 	/* security checks for unprivileged users */
    290  1.1  gdamore 	if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
    291  1.1  gdamore 	    && (hci_security_check_opcode(le16toh(hdr.opcode)) == 0)) {
    292  1.1  gdamore 		err = EPERM;
    293  1.1  gdamore 		goto bad;
    294  1.1  gdamore 	}
    295  1.1  gdamore 
    296  1.1  gdamore 	/* finds destination */
    297  1.1  gdamore 	unit = hci_unit_lookup(addr);
    298  1.1  gdamore 	if (unit == NULL) {
    299  1.1  gdamore 		err = ENETDOWN;
    300  1.1  gdamore 		goto bad;
    301  1.1  gdamore 	}
    302  1.1  gdamore 
    303  1.1  gdamore 	/* makess a copy for precious to keep */
    304  1.1  gdamore 	m0 = m_copypacket(m, M_DONTWAIT);
    305  1.1  gdamore 	if (m0 == NULL) {
    306  1.1  gdamore 		err = ENOMEM;
    307  1.1  gdamore 		goto bad;
    308  1.1  gdamore 	}
    309  1.1  gdamore 	sbappendrecord(&pcb->hp_socket->so_snd, m0);
    310  1.1  gdamore 	M_SETCTX(m, pcb->hp_socket);	/* enable drop callback */
    311  1.1  gdamore 
    312  1.1  gdamore 	DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", unit->hci_devname,
    313  1.1  gdamore 		HCI_OGF(le16toh(hdr.opcode)), HCI_OCF(le16toh(hdr.opcode)));
    314  1.1  gdamore 
    315  1.1  gdamore 	/* Sendss it */
    316  1.1  gdamore 	if (unit->hci_num_cmd_pkts == 0)
    317  1.1  gdamore 		MBUFQ_ENQUEUE(&unit->hci_cmdwait, m);
    318  1.1  gdamore 	else
    319  1.1  gdamore 		hci_output_cmd(unit, m);
    320  1.1  gdamore 
    321  1.1  gdamore 	return 0;
    322  1.1  gdamore 
    323  1.1  gdamore bad:
    324  1.1  gdamore 	DPRINTF("packet (%d bytes) not sent (error %d)\n",
    325  1.1  gdamore 			m->m_pkthdr.len, err);
    326  1.1  gdamore 	if (m) m_freem(m);
    327  1.1  gdamore 	return err;
    328  1.1  gdamore }
    329  1.1  gdamore 
    330  1.1  gdamore /*
    331  1.1  gdamore  * User Request.
    332  1.1  gdamore  * up is socket
    333  1.1  gdamore  * m is either
    334  1.1  gdamore  *	optional mbuf chain containing message
    335  1.1  gdamore  *	ioctl command (PRU_CONTROL)
    336  1.1  gdamore  * nam is either
    337  1.1  gdamore  *	optional mbuf chain containing an address
    338  1.1  gdamore  *	ioctl data (PRU_CONTROL)
    339  1.1  gdamore  *      optionally, protocol number (PRU_ATTACH)
    340  1.1  gdamore  * ctl is optional mbuf chain containing socket options
    341  1.1  gdamore  * l is pointer to process requesting action (if any)
    342  1.1  gdamore  *
    343  1.1  gdamore  * we are responsible for disposing of m and ctl if
    344  1.1  gdamore  * they are mbuf chains
    345  1.1  gdamore  */
    346  1.1  gdamore int
    347  1.1  gdamore hci_usrreq(struct socket *up, int req, struct mbuf *m,
    348  1.1  gdamore 		struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    349  1.1  gdamore {
    350  1.1  gdamore 	struct hci_pcb *pcb = (struct hci_pcb *)up->so_pcb;
    351  1.1  gdamore 	struct sockaddr_bt *sa;
    352  1.1  gdamore 	int err = 0, flags;
    353  1.1  gdamore 
    354  1.1  gdamore 	DPRINTFN(2, "%s\n", prurequests[req]);
    355  1.1  gdamore 
    356  1.1  gdamore 	switch(req) {
    357  1.1  gdamore 	case PRU_CONTROL:
    358  1.2       ad 		return hci_ioctl((unsigned long)m, (void *)nam, l);
    359  1.1  gdamore 
    360  1.1  gdamore 	case PRU_PURGEIF:
    361  1.1  gdamore 		return EOPNOTSUPP;
    362  1.1  gdamore 
    363  1.1  gdamore 	case PRU_ATTACH:
    364  1.1  gdamore 		if (pcb)
    365  1.1  gdamore 			return EINVAL;
    366  1.1  gdamore 
    367  1.1  gdamore 		flags = 0;
    368  1.2       ad 		if (l != NULL && kauth_authorize_generic(l->l_cred,
    369  1.2       ad 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) {
    370  1.1  gdamore 			err = hci_security_init();
    371  1.1  gdamore 			if (err)
    372  1.1  gdamore 				return err;
    373  1.1  gdamore 		} else {
    374  1.1  gdamore 			flags = HCI_PRIVILEGED;
    375  1.1  gdamore 		}
    376  1.1  gdamore 
    377  1.1  gdamore 		err = soreserve(up, hci_sendspace, hci_recvspace);
    378  1.1  gdamore 		if (err)
    379  1.1  gdamore 			return err;
    380  1.1  gdamore 
    381  1.1  gdamore 		pcb = malloc(sizeof(struct hci_pcb), M_PCB, M_NOWAIT | M_ZERO);
    382  1.1  gdamore 		if (pcb == NULL)
    383  1.1  gdamore 			return ENOMEM;
    384  1.1  gdamore 
    385  1.1  gdamore 		up->so_pcb = pcb;
    386  1.1  gdamore 		pcb->hp_socket = up;
    387  1.1  gdamore 		pcb->hp_flags = flags;
    388  1.1  gdamore 
    389  1.1  gdamore 		/*
    390  1.1  gdamore 		 * Set default user filter. By default, socket only passes
    391  1.1  gdamore 		 * Command_Complete and Command_Status Events.
    392  1.1  gdamore 		 */
    393  1.1  gdamore 		hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
    394  1.1  gdamore 		hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
    395  1.1  gdamore 		hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
    396  1.1  gdamore 
    397  1.1  gdamore 		LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
    398  1.1  gdamore 
    399  1.1  gdamore 		return 0;
    400  1.1  gdamore 	}
    401  1.1  gdamore 
    402  1.1  gdamore 	/* anything after here *requires* a pcb */
    403  1.1  gdamore 	if (pcb == NULL) {
    404  1.1  gdamore 		err = EINVAL;
    405  1.1  gdamore 		goto release;
    406  1.1  gdamore 	}
    407  1.1  gdamore 
    408  1.1  gdamore 	switch(req) {
    409  1.1  gdamore 	case PRU_DISCONNECT:
    410  1.1  gdamore 		bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
    411  1.1  gdamore 
    412  1.1  gdamore 		/* XXX we cannot call soisdisconnected() here, as it sets
    413  1.1  gdamore 		 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being,
    414  1.1  gdamore 		 * that soisconnected() does not clear these and if you
    415  1.1  gdamore 		 * try to reconnect this socket (which is permitted) you
    416  1.1  gdamore 		 * get a broken pipe when you try to write any data.
    417  1.1  gdamore 		 */
    418  1.1  gdamore 		up->so_state &= ~SS_ISCONNECTED;
    419  1.1  gdamore 		break;
    420  1.1  gdamore 
    421  1.1  gdamore 	case PRU_ABORT:
    422  1.1  gdamore 		soisdisconnected(up);
    423  1.1  gdamore 		/* fall through to */
    424  1.1  gdamore 	case PRU_DETACH:
    425  1.1  gdamore 		if (up->so_snd.sb_mb != NULL)
    426  1.1  gdamore 			hci_cmdwait_flush(up);
    427  1.1  gdamore 
    428  1.1  gdamore 		up->so_pcb = NULL;
    429  1.1  gdamore 		LIST_REMOVE(pcb, hp_next);
    430  1.1  gdamore 		free(pcb, M_PCB);
    431  1.1  gdamore 		return 0;
    432  1.1  gdamore 
    433  1.1  gdamore 	case PRU_BIND:
    434  1.1  gdamore 		KASSERT(nam);
    435  1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    436  1.1  gdamore 
    437  1.1  gdamore 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    438  1.1  gdamore 			return EINVAL;
    439  1.1  gdamore 
    440  1.1  gdamore 		if (sa->bt_family != AF_BLUETOOTH)
    441  1.1  gdamore 			return EAFNOSUPPORT;
    442  1.1  gdamore 
    443  1.1  gdamore 		bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
    444  1.1  gdamore 
    445  1.1  gdamore 		if (bdaddr_any(&sa->bt_bdaddr))
    446  1.1  gdamore 			pcb->hp_flags |= HCI_PROMISCUOUS;
    447  1.1  gdamore 		else
    448  1.1  gdamore 			pcb->hp_flags &= ~HCI_PROMISCUOUS;
    449  1.1  gdamore 
    450  1.1  gdamore 		return 0;
    451  1.1  gdamore 
    452  1.1  gdamore 	case PRU_CONNECT:
    453  1.1  gdamore 		KASSERT(nam);
    454  1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    455  1.1  gdamore 
    456  1.1  gdamore 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    457  1.1  gdamore 			return EINVAL;
    458  1.1  gdamore 
    459  1.1  gdamore 		if (sa->bt_family != AF_BLUETOOTH)
    460  1.1  gdamore 			return EAFNOSUPPORT;
    461  1.1  gdamore 
    462  1.1  gdamore 		if (hci_unit_lookup(&sa->bt_bdaddr) == NULL)
    463  1.1  gdamore 			return EADDRNOTAVAIL;
    464  1.1  gdamore 
    465  1.1  gdamore 		bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
    466  1.1  gdamore 		soisconnected(up);
    467  1.1  gdamore 		return 0;
    468  1.1  gdamore 
    469  1.1  gdamore 	case PRU_PEERADDR:
    470  1.1  gdamore 		KASSERT(nam);
    471  1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    472  1.1  gdamore 
    473  1.1  gdamore 		memset(sa, 0, sizeof(struct sockaddr_bt));
    474  1.1  gdamore 		nam->m_len =
    475  1.1  gdamore 		sa->bt_len = sizeof(struct sockaddr_bt);
    476  1.1  gdamore 		sa->bt_family = AF_BLUETOOTH;
    477  1.1  gdamore 		bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
    478  1.1  gdamore 		return 0;
    479  1.1  gdamore 
    480  1.1  gdamore 	case PRU_SOCKADDR:
    481  1.1  gdamore 		KASSERT(nam);
    482  1.1  gdamore 		sa = mtod(nam, struct sockaddr_bt *);
    483  1.1  gdamore 
    484  1.1  gdamore 		memset(sa, 0, sizeof(struct sockaddr_bt));
    485  1.1  gdamore 		nam->m_len =
    486  1.1  gdamore 		sa->bt_len = sizeof(struct sockaddr_bt);
    487  1.1  gdamore 		sa->bt_family = AF_BLUETOOTH;
    488  1.1  gdamore 		bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
    489  1.1  gdamore 		return 0;
    490  1.1  gdamore 
    491  1.1  gdamore 	case PRU_SHUTDOWN:
    492  1.1  gdamore 		socantsendmore(up);
    493  1.1  gdamore 		break;
    494  1.1  gdamore 
    495  1.1  gdamore 	case PRU_SEND:
    496  1.1  gdamore 		sa = NULL;
    497  1.1  gdamore 		if (nam) {
    498  1.1  gdamore 			sa = mtod(nam, struct sockaddr_bt *);
    499  1.1  gdamore 
    500  1.1  gdamore 			if (sa->bt_len != sizeof(struct sockaddr_bt)) {
    501  1.1  gdamore 				err = EINVAL;
    502  1.1  gdamore 				goto release;
    503  1.1  gdamore 			}
    504  1.1  gdamore 
    505  1.1  gdamore 			if (sa->bt_family != AF_BLUETOOTH) {
    506  1.1  gdamore 				err = EAFNOSUPPORT;
    507  1.1  gdamore 				goto release;
    508  1.1  gdamore 			}
    509  1.1  gdamore 		}
    510  1.1  gdamore 
    511  1.1  gdamore 		if (ctl) /* have no use for this */
    512  1.1  gdamore 			m_freem(ctl);
    513  1.1  gdamore 
    514  1.1  gdamore 		return hci_send(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
    515  1.1  gdamore 
    516  1.1  gdamore 	case PRU_SENSE:
    517  1.1  gdamore 		return 0;		/* (no sense - Doh!) */
    518  1.1  gdamore 
    519  1.1  gdamore 	case PRU_RCVD:
    520  1.1  gdamore 	case PRU_RCVOOB:
    521  1.1  gdamore 		return EOPNOTSUPP;	/* (no release) */
    522  1.1  gdamore 
    523  1.1  gdamore 	case PRU_ACCEPT:
    524  1.1  gdamore 	case PRU_CONNECT2:
    525  1.1  gdamore 	case PRU_LISTEN:
    526  1.1  gdamore 	case PRU_SENDOOB:
    527  1.1  gdamore 	case PRU_FASTTIMO:
    528  1.1  gdamore 	case PRU_SLOWTIMO:
    529  1.1  gdamore 	case PRU_PROTORCV:
    530  1.1  gdamore 	case PRU_PROTOSEND:
    531  1.1  gdamore 		err = EOPNOTSUPP;
    532  1.1  gdamore 		break;
    533  1.1  gdamore 
    534  1.1  gdamore 	default:
    535  1.1  gdamore 		UNKNOWN(req);
    536  1.1  gdamore 		err = EOPNOTSUPP;
    537  1.1  gdamore 		break;
    538  1.1  gdamore 	}
    539  1.1  gdamore 
    540  1.1  gdamore release:
    541  1.2       ad 	if (m)
    542  1.2       ad 		m_freem(m);
    543  1.2       ad 	if (ctl)
    544  1.2       ad 		m_freem(ctl);
    545  1.1  gdamore 	return err;
    546  1.1  gdamore }
    547  1.1  gdamore 
    548  1.1  gdamore /*
    549  1.1  gdamore  * System is short on memory.
    550  1.1  gdamore  */
    551  1.1  gdamore void
    552  1.1  gdamore hci_drain(void)
    553  1.1  gdamore {
    554  1.1  gdamore 
    555  1.1  gdamore 	/*
    556  1.1  gdamore 	 * We can give the security masks back, if there
    557  1.1  gdamore 	 * are no unprivileged sockets in operation..
    558  1.1  gdamore 	 */
    559  1.1  gdamore 	if (hci_event_mask) {
    560  1.1  gdamore 		struct hci_pcb *pcb;
    561  1.1  gdamore 
    562  1.1  gdamore 		LIST_FOREACH(pcb, &hci_pcb, hp_next) {
    563  1.1  gdamore 			if ((pcb->hp_flags & HCI_PRIVILEGED) == 0)
    564  1.1  gdamore 				break;
    565  1.1  gdamore 		}
    566  1.1  gdamore 
    567  1.1  gdamore 		if (pcb == NULL) {
    568  1.1  gdamore 			free(hci_event_mask, M_BLUETOOTH);
    569  1.1  gdamore 			hci_event_mask = NULL;
    570  1.1  gdamore 			hci_opcode_mask = NULL;
    571  1.1  gdamore 		}
    572  1.1  gdamore 	}
    573  1.1  gdamore }
    574  1.1  gdamore 
    575  1.1  gdamore /*
    576  1.1  gdamore  * get/set socket options
    577  1.1  gdamore  */
    578  1.1  gdamore int
    579  1.1  gdamore hci_ctloutput(int req, struct socket *so, int level,
    580  1.1  gdamore 		int optname, struct mbuf **opt)
    581  1.1  gdamore {
    582  1.1  gdamore 	struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
    583  1.1  gdamore 	struct mbuf *m;
    584  1.1  gdamore 	int err = 0;
    585  1.1  gdamore 
    586  1.1  gdamore 	DPRINTFN(2, "req %s\n", prcorequests[req]);
    587  1.1  gdamore 
    588  1.1  gdamore 	if (pcb == NULL)
    589  1.1  gdamore 		return EINVAL;
    590  1.1  gdamore 
    591  1.1  gdamore 	if (level != BTPROTO_HCI)
    592  1.1  gdamore 		return 0;
    593  1.1  gdamore 
    594  1.1  gdamore 	switch(req) {
    595  1.1  gdamore 	case PRCO_GETOPT:
    596  1.1  gdamore 		m = m_get(M_WAIT, MT_SOOPTS);
    597  1.1  gdamore 		switch (optname) {
    598  1.1  gdamore 		case SO_HCI_EVT_FILTER:
    599  1.1  gdamore 			m->m_len = sizeof(struct hci_filter);
    600  1.1  gdamore 			memcpy(mtod(m, void *), &pcb->hp_efilter, m->m_len);
    601  1.1  gdamore 			break;
    602  1.1  gdamore 
    603  1.1  gdamore 		case SO_HCI_PKT_FILTER:
    604  1.1  gdamore 			m->m_len = sizeof(struct hci_filter);
    605  1.1  gdamore 			memcpy(mtod(m, void *), &pcb->hp_pfilter, m->m_len);
    606  1.1  gdamore 			break;
    607  1.1  gdamore 
    608  1.1  gdamore 		case SO_HCI_DIRECTION:
    609  1.1  gdamore 			m->m_len = sizeof(int);
    610  1.1  gdamore 			if (pcb->hp_flags & HCI_DIRECTION)
    611  1.1  gdamore 				*mtod(m, int *) = 1;
    612  1.1  gdamore 			else
    613  1.1  gdamore 				*mtod(m, int *) = 0;
    614  1.1  gdamore 			break;
    615  1.1  gdamore 
    616  1.1  gdamore 		default:
    617  1.1  gdamore 			err = EINVAL;
    618  1.1  gdamore 			m_freem(m);
    619  1.1  gdamore 			m = NULL;
    620  1.1  gdamore 			break;
    621  1.1  gdamore 		}
    622  1.1  gdamore 		*opt = m;
    623  1.1  gdamore 		break;
    624  1.1  gdamore 
    625  1.1  gdamore 	case PRCO_SETOPT:
    626  1.1  gdamore 		m = *opt;
    627  1.1  gdamore 		if (m) switch (optname) {
    628  1.1  gdamore 		case SO_HCI_EVT_FILTER:	/* set event filter */
    629  1.1  gdamore 			m->m_len = min(m->m_len, sizeof(struct hci_filter));
    630  1.1  gdamore 			memcpy(&pcb->hp_efilter, mtod(m, void *), m->m_len);
    631  1.1  gdamore 			break;
    632  1.1  gdamore 
    633  1.1  gdamore 		case SO_HCI_PKT_FILTER:	/* set packet filter */
    634  1.1  gdamore 			m->m_len = min(m->m_len, sizeof(struct hci_filter));
    635  1.1  gdamore 			memcpy(&pcb->hp_pfilter, mtod(m, void *), m->m_len);
    636  1.1  gdamore 			break;
    637  1.1  gdamore 
    638  1.1  gdamore 		case SO_HCI_DIRECTION:	/* request direction ctl messages */
    639  1.1  gdamore 			if (*mtod(m, int *))
    640  1.1  gdamore 				pcb->hp_flags |= HCI_DIRECTION;
    641  1.1  gdamore 			else
    642  1.1  gdamore 				pcb->hp_flags &= ~HCI_DIRECTION;
    643  1.1  gdamore 			break;
    644  1.1  gdamore 
    645  1.1  gdamore 		default:
    646  1.1  gdamore 			err = EINVAL;
    647  1.1  gdamore 			break;
    648  1.1  gdamore 		}
    649  1.1  gdamore 		m_freem(m);
    650  1.1  gdamore 		break;
    651  1.1  gdamore 
    652  1.1  gdamore 	default:
    653  1.1  gdamore 		err = EINVAL;
    654  1.1  gdamore 		break;
    655  1.1  gdamore 	}
    656  1.1  gdamore 
    657  1.1  gdamore 	return err;
    658  1.1  gdamore }
    659  1.1  gdamore 
    660  1.1  gdamore /*
    661  1.1  gdamore  * HCI mbuf tap routine
    662  1.1  gdamore  *
    663  1.1  gdamore  * copy packets to any raw HCI sockets that wish (and are
    664  1.1  gdamore  * permitted) to see them
    665  1.1  gdamore  */
    666  1.1  gdamore void
    667  1.1  gdamore hci_mtap(struct mbuf *m, struct hci_unit *unit)
    668  1.1  gdamore {
    669  1.1  gdamore 	struct hci_pcb *pcb;
    670  1.1  gdamore 	struct mbuf *m0, *ctlmsg, **ctl;
    671  1.1  gdamore 	struct sockaddr_bt sa;
    672  1.1  gdamore 	uint8_t type;
    673  1.1  gdamore 	uint8_t event;
    674  1.1  gdamore 	uint16_t opcode;
    675  1.1  gdamore 
    676  1.1  gdamore 	KASSERT(m->m_len >= sizeof(type));
    677  1.1  gdamore 
    678  1.1  gdamore 	type = *mtod(m, uint8_t *);
    679  1.1  gdamore 
    680  1.1  gdamore 	memset(&sa, 0, sizeof(sa));
    681  1.1  gdamore 	sa.bt_len = sizeof(struct sockaddr_bt);
    682  1.1  gdamore 	sa.bt_family = AF_BLUETOOTH;
    683  1.1  gdamore 	bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
    684  1.1  gdamore 
    685  1.1  gdamore 	LIST_FOREACH(pcb, &hci_pcb, hp_next) {
    686  1.1  gdamore 		/*
    687  1.1  gdamore 		 * filter according to source address
    688  1.1  gdamore 		 */
    689  1.1  gdamore 		if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
    690  1.1  gdamore 		    && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
    691  1.1  gdamore 			continue;
    692  1.1  gdamore 
    693  1.1  gdamore 		/*
    694  1.1  gdamore 		 * filter according to packet type filter
    695  1.1  gdamore 		 */
    696  1.1  gdamore 		if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
    697  1.1  gdamore 			continue;
    698  1.1  gdamore 
    699  1.1  gdamore 		/*
    700  1.1  gdamore 		 * filter according to event/security filters
    701  1.1  gdamore 		 */
    702  1.1  gdamore 		switch(type) {
    703  1.1  gdamore 		case HCI_EVENT_PKT:
    704  1.1  gdamore 			KASSERT(m->m_len >= sizeof(hci_event_hdr_t));
    705  1.1  gdamore 
    706  1.1  gdamore 			event = mtod(m, hci_event_hdr_t *)->event;
    707  1.1  gdamore 
    708  1.1  gdamore 			if (hci_filter_test(event, &pcb->hp_efilter) == 0)
    709  1.1  gdamore 				continue;
    710  1.1  gdamore 
    711  1.1  gdamore 			if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
    712  1.1  gdamore 			    && hci_security_check_event(event) == 0)
    713  1.1  gdamore 				continue;
    714  1.1  gdamore 			break;
    715  1.1  gdamore 
    716  1.1  gdamore 		case HCI_CMD_PKT:
    717  1.1  gdamore 			KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
    718  1.1  gdamore 
    719  1.1  gdamore 			opcode = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode);
    720  1.1  gdamore 
    721  1.1  gdamore 			if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
    722  1.1  gdamore 			    && hci_security_check_opcode(opcode) == 0)
    723  1.1  gdamore 				continue;
    724  1.1  gdamore 			break;
    725  1.1  gdamore 
    726  1.1  gdamore 		case HCI_ACL_DATA_PKT:
    727  1.1  gdamore 		case HCI_SCO_DATA_PKT:
    728  1.1  gdamore 		default:
    729  1.1  gdamore 			if ((pcb->hp_flags & HCI_PRIVILEGED) == 0)
    730  1.1  gdamore 				continue;
    731  1.1  gdamore 
    732  1.1  gdamore 			break;
    733  1.1  gdamore 		}
    734  1.1  gdamore 
    735  1.1  gdamore 		/*
    736  1.1  gdamore 		 * create control messages
    737  1.1  gdamore 		 */
    738  1.1  gdamore 		ctlmsg = NULL;
    739  1.1  gdamore 		ctl = &ctlmsg;
    740  1.1  gdamore 		if (pcb->hp_flags & HCI_DIRECTION) {
    741  1.1  gdamore 			int dir = m->m_flags & M_LINK0 ? 1 : 0;
    742  1.1  gdamore 
    743  1.1  gdamore 			*ctl = sbcreatecontrol((caddr_t)&dir, sizeof(dir),
    744  1.1  gdamore 			    SCM_HCI_DIRECTION, BTPROTO_HCI);
    745  1.1  gdamore 
    746  1.1  gdamore 			if (*ctl != NULL)
    747  1.1  gdamore 				ctl = &((*ctl)->m_next);
    748  1.1  gdamore 		}
    749  1.1  gdamore 
    750  1.1  gdamore 		/*
    751  1.1  gdamore 		 * copy to socket
    752  1.1  gdamore 		 */
    753  1.1  gdamore 		m0 = m_copypacket(m, M_DONTWAIT);
    754  1.1  gdamore 		if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv,
    755  1.1  gdamore 				(struct sockaddr *)&sa, m0, ctlmsg)) {
    756  1.1  gdamore 			sorwakeup(pcb->hp_socket);
    757  1.1  gdamore 		} else {
    758  1.1  gdamore 			m_freem(ctlmsg);
    759  1.1  gdamore 			m_freem(m0);
    760  1.1  gdamore 		}
    761  1.1  gdamore 	}
    762  1.1  gdamore }
    763