1 /* $OpenBSD: src/sys/netbt/hci_socket.c,v 1.5 2008/02/24 21:34:48 uwe Exp $ */
2 /* $NetBSD: hci_socket.c,v 1.14 2008/02/10 17:40:54 plunky Exp $ */
5 * Copyright (c) 2005 Iain Hibbert.
6 * Copyright (c) 2006 Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 /* load symbolic names */
35 #ifdef BLUETOOTH_DEBUG
40 #include <sys/param.h>
41 #include <sys/domain.h>
42 #include <sys/kernel.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/systm.h>
50 #include <sys/endian.h>
52 #include <net/if_var.h>
53 #include <sys/sysctl.h>
55 #include <sys/thread2.h>
56 #include <sys/socketvar2.h>
57 #include <sys/msgport2.h>
59 #include <netbt/bluetooth.h>
60 #include <netbt/hci.h>
62 /*******************************************************************************
64 * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
69 * the raw HCI protocol control block
72 struct socket
*hp_socket
; /* socket */
73 unsigned int hp_flags
; /* flags */
74 bdaddr_t hp_laddr
; /* local address */
75 bdaddr_t hp_raddr
; /* remote address */
76 struct hci_filter hp_efilter
; /* user event filter */
77 struct hci_filter hp_pfilter
; /* user packet filter */
78 LIST_ENTRY(hci_pcb
) hp_next
; /* next HCI pcb */
82 #define HCI_PRIVILEGED (1<<0) /* no security filter for root */
83 #define HCI_DIRECTION (1<<1) /* direction control messages */
84 #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
86 LIST_HEAD(hci_pcb_list
, hci_pcb
) hci_pcb
= LIST_HEAD_INITIALIZER(hci_pcb
);
89 int hci_sendspace
= HCI_CMD_PKT_SIZE
;
90 int hci_recvspace
= 4096;
92 extern struct pr_usrreqs hci_usrreqs
;
94 /* Prototypes for usrreqs methods. */
95 static void hci_sdetach(netmsg_t msg
);
97 /* supported commands opcode table */
100 uint8_t offs
; /* 0 - 63 */
101 uint8_t mask
; /* bit 0 - 7 */
102 int16_t length
; /* -1 if privileged */
105 0, 0x01, sizeof(hci_inquiry_cp
) },
106 { HCI_CMD_INQUIRY_CANCEL
,
108 { HCI_CMD_PERIODIC_INQUIRY
,
110 { HCI_CMD_EXIT_PERIODIC_INQUIRY
,
112 { HCI_CMD_CREATE_CON
,
114 { HCI_CMD_DISCONNECT
,
116 { HCI_CMD_ADD_SCO_CON
,
118 { HCI_CMD_CREATE_CON_CANCEL
,
120 { HCI_CMD_ACCEPT_CON
,
122 { HCI_CMD_REJECT_CON
,
124 { HCI_CMD_LINK_KEY_REP
,
126 { HCI_CMD_LINK_KEY_NEG_REP
,
128 { HCI_CMD_PIN_CODE_REP
,
130 { HCI_CMD_PIN_CODE_NEG_REP
,
132 { HCI_CMD_CHANGE_CON_PACKET_TYPE
,
136 { HCI_CMD_SET_CON_ENCRYPTION
,
138 { HCI_CMD_CHANGE_CON_LINK_KEY
,
140 { HCI_CMD_MASTER_LINK_KEY
,
142 { HCI_CMD_REMOTE_NAME_REQ
,
143 2, 0x08, sizeof(hci_remote_name_req_cp
) },
144 { HCI_CMD_REMOTE_NAME_REQ_CANCEL
,
146 { HCI_CMD_READ_REMOTE_FEATURES
,
147 2, 0x20, sizeof(hci_read_remote_features_cp
) },
148 { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES
,
149 2, 0x40, sizeof(hci_read_remote_extended_features_cp
) },
150 { HCI_CMD_READ_REMOTE_VER_INFO
,
151 2, 0x80, sizeof(hci_read_remote_ver_info_cp
) },
152 { HCI_CMD_READ_CLOCK_OFFSET
,
153 3, 0x01, sizeof(hci_read_clock_offset_cp
) },
154 { HCI_CMD_READ_LMP_HANDLE
,
155 3, 0x02, sizeof(hci_read_lmp_handle_cp
) },
158 { HCI_CMD_SNIFF_MODE
,
160 { HCI_CMD_EXIT_SNIFF_MODE
,
164 { HCI_CMD_EXIT_PARK_MODE
,
168 { HCI_CMD_ROLE_DISCOVERY
,
169 4, 0x80, sizeof(hci_role_discovery_cp
) },
170 { HCI_CMD_SWITCH_ROLE
,
172 { HCI_CMD_READ_LINK_POLICY_SETTINGS
,
173 5, 0x02, sizeof(hci_read_link_policy_settings_cp
) },
174 { HCI_CMD_WRITE_LINK_POLICY_SETTINGS
,
176 { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS
,
178 { HCI_CMD_WRITE_DEFAULT_LINK_POLICY_SETTINGS
,
180 { HCI_CMD_FLOW_SPECIFICATION
,
182 { HCI_CMD_SET_EVENT_MASK
,
186 { HCI_CMD_SET_EVENT_FILTER
,
190 { HCI_CMD_READ_PIN_TYPE
,
192 { HCI_CMD_WRITE_PIN_TYPE
,
194 { HCI_CMD_CREATE_NEW_UNIT_KEY
,
196 { HCI_CMD_READ_STORED_LINK_KEY
,
198 { HCI_CMD_WRITE_STORED_LINK_KEY
,
200 { HCI_CMD_DELETE_STORED_LINK_KEY
,
202 { HCI_CMD_WRITE_LOCAL_NAME
,
204 { HCI_CMD_READ_LOCAL_NAME
,
206 { HCI_CMD_READ_CON_ACCEPT_TIMEOUT
,
208 { HCI_CMD_WRITE_CON_ACCEPT_TIMEOUT
,
210 { HCI_CMD_READ_PAGE_TIMEOUT
,
212 { HCI_CMD_WRITE_PAGE_TIMEOUT
,
214 { HCI_CMD_READ_SCAN_ENABLE
,
216 { HCI_CMD_WRITE_SCAN_ENABLE
,
218 { HCI_CMD_READ_PAGE_SCAN_ACTIVITY
,
220 { HCI_CMD_WRITE_PAGE_SCAN_ACTIVITY
,
222 { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY
,
224 { HCI_CMD_WRITE_INQUIRY_SCAN_ACTIVITY
,
226 { HCI_CMD_READ_AUTH_ENABLE
,
228 { HCI_CMD_WRITE_AUTH_ENABLE
,
230 { HCI_CMD_READ_ENCRYPTION_MODE
,
232 { HCI_CMD_WRITE_ENCRYPTION_MODE
,
234 { HCI_CMD_READ_UNIT_CLASS
,
236 { HCI_CMD_WRITE_UNIT_CLASS
,
238 { HCI_CMD_READ_VOICE_SETTING
,
240 { HCI_CMD_WRITE_VOICE_SETTING
,
242 { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT
,
243 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp
) },
244 { HCI_CMD_WRITE_AUTO_FLUSH_TIMEOUT
,
246 { HCI_CMD_READ_NUM_BROADCAST_RETRANS
,
248 { HCI_CMD_WRITE_NUM_BROADCAST_RETRANS
,
250 { HCI_CMD_READ_HOLD_MODE_ACTIVITY
,
252 { HCI_CMD_WRITE_HOLD_MODE_ACTIVITY
,
254 { HCI_CMD_READ_XMIT_LEVEL
,
255 10, 0x04, sizeof(hci_read_xmit_level_cp
) },
256 { HCI_CMD_READ_SCO_FLOW_CONTROL
,
258 { HCI_CMD_WRITE_SCO_FLOW_CONTROL
,
260 { HCI_CMD_HC2H_FLOW_CONTROL
,
262 { HCI_CMD_HOST_BUFFER_SIZE
,
264 { HCI_CMD_HOST_NUM_COMPL_PKTS
,
266 { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT
,
267 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp
) },
268 { HCI_CMD_WRITE_LINK_SUPERVISION_TIMEOUT
,
270 { HCI_CMD_READ_NUM_SUPPORTED_IAC
,
272 { HCI_CMD_READ_IAC_LAP
,
274 { HCI_CMD_WRITE_IAC_LAP
,
276 { HCI_CMD_READ_PAGE_SCAN_PERIOD
,
278 { HCI_CMD_WRITE_PAGE_SCAN_PERIOD
,
280 { HCI_CMD_READ_PAGE_SCAN
,
282 { HCI_CMD_WRITE_PAGE_SCAN
,
284 { HCI_CMD_SET_AFH_CLASSIFICATION
,
286 { HCI_CMD_READ_INQUIRY_SCAN_TYPE
,
288 { HCI_CMD_WRITE_INQUIRY_SCAN_TYPE
,
290 { HCI_CMD_READ_INQUIRY_MODE
,
292 { HCI_CMD_WRITE_INQUIRY_MODE
,
294 { HCI_CMD_READ_PAGE_SCAN_TYPE
,
296 { HCI_CMD_WRITE_PAGE_SCAN_TYPE
,
298 { HCI_CMD_READ_AFH_ASSESSMENT
,
300 { HCI_CMD_WRITE_AFH_ASSESSMENT
,
302 { HCI_CMD_READ_LOCAL_VER
,
304 { HCI_CMD_READ_LOCAL_COMMANDS
,
306 { HCI_CMD_READ_LOCAL_FEATURES
,
308 { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES
,
309 14, 0x40, sizeof(hci_read_local_extended_features_cp
) },
310 { HCI_CMD_READ_BUFFER_SIZE
,
312 { HCI_CMD_READ_COUNTRY_CODE
,
314 { HCI_CMD_READ_BDADDR
,
316 { HCI_CMD_READ_FAILED_CONTACT_CNTR
,
317 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp
) },
318 { HCI_CMD_RESET_FAILED_CONTACT_CNTR
,
320 { HCI_CMD_READ_LINK_QUALITY
,
321 15, 0x10, sizeof(hci_read_link_quality_cp
) },
323 15, 0x20, sizeof(hci_read_rssi_cp
) },
324 { HCI_CMD_READ_AFH_CHANNEL_MAP
,
325 15, 0x40, sizeof(hci_read_afh_channel_map_cp
) },
326 { HCI_CMD_READ_CLOCK
,
327 15, 0x80, sizeof(hci_read_clock_cp
) },
328 { HCI_CMD_READ_LOOPBACK_MODE
,
330 { HCI_CMD_WRITE_LOOPBACK_MODE
,
332 { HCI_CMD_ENABLE_UNIT_UNDER_TEST
,
334 { HCI_CMD_SETUP_SCO_CON
,
336 { HCI_CMD_ACCEPT_SCO_CON_REQ
,
338 { HCI_CMD_REJECT_SCO_CON_REQ
,
340 { HCI_CMD_READ_EXTENDED_INQUIRY_RSP
,
342 { HCI_CMD_WRITE_EXTENDED_INQUIRY_RSP
,
344 { HCI_CMD_REFRESH_ENCRYPTION_KEY
,
346 { HCI_CMD_SNIFF_SUBRATING
,
348 { HCI_CMD_READ_SIMPLE_PAIRING_MODE
,
350 { HCI_CMD_WRITE_SIMPLE_PAIRING_MODE
,
352 { HCI_CMD_READ_LOCAL_OOB_DATA
,
354 { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER
,
356 { HCI_CMD_WRITE_INQUIRY_RSP_XMIT_POWER
,
358 { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING
,
360 { HCI_CMD_WRITE_DEFAULT_ERRDATA_REPORTING
,
362 { HCI_CMD_IO_CAPABILITY_REP
,
364 { HCI_CMD_USER_CONFIRM_REP
,
366 { HCI_CMD_USER_CONFIRM_NEG_REP
,
368 { HCI_CMD_USER_PASSKEY_REP
,
370 { HCI_CMD_USER_PASSKEY_NEG_REP
,
372 { HCI_CMD_OOB_DATA_REP
,
374 { HCI_CMD_WRITE_SIMPLE_PAIRING_DEBUG_MODE
,
376 { HCI_CMD_ENHANCED_FLUSH
,
378 { HCI_CMD_OOB_DATA_NEG_REP
,
380 { HCI_CMD_SEND_KEYPRESS_NOTIFICATION
,
382 { HCI_CMD_IO_CAPABILITY_NEG_REP
,
387 * Security filter routines for unprivileged users.
388 * Allow all but a few critical events, and only permit read commands.
389 * If a unit is given, verify the command is supported.
393 hci_security_check_opcode(struct hci_unit
*unit
, uint16_t opcode
)
397 for (i
= 0 ; i
< NELEM(hci_cmds
); i
++) {
398 if (opcode
!= hci_cmds
[i
].opcode
)
402 || (unit
->hci_cmds
[hci_cmds
[i
].offs
] & hci_cmds
[i
].mask
))
403 return hci_cmds
[i
].length
;
412 hci_security_check_event(uint8_t event
)
416 case HCI_EVENT_RETURN_LINK_KEYS
:
417 case HCI_EVENT_LINK_KEY_NOTIFICATION
:
418 case HCI_EVENT_USER_CONFIRM_REQ
:
419 case HCI_EVENT_USER_PASSKEY_NOTIFICATION
:
420 case HCI_EVENT_VENDOR
:
421 return -1; /* disallowed */
428 * When command packet reaches the device, we can drop
429 * it from the socket buffer (called from hci_output_acl)
434 struct socket
*so
= arg
;
436 sbdroprecord(&so
->so_snd
.sb
);
441 * HCI socket is going away and has some pending packets. We let them
442 * go by design, but remove the context pointer as it will be invalid
443 * and we no longer need to be notified.
446 hci_cmdwait_flush(struct socket
*so
)
448 struct hci_unit
*unit
;
452 DPRINTF("flushing %p\n", so
);
454 TAILQ_FOREACH(unit
, &hci_unit_list
, hci_next
) {
455 IF_POLL(&unit
->hci_cmdwait
, m
);
457 ctx
= M_GETCTX(m
, struct socket
*);
468 * This came from userland, so check it out.
471 hci_send(struct hci_pcb
*pcb
, struct mbuf
*m
, bdaddr_t
*addr
)
473 struct hci_unit
*unit
;
479 KKASSERT(addr
!= NULL
);
481 /* wants at least a header to start with */
482 if (m
->m_pkthdr
.len
< sizeof(hdr
)) {
486 m_copydata(m
, 0, sizeof(hdr
), (caddr_t
)&hdr
);
487 hdr
.opcode
= letoh16(hdr
.opcode
);
489 /* only allows CMD packets to be sent */
490 if (hdr
.type
!= HCI_CMD_PKT
) {
495 /* validates packet length */
496 if (m
->m_pkthdr
.len
!= sizeof(hdr
) + hdr
.length
) {
501 /* finds destination */
502 unit
= hci_unit_lookup(addr
);
508 /* security checks for unprivileged users */
509 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
510 && hci_security_check_opcode(unit
, hdr
.opcode
) != hdr
.length
) {
515 /* makes a copy for precious to keep */
516 m0
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
521 sbappendrecord(&pcb
->hp_socket
->so_snd
.sb
, m0
);
522 M_SETCTX(m
, pcb
->hp_socket
); /* enable drop callback */
524 DPRINTFN(2, "(%s) opcode (%03x|%04x)\n",
525 device_get_nameunit(unit
->hci_dev
),
526 HCI_OGF(hdr
.opcode
), HCI_OCF(hdr
.opcode
));
529 if (unit
->hci_num_cmd_pkts
== 0)
530 IF_ENQUEUE(&unit
->hci_cmdwait
, m
);
532 hci_output_cmd(unit
, m
);
537 DPRINTF("packet (%d bytes) not sent (error %d)\n",
538 m
->m_pkthdr
.len
, err
);
544 * Implementation of usrreqs.
546 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
547 * will sofree() it when we return.
550 hci_sabort(netmsg_t msg
)
552 /* struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; */
554 soisdisconnected(msg
->abort
.base
.nm_so
);
556 /* msg now invalid */
560 hci_sdetach(netmsg_t msg
)
562 struct socket
*so
= msg
->detach
.base
.nm_so
;
563 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
569 if (so
->so_snd
.ssb_mb
!= NULL
)
570 hci_cmdwait_flush(so
);
573 sofree(so
); /* remove pcb ref */
575 LIST_REMOVE(pcb
, hp_next
);
579 lwkt_replymsg(&msg
->detach
.base
.lmsg
, error
);
583 hci_sdisconnect(netmsg_t msg
)
585 struct socket
*so
= msg
->disconnect
.base
.nm_so
;
586 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
590 bdaddr_copy(&pcb
->hp_raddr
, BDADDR_ANY
);
592 * XXX We cannot call soisdisconnected() here, as it sets
593 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem is that
594 * soisconnected() does not clear these and if you try to
595 * reconnect this socket (which is permitted) you get a
596 * broken pipe when you try to write any data.
598 soclrstate(so
, SS_ISCONNECTED
);
603 lwkt_replymsg(&msg
->disconnect
.base
.lmsg
, error
);
607 hci_scontrol(netmsg_t msg
)
611 error
= hci_ioctl(msg
->control
.nm_cmd
,
612 (void *)msg
->control
.nm_data
,
614 lwkt_replymsg(&msg
->control
.base
.lmsg
, error
);
618 hci_sattach(netmsg_t msg
)
620 struct socket
*so
= msg
->attach
.base
.nm_so
;
621 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
629 error
= soreserve(so
, hci_sendspace
, hci_recvspace
,NULL
);
633 pcb
= kmalloc(sizeof *pcb
, M_PCB
, M_NOWAIT
| M_ZERO
);
643 if (curproc
== NULL
|| priv_check(curthread
, PRIV_ROOT
) == 0)
644 pcb
->hp_flags
|= HCI_PRIVILEGED
;
647 * Set default user filter. By default, socket only passes
648 * Command_Complete and Command_Status Events.
650 hci_filter_set(HCI_EVENT_COMMAND_COMPL
, &pcb
->hp_efilter
);
651 hci_filter_set(HCI_EVENT_COMMAND_STATUS
, &pcb
->hp_efilter
);
652 hci_filter_set(HCI_EVENT_PKT
, &pcb
->hp_pfilter
);
655 LIST_INSERT_HEAD(&hci_pcb
, pcb
, hp_next
);
659 lwkt_replymsg(&msg
->attach
.base
.lmsg
, error
);
663 hci_sbind(netmsg_t msg
)
665 struct socket
*so
= msg
->bind
.base
.nm_so
;
666 struct sockaddr
*nam
= msg
->bind
.nm_nam
;
667 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
668 struct sockaddr_bt
*sa
;
671 KKASSERT(nam
!= NULL
);
672 sa
= (struct sockaddr_bt
*)nam
;
674 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
679 if (sa
->bt_family
!= AF_BLUETOOTH
) {
680 error
= EAFNOSUPPORT
;
684 bdaddr_copy(&pcb
->hp_laddr
, &sa
->bt_bdaddr
);
686 if (bdaddr_any(&sa
->bt_bdaddr
))
687 pcb
->hp_flags
|= HCI_PROMISCUOUS
;
689 pcb
->hp_flags
&= ~HCI_PROMISCUOUS
;
692 lwkt_replymsg(&msg
->bind
.base
.lmsg
, error
);
696 hci_sconnect(netmsg_t msg
)
698 struct socket
*so
= msg
->connect
.base
.nm_so
;
699 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
700 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
701 struct sockaddr_bt
*sa
;
704 KKASSERT(nam
!= NULL
);
705 sa
= (struct sockaddr_bt
*)nam
;
707 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
712 if (sa
->bt_family
!= AF_BLUETOOTH
) {
713 error
= EAFNOSUPPORT
;
717 if (hci_unit_lookup(&sa
->bt_bdaddr
) == NULL
) {
718 error
= EADDRNOTAVAIL
;
721 bdaddr_copy(&pcb
->hp_raddr
, &sa
->bt_bdaddr
);
725 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
729 hci_speeraddr(netmsg_t msg
)
731 struct socket
*so
= msg
->peeraddr
.base
.nm_so
;
732 struct sockaddr
**nam
= msg
->peeraddr
.nm_nam
;
733 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
734 struct sockaddr_bt
*sa
;
736 KKASSERT(nam
!= NULL
);
737 sa
= (struct sockaddr_bt
*)nam
;
739 memset(sa
, 0, sizeof(struct sockaddr_bt
));
740 sa
->bt_len
= sizeof(struct sockaddr_bt
);
741 sa
->bt_family
= AF_BLUETOOTH
;
742 bdaddr_copy(&sa
->bt_bdaddr
, &pcb
->hp_raddr
);
744 lwkt_replymsg(&msg
->connect
.base
.lmsg
, 0);
748 hci_ssockaddr(netmsg_t msg
)
750 struct socket
*so
= msg
->sockaddr
.base
.nm_so
;
751 struct sockaddr
**nam
= msg
->sockaddr
.nm_nam
;
752 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
753 struct sockaddr_bt
*sa
;
755 KKASSERT(nam
!= NULL
);
756 sa
= (struct sockaddr_bt
*)nam
;
758 memset(sa
, 0, sizeof(struct sockaddr_bt
));
759 sa
->bt_len
= sizeof(struct sockaddr_bt
);
760 sa
->bt_family
= AF_BLUETOOTH
;
761 bdaddr_copy(&sa
->bt_bdaddr
, &pcb
->hp_laddr
);
763 lwkt_replymsg(&msg
->connect
.base
.lmsg
, 0);
767 hci_sshutdown(netmsg_t msg
)
769 struct socket
*so
= msg
->shutdown
.base
.nm_so
;
772 lwkt_replymsg(&msg
->connect
.base
.lmsg
, 0);
776 hci_ssend(netmsg_t msg
)
778 struct socket
*so
= msg
->send
.base
.nm_so
;
779 struct mbuf
*m
= msg
->send
.nm_m
;
780 struct sockaddr
*addr
= msg
->send
.nm_addr
;
781 struct mbuf
*control
= msg
->send
.nm_control
;
782 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
783 struct sockaddr_bt
*sa
;
788 sa
= (struct sockaddr_bt
*)addr
;
790 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
795 if (sa
->bt_family
!= AF_BLUETOOTH
) {
796 error
= EAFNOSUPPORT
;
801 /* have no use for this */
806 error
= hci_send(pcb
, m
, (sa
? &sa
->bt_bdaddr
: &pcb
->hp_raddr
));
814 lwkt_replymsg(&msg
->send
.base
.lmsg
, error
);
818 * get/set socket options
821 hci_ctloutput(netmsg_t msg
)
823 struct socket
*so
= msg
->ctloutput
.base
.nm_so
;
824 struct sockopt
*sopt
= msg
->ctloutput
.nm_sopt
;
825 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
829 #ifdef notyet /* XXX */
830 DPRINTFN(2, "req %s\n", prcorequests
[req
]);
838 if (sopt
->sopt_level
!= BTPROTO_HCI
) {
843 switch(sopt
->sopt_dir
) {
845 switch (sopt
->sopt_name
) {
846 case SO_HCI_EVT_FILTER
:
847 soopt_from_kbuf(sopt
, &pcb
->hp_efilter
,
848 sizeof(struct hci_filter
));
851 case SO_HCI_PKT_FILTER
:
852 soopt_from_kbuf(sopt
, &pcb
->hp_pfilter
,
853 sizeof(struct hci_filter
));
856 case SO_HCI_DIRECTION
:
857 if (pcb
->hp_flags
& HCI_DIRECTION
)
861 soopt_from_kbuf(sopt
, &idir
, sizeof(int));
871 switch (sopt
->sopt_name
) {
872 case SO_HCI_EVT_FILTER
: /* set event filter */
873 error
= soopt_to_kbuf(sopt
, &pcb
->hp_efilter
,
874 sizeof(struct hci_filter
),
875 sizeof(struct hci_filter
));
878 case SO_HCI_PKT_FILTER
: /* set packet filter */
879 error
= soopt_to_kbuf(sopt
, &pcb
->hp_pfilter
,
880 sizeof(struct hci_filter
),
881 sizeof(struct hci_filter
));
884 case SO_HCI_DIRECTION
: /* request direction ctl messages */
885 error
= soopt_to_kbuf(sopt
, &idir
, sizeof(int),
890 pcb
->hp_flags
|= HCI_DIRECTION
;
892 pcb
->hp_flags
&= ~HCI_DIRECTION
;
906 lwkt_replymsg(&msg
->ctloutput
.base
.lmsg
, error
);
910 * HCI mbuf tap routine
912 * copy packets to any raw HCI sockets that wish (and are
913 * permitted) to see them
916 hci_mtap(struct mbuf
*m
, struct hci_unit
*unit
)
919 struct mbuf
*m0
, *ctlmsg
, **ctl
;
920 struct sockaddr_bt sa
;
925 KKASSERT(m
->m_len
>= sizeof(type
));
927 type
= *mtod(m
, uint8_t *);
929 memset(&sa
, 0, sizeof(sa
));
930 sa
.bt_len
= sizeof(struct sockaddr_bt
);
931 sa
.bt_family
= AF_BLUETOOTH
;
932 bdaddr_copy(&sa
.bt_bdaddr
, &unit
->hci_bdaddr
);
934 LIST_FOREACH(pcb
, &hci_pcb
, hp_next
) {
936 * filter according to source address
938 if ((pcb
->hp_flags
& HCI_PROMISCUOUS
) == 0
939 && bdaddr_same(&pcb
->hp_laddr
, &sa
.bt_bdaddr
) == 0)
943 * filter according to packet type filter
945 if (hci_filter_test(type
, &pcb
->hp_pfilter
) == 0)
949 * filter according to event/security filters
953 KKASSERT(m
->m_len
>= sizeof(hci_event_hdr_t
));
955 event
= mtod(m
, hci_event_hdr_t
*)->event
;
957 if (hci_filter_test(event
, &pcb
->hp_efilter
) == 0)
960 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
961 && hci_security_check_event(event
) == -1)
966 KKASSERT(m
->m_len
>= sizeof(hci_cmd_hdr_t
));
968 opcode
= letoh16(mtod(m
, hci_cmd_hdr_t
*)->opcode
);
970 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
971 && hci_security_check_opcode(NULL
, opcode
) == -1)
975 case HCI_ACL_DATA_PKT
:
976 case HCI_SCO_DATA_PKT
:
978 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0)
985 * create control messages
989 if (pcb
->hp_flags
& HCI_DIRECTION
) {
990 int dir
= m
->m_flags
& IFF_LINK0
? 1 : 0;
992 *ctl
= sbcreatecontrol((void *)&dir
, sizeof(dir
),
993 SCM_HCI_DIRECTION
, BTPROTO_HCI
);
996 ctl
= &((*ctl
)->m_next
);
1002 m0
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
1003 if (m0
&& sbappendaddr(&pcb
->hp_socket
->so_rcv
.sb
,
1004 (struct sockaddr
*)&sa
, m0
, ctlmsg
)) {
1005 sorwakeup(pcb
->hp_socket
);
1013 struct pr_usrreqs hci_usrreqs
= {
1014 .pru_abort
= hci_sabort
,
1015 .pru_accept
= pr_generic_notsupp
,
1016 .pru_attach
= hci_sattach
,
1017 .pru_bind
= hci_sbind
,
1018 .pru_connect
= hci_sconnect
,
1019 .pru_connect2
= pr_generic_notsupp
,
1020 .pru_control
= hci_scontrol
,
1021 .pru_detach
= hci_sdetach
,
1022 .pru_disconnect
= hci_sdisconnect
,
1023 .pru_listen
= pr_generic_notsupp
,
1024 .pru_peeraddr
= hci_speeraddr
,
1025 .pru_rcvd
= pr_generic_notsupp
,
1026 .pru_rcvoob
= pr_generic_notsupp
,
1027 .pru_send
= hci_ssend
,
1028 .pru_sense
= pru_sense_null
,
1029 .pru_shutdown
= hci_sshutdown
,
1030 .pru_sockaddr
= hci_ssockaddr
,
1031 .pru_sosend
= sosend
,
1032 .pru_soreceive
= soreceive