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 <sys/event.h>
61 #include <sys/types.h>
62 #include <bluetooth.h>
70 static int process_pin_code_request_event
71 (int, struct sockaddr_bt
*, bdaddr_t
*);
72 static int process_link_key_request_event
73 (int, struct sockaddr_bt
*, bdaddr_t
*);
74 static int process_link_key_notification_event
75 (int, struct sockaddr_bt
*, hci_link_key_notification_ep
*);
77 static int send_link_key_reply
78 (int, struct sockaddr_bt
*, bdaddr_t
*, uint8_t *);
79 static int send_hci_cmd
80 (int, struct sockaddr_bt
*, uint16_t, size_t, void *);
82 static char dev_name
[HCI_DEVNAME_SIZE
];
84 /* Initialise HCI Events */
86 init_hci(bdaddr_t
*bdaddr
)
88 struct sockaddr_bt sa
;
89 struct hci_filter filter
;
91 struct timespec timeout
= { 0, 0 };
94 hci
= socket(PF_BLUETOOTH
, SOCK_RAW
, BTPROTO_HCI
);
98 memset(&sa
, 0, sizeof(sa
));
99 sa
.bt_len
= sizeof(sa
);
100 sa
.bt_family
= AF_BLUETOOTH
;
101 bdaddr_copy(&sa
.bt_bdaddr
, bdaddr
);
102 if (bind(hci
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0) {
107 memset(&filter
, 0, sizeof(filter
));
108 hci_filter_set(HCI_EVENT_PIN_CODE_REQ
, &filter
);
109 hci_filter_set(HCI_EVENT_LINK_KEY_REQ
, &filter
);
110 hci_filter_set(HCI_EVENT_LINK_KEY_NOTIFICATION
, &filter
);
112 if (setsockopt(hci
, BTPROTO_HCI
, SO_HCI_EVT_FILTER
,
113 (const void *)&filter
, sizeof(filter
)) < 0) {
118 EV_SET(&change
, hci
, EVFILT_READ
, EV_ADD
, 0, 0, NULL
);
119 if (kevent(hci_kq
, &change
, 1, NULL
, 0, &timeout
) == -1) {
127 /* Process an HCI event */
129 process_hci(int sock
)
131 char buffer
[HCI_EVENT_PKT_SIZE
];
132 hci_event_hdr_t
*event
= (hci_event_hdr_t
*)buffer
;
133 struct sockaddr_bt addr
;
138 n
= recvfrom(sock
, buffer
, sizeof(buffer
), 0,
139 (struct sockaddr
*) &addr
, &size
);
141 syslog(LOG_ERR
, "Could not receive from HCI socket: %m");
145 if (event
->type
!= HCI_EVENT_PKT
) {
146 syslog(LOG_ERR
, "Received unexpected HCI packet, "
147 "type=%#x", event
->type
);
152 if (!bt_devname(dev_name
, &addr
.bt_bdaddr
))
153 strlcpy(dev_name
, "unknown", sizeof(dev_name
));
155 switch (event
->event
) {
156 case HCI_EVENT_PIN_CODE_REQ
:
157 process_pin_code_request_event(sock
, &addr
,
158 (bdaddr_t
*)(event
+ 1));
161 case HCI_EVENT_LINK_KEY_REQ
:
162 process_link_key_request_event(sock
, &addr
,
163 (bdaddr_t
*)(event
+ 1));
166 case HCI_EVENT_LINK_KEY_NOTIFICATION
:
167 process_link_key_notification_event(sock
, &addr
,
168 (hci_link_key_notification_ep
*)(event
+ 1));
172 syslog(LOG_ERR
, "Received unexpected HCI event, "
173 "event=%#x", event
->event
);
180 /* Process PIN_Code_Request event */
182 process_pin_code_request_event(int sock
, struct sockaddr_bt
*addr
,
185 /* TODO: Add search pin in config file */
190 syslog(LOG_DEBUG
, "Got PIN_Code_Request event from %s, "
193 bt_ntoa(bdaddr
, NULL
));
195 pin
= lookup_pin(&addr
->bt_bdaddr
, bdaddr
);
197 return send_pin_code_reply(sock
, addr
, bdaddr
, pin
);
199 pin2
= lookup_pin_conf(&addr
->bt_bdaddr
, bdaddr
);
201 return send_pin_code_reply(sock
, addr
, bdaddr
, pin2
);
204 if (send_client_request(&addr
->bt_bdaddr
, bdaddr
, sock
) == 0)
205 return send_pin_code_reply(sock
, addr
, bdaddr
, NULL
);
210 /* Process Link_Key_Request event */
212 process_link_key_request_event(int sock
, struct sockaddr_bt
*addr
,
218 "Got Link_Key_Request event from %s, remote bdaddr %s",
219 dev_name
, bt_ntoa(bdaddr
, NULL
));
221 key
= lookup_key(&addr
->bt_bdaddr
, bdaddr
);
224 syslog(LOG_DEBUG
, "Found Key, remote bdaddr %s",
225 bt_ntoa(bdaddr
, NULL
));
227 return send_link_key_reply(sock
, addr
, bdaddr
, key
);
230 syslog(LOG_DEBUG
, "Could not find link key for remote bdaddr %s",
231 bt_ntoa(bdaddr
, NULL
));
233 return send_link_key_reply(sock
, addr
, bdaddr
, NULL
);
236 /* Send PIN_Code_[Negative]_Reply */
238 send_pin_code_reply(int sock
, struct sockaddr_bt
*addr
,
239 bdaddr_t
*bdaddr
, uint8_t *pin
)
244 hci_pin_code_rep_cp cp
;
246 syslog(LOG_DEBUG
, "Sending PIN_Code_Reply to %s "
247 "for remote bdaddr %s",
249 bt_ntoa(bdaddr
, NULL
));
251 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
252 memcpy(cp
.pin
, pin
, HCI_PIN_SIZE
);
255 while (n
> 0 && pin
[n
- 1] == 0)
259 n
= send_hci_cmd(sock
, addr
,
260 HCI_CMD_PIN_CODE_REP
, sizeof(cp
), &cp
);
263 syslog(LOG_DEBUG
, "Sending PIN_Code_Negative_Reply to %s "
264 "for remote bdaddr %s",
266 bt_ntoa(bdaddr
, NULL
));
268 n
= send_hci_cmd(sock
, addr
, HCI_CMD_PIN_CODE_NEG_REP
,
269 sizeof(bdaddr_t
), bdaddr
);
273 syslog(LOG_ERR
, "Could not send PIN code reply to %s "
274 "for remote bdaddr %s: %m",
276 bt_ntoa(bdaddr
, NULL
));
284 /* Send Link_Key_[Negative]_Reply */
286 send_link_key_reply(int sock
, struct sockaddr_bt
*addr
,
287 bdaddr_t
*bdaddr
, uint8_t *key
)
292 hci_link_key_rep_cp cp
;
294 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
295 memcpy(&cp
.key
, key
, sizeof(cp
.key
));
297 syslog(LOG_DEBUG
, "Sending Link_Key_Reply to %s "
298 "for remote bdaddr %s",
299 dev_name
, bt_ntoa(bdaddr
, NULL
));
301 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_REP
, sizeof(cp
), &cp
);
303 hci_link_key_neg_rep_cp cp
;
305 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
307 syslog(LOG_DEBUG
, "Sending Link_Key_Negative_Reply to %s "
308 "for remote bdaddr %s",
309 dev_name
, bt_ntoa(bdaddr
, NULL
));
311 n
= send_hci_cmd(sock
, addr
, HCI_CMD_LINK_KEY_NEG_REP
, sizeof(cp
), &cp
);
315 syslog(LOG_ERR
, "Could not send link key reply to %s "
316 "for remote bdaddr %s: %m",
317 dev_name
, bt_ntoa(bdaddr
, NULL
));
324 /* Process Link_Key_Notification event */
326 process_link_key_notification_event(int sock __unused
, struct sockaddr_bt
*addr
,
327 hci_link_key_notification_ep
*ep
)
330 syslog(LOG_DEBUG
, "Got Link_Key_Notification event from %s, "
333 bt_ntoa(&ep
->bdaddr
, NULL
));
335 save_key(&addr
->bt_bdaddr
, &ep
->bdaddr
, ep
->key
);
339 /* Send HCI Command Packet to socket */
341 send_hci_cmd(int sock
, struct sockaddr_bt
*sa
, uint16_t opcode
, size_t len
, void *buf
)
343 char msg
[HCI_CMD_PKT_SIZE
];
344 hci_cmd_hdr_t
*h
= (hci_cmd_hdr_t
*)msg
;
346 h
->type
= HCI_CMD_PKT
;
347 h
->opcode
= htole16(opcode
);
351 memcpy(msg
+ sizeof(hci_cmd_hdr_t
), buf
, len
);
353 return sendto(sock
, msg
, sizeof(hci_cmd_hdr_t
) + len
, 0,
354 (struct sockaddr
*)sa
, sizeof(*sa
));