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