1 /* $NetBSD: client.c,v 1.4 2006/09/29 20:06:11 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/bthcid/client.c,v 1.1 2008/01/30 14:10:19 hasso 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 #include <sys/ioctl.h>
34 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/event.h>
40 #include <bluetooth.h>
51 * A client is anybody who connects to our control socket to
52 * receive PIN requests.
55 int fd; /* client descriptor */
56 LIST_ENTRY(client) next;
60 * PIN cache items are made when we have sent a client pin
61 * request. The event is used to expire the item.
64 bdaddr_t laddr; /* local device BDADDR */
65 bdaddr_t raddr; /* remote device BDADDR */
66 uint8_t pin[HCI_PIN_SIZE]; /* PIN */
67 int hci; /* HCI socket */
68 LIST_ENTRY(item) next;
71 static LIST_HEAD(,client) client_list;
72 static LIST_HEAD(,item) item_list;
74 #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
75 #define PIN_TIMEOUT 300 /* PIN is valid */
78 init_control(const char *name, mode_t mode)
80 struct sockaddr_un un;
82 struct timespec timeout = { 0, 0 };
85 LIST_INIT(&client_list);
86 LIST_INIT(&item_list);
91 if (unlink(name) < 0 && errno != ENOENT)
94 ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
98 memset(&un, 0, sizeof(un));
99 un.sun_len = sizeof(un);
100 un.sun_family = AF_LOCAL;
101 strlcpy(un.sun_path, name, sizeof(un.sun_path));
102 if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
107 if (chmod(name, mode) < 0) {
113 if (listen(ctl, 10) < 0) {
119 EV_SET(&change, ctl, EVFILT_READ, EV_ADD, 0, 0, NULL);
120 if (kevent(hci_kq, &change, 1, NULL, 0, &timeout) == -1) {
129 /* Process control socket event */
131 process_control(int sock)
133 struct sockaddr_un un;
135 struct kevent change;
136 struct timespec timeout = { 0, 0 };
141 fd = accept(sock, (struct sockaddr *)&un, &n);
143 syslog(LOG_ERR, "Could not accept PIN client connection");
148 if (ioctl(fd, FIONBIO, &n) < 0) {
149 syslog(LOG_ERR, "Could not set non blocking IO for client");
154 cl = malloc(sizeof(struct client));
156 syslog(LOG_ERR, "Could not malloc client");
161 memset(cl, 0, sizeof(struct client));
164 EV_SET(&change, cl->fd, EVFILT_READ, EV_ADD, 0, 0, cl);
165 if (kevent(hci_kq, &change, 1, NULL, 0, &timeout) == -1) {
166 syslog(LOG_ERR, "Could not add client event");
172 syslog(LOG_DEBUG, "New Client");
173 LIST_INSERT_HEAD(&client_list, cl, next);
176 /* Process client response packet */
178 process_client(int sock, void *arg)
180 bthcid_pin_response_t rp;
181 struct sockaddr_bt sa;
182 struct client *cl = arg;
184 struct kevent change;
185 struct timespec timeout = { 0, 0 };
188 n = recv(sock, &rp, sizeof(rp), 0);
189 if (n != sizeof(rp)) {
191 syslog(LOG_ERR, "Bad Client");
194 LIST_REMOVE(cl, next);
197 syslog(LOG_DEBUG, "Client Closed");
201 syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
203 LIST_FOREACH(item, &item_list, next) {
204 if (bdaddr_same(&rp.laddr, &item->laddr) == 0
205 || bdaddr_same(&rp.raddr, &item->raddr) == 0)
208 EV_SET(&change, sock, EVFILT_TIMER, EV_DELETE, 0, 0, NULL);
209 kevent(hci_kq, &change, 1, NULL, 0, &timeout);
210 if (item->hci != -1) {
211 memset(&sa, 0, sizeof(sa));
212 sa.bt_len = sizeof(sa);
213 sa.bt_family = AF_BLUETOOTH;
214 bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
216 send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
217 LIST_REMOVE(item, next);
224 item = malloc(sizeof(struct item));
226 syslog(LOG_ERR, "Item allocation failed");
230 memset(item, 0, sizeof(struct item));
231 bdaddr_copy(&item->laddr, &rp.laddr);
232 bdaddr_copy(&item->raddr, &rp.raddr);
233 LIST_INSERT_HEAD(&item_list, item, next);
236 syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
238 memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
241 EV_SET(&change, sock, EVFILT_TIMER, EV_ADD, 0, PIN_TIMEOUT * 1000, NULL);
242 if (kevent(hci_kq, &change, 1, NULL, 0, &timeout) == -1) {
243 syslog(LOG_ERR, "Cannot add event timer for item");
244 LIST_REMOVE(item, next);
249 /* Send PIN request to client */
251 send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
253 bthcid_pin_request_t cp;
256 struct kevent change;
257 struct timespec timeout = { 0, 0 };
260 memset(&cp, 0, sizeof(cp));
261 bdaddr_copy(&cp.laddr, laddr);
262 bdaddr_copy(&cp.raddr, raddr);
263 cp.time = PIN_REQUEST_TIMEOUT;
265 LIST_FOREACH(cl, &client_list, next) {
266 if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
267 syslog(LOG_ERR, "send PIN request failed");
275 syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
276 n, (n == 1 ? "" : "s"));
278 item = malloc(sizeof(struct item));
280 syslog(LOG_ERR, "Cannot allocate PIN request item");
284 memset(item, 0, sizeof(struct item));
285 bdaddr_copy(&item->laddr, laddr);
286 bdaddr_copy(&item->raddr, raddr);
288 EV_SET(&change, item->hci, EVFILT_TIMER, EV_ADD, 0, cp.time * 1000, item);
289 if (kevent(hci_kq, &change, 1, NULL, 0, &timeout) == -1) {
290 syslog(LOG_ERR, "Cannot add request timer");
295 LIST_INSERT_HEAD(&item_list, item, next);
299 /* Process item event (by expiring it) */
301 process_item(void *arg)
303 struct item *item = arg;
304 struct kevent change;
305 struct timespec timeout = { 0, 0 };
307 syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
308 LIST_REMOVE(item, next);
309 EV_SET(&change, item->hci, EVFILT_TIMER, EV_DELETE, 0, 0, 0);
310 kevent(hci_kq, &change, 1, NULL, 0, &timeout);
314 /* lookup PIN in item cache */
316 lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
318 static uint8_t pin[HCI_PIN_SIZE];
320 struct kevent change;
321 struct timespec timeout = { 0, 0 };
323 LIST_FOREACH(item, &item_list, next) {
324 if (bdaddr_same(raddr, &item->raddr) == 0)
327 if (bdaddr_same(laddr, &item->laddr) == 0
328 && bdaddr_any(&item->laddr) == 0)
334 syslog(LOG_DEBUG, "Matched PIN from cache");
335 memcpy(pin, item->pin, sizeof(pin));
337 LIST_REMOVE(item, next);
338 EV_SET(&change, item->hci, EVFILT_TIMER, EV_DELETE, 0, 0, 0);
339 kevent(hci_kq, &change, 1, NULL, 0, &timeout);