1 /* $NetBSD: hci.c,v 1.2 2007/01/25 20:33:41 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/bthcid/hci.c,v 1.2 2008/02/02 09:21:24 swildner Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/ioctl.h>
60 #include <bluetooth.h>
69 static struct event hci_ev
;
71 static void process_hci
74 static int process_pin_code_request_event
75 (int, struct sockaddr_bt
*, bdaddr_t
*);
76 static int process_link_key_request_event
77 (int, struct sockaddr_bt
*, bdaddr_t
*);
78 static int process_link_key_notification_event
79 (int, struct sockaddr_bt
*, hci_link_key_notification_ep
*);
81 static int send_link_key_reply
82 (int, struct sockaddr_bt
*, bdaddr_t
*, uint8_t *);
83 static int send_hci_cmd
84 (int, struct sockaddr_bt
*, uint16_t, size_t, void *);
86 static char dev_name
[HCI_DEVNAME_SIZE
];
88 /* Initialise HCI Events */
90 init_hci(bdaddr_t
*bdaddr
)
92 struct sockaddr_bt sa
;
93 struct hci_filter filter
;
96 hci
= socket(PF_BLUETOOTH
, SOCK_RAW
, BTPROTO_HCI
);
100 memset(&sa
, 0, sizeof(sa
));
101 sa
.bt_len
= sizeof(sa
);
102 sa
.bt_family
= AF_BLUETOOTH
;
103 bdaddr_copy(&sa
.bt_bdaddr
, bdaddr
);
104 if (bind(hci
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0) {
109 memset(&filter
, 0, sizeof(filter
));
110 hci_filter_set(HCI_EVENT_PIN_CODE_REQ
, &filter
);
111 hci_filter_set(HCI_EVENT_LINK_KEY_REQ
, &filter
);
112 hci_filter_set(HCI_EVENT_LINK_KEY_NOTIFICATION
, &filter
);
114 if (setsockopt(hci
, BTPROTO_HCI
, SO_HCI_EVT_FILTER
,
115 (const void *)&filter
, sizeof(filter
)) < 0) {
120 event_set(&hci_ev
, hci
, EV_READ
| EV_PERSIST
, process_hci
, NULL
);
121 if (event_add(&hci_ev
, NULL
) < 0) {
129 /* Process an HCI event */
131 process_hci(int sock
, short ev __unused
, void *arg __unused
)
133 char buffer
[HCI_EVENT_PKT_SIZE
];
134 hci_event_hdr_t
*event
= (hci_event_hdr_t
*)buffer
;
135 struct sockaddr_bt addr
;
140 n
= recvfrom(sock
, buffer
, sizeof(buffer
), 0,
141 (struct sockaddr
*) &addr
, &size
);
143 syslog(LOG_ERR
, "Could not receive from HCI socket: %m");
147 if (event
->type
!= HCI_EVENT_PKT
) {
148 syslog(LOG_ERR
, "Received unexpected HCI packet, "
149 "type=%#x", event
->type
);
154 if (!bt_devname(dev_name
, &addr
.bt_bdaddr
))
155 strlcpy(dev_name
, "unknown", sizeof(dev_name
));
157 switch (event
->event
) {
158 case HCI_EVENT_PIN_CODE_REQ
:
159 process_pin_code_request_event(sock
, &addr
,
160 (bdaddr_t
*)(event
+ 1));
163 case HCI_EVENT_LINK_KEY_REQ
:
164 process_link_key_request_event(sock
, &addr
,
165 (bdaddr_t
*)(event
+ 1));
168 case HCI_EVENT_LINK_KEY_NOTIFICATION
:
169 process_link_key_notification_event(sock
, &addr
,
170 (hci_link_key_notification_ep
*)(event
+ 1));
174 syslog(LOG_ERR
, "Received unexpected HCI event, "
175 "event=%#x", event
->event
);
182 /* Process PIN_Code_Request event */
184 process_pin_code_request_event(int sock
, struct sockaddr_bt
*addr
,
187 /* TODO: Add search pin in config file */
192 syslog(LOG_DEBUG
, "Got PIN_Code_Request event from %s, "
195 bt_ntoa(bdaddr
, NULL
));
197 pin
= lookup_pin(&addr
->bt_bdaddr
, bdaddr
);
199 return send_pin_code_reply(sock
, addr
, bdaddr
, pin
);
201 pin2
= lookup_pin_conf(&addr
->bt_bdaddr
, bdaddr
);
203 return send_pin_code_reply(sock
, addr
, bdaddr
, pin2
);
206 if (send_client_request(&addr
->bt_bdaddr
, bdaddr
, sock
) == 0)
207 return send_pin_code_reply(sock
, addr
, bdaddr
, NULL
);
212 /* Process Link_Key_Request event */
214 process_link_key_request_event(int sock
, struct sockaddr_bt
*addr
,
220 "Got Link_Key_Request event from %s, remote bdaddr %s",
221 dev_name
, bt_ntoa(bdaddr
, NULL
));
223 key
= lookup_key(&addr
->bt_bdaddr
, bdaddr
);
226 syslog(LOG_DEBUG
, "Found Key, remote bdaddr %s",
227 bt_ntoa(bdaddr
, NULL
));
229 return send_link_key_reply(sock
, addr
, bdaddr
, key
);
232 syslog(LOG_DEBUG
, "Could not find link key for remote bdaddr %s",
233 bt_ntoa(bdaddr
, NULL
));
235 return send_link_key_reply(sock
, addr
, bdaddr
, NULL
);
238 /* Send PIN_Code_[Negative]_Reply */
240 send_pin_code_reply(int sock
, struct sockaddr_bt
*addr
,
241 bdaddr_t
*bdaddr
, uint8_t *pin
)
246 hci_pin_code_rep_cp cp
;
248 syslog(LOG_DEBUG
, "Sending PIN_Code_Reply to %s "
249 "for remote bdaddr %s",
251 bt_ntoa(bdaddr
, NULL
));
253 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
254 memcpy(cp
.pin
, pin
, HCI_PIN_SIZE
);
257 while (n
> 0 && pin
[n
- 1] == 0)
261 n
= send_hci_cmd(sock
, addr
,
262 HCI_CMD_PIN_CODE_REP
, sizeof(cp
), &cp
);
265 syslog(LOG_DEBUG
, "Sending PIN_Code_Negative_Reply to %s "
266 "for remote bdaddr %s",
268 bt_ntoa(bdaddr
, NULL
));
270 n
= send_hci_cmd(sock
, addr
, HCI_CMD_PIN_CODE_NEG_REP
,
271 sizeof(bdaddr_t
), bdaddr
);
275 syslog(LOG_ERR
, "Could not send PIN code reply to %s "
276 "for remote bdaddr %s: %m",
278 bt_ntoa(bdaddr
, NULL
));
286 /* Send Link_Key_[Negative]_Reply */
288 send_link_key_reply(int sock
, struct sockaddr_bt
*addr
,
289 bdaddr_t
*bdaddr
, uint8_t *key
)
294 hci_link_key_rep_cp cp
;
296 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
297 memcpy(&cp
.key
, key
, sizeof(cp
.key
));
299 syslog(LOG_DEBUG
, "Sending Link_Key_Reply to %s "
300 "for remote bdaddr %s",
301 dev_name
, bt_ntoa(bdaddr
, NULL
));
303 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_REP
, sizeof(cp
), &cp
);
305 hci_link_key_neg_rep_cp cp
;
307 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
309 syslog(LOG_DEBUG
, "Sending Link_Key_Negative_Reply to %s "
310 "for remote bdaddr %s",
311 dev_name
, bt_ntoa(bdaddr
, NULL
));
313 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_NEG_REP
, sizeof(cp
), &cp
);
317 syslog(LOG_ERR
, "Could not send link key reply to %s "
318 "for remote bdaddr %s: %m",
319 dev_name
, bt_ntoa(bdaddr
, NULL
));
326 /* Process Link_Key_Notification event */
328 process_link_key_notification_event(int sock __unused
, struct sockaddr_bt
*addr
,
329 hci_link_key_notification_ep
*ep
)
332 syslog(LOG_DEBUG
, "Got Link_Key_Notification event from %s, "
335 bt_ntoa(&ep
->bdaddr
, NULL
));
337 save_key(&addr
->bt_bdaddr
, &ep
->bdaddr
, ep
->key
);
341 /* Send HCI Command Packet to socket */
343 send_hci_cmd(int sock
, struct sockaddr_bt
*sa
, uint16_t opcode
, size_t len
, void *buf
)
345 char msg
[HCI_CMD_PKT_SIZE
];
346 hci_cmd_hdr_t
*h
= (hci_cmd_hdr_t
*)msg
;
348 h
->type
= HCI_CMD_PKT
;
349 h
->opcode
= htole16(opcode
);
353 memcpy(msg
+ sizeof(hci_cmd_hdr_t
), buf
, len
);
355 return sendto(sock
, msg
, sizeof(hci_cmd_hdr_t
) + len
, 0,
356 (struct sockaddr
*)sa
, sizeof(*sa
));