forked from NetBSD/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhci.c
340 lines (276 loc) · 9.27 KB
/
hci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* $NetBSD: hci.c,v 1.4 2024/09/07 13:57:25 mlelstv Exp $ */
/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: hci.c,v 1.4 2024/09/07 13:57:25 mlelstv Exp $");
#include <sys/ioctl.h>
#include <sys/time.h>
#include <bluetooth.h>
#include <errno.h>
#include <event.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "bthcid.h"
static struct event hci_ev;
static void process_hci
(int, short, void *);
static int process_pin_code_request_event
(int, struct sockaddr_bt *, bdaddr_t *);
static int process_link_key_request_event
(int, struct sockaddr_bt *, bdaddr_t *);
static int process_link_key_notification_event
(int, struct sockaddr_bt *, hci_link_key_notification_ep *);
static int send_link_key_reply
(int, struct sockaddr_bt *, bdaddr_t *, uint8_t *);
static int send_hci_cmd
(int, struct sockaddr_bt *, uint16_t, size_t, void *);
static char dev_name[HCI_DEVNAME_SIZE];
/* Initialise HCI Events */
int
init_hci(const char *device)
{
struct bt_devfilter filter;
int hci;
hci = bt_devopen(device, 0);
if (hci < 0)
return -1;
memset(&filter, 0, sizeof(filter));
bt_devfilter_pkt_set(&filter, HCI_EVENT_PKT);
bt_devfilter_evt_set(&filter, HCI_EVENT_PIN_CODE_REQ);
bt_devfilter_evt_set(&filter, HCI_EVENT_LINK_KEY_REQ);
bt_devfilter_evt_set(&filter, HCI_EVENT_LINK_KEY_NOTIFICATION);
if (bt_devfilter(hci, &filter, NULL) < 0) {
close(hci);
return -1;
}
event_set(&hci_ev, hci, EV_READ | EV_PERSIST, process_hci, NULL);
if (event_add(&hci_ev, NULL) < 0) {
close(hci);
return -1;
}
return 0;
}
/* Process an HCI event */
static void
process_hci(int sock, short ev, void *arg)
{
char buffer[HCI_EVENT_PKT_SIZE];
hci_event_hdr_t *event = (hci_event_hdr_t *)buffer;
struct sockaddr_bt addr;
int n;
socklen_t size;
size = sizeof(addr);
n = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *) &addr, &size);
if (n < 0) {
syslog(LOG_ERR, "Could not receive from HCI socket: %m");
return;
}
if (event->type != HCI_EVENT_PKT) {
syslog(LOG_ERR, "Received unexpected HCI packet, "
"type=%#x", event->type);
return;
}
if (!bt_devname(dev_name, &addr.bt_bdaddr))
strlcpy(dev_name, "unknown", sizeof(dev_name));
switch (event->event) {
case HCI_EVENT_PIN_CODE_REQ:
process_pin_code_request_event(sock, &addr,
(bdaddr_t *)(event + 1));
break;
case HCI_EVENT_LINK_KEY_REQ:
process_link_key_request_event(sock, &addr,
(bdaddr_t *)(event + 1));
break;
case HCI_EVENT_LINK_KEY_NOTIFICATION:
process_link_key_notification_event(sock, &addr,
(hci_link_key_notification_ep *)(event + 1));
break;
default:
syslog(LOG_ERR, "Received unexpected HCI event, "
"event=%#x", event->event);
break;
}
return;
}
/* Process PIN_Code_Request event */
static int
process_pin_code_request_event(int sock, struct sockaddr_bt *addr,
bdaddr_t *bdaddr)
{
uint8_t *pin;
syslog(LOG_DEBUG, "Got PIN_Code_Request event from %s, "
"remote bdaddr %s",
dev_name,
bt_ntoa(bdaddr, NULL));
pin = lookup_pin(&addr->bt_bdaddr, bdaddr);
if (pin != NULL)
return send_pin_code_reply(sock, addr, bdaddr, pin);
if (send_client_request(&addr->bt_bdaddr, bdaddr, sock) == 0)
return send_pin_code_reply(sock, addr, bdaddr, NULL);
return 0;
}
/* Process Link_Key_Request event */
static int
process_link_key_request_event(int sock, struct sockaddr_bt *addr,
bdaddr_t *bdaddr)
{
uint8_t *key;
syslog(LOG_DEBUG,
"Got Link_Key_Request event from %s, remote bdaddr %s",
dev_name, bt_ntoa(bdaddr, NULL));
key = lookup_key(&addr->bt_bdaddr, bdaddr);
if (key != NULL) {
syslog(LOG_DEBUG, "Found Key, remote bdaddr %s",
bt_ntoa(bdaddr, NULL));
return send_link_key_reply(sock, addr, bdaddr, key);
}
syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s",
bt_ntoa(bdaddr, NULL));
return send_link_key_reply(sock, addr, bdaddr, NULL);
}
/* Send PIN_Code_[Negative]_Reply */
int
send_pin_code_reply(int sock, struct sockaddr_bt *addr,
bdaddr_t *bdaddr, uint8_t *pin)
{
int n;
if (pin != NULL) {
hci_pin_code_rep_cp cp;
syslog(LOG_DEBUG, "Sending PIN_Code_Reply to %s "
"for remote bdaddr %s",
dev_name,
bt_ntoa(bdaddr, NULL));
bdaddr_copy(&cp.bdaddr, bdaddr);
memcpy(cp.pin, pin, HCI_PIN_SIZE);
n = HCI_PIN_SIZE;
while (n > 0 && pin[n - 1] == 0)
n--;
cp.pin_size = n;
n = send_hci_cmd(sock, addr,
HCI_CMD_PIN_CODE_REP, sizeof(cp), &cp);
} else {
syslog(LOG_DEBUG, "Sending PIN_Code_Negative_Reply to %s "
"for remote bdaddr %s",
dev_name,
bt_ntoa(bdaddr, NULL));
n = send_hci_cmd(sock, addr, HCI_CMD_PIN_CODE_NEG_REP,
sizeof(bdaddr_t), bdaddr);
}
if (n < 0) {
syslog(LOG_ERR, "Could not send PIN code reply to %s "
"for remote bdaddr %s: %m",
dev_name,
bt_ntoa(bdaddr, NULL));
return -1;
}
return 0;
}
/* Send Link_Key_[Negative]_Reply */
static int
send_link_key_reply(int sock, struct sockaddr_bt *addr,
bdaddr_t *bdaddr, uint8_t *key)
{
int n;
if (key != NULL) {
hci_link_key_rep_cp cp;
bdaddr_copy(&cp.bdaddr, bdaddr);
memcpy(&cp.key, key, sizeof(cp.key));
syslog(LOG_DEBUG, "Sending Link_Key_Reply to %s "
"for remote bdaddr %s",
dev_name, bt_ntoa(bdaddr, NULL));
n = send_hci_cmd(sock, addr, HCI_CMD_LINK_KEY_REP, sizeof(cp), &cp);
} else {
hci_link_key_neg_rep_cp cp;
bdaddr_copy(&cp.bdaddr, bdaddr);
syslog(LOG_DEBUG, "Sending Link_Key_Negative_Reply to %s "
"for remote bdaddr %s",
dev_name, bt_ntoa(bdaddr, NULL));
n = send_hci_cmd(sock, addr, HCI_CMD_LINK_KEY_NEG_REP, sizeof(cp), &cp);
}
if (n < 0) {
syslog(LOG_ERR, "Could not send link key reply to %s "
"for remote bdaddr %s: %m",
dev_name, bt_ntoa(bdaddr, NULL));
return -1;
}
return 0;
}
/* Process Link_Key_Notification event */
static int
process_link_key_notification_event(int sock, struct sockaddr_bt *addr,
hci_link_key_notification_ep *ep)
{
syslog(LOG_DEBUG, "Got Link_Key_Notification event from %s, "
"remote bdaddr %s",
dev_name,
bt_ntoa(&ep->bdaddr, NULL));
save_key(&addr->bt_bdaddr, &ep->bdaddr, ep->key);
return 0;
}
/* Send HCI Command Packet to socket */
static int
send_hci_cmd(int sock, struct sockaddr_bt *sa, uint16_t opcode, size_t len, void *buf)
{
char msg[HCI_CMD_PKT_SIZE];
hci_cmd_hdr_t *h = (hci_cmd_hdr_t *)msg;
h->type = HCI_CMD_PKT;
h->opcode = htole16(opcode);
h->length = len;
if (len > 0)
memcpy(msg + sizeof(hci_cmd_hdr_t), buf, len);
return sendto(sock, msg, sizeof(hci_cmd_hdr_t) + len, MSG_NOSIGNAL,
(struct sockaddr *)sa, sizeof(*sa));
}