• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2007  Nokia Corporation
6  *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #define _GNU_SOURCE
30 #include <stdio.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 
39 #include <bluetooth/bluetooth.h>
40 #include <bluetooth/hci.h>
41 #include <bluetooth/hci_lib.h>
42 #include <bluetooth/sdp.h>
43 
44 #include <glib.h>
45 #include <dbus/dbus.h>
46 #include <gdbus.h>
47 
48 #include "logging.h"
49 #include "textfile.h"
50 
51 #include "hcid.h"
52 #include "manager.h"
53 #include "adapter.h"
54 #include "device.h"
55 #include "error.h"
56 #include "glib-helper.h"
57 #include "dbus-common.h"
58 #include "agent.h"
59 #include "storage.h"
60 #include "dbus-hci.h"
61 
62 static DBusConnection *connection = NULL;
63 
get_adapter_and_device(bdaddr_t * src,bdaddr_t * dst,struct btd_adapter ** adapter,struct btd_device ** device,gboolean create)64 static gboolean get_adapter_and_device(bdaddr_t *src, bdaddr_t *dst,
65 					struct btd_adapter **adapter,
66 					struct btd_device **device,
67 					gboolean create)
68 {
69 	char peer_addr[18];
70 
71 	*adapter = manager_find_adapter(src);
72 	if (!*adapter) {
73 		error("Unable to find matching adapter");
74 		return FALSE;
75 	}
76 
77 	ba2str(dst, peer_addr);
78 
79 	if (create)
80 		*device = adapter_get_device(connection, *adapter, peer_addr);
81 	else
82 		*device = adapter_find_device(*adapter, peer_addr);
83 
84 	if (create && !*device) {
85 		error("Unable to get device object!");
86 		return FALSE;
87 	}
88 
89 	return TRUE;
90 }
91 
class_to_icon(uint32_t class)92 const char *class_to_icon(uint32_t class)
93 {
94 	switch ((class & 0x1f00) >> 8) {
95 	case 0x01:
96 		return "computer";
97 	case 0x02:
98 		switch ((class & 0xfc) >> 2) {
99 		case 0x01:
100 		case 0x02:
101 		case 0x03:
102 		case 0x05:
103 			return "phone";
104 		case 0x04:
105 			return "modem";
106 		}
107 		break;
108 	case 0x03:
109 		return "network-wireless";
110 	case 0x04:
111 		switch ((class & 0xfc) >> 2) {
112 		case 0x01:
113 		case 0x02:
114 			return "audio-card";	/* Headset */
115 		case 0x06:
116 			return "audio-card";	/* Headphone */
117 		default:
118 			return "audio-card";	/* Other audio device */
119 		}
120 		break;
121 	case 0x05:
122 		switch ((class & 0xc0) >> 6) {
123 		case 0x00:
124 			switch ((class & 0x1e) >> 2) {
125 			case 0x01:
126 			case 0x02:
127 				return "input-gaming";
128 			}
129 			break;
130 		case 0x01:
131 			return "input-keyboard";
132 		case 0x02:
133 			switch ((class & 0x1e) >> 2) {
134 			case 0x05:
135 				return "input-tablet";
136 			default:
137 				return "input-mouse";
138 			}
139 		}
140 		break;
141 	case 0x06:
142 		if (class & 0x80)
143 			return "printer";
144 		if (class & 0x20)
145 			return "camera-photo";
146 		break;
147 	}
148 
149 	return NULL;
150 }
151 
152 /*****************************************************************
153  *
154  *  Section reserved to HCI commands confirmation handling and low
155  *  level events(eg: device attached/dettached.
156  *
157  *****************************************************************/
158 
pincode_cb(struct agent * agent,DBusError * err,const char * pincode,struct btd_device * device)159 static void pincode_cb(struct agent *agent, DBusError *err, const char *pincode,
160 			struct btd_device *device)
161 {
162 	struct btd_adapter *adapter = device_get_adapter(device);
163 	pin_code_reply_cp pr;
164 	bdaddr_t sba, dba;
165 	size_t len;
166 	int dev;
167 	uint16_t dev_id = adapter_get_dev_id(adapter);
168 
169 	dev = hci_open_dev(dev_id);
170 	if (dev < 0) {
171 		error("hci_open_dev(%d): %s (%d)", dev_id,
172 				strerror(errno), errno);
173 		return;
174 	}
175 
176 	adapter_get_address(adapter, &sba);
177 	device_get_address(device, &dba);
178 
179 	if (err) {
180 		hci_send_cmd(dev, OGF_LINK_CTL,
181 				OCF_PIN_CODE_NEG_REPLY, 6, &dba);
182 		goto done;
183 	}
184 
185 	len = strlen(pincode);
186 
187 	set_pin_length(&sba, len);
188 
189 	memset(&pr, 0, sizeof(pr));
190 	bacpy(&pr.bdaddr, &dba);
191 	memcpy(pr.pin_code, pincode, len);
192 	pr.pin_len = len;
193 	hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY,
194 						PIN_CODE_REPLY_CP_SIZE, &pr);
195 
196 done:
197 	hci_close_dev(dev);
198 }
199 
hcid_dbus_request_pin(int dev,bdaddr_t * sba,struct hci_conn_info * ci)200 int hcid_dbus_request_pin(int dev, bdaddr_t *sba, struct hci_conn_info *ci)
201 {
202 	struct btd_adapter *adapter;
203 	struct btd_device *device;
204 
205 	if (!get_adapter_and_device(sba, &ci->bdaddr, &adapter, &device, TRUE))
206 		return -ENODEV;
207 
208 	/* Check if the adapter is not pairable and if there isn't a bonding in
209 	 * progress */
210 	if (!adapter_is_pairable(adapter) && !device_is_bonding(device, NULL))
211 		return -EPERM;
212 
213 	return device_request_authentication(device, AUTH_TYPE_PINCODE, 0,
214 								pincode_cb);
215 }
216 
confirm_cb(struct agent * agent,DBusError * err,void * user_data)217 static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
218 {
219 	struct btd_device *device = user_data;
220 	struct btd_adapter *adapter = device_get_adapter(device);
221 	user_confirm_reply_cp cp;
222 	int dd;
223 	uint16_t dev_id = adapter_get_dev_id(adapter);
224 
225 	dd = hci_open_dev(dev_id);
226 	if (dd < 0) {
227 		error("Unable to open hci%d", dev_id);
228 		return;
229 	}
230 
231 	memset(&cp, 0, sizeof(cp));
232 	device_get_address(device, &cp.bdaddr);
233 
234 	if (err)
235 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
236 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
237 	else
238 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_REPLY,
239 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
240 
241 	hci_close_dev(dd);
242 }
243 
passkey_cb(struct agent * agent,DBusError * err,uint32_t passkey,void * user_data)244 static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
245 			void *user_data)
246 {
247 	struct btd_device *device = user_data;
248 	struct btd_adapter *adapter = device_get_adapter(device);
249 	user_passkey_reply_cp cp;
250 	bdaddr_t dba;
251 	int dd;
252 	uint16_t dev_id = adapter_get_dev_id(adapter);
253 
254 	dd = hci_open_dev(dev_id);
255 	if (dd < 0) {
256 		error("Unable to open hci%d", dev_id);
257 		return;
258 	}
259 
260 	device_get_address(device, &dba);
261 
262 	memset(&cp, 0, sizeof(cp));
263 	bacpy(&cp.bdaddr, &dba);
264 	cp.passkey = passkey;
265 
266 	if (err)
267 		hci_send_cmd(dd, OGF_LINK_CTL,
268 				OCF_USER_PASSKEY_NEG_REPLY, 6, &dba);
269 	else
270 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_REPLY,
271 					USER_PASSKEY_REPLY_CP_SIZE, &cp);
272 
273 	hci_close_dev(dd);
274 }
275 
pairing_consent_cb(struct agent * agent,DBusError * err,void * user_data)276 static void pairing_consent_cb(struct agent *agent, DBusError *err,
277 					void *user_data)
278 {
279 	struct btd_device *device = user_data;
280 	struct btd_adapter *adapter = device_get_adapter(device);
281 	user_confirm_reply_cp cp;
282 	int dd;
283 	uint16_t dev_id = adapter_get_dev_id(adapter);
284 
285 	dd = hci_open_dev(dev_id);
286 	if (dd < 0) {
287 		error("Unable to open hci%d", dev_id);
288 		return;
289 	}
290 
291 	memset(&cp, 0, sizeof(cp));
292 	device_get_address(device, &cp.bdaddr);
293 
294 	if (err)
295 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
296 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
297 	else
298 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_REPLY,
299 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
300 
301 	hci_close_dev(dd);
302 }
303 
get_auth_requirements(bdaddr_t * local,bdaddr_t * remote,uint8_t * auth)304 static int get_auth_requirements(bdaddr_t *local, bdaddr_t *remote,
305 							uint8_t *auth)
306 {
307 	struct hci_auth_info_req req;
308 	char addr[18];
309 	int err, dd, dev_id;
310 
311 	ba2str(local, addr);
312 
313 	dev_id = hci_devid(addr);
314 	if (dev_id < 0)
315 		return dev_id;
316 
317 	dd = hci_open_dev(dev_id);
318 	if (dd < 0)
319 		return dd;
320 
321 	memset(&req, 0, sizeof(req));
322 	bacpy(&req.bdaddr, remote);
323 
324 	err = ioctl(dd, HCIGETAUTHINFO, (unsigned long) &req);
325 	if (err < 0) {
326 		debug("HCIGETAUTHINFO failed: %s (%d)",
327 					strerror(errno), errno);
328 		hci_close_dev(dd);
329 		return err;
330 	}
331 
332 	hci_close_dev(dd);
333 
334 	if (auth)
335 		*auth = req.type;
336 
337 	return 0;
338 }
339 
hcid_dbus_user_confirm(bdaddr_t * sba,bdaddr_t * dba,uint32_t passkey)340 int hcid_dbus_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
341 {
342 	struct btd_adapter *adapter;
343 	struct btd_device *device;
344 	uint8_t remcap, remauth, type;
345 	uint16_t dev_id;
346 
347 	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
348 		return -ENODEV;
349 
350 	dev_id = adapter_get_dev_id(adapter);
351 
352 	if (get_auth_requirements(sba, dba, &type) < 0) {
353 		int dd;
354 
355 		dd = hci_open_dev(dev_id);
356 		if (dd < 0) {
357 			error("Unable to open hci%d", dev_id);
358 			return -1;
359 		}
360 
361 		hci_send_cmd(dd, OGF_LINK_CTL,
362 					OCF_USER_CONFIRM_NEG_REPLY, 6, dba);
363 
364 		hci_close_dev(dd);
365 
366 		return 0;
367 	}
368 
369 	debug("confirm authentication requirement is 0x%02x", type);
370 
371 	remcap = device_get_cap(device);
372 	remauth = device_get_auth(device);
373 
374 	debug("remote IO capabilities are 0x%02x", remcap);
375 	debug("remote authentication requirement is 0x%02x", remauth);
376 
377 	/* If local IO capabilities are DisplayYesNo and remote IO
378 	 * capabiltiies are DisplayOnly or NoInputNoOutput;
379 	 * call PairingConsent callback for incoming requests. */
380 	struct agent *agent = NULL;
381 	agent = device_get_agent(device);
382 	if (!agent) {
383 		agent = adapter_get_agent(adapter);
384 		if ((agent_get_io_capability(agent) & 0x01) &&
385 		            (remcap == 0x00 || remcap == 0x03))
386 			return device_request_authentication(device,
387 					AUTH_TYPE_PAIRING_CONSENT, 0,
388 					pairing_consent_cb);
389 	}
390 
391 	/* If no side requires MITM protection; auto-accept */
392 	if (!(remauth & 0x01) &&
393 			(type == 0xff || !(type & 0x01) || remcap == 0x03)) {
394 		int dd;
395 
396 		/* Wait 5 milliseconds before doing auto-accept */
397 		usleep(5000);
398 
399 		dd = hci_open_dev(dev_id);
400 		if (dd < 0) {
401 			error("Unable to open hci%d", dev_id);
402 			return -1;
403 		}
404 
405 		hci_send_cmd(dd, OGF_LINK_CTL,
406 					OCF_USER_CONFIRM_REPLY, 6, dba);
407 
408 		hci_close_dev(dd);
409 
410 		debug("auto accept of confirmation");
411 
412 		return device_request_authentication(device,
413 						AUTH_TYPE_AUTO, 0, NULL);
414 	}
415 
416 	return device_request_authentication(device, AUTH_TYPE_CONFIRM,
417 							passkey, confirm_cb);
418 }
419 
hcid_dbus_user_passkey(bdaddr_t * sba,bdaddr_t * dba)420 int hcid_dbus_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
421 {
422 	struct btd_adapter *adapter;
423 	struct btd_device *device;
424 
425 	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
426 		return -ENODEV;
427 
428 	return device_request_authentication(device, AUTH_TYPE_PASSKEY, 0,
429 								passkey_cb);
430 }
431 
hcid_dbus_user_notify(bdaddr_t * sba,bdaddr_t * dba,uint32_t passkey)432 int hcid_dbus_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
433 {
434 	struct btd_adapter *adapter;
435 	struct btd_device *device;
436 
437 	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
438 		return -ENODEV;
439 
440 	return device_request_authentication(device, AUTH_TYPE_NOTIFY,
441 								passkey, NULL);
442 }
443 
hcid_dbus_bonding_process_complete(bdaddr_t * local,bdaddr_t * peer,uint8_t status)444 void hcid_dbus_bonding_process_complete(bdaddr_t *local, bdaddr_t *peer,
445 								uint8_t status)
446 {
447 	struct btd_adapter *adapter;
448 	struct btd_device *device;
449 
450 	debug("hcid_dbus_bonding_process_complete: status=%02x", status);
451 
452 	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
453 		return;
454 
455 	if (!device_is_authenticating(device)) {
456 		/* This means that there was no pending PIN or SSP token
457 		 * request from the controller, i.e. this is not a new
458 		 * pairing */
459 		debug("hcid_dbus_bonding_process_complete: no pending auth request");
460 		return;
461 	}
462 
463 	/* If this is a new pairing send the appropriate reply and signal for
464 	 * it and proceed with service discovery */
465 	device_bonding_complete(device, status);
466 }
467 
hcid_dbus_simple_pairing_complete(bdaddr_t * local,bdaddr_t * peer,uint8_t status)468 void hcid_dbus_simple_pairing_complete(bdaddr_t *local, bdaddr_t *peer,
469 					uint8_t status)
470 {
471 	struct btd_adapter *adapter;
472 	struct btd_device *device;
473 
474 	debug("hcid_dbus_simple_pairing_complete: status=%02x", status);
475 
476 	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
477 		return;
478 
479 	device_simple_pairing_complete(device, status);
480 }
481 
extract_eir_name(uint8_t * data,uint8_t * type)482 static char *extract_eir_name(uint8_t *data, uint8_t *type)
483 {
484 	if (!data || !type)
485 		return NULL;
486 
487 	if (data[0] == 0)
488 		return NULL;
489 
490 	*type = data[1];
491 
492 	switch (*type) {
493 	case 0x08:
494 	case 0x09:
495 		return strndup((char *) (data + 2), data[0] - 1);
496 	}
497 
498 	return NULL;
499 }
500 
hcid_dbus_inquiry_result(bdaddr_t * local,bdaddr_t * peer,uint32_t class,int8_t rssi,uint8_t * data)501 void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class,
502 				int8_t rssi, uint8_t *data)
503 {
504 	char filename[PATH_MAX + 1];
505 	struct btd_adapter *adapter;
506 	struct btd_device *device;
507 	char local_addr[18], peer_addr[18], *alias, *name, *tmp_name;
508 	struct remote_dev_info *dev, match;
509 	uint8_t name_type = 0x00;
510 	name_status_t name_status;
511 	int state;
512 	dbus_bool_t legacy;
513 
514 	ba2str(local, local_addr);
515 	ba2str(peer, peer_addr);
516 
517 	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE)) {
518 		error("No matching adapter found");
519 		return;
520 	}
521 
522 	write_remote_class(local, peer, class);
523 
524 	if (data)
525 		write_remote_eir(local, peer, data);
526 
527 	/*
528 	 * workaround to identify situation when the daemon started and
529 	 * a standard inquiry or periodic inquiry was already running
530 	 */
531 	if (!(adapter_get_state(adapter) & STD_INQUIRY) &&
532 			!(adapter_get_state(adapter) & PERIODIC_INQUIRY)) {
533 		state = adapter_get_state(adapter);
534 		state |= PERIODIC_INQUIRY;
535 		adapter_set_state(adapter, state);
536 	}
537 
538 	legacy = (data == NULL);
539 
540 	memset(&match, 0, sizeof(struct remote_dev_info));
541 	bacpy(&match.bdaddr, peer);
542 	match.name_status = NAME_SENT;
543 	/* if found: don't send the name again */
544 	dev = adapter_search_found_devices(adapter, &match);
545 	if (dev) {
546 		adapter_update_found_devices(adapter, peer, rssi, class,
547 						NULL, NULL, legacy,
548 						NAME_NOT_REQUIRED);
549 		return;
550 	}
551 
552 	/* the inquiry result can be triggered by NON D-Bus client */
553 	if (adapter_get_state(adapter) & RESOLVE_NAME)
554 		name_status = NAME_REQUIRED;
555 	else
556 		name_status = NAME_NOT_REQUIRED;
557 
558 	create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "aliases");
559 	alias = textfile_get(filename, peer_addr);
560 
561 	create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "names");
562 	name = textfile_get(filename, peer_addr);
563 
564 	tmp_name = extract_eir_name(data, &name_type);
565 	if (tmp_name) {
566 		if (name_type == 0x09) {
567 			write_device_name(local, peer, tmp_name);
568 			name_status = NAME_NOT_REQUIRED;
569 
570 			if (name)
571 				g_free(name);
572 
573 			name = tmp_name;
574 		} else {
575 			if (name)
576 				free(tmp_name);
577 			else
578 				name = tmp_name;
579 		}
580 	}
581 
582 
583 	if (name && name_type != 0x08)
584 		name_status = NAME_SENT;
585 
586 	/* add in the list to track name sent/pending */
587 	adapter_update_found_devices(adapter, peer, rssi, class, name, alias,
588 					legacy, name_status);
589 
590 	g_free(name);
591 	g_free(alias);
592 }
593 
hcid_dbus_remote_class(bdaddr_t * local,bdaddr_t * peer,uint32_t class)594 void hcid_dbus_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
595 {
596 	uint32_t old_class = 0;
597 	struct btd_adapter *adapter;
598 	struct btd_device *device;
599 	const gchar *dev_path;
600 
601 	read_remote_class(local, peer, &old_class);
602 
603 	if (old_class == class)
604 		return;
605 
606 	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
607 		return;
608 
609 	if (!device)
610 		return;
611 
612 	dev_path = device_get_path(device);
613 
614 	emit_property_changed(connection, dev_path, DEVICE_INTERFACE, "Class",
615 				DBUS_TYPE_UINT32, &class);
616 }
617 
hcid_dbus_remote_name(bdaddr_t * local,bdaddr_t * peer,uint8_t status,char * name)618 void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status,
619 				char *name)
620 {
621 	struct btd_adapter *adapter;
622 	char srcaddr[18], dstaddr[18];
623 	int state;
624 	struct btd_device *device;
625 	struct remote_dev_info match, *dev_info;
626 
627 	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
628 		return;
629 
630 	ba2str(local, srcaddr);
631 	ba2str(peer, dstaddr);
632 
633 	if (status != 0)
634 		goto proceed;
635 
636 	bacpy(&match.bdaddr, peer);
637 	match.name_status = NAME_ANY;
638 
639 	dev_info = adapter_search_found_devices(adapter, &match);
640 	if (dev_info) {
641 		g_free(dev_info->name);
642 		dev_info->name = g_strdup(name);
643 		adapter_emit_device_found(adapter, dev_info);
644 	}
645 
646 	if (device)
647 		device_set_name(device, name);
648 
649 proceed:
650 	/* remove from remote name request list */
651 	adapter_remove_found_device(adapter, peer);
652 
653 	/* check if there is more devices to request names */
654 	if (adapter_resolve_names(adapter) == 0)
655 		return;
656 
657 	state = adapter_get_state(adapter);
658 	state &= ~PERIODIC_INQUIRY;
659 	state &= ~STD_INQUIRY;
660 	adapter_set_state(adapter, state);
661 }
662 
hcid_dbus_link_key_notify(bdaddr_t * local,bdaddr_t * peer,uint8_t * key,uint8_t key_type,int pin_length,uint8_t old_key_type)663 int hcid_dbus_link_key_notify(bdaddr_t *local, bdaddr_t *peer,
664 				uint8_t *key, uint8_t key_type,
665 				int pin_length, uint8_t old_key_type)
666 {
667 	struct btd_device *device;
668 	struct btd_adapter *adapter;
669 	uint8_t local_auth = 0xff, remote_auth, new_key_type;
670 	gboolean bonding;
671 
672 	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
673 		return -ENODEV;
674 
675 	if (key_type == 0x06 && old_key_type != 0xff)
676 		new_key_type = old_key_type;
677 	else
678 		new_key_type = key_type;
679 
680 	get_auth_requirements(local, peer, &local_auth);
681 	remote_auth = device_get_auth(device);
682 	bonding = device_is_bonding(device, NULL);
683 
684 	debug("local auth 0x%02x and remote auth 0x%02x",
685 					local_auth, remote_auth);
686 
687 	/* Only store the link key if one of the following is true:
688 	 * 1. this is a legacy link key
689 	 * 2. this is a changed combination key and there was a previously
690 	 *    stored one
691 	 * 3. neither local nor remote side had no-bonding as a requirement
692 	 * 4. the local side had dedicated bonding as a requirement
693 	 * 5. the remote side is using dedicated bonding since in that case
694 	 *    also the local requirements are set to dedicated bonding
695 	 */
696 	if (key_type < 0x03 || (key_type == 0x06 && old_key_type != 0xff) ||
697 				(local_auth > 0x01 && remote_auth > 0x01) ||
698 				(local_auth == 0x02 || local_auth == 0x03) ||
699 				(remote_auth == 0x02 || remote_auth == 0x03)) {
700 		int err;
701 
702 		debug("storing link key of type 0x%02x", key_type);
703 
704 		err = write_link_key(local, peer, key, new_key_type,
705 								pin_length);
706 		if (err < 0) {
707 			error("write_link_key: %s (%d)", strerror(-err), -err);
708 			return err;
709 		}
710 	}
711 
712 	/* If this is not the first link key set a flag so a subsequent auth
713 	 * complete event doesn't trigger SDP */
714 	if (old_key_type != 0xff)
715 		device_set_renewed_key(device, TRUE);
716 
717 	if (!device_is_connected(device))
718 		device_set_secmode3_conn(device, TRUE);
719 	else if (!bonding && old_key_type == 0xff)
720 		hcid_dbus_bonding_process_complete(local, peer, 0);
721 
722 	return 0;
723 }
724 
hcid_dbus_conn_complete(bdaddr_t * local,uint8_t status,uint16_t handle,bdaddr_t * peer)725 void hcid_dbus_conn_complete(bdaddr_t *local, uint8_t status, uint16_t handle,
726 				bdaddr_t *peer)
727 {
728 	struct btd_adapter *adapter;
729 	struct btd_device *device;
730 
731 	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
732 		return;
733 
734 	if (status) {
735 		device_set_secmode3_conn(device, FALSE);
736 		if (device_is_bonding(device, NULL))
737 			device_bonding_complete(device, status);
738 		if (device_is_temporary(device))
739 			adapter_remove_device(connection, adapter, device);
740 		return;
741 	}
742 
743 	/* add in the device connetions list */
744 	adapter_add_connection(adapter, device, handle);
745 }
746 
hcid_dbus_disconn_complete(bdaddr_t * local,uint8_t status,uint16_t handle,uint8_t reason)747 void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status,
748 				uint16_t handle, uint8_t reason)
749 {
750 	struct btd_adapter *adapter;
751 	struct btd_device *device;
752 
753 	if (status) {
754 		error("Disconnection failed: 0x%02x", status);
755 		return;
756 	}
757 
758 	adapter = manager_find_adapter(local);
759 	if (!adapter) {
760 		error("No matching adapter found");
761 		return;
762 	}
763 
764 	device = adapter_find_connection(adapter, handle);
765 	if (!device) {
766 		error("No matching connection found for handle %u", handle);
767 		return;
768 	}
769 
770 	adapter_remove_connection(adapter, device, handle);
771 }
772 
773 /* Section reserved to device HCI callbacks */
774 
hcid_dbus_setscan_enable_complete(bdaddr_t * local)775 void hcid_dbus_setscan_enable_complete(bdaddr_t *local)
776 {
777 	struct btd_adapter *adapter;
778 	read_scan_enable_rp rp;
779 	struct hci_request rq;
780 	int dd = -1;
781 	uint16_t dev_id;
782 
783 	adapter = manager_find_adapter(local);
784 	if (!adapter) {
785 		error("No matching adapter found");
786 		return;
787 	}
788 
789 	if (adapter_powering_down(adapter))
790 		return;
791 
792 	dev_id = adapter_get_dev_id(adapter);
793 
794 	dd = hci_open_dev(dev_id);
795 	if (dd < 0) {
796 		error("HCI device open failed: hci%d", dev_id);
797 		return;
798 	}
799 
800 	memset(&rq, 0, sizeof(rq));
801 	rq.ogf    = OGF_HOST_CTL;
802 	rq.ocf    = OCF_READ_SCAN_ENABLE;
803 	rq.rparam = &rp;
804 	rq.rlen   = READ_SCAN_ENABLE_RP_SIZE;
805 	rq.event  = EVT_CMD_COMPLETE;
806 
807 	if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
808 		error("Sending read scan enable command failed: %s (%d)",
809 				strerror(errno), errno);
810 		goto failed;
811 	}
812 
813 	if (rp.status) {
814 		error("Getting scan enable failed with status 0x%02x",
815 				rp.status);
816 		goto failed;
817 	}
818 
819 	adapter_mode_changed(adapter, rp.enable);
820 
821 failed:
822 	if (dd >= 0)
823 		hci_close_dev(dd);
824 }
825 
hcid_dbus_write_class_complete(bdaddr_t * local)826 void hcid_dbus_write_class_complete(bdaddr_t *local)
827 {
828 	struct btd_adapter *adapter;
829 	int dd;
830 	uint8_t cls[3];
831 	uint16_t dev_id;
832 
833 	adapter = manager_find_adapter(local);
834 	if (!adapter) {
835 		error("No matching adapter found");
836 		return;
837 	}
838 
839 	dev_id = adapter_get_dev_id(adapter);
840 
841 	dd = hci_open_dev(dev_id);
842 	if (dd < 0) {
843 		error("HCI device open failed: hci%d", dev_id);
844 		return;
845 	}
846 
847 	if (hci_read_class_of_dev(dd, cls, HCI_REQ_TIMEOUT) < 0) {
848 		error("Can't read class of device on hci%d: %s (%d)",
849 			dev_id, strerror(errno), errno);
850 		hci_close_dev(dd);
851 		return;
852 	}
853 
854 	hci_close_dev(dd);
855 
856 	adapter_set_class(adapter, cls);
857 }
858 
hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t * local)859 void hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t *local)
860 {
861 	struct btd_adapter *adapter;
862 	int dd;
863 	uint8_t mode;
864 	uint16_t dev_id;
865 	const gchar *path;
866 
867 	adapter = manager_find_adapter(local);
868 	if (!adapter) {
869 		error("No matching adapter found");
870 		return;
871 	}
872 
873 	dev_id = adapter_get_dev_id(adapter);
874 	path = adapter_get_path(adapter);
875 
876 	dd = hci_open_dev(dev_id);
877 	if (dd < 0) {
878 		error("HCI adapter open failed: %s", path);
879 		return;
880 	}
881 
882 	if (hci_read_simple_pairing_mode(dd, &mode,
883 						HCI_REQ_TIMEOUT) < 0) {
884 		error("Can't read simple pairing mode for %s: %s(%d)",
885 					path, strerror(errno), errno);
886 		hci_close_dev(dd);
887 		return;
888 	}
889 
890 	hci_close_dev(dd);
891 
892 	adapter_update_ssp_mode(adapter, mode);
893 }
894 
hcid_dbus_get_io_cap(bdaddr_t * local,bdaddr_t * remote,uint8_t * cap,uint8_t * auth)895 int hcid_dbus_get_io_cap(bdaddr_t *local, bdaddr_t *remote,
896 						uint8_t *cap, uint8_t *auth)
897 {
898 	struct btd_adapter *adapter;
899 	struct btd_device *device;
900 	struct agent *agent = NULL;
901 
902 	if (!get_adapter_and_device(local, remote, &adapter, &device, TRUE))
903 		return -ENODEV;
904 
905 	if (get_auth_requirements(local, remote, auth) < 0)
906 		return -1;
907 
908 	debug("initial authentication requirement is 0x%02x", *auth);
909 
910 	if (*auth == 0xff)
911 		*auth = device_get_auth(device);
912 
913 	/* Check if the adapter is not pairable and if there isn't a bonding
914 	 * in progress */
915 	if (!adapter_is_pairable(adapter) &&
916 				!device_is_bonding(device, NULL)) {
917 		if (*auth < 0x02 && device_get_auth(device) < 0x02) {
918 			debug("Allowing no bonding in non-bondable mode");
919 			/* No input, no output */
920 			*cap = 0x03;
921 			goto done;
922 		}
923 		return -EPERM;
924 	}
925 
926 	/* For CreatePairedDevice use dedicated bonding */
927 	agent = device_get_agent(device);
928 	if (!agent)
929 		agent = adapter_get_agent(adapter);
930 
931 	if (!agent) {
932 		/* This is the non bondable mode case */
933 		if (device_get_auth(device) > 0x01) {
934 			debug("Bonding request, but no agent present");
935 			return -1;
936 		}
937 
938 		/* No agent available, and no bonding case */
939 		if (*auth == 0x00) {
940 			debug("Allowing no bonding without agent");
941 			/* No input, no output */
942 			*cap = 0x03;
943 			goto done;
944 		}
945 
946 		error("No agent available for IO capability");
947 		return -1;
948 	}
949 
950 	if (*auth == 0x00) {
951 		/* If remote requests dedicated bonding follow that lead */
952 		if (device_get_auth(device) == 0x02 ||
953 				device_get_auth(device) == 0x03) {
954 			uint8_t agent_cap = agent_get_io_capability(agent);
955 
956 			/* If both remote and local IO capabilities allow MITM
957 			 * then require it, otherwise don't */
958 			if (device_get_cap(device) == 0x03 ||
959 							agent_cap == 0x03)
960 				*auth = 0x02;
961 			else
962 				*auth = 0x03;
963 		}
964 
965 		/* If remote requires MITM then also require it */
966 		if (device_get_auth(device) != 0xff &&
967 					(device_get_auth(device) & 0x01))
968 			*auth |= 0x01;
969 	}
970 
971 	*cap = agent_get_io_capability(agent);
972 
973 done:
974 	debug("final authentication requirement is 0x%02x", *auth);
975 
976 	return 0;
977 }
978 
hcid_dbus_set_io_cap(bdaddr_t * local,bdaddr_t * remote,uint8_t cap,uint8_t auth)979 int hcid_dbus_set_io_cap(bdaddr_t *local, bdaddr_t *remote,
980 						uint8_t cap, uint8_t auth)
981 {
982 	struct btd_adapter *adapter;
983 	struct btd_device *device;
984 
985 	if (!get_adapter_and_device(local, remote, &adapter, &device, TRUE))
986 		return -ENODEV;
987 
988 	device_set_cap(device, cap);
989 	device_set_auth(device, auth);
990 
991 	return 0;
992 }
993 
994 /* Most of the functions in this module require easy access to a connection so
995  * we keep it global here and provide these access functions the other (few)
996  * modules that require access to it */
997 
set_dbus_connection(DBusConnection * conn)998 void set_dbus_connection(DBusConnection *conn)
999 {
1000 	connection = conn;
1001 }
1002 
get_dbus_connection(void)1003 DBusConnection *get_dbus_connection(void)
1004 {
1005 	return connection;
1006 }
1007