kernel - Fix some rare pmap races in i386 and x86_64 (followup)
[dragonfly.git] / usr.sbin / bthcid / client.c
blob3abbe0548e1963ff94da9afdfc67668b8e5f3db7
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 $ */
4 /*-
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/un.h>
38 #include <bluetooth.h>
39 #include <errno.h>
40 #include <event.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
47 #include "bthcid.h"
50 * A client is anybody who connects to our control socket to
51 * receive PIN requests.
53 struct client {
54 struct event ev;
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.
63 struct item {
64 struct event ev;
65 bdaddr_t laddr; /* local device BDADDR */
66 bdaddr_t raddr; /* remote device BDADDR */
67 uint8_t pin[HCI_PIN_SIZE]; /* PIN */
68 int hci; /* HCI socket */
69 LIST_ENTRY(item) next;
72 static struct event control_ev;
74 static LIST_HEAD(,client) client_list;
75 static LIST_HEAD(,item) item_list;
77 static void process_control (int, short, void *);
78 static void process_client (int, short, void *);
79 static void process_item (int, short, void *);
81 #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
82 #define PIN_TIMEOUT 300 /* PIN is valid */
84 int
85 init_control(const char *name, mode_t mode)
87 struct sockaddr_un un;
88 int ctl;
90 LIST_INIT(&client_list);
91 LIST_INIT(&item_list);
93 if (name == NULL)
94 return 0;
96 if (unlink(name) < 0 && errno != ENOENT)
97 return -1;
99 ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
100 if (ctl < 0)
101 return -1;
103 memset(&un, 0, sizeof(un));
104 un.sun_len = sizeof(un);
105 un.sun_family = AF_LOCAL;
106 strlcpy(un.sun_path, name, sizeof(un.sun_path));
107 if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
108 close(ctl);
109 return -1;
112 if (chmod(name, mode) < 0) {
113 close(ctl);
114 unlink(name);
115 return -1;
118 if (listen(ctl, 10) < 0) {
119 close(ctl);
120 unlink(name);
121 return -1;
124 event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
125 if (event_add(&control_ev, NULL) < 0) {
126 close(ctl);
127 unlink(name);
128 return -1;
131 return 0;
134 /* Process control socket event */
135 static void
136 process_control(int sock, short ev __unused, void *arg __unused)
138 struct sockaddr_un un;
139 socklen_t n;
140 int fd;
141 struct client *cl;
143 n = sizeof(un);
144 fd = accept(sock, (struct sockaddr *)&un, &n);
145 if (fd < 0) {
146 syslog(LOG_ERR, "Could not accept PIN client connection");
147 return;
150 n = 1;
151 if (ioctl(fd, FIONBIO, &n) < 0) {
152 syslog(LOG_ERR, "Could not set non blocking IO for client");
153 close(fd);
154 return;
157 cl = malloc(sizeof(struct client));
158 if (cl == NULL) {
159 syslog(LOG_ERR, "Could not malloc client");
160 close(fd);
161 return;
164 memset(cl, 0, sizeof(struct client));
165 cl->fd = fd;
167 event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
168 if (event_add(&cl->ev, NULL) < 0) {
169 syslog(LOG_ERR, "Could not add client event");
170 free(cl);
171 close(fd);
172 return;
175 syslog(LOG_DEBUG, "New Client");
176 LIST_INSERT_HEAD(&client_list, cl, next);
179 /* Process client response packet */
180 static void
181 process_client(int sock, short ev __unused, void *arg)
183 bthcid_pin_response_t rp;
184 struct timeval tv;
185 struct sockaddr_bt sa;
186 struct client *cl = arg;
187 struct item *item;
188 int n;
190 n = recv(sock, &rp, sizeof(rp), 0);
191 if (n != sizeof(rp)) {
192 if (n != 0)
193 syslog(LOG_ERR, "Bad Client");
195 close(sock);
196 LIST_REMOVE(cl, next);
197 free(cl);
199 syslog(LOG_DEBUG, "Client Closed");
200 return;
203 syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
205 LIST_FOREACH(item, &item_list, next) {
206 if (bdaddr_same(&rp.laddr, &item->laddr) == 0
207 || bdaddr_same(&rp.raddr, &item->raddr) == 0)
208 continue;
210 evtimer_del(&item->ev);
211 if (item->hci != -1) {
212 memset(&sa, 0, sizeof(sa));
213 sa.bt_len = sizeof(sa);
214 sa.bt_family = AF_BLUETOOTH;
215 bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
217 send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
218 LIST_REMOVE(item, next);
219 free(item);
220 return;
222 goto newpin;
225 item = malloc(sizeof(struct item));
226 if (item == NULL) {
227 syslog(LOG_ERR, "Item allocation failed");
228 return;
231 memset(item, 0, sizeof(struct item));
232 bdaddr_copy(&item->laddr, &rp.laddr);
233 bdaddr_copy(&item->raddr, &rp.raddr);
234 evtimer_set(&item->ev, process_item, item);
235 LIST_INSERT_HEAD(&item_list, item, next);
237 newpin:
238 syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
240 memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
241 item->hci = -1;
243 tv.tv_sec = PIN_TIMEOUT;
244 tv.tv_usec = 0;
246 if (evtimer_add(&item->ev, &tv) < 0) {
247 syslog(LOG_ERR, "Cannot add event timer for item");
248 LIST_REMOVE(item, next);
249 free(item);
253 /* Send PIN request to client */
255 send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
257 bthcid_pin_request_t cp;
258 struct client *cl;
259 struct item *item;
260 int n = 0;
261 struct timeval tv;
263 memset(&cp, 0, sizeof(cp));
264 bdaddr_copy(&cp.laddr, laddr);
265 bdaddr_copy(&cp.raddr, raddr);
266 cp.time = PIN_REQUEST_TIMEOUT;
268 LIST_FOREACH(cl, &client_list, next) {
269 if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
270 syslog(LOG_ERR, "send PIN request failed");
271 else
272 n++;
275 if (n == 0)
276 return 0;
278 syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
279 n, (n == 1 ? "" : "s"));
281 item = malloc(sizeof(struct item));
282 if (item == NULL) {
283 syslog(LOG_ERR, "Cannot allocate PIN request item");
284 return 0;
287 memset(item, 0, sizeof(struct item));
288 bdaddr_copy(&item->laddr, laddr);
289 bdaddr_copy(&item->raddr, raddr);
290 item->hci = hci;
291 evtimer_set(&item->ev, process_item, item);
293 tv.tv_sec = cp.time;
294 tv.tv_usec = 0;
296 if (evtimer_add(&item->ev, &tv) < 0) {
297 syslog(LOG_ERR, "Cannot add request timer");
298 free(item);
299 return 0;
302 LIST_INSERT_HEAD(&item_list, item, next);
303 return 1;
306 /* Process item event (by expiring it) */
307 static void
308 process_item(int fd __unused, short ev __unused, void *arg)
310 struct item *item = arg;
312 syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
313 LIST_REMOVE(item, next);
314 evtimer_del(&item->ev);
315 free(item);
318 /* lookup PIN in item cache */
319 uint8_t *
320 lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
322 static uint8_t pin[HCI_PIN_SIZE];
323 struct item *item;
325 LIST_FOREACH(item, &item_list, next) {
326 if (bdaddr_same(raddr, &item->raddr) == 0)
327 continue;
329 if (bdaddr_same(laddr, &item->laddr) == 0
330 && bdaddr_any(&item->laddr) == 0)
331 continue;
333 if (item->hci >= 0)
334 break;
336 syslog(LOG_DEBUG, "Matched PIN from cache");
337 memcpy(pin, item->pin, sizeof(pin));
339 LIST_REMOVE(item, next);
340 evtimer_del(&item->ev);
341 free(item);
343 return pin;
346 return NULL;