1*829fbcb5Schristos /* $NetBSD: client.c,v 1.5 2017/01/10 20:56:41 christos Exp $ */
2a5c89047Sgdamore
3a5c89047Sgdamore /*-
4a5c89047Sgdamore * Copyright (c) 2006 Itronix Inc.
5a5c89047Sgdamore * All rights reserved.
6a5c89047Sgdamore *
7a5c89047Sgdamore * Redistribution and use in source and binary forms, with or without
8a5c89047Sgdamore * modification, are permitted provided that the following conditions
9a5c89047Sgdamore * are met:
10a5c89047Sgdamore * 1. Redistributions of source code must retain the above copyright
11a5c89047Sgdamore * notice, this list of conditions and the following disclaimer.
12a5c89047Sgdamore * 2. Redistributions in binary form must reproduce the above copyright
13a5c89047Sgdamore * notice, this list of conditions and the following disclaimer in the
14a5c89047Sgdamore * documentation and/or other materials provided with the distribution.
15a5c89047Sgdamore * 3. The name of Itronix Inc. may not be used to endorse
16a5c89047Sgdamore * or promote products derived from this software without specific
17a5c89047Sgdamore * prior written permission.
18a5c89047Sgdamore *
19a5c89047Sgdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20a5c89047Sgdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a5c89047Sgdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a5c89047Sgdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23a5c89047Sgdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24a5c89047Sgdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25a5c89047Sgdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26a5c89047Sgdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
27a5c89047Sgdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a5c89047Sgdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a5c89047Sgdamore * POSSIBILITY OF SUCH DAMAGE.
30a5c89047Sgdamore */
31a5c89047Sgdamore
32a5c89047Sgdamore #include <sys/cdefs.h>
33*829fbcb5Schristos __RCSID("$NetBSD: client.c,v 1.5 2017/01/10 20:56:41 christos Exp $");
34a5c89047Sgdamore
35a5c89047Sgdamore #include <sys/ioctl.h>
36a5c89047Sgdamore #include <sys/queue.h>
37a5c89047Sgdamore #include <sys/time.h>
38*829fbcb5Schristos #include <sys/stat.h>
39a5c89047Sgdamore #include <sys/un.h>
40a5c89047Sgdamore #include <bluetooth.h>
41a5c89047Sgdamore #include <errno.h>
42a5c89047Sgdamore #include <event.h>
43a5c89047Sgdamore #include <fcntl.h>
44a5c89047Sgdamore #include <stdlib.h>
45a5c89047Sgdamore #include <string.h>
46a5c89047Sgdamore #include <syslog.h>
47a5c89047Sgdamore #include <unistd.h>
48a5c89047Sgdamore
49a5c89047Sgdamore #include "bthcid.h"
50a5c89047Sgdamore
51a5c89047Sgdamore /*
52a5c89047Sgdamore * A client is anybody who connects to our control socket to
53a5c89047Sgdamore * receive PIN requests.
54a5c89047Sgdamore */
55a5c89047Sgdamore struct client {
56a5c89047Sgdamore struct event ev;
57a5c89047Sgdamore int fd; /* client descriptor */
58a5c89047Sgdamore LIST_ENTRY(client) next;
59a5c89047Sgdamore };
60a5c89047Sgdamore
61a5c89047Sgdamore /*
62a5c89047Sgdamore * PIN cache items are made when we have sent a client pin
63a5c89047Sgdamore * request. The event is used to expire the item.
64a5c89047Sgdamore */
65a5c89047Sgdamore struct item {
66a5c89047Sgdamore struct event ev;
67a5c89047Sgdamore bdaddr_t laddr; /* local device BDADDR */
68a5c89047Sgdamore bdaddr_t raddr; /* remote device BDADDR */
69a5c89047Sgdamore uint8_t pin[HCI_PIN_SIZE]; /* PIN */
70a5c89047Sgdamore int hci; /* HCI socket */
71a5c89047Sgdamore LIST_ENTRY(item) next;
72a5c89047Sgdamore };
73a5c89047Sgdamore
74a5c89047Sgdamore static struct event control_ev;
75a5c89047Sgdamore
76a5c89047Sgdamore static LIST_HEAD(,client) client_list;
77a5c89047Sgdamore static LIST_HEAD(,item) item_list;
78a5c89047Sgdamore
79a5c89047Sgdamore static void process_control (int, short, void *);
80a5c89047Sgdamore static void process_client (int, short, void *);
81a5c89047Sgdamore static void process_item (int, short, void *);
82a5c89047Sgdamore
83a5c89047Sgdamore #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
84a5c89047Sgdamore #define PIN_TIMEOUT 300 /* PIN is valid */
85a5c89047Sgdamore
86a5c89047Sgdamore int
init_control(const char * name,mode_t mode)87a5c89047Sgdamore init_control(const char *name, mode_t mode)
88a5c89047Sgdamore {
89a5c89047Sgdamore struct sockaddr_un un;
90a5c89047Sgdamore int ctl;
91a5c89047Sgdamore
92a5c89047Sgdamore LIST_INIT(&client_list);
93a5c89047Sgdamore LIST_INIT(&item_list);
94a5c89047Sgdamore
95a5c89047Sgdamore if (name == NULL)
96a5c89047Sgdamore return 0;
97a5c89047Sgdamore
98a5c89047Sgdamore if (unlink(name) < 0 && errno != ENOENT)
99a5c89047Sgdamore return -1;
100a5c89047Sgdamore
101a5c89047Sgdamore ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
102a5c89047Sgdamore if (ctl < 0)
103a5c89047Sgdamore return -1;
104a5c89047Sgdamore
105a5c89047Sgdamore memset(&un, 0, sizeof(un));
106a5c89047Sgdamore un.sun_len = sizeof(un);
107a5c89047Sgdamore un.sun_family = AF_LOCAL;
108a5c89047Sgdamore strlcpy(un.sun_path, name, sizeof(un.sun_path));
109a5c89047Sgdamore if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
110a5c89047Sgdamore close(ctl);
111a5c89047Sgdamore return -1;
112a5c89047Sgdamore }
113a5c89047Sgdamore
114a5c89047Sgdamore if (chmod(name, mode) < 0) {
115a5c89047Sgdamore close(ctl);
116a5c89047Sgdamore unlink(name);
117a5c89047Sgdamore return -1;
118a5c89047Sgdamore }
119a5c89047Sgdamore
120a5c89047Sgdamore if (listen(ctl, 10) < 0) {
121a5c89047Sgdamore close(ctl);
122a5c89047Sgdamore unlink(name);
123a5c89047Sgdamore return -1;
124a5c89047Sgdamore }
125a5c89047Sgdamore
126a5c89047Sgdamore event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
127a5c89047Sgdamore if (event_add(&control_ev, NULL) < 0) {
128a5c89047Sgdamore close(ctl);
129a5c89047Sgdamore unlink(name);
130a5c89047Sgdamore return -1;
131a5c89047Sgdamore }
132a5c89047Sgdamore
133a5c89047Sgdamore return 0;
134a5c89047Sgdamore }
135a5c89047Sgdamore
136a5c89047Sgdamore /* Process control socket event */
137a5c89047Sgdamore static void
process_control(int sock,short ev,void * arg)138a5c89047Sgdamore process_control(int sock, short ev, void *arg)
139a5c89047Sgdamore {
140a5c89047Sgdamore struct sockaddr_un un;
141a5c89047Sgdamore socklen_t n;
142a5c89047Sgdamore int fd;
143a5c89047Sgdamore struct client *cl;
144a5c89047Sgdamore
145a5c89047Sgdamore n = sizeof(un);
146a5c89047Sgdamore fd = accept(sock, (struct sockaddr *)&un, &n);
147a5c89047Sgdamore if (fd < 0) {
148a5c89047Sgdamore syslog(LOG_ERR, "Could not accept PIN client connection");
149a5c89047Sgdamore return;
150a5c89047Sgdamore }
151a5c89047Sgdamore
152a5c89047Sgdamore n = 1;
153a5c89047Sgdamore if (ioctl(fd, FIONBIO, &n) < 0) {
154a5c89047Sgdamore syslog(LOG_ERR, "Could not set non blocking IO for client");
155a5c89047Sgdamore close(fd);
156a5c89047Sgdamore return;
157a5c89047Sgdamore }
158a5c89047Sgdamore
159a5c89047Sgdamore cl = malloc(sizeof(struct client));
160a5c89047Sgdamore if (cl == NULL) {
161a5c89047Sgdamore syslog(LOG_ERR, "Could not malloc client");
162a5c89047Sgdamore close(fd);
163a5c89047Sgdamore return;
164a5c89047Sgdamore }
165a5c89047Sgdamore
166a5c89047Sgdamore memset(cl, 0, sizeof(struct client));
167a5c89047Sgdamore cl->fd = fd;
168a5c89047Sgdamore
169a5c89047Sgdamore event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
170a5c89047Sgdamore if (event_add(&cl->ev, NULL) < 0) {
171a5c89047Sgdamore syslog(LOG_ERR, "Could not add client event");
172a5c89047Sgdamore free(cl);
173a5c89047Sgdamore close(fd);
174a5c89047Sgdamore return;
175a5c89047Sgdamore }
176a5c89047Sgdamore
177a5c89047Sgdamore syslog(LOG_DEBUG, "New Client");
178a5c89047Sgdamore LIST_INSERT_HEAD(&client_list, cl, next);
179a5c89047Sgdamore }
180a5c89047Sgdamore
181a5c89047Sgdamore /* Process client response packet */
182a5c89047Sgdamore static void
process_client(int sock,short ev,void * arg)183a5c89047Sgdamore process_client(int sock, short ev, void *arg)
184a5c89047Sgdamore {
1850b73a6ecSplunky bthcid_pin_response_t rp;
186a5c89047Sgdamore struct timeval tv;
187a5c89047Sgdamore struct sockaddr_bt sa;
188a5c89047Sgdamore struct client *cl = arg;
189a5c89047Sgdamore struct item *item;
190a5c89047Sgdamore int n;
191a5c89047Sgdamore
192a5c89047Sgdamore n = recv(sock, &rp, sizeof(rp), 0);
193a5c89047Sgdamore if (n != sizeof(rp)) {
194a5c89047Sgdamore if (n != 0)
195a5c89047Sgdamore syslog(LOG_ERR, "Bad Client");
196a5c89047Sgdamore
197a5c89047Sgdamore close(sock);
198a5c89047Sgdamore LIST_REMOVE(cl, next);
199a5c89047Sgdamore free(cl);
200a5c89047Sgdamore
201a5c89047Sgdamore syslog(LOG_DEBUG, "Client Closed");
202a5c89047Sgdamore return;
203a5c89047Sgdamore }
204a5c89047Sgdamore
205a9ed4719Splunky syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
206a5c89047Sgdamore
207a5c89047Sgdamore LIST_FOREACH(item, &item_list, next) {
208a5c89047Sgdamore if (bdaddr_same(&rp.laddr, &item->laddr) == 0
209a5c89047Sgdamore || bdaddr_same(&rp.raddr, &item->raddr) == 0)
210a5c89047Sgdamore continue;
211a5c89047Sgdamore
212a5c89047Sgdamore evtimer_del(&item->ev);
213a5c89047Sgdamore if (item->hci != -1) {
214a5c89047Sgdamore memset(&sa, 0, sizeof(sa));
215a5c89047Sgdamore sa.bt_len = sizeof(sa);
216a5c89047Sgdamore sa.bt_family = AF_BLUETOOTH;
217a5c89047Sgdamore bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
218a5c89047Sgdamore
219a5c89047Sgdamore send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
220a9ed4719Splunky LIST_REMOVE(item, next);
221a9ed4719Splunky free(item);
222a9ed4719Splunky return;
223a5c89047Sgdamore }
224a5c89047Sgdamore goto newpin;
225a5c89047Sgdamore }
226a5c89047Sgdamore
227a5c89047Sgdamore item = malloc(sizeof(struct item));
228a5c89047Sgdamore if (item == NULL) {
229a5c89047Sgdamore syslog(LOG_ERR, "Item allocation failed");
230a5c89047Sgdamore return;
231a5c89047Sgdamore }
232a5c89047Sgdamore
233a5c89047Sgdamore memset(item, 0, sizeof(struct item));
234a5c89047Sgdamore bdaddr_copy(&item->laddr, &rp.laddr);
235a5c89047Sgdamore bdaddr_copy(&item->raddr, &rp.raddr);
236a5c89047Sgdamore evtimer_set(&item->ev, process_item, item);
237a5c89047Sgdamore LIST_INSERT_HEAD(&item_list, item, next);
238a5c89047Sgdamore
239a5c89047Sgdamore newpin:
240a9ed4719Splunky syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
241a9ed4719Splunky
242a5c89047Sgdamore memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
243a5c89047Sgdamore item->hci = -1;
244a5c89047Sgdamore
245a5c89047Sgdamore tv.tv_sec = PIN_TIMEOUT;
246a5c89047Sgdamore tv.tv_usec = 0;
247a5c89047Sgdamore
248a5c89047Sgdamore if (evtimer_add(&item->ev, &tv) < 0) {
249a5c89047Sgdamore syslog(LOG_ERR, "Cannot add event timer for item");
250a5c89047Sgdamore LIST_REMOVE(item, next);
251a5c89047Sgdamore free(item);
252a5c89047Sgdamore }
253a5c89047Sgdamore }
254a5c89047Sgdamore
255a5c89047Sgdamore /* Send PIN request to client */
256a5c89047Sgdamore int
send_client_request(bdaddr_t * laddr,bdaddr_t * raddr,int hci)257a5c89047Sgdamore send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
258a5c89047Sgdamore {
2590b73a6ecSplunky bthcid_pin_request_t cp;
260a5c89047Sgdamore struct client *cl;
261a5c89047Sgdamore struct item *item;
262a5c89047Sgdamore int n = 0;
263a5c89047Sgdamore struct timeval tv;
264a5c89047Sgdamore
265a5c89047Sgdamore memset(&cp, 0, sizeof(cp));
266a5c89047Sgdamore bdaddr_copy(&cp.laddr, laddr);
267a5c89047Sgdamore bdaddr_copy(&cp.raddr, raddr);
268a5c89047Sgdamore cp.time = PIN_REQUEST_TIMEOUT;
269a5c89047Sgdamore
270a5c89047Sgdamore LIST_FOREACH(cl, &client_list, next) {
271a5c89047Sgdamore if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
272a5c89047Sgdamore syslog(LOG_ERR, "send PIN request failed");
273a5c89047Sgdamore else
274a5c89047Sgdamore n++;
275a5c89047Sgdamore }
276a5c89047Sgdamore
277a5c89047Sgdamore if (n == 0)
278a5c89047Sgdamore return 0;
279a5c89047Sgdamore
280a5c89047Sgdamore syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
281a5c89047Sgdamore n, (n == 1 ? "" : "s"));
282a5c89047Sgdamore
283a5c89047Sgdamore item = malloc(sizeof(struct item));
284a5c89047Sgdamore if (item == NULL) {
285a5c89047Sgdamore syslog(LOG_ERR, "Cannot allocate PIN request item");
286a5c89047Sgdamore return 0;
287a5c89047Sgdamore }
288a5c89047Sgdamore
289a5c89047Sgdamore memset(item, 0, sizeof(struct item));
290a5c89047Sgdamore bdaddr_copy(&item->laddr, laddr);
291a5c89047Sgdamore bdaddr_copy(&item->raddr, raddr);
292a5c89047Sgdamore item->hci = hci;
293a5c89047Sgdamore evtimer_set(&item->ev, process_item, item);
294a5c89047Sgdamore
295a5c89047Sgdamore tv.tv_sec = cp.time;
296a5c89047Sgdamore tv.tv_usec = 0;
297a5c89047Sgdamore
298a5c89047Sgdamore if (evtimer_add(&item->ev, &tv) < 0) {
299a5c89047Sgdamore syslog(LOG_ERR, "Cannot add request timer");
300a5c89047Sgdamore free(item);
301a5c89047Sgdamore return 0;
302a5c89047Sgdamore }
303a5c89047Sgdamore
304a5c89047Sgdamore LIST_INSERT_HEAD(&item_list, item, next);
305a5c89047Sgdamore return 1;
306a5c89047Sgdamore }
307a5c89047Sgdamore
308a5c89047Sgdamore /* Process item event (by expiring it) */
309a5c89047Sgdamore static void
process_item(int fd,short ev,void * arg)310a5c89047Sgdamore process_item(int fd, short ev, void *arg)
311a5c89047Sgdamore {
312a5c89047Sgdamore struct item *item = arg;
313a5c89047Sgdamore
314a5c89047Sgdamore syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
315a5c89047Sgdamore LIST_REMOVE(item, next);
316a9ed4719Splunky evtimer_del(&item->ev);
317a5c89047Sgdamore free(item);
318a5c89047Sgdamore }
319a5c89047Sgdamore
320a5c89047Sgdamore /* lookup PIN in item cache */
321a5c89047Sgdamore uint8_t *
lookup_pin(bdaddr_t * laddr,bdaddr_t * raddr)3227b1d74d7Stron lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
323a5c89047Sgdamore {
324a9ed4719Splunky static uint8_t pin[HCI_PIN_SIZE];
325a5c89047Sgdamore struct item *item;
326a5c89047Sgdamore
327a5c89047Sgdamore LIST_FOREACH(item, &item_list, next) {
328a5c89047Sgdamore if (bdaddr_same(raddr, &item->raddr) == 0)
329a5c89047Sgdamore continue;
330a5c89047Sgdamore
331a5c89047Sgdamore if (bdaddr_same(laddr, &item->laddr) == 0
332a5c89047Sgdamore && bdaddr_any(&item->laddr) == 0)
333a5c89047Sgdamore continue;
334a5c89047Sgdamore
335a5c89047Sgdamore if (item->hci >= 0)
336a5c89047Sgdamore break;
337a5c89047Sgdamore
338a5c89047Sgdamore syslog(LOG_DEBUG, "Matched PIN from cache");
339a9ed4719Splunky memcpy(pin, item->pin, sizeof(pin));
340a9ed4719Splunky
341a9ed4719Splunky LIST_REMOVE(item, next);
342a9ed4719Splunky evtimer_del(&item->ev);
343a9ed4719Splunky free(item);
344a9ed4719Splunky
345a9ed4719Splunky return pin;
346a5c89047Sgdamore }
347a5c89047Sgdamore
348a5c89047Sgdamore return NULL;
349a5c89047Sgdamore }
350