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