Home | History | Annotate | Line # | Download | only in bthcid
      1  1.6   mlelstv /*	$NetBSD: client.c,v 1.6 2024/09/07 13:57:25 mlelstv 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.6   mlelstv __RCSID("$NetBSD: client.c,v 1.6 2024/09/07 13:57:25 mlelstv 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.5  christos #include <sys/stat.h>
     39  1.1   gdamore #include <sys/un.h>
     40  1.1   gdamore #include <bluetooth.h>
     41  1.1   gdamore #include <errno.h>
     42  1.1   gdamore #include <event.h>
     43  1.1   gdamore #include <fcntl.h>
     44  1.1   gdamore #include <stdlib.h>
     45  1.1   gdamore #include <string.h>
     46  1.1   gdamore #include <syslog.h>
     47  1.1   gdamore #include <unistd.h>
     48  1.1   gdamore 
     49  1.1   gdamore #include "bthcid.h"
     50  1.1   gdamore 
     51  1.1   gdamore /*
     52  1.1   gdamore  * A client is anybody who connects to our control socket to
     53  1.1   gdamore  * receive PIN requests.
     54  1.1   gdamore  */
     55  1.1   gdamore struct client {
     56  1.1   gdamore 	struct event		ev;
     57  1.1   gdamore 	int			fd;		/* client descriptor */
     58  1.1   gdamore 	LIST_ENTRY(client)	next;
     59  1.1   gdamore };
     60  1.1   gdamore 
     61  1.1   gdamore /*
     62  1.1   gdamore  * PIN cache items are made when we have sent a client pin
     63  1.1   gdamore  * request. The event is used to expire the item.
     64  1.1   gdamore  */
     65  1.1   gdamore struct item {
     66  1.1   gdamore 	struct event	 ev;
     67  1.1   gdamore 	bdaddr_t	 laddr;			/* local device BDADDR */
     68  1.1   gdamore 	bdaddr_t	 raddr;			/* remote device BDADDR */
     69  1.1   gdamore 	uint8_t		 pin[HCI_PIN_SIZE];	/* PIN */
     70  1.1   gdamore 	int		 hci;			/* HCI socket */
     71  1.1   gdamore 	LIST_ENTRY(item) next;
     72  1.1   gdamore };
     73  1.1   gdamore 
     74  1.1   gdamore static struct event		control_ev;
     75  1.1   gdamore 
     76  1.1   gdamore static LIST_HEAD(,client)	client_list;
     77  1.1   gdamore static LIST_HEAD(,item)		item_list;
     78  1.1   gdamore 
     79  1.1   gdamore static void process_control	(int, short, void *);
     80  1.1   gdamore static void process_client	(int, short, void *);
     81  1.1   gdamore static void process_item	(int, short, void *);
     82  1.1   gdamore 
     83  1.1   gdamore #define PIN_REQUEST_TIMEOUT	30	/* Request is valid */
     84  1.1   gdamore #define PIN_TIMEOUT		300	/* PIN is valid */
     85  1.1   gdamore 
     86  1.1   gdamore int
     87  1.1   gdamore init_control(const char *name, mode_t mode)
     88  1.1   gdamore {
     89  1.1   gdamore 	struct sockaddr_un	un;
     90  1.1   gdamore 	int			ctl;
     91  1.1   gdamore 
     92  1.1   gdamore 	LIST_INIT(&client_list);
     93  1.1   gdamore 	LIST_INIT(&item_list);
     94  1.1   gdamore 
     95  1.1   gdamore 	if (name == NULL)
     96  1.1   gdamore 		return 0;
     97  1.1   gdamore 
     98  1.1   gdamore 	if (unlink(name) < 0 && errno != ENOENT)
     99  1.1   gdamore 		return -1;
    100  1.1   gdamore 
    101  1.1   gdamore 	ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
    102  1.1   gdamore 	if (ctl < 0)
    103  1.1   gdamore 		return -1;
    104  1.1   gdamore 
    105  1.1   gdamore 	memset(&un, 0, sizeof(un));
    106  1.1   gdamore 	un.sun_len = sizeof(un);
    107  1.1   gdamore 	un.sun_family = AF_LOCAL;
    108  1.1   gdamore 	strlcpy(un.sun_path, name, sizeof(un.sun_path));
    109  1.1   gdamore 	if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
    110  1.1   gdamore 		close(ctl);
    111  1.1   gdamore 		return -1;
    112  1.1   gdamore 	}
    113  1.1   gdamore 
    114  1.1   gdamore 	if (chmod(name, mode) < 0) {
    115  1.1   gdamore 		close(ctl);
    116  1.1   gdamore 		unlink(name);
    117  1.1   gdamore 		return -1;
    118  1.1   gdamore 	}
    119  1.1   gdamore 
    120  1.1   gdamore 	if (listen(ctl, 10) < 0) {
    121  1.1   gdamore 		close(ctl);
    122  1.1   gdamore 		unlink(name);
    123  1.1   gdamore 		return -1;
    124  1.1   gdamore 	}
    125  1.1   gdamore 
    126  1.1   gdamore 	event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
    127  1.1   gdamore 	if (event_add(&control_ev, NULL) < 0) {
    128  1.1   gdamore 		close(ctl);
    129  1.1   gdamore 		unlink(name);
    130  1.1   gdamore 		return -1;
    131  1.1   gdamore 	}
    132  1.1   gdamore 
    133  1.1   gdamore 	return 0;
    134  1.1   gdamore }
    135  1.1   gdamore 
    136  1.1   gdamore /* Process control socket event */
    137  1.1   gdamore static void
    138  1.1   gdamore process_control(int sock, short ev, void *arg)
    139  1.1   gdamore {
    140  1.1   gdamore 	struct sockaddr_un	un;
    141  1.1   gdamore 	socklen_t		n;
    142  1.1   gdamore 	int			fd;
    143  1.1   gdamore 	struct client		*cl;
    144  1.1   gdamore 
    145  1.1   gdamore 	n = sizeof(un);
    146  1.1   gdamore 	fd = accept(sock, (struct sockaddr *)&un, &n);
    147  1.1   gdamore 	if (fd < 0) {
    148  1.1   gdamore 		syslog(LOG_ERR, "Could not accept PIN client connection");
    149  1.1   gdamore 		return;
    150  1.1   gdamore 	}
    151  1.1   gdamore 
    152  1.1   gdamore 	n = 1;
    153  1.1   gdamore 	if (ioctl(fd, FIONBIO, &n) < 0) {
    154  1.1   gdamore 		syslog(LOG_ERR, "Could not set non blocking IO for client");
    155  1.1   gdamore 		close(fd);
    156  1.1   gdamore 		return;
    157  1.1   gdamore 	}
    158  1.1   gdamore 
    159  1.1   gdamore 	cl = malloc(sizeof(struct client));
    160  1.1   gdamore 	if (cl == NULL) {
    161  1.1   gdamore 		syslog(LOG_ERR, "Could not malloc client");
    162  1.1   gdamore 		close(fd);
    163  1.1   gdamore 		return;
    164  1.1   gdamore 	}
    165  1.1   gdamore 
    166  1.1   gdamore 	memset(cl, 0, sizeof(struct client));
    167  1.1   gdamore 	cl->fd = fd;
    168  1.1   gdamore 
    169  1.1   gdamore 	event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
    170  1.1   gdamore 	if (event_add(&cl->ev, NULL) < 0) {
    171  1.1   gdamore 		syslog(LOG_ERR, "Could not add client event");
    172  1.1   gdamore 		free(cl);
    173  1.1   gdamore 		close(fd);
    174  1.1   gdamore 		return;
    175  1.1   gdamore 	}
    176  1.1   gdamore 
    177  1.1   gdamore 	syslog(LOG_DEBUG, "New Client");
    178  1.1   gdamore 	LIST_INSERT_HEAD(&client_list, cl, next);
    179  1.1   gdamore }
    180  1.1   gdamore 
    181  1.1   gdamore /* Process client response packet */
    182  1.1   gdamore static void
    183  1.1   gdamore process_client(int sock, short ev, void *arg)
    184  1.1   gdamore {
    185  1.3    plunky 	bthcid_pin_response_t	 rp;
    186  1.1   gdamore 	struct timeval		 tv;
    187  1.1   gdamore 	struct sockaddr_bt	 sa;
    188  1.1   gdamore 	struct client		*cl = arg;
    189  1.1   gdamore 	struct item		*item;
    190  1.1   gdamore 	int			 n;
    191  1.1   gdamore 
    192  1.1   gdamore 	n = recv(sock, &rp, sizeof(rp), 0);
    193  1.1   gdamore 	if (n != sizeof(rp)) {
    194  1.1   gdamore 		if (n != 0)
    195  1.1   gdamore 			syslog(LOG_ERR, "Bad Client");
    196  1.1   gdamore 
    197  1.1   gdamore 		close(sock);
    198  1.1   gdamore 		LIST_REMOVE(cl, next);
    199  1.1   gdamore 		free(cl);
    200  1.1   gdamore 
    201  1.1   gdamore 		syslog(LOG_DEBUG, "Client Closed");
    202  1.1   gdamore 		return;
    203  1.1   gdamore 	}
    204  1.1   gdamore 
    205  1.4    plunky 	syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
    206  1.1   gdamore 
    207  1.1   gdamore 	LIST_FOREACH(item, &item_list, next) {
    208  1.1   gdamore 		if (bdaddr_same(&rp.laddr, &item->laddr) == 0
    209  1.1   gdamore 		    || bdaddr_same(&rp.raddr, &item->raddr) == 0)
    210  1.1   gdamore 			continue;
    211  1.1   gdamore 
    212  1.1   gdamore 		evtimer_del(&item->ev);
    213  1.1   gdamore 		if (item->hci != -1) {
    214  1.1   gdamore 			memset(&sa, 0, sizeof(sa));
    215  1.1   gdamore 			sa.bt_len = sizeof(sa);
    216  1.1   gdamore 			sa.bt_family = AF_BLUETOOTH;
    217  1.1   gdamore 			bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
    218  1.1   gdamore 
    219  1.1   gdamore 			send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
    220  1.4    plunky 			LIST_REMOVE(item, next);
    221  1.4    plunky 			free(item);
    222  1.4    plunky 			return;
    223  1.1   gdamore 		}
    224  1.1   gdamore 		goto newpin;
    225  1.1   gdamore 	}
    226  1.1   gdamore 
    227  1.1   gdamore 	item = malloc(sizeof(struct item));
    228  1.1   gdamore 	if (item == NULL) {
    229  1.1   gdamore 		syslog(LOG_ERR, "Item allocation failed");
    230  1.1   gdamore 		return;
    231  1.1   gdamore 	}
    232  1.1   gdamore 
    233  1.1   gdamore 	memset(item, 0, sizeof(struct item));
    234  1.1   gdamore 	bdaddr_copy(&item->laddr, &rp.laddr);
    235  1.1   gdamore 	bdaddr_copy(&item->raddr, &rp.raddr);
    236  1.1   gdamore 	evtimer_set(&item->ev, process_item, item);
    237  1.1   gdamore 	LIST_INSERT_HEAD(&item_list, item, next);
    238  1.1   gdamore 
    239  1.1   gdamore newpin:
    240  1.4    plunky 	syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
    241  1.4    plunky 
    242  1.1   gdamore 	memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
    243  1.1   gdamore 	item->hci = -1;
    244  1.1   gdamore 
    245  1.1   gdamore 	tv.tv_sec = PIN_TIMEOUT;
    246  1.1   gdamore 	tv.tv_usec = 0;
    247  1.1   gdamore 
    248  1.1   gdamore 	if (evtimer_add(&item->ev, &tv) < 0) {
    249  1.1   gdamore 		syslog(LOG_ERR, "Cannot add event timer for item");
    250  1.1   gdamore 		LIST_REMOVE(item, next);
    251  1.1   gdamore 		free(item);
    252  1.1   gdamore 	}
    253  1.1   gdamore }
    254  1.1   gdamore 
    255  1.1   gdamore /* Send PIN request to client */
    256  1.1   gdamore int
    257  1.1   gdamore send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
    258  1.1   gdamore {
    259  1.3    plunky 	bthcid_pin_request_t	 cp;
    260  1.1   gdamore 	struct client		*cl;
    261  1.1   gdamore 	struct item		*item;
    262  1.1   gdamore 	int			 n = 0;
    263  1.1   gdamore 	struct timeval		 tv;
    264  1.1   gdamore 
    265  1.1   gdamore 	memset(&cp, 0, sizeof(cp));
    266  1.1   gdamore 	bdaddr_copy(&cp.laddr, laddr);
    267  1.1   gdamore 	bdaddr_copy(&cp.raddr, raddr);
    268  1.1   gdamore 	cp.time = PIN_REQUEST_TIMEOUT;
    269  1.1   gdamore 
    270  1.1   gdamore 	LIST_FOREACH(cl, &client_list, next) {
    271  1.6   mlelstv 		if (send(cl->fd, &cp, sizeof(cp), MSG_NOSIGNAL) != sizeof(cp))
    272  1.1   gdamore 			syslog(LOG_ERR, "send PIN request failed");
    273  1.1   gdamore 		else
    274  1.1   gdamore 			n++;
    275  1.1   gdamore 	}
    276  1.1   gdamore 
    277  1.1   gdamore 	if (n == 0)
    278  1.1   gdamore 		return 0;
    279  1.1   gdamore 
    280  1.1   gdamore 	syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
    281  1.1   gdamore 				n, (n == 1 ? "" : "s"));
    282  1.1   gdamore 
    283  1.1   gdamore 	item = malloc(sizeof(struct item));
    284  1.1   gdamore 	if (item == NULL) {
    285  1.1   gdamore 		syslog(LOG_ERR, "Cannot allocate PIN request item");
    286  1.1   gdamore 		return 0;
    287  1.1   gdamore 	}
    288  1.1   gdamore 
    289  1.1   gdamore 	memset(item, 0, sizeof(struct item));
    290  1.1   gdamore 	bdaddr_copy(&item->laddr, laddr);
    291  1.1   gdamore 	bdaddr_copy(&item->raddr, raddr);
    292  1.1   gdamore 	item->hci = hci;
    293  1.1   gdamore 	evtimer_set(&item->ev, process_item, item);
    294  1.1   gdamore 
    295  1.1   gdamore 	tv.tv_sec = cp.time;
    296  1.1   gdamore 	tv.tv_usec = 0;
    297  1.1   gdamore 
    298  1.1   gdamore 	if (evtimer_add(&item->ev, &tv) < 0) {
    299  1.1   gdamore 		syslog(LOG_ERR, "Cannot add request timer");
    300  1.1   gdamore 		free(item);
    301  1.1   gdamore 		return 0;
    302  1.1   gdamore 	}
    303  1.1   gdamore 
    304  1.1   gdamore 	LIST_INSERT_HEAD(&item_list, item, next);
    305  1.1   gdamore 	return 1;
    306  1.1   gdamore }
    307  1.1   gdamore 
    308  1.1   gdamore /* Process item event (by expiring it) */
    309  1.1   gdamore static void
    310  1.1   gdamore process_item(int fd, short ev, void *arg)
    311  1.1   gdamore {
    312  1.1   gdamore 	struct item *item = arg;
    313  1.1   gdamore 
    314  1.1   gdamore 	syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
    315  1.1   gdamore 	LIST_REMOVE(item, next);
    316  1.4    plunky 	evtimer_del(&item->ev);
    317  1.1   gdamore 	free(item);
    318  1.1   gdamore }
    319  1.1   gdamore 
    320  1.1   gdamore /* lookup PIN in item cache */
    321  1.1   gdamore uint8_t *
    322  1.2      tron lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
    323  1.1   gdamore {
    324  1.4    plunky 	static uint8_t pin[HCI_PIN_SIZE];
    325  1.1   gdamore 	struct item *item;
    326  1.1   gdamore 
    327  1.1   gdamore 	LIST_FOREACH(item, &item_list, next) {
    328  1.1   gdamore 		if (bdaddr_same(raddr, &item->raddr) == 0)
    329  1.1   gdamore 			continue;
    330  1.1   gdamore 
    331  1.1   gdamore 		if (bdaddr_same(laddr, &item->laddr) == 0
    332  1.1   gdamore 		    && bdaddr_any(&item->laddr) == 0)
    333  1.1   gdamore 			continue;
    334  1.1   gdamore 
    335  1.1   gdamore 		if (item->hci >= 0)
    336  1.1   gdamore 			break;
    337  1.1   gdamore 
    338  1.1   gdamore 		syslog(LOG_DEBUG, "Matched PIN from cache");
    339  1.4    plunky 		memcpy(pin, item->pin, sizeof(pin));
    340  1.4    plunky 
    341  1.4    plunky 		LIST_REMOVE(item, next);
    342  1.4    plunky 		evtimer_del(&item->ev);
    343  1.4    plunky 		free(item);
    344  1.4    plunky 
    345  1.4    plunky 		return pin;
    346  1.1   gdamore 	}
    347  1.1   gdamore 
    348  1.1   gdamore 	return NULL;
    349  1.1   gdamore }
    350