1 /* $DragonFly: src/sys/netbt/hci_socket.c,v 1.2 2008/03/18 13:41:42 hasso 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>
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>
54 #include <sys/thread2.h>
56 #include <netbt/bluetooth.h>
57 #include <netbt/hci.h>
59 /*******************************************************************************
61 * HCI SOCK_RAW Sockets - for control of Bluetooth Devices
66 * the raw HCI protocol control block
69 struct socket
*hp_socket
; /* socket */
70 unsigned int hp_flags
; /* flags */
71 bdaddr_t hp_laddr
; /* local address */
72 bdaddr_t hp_raddr
; /* remote address */
73 struct hci_filter hp_efilter
; /* user event filter */
74 struct hci_filter hp_pfilter
; /* user packet filter */
75 LIST_ENTRY(hci_pcb
) hp_next
; /* next HCI pcb */
79 #define HCI_PRIVILEGED (1<<0) /* no security filter for root */
80 #define HCI_DIRECTION (1<<1) /* direction control messages */
81 #define HCI_PROMISCUOUS (1<<2) /* listen to all units */
83 LIST_HEAD(hci_pcb_list
, hci_pcb
) hci_pcb
= LIST_HEAD_INITIALIZER(hci_pcb
);
86 int hci_sendspace
= HCI_CMD_PKT_SIZE
;
87 int hci_recvspace
= 4096;
89 extern struct pr_usrreqs hci_usrreqs
;
91 /* Prototypes for usrreqs methods. */
92 static int hci_sabort (struct socket
*so
);
93 static int hci_sdetach(struct socket
*so
);
94 static int hci_sdisconnect (struct socket
*so
);
95 static int hci_scontrol (struct socket
*so
, u_long cmd
, caddr_t data
,
96 struct ifnet
*ifp
, struct thread
*td
);
97 static int hci_sattach (struct socket
*so
, int proto
,
98 struct pru_attach_info
*ai
);
99 static int hci_sbind (struct socket
*so
, struct sockaddr
*nam
,
101 static int hci_sconnect (struct socket
*so
, struct sockaddr
*nam
,
103 static int hci_speeraddr (struct socket
*so
, struct sockaddr
**nam
);
104 static int hci_ssockaddr (struct socket
*so
, struct sockaddr
**nam
);
105 static int hci_sshutdown (struct socket
*so
);
106 static int hci_ssend (struct socket
*so
, int flags
, struct mbuf
*m
,
107 struct sockaddr
*addr
, struct mbuf
*control
,
110 /* supported commands opcode table */
111 static const struct {
113 uint8_t offs
; /* 0 - 63 */
114 uint8_t mask
; /* bit 0 - 7 */
115 int16_t length
; /* -1 if privileged */
118 0, 0x01, sizeof(hci_inquiry_cp
) },
119 { HCI_CMD_INQUIRY_CANCEL
,
121 { HCI_CMD_PERIODIC_INQUIRY
,
123 { HCI_CMD_EXIT_PERIODIC_INQUIRY
,
125 { HCI_CMD_CREATE_CON
,
127 { HCI_CMD_DISCONNECT
,
129 { HCI_CMD_ADD_SCO_CON
,
131 { HCI_CMD_CREATE_CON_CANCEL
,
133 { HCI_CMD_ACCEPT_CON
,
135 { HCI_CMD_REJECT_CON
,
137 { HCI_CMD_LINK_KEY_REP
,
139 { HCI_CMD_LINK_KEY_NEG_REP
,
141 { HCI_CMD_PIN_CODE_REP
,
143 { HCI_CMD_PIN_CODE_NEG_REP
,
145 { HCI_CMD_CHANGE_CON_PACKET_TYPE
,
149 { HCI_CMD_SET_CON_ENCRYPTION
,
151 { HCI_CMD_CHANGE_CON_LINK_KEY
,
153 { HCI_CMD_MASTER_LINK_KEY
,
155 { HCI_CMD_REMOTE_NAME_REQ
,
156 2, 0x08, sizeof(hci_remote_name_req_cp
) },
157 { HCI_CMD_REMOTE_NAME_REQ_CANCEL
,
159 { HCI_CMD_READ_REMOTE_FEATURES
,
160 2, 0x20, sizeof(hci_read_remote_features_cp
) },
161 { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES
,
162 2, 0x40, sizeof(hci_read_remote_extended_features_cp
) },
163 { HCI_CMD_READ_REMOTE_VER_INFO
,
164 2, 0x80, sizeof(hci_read_remote_ver_info_cp
) },
165 { HCI_CMD_READ_CLOCK_OFFSET
,
166 3, 0x01, sizeof(hci_read_clock_offset_cp
) },
167 { HCI_CMD_READ_LMP_HANDLE
,
168 3, 0x02, sizeof(hci_read_lmp_handle_cp
) },
171 { HCI_CMD_SNIFF_MODE
,
173 { HCI_CMD_EXIT_SNIFF_MODE
,
177 { HCI_CMD_EXIT_PARK_MODE
,
181 { HCI_CMD_ROLE_DISCOVERY
,
182 4, 0x80, sizeof(hci_role_discovery_cp
) },
183 { HCI_CMD_SWITCH_ROLE
,
185 { HCI_CMD_READ_LINK_POLICY_SETTINGS
,
186 5, 0x02, sizeof(hci_read_link_policy_settings_cp
) },
187 { HCI_CMD_WRITE_LINK_POLICY_SETTINGS
,
189 { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS
,
191 { HCI_CMD_WRITE_DEFAULT_LINK_POLICY_SETTINGS
,
193 { HCI_CMD_FLOW_SPECIFICATION
,
195 { HCI_CMD_SET_EVENT_MASK
,
199 { HCI_CMD_SET_EVENT_FILTER
,
203 { HCI_CMD_READ_PIN_TYPE
,
205 { HCI_CMD_WRITE_PIN_TYPE
,
207 { HCI_CMD_CREATE_NEW_UNIT_KEY
,
209 { HCI_CMD_READ_STORED_LINK_KEY
,
211 { HCI_CMD_WRITE_STORED_LINK_KEY
,
213 { HCI_CMD_DELETE_STORED_LINK_KEY
,
215 { HCI_CMD_WRITE_LOCAL_NAME
,
217 { HCI_CMD_READ_LOCAL_NAME
,
219 { HCI_CMD_READ_CON_ACCEPT_TIMEOUT
,
221 { HCI_CMD_WRITE_CON_ACCEPT_TIMEOUT
,
223 { HCI_CMD_READ_PAGE_TIMEOUT
,
225 { HCI_CMD_WRITE_PAGE_TIMEOUT
,
227 { HCI_CMD_READ_SCAN_ENABLE
,
229 { HCI_CMD_WRITE_SCAN_ENABLE
,
231 { HCI_CMD_READ_PAGE_SCAN_ACTIVITY
,
233 { HCI_CMD_WRITE_PAGE_SCAN_ACTIVITY
,
235 { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY
,
237 { HCI_CMD_WRITE_INQUIRY_SCAN_ACTIVITY
,
239 { HCI_CMD_READ_AUTH_ENABLE
,
241 { HCI_CMD_WRITE_AUTH_ENABLE
,
243 { HCI_CMD_READ_ENCRYPTION_MODE
,
245 { HCI_CMD_WRITE_ENCRYPTION_MODE
,
247 { HCI_CMD_READ_UNIT_CLASS
,
249 { HCI_CMD_WRITE_UNIT_CLASS
,
251 { HCI_CMD_READ_VOICE_SETTING
,
253 { HCI_CMD_WRITE_VOICE_SETTING
,
255 { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT
,
256 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp
) },
257 { HCI_CMD_WRITE_AUTO_FLUSH_TIMEOUT
,
259 { HCI_CMD_READ_NUM_BROADCAST_RETRANS
,
261 { HCI_CMD_WRITE_NUM_BROADCAST_RETRANS
,
263 { HCI_CMD_READ_HOLD_MODE_ACTIVITY
,
265 { HCI_CMD_WRITE_HOLD_MODE_ACTIVITY
,
267 { HCI_CMD_READ_XMIT_LEVEL
,
268 10, 0x04, sizeof(hci_read_xmit_level_cp
) },
269 { HCI_CMD_READ_SCO_FLOW_CONTROL
,
271 { HCI_CMD_WRITE_SCO_FLOW_CONTROL
,
273 { HCI_CMD_HC2H_FLOW_CONTROL
,
275 { HCI_CMD_HOST_BUFFER_SIZE
,
277 { HCI_CMD_HOST_NUM_COMPL_PKTS
,
279 { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT
,
280 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp
) },
281 { HCI_CMD_WRITE_LINK_SUPERVISION_TIMEOUT
,
283 { HCI_CMD_READ_NUM_SUPPORTED_IAC
,
285 { HCI_CMD_READ_IAC_LAP
,
287 { HCI_CMD_WRITE_IAC_LAP
,
289 { HCI_CMD_READ_PAGE_SCAN_PERIOD
,
291 { HCI_CMD_WRITE_PAGE_SCAN_PERIOD
,
293 { HCI_CMD_READ_PAGE_SCAN
,
295 { HCI_CMD_WRITE_PAGE_SCAN
,
297 { HCI_CMD_SET_AFH_CLASSIFICATION
,
299 { HCI_CMD_READ_INQUIRY_SCAN_TYPE
,
301 { HCI_CMD_WRITE_INQUIRY_SCAN_TYPE
,
303 { HCI_CMD_READ_INQUIRY_MODE
,
305 { HCI_CMD_WRITE_INQUIRY_MODE
,
307 { HCI_CMD_READ_PAGE_SCAN_TYPE
,
309 { HCI_CMD_WRITE_PAGE_SCAN_TYPE
,
311 { HCI_CMD_READ_AFH_ASSESSMENT
,
313 { HCI_CMD_WRITE_AFH_ASSESSMENT
,
315 { HCI_CMD_READ_LOCAL_VER
,
317 { HCI_CMD_READ_LOCAL_COMMANDS
,
319 { HCI_CMD_READ_LOCAL_FEATURES
,
321 { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES
,
322 14, 0x40, sizeof(hci_read_local_extended_features_cp
) },
323 { HCI_CMD_READ_BUFFER_SIZE
,
325 { HCI_CMD_READ_COUNTRY_CODE
,
327 { HCI_CMD_READ_BDADDR
,
329 { HCI_CMD_READ_FAILED_CONTACT_CNTR
,
330 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp
) },
331 { HCI_CMD_RESET_FAILED_CONTACT_CNTR
,
333 { HCI_CMD_READ_LINK_QUALITY
,
334 15, 0x10, sizeof(hci_read_link_quality_cp
) },
336 15, 0x20, sizeof(hci_read_rssi_cp
) },
337 { HCI_CMD_READ_AFH_CHANNEL_MAP
,
338 15, 0x40, sizeof(hci_read_afh_channel_map_cp
) },
339 { HCI_CMD_READ_CLOCK
,
340 15, 0x80, sizeof(hci_read_clock_cp
) },
341 { HCI_CMD_READ_LOOPBACK_MODE
,
343 { HCI_CMD_WRITE_LOOPBACK_MODE
,
345 { HCI_CMD_ENABLE_UNIT_UNDER_TEST
,
347 { HCI_CMD_SETUP_SCO_CON
,
349 { HCI_CMD_ACCEPT_SCO_CON_REQ
,
351 { HCI_CMD_REJECT_SCO_CON_REQ
,
353 { HCI_CMD_READ_EXTENDED_INQUIRY_RSP
,
355 { HCI_CMD_WRITE_EXTENDED_INQUIRY_RSP
,
357 { HCI_CMD_REFRESH_ENCRYPTION_KEY
,
359 { HCI_CMD_SNIFF_SUBRATING
,
361 { HCI_CMD_READ_SIMPLE_PAIRING_MODE
,
363 { HCI_CMD_WRITE_SIMPLE_PAIRING_MODE
,
365 { HCI_CMD_READ_LOCAL_OOB_DATA
,
367 { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER
,
369 { HCI_CMD_WRITE_INQUIRY_RSP_XMIT_POWER
,
371 { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING
,
373 { HCI_CMD_WRITE_DEFAULT_ERRDATA_REPORTING
,
375 { HCI_CMD_IO_CAPABILITY_REP
,
377 { HCI_CMD_USER_CONFIRM_REP
,
379 { HCI_CMD_USER_CONFIRM_NEG_REP
,
381 { HCI_CMD_USER_PASSKEY_REP
,
383 { HCI_CMD_USER_PASSKEY_NEG_REP
,
385 { HCI_CMD_OOB_DATA_REP
,
387 { HCI_CMD_WRITE_SIMPLE_PAIRING_DEBUG_MODE
,
389 { HCI_CMD_ENHANCED_FLUSH
,
391 { HCI_CMD_OOB_DATA_NEG_REP
,
393 { HCI_CMD_SEND_KEYPRESS_NOTIFICATION
,
395 { HCI_CMD_IO_CAPABILITY_NEG_REP
,
400 * Security filter routines for unprivileged users.
401 * Allow all but a few critical events, and only permit read commands.
402 * If a unit is given, verify the command is supported.
406 hci_security_check_opcode(struct hci_unit
*unit
, uint16_t opcode
)
410 for (i
= 0 ; i
< sizeof(hci_cmds
) / sizeof(hci_cmds
[0]); i
++) {
411 if (opcode
!= hci_cmds
[i
].opcode
)
415 || (unit
->hci_cmds
[hci_cmds
[i
].offs
] & hci_cmds
[i
].mask
))
416 return hci_cmds
[i
].length
;
425 hci_security_check_event(uint8_t event
)
429 case HCI_EVENT_RETURN_LINK_KEYS
:
430 case HCI_EVENT_LINK_KEY_NOTIFICATION
:
431 case HCI_EVENT_USER_CONFIRM_REQ
:
432 case HCI_EVENT_USER_PASSKEY_NOTIFICATION
:
433 case HCI_EVENT_VENDOR
:
434 return -1; /* disallowed */
441 * When command packet reaches the device, we can drop
442 * it from the socket buffer (called from hci_output_acl)
447 struct socket
*so
= arg
;
449 sbdroprecord(&so
->so_snd
.sb
);
454 * HCI socket is going away and has some pending packets. We let them
455 * go by design, but remove the context pointer as it will be invalid
456 * and we no longer need to be notified.
459 hci_cmdwait_flush(struct socket
*so
)
461 struct hci_unit
*unit
;
465 DPRINTF("flushing %p\n", so
);
467 TAILQ_FOREACH(unit
, &hci_unit_list
, hci_next
) {
468 IF_POLL(&unit
->hci_cmdwait
, m
);
470 ctx
= M_GETCTX(m
, struct socket
*);
481 * This came from userland, so check it out.
484 hci_send(struct hci_pcb
*pcb
, struct mbuf
*m
, bdaddr_t
*addr
)
486 struct hci_unit
*unit
;
492 KKASSERT(addr
!= NULL
);
494 /* wants at least a header to start with */
495 if (m
->m_pkthdr
.len
< sizeof(hdr
)) {
499 m_copydata(m
, 0, sizeof(hdr
), (caddr_t
)&hdr
);
500 hdr
.opcode
= letoh16(hdr
.opcode
);
502 /* only allows CMD packets to be sent */
503 if (hdr
.type
!= HCI_CMD_PKT
) {
508 /* validates packet length */
509 if (m
->m_pkthdr
.len
!= sizeof(hdr
) + hdr
.length
) {
514 /* finds destination */
515 unit
= hci_unit_lookup(addr
);
521 /* security checks for unprivileged users */
522 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
523 && hci_security_check_opcode(unit
, hdr
.opcode
) != hdr
.length
) {
528 /* makes a copy for precious to keep */
529 m0
= m_copym(m
, 0, M_COPYALL
, MB_DONTWAIT
);
534 sbappendrecord(&pcb
->hp_socket
->so_snd
.sb
, m0
);
535 M_SETCTX(m
, pcb
->hp_socket
); /* enable drop callback */
537 DPRINTFN(2, "(%s) opcode (%03x|%04x)\n",
538 device_get_nameunit(unit
->hci_dev
),
539 HCI_OGF(hdr
.opcode
), HCI_OCF(hdr
.opcode
));
542 if (unit
->hci_num_cmd_pkts
== 0)
543 IF_ENQUEUE(&unit
->hci_cmdwait
, m
);
545 hci_output_cmd(unit
, m
);
550 DPRINTF("packet (%d bytes) not sent (error %d)\n",
551 m
->m_pkthdr
.len
, err
);
557 * Implementation of usrreqs.
560 hci_sabort (struct socket
*so
)
562 /* struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; */
564 soisdisconnected(so
);
565 return hci_sdetach(so
);
569 hci_sdetach(struct socket
*so
)
571 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
575 if (so
->so_snd
.ssb_mb
!= NULL
)
576 hci_cmdwait_flush(so
);
579 LIST_REMOVE(pcb
, hp_next
);
586 hci_sdisconnect (struct socket
*so
)
588 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
593 bdaddr_copy(&pcb
->hp_raddr
, BDADDR_ANY
);
595 * XXX We cannot call soisdisconnected() here, as it sets
596 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem is that
597 * soisconnected() does not clear these and if you try to reconnect
598 * this socket (which is permitted) you get a broken pipe when you
599 * try to write any data.
601 so
->so_state
&= ~SS_ISCONNECTED
;
607 hci_scontrol (struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
610 return hci_ioctl(cmd
, (void*)data
, NULL
);
614 hci_sattach (struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
616 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
622 err
= soreserve(so
, hci_sendspace
, hci_recvspace
,NULL
);
626 pcb
= kmalloc(sizeof *pcb
, M_PCB
, M_NOWAIT
| M_ZERO
);
633 if (curproc
== NULL
|| suser(curthread
) == 0)
634 pcb
->hp_flags
|= HCI_PRIVILEGED
;
637 * Set default user filter. By default, socket only passes
638 * Command_Complete and Command_Status Events.
640 hci_filter_set(HCI_EVENT_COMMAND_COMPL
, &pcb
->hp_efilter
);
641 hci_filter_set(HCI_EVENT_COMMAND_STATUS
, &pcb
->hp_efilter
);
642 hci_filter_set(HCI_EVENT_PKT
, &pcb
->hp_pfilter
);
645 LIST_INSERT_HEAD(&hci_pcb
, pcb
, hp_next
);
652 hci_sbind (struct socket
*so
, struct sockaddr
*nam
,
655 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
656 struct sockaddr_bt
*sa
;
658 KKASSERT(nam
!= NULL
);
659 sa
= (struct sockaddr_bt
*)nam
;
661 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
664 if (sa
->bt_family
!= AF_BLUETOOTH
)
667 bdaddr_copy(&pcb
->hp_laddr
, &sa
->bt_bdaddr
);
669 if (bdaddr_any(&sa
->bt_bdaddr
))
670 pcb
->hp_flags
|= HCI_PROMISCUOUS
;
672 pcb
->hp_flags
&= ~HCI_PROMISCUOUS
;
678 hci_sconnect (struct socket
*so
, struct sockaddr
*nam
,
681 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
682 struct sockaddr_bt
*sa
;
683 KKASSERT(nam
!= NULL
);
684 sa
= (struct sockaddr_bt
*)nam
;
686 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
689 if (sa
->bt_family
!= AF_BLUETOOTH
)
692 if (hci_unit_lookup(&sa
->bt_bdaddr
) == NULL
)
693 return EADDRNOTAVAIL
;
695 bdaddr_copy(&pcb
->hp_raddr
, &sa
->bt_bdaddr
);
702 hci_speeraddr (struct socket
*so
, struct sockaddr
**nam
)
704 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
705 struct sockaddr_bt
*sa
;
707 KKASSERT(nam
!= NULL
);
708 sa
= (struct sockaddr_bt
*)nam
;
710 memset(sa
, 0, sizeof(struct sockaddr_bt
));
711 sa
->bt_len
= sizeof(struct sockaddr_bt
);
712 sa
->bt_family
= AF_BLUETOOTH
;
713 bdaddr_copy(&sa
->bt_bdaddr
, &pcb
->hp_raddr
);
719 hci_ssockaddr (struct socket
*so
, struct sockaddr
**nam
)
721 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
722 struct sockaddr_bt
*sa
;
724 KKASSERT(nam
!= NULL
);
725 sa
= (struct sockaddr_bt
*)nam
;
727 memset(sa
, 0, sizeof(struct sockaddr_bt
));
728 sa
->bt_len
= sizeof(struct sockaddr_bt
);
729 sa
->bt_family
= AF_BLUETOOTH
;
730 bdaddr_copy(&sa
->bt_bdaddr
, &pcb
->hp_laddr
);
736 hci_sshutdown (struct socket
*so
)
743 hci_ssend (struct socket
*so
, int flags
, struct mbuf
*m
,
744 struct sockaddr
*addr
, struct mbuf
*control
,
748 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
749 struct sockaddr_bt
*sa
;
753 sa
= (struct sockaddr_bt
*)addr
;
755 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
)) {
758 if (control
) m_freem(control
);
762 if (sa
->bt_family
!= AF_BLUETOOTH
) {
765 if (control
) m_freem(control
);
770 if (control
) /* have no use for this */
773 return hci_send(pcb
, m
, (sa
? &sa
->bt_bdaddr
: &pcb
->hp_raddr
));
777 * get/set socket options
780 hci_ctloutput (struct socket
*so
, struct sockopt
*sopt
)
782 struct hci_pcb
*pcb
= (struct hci_pcb
*)so
->so_pcb
;
786 #ifdef notyet /* XXX */
787 DPRINTFN(2, "req %s\n", prcorequests
[req
]);
793 if (sopt
->sopt_level
!= BTPROTO_HCI
)
796 switch(sopt
->sopt_dir
) {
798 switch (sopt
->sopt_name
) {
799 case SO_HCI_EVT_FILTER
:
800 err
= sooptcopyout(sopt
, &pcb
->hp_efilter
,
801 sizeof(struct hci_filter
));
804 case SO_HCI_PKT_FILTER
:
805 err
= sooptcopyout(sopt
, &pcb
->hp_pfilter
,
806 sizeof(struct hci_filter
));
809 case SO_HCI_DIRECTION
:
810 if (pcb
->hp_flags
& HCI_DIRECTION
)
814 err
= sooptcopyout(sopt
, &idir
, sizeof(int));
824 switch (sopt
->sopt_name
) {
825 case SO_HCI_EVT_FILTER
: /* set event filter */
826 err
= sooptcopyin(sopt
, &pcb
->hp_efilter
,
827 sizeof(struct hci_filter
),
828 sizeof(struct hci_filter
));
831 case SO_HCI_PKT_FILTER
: /* set packet filter */
832 err
= sooptcopyin(sopt
, &pcb
->hp_pfilter
,
833 sizeof(struct hci_filter
),
834 sizeof(struct hci_filter
));
837 case SO_HCI_DIRECTION
: /* request direction ctl messages */
838 err
= sooptcopyin(sopt
, &idir
, sizeof(int),
842 pcb
->hp_flags
|= HCI_DIRECTION
;
844 pcb
->hp_flags
&= ~HCI_DIRECTION
;
862 * HCI mbuf tap routine
864 * copy packets to any raw HCI sockets that wish (and are
865 * permitted) to see them
868 hci_mtap(struct mbuf
*m
, struct hci_unit
*unit
)
871 struct mbuf
*m0
, *ctlmsg
, **ctl
;
872 struct sockaddr_bt sa
;
877 KKASSERT(m
->m_len
>= sizeof(type
));
879 type
= *mtod(m
, uint8_t *);
881 memset(&sa
, 0, sizeof(sa
));
882 sa
.bt_len
= sizeof(struct sockaddr_bt
);
883 sa
.bt_family
= AF_BLUETOOTH
;
884 bdaddr_copy(&sa
.bt_bdaddr
, &unit
->hci_bdaddr
);
886 LIST_FOREACH(pcb
, &hci_pcb
, hp_next
) {
888 * filter according to source address
890 if ((pcb
->hp_flags
& HCI_PROMISCUOUS
) == 0
891 && bdaddr_same(&pcb
->hp_laddr
, &sa
.bt_bdaddr
) == 0)
895 * filter according to packet type filter
897 if (hci_filter_test(type
, &pcb
->hp_pfilter
) == 0)
901 * filter according to event/security filters
905 KKASSERT(m
->m_len
>= sizeof(hci_event_hdr_t
));
907 event
= mtod(m
, hci_event_hdr_t
*)->event
;
909 if (hci_filter_test(event
, &pcb
->hp_efilter
) == 0)
912 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
913 && hci_security_check_event(event
) == -1)
918 KKASSERT(m
->m_len
>= sizeof(hci_cmd_hdr_t
));
920 opcode
= letoh16(mtod(m
, hci_cmd_hdr_t
*)->opcode
);
922 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0
923 && hci_security_check_opcode(NULL
, opcode
) == -1)
927 case HCI_ACL_DATA_PKT
:
928 case HCI_SCO_DATA_PKT
:
930 if ((pcb
->hp_flags
& HCI_PRIVILEGED
) == 0)
937 * create control messages
941 if (pcb
->hp_flags
& HCI_DIRECTION
) {
942 int dir
= m
->m_flags
& IFF_LINK0
? 1 : 0;
944 *ctl
= sbcreatecontrol((void *)&dir
, sizeof(dir
),
945 SCM_HCI_DIRECTION
, BTPROTO_HCI
);
948 ctl
= &((*ctl
)->m_next
);
954 m0
= m_copym(m
, 0, M_COPYALL
, MB_DONTWAIT
);
955 if (m0
&& sbappendaddr(&pcb
->hp_socket
->so_rcv
.sb
,
956 (struct sockaddr
*)&sa
, m0
, ctlmsg
)) {
957 sorwakeup(pcb
->hp_socket
);
965 struct pr_usrreqs hci_usrreqs
= {
966 .pru_abort
= hci_sabort
,
967 .pru_accept
= pru_accept_notsupp
,
968 .pru_attach
= hci_sattach
,
969 .pru_bind
= hci_sbind
,
970 .pru_connect
= hci_sconnect
,
971 .pru_connect2
= pru_connect2_notsupp
,
972 .pru_control
= hci_scontrol
,
973 .pru_detach
= hci_sdetach
,
974 .pru_disconnect
= hci_sdisconnect
,
975 .pru_listen
= pru_listen_notsupp
,
976 .pru_peeraddr
= hci_speeraddr
,
977 .pru_rcvd
= pru_rcvd_notsupp
,
978 .pru_rcvoob
= pru_rcvoob_notsupp
,
979 .pru_send
= hci_ssend
,
980 .pru_sense
= pru_sense_null
,
981 .pru_shutdown
= hci_sshutdown
,
982 .pru_sockaddr
= hci_ssockaddr
,
983 .pru_sosend
= sosend
,
984 .pru_soreceive
= soreceive
,