1 /* $DragonFly: src/sys/netbt/hci_socket.c,v 1.3 2008/06/20 20:52:29 aggelos Exp $ */
2 /* $OpenBSD: src/sys/netbt/hci_socket.c,v 1.5 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: hci_socket.c,v 1.14 2008/02/10 17:40:54 plunky Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 /* load symbolic names */
36 #ifdef BLUETOOTH_DEBUG
41 #include <sys/param.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/systm.h>
51 #include <sys/endian.h>
53 #include <net/if_var.h>
54 #include <sys/sysctl.h>
56 #include <sys/thread2.h>
57 #include <sys/socketvar2.h>
58 #include <sys/msgport2.h>
60 #include <netbt/bluetooth.h>
61 #include <netbt/hci.h>
63 /*******************************************************************************
65 * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
70 * the raw HCI protocol control block
73 struct socket *hp_socket; /* socket */
74 unsigned int hp_flags; /* flags */
75 bdaddr_t hp_laddr; /* local address */
76 bdaddr_t hp_raddr; /* remote address */
77 struct hci_filter hp_efilter; /* user event filter */
78 struct hci_filter hp_pfilter; /* user packet filter */
79 LIST_ENTRY(hci_pcb) hp_next; /* next HCI pcb */
83 #define HCI_PRIVILEGED (1<<0) /* no security filter for root */
84 #define HCI_DIRECTION (1<<1) /* direction control messages */
85 #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
87 LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb);
90 int hci_sendspace = HCI_CMD_PKT_SIZE;
91 int hci_recvspace = 4096;
93 extern struct pr_usrreqs hci_usrreqs;
95 /* Prototypes for usrreqs methods. */
96 static void hci_sdetach(netmsg_t msg);
98 /* supported commands opcode table */
101 uint8_t offs; /* 0 - 63 */
102 uint8_t mask; /* bit 0 - 7 */
103 int16_t length; /* -1 if privileged */
106 0, 0x01, sizeof(hci_inquiry_cp) },
107 { HCI_CMD_INQUIRY_CANCEL,
109 { HCI_CMD_PERIODIC_INQUIRY,
111 { HCI_CMD_EXIT_PERIODIC_INQUIRY,
113 { HCI_CMD_CREATE_CON,
115 { HCI_CMD_DISCONNECT,
117 { HCI_CMD_ADD_SCO_CON,
119 { HCI_CMD_CREATE_CON_CANCEL,
121 { HCI_CMD_ACCEPT_CON,
123 { HCI_CMD_REJECT_CON,
125 { HCI_CMD_LINK_KEY_REP,
127 { HCI_CMD_LINK_KEY_NEG_REP,
129 { HCI_CMD_PIN_CODE_REP,
131 { HCI_CMD_PIN_CODE_NEG_REP,
133 { HCI_CMD_CHANGE_CON_PACKET_TYPE,
137 { HCI_CMD_SET_CON_ENCRYPTION,
139 { HCI_CMD_CHANGE_CON_LINK_KEY,
141 { HCI_CMD_MASTER_LINK_KEY,
143 { HCI_CMD_REMOTE_NAME_REQ,
144 2, 0x08, sizeof(hci_remote_name_req_cp) },
145 { HCI_CMD_REMOTE_NAME_REQ_CANCEL,
147 { HCI_CMD_READ_REMOTE_FEATURES,
148 2, 0x20, sizeof(hci_read_remote_features_cp) },
149 { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES,
150 2, 0x40, sizeof(hci_read_remote_extended_features_cp) },
151 { HCI_CMD_READ_REMOTE_VER_INFO,
152 2, 0x80, sizeof(hci_read_remote_ver_info_cp) },
153 { HCI_CMD_READ_CLOCK_OFFSET,
154 3, 0x01, sizeof(hci_read_clock_offset_cp) },
155 { HCI_CMD_READ_LMP_HANDLE,
156 3, 0x02, sizeof(hci_read_lmp_handle_cp) },
159 { HCI_CMD_SNIFF_MODE,
161 { HCI_CMD_EXIT_SNIFF_MODE,
165 { HCI_CMD_EXIT_PARK_MODE,
169 { HCI_CMD_ROLE_DISCOVERY,
170 4, 0x80, sizeof(hci_role_discovery_cp) },
171 { HCI_CMD_SWITCH_ROLE,
173 { HCI_CMD_READ_LINK_POLICY_SETTINGS,
174 5, 0x02, sizeof(hci_read_link_policy_settings_cp) },
175 { HCI_CMD_WRITE_LINK_POLICY_SETTINGS,
177 { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS,
179 { HCI_CMD_WRITE_DEFAULT_LINK_POLICY_SETTINGS,
181 { HCI_CMD_FLOW_SPECIFICATION,
183 { HCI_CMD_SET_EVENT_MASK,
187 { HCI_CMD_SET_EVENT_FILTER,
191 { HCI_CMD_READ_PIN_TYPE,
193 { HCI_CMD_WRITE_PIN_TYPE,
195 { HCI_CMD_CREATE_NEW_UNIT_KEY,
197 { HCI_CMD_READ_STORED_LINK_KEY,
199 { HCI_CMD_WRITE_STORED_LINK_KEY,
201 { HCI_CMD_DELETE_STORED_LINK_KEY,
203 { HCI_CMD_WRITE_LOCAL_NAME,
205 { HCI_CMD_READ_LOCAL_NAME,
207 { HCI_CMD_READ_CON_ACCEPT_TIMEOUT,
209 { HCI_CMD_WRITE_CON_ACCEPT_TIMEOUT,
211 { HCI_CMD_READ_PAGE_TIMEOUT,
213 { HCI_CMD_WRITE_PAGE_TIMEOUT,
215 { HCI_CMD_READ_SCAN_ENABLE,
217 { HCI_CMD_WRITE_SCAN_ENABLE,
219 { HCI_CMD_READ_PAGE_SCAN_ACTIVITY,
221 { HCI_CMD_WRITE_PAGE_SCAN_ACTIVITY,
223 { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY,
225 { HCI_CMD_WRITE_INQUIRY_SCAN_ACTIVITY,
227 { HCI_CMD_READ_AUTH_ENABLE,
229 { HCI_CMD_WRITE_AUTH_ENABLE,
231 { HCI_CMD_READ_ENCRYPTION_MODE,
233 { HCI_CMD_WRITE_ENCRYPTION_MODE,
235 { HCI_CMD_READ_UNIT_CLASS,
237 { HCI_CMD_WRITE_UNIT_CLASS,
239 { HCI_CMD_READ_VOICE_SETTING,
241 { HCI_CMD_WRITE_VOICE_SETTING,
243 { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT,
244 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp) },
245 { HCI_CMD_WRITE_AUTO_FLUSH_TIMEOUT,
247 { HCI_CMD_READ_NUM_BROADCAST_RETRANS,
249 { HCI_CMD_WRITE_NUM_BROADCAST_RETRANS,
251 { HCI_CMD_READ_HOLD_MODE_ACTIVITY,
253 { HCI_CMD_WRITE_HOLD_MODE_ACTIVITY,
255 { HCI_CMD_READ_XMIT_LEVEL,
256 10, 0x04, sizeof(hci_read_xmit_level_cp) },
257 { HCI_CMD_READ_SCO_FLOW_CONTROL,
259 { HCI_CMD_WRITE_SCO_FLOW_CONTROL,
261 { HCI_CMD_HC2H_FLOW_CONTROL,
263 { HCI_CMD_HOST_BUFFER_SIZE,
265 { HCI_CMD_HOST_NUM_COMPL_PKTS,
267 { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT,
268 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp) },
269 { HCI_CMD_WRITE_LINK_SUPERVISION_TIMEOUT,
271 { HCI_CMD_READ_NUM_SUPPORTED_IAC,
273 { HCI_CMD_READ_IAC_LAP,
275 { HCI_CMD_WRITE_IAC_LAP,
277 { HCI_CMD_READ_PAGE_SCAN_PERIOD,
279 { HCI_CMD_WRITE_PAGE_SCAN_PERIOD,
281 { HCI_CMD_READ_PAGE_SCAN,
283 { HCI_CMD_WRITE_PAGE_SCAN,
285 { HCI_CMD_SET_AFH_CLASSIFICATION,
287 { HCI_CMD_READ_INQUIRY_SCAN_TYPE,
289 { HCI_CMD_WRITE_INQUIRY_SCAN_TYPE,
291 { HCI_CMD_READ_INQUIRY_MODE,
293 { HCI_CMD_WRITE_INQUIRY_MODE,
295 { HCI_CMD_READ_PAGE_SCAN_TYPE,
297 { HCI_CMD_WRITE_PAGE_SCAN_TYPE,
299 { HCI_CMD_READ_AFH_ASSESSMENT,
301 { HCI_CMD_WRITE_AFH_ASSESSMENT,
303 { HCI_CMD_READ_LOCAL_VER,
305 { HCI_CMD_READ_LOCAL_COMMANDS,
307 { HCI_CMD_READ_LOCAL_FEATURES,
309 { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
310 14, 0x40, sizeof(hci_read_local_extended_features_cp) },
311 { HCI_CMD_READ_BUFFER_SIZE,
313 { HCI_CMD_READ_COUNTRY_CODE,
315 { HCI_CMD_READ_BDADDR,
317 { HCI_CMD_READ_FAILED_CONTACT_CNTR,
318 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp) },
319 { HCI_CMD_RESET_FAILED_CONTACT_CNTR,
321 { HCI_CMD_READ_LINK_QUALITY,
322 15, 0x10, sizeof(hci_read_link_quality_cp) },
324 15, 0x20, sizeof(hci_read_rssi_cp) },
325 { HCI_CMD_READ_AFH_CHANNEL_MAP,
326 15, 0x40, sizeof(hci_read_afh_channel_map_cp) },
327 { HCI_CMD_READ_CLOCK,
328 15, 0x80, sizeof(hci_read_clock_cp) },
329 { HCI_CMD_READ_LOOPBACK_MODE,
331 { HCI_CMD_WRITE_LOOPBACK_MODE,
333 { HCI_CMD_ENABLE_UNIT_UNDER_TEST,
335 { HCI_CMD_SETUP_SCO_CON,
337 { HCI_CMD_ACCEPT_SCO_CON_REQ,
339 { HCI_CMD_REJECT_SCO_CON_REQ,
341 { HCI_CMD_READ_EXTENDED_INQUIRY_RSP,
343 { HCI_CMD_WRITE_EXTENDED_INQUIRY_RSP,
345 { HCI_CMD_REFRESH_ENCRYPTION_KEY,
347 { HCI_CMD_SNIFF_SUBRATING,
349 { HCI_CMD_READ_SIMPLE_PAIRING_MODE,
351 { HCI_CMD_WRITE_SIMPLE_PAIRING_MODE,
353 { HCI_CMD_READ_LOCAL_OOB_DATA,
355 { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER,
357 { HCI_CMD_WRITE_INQUIRY_RSP_XMIT_POWER,
359 { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING,
361 { HCI_CMD_WRITE_DEFAULT_ERRDATA_REPORTING,
363 { HCI_CMD_IO_CAPABILITY_REP,
365 { HCI_CMD_USER_CONFIRM_REP,
367 { HCI_CMD_USER_CONFIRM_NEG_REP,
369 { HCI_CMD_USER_PASSKEY_REP,
371 { HCI_CMD_USER_PASSKEY_NEG_REP,
373 { HCI_CMD_OOB_DATA_REP,
375 { HCI_CMD_WRITE_SIMPLE_PAIRING_DEBUG_MODE,
377 { HCI_CMD_ENHANCED_FLUSH,
379 { HCI_CMD_OOB_DATA_NEG_REP,
381 { HCI_CMD_SEND_KEYPRESS_NOTIFICATION,
383 { HCI_CMD_IO_CAPABILITY_NEG_REP,
388 * Security filter routines for unprivileged users.
389 * Allow all but a few critical events, and only permit read commands.
390 * If a unit is given, verify the command is supported.
394 hci_security_check_opcode(struct hci_unit *unit, uint16_t opcode)
398 for (i = 0 ; i < sizeof(hci_cmds) / sizeof(hci_cmds[0]); i++) {
399 if (opcode != hci_cmds[i].opcode)
403 || (unit->hci_cmds[hci_cmds[i].offs] & hci_cmds[i].mask))
404 return hci_cmds[i].length;
413 hci_security_check_event(uint8_t event)
417 case HCI_EVENT_RETURN_LINK_KEYS:
418 case HCI_EVENT_LINK_KEY_NOTIFICATION:
419 case HCI_EVENT_USER_CONFIRM_REQ:
420 case HCI_EVENT_USER_PASSKEY_NOTIFICATION:
421 case HCI_EVENT_VENDOR:
422 return -1; /* disallowed */
429 * When command packet reaches the device, we can drop
430 * it from the socket buffer (called from hci_output_acl)
435 struct socket *so = arg;
437 sbdroprecord(&so->so_snd.sb);
442 * HCI socket is going away and has some pending packets. We let them
443 * go by design, but remove the context pointer as it will be invalid
444 * and we no longer need to be notified.
447 hci_cmdwait_flush(struct socket *so)
449 struct hci_unit *unit;
453 DPRINTF("flushing %p\n", so);
455 TAILQ_FOREACH(unit, &hci_unit_list, hci_next) {
456 IF_POLL(&unit->hci_cmdwait, m);
458 ctx = M_GETCTX(m, struct socket *);
469 * This came from userland, so check it out.
472 hci_send(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr)
474 struct hci_unit *unit;
480 KKASSERT(addr != NULL);
482 /* wants at least a header to start with */
483 if (m->m_pkthdr.len < sizeof(hdr)) {
487 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
488 hdr.opcode = letoh16(hdr.opcode);
490 /* only allows CMD packets to be sent */
491 if (hdr.type != HCI_CMD_PKT) {
496 /* validates packet length */
497 if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) {
502 /* finds destination */
503 unit = hci_unit_lookup(addr);
509 /* security checks for unprivileged users */
510 if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
511 && hci_security_check_opcode(unit, hdr.opcode) != hdr.length) {
516 /* makes a copy for precious to keep */
517 m0 = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
522 sbappendrecord(&pcb->hp_socket->so_snd.sb, m0);
523 M_SETCTX(m, pcb->hp_socket); /* enable drop callback */
525 DPRINTFN(2, "(%s) opcode (%03x|%04x)\n",
526 device_get_nameunit(unit->hci_dev),
527 HCI_OGF(hdr.opcode), HCI_OCF(hdr.opcode));
530 if (unit->hci_num_cmd_pkts == 0)
531 IF_ENQUEUE(&unit->hci_cmdwait, m);
533 hci_output_cmd(unit, m);
538 DPRINTF("packet (%d bytes) not sent (error %d)\n",
539 m->m_pkthdr.len, err);
545 * Implementation of usrreqs.
547 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
548 * will sofree() it when we return.
551 hci_sabort(netmsg_t msg)
553 /* struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; */
555 soisdisconnected(msg->abort.base.nm_so);
557 /* msg now invalid */
561 hci_sdetach(netmsg_t msg)
563 struct socket *so = msg->detach.base.nm_so;
564 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
570 if (so->so_snd.ssb_mb != NULL)
571 hci_cmdwait_flush(so);
574 sofree(so); /* remove pcb ref */
576 LIST_REMOVE(pcb, hp_next);
580 lwkt_replymsg(&msg->detach.base.lmsg, error);
584 hci_sdisconnect(netmsg_t msg)
586 struct socket *so = msg->disconnect.base.nm_so;
587 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
591 bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY);
593 * XXX We cannot call soisdisconnected() here, as it sets
594 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem is that
595 * soisconnected() does not clear these and if you try to
596 * reconnect this socket (which is permitted) you get a
597 * broken pipe when you try to write any data.
599 soclrstate(so, SS_ISCONNECTED);
604 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
608 hci_scontrol(netmsg_t msg)
612 error = hci_ioctl(msg->control.nm_cmd,
613 (void *)msg->control.nm_data,
615 lwkt_replymsg(&msg->control.base.lmsg, error);
619 hci_sattach(netmsg_t msg)
621 struct socket *so = msg->attach.base.nm_so;
622 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
630 error = soreserve(so, hci_sendspace, hci_recvspace,NULL);
634 pcb = kmalloc(sizeof *pcb, M_PCB, M_NOWAIT | M_ZERO);
644 if (curproc == NULL || priv_check(curthread, PRIV_ROOT) == 0)
645 pcb->hp_flags |= HCI_PRIVILEGED;
648 * Set default user filter. By default, socket only passes
649 * Command_Complete and Command_Status Events.
651 hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter);
652 hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter);
653 hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter);
656 LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next);
660 lwkt_replymsg(&msg->attach.base.lmsg, error);
664 hci_sbind(netmsg_t msg)
666 struct socket *so = msg->bind.base.nm_so;
667 struct sockaddr *nam = msg->bind.nm_nam;
668 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
669 struct sockaddr_bt *sa;
672 KKASSERT(nam != NULL);
673 sa = (struct sockaddr_bt *)nam;
675 if (sa->bt_len != sizeof(struct sockaddr_bt)) {
680 if (sa->bt_family != AF_BLUETOOTH) {
681 error = EAFNOSUPPORT;
685 bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr);
687 if (bdaddr_any(&sa->bt_bdaddr))
688 pcb->hp_flags |= HCI_PROMISCUOUS;
690 pcb->hp_flags &= ~HCI_PROMISCUOUS;
693 lwkt_replymsg(&msg->bind.base.lmsg, error);
697 hci_sconnect(netmsg_t msg)
699 struct socket *so = msg->connect.base.nm_so;
700 struct sockaddr *nam = msg->connect.nm_nam;
701 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
702 struct sockaddr_bt *sa;
705 KKASSERT(nam != NULL);
706 sa = (struct sockaddr_bt *)nam;
708 if (sa->bt_len != sizeof(struct sockaddr_bt)) {
713 if (sa->bt_family != AF_BLUETOOTH) {
714 error = EAFNOSUPPORT;
718 if (hci_unit_lookup(&sa->bt_bdaddr) == NULL) {
719 error = EADDRNOTAVAIL;
722 bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr);
726 lwkt_replymsg(&msg->connect.base.lmsg, error);
730 hci_speeraddr(netmsg_t msg)
732 struct socket *so = msg->peeraddr.base.nm_so;
733 struct sockaddr **nam = msg->peeraddr.nm_nam;
734 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
735 struct sockaddr_bt *sa;
737 KKASSERT(nam != NULL);
738 sa = (struct sockaddr_bt *)nam;
740 memset(sa, 0, sizeof(struct sockaddr_bt));
741 sa->bt_len = sizeof(struct sockaddr_bt);
742 sa->bt_family = AF_BLUETOOTH;
743 bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr);
745 lwkt_replymsg(&msg->connect.base.lmsg, 0);
749 hci_ssockaddr(netmsg_t msg)
751 struct socket *so = msg->sockaddr.base.nm_so;
752 struct sockaddr **nam = msg->sockaddr.nm_nam;
753 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
754 struct sockaddr_bt *sa;
756 KKASSERT(nam != NULL);
757 sa = (struct sockaddr_bt *)nam;
759 memset(sa, 0, sizeof(struct sockaddr_bt));
760 sa->bt_len = sizeof(struct sockaddr_bt);
761 sa->bt_family = AF_BLUETOOTH;
762 bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr);
764 lwkt_replymsg(&msg->connect.base.lmsg, 0);
768 hci_sshutdown(netmsg_t msg)
770 struct socket *so = msg->shutdown.base.nm_so;
773 lwkt_replymsg(&msg->connect.base.lmsg, 0);
777 hci_ssend(netmsg_t msg)
779 struct socket *so = msg->send.base.nm_so;
780 struct mbuf *m = msg->send.nm_m;
781 struct sockaddr *addr = msg->send.nm_addr;
782 struct mbuf *control = msg->send.nm_control;
783 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
784 struct sockaddr_bt *sa;
789 sa = (struct sockaddr_bt *)addr;
791 if (sa->bt_len != sizeof(struct sockaddr_bt)) {
796 if (sa->bt_family != AF_BLUETOOTH) {
797 error = EAFNOSUPPORT;
802 /* have no use for this */
807 error = hci_send(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr));
815 lwkt_replymsg(&msg->send.base.lmsg, error);
819 * get/set socket options
822 hci_ctloutput(netmsg_t msg)
824 struct socket *so = msg->ctloutput.base.nm_so;
825 struct sockopt *sopt = msg->ctloutput.nm_sopt;
826 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb;
830 #ifdef notyet /* XXX */
831 DPRINTFN(2, "req %s\n", prcorequests[req]);
839 if (sopt->sopt_level != BTPROTO_HCI) {
844 switch(sopt->sopt_dir) {
846 switch (sopt->sopt_name) {
847 case SO_HCI_EVT_FILTER:
848 soopt_from_kbuf(sopt, &pcb->hp_efilter,
849 sizeof(struct hci_filter));
852 case SO_HCI_PKT_FILTER:
853 soopt_from_kbuf(sopt, &pcb->hp_pfilter,
854 sizeof(struct hci_filter));
857 case SO_HCI_DIRECTION:
858 if (pcb->hp_flags & HCI_DIRECTION)
862 soopt_from_kbuf(sopt, &idir, sizeof(int));
872 switch (sopt->sopt_name) {
873 case SO_HCI_EVT_FILTER: /* set event filter */
874 error = soopt_to_kbuf(sopt, &pcb->hp_efilter,
875 sizeof(struct hci_filter),
876 sizeof(struct hci_filter));
879 case SO_HCI_PKT_FILTER: /* set packet filter */
880 error = soopt_to_kbuf(sopt, &pcb->hp_pfilter,
881 sizeof(struct hci_filter),
882 sizeof(struct hci_filter));
885 case SO_HCI_DIRECTION: /* request direction ctl messages */
886 error = soopt_to_kbuf(sopt, &idir, sizeof(int),
891 pcb->hp_flags |= HCI_DIRECTION;
893 pcb->hp_flags &= ~HCI_DIRECTION;
907 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
911 * HCI mbuf tap routine
913 * copy packets to any raw HCI sockets that wish (and are
914 * permitted) to see them
917 hci_mtap(struct mbuf *m, struct hci_unit *unit)
920 struct mbuf *m0, *ctlmsg, **ctl;
921 struct sockaddr_bt sa;
926 KKASSERT(m->m_len >= sizeof(type));
928 type = *mtod(m, uint8_t *);
930 memset(&sa, 0, sizeof(sa));
931 sa.bt_len = sizeof(struct sockaddr_bt);
932 sa.bt_family = AF_BLUETOOTH;
933 bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr);
935 LIST_FOREACH(pcb, &hci_pcb, hp_next) {
937 * filter according to source address
939 if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0
940 && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0)
944 * filter according to packet type filter
946 if (hci_filter_test(type, &pcb->hp_pfilter) == 0)
950 * filter according to event/security filters
954 KKASSERT(m->m_len >= sizeof(hci_event_hdr_t));
956 event = mtod(m, hci_event_hdr_t *)->event;
958 if (hci_filter_test(event, &pcb->hp_efilter) == 0)
961 if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
962 && hci_security_check_event(event) == -1)
967 KKASSERT(m->m_len >= sizeof(hci_cmd_hdr_t));
969 opcode = letoh16(mtod(m, hci_cmd_hdr_t *)->opcode);
971 if ((pcb->hp_flags & HCI_PRIVILEGED) == 0
972 && hci_security_check_opcode(NULL, opcode) == -1)
976 case HCI_ACL_DATA_PKT:
977 case HCI_SCO_DATA_PKT:
979 if ((pcb->hp_flags & HCI_PRIVILEGED) == 0)
986 * create control messages
990 if (pcb->hp_flags & HCI_DIRECTION) {
991 int dir = m->m_flags & IFF_LINK0 ? 1 : 0;
993 *ctl = sbcreatecontrol((void *)&dir, sizeof(dir),
994 SCM_HCI_DIRECTION, BTPROTO_HCI);
997 ctl = &((*ctl)->m_next);
1003 m0 = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
1004 if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv.sb,
1005 (struct sockaddr *)&sa, m0, ctlmsg)) {
1006 sorwakeup(pcb->hp_socket);
1014 struct pr_usrreqs hci_usrreqs = {
1015 .pru_abort = hci_sabort,
1016 .pru_accept = pr_generic_notsupp,
1017 .pru_attach = hci_sattach,
1018 .pru_bind = hci_sbind,
1019 .pru_connect = hci_sconnect,
1020 .pru_connect2 = pr_generic_notsupp,
1021 .pru_control = hci_scontrol,
1022 .pru_detach = hci_sdetach,
1023 .pru_disconnect = hci_sdisconnect,
1024 .pru_listen = pr_generic_notsupp,
1025 .pru_peeraddr = hci_speeraddr,
1026 .pru_rcvd = pr_generic_notsupp,
1027 .pru_rcvoob = pr_generic_notsupp,
1028 .pru_send = hci_ssend,
1029 .pru_sense = pru_sense_null,
1030 .pru_shutdown = hci_sshutdown,
1031 .pru_sockaddr = hci_ssockaddr,
1032 .pru_sosend = sosend,
1033 .pru_soreceive = soreceive